18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0 WITH Linux-syscall-note) OR MIT */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Documentation
68c2ecf20Sopenharmony_ci * =============
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * The below enums and macros are for interfacing with WireGuard, using generic
98c2ecf20Sopenharmony_ci * netlink, with family WG_GENL_NAME and version WG_GENL_VERSION. It defines two
108c2ecf20Sopenharmony_ci * methods: get and set. Note that while they share many common attributes,
118c2ecf20Sopenharmony_ci * these two functions actually accept a slightly different set of inputs and
128c2ecf20Sopenharmony_ci * outputs.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * WG_CMD_GET_DEVICE
158c2ecf20Sopenharmony_ci * -----------------
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * May only be called via NLM_F_REQUEST | NLM_F_DUMP. The command should contain
188c2ecf20Sopenharmony_ci * one but not both of:
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *    WGDEVICE_A_IFINDEX: NLA_U32
218c2ecf20Sopenharmony_ci *    WGDEVICE_A_IFNAME: NLA_NUL_STRING, maxlen IFNAMSIZ - 1
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * The kernel will then return several messages (NLM_F_MULTI) containing the
248c2ecf20Sopenharmony_ci * following tree of nested items:
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci *    WGDEVICE_A_IFINDEX: NLA_U32
278c2ecf20Sopenharmony_ci *    WGDEVICE_A_IFNAME: NLA_NUL_STRING, maxlen IFNAMSIZ - 1
288c2ecf20Sopenharmony_ci *    WGDEVICE_A_PRIVATE_KEY: NLA_EXACT_LEN, len WG_KEY_LEN
298c2ecf20Sopenharmony_ci *    WGDEVICE_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN
308c2ecf20Sopenharmony_ci *    WGDEVICE_A_LISTEN_PORT: NLA_U16
318c2ecf20Sopenharmony_ci *    WGDEVICE_A_FWMARK: NLA_U32
328c2ecf20Sopenharmony_ci *    WGDEVICE_A_PEERS: NLA_NESTED
338c2ecf20Sopenharmony_ci *        0: NLA_NESTED
348c2ecf20Sopenharmony_ci *            WGPEER_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN
358c2ecf20Sopenharmony_ci *            WGPEER_A_PRESHARED_KEY: NLA_EXACT_LEN, len WG_KEY_LEN
368c2ecf20Sopenharmony_ci *            WGPEER_A_ENDPOINT: NLA_MIN_LEN(struct sockaddr), struct sockaddr_in or struct sockaddr_in6
378c2ecf20Sopenharmony_ci *            WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL: NLA_U16
388c2ecf20Sopenharmony_ci *            WGPEER_A_LAST_HANDSHAKE_TIME: NLA_EXACT_LEN, struct __kernel_timespec
398c2ecf20Sopenharmony_ci *            WGPEER_A_RX_BYTES: NLA_U64
408c2ecf20Sopenharmony_ci *            WGPEER_A_TX_BYTES: NLA_U64
418c2ecf20Sopenharmony_ci *            WGPEER_A_ALLOWEDIPS: NLA_NESTED
428c2ecf20Sopenharmony_ci *                0: NLA_NESTED
438c2ecf20Sopenharmony_ci *                    WGALLOWEDIP_A_FAMILY: NLA_U16
448c2ecf20Sopenharmony_ci *                    WGALLOWEDIP_A_IPADDR: NLA_MIN_LEN(struct in_addr), struct in_addr or struct in6_addr
458c2ecf20Sopenharmony_ci *                    WGALLOWEDIP_A_CIDR_MASK: NLA_U8
468c2ecf20Sopenharmony_ci *                0: NLA_NESTED
478c2ecf20Sopenharmony_ci *                    ...
488c2ecf20Sopenharmony_ci *                0: NLA_NESTED
498c2ecf20Sopenharmony_ci *                    ...
508c2ecf20Sopenharmony_ci *                ...
518c2ecf20Sopenharmony_ci *            WGPEER_A_PROTOCOL_VERSION: NLA_U32
528c2ecf20Sopenharmony_ci *        0: NLA_NESTED
538c2ecf20Sopenharmony_ci *            ...
548c2ecf20Sopenharmony_ci *        ...
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci * It is possible that all of the allowed IPs of a single peer will not
578c2ecf20Sopenharmony_ci * fit within a single netlink message. In that case, the same peer will
588c2ecf20Sopenharmony_ci * be written in the following message, except it will only contain
598c2ecf20Sopenharmony_ci * WGPEER_A_PUBLIC_KEY and WGPEER_A_ALLOWEDIPS. This may occur several
608c2ecf20Sopenharmony_ci * times in a row for the same peer. It is then up to the receiver to
618c2ecf20Sopenharmony_ci * coalesce adjacent peers. Likewise, it is possible that all peers will
628c2ecf20Sopenharmony_ci * not fit within a single message. So, subsequent peers will be sent
638c2ecf20Sopenharmony_ci * in following messages, except those will only contain WGDEVICE_A_IFNAME
648c2ecf20Sopenharmony_ci * and WGDEVICE_A_PEERS. It is then up to the receiver to coalesce these
658c2ecf20Sopenharmony_ci * messages to form the complete list of peers.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * Since this is an NLA_F_DUMP command, the final message will always be
688c2ecf20Sopenharmony_ci * NLMSG_DONE, even if an error occurs. However, this NLMSG_DONE message
698c2ecf20Sopenharmony_ci * contains an integer error code. It is either zero or a negative error
708c2ecf20Sopenharmony_ci * code corresponding to the errno.
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci * WG_CMD_SET_DEVICE
738c2ecf20Sopenharmony_ci * -----------------
748c2ecf20Sopenharmony_ci *
758c2ecf20Sopenharmony_ci * May only be called via NLM_F_REQUEST. The command should contain the
768c2ecf20Sopenharmony_ci * following tree of nested items, containing one but not both of
778c2ecf20Sopenharmony_ci * WGDEVICE_A_IFINDEX and WGDEVICE_A_IFNAME:
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci *    WGDEVICE_A_IFINDEX: NLA_U32
808c2ecf20Sopenharmony_ci *    WGDEVICE_A_IFNAME: NLA_NUL_STRING, maxlen IFNAMSIZ - 1
818c2ecf20Sopenharmony_ci *    WGDEVICE_A_FLAGS: NLA_U32, 0 or WGDEVICE_F_REPLACE_PEERS if all current
828c2ecf20Sopenharmony_ci *                      peers should be removed prior to adding the list below.
838c2ecf20Sopenharmony_ci *    WGDEVICE_A_PRIVATE_KEY: len WG_KEY_LEN, all zeros to remove
848c2ecf20Sopenharmony_ci *    WGDEVICE_A_LISTEN_PORT: NLA_U16, 0 to choose randomly
858c2ecf20Sopenharmony_ci *    WGDEVICE_A_FWMARK: NLA_U32, 0 to disable
868c2ecf20Sopenharmony_ci *    WGDEVICE_A_PEERS: NLA_NESTED
878c2ecf20Sopenharmony_ci *        0: NLA_NESTED
888c2ecf20Sopenharmony_ci *            WGPEER_A_PUBLIC_KEY: len WG_KEY_LEN
898c2ecf20Sopenharmony_ci *            WGPEER_A_FLAGS: NLA_U32, 0 and/or WGPEER_F_REMOVE_ME if the
908c2ecf20Sopenharmony_ci *                            specified peer should not exist at the end of the
918c2ecf20Sopenharmony_ci *                            operation, rather than added/updated and/or
928c2ecf20Sopenharmony_ci *                            WGPEER_F_REPLACE_ALLOWEDIPS if all current allowed
938c2ecf20Sopenharmony_ci *                            IPs of this peer should be removed prior to adding
948c2ecf20Sopenharmony_ci *                            the list below and/or WGPEER_F_UPDATE_ONLY if the
958c2ecf20Sopenharmony_ci *                            peer should only be set if it already exists.
968c2ecf20Sopenharmony_ci *            WGPEER_A_PRESHARED_KEY: len WG_KEY_LEN, all zeros to remove
978c2ecf20Sopenharmony_ci *            WGPEER_A_ENDPOINT: struct sockaddr_in or struct sockaddr_in6
988c2ecf20Sopenharmony_ci *            WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL: NLA_U16, 0 to disable
998c2ecf20Sopenharmony_ci *            WGPEER_A_ALLOWEDIPS: NLA_NESTED
1008c2ecf20Sopenharmony_ci *                0: NLA_NESTED
1018c2ecf20Sopenharmony_ci *                    WGALLOWEDIP_A_FAMILY: NLA_U16
1028c2ecf20Sopenharmony_ci *                    WGALLOWEDIP_A_IPADDR: struct in_addr or struct in6_addr
1038c2ecf20Sopenharmony_ci *                    WGALLOWEDIP_A_CIDR_MASK: NLA_U8
1048c2ecf20Sopenharmony_ci *                0: NLA_NESTED
1058c2ecf20Sopenharmony_ci *                    ...
1068c2ecf20Sopenharmony_ci *                0: NLA_NESTED
1078c2ecf20Sopenharmony_ci *                    ...
1088c2ecf20Sopenharmony_ci *                ...
1098c2ecf20Sopenharmony_ci *            WGPEER_A_PROTOCOL_VERSION: NLA_U32, should not be set or used at
1108c2ecf20Sopenharmony_ci *                                       all by most users of this API, as the
1118c2ecf20Sopenharmony_ci *                                       most recent protocol will be used when
1128c2ecf20Sopenharmony_ci *                                       this is unset. Otherwise, must be set
1138c2ecf20Sopenharmony_ci *                                       to 1.
1148c2ecf20Sopenharmony_ci *        0: NLA_NESTED
1158c2ecf20Sopenharmony_ci *            ...
1168c2ecf20Sopenharmony_ci *        ...
1178c2ecf20Sopenharmony_ci *
1188c2ecf20Sopenharmony_ci * It is possible that the amount of configuration data exceeds that of
1198c2ecf20Sopenharmony_ci * the maximum message length accepted by the kernel. In that case, several
1208c2ecf20Sopenharmony_ci * messages should be sent one after another, with each successive one
1218c2ecf20Sopenharmony_ci * filling in information not contained in the prior. Note that if
1228c2ecf20Sopenharmony_ci * WGDEVICE_F_REPLACE_PEERS is specified in the first message, it probably
1238c2ecf20Sopenharmony_ci * should not be specified in fragments that come after, so that the list
1248c2ecf20Sopenharmony_ci * of peers is only cleared the first time but appended after. Likewise for
1258c2ecf20Sopenharmony_ci * peers, if WGPEER_F_REPLACE_ALLOWEDIPS is specified in the first message
1268c2ecf20Sopenharmony_ci * of a peer, it likely should not be specified in subsequent fragments.
1278c2ecf20Sopenharmony_ci *
1288c2ecf20Sopenharmony_ci * If an error occurs, NLMSG_ERROR will reply containing an errno.
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci#ifndef _WG_UAPI_WIREGUARD_H
1328c2ecf20Sopenharmony_ci#define _WG_UAPI_WIREGUARD_H
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define WG_GENL_NAME "wireguard"
1358c2ecf20Sopenharmony_ci#define WG_GENL_VERSION 1
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci#define WG_KEY_LEN 32
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cienum wg_cmd {
1408c2ecf20Sopenharmony_ci	WG_CMD_GET_DEVICE,
1418c2ecf20Sopenharmony_ci	WG_CMD_SET_DEVICE,
1428c2ecf20Sopenharmony_ci	__WG_CMD_MAX
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci#define WG_CMD_MAX (__WG_CMD_MAX - 1)
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cienum wgdevice_flag {
1478c2ecf20Sopenharmony_ci	WGDEVICE_F_REPLACE_PEERS = 1U << 0,
1488c2ecf20Sopenharmony_ci	__WGDEVICE_F_ALL = WGDEVICE_F_REPLACE_PEERS
1498c2ecf20Sopenharmony_ci};
1508c2ecf20Sopenharmony_cienum wgdevice_attribute {
1518c2ecf20Sopenharmony_ci	WGDEVICE_A_UNSPEC,
1528c2ecf20Sopenharmony_ci	WGDEVICE_A_IFINDEX,
1538c2ecf20Sopenharmony_ci	WGDEVICE_A_IFNAME,
1548c2ecf20Sopenharmony_ci	WGDEVICE_A_PRIVATE_KEY,
1558c2ecf20Sopenharmony_ci	WGDEVICE_A_PUBLIC_KEY,
1568c2ecf20Sopenharmony_ci	WGDEVICE_A_FLAGS,
1578c2ecf20Sopenharmony_ci	WGDEVICE_A_LISTEN_PORT,
1588c2ecf20Sopenharmony_ci	WGDEVICE_A_FWMARK,
1598c2ecf20Sopenharmony_ci	WGDEVICE_A_PEERS,
1608c2ecf20Sopenharmony_ci	__WGDEVICE_A_LAST
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci#define WGDEVICE_A_MAX (__WGDEVICE_A_LAST - 1)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cienum wgpeer_flag {
1658c2ecf20Sopenharmony_ci	WGPEER_F_REMOVE_ME = 1U << 0,
1668c2ecf20Sopenharmony_ci	WGPEER_F_REPLACE_ALLOWEDIPS = 1U << 1,
1678c2ecf20Sopenharmony_ci	WGPEER_F_UPDATE_ONLY = 1U << 2,
1688c2ecf20Sopenharmony_ci	__WGPEER_F_ALL = WGPEER_F_REMOVE_ME | WGPEER_F_REPLACE_ALLOWEDIPS |
1698c2ecf20Sopenharmony_ci			 WGPEER_F_UPDATE_ONLY
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_cienum wgpeer_attribute {
1728c2ecf20Sopenharmony_ci	WGPEER_A_UNSPEC,
1738c2ecf20Sopenharmony_ci	WGPEER_A_PUBLIC_KEY,
1748c2ecf20Sopenharmony_ci	WGPEER_A_PRESHARED_KEY,
1758c2ecf20Sopenharmony_ci	WGPEER_A_FLAGS,
1768c2ecf20Sopenharmony_ci	WGPEER_A_ENDPOINT,
1778c2ecf20Sopenharmony_ci	WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
1788c2ecf20Sopenharmony_ci	WGPEER_A_LAST_HANDSHAKE_TIME,
1798c2ecf20Sopenharmony_ci	WGPEER_A_RX_BYTES,
1808c2ecf20Sopenharmony_ci	WGPEER_A_TX_BYTES,
1818c2ecf20Sopenharmony_ci	WGPEER_A_ALLOWEDIPS,
1828c2ecf20Sopenharmony_ci	WGPEER_A_PROTOCOL_VERSION,
1838c2ecf20Sopenharmony_ci	__WGPEER_A_LAST
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_ci#define WGPEER_A_MAX (__WGPEER_A_LAST - 1)
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cienum wgallowedip_attribute {
1888c2ecf20Sopenharmony_ci	WGALLOWEDIP_A_UNSPEC,
1898c2ecf20Sopenharmony_ci	WGALLOWEDIP_A_FAMILY,
1908c2ecf20Sopenharmony_ci	WGALLOWEDIP_A_IPADDR,
1918c2ecf20Sopenharmony_ci	WGALLOWEDIP_A_CIDR_MASK,
1928c2ecf20Sopenharmony_ci	__WGALLOWEDIP_A_LAST
1938c2ecf20Sopenharmony_ci};
1948c2ecf20Sopenharmony_ci#define WGALLOWEDIP_A_MAX (__WGALLOWEDIP_A_LAST - 1)
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci#endif /* _WG_UAPI_WIREGUARD_H */
197