Skip to content

Commit 3fd2f3d

Browse files
Asbjørn Sloth Tønnesenzx2c4
authored andcommitted
wireguard: netlink: generate netlink code
This patch adopts netlink policies and command definitions generated by ynl-gen, thus completing the conversion to YNL. Given that the old and new policies are functionally identical and have just been moved to a new file, it serves to verify that the policies generated from the spec are identical to the previous policy code. The following functions are renamed: wg_get_device_dump() -> wg_get_device_dumpit() wg_set_device() -> wg_set_device_doit() The new files are covered by the existing drivers/net/wireguard/ pattern in MAINTAINERS. No behavioural changes intended. Signed-off-by: Asbjørn Sloth Tønnesen <[email protected]> Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent 88cedad commit 3fd2f3d

File tree

4 files changed

+109
-56
lines changed

4 files changed

+109
-56
lines changed

drivers/net/wireguard/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ wireguard-y += peerlookup.o
1313
wireguard-y += allowedips.o
1414
wireguard-y += ratelimiter.o
1515
wireguard-y += cookie.o
16-
wireguard-y += netlink.o
16+
wireguard-y += netlink.o generated/netlink.o
1717
obj-$(CONFIG_WIREGUARD) := wireguard.o
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
2+
/* Do not edit directly, auto-generated from: */
3+
/* Documentation/netlink/specs/wireguard.yaml */
4+
/* YNL-GEN kernel source */
5+
/* YNL-ARG --function-prefix wg */
6+
/* To regenerate run: tools/net/ynl/ynl-regen.sh */
7+
8+
#include <net/netlink.h>
9+
#include <net/genetlink.h>
10+
11+
#include "netlink.h"
12+
13+
#include <uapi/linux/wireguard.h>
14+
#include <linux/time_types.h>
15+
16+
/* Common nested types */
17+
const struct nla_policy wireguard_wgallowedip_nl_policy[WGALLOWEDIP_A_FLAGS + 1] = {
18+
[WGALLOWEDIP_A_FAMILY] = { .type = NLA_U16, },
19+
[WGALLOWEDIP_A_IPADDR] = NLA_POLICY_MIN_LEN(4),
20+
[WGALLOWEDIP_A_CIDR_MASK] = { .type = NLA_U8, },
21+
[WGALLOWEDIP_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x1),
22+
};
23+
24+
const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_PROTOCOL_VERSION + 1] = {
25+
[WGPEER_A_PUBLIC_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN),
26+
[WGPEER_A_PRESHARED_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN),
27+
[WGPEER_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x7),
28+
[WGPEER_A_ENDPOINT] = NLA_POLICY_MIN_LEN(16),
29+
[WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL] = { .type = NLA_U16, },
30+
[WGPEER_A_LAST_HANDSHAKE_TIME] = NLA_POLICY_EXACT_LEN(16),
31+
[WGPEER_A_RX_BYTES] = { .type = NLA_U64, },
32+
[WGPEER_A_TX_BYTES] = { .type = NLA_U64, },
33+
[WGPEER_A_ALLOWEDIPS] = NLA_POLICY_NESTED_ARRAY(wireguard_wgallowedip_nl_policy),
34+
[WGPEER_A_PROTOCOL_VERSION] = { .type = NLA_U32, },
35+
};
36+
37+
/* WG_CMD_GET_DEVICE - dump */
38+
static const struct nla_policy wireguard_get_device_nl_policy[WGDEVICE_A_IFNAME + 1] = {
39+
[WGDEVICE_A_IFINDEX] = { .type = NLA_U32, },
40+
[WGDEVICE_A_IFNAME] = { .type = NLA_NUL_STRING, .len = 15, },
41+
};
42+
43+
/* WG_CMD_SET_DEVICE - do */
44+
static const struct nla_policy wireguard_set_device_nl_policy[WGDEVICE_A_PEERS + 1] = {
45+
[WGDEVICE_A_IFINDEX] = { .type = NLA_U32, },
46+
[WGDEVICE_A_IFNAME] = { .type = NLA_NUL_STRING, .len = 15, },
47+
[WGDEVICE_A_PRIVATE_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN),
48+
[WGDEVICE_A_PUBLIC_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN),
49+
[WGDEVICE_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x1),
50+
[WGDEVICE_A_LISTEN_PORT] = { .type = NLA_U16, },
51+
[WGDEVICE_A_FWMARK] = { .type = NLA_U32, },
52+
[WGDEVICE_A_PEERS] = NLA_POLICY_NESTED_ARRAY(wireguard_wgpeer_nl_policy),
53+
};
54+
55+
/* Ops table for wireguard */
56+
const struct genl_split_ops wireguard_nl_ops[2] = {
57+
{
58+
.cmd = WG_CMD_GET_DEVICE,
59+
.start = wg_get_device_start,
60+
.dumpit = wg_get_device_dumpit,
61+
.done = wg_get_device_done,
62+
.policy = wireguard_get_device_nl_policy,
63+
.maxattr = WGDEVICE_A_IFNAME,
64+
.flags = GENL_UNS_ADMIN_PERM | GENL_CMD_CAP_DUMP,
65+
},
66+
{
67+
.cmd = WG_CMD_SET_DEVICE,
68+
.doit = wg_set_device_doit,
69+
.policy = wireguard_set_device_nl_policy,
70+
.maxattr = WGDEVICE_A_PEERS,
71+
.flags = GENL_UNS_ADMIN_PERM | GENL_CMD_CAP_DO,
72+
},
73+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
2+
/* Do not edit directly, auto-generated from: */
3+
/* Documentation/netlink/specs/wireguard.yaml */
4+
/* YNL-GEN kernel header */
5+
/* YNL-ARG --function-prefix wg */
6+
/* To regenerate run: tools/net/ynl/ynl-regen.sh */
7+
8+
#ifndef _LINUX_WIREGUARD_GEN_H
9+
#define _LINUX_WIREGUARD_GEN_H
10+
11+
#include <net/netlink.h>
12+
#include <net/genetlink.h>
13+
14+
#include <uapi/linux/wireguard.h>
15+
#include <linux/time_types.h>
16+
17+
/* Common nested types */
18+
extern const struct nla_policy wireguard_wgallowedip_nl_policy[WGALLOWEDIP_A_FLAGS + 1];
19+
extern const struct nla_policy wireguard_wgpeer_nl_policy[WGPEER_A_PROTOCOL_VERSION + 1];
20+
21+
/* Ops table for wireguard */
22+
extern const struct genl_split_ops wireguard_nl_ops[2];
23+
24+
int wg_get_device_start(struct netlink_callback *cb);
25+
int wg_get_device_done(struct netlink_callback *cb);
26+
27+
int wg_get_device_dumpit(struct sk_buff *skb, struct netlink_callback *cb);
28+
int wg_set_device_doit(struct sk_buff *skb, struct genl_info *info);
29+
30+
#endif /* _LINUX_WIREGUARD_GEN_H */

drivers/net/wireguard/netlink.c

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "socket.h"
1010
#include "queueing.h"
1111
#include "messages.h"
12+
#include "generated/netlink.h"
1213

1314
#include <uapi/linux/wireguard.h>
1415

@@ -18,39 +19,6 @@
1819
#include <crypto/utils.h>
1920

2021
static struct genl_family genl_family;
21-
static const struct nla_policy peer_policy[WGPEER_A_MAX + 1];
22-
static const struct nla_policy allowedip_policy[WGALLOWEDIP_A_MAX + 1];
23-
24-
static const struct nla_policy device_policy[WGDEVICE_A_MAX + 1] = {
25-
[WGDEVICE_A_IFINDEX] = { .type = NLA_U32 },
26-
[WGDEVICE_A_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
27-
[WGDEVICE_A_PRIVATE_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN),
28-
[WGDEVICE_A_PUBLIC_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN),
29-
[WGDEVICE_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x1),
30-
[WGDEVICE_A_LISTEN_PORT] = { .type = NLA_U16 },
31-
[WGDEVICE_A_FWMARK] = { .type = NLA_U32 },
32-
[WGDEVICE_A_PEERS] = NLA_POLICY_NESTED_ARRAY(peer_policy),
33-
};
34-
35-
static const struct nla_policy peer_policy[WGPEER_A_MAX + 1] = {
36-
[WGPEER_A_PUBLIC_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN),
37-
[WGPEER_A_PRESHARED_KEY] = NLA_POLICY_EXACT_LEN(WG_KEY_LEN),
38-
[WGPEER_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x7),
39-
[WGPEER_A_ENDPOINT] = NLA_POLICY_MIN_LEN(sizeof(struct sockaddr)),
40-
[WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL] = { .type = NLA_U16 },
41-
[WGPEER_A_LAST_HANDSHAKE_TIME] = NLA_POLICY_EXACT_LEN(sizeof(struct __kernel_timespec)),
42-
[WGPEER_A_RX_BYTES] = { .type = NLA_U64 },
43-
[WGPEER_A_TX_BYTES] = { .type = NLA_U64 },
44-
[WGPEER_A_ALLOWEDIPS] = NLA_POLICY_NESTED_ARRAY(allowedip_policy),
45-
[WGPEER_A_PROTOCOL_VERSION] = { .type = NLA_U32 }
46-
};
47-
48-
static const struct nla_policy allowedip_policy[WGALLOWEDIP_A_MAX + 1] = {
49-
[WGALLOWEDIP_A_FAMILY] = { .type = NLA_U16 },
50-
[WGALLOWEDIP_A_IPADDR] = NLA_POLICY_MIN_LEN(sizeof(struct in_addr)),
51-
[WGALLOWEDIP_A_CIDR_MASK] = { .type = NLA_U8 },
52-
[WGALLOWEDIP_A_FLAGS] = NLA_POLICY_MASK(NLA_U32, 0x1),
53-
};
5422

5523
static struct wg_device *lookup_interface(struct nlattr **attrs,
5624
struct sk_buff *skb)
@@ -199,7 +167,7 @@ get_peer(struct wg_peer *peer, struct sk_buff *skb, struct dump_ctx *ctx)
199167
return -EMSGSIZE;
200168
}
201169

202-
static int wg_get_device_start(struct netlink_callback *cb)
170+
int wg_get_device_start(struct netlink_callback *cb)
203171
{
204172
struct wg_device *wg;
205173

@@ -210,7 +178,7 @@ static int wg_get_device_start(struct netlink_callback *cb)
210178
return 0;
211179
}
212180

213-
static int wg_get_device_dump(struct sk_buff *skb, struct netlink_callback *cb)
181+
int wg_get_device_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
214182
{
215183
struct wg_peer *peer, *next_peer_cursor;
216184
struct dump_ctx *ctx = DUMP_CTX(cb);
@@ -304,7 +272,7 @@ static int wg_get_device_dump(struct sk_buff *skb, struct netlink_callback *cb)
304272
*/
305273
}
306274

307-
static int wg_get_device_done(struct netlink_callback *cb)
275+
int wg_get_device_done(struct netlink_callback *cb)
308276
{
309277
struct dump_ctx *ctx = DUMP_CTX(cb);
310278

@@ -502,7 +470,7 @@ static int set_peer(struct wg_device *wg, struct nlattr **attrs)
502470
return ret;
503471
}
504472

505-
static int wg_set_device(struct sk_buff *skb, struct genl_info *info)
473+
int wg_set_device_doit(struct sk_buff *skb, struct genl_info *info)
506474
{
507475
struct wg_device *wg = lookup_interface(info->attrs, skb);
508476
u32 flags = 0;
@@ -616,24 +584,6 @@ static int wg_set_device(struct sk_buff *skb, struct genl_info *info)
616584
return ret;
617585
}
618586

619-
static const struct genl_split_ops wireguard_nl_ops[] = {
620-
{
621-
.cmd = WG_CMD_GET_DEVICE,
622-
.start = wg_get_device_start,
623-
.dumpit = wg_get_device_dump,
624-
.done = wg_get_device_done,
625-
.policy = device_policy,
626-
.maxattr = WGDEVICE_A_IFNAME,
627-
.flags = GENL_UNS_ADMIN_PERM | GENL_CMD_CAP_DUMP,
628-
}, {
629-
.cmd = WG_CMD_SET_DEVICE,
630-
.doit = wg_set_device,
631-
.policy = device_policy,
632-
.maxattr = WGDEVICE_A_PEERS,
633-
.flags = GENL_UNS_ADMIN_PERM | GENL_CMD_CAP_DO,
634-
}
635-
};
636-
637587
static struct genl_family genl_family __ro_after_init = {
638588
.split_ops = wireguard_nl_ops,
639589
.n_split_ops = ARRAY_SIZE(wireguard_nl_ops),

0 commit comments

Comments
 (0)