-
Notifications
You must be signed in to change notification settings - Fork 4
/
wg-linux.c
353 lines (275 loc) · 7.58 KB
/
wg-linux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
*
* Based on wireguard-tools:
* Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
#define _GNU_SOURCE
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <linux/rtnetlink.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include <netlink/socket.h>
#include <unl.h>
#include "linux/wireguard.h"
#include "unetd.h"
struct timespec64 {
int64_t tv_sec;
int64_t tv_nsec;
};
struct wg_linux_peer_req {
struct nl_msg *msg;
struct nlattr *peers, *entry, *ips;
};
static struct unl unl;
static int
wg_nl_init(void)
{
int ret;
if (unl.sock)
return 0;
ret = unl_genl_init(&unl, "wireguard");
if (ret)
return ret;
nl_socket_set_buffer_size(unl.sock, 32768, 32768);
nlmsg_set_default_size(32768);
return 0;
}
static struct nl_msg *
wg_genl_msg(struct network *net, bool set)
{
struct nl_msg *msg;
msg = unl_genl_msg(&unl, set ? WG_CMD_SET_DEVICE : WG_CMD_GET_DEVICE, !set);
nla_put_string(msg, WGDEVICE_A_IFNAME, network_name(net));
return msg;
}
static int
wg_genl_call(struct nl_msg *msg)
{
return unl_request(&unl, msg, NULL, NULL);
}
static int
__wg_linux_init(struct network *net, void *key)
{
struct nl_msg *msg;
msg = wg_genl_msg(net, true);
nla_put(msg, WGDEVICE_A_PRIVATE_KEY, WG_KEY_LEN, key);
nla_put_u32(msg, WGDEVICE_A_FLAGS, WGDEVICE_F_REPLACE_PEERS);
return wg_genl_call(msg);
}
static void
wg_linux_cleanup(struct network *net)
{
uint8_t key[WG_KEY_LEN] = {};
__wg_linux_init(net, key);
}
static int
wg_linux_init(struct network *net)
{
if (wg_nl_init())
return -1;
return __wg_linux_init(net, net->config.key);
}
static int
wg_linux_init_local(struct network *net, struct network_peer *peer)
{
struct nl_msg *msg;
msg = wg_genl_msg(net, true);
nla_put_u16(msg, WGDEVICE_A_LISTEN_PORT, peer ? peer->port : 0);
return wg_genl_call(msg);
}
static void
wg_linux_msg_add_ip(struct nl_msg *msg, int af, void *addr, int mask)
{
struct nlattr *ip;
int len;
if (af == AF_INET6)
len = sizeof(struct in6_addr);
else
len = sizeof(struct in_addr);
ip = nla_nest_start(msg, 0);
nla_put_u16(msg, WGALLOWEDIP_A_FAMILY, af);
nla_put(msg, WGALLOWEDIP_A_IPADDR, len, addr);
nla_put_u8(msg, WGALLOWEDIP_A_CIDR_MASK, mask);
nla_nest_end(msg, ip);
}
static struct nl_msg *
wg_linux_peer_req_init(struct network *net, struct network_peer *peer,
struct wg_linux_peer_req *req)
{
req->msg = wg_genl_msg(net, true);
req->peers = nla_nest_start(req->msg, WGDEVICE_A_PEERS);
req->entry = nla_nest_start(req->msg, 0);
nla_put(req->msg, WGPEER_A_PUBLIC_KEY, WG_KEY_LEN, peer->key);
return req->msg;
}
static int
wg_linux_peer_req_done(struct wg_linux_peer_req *req)
{
nla_nest_end(req->msg, req->entry);
nla_nest_end(req->msg, req->peers);
return wg_genl_call(req->msg);
}
static struct nl_msg *
wg_linux_peer_msg_size_check(struct wg_linux_peer_req *req, struct network *net,
struct network_peer *peer)
{
if (nlmsg_get_max_size(req->msg) >
nlmsg_total_size(nlmsg_hdr(req->msg)->nlmsg_len) + 256)
return req->msg;
nla_nest_end(req->msg, req->ips);
wg_linux_peer_req_done(req);
wg_linux_peer_req_init(net, peer, req);
req->ips = nla_nest_start(req->msg, WGPEER_A_ALLOWEDIPS);
return req->msg;
}
static void
wg_linux_peer_msg_add_allowed_ip(struct wg_linux_peer_req *req, struct network *net,
struct network_peer *peer)
{
struct nl_msg *msg = req->msg;
struct blob_attr *cur;
int rem;
wg_linux_msg_add_ip(msg, AF_INET6, &peer->local_addr.in6, 128);
msg = wg_linux_peer_msg_size_check(req, net, peer);
blobmsg_for_each_attr(cur, peer->ipaddr, rem) {
const char *str = blobmsg_get_string(cur);
struct in6_addr in6;
int af, mask;
if (strchr(str, ':')) {
af = AF_INET6;
mask = 128;
} else {
af = AF_INET;
mask = 32;
}
if (inet_pton(af, str, &in6) != 1)
continue;
wg_linux_msg_add_ip(msg, af, &in6, mask);
msg = wg_linux_peer_msg_size_check(req, net, peer);
}
blobmsg_for_each_attr(cur, peer->subnet, rem) {
const char *str = blobmsg_get_string(cur);
union network_addr addr;
int mask;
int af;
af = strchr(str, ':') ? AF_INET6 : AF_INET;
if (network_get_subnet(af, &addr, &mask, str))
continue;
wg_linux_msg_add_ip(msg, af, &addr, mask);
msg = wg_linux_peer_msg_size_check(req, net, peer);
}
}
static int
wg_linux_peer_update(struct network *net, struct network_peer *peer, enum wg_update_cmd cmd)
{
struct wg_linux_peer_req req;
struct network_host *host;
wg_linux_peer_req_init(net, peer, &req);
if (cmd == WG_PEER_DELETE) {
nla_put_u32(req.msg, WGPEER_A_FLAGS, WGPEER_F_REMOVE_ME);
goto out;
}
nla_put_u32(req.msg, WGPEER_A_FLAGS, WGPEER_F_REPLACE_ALLOWEDIPS);
req.ips = nla_nest_start(req.msg, WGPEER_A_ALLOWEDIPS);
wg_linux_peer_msg_add_allowed_ip(&req, net, peer);
for_each_routed_host(host, net, peer)
wg_linux_peer_msg_add_allowed_ip(&req, net, &host->peer);
nla_nest_end(req.msg, req.ips);
out:
return wg_linux_peer_req_done(&req);
}
static void
wg_linux_parse_peer(struct network *net, struct nlattr *data, time_t now)
{
struct network_peer *peer = NULL;
struct nlattr *tb[__WGPEER_A_LAST];
struct nlattr *cur;
nla_parse_nested(tb, WGPEER_A_MAX, data, NULL);
cur = tb[WGPEER_A_PUBLIC_KEY];
if (!cur)
return;
peer = wg_peer_update_start(net, nla_data(cur));
if (!peer)
return;
if ((cur = tb[WGPEER_A_LAST_HANDSHAKE_TIME]) != NULL) {
struct timespec64 *tv = nla_data(cur);
wg_peer_set_last_handshake(net, peer, now, tv->tv_sec);
}
if ((cur = tb[WGPEER_A_RX_BYTES]) != NULL)
wg_peer_set_rx_bytes(net, peer, nla_get_u64(cur));
if ((cur = tb[WGPEER_A_ENDPOINT]) != NULL)
wg_peer_set_endpoint(net, peer, nla_data(cur), nla_len(cur));
wg_peer_update_done(net, peer);
}
static void
wg_linux_parse_peer_list(struct network *net, struct nlattr *data, time_t now)
{
struct nlattr *cur;
int rem;
if (!data)
return;
nla_for_each_nested(cur, data, rem)
wg_linux_parse_peer(net, cur, now);
}
static int
wg_linux_get_cb(struct nl_msg *msg, void *arg)
{
struct nlmsghdr *nh = nlmsg_hdr(msg);
struct network *net = arg;
struct nlattr *tb[__WGDEVICE_A_LAST];
time_t now = time(NULL);
nlmsg_parse(nh, sizeof(struct genlmsghdr), tb, __WGDEVICE_A_LAST, NULL);
wg_linux_parse_peer_list(net, tb[WGDEVICE_A_PEERS], now);
return NL_SKIP;
}
static int
wg_linux_peer_refresh(struct network *net)
{
struct nl_msg *msg = wg_genl_msg(net, false);
return unl_request(&unl, msg, wg_linux_get_cb, net);
}
static int
wg_linux_peer_connect(struct network *net, struct network_peer *peer,
union network_endpoint *ep)
{
struct wg_linux_peer_req req;
struct nl_msg *msg;
int len;
msg = wg_linux_peer_req_init(net, peer, &req);
if (net->net_config.keepalive) {
nla_put_u16(msg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL, 0);
wg_linux_peer_req_done(&req);
msg = wg_linux_peer_req_init(net, peer, &req);
nla_put_u16(msg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
net->net_config.keepalive);
}
if (ep->in.sin_family == AF_INET6)
len = sizeof(ep->in6);
else
len = sizeof(ep->in);
nla_put(msg, WGPEER_A_ENDPOINT, len, &ep->in6);
return wg_linux_peer_req_done(&req);
}
const struct wg_ops wg_linux_ops = {
.name = "user",
.init = wg_linux_init,
.cleanup = wg_linux_cleanup,
.init_local = wg_linux_init_local,
.peer_update = wg_linux_peer_update,
.peer_refresh = wg_linux_peer_refresh,
.peer_connect = wg_linux_peer_connect,
};