1*4882a593Smuzhiyun /* SPDX-License-Identifier: (GPL-2.0 WITH Linux-syscall-note) OR MIT */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Documentation 6*4882a593Smuzhiyun * ============= 7*4882a593Smuzhiyun * 8*4882a593Smuzhiyun * The below enums and macros are for interfacing with WireGuard, using generic 9*4882a593Smuzhiyun * netlink, with family WG_GENL_NAME and version WG_GENL_VERSION. It defines two 10*4882a593Smuzhiyun * methods: get and set. Note that while they share many common attributes, 11*4882a593Smuzhiyun * these two functions actually accept a slightly different set of inputs and 12*4882a593Smuzhiyun * outputs. 13*4882a593Smuzhiyun * 14*4882a593Smuzhiyun * WG_CMD_GET_DEVICE 15*4882a593Smuzhiyun * ----------------- 16*4882a593Smuzhiyun * 17*4882a593Smuzhiyun * May only be called via NLM_F_REQUEST | NLM_F_DUMP. The command should contain 18*4882a593Smuzhiyun * one but not both of: 19*4882a593Smuzhiyun * 20*4882a593Smuzhiyun * WGDEVICE_A_IFINDEX: NLA_U32 21*4882a593Smuzhiyun * WGDEVICE_A_IFNAME: NLA_NUL_STRING, maxlen IFNAMSIZ - 1 22*4882a593Smuzhiyun * 23*4882a593Smuzhiyun * The kernel will then return several messages (NLM_F_MULTI) containing the 24*4882a593Smuzhiyun * following tree of nested items: 25*4882a593Smuzhiyun * 26*4882a593Smuzhiyun * WGDEVICE_A_IFINDEX: NLA_U32 27*4882a593Smuzhiyun * WGDEVICE_A_IFNAME: NLA_NUL_STRING, maxlen IFNAMSIZ - 1 28*4882a593Smuzhiyun * WGDEVICE_A_PRIVATE_KEY: NLA_EXACT_LEN, len WG_KEY_LEN 29*4882a593Smuzhiyun * WGDEVICE_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN 30*4882a593Smuzhiyun * WGDEVICE_A_LISTEN_PORT: NLA_U16 31*4882a593Smuzhiyun * WGDEVICE_A_FWMARK: NLA_U32 32*4882a593Smuzhiyun * WGDEVICE_A_PEERS: NLA_NESTED 33*4882a593Smuzhiyun * 0: NLA_NESTED 34*4882a593Smuzhiyun * WGPEER_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN 35*4882a593Smuzhiyun * WGPEER_A_PRESHARED_KEY: NLA_EXACT_LEN, len WG_KEY_LEN 36*4882a593Smuzhiyun * WGPEER_A_ENDPOINT: NLA_MIN_LEN(struct sockaddr), struct sockaddr_in or struct sockaddr_in6 37*4882a593Smuzhiyun * WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL: NLA_U16 38*4882a593Smuzhiyun * WGPEER_A_LAST_HANDSHAKE_TIME: NLA_EXACT_LEN, struct __kernel_timespec 39*4882a593Smuzhiyun * WGPEER_A_RX_BYTES: NLA_U64 40*4882a593Smuzhiyun * WGPEER_A_TX_BYTES: NLA_U64 41*4882a593Smuzhiyun * WGPEER_A_ALLOWEDIPS: NLA_NESTED 42*4882a593Smuzhiyun * 0: NLA_NESTED 43*4882a593Smuzhiyun * WGALLOWEDIP_A_FAMILY: NLA_U16 44*4882a593Smuzhiyun * WGALLOWEDIP_A_IPADDR: NLA_MIN_LEN(struct in_addr), struct in_addr or struct in6_addr 45*4882a593Smuzhiyun * WGALLOWEDIP_A_CIDR_MASK: NLA_U8 46*4882a593Smuzhiyun * 0: NLA_NESTED 47*4882a593Smuzhiyun * ... 48*4882a593Smuzhiyun * 0: NLA_NESTED 49*4882a593Smuzhiyun * ... 50*4882a593Smuzhiyun * ... 51*4882a593Smuzhiyun * WGPEER_A_PROTOCOL_VERSION: NLA_U32 52*4882a593Smuzhiyun * 0: NLA_NESTED 53*4882a593Smuzhiyun * ... 54*4882a593Smuzhiyun * ... 55*4882a593Smuzhiyun * 56*4882a593Smuzhiyun * It is possible that all of the allowed IPs of a single peer will not 57*4882a593Smuzhiyun * fit within a single netlink message. In that case, the same peer will 58*4882a593Smuzhiyun * be written in the following message, except it will only contain 59*4882a593Smuzhiyun * WGPEER_A_PUBLIC_KEY and WGPEER_A_ALLOWEDIPS. This may occur several 60*4882a593Smuzhiyun * times in a row for the same peer. It is then up to the receiver to 61*4882a593Smuzhiyun * coalesce adjacent peers. Likewise, it is possible that all peers will 62*4882a593Smuzhiyun * not fit within a single message. So, subsequent peers will be sent 63*4882a593Smuzhiyun * in following messages, except those will only contain WGDEVICE_A_IFNAME 64*4882a593Smuzhiyun * and WGDEVICE_A_PEERS. It is then up to the receiver to coalesce these 65*4882a593Smuzhiyun * messages to form the complete list of peers. 66*4882a593Smuzhiyun * 67*4882a593Smuzhiyun * Since this is an NLA_F_DUMP command, the final message will always be 68*4882a593Smuzhiyun * NLMSG_DONE, even if an error occurs. However, this NLMSG_DONE message 69*4882a593Smuzhiyun * contains an integer error code. It is either zero or a negative error 70*4882a593Smuzhiyun * code corresponding to the errno. 71*4882a593Smuzhiyun * 72*4882a593Smuzhiyun * WG_CMD_SET_DEVICE 73*4882a593Smuzhiyun * ----------------- 74*4882a593Smuzhiyun * 75*4882a593Smuzhiyun * May only be called via NLM_F_REQUEST. The command should contain the 76*4882a593Smuzhiyun * following tree of nested items, containing one but not both of 77*4882a593Smuzhiyun * WGDEVICE_A_IFINDEX and WGDEVICE_A_IFNAME: 78*4882a593Smuzhiyun * 79*4882a593Smuzhiyun * WGDEVICE_A_IFINDEX: NLA_U32 80*4882a593Smuzhiyun * WGDEVICE_A_IFNAME: NLA_NUL_STRING, maxlen IFNAMSIZ - 1 81*4882a593Smuzhiyun * WGDEVICE_A_FLAGS: NLA_U32, 0 or WGDEVICE_F_REPLACE_PEERS if all current 82*4882a593Smuzhiyun * peers should be removed prior to adding the list below. 83*4882a593Smuzhiyun * WGDEVICE_A_PRIVATE_KEY: len WG_KEY_LEN, all zeros to remove 84*4882a593Smuzhiyun * WGDEVICE_A_LISTEN_PORT: NLA_U16, 0 to choose randomly 85*4882a593Smuzhiyun * WGDEVICE_A_FWMARK: NLA_U32, 0 to disable 86*4882a593Smuzhiyun * WGDEVICE_A_PEERS: NLA_NESTED 87*4882a593Smuzhiyun * 0: NLA_NESTED 88*4882a593Smuzhiyun * WGPEER_A_PUBLIC_KEY: len WG_KEY_LEN 89*4882a593Smuzhiyun * WGPEER_A_FLAGS: NLA_U32, 0 and/or WGPEER_F_REMOVE_ME if the 90*4882a593Smuzhiyun * specified peer should not exist at the end of the 91*4882a593Smuzhiyun * operation, rather than added/updated and/or 92*4882a593Smuzhiyun * WGPEER_F_REPLACE_ALLOWEDIPS if all current allowed 93*4882a593Smuzhiyun * IPs of this peer should be removed prior to adding 94*4882a593Smuzhiyun * the list below and/or WGPEER_F_UPDATE_ONLY if the 95*4882a593Smuzhiyun * peer should only be set if it already exists. 96*4882a593Smuzhiyun * WGPEER_A_PRESHARED_KEY: len WG_KEY_LEN, all zeros to remove 97*4882a593Smuzhiyun * WGPEER_A_ENDPOINT: struct sockaddr_in or struct sockaddr_in6 98*4882a593Smuzhiyun * WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL: NLA_U16, 0 to disable 99*4882a593Smuzhiyun * WGPEER_A_ALLOWEDIPS: NLA_NESTED 100*4882a593Smuzhiyun * 0: NLA_NESTED 101*4882a593Smuzhiyun * WGALLOWEDIP_A_FAMILY: NLA_U16 102*4882a593Smuzhiyun * WGALLOWEDIP_A_IPADDR: struct in_addr or struct in6_addr 103*4882a593Smuzhiyun * WGALLOWEDIP_A_CIDR_MASK: NLA_U8 104*4882a593Smuzhiyun * 0: NLA_NESTED 105*4882a593Smuzhiyun * ... 106*4882a593Smuzhiyun * 0: NLA_NESTED 107*4882a593Smuzhiyun * ... 108*4882a593Smuzhiyun * ... 109*4882a593Smuzhiyun * WGPEER_A_PROTOCOL_VERSION: NLA_U32, should not be set or used at 110*4882a593Smuzhiyun * all by most users of this API, as the 111*4882a593Smuzhiyun * most recent protocol will be used when 112*4882a593Smuzhiyun * this is unset. Otherwise, must be set 113*4882a593Smuzhiyun * to 1. 114*4882a593Smuzhiyun * 0: NLA_NESTED 115*4882a593Smuzhiyun * ... 116*4882a593Smuzhiyun * ... 117*4882a593Smuzhiyun * 118*4882a593Smuzhiyun * It is possible that the amount of configuration data exceeds that of 119*4882a593Smuzhiyun * the maximum message length accepted by the kernel. In that case, several 120*4882a593Smuzhiyun * messages should be sent one after another, with each successive one 121*4882a593Smuzhiyun * filling in information not contained in the prior. Note that if 122*4882a593Smuzhiyun * WGDEVICE_F_REPLACE_PEERS is specified in the first message, it probably 123*4882a593Smuzhiyun * should not be specified in fragments that come after, so that the list 124*4882a593Smuzhiyun * of peers is only cleared the first time but appended after. Likewise for 125*4882a593Smuzhiyun * peers, if WGPEER_F_REPLACE_ALLOWEDIPS is specified in the first message 126*4882a593Smuzhiyun * of a peer, it likely should not be specified in subsequent fragments. 127*4882a593Smuzhiyun * 128*4882a593Smuzhiyun * If an error occurs, NLMSG_ERROR will reply containing an errno. 129*4882a593Smuzhiyun */ 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun #ifndef _WG_UAPI_WIREGUARD_H 132*4882a593Smuzhiyun #define _WG_UAPI_WIREGUARD_H 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun #define WG_GENL_NAME "wireguard" 135*4882a593Smuzhiyun #define WG_GENL_VERSION 1 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun #define WG_KEY_LEN 32 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun enum wg_cmd { 140*4882a593Smuzhiyun WG_CMD_GET_DEVICE, 141*4882a593Smuzhiyun WG_CMD_SET_DEVICE, 142*4882a593Smuzhiyun __WG_CMD_MAX 143*4882a593Smuzhiyun }; 144*4882a593Smuzhiyun #define WG_CMD_MAX (__WG_CMD_MAX - 1) 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun enum wgdevice_flag { 147*4882a593Smuzhiyun WGDEVICE_F_REPLACE_PEERS = 1U << 0, 148*4882a593Smuzhiyun __WGDEVICE_F_ALL = WGDEVICE_F_REPLACE_PEERS 149*4882a593Smuzhiyun }; 150*4882a593Smuzhiyun enum wgdevice_attribute { 151*4882a593Smuzhiyun WGDEVICE_A_UNSPEC, 152*4882a593Smuzhiyun WGDEVICE_A_IFINDEX, 153*4882a593Smuzhiyun WGDEVICE_A_IFNAME, 154*4882a593Smuzhiyun WGDEVICE_A_PRIVATE_KEY, 155*4882a593Smuzhiyun WGDEVICE_A_PUBLIC_KEY, 156*4882a593Smuzhiyun WGDEVICE_A_FLAGS, 157*4882a593Smuzhiyun WGDEVICE_A_LISTEN_PORT, 158*4882a593Smuzhiyun WGDEVICE_A_FWMARK, 159*4882a593Smuzhiyun WGDEVICE_A_PEERS, 160*4882a593Smuzhiyun __WGDEVICE_A_LAST 161*4882a593Smuzhiyun }; 162*4882a593Smuzhiyun #define WGDEVICE_A_MAX (__WGDEVICE_A_LAST - 1) 163*4882a593Smuzhiyun 164*4882a593Smuzhiyun enum wgpeer_flag { 165*4882a593Smuzhiyun WGPEER_F_REMOVE_ME = 1U << 0, 166*4882a593Smuzhiyun WGPEER_F_REPLACE_ALLOWEDIPS = 1U << 1, 167*4882a593Smuzhiyun WGPEER_F_UPDATE_ONLY = 1U << 2, 168*4882a593Smuzhiyun __WGPEER_F_ALL = WGPEER_F_REMOVE_ME | WGPEER_F_REPLACE_ALLOWEDIPS | 169*4882a593Smuzhiyun WGPEER_F_UPDATE_ONLY 170*4882a593Smuzhiyun }; 171*4882a593Smuzhiyun enum wgpeer_attribute { 172*4882a593Smuzhiyun WGPEER_A_UNSPEC, 173*4882a593Smuzhiyun WGPEER_A_PUBLIC_KEY, 174*4882a593Smuzhiyun WGPEER_A_PRESHARED_KEY, 175*4882a593Smuzhiyun WGPEER_A_FLAGS, 176*4882a593Smuzhiyun WGPEER_A_ENDPOINT, 177*4882a593Smuzhiyun WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL, 178*4882a593Smuzhiyun WGPEER_A_LAST_HANDSHAKE_TIME, 179*4882a593Smuzhiyun WGPEER_A_RX_BYTES, 180*4882a593Smuzhiyun WGPEER_A_TX_BYTES, 181*4882a593Smuzhiyun WGPEER_A_ALLOWEDIPS, 182*4882a593Smuzhiyun WGPEER_A_PROTOCOL_VERSION, 183*4882a593Smuzhiyun __WGPEER_A_LAST 184*4882a593Smuzhiyun }; 185*4882a593Smuzhiyun #define WGPEER_A_MAX (__WGPEER_A_LAST - 1) 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun enum wgallowedip_attribute { 188*4882a593Smuzhiyun WGALLOWEDIP_A_UNSPEC, 189*4882a593Smuzhiyun WGALLOWEDIP_A_FAMILY, 190*4882a593Smuzhiyun WGALLOWEDIP_A_IPADDR, 191*4882a593Smuzhiyun WGALLOWEDIP_A_CIDR_MASK, 192*4882a593Smuzhiyun __WGALLOWEDIP_A_LAST 193*4882a593Smuzhiyun }; 194*4882a593Smuzhiyun #define WGALLOWEDIP_A_MAX (__WGALLOWEDIP_A_LAST - 1) 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun #endif /* _WG_UAPI_WIREGUARD_H */ 197