wireguard: netlink: ynl conversion
v2 cover wireguard: netlink: ynl conversion
This series completes the implementation of YNL for wireguard, as previously announced[1].
This series consist of 5 parts:
1) Patch 01-03 - Misc. changes
2) Patch 04 - Add YNL specification for wireguard
3) Patch 05-07 - Transition to a generated UAPI header
4) Patch 08 - Adds a sample program for the generated C library
5) Patch 09-11 - Transition to generated netlink policy code
The main benefit of having a YNL specification is unlocked after the first 2 parts, the RFC version seems to already have spawned a new Rust netlink binding[2] using wireguard as it's main example.
Part 3 and 5 validates that the specification is complete and aligned, the generated code might have a few warts, but they don't matter too much, and are mostly a transitional problem[3].
Part 4 is possible after part 2, but is ordered after part 3, as it needs to duplicate the UAPI header in tools/include.
For the non-generated kernel C code the diff stat looks like this:
$ git diff --stat net-next/main..wg-ynl include/ drivers/ \ ':(exclude)*netlink_gen*' drivers/net/wireguard/Makefile | 1 + drivers/net/wireguard/netlink.c | 70 +++--------- include/uapi/linux/wireguard.h | 190 ++++++-------------------------- 3 files changed, 47 insertions(+), 214 deletions(-)
[1] [PATCH net 0/4] tools: ynl-gen: misc fixes + wireguard ynl plan
https://lore.kernel.org/r/[email protected]/
[2] https://github.com/one-d-wide/netlink-bindings/
[3] https://lore.kernel.org/r/[email protected]/
---
v2:
- Add missing forward declaration
v1: https://lore.kernel.org/r/[email protected]/
- Policy arguement to nla_parse_nested() changed to NULL (thanks Johannes).
- Added attr-cnt-name to the spec, to reduce the diff a bit.
- Refined the doc in the spec a bit.
- Reword commit messages a bit.
- Reordered the patches, and reduced the series from 14 to 11 patches.
RFC: https://lore.kernel.org/r/[email protected]/
Asbjørn Sloth Tønnesen (11):
wireguard: netlink: validate nested arrays in policy
wireguard: netlink: use WG_KEY_LEN in policies
wireguard: netlink: enable strict genetlink validation
netlink: specs: add specification for wireguard
uapi: wireguard: move enum wg_cmd
uapi: wireguard: move flag enums
uapi: wireguard: generate header with ynl-gen
tools: ynl: add sample for wireguard
wireguard: netlink: convert to split ops
wireguard: netlink: rename netlink handlers
wireguard: netlink: generate netlink code
Documentation/netlink/specs/wireguard.yaml | 307 +++++++++++++++++++++ MAINTAINERS | 3 + drivers/net/wireguard/Makefile | 1 + drivers/net/wireguard/netlink.c | 70 +---- drivers/net/wireguard/netlink_gen.c | 77 ++++++ drivers/net/wireguard/netlink_gen.h | 29 ++ include/uapi/linux/wireguard.h | 190 +++---------- tools/include/uapi/linux/wireguard.h | 79 ++++++ tools/net/ynl/samples/.gitignore | 1 + tools/net/ynl/samples/wireguard.c | 104 +++++++ 10 files changed, 647 insertions(+), 214 deletions(-) create mode 100644 Documentation/netlink/specs/wireguard.yaml create mode 100644 drivers/net/wireguard/netlink_gen.c create mode 100644 drivers/net/wireguard/netlink_gen.h create mode 100644 tools/include/uapi/linux/wireguard.h create mode 100644 tools/net/ynl/samples/wireguard.c
Signature · 1 line
2.51.0
PATCH v2 1/11 wireguard: netlink: validate nested arrays in policy
Use NLA_POLICY_NESTED_ARRAY() to perform nested array validation in the policy validation step.
The nested policy was already enforced through nla_parse_nested(), however extack wasn't passed previously.
--- drivers/net/wireguard/netlink.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/net/wireguard/netlink.c +6 -4
@@ -18,6 +18,8 @@#include <crypto/utils.h> static struct genl_family genl_family; +static const struct nla_policy peer_policy[WGPEER_A_MAX +1 ]; +static const struct nla_policy allowedip_policy[WGALLOWEDIP_A_MAX +1 ];static const struct nla_policy device_policy[WGDEVICE_A_MAX +1 ] = { [WGDEVICE_A_IFINDEX ] = { .type =NLA_U32 },
@@ -27,7 +29,7 @@ static const struct nla_policy device_policy[WGDEVICE_A_MAX + 1] = { [WGDEVICE_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, __WGDEVICE_F_ALL), [WGDEVICE_A_LISTEN_PORT] = { .type = NLA_U16 }, [WGDEVICE_A_FWMARK] = { .type = NLA_U32 }, - [WGDEVICE_A_PEERS ] = { .type =NLA_NESTED } + [WGDEVICE_A_PEERS] = NLA_POLICY_NESTED_ARRAY(peer_policy), };static const struct nla_policy peer_policy[WGPEER_A_MAX +1 ] = {
@@ -39,7 +41,7 @@ static const struct nla_policy peer_policy[WGPEER_A_MAX + 1] = { [WGPEER_A_LAST_HANDSHAKE_TIME] = NLA_POLICY_EXACT_LEN(sizeof(struct __kernel_timespec)), [WGPEER_A_RX_BYTES] = { .type = NLA_U64 }, [WGPEER_A_TX_BYTES] = { .type = NLA_U64 }, - [WGPEER_A_ALLOWEDIPS] = { .type = NLA_NESTED }, + [WGPEER_A_ALLOWEDIPS] = NLA_POLICY_NESTED_ARRAY(allowedip_policy), [WGPEER_A_PROTOCOL_VERSION] = { .type = NLA_U32 } };
@@ -467,7 +469,7 @@ static int set_peer(struct wg_device *wg, struct nlattr **attrs)nla_for_each_nested (attr, attrs[WGPEER_A_ALLOWEDIPS ], rem) { ret =nla_parse_nested (allowedip,WGALLOWEDIP_A_MAX , - attr, allowedip_policy,NULL ); + attr,NULL ,NULL );if (ret <0 ) goto out; ret =set_allowedip (peer, allowedip);
@@ -593,7 +595,7 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)nla_for_each_nested (attr, info->attrs[WGDEVICE_A_PEERS ], rem) { ret =nla_parse_nested (peer,WGPEER_A_MAX , attr, - peer_policy,NULL ); +NULL ,NULL );if (ret <0 ) goto out; ret =set_peer (wg, peer);
Signature · 1 line
2.51.0
PATCH v2 2/11 wireguard: netlink: use WG_KEY_LEN in policies
In reply to Asbjørn Sloth TønnesenWhen converting the netlink policies to YNL, then the constants used in the policy has to be visible to user-space.
As NOISE_*_KEY_LEN isn't visible for userspace, then change the policy to use WG_KEY_LEN, as is also documented in the UAPI header:
$ grep WG_KEY_LEN include/uapi/linux/wireguard.h * WGDEVICE_A_PRIVATE_KEY: NLA_EXACT_LEN, len WG_KEY_LEN * WGDEVICE_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN * WGPEER_A_PUBLIC_KEY: NLA_EXACT_LEN, len WG_KEY_LEN * WGPEER_A_PRESHARED_KEY: NLA_EXACT_LEN, len WG_KEY_LEN [...]
Add a couple of BUILD_BUG_ON() to ensure that they stay in sync.
No behavioural changes intended.
--- drivers/net/wireguard/netlink.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/net/wireguard/netlink.c +7 -4
@@ -24,8 +24,8 @@ static const struct nla_policy allowedip_policy[WGALLOWEDIP_A_MAX + 1];static const struct nla_policy device_policy[WGDEVICE_A_MAX +1 ] = { [WGDEVICE_A_IFINDEX ] = { .type =NLA_U32 }, [WGDEVICE_A_IFNAME ] = { .type =NLA_NUL_STRING , .len =IFNAMSIZ -1 }, - [WGDEVICE_A_PRIVATE_KEY ] =NLA_POLICY_EXACT_LEN (NOISE_PUBLIC_KEY_LEN ), - [WGDEVICE_A_PUBLIC_KEY ] =NLA_POLICY_EXACT_LEN (NOISE_PUBLIC_KEY_LEN ), + [WGDEVICE_A_PRIVATE_KEY ] =NLA_POLICY_EXACT_LEN (WG_KEY_LEN ), + [WGDEVICE_A_PUBLIC_KEY ] =NLA_POLICY_EXACT_LEN (WG_KEY_LEN ), [WGDEVICE_A_FLAGS ] =NLA_POLICY_MASK (NLA_U32 , __WGDEVICE_F_ALL), [WGDEVICE_A_LISTEN_PORT ] = { .type =NLA_U16 }, [WGDEVICE_A_FWMARK ] = { .type =NLA_U32 },
@@ -33,8 +33,8 @@ static const struct nla_policy device_policy[WGDEVICE_A_MAX + 1] = { };static const struct nla_policy peer_policy[WGPEER_A_MAX +1 ] = { - [WGPEER_A_PUBLIC_KEY ] =NLA_POLICY_EXACT_LEN (NOISE_PUBLIC_KEY_LEN ), - [WGPEER_A_PRESHARED_KEY ] =NLA_POLICY_EXACT_LEN (NOISE_SYMMETRIC_KEY_LEN ), + [WGPEER_A_PUBLIC_KEY ] =NLA_POLICY_EXACT_LEN (WG_KEY_LEN ), + [WGPEER_A_PRESHARED_KEY ] =NLA_POLICY_EXACT_LEN (WG_KEY_LEN ), [WGPEER_A_FLAGS ] =NLA_POLICY_MASK (NLA_U32 , __WGPEER_F_ALL), [WGPEER_A_ENDPOINT ] =NLA_POLICY_MIN_LEN (sizeof (struct sockaddr )), [WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL ] = { .type =NLA_U16 },
@@ -644,6 +644,9 @@ static struct genl_family genl_family __ro_after_init = {int __init wg_genetlink_init (void ) { +BUILD_BUG_ON (WG_KEY_LEN !=NOISE_PUBLIC_KEY_LEN ); +BUILD_BUG_ON (WG_KEY_LEN !=NOISE_SYMMETRIC_KEY_LEN ); +return genl_register_family (&genl_family); }
Signature · 1 line
2.51.0
PATCH v2 3/11 wireguard: netlink: enable strict genetlink validation
In reply to Asbjørn Sloth TønnesenWireguard is a modern enough genetlink family, that it doesn't need resv_start_op. It already had policies in place when it was first merged, it has also never used the reserved field, or other things toggled by resv_start_op.
wireguard-tools have always used zero initialized memory, and have never touched the reserved field, neither have any other clients I have checked. Closed-source clients are much more likely to use the embeddedable library from wireguard-tools, than a DIY implementation using uninitialized memory.
--- drivers/net/wireguard/netlink.c | 1 - 1 file changed, 1 deletion(-)
drivers/net/wireguard/netlink.c +0 -1
@@ -633,7 +633,6 @@ static const struct genl_ops genl_ops[] = {static struct genl_family genl_family__ro_after_init = { .ops = genl_ops, .n_ops =ARRAY_SIZE (genl_ops), - .resv_start_op =WG_CMD_SET_DEVICE +1 , .name =WG_GENL_NAME , .version =WG_GENL_VERSION , .maxattr =WGDEVICE_A_MAX ,
Signature · 1 line
2.51.0
PATCH v2 4/11 netlink: specs: add specification for wireguard
In reply to Asbjørn Sloth TønnesenThis patch adds an near[1] complete YNL specification for wireguard, documenting the protocol in a machine-readable format, than the comment in wireguard.h, and eases usage from C and non-C programming languages alike.
The generated C library will be featured in the next patch, so in this patch I will use the in-kernel python client for examples.
This makes the documentation in the UAPI header redundant, and it is therefore removed. The in-line documentation in the spec, is based on the existing comment in wireguard.h, and once released then it will be available in the kernel documentation at:
https://docs.kernel.org/netlink/specs/wireguard.html
(until then run: make htmldocs)
Generate wireguard.rst from this spec:
$ make -C tools/net/ynl/generated/ wireguard.rst
Query wireguard interface through pyynl:
$ sudo ./tools/net/ynl/pyynl/cli.py --family wireguard \
--dump get-device \
--json '{"ifindex":3}'
[{'fwmark': 0,
'ifindex': 3,
'ifname': 'wg-test',
'listen-port': 54318,
'peers': [{0: {'allowedips': [{0: {'cidr-mask': 0,
'family': 2,
'ipaddr': '0.0.0.0'}},
{0: {'cidr-mask': 0,
'family': 10,
'ipaddr': '::'}}],
'endpoint': b'[...]',
'last-handshake-time': {'nsec': 42, 'sec': 42},
'persistent-keepalive-interval': 42,
'preshared-key': '[...]',
'protocol-version': 1,
'public-key': '[...]',
'rx-bytes': 42,
'tx-bytes': 42}}],
'private-key': '[...]',
'public-key': '[...]'}]Add another allowed IP prefix:
$ sudo ./tools/net/ynl/pyynl/cli.py --family wireguard \
--do set-device --json '{"ifindex":3,"peers":[
{"public-key":"6a df b1 83 a4 ..","allowedips":[
{"cidr-mask":0,"family":10,"ipaddr":"::"}]}]}'[1] As can be seen above, the "endpoint" is only decoded as binary data, as it can't be described fully in YNL. It's a struct sockaddr_in or struct sockaddr_in6 depending on the attribute length.
--- Documentation/netlink/specs/wireguard.yaml | 307 +++++++++++++++++++++ MAINTAINERS | 1 + include/uapi/linux/wireguard.h | 129 --------- 3 files changed, 308 insertions(+), 129 deletions(-) create mode 100644 Documentation/netlink/specs/wireguard.yaml
Documentation/netlink/specs/wireguard.yaml new file +307 -0
@@ -0,0 +1,307 @@ +# SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) +--- +name: wireguard +protocol: genetlink-legacy + +doc: | + **Netlink protocol to control WireGuard network devices.** + + The below enums and macros are for interfacing with WireGuard, + using generic netlink, with family ``WG_GENL_NAME`` and version + ``WG_GENL_VERSION``. It defines two commands: get and set. + Note that while they share many common attributes, these two + commands actually accept a slightly different set of inputs and + outputs. These differences are noted under the individual attributes. +c-family-name: wg-genl-name +c-version-name: wg-genl-version +max-by-define: true + +definitions: + - + name-prefix: wg- + name: key-len + type: const + value: 32 + - + name: --kernel-timespec + type: struct + header: linux/time_types.h + members: + - + name: sec + type: u64 + doc: Number of seconds, since UNIX epoch. + - + name: nsec + type: u64 + doc: Number of nanoseconds, after the second began. + - + name: wgdevice-flags + name-prefix: wgdevice-f- + enum-name: wgdevice-flag + type: flags + entries: + - replace-peers + - + name: wgpeer-flags + name-prefix: wgpeer-f- + enum-name: wgpeer-flag + type: flags + entries: + - remove-me + - replace-allowedips + - update-only + - + name: wgallowedip-flags + name-prefix: wgallowedip-f- + enum-name: wgallowedip-flag + type: flags + entries: + - remove-me + +attribute-sets: + - + name: wgdevice + enum-name: wgdevice-attribute + name-prefix: wgdevice-a- + attr-cnt-name: --wgdevice-a-last + attributes: + - + name: unspec + type: unused + value: 0 + - + name: ifindex + type: u32 + - + name: ifname + type: string + checks: + max-len: 15 + - + name: private-key + type: binary + doc: Set to all zeros to remove. + display-hint: hex + checks: + exact-len: wg-key-len + - + name: public-key + type: binary + display-hint: hex + checks: + exact-len: wg-key-len + - + name: flags + doc: | + ``0`` or ``WGDEVICE_F_REPLACE_PEERS`` if all current peers + should be removed prior to adding the list below. + type: u32 + enum: wgdevice-flags + checks: + flags-mask: wgdevice-flags + - + name: listen-port + type: u16 + doc: Set as ``0`` to choose randomly. + - + name: fwmark + type: u32 + doc: Set as ``0`` to disable. + - + name: peers + type: indexed-array + sub-type: nest + nested-attributes: wgpeer + doc: The index is set as ``0`` in ``DUMP``, and unused in ``DO``. + - + name: wgpeer + enum-name: wgpeer-attribute + name-prefix: wgpeer-a- + attr-cnt-name: --wgpeer-a-last + attributes: + - + name: unspec + type: unused + value: 0 + - + name: public-key + type: binary + display-hint: hex + checks: + exact-len: wg-key-len + - + name: preshared-key + type: binary + doc: Set as all zeros to remove. + display-hint: hex + checks: + exact-len: wg-key-len + - + name: flags + doc: | + ``0`` and/or ``WGPEER_F_REMOVE_ME`` if the specified peer should not + exist at the end of the operation, rather than added/updated + and/or ``WGPEER_F_REPLACE_ALLOWEDIPS`` if all current allowed IPs + of this peer should be removed prior to adding the list below + and/or ``WGPEER_F_UPDATE_ONLY`` if the peer should only be set if + it already exists. + type: u32 + enum: wgpeer-flags + checks: + flags-mask: wgpeer-flags + - + name: endpoint + doc: struct sockaddr_in or struct sockaddr_in6 + type: binary + checks: + min-len: 16 + - + name: persistent-keepalive-interval + type: u16 + doc: Set as ``0`` to disable. + - + name: last-handshake-time + type: binary + struct: --kernel-timespec + checks: + exact-len: 16 + - + name: rx-bytes + type: u64 + - + name: tx-bytes + type: u64 + - + name: allowedips + type: indexed-array + sub-type: nest + nested-attributes: wgallowedip + doc: The index is set as ``0`` in ``DUMP``, and unused in ``DO``. + - + name: protocol-version + type: u32 + doc: | + Should not be set or used at all by most users of this API, + as the most recent protocol will be used when this is unset. + Otherwise, must be set to ``1``. + - + name: wgallowedip + enum-name: wgallowedip-attribute + name-prefix: wgallowedip-a- + attr-cnt-name: --wgallowedip-a-last + attributes: + - + name: unspec + type: unused + value: 0 + - + name: family + type: u16 + doc: IP family, either ``AF_INET`` or ``AF_INET6``. + - + name: ipaddr + type: binary + doc: Either ``struct in_addr`` or ``struct in6_addr``. + display-hint: ipv4-or-v6 + checks: + min-len: 4 + - + name: cidr-mask + type: u8 + - + name: flags + type: u32 + doc: | + ``WGALLOWEDIP_F_REMOVE_ME`` if the specified IP should be + removed; otherwise, this IP will be added if it is not + already present. + enum: wgallowedip-flags + checks: + flags-mask: wgallowedip-flags + +operations: + enum-name: wg-cmd + name-prefix: wg-cmd- + list: + - + name: get-device + value: 0 + doc: | + Retrieve WireGuard device + ~~~~~~~~~~~~~~~~~~~~~~~~~ + + The command should be called with one but not both of: + + - ``WGDEVICE_A_IFINDEX`` + - ``WGDEVICE_A_IFNAME`` + + The kernel will then return several messages (``NLM_F_MULTI``). + It is possible that all of the allowed IPs of a single peer + will not fit within a single netlink message. In that case, the + same peer will be written in the following message, except it will + only contain ``WGPEER_A_PUBLIC_KEY`` and ``WGPEER_A_ALLOWEDIPS``. + This may occur several times in a row for the same peer. + It is then up to the receiver to coalesce adjacent peers. + Likewise, it is possible that all peers will not fit within a + single message. + So, subsequent peers will be sent in following messages, + except those will only contain ``WGDEVICE_A_IFNAME`` and + ``WGDEVICE_A_PEERS``. It is then up to the receiver to coalesce + these messages to form the complete list of peers. + + While this command does accept the other ``WGDEVICE_A_*`` + attributes, for compatibility reasons, but they are ignored + by this command, and should not be used in requests. + + Since this is an ``NLA_F_DUMP`` command, the final message will + always be ``NLMSG_DONE``, even if an error occurs. However, this + ``NLMSG_DONE`` message contains an integer error code. It is + either zero or a negative error code corresponding to the errno. + attribute-set: wgdevice + flags: [uns-admin-perm] + + dump: + pre: wireguard-nl-get-device-start + post: wireguard-nl-get-device-done + # request only uses ifindex | ifname, but keep .maxattr as is + request: &all-attrs + attributes: + - ifindex + - ifname + - private-key + - public-key + - flags + - listen-port + - fwmark + - peers + reply: *all-attrs + - + name: set-device + value: 1 + doc: | + Set WireGuard device + ~~~~~~~~~~~~~~~~~~~~ + + This command should be called with a wgdevice set, containing one + but not both of ``WGDEVICE_A_IFINDEX`` and ``WGDEVICE_A_IFNAME``. + + It is possible that the amount of configuration data exceeds that + of the maximum message length accepted by the kernel. + In that case, several messages should be sent one after another, + with each successive one filling in information not contained in + the prior. + Note that if ``WGDEVICE_F_REPLACE_PEERS`` is specified in the first + message, it probably should not be specified in fragments that come + after, so that the list of peers is only cleared the first time but + appended after. + Likewise for peers, if ``WGPEER_F_REPLACE_ALLOWEDIPS`` is specified + in the first message of a peer, it likely should not be specified + in subsequent fragments. + + If an error occurs, ``NLMSG_ERROR`` will reply containing an errno. + attribute-set: wgdevice + flags: [uns-admin-perm] + + do: + request: *all-attrs
MAINTAINERS +1 -0
@@ -27641,6 +27641,7 @@ M: Jason A. Donenfeld <[email protected]> L: [email protected] L: [email protected] S: Maintained +F: Documentation/netlink/specs/wireguard.yaml F: drivers/net/wireguard/ F: tools/testing/selftests/wireguard/
include/uapi/linux/wireguard.h +0 -129
135 lines not on this page · raw message
Signature · 1 line
2.51.0
Jakub Kicinski
maintainerRe: [PATCH net-next v2 04/11] netlink: specs: add specification for wireguard
Documentation/netlink/specs/wireguard.yaml line 102
Quoted 8 lines · On Fri, 31 Oct 2025 16:05:30 +0000 Asbjørn Sloth Tønnesen wrote:
Documentation/netlink/specs/wireguard.yaml new file +8
-0+ name: flags + doc: | + ``0`` or ``WGDEVICE_F_REPLACE_PEERS`` if all current peers + should be removed prior to adding the list below. + type: u32 + enum: wgdevice-flags + checks: + flags-mask: wgdevice-flags
The checks: should not be necessary if the enum is specified and it is itself of type: flags.
PATCH v2 5/11 uapi: wireguard: move enum wg_cmd
In reply to Asbjørn Sloth TønnesenThis patch moves enum wg_cmd to the end of the file, where ynl-gen would like to generate it.
This is an incremental step towards adopting an UAPI header generated by ynl-gen. This is split out to keep the patches readable.
This is a trivial patch with no behavioural changes intended.
--- include/uapi/linux/wireguard.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
include/uapi/linux/wireguard.h +8 -7
@@ -11,13 +11,6 @@#define WG_KEY_LEN 32 -enum wg_cmd { -WG_CMD_GET_DEVICE , -WG_CMD_SET_DEVICE , - __WG_CMD_MAX -}; -#define WG_CMD_MAX (__WG_CMD_MAX - 1) -enum wgdevice_flag {WGDEVICE_F_REPLACE_PEERS =1U <<0 , __WGDEVICE_F_ALL =WGDEVICE_F_REPLACE_PEERS
@@ -73,4 +66,12 @@ enum wgallowedip_attribute { };#define WGALLOWEDIP_A_MAX (__WGALLOWEDIP_A_LAST - 1) +enum wg_cmd { +WG_CMD_GET_DEVICE , +WG_CMD_SET_DEVICE , + + __WG_CMD_MAX +}; +#define WG_CMD_MAX (__WG_CMD_MAX - 1) +#endif /* _WG_UAPI_WIREGUARD_H */
Signature · 1 line
2.51.0
PATCH v2 6/11 uapi: wireguard: move flag enums
In reply to Asbjørn Sloth TønnesenMove the wg*_flag enums, so that they are defined above the attribute set enums, as ynl-gen would place them.
This is an incremental step towards adopting an UAPI header generated by ynl-gen. This is split out to keep the patches readable.
This is a trivial patch with no behavioural changes intended.
--- include/uapi/linux/wireguard.h | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
include/uapi/linux/wireguard.h +14 -11
@@ -15,6 +15,20 @@ enum wgdevice_flag {WGDEVICE_F_REPLACE_PEERS =1U <<0 , __WGDEVICE_F_ALL =WGDEVICE_F_REPLACE_PEERS }; + +enum wgpeer_flag { +WGPEER_F_REMOVE_ME =1U <<0 , +WGPEER_F_REPLACE_ALLOWEDIPS =1U <<1 , +WGPEER_F_UPDATE_ONLY =1U <<2 , + __WGPEER_F_ALL =WGPEER_F_REMOVE_ME |WGPEER_F_REPLACE_ALLOWEDIPS | +WGPEER_F_UPDATE_ONLY +}; + +enum wgallowedip_flag { +WGALLOWEDIP_F_REMOVE_ME =1U <<0 , + __WGALLOWEDIP_F_ALL =WGALLOWEDIP_F_REMOVE_ME +}; +enum wgdevice_attribute {WGDEVICE_A_UNSPEC , WGDEVICE_A_IFINDEX,
@@ -29,13 +43,6 @@ enum wgdevice_attribute { };#define WGDEVICE_A_MAX (__WGDEVICE_A_LAST - 1) -enum wgpeer_flag { -WGPEER_F_REMOVE_ME =1U <<0 , -WGPEER_F_REPLACE_ALLOWEDIPS =1U <<1 , -WGPEER_F_UPDATE_ONLY =1U <<2 , - __WGPEER_F_ALL =WGPEER_F_REMOVE_ME |WGPEER_F_REPLACE_ALLOWEDIPS | -WGPEER_F_UPDATE_ONLY -};enum wgpeer_attribute {WGPEER_A_UNSPEC ,WGPEER_A_PUBLIC_KEY ,
@@ -52,10 +59,6 @@ enum wgpeer_attribute { };#define WGPEER_A_MAX (__WGPEER_A_LAST - 1) -enum wgallowedip_flag { -WGALLOWEDIP_F_REMOVE_ME =1U <<0 , - __WGALLOWEDIP_F_ALL =WGALLOWEDIP_F_REMOVE_ME -};enum wgallowedip_attribute {WGALLOWEDIP_A_UNSPEC ,WGALLOWEDIP_A_FAMILY ,
Signature · 1 line
2.51.0
PATCH v2 7/11 uapi: wireguard: generate header with ynl-gen
In reply to Asbjørn Sloth TønnesenUse ynl-gen to generate the UAPI header for wireguard.
The cosmetic changes in this patch, confirms that the spec is aligned with the implementation, and ensures that it stays in sync.
Changes in generated header:
* Trivial include guard rename.
* Trivial white space changes.
* Trivial comment changes.
* Precompute bitflags in ynl-gen (see [1]).
* Drop __*_F_ALL constants (see [1]).
[1] https://lore.kernel.org/r/[email protected]/
No behavioural changes intended.
--- drivers/net/wireguard/netlink.c | 6 +++--- include/uapi/linux/wireguard.h | 37 ++++++++++++++++----------------- 2 files changed, 21 insertions(+), 22 deletions(-)
drivers/net/wireguard/netlink.c +3 -3
@@ -26,7 +26,7 @@ static const struct nla_policy device_policy[WGDEVICE_A_MAX + 1] = { [WGDEVICE_A_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, [WGDEVICE_A_PRIVATE_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN), [WGDEVICE_A_PUBLIC_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN), - [WGDEVICE_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, __WGDEVICE_F_ALL), + [WGDEVICE_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x1), [WGDEVICE_A_LISTEN_PORT] = { .type = NLA_U16 }, [WGDEVICE_A_FWMARK] = { .type = NLA_U32 }, [WGDEVICE_A_PEERS] = NLA_POLICY_NESTED_ARRAY(peer_policy),
@@ -35,7 +35,7 @@ static const struct nla_policy device_policy[WGDEVICE_A_MAX + 1] = {static const struct nla_policy peer_policy[WGPEER_A_MAX +1 ] = { [WGPEER_A_PUBLIC_KEY ] =NLA_POLICY_EXACT_LEN (WG_KEY_LEN ), [WGPEER_A_PRESHARED_KEY ] =NLA_POLICY_EXACT_LEN (WG_KEY_LEN ), - [WGPEER_A_FLAGS ] =NLA_POLICY_MASK (NLA_U32 , __WGPEER_F_ALL), + [WGPEER_A_FLAGS ] =NLA_POLICY_MASK (NLA_U32 ,0x7 ), [WGPEER_A_ENDPOINT ] =NLA_POLICY_MIN_LEN (sizeof (struct sockaddr )), [WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL ] = { .type =NLA_U16 }, [WGPEER_A_LAST_HANDSHAKE_TIME ] =NLA_POLICY_EXACT_LEN (sizeof (struct __kernel_timespec )),
@@ -49,7 +49,7 @@ static const struct nla_policy allowedip_policy[WGALLOWEDIP_A_MAX + 1] = { [WGALLOWEDIP_A_FAMILY] = { .type = NLA_U16 }, [WGALLOWEDIP_A_IPADDR] = NLA_POLICY_MIN_LEN(sizeof(struct in_addr)), [WGALLOWEDIP_A_CIDR_MASK] = { .type = NLA_U8 }, - [WGALLOWEDIP_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, __WGALLOWEDIP_F_ALL), + [WGALLOWEDIP_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x1), }; static struct wg_device *lookup_interface(struct nlattr **attrs,
include/uapi/linux/wireguard.h +18 -19
@@ -1,32 +1,28 @@ -/* SPDX-License-Identifier: (GPL-2.0 WITH Linux-syscall-note) OR MIT */ -/* -* Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved. -*/ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */ +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/wireguard.yaml */ +/* YNL-GEN uapi header */ -#ifndef _WG_UAPI_WIREGUARD_H -#define _WG_UAPI_WIREGUARD_H +#ifndef _UAPI_LINUX_WIREGUARD_H +#define _UAPI_LINUX_WIREGUARD_H -#define WG_GENL_NAME "wireguard" -#define WG_GENL_VERSION 1 +#define WG_GENL_NAME "wireguard" +#define WG_GENL_VERSION 1 -#define WG_KEY_LEN 32 +#define WG_KEY_LEN 32enum wgdevice_flag { -WGDEVICE_F_REPLACE_PEERS =1U <<0 , - __WGDEVICE_F_ALL =WGDEVICE_F_REPLACE_PEERS +WGDEVICE_F_REPLACE_PEERS =1 , };enum wgpeer_flag { -WGPEER_F_REMOVE_ME =1U <<0 , -WGPEER_F_REPLACE_ALLOWEDIPS =1U <<1 , -WGPEER_F_UPDATE_ONLY =1U <<2 , - __WGPEER_F_ALL =WGPEER_F_REMOVE_ME |WGPEER_F_REPLACE_ALLOWEDIPS | -WGPEER_F_UPDATE_ONLY +WGPEER_F_REMOVE_ME =1 , +WGPEER_F_REPLACE_ALLOWEDIPS =2 , +WGPEER_F_UPDATE_ONLY =4 , };enum wgallowedip_flag { -WGALLOWEDIP_F_REMOVE_ME =1U <<0 , - __WGALLOWEDIP_F_ALL =WGALLOWEDIP_F_REMOVE_ME +WGALLOWEDIP_F_REMOVE_ME =1 , }; enum wgdevice_attribute {
@@ -39,6 +35,7 @@ enum wgdevice_attribute {WGDEVICE_A_LISTEN_PORT ,WGDEVICE_A_FWMARK ,WGDEVICE_A_PEERS , + __WGDEVICE_A_LAST };#define WGDEVICE_A_MAX (__WGDEVICE_A_LAST - 1)
@@ -55,6 +52,7 @@ enum wgpeer_attribute {WGPEER_A_TX_BYTES ,WGPEER_A_ALLOWEDIPS ,WGPEER_A_PROTOCOL_VERSION , + __WGPEER_A_LAST };#define WGPEER_A_MAX (__WGPEER_A_LAST - 1)
@@ -65,6 +63,7 @@ enum wgallowedip_attribute {WGALLOWEDIP_A_IPADDR ,WGALLOWEDIP_A_CIDR_MASK ,WGALLOWEDIP_A_FLAGS , + __WGALLOWEDIP_A_LAST };#define WGALLOWEDIP_A_MAX (__WGALLOWEDIP_A_LAST - 1)
@@ -77,4 +76,4 @@ enum wg_cmd { };#define WG_CMD_MAX (__WG_CMD_MAX - 1) -#endif /* _WG_UAPI_WIREGUARD_H */ +#endif /* _UAPI_LINUX_WIREGUARD_H */
Signature · 1 line
2.51.0
PATCH v2 8/11 tools: ynl: add sample for wireguard
In reply to Asbjørn Sloth TønnesenAdd a sample application for wireguard, using the generated C library,
The main benefit of this is to exercise the generated library, which might be useful for future selftests.
The UAPI header is copied to tools/include/uapi/, when the header changes ynl-gen will regenerate both copies.
Example:
$ make -C tools/net/ynl/lib
$ make -C tools/net/ynl/generated
$ make -C tools/net/ynl/samples wireguard
$ ./tools/net/ynl/samples/wireguard
usage: ./tools/net/ynl/samples/wireguard <ifindex|ifname>
$ sudo ./tools/net/ynl/samples/wireguard wg-test
Interface 3: wg-test
Peer 6adfb183a4a2c94a2f92dab5ade762a4788[...]:
Data: rx: 42 / tx: 42 bytes
Allowed IPs:
0.0.0.0/0
::/0--- MAINTAINERS | 2 + tools/include/uapi/linux/wireguard.h | 79 ++++++++++++++++++++ tools/net/ynl/samples/.gitignore | 1 + tools/net/ynl/samples/wireguard.c | 104 +++++++++++++++++++++++++++ 4 files changed, 186 insertions(+) create mode 100644 tools/include/uapi/linux/wireguard.h create mode 100644 tools/net/ynl/samples/wireguard.c
MAINTAINERS +2 -0
@@ -27643,6 +27643,8 @@ L: [email protected] S: Maintained F: Documentation/netlink/specs/wireguard.yaml F: drivers/net/wireguard/ +F: tools/include/uapi/linux/wireguard.h +F: tools/net/ynl/samples/wireguard.c F: tools/testing/selftests/wireguard/ WISTRON LAPTOP BUTTON DRIVER
tools/include/uapi/linux/wireguard.h new file +79 -0
79 lines not on this page · raw message
tools/net/ynl/samples/.gitignore +1 -0
@@ -7,3 +7,4 @@ rt-addr rt-link rt-route tc +wireguard
tools/net/ynl/samples/wireguard.c new file +104 -0
104 lines not on this page · raw message
Signature · 1 line
2.51.0
Jakub Kicinski
maintainerRe: [PATCH net-next v2 08/11] tools: ynl: add sample for wireguard
Quoted 1 line · On Fri, 31 Oct 2025 16:05:34 +0000 Asbjørn Sloth Tønnesen wrote:
+F:
tools/include/uapi/linux/wireguard.h
Please don't copy the headers. Add an entry in Makefile.deps instead.
PATCH v2 9/11 wireguard: netlink: convert to split ops
In reply to Asbjørn Sloth TønnesenThis patch converts wireguard from using legacy struct genl_ops to struct genl_split_ops, by applying the same transformation as genl_cmd_full_to_split() would otherwise do at runtime.
WGDEVICE_A_MAX is swapped for WGDEVICE_A_PEERS, while they are currently equivalent, then .maxattr should be the maximum attribute that a given command supports, which might not be WGDEVICE_A_MAX.
This is an incremental step towards adopting netlink policy code generated by ynl-gen, ensuring that the code and spec is aligned.
This is a trivial patch with no behavioural changes intended.
--- drivers/net/wireguard/netlink.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/net/wireguard/netlink.c +9 -7
@@ -616,28 +616,30 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)return ret; } -static const struct genl_ops genl_ops[] = { +static const struct genl_split_ops wireguard_nl_ops[] = { { .cmd =WG_CMD_GET_DEVICE , .start = wg_get_device_start, .dumpit = wg_get_device_dump, .done = wg_get_device_done, - .flags =GENL_UNS_ADMIN_PERM + .policy = device_policy, + .maxattr =WGDEVICE_A_PEERS , + .flags =GENL_UNS_ADMIN_PERM |GENL_CMD_CAP_DUMP , }, { .cmd =WG_CMD_SET_DEVICE , .doit = wg_set_device, - .flags =GENL_UNS_ADMIN_PERM + .policy = device_policy, + .maxattr =WGDEVICE_A_PEERS , + .flags =GENL_UNS_ADMIN_PERM |GENL_CMD_CAP_DO , } };static struct genl_family genl_family__ro_after_init = { - .ops = genl_ops, - .n_ops =ARRAY_SIZE (genl_ops), + .split_ops = wireguard_nl_ops, + .n_split_ops =ARRAY_SIZE (wireguard_nl_ops), .name =WG_GENL_NAME , .version =WG_GENL_VERSION , - .maxattr =WGDEVICE_A_MAX , .module =THIS_MODULE , - .policy = device_policy, .netnsok = true };
Signature · 1 line
2.51.0
PATCH v2 10/11 wireguard: netlink: rename netlink handlers
In reply to Asbjørn Sloth TønnesenRename netlink handlers to use the naming expected by ynl-gen.
This is an incremental step towards adopting netlink command definitions generated by ynl-gen.
This is a trivial patch with no behavioural changes intended.
--- drivers/net/wireguard/netlink.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
drivers/net/wireguard/netlink.c +10 -8
@@ -199,7 +199,7 @@ get_peer(struct wg_peer *peer, struct sk_buff *skb, struct dump_ctx *ctx)return -EMSGSIZE ; } -static int wg_get_device_start (struct netlink_callback *cb) +static int wireguard_nl_get_device_start (struct netlink_callback *cb) {struct wg_device *wg;
@@ -210,7 +210,8 @@ static int wg_get_device_start(struct netlink_callback *cb)return 0 ; } -static int wg_get_device_dump (struct sk_buff *skb,struct netlink_callback *cb) +static int wireguard_nl_get_device_dumpit (struct sk_buff *skb, +struct netlink_callback *cb) {struct wg_peer *peer, *next_peer_cursor;struct dump_ctx *ctx =DUMP_CTX (cb);
@@ -304,7 +305,7 @@ static int wg_get_device_dump(struct sk_buff *skb, struct netlink_callback *cb)*/ } -static int wg_get_device_done (struct netlink_callback *cb) +static int wireguard_nl_get_device_done (struct netlink_callback *cb) {struct dump_ctx *ctx =DUMP_CTX (cb);
@@ -502,7 +503,8 @@ static int set_peer(struct wg_device *wg, struct nlattr **attrs)return ret; } -static int wg_set_device (struct sk_buff *skb,struct genl_info *info) +static int wireguard_nl_set_device_doit (struct sk_buff *skb, +struct genl_info *info) {struct wg_device *wg =lookup_interface (info->attrs, skb);u32 flags =0 ;
@@ -619,15 +621,15 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)static const struct genl_split_ops wireguard_nl_ops[] = { { .cmd =WG_CMD_GET_DEVICE , - .start = wg_get_device_start, - .dumpit = wg_get_device_dump, - .done = wg_get_device_done, + .start = wireguard_nl_get_device_start, + .dumpit = wireguard_nl_get_device_dumpit, + .done = wireguard_nl_get_device_done, .policy = device_policy, .maxattr =WGDEVICE_A_PEERS , .flags =GENL_UNS_ADMIN_PERM |GENL_CMD_CAP_DUMP , }, { .cmd =WG_CMD_SET_DEVICE , - .doit = wg_set_device, + .doit = wireguard_nl_set_device_doit, .policy = device_policy, .maxattr =WGDEVICE_A_PEERS , .flags =GENL_UNS_ADMIN_PERM |GENL_CMD_CAP_DO ,
Signature · 1 line
2.51.0
PATCH v2 11/11 wireguard: netlink: generate netlink code
In reply to Asbjørn Sloth TønnesenThis patch adopts netlink policy and command definitions as generated by ynl-gen, thus completing the conversion to YNL.
Given that the old and new policy is functionally identical, and just moved to a new file, then it serves to verify that the policy in the spec in identical to the previous policy code.
The new files are covered by drivers/net/wireguard/ pattern in MAINTAINERS.
No behavioural changes intended.
--- drivers/net/wireguard/Makefile | 1 + drivers/net/wireguard/netlink.c | 64 +++--------------------- drivers/net/wireguard/netlink_gen.c | 77 +++++++++++++++++++++++++++++ drivers/net/wireguard/netlink_gen.h | 29 +++++++++++ 4 files changed, 114 insertions(+), 57 deletions(-) create mode 100644 drivers/net/wireguard/netlink_gen.c create mode 100644 drivers/net/wireguard/netlink_gen.h
drivers/net/wireguard/Makefile +1 -0
@@ -14,4 +14,5 @@ wireguard-y += allowedips.o wireguard-y += ratelimiter.o wireguard-y += cookie.o wireguard-y += netlink.o +wireguard-y += netlink_gen.o obj-$(CONFIG_WIREGUARD) := wireguard.o
drivers/net/wireguard/netlink.c +7 -57
106 lines not on this page · raw message
drivers/net/wireguard/netlink_gen.c new file +77 -0
77 lines not on this page · raw message
drivers/net/wireguard/netlink_gen.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */ +/* Do not edit directly, auto-generated from: */ +/* Documentation/netlink/specs/wireguard.yaml */ +/* YNL-GEN kernel header */ + +#ifndef _LINUX_WIREGUARD_GEN_H +#define _LINUX_WIREGUARD_GEN_H + +#include <net/netlink.h> +#include <net/genetlink.h> + +#include <uapi/linux/wireguard.h> +#include <linux/time_types.h> + +/* Common nested types */ +extern const struct nla_policy wireguard_wgallowedip_nl_policy[WGALLOWEDIP_A_FLAGS +1 ]; +extern const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_PROTOCOL_VERSION +1 ]; + +/* Ops table for wireguard */ +extern const struct genl_split_ops wireguard_nl_ops[2 ]; + +int wireguard_nl_get_device_start (struct netlink_callback *cb); +int wireguard_nl_get_device_done (struct netlink_callback *cb); + +int wireguard_nl_get_device_dumpit (struct sk_buff *skb, +struct netlink_callback *cb); +int wireguard_nl_set_device_doit (struct sk_buff *skb,struct genl_info *info); + +#endif /* _LINUX_WIREGUARD_GEN_H */
Signature · 1 line
2.51.0