-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemd.patch
298 lines (285 loc) · 14.5 KB
/
systemd.patch
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
From 17841aad1f39e001562208e9146bdb1370dd0752 Mon Sep 17 00:00:00 2001
From: Ivan Trubach <[email protected]>
Date: Fri, 27 Sep 2024 14:02:26 +0300
Subject: [PATCH] network/wireguard: add support for AmneziaWG
---
src/basic/linux/wireguard.h | 35 +++++++-
.../sd-netlink/netlink-types-genl.c | 10 +++
src/network/netdev/netdev-gperf.gperf | 10 +++
src/network/netdev/wireguard.c | 79 ++++++++++++++++++-
src/network/netdev/wireguard.h | 12 +++
5 files changed, 144 insertions(+), 2 deletions(-)
diff --git a/src/basic/linux/wireguard.h b/src/basic/linux/wireguard.h
index ae88be14c9..2270094d47 100644
--- a/src/basic/linux/wireguard.h
+++ b/src/basic/linux/wireguard.h
@@ -49,9 +49,19 @@
* ...
* ...
* WGPEER_A_PROTOCOL_VERSION: NLA_U32
+ * WGPEER_A_ADVANCED_SECURITY: NLA_FLAG
* 0: NLA_NESTED
* ...
* ...
+ * WGDEVICE_A_JC: NLA_U16
+ * WGDEVICE_A_JMIN: NLA_U16
+ * WGDEVICE_A_JMAX: NLA_U16
+ * WGDEVICE_A_S1: NLA_U16
+ * WGDEVICE_A_S2: NLA_U16
+ * WGDEVICE_A_H1: NLA_U32
+ * WGDEVICE_A_H2: NLA_U32
+ * WGDEVICE_A_H3: NLA_U32
+ * WGDEVICE_A_H4: NLA_U32
*
* 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
@@ -111,9 +121,21 @@
* most recent protocol will be used when
* this is unset. Otherwise, must be set
* to 1.
+ * WGPEER_A_ADVANCED_SECURITY: NLA_FLAG, flag indicating that
+ * advanced security techniques provided
+ * by AmneziaWG should be used.
* 0: NLA_NESTED
* ...
* ...
+ * WGDEVICE_A_JC: NLA_U16, junk packet count (Jc)
+ * WGDEVICE_A_JMIN: NLA_U16, junk packet minimum size (Jmin)
+ * WGDEVICE_A_JMAX: NLA_U16, junk packet maximum size (Jmax)
+ * WGDEVICE_A_S1: NLA_U16, init packet junk size (S1)
+ * WGDEVICE_A_S2: NLA_U16, response packet junk size (S2)
+ * WGDEVICE_A_H1: NLA_U32, init packet magic header (H1)
+ * WGDEVICE_A_H2: NLA_U32, response packet magic header (H2)
+ * WGDEVICE_A_H3: NLA_U32, cookie packet magic header (H3)
+ * WGDEVICE_A_H4: NLA_U32, transport packet magic header (H4)
*
* It is possible that the amount of configuration data exceeds that of
* the maximum message length accepted by the kernel. In that case, several
@@ -157,6 +179,15 @@ enum wgdevice_attribute {
WGDEVICE_A_LISTEN_PORT,
WGDEVICE_A_FWMARK,
WGDEVICE_A_PEERS,
+ WGDEVICE_A_JC,
+ WGDEVICE_A_JMIN,
+ WGDEVICE_A_JMAX,
+ WGDEVICE_A_S1,
+ WGDEVICE_A_S2,
+ WGDEVICE_A_H1,
+ WGDEVICE_A_H2,
+ WGDEVICE_A_H3,
+ WGDEVICE_A_H4,
__WGDEVICE_A_LAST
};
#define WGDEVICE_A_MAX (__WGDEVICE_A_LAST - 1)
@@ -165,8 +196,9 @@ enum wgpeer_flag {
WGPEER_F_REMOVE_ME = 1U << 0,
WGPEER_F_REPLACE_ALLOWEDIPS = 1U << 1,
WGPEER_F_UPDATE_ONLY = 1U << 2,
+ WGPEER_F_SET_ADVANCED_SECURITY = 1U << 3,
__WGPEER_F_ALL = WGPEER_F_REMOVE_ME | WGPEER_F_REPLACE_ALLOWEDIPS |
- WGPEER_F_UPDATE_ONLY
+ WGPEER_F_UPDATE_ONLY | WGPEER_F_SET_ADVANCED_SECURITY
};
enum wgpeer_attribute {
WGPEER_A_UNSPEC,
@@ -180,6 +212,7 @@ enum wgpeer_attribute {
WGPEER_A_TX_BYTES,
WGPEER_A_ALLOWEDIPS,
WGPEER_A_PROTOCOL_VERSION,
+ WGPEER_A_ADVANCED_SECURITY,
__WGPEER_A_LAST
};
#define WGPEER_A_MAX (__WGPEER_A_LAST - 1)
diff --git a/src/libsystemd/sd-netlink/netlink-types-genl.c b/src/libsystemd/sd-netlink/netlink-types-genl.c
index 226ac865c9..140803c231 100644
--- a/src/libsystemd/sd-netlink/netlink-types-genl.c
+++ b/src/libsystemd/sd-netlink/netlink-types-genl.c
@@ -218,6 +218,7 @@ static const NLAPolicy genl_wireguard_peer_policies[] = {
[WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL] = BUILD_POLICY(U16),
[WGPEER_A_ENDPOINT] = BUILD_POLICY(SOCKADDR),
[WGPEER_A_ALLOWEDIPS] = BUILD_POLICY_NESTED(genl_wireguard_allowedip),
+ [WGPEER_A_ADVANCED_SECURITY] = BUILD_POLICY(FLAG),
};
DEFINE_POLICY_SET(genl_wireguard_peer);
@@ -230,6 +231,15 @@ static const NLAPolicy genl_wireguard_policies[] = {
[WGDEVICE_A_LISTEN_PORT] = BUILD_POLICY(U16),
[WGDEVICE_A_FWMARK] = BUILD_POLICY(U32),
[WGDEVICE_A_PEERS] = BUILD_POLICY_NESTED(genl_wireguard_peer),
+ [WGDEVICE_A_JC] = BUILD_POLICY(U16),
+ [WGDEVICE_A_JMIN] = BUILD_POLICY(U16),
+ [WGDEVICE_A_JMAX] = BUILD_POLICY(U16),
+ [WGDEVICE_A_S1] = BUILD_POLICY(U16),
+ [WGDEVICE_A_S2] = BUILD_POLICY(U16),
+ [WGDEVICE_A_H1] = BUILD_POLICY(U32),
+ [WGDEVICE_A_H2] = BUILD_POLICY(U32),
+ [WGDEVICE_A_H3] = BUILD_POLICY(U32),
+ [WGDEVICE_A_H4] = BUILD_POLICY(U32),
};
/***************** genl families *****************/
diff --git a/src/network/netdev/netdev-gperf.gperf b/src/network/netdev/netdev-gperf.gperf
index be010665c5..4d338d6119 100644
--- a/src/network/netdev/netdev-gperf.gperf
+++ b/src/network/netdev/netdev-gperf.gperf
@@ -245,6 +245,15 @@ WireGuard.FwMark, config_parse_unsigned,
WireGuard.ListenPort, config_parse_wireguard_listen_port, 0, offsetof(Wireguard, port)
WireGuard.PrivateKey, config_parse_wireguard_private_key, 0, 0
WireGuard.PrivateKeyFile, config_parse_wireguard_private_key_file, 0, 0
+WireGuard.Jc, config_parse_uint16, 0, offsetof(Wireguard, jc)
+WireGuard.Jmin, config_parse_uint16, 0, offsetof(Wireguard, jmin)
+WireGuard.Jmax, config_parse_uint16, 0, offsetof(Wireguard, jmax)
+WireGuard.S1, config_parse_uint16, 0, offsetof(Wireguard, s1)
+WireGuard.S2, config_parse_uint16, 0, offsetof(Wireguard, s2)
+WireGuard.H1, config_parse_uint32, 0, offsetof(Wireguard, h1)
+WireGuard.H2, config_parse_uint32, 0, offsetof(Wireguard, h2)
+WireGuard.H3, config_parse_uint32, 0, offsetof(Wireguard, h3)
+WireGuard.H4, config_parse_uint32, 0, offsetof(Wireguard, h4)
WireGuard.RouteTable, config_parse_wireguard_route_table, 0, offsetof(Wireguard, route_table)
WireGuard.RouteMetric, config_parse_wireguard_route_priority, 0, offsetof(Wireguard, route_priority)
WireGuardPeer.AllowedIPs, config_parse_wireguard_allowed_ips, 0, 0
@@ -256,6 +265,7 @@ WireGuardPeer.PresharedKeyFile, config_parse_wireguard_peer_key_file,
WireGuardPeer.PersistentKeepalive, config_parse_wireguard_keepalive, 0, 0
WireGuardPeer.RouteTable, config_parse_wireguard_peer_route_table, 0, 0
WireGuardPeer.RouteMetric, config_parse_wireguard_peer_route_priority,0, 0
+WireGuardPeer.AdvancedSecurity, config_parse_wireguard_peer_advanced_security, 0, 0
Xfrm.InterfaceId, config_parse_uint32, 0, offsetof(Xfrm, if_id)
Xfrm.Independent, config_parse_bool, 0, offsetof(Xfrm, independent)
BatmanAdvanced.Aggregation, config_parse_bool, 0, offsetof(BatmanAdvanced, aggregation)
diff --git a/src/network/netdev/wireguard.c b/src/network/netdev/wireguard.c
index 9715cf4034..221e05239f 100644
--- a/src/network/netdev/wireguard.c
+++ b/src/network/netdev/wireguard.c
@@ -97,7 +97,7 @@ static int wireguard_peer_new_static(Wireguard *w, const char *filename, unsigne
return -ENOMEM;
*peer = (WireguardPeer) {
- .flags = WGPEER_F_REPLACE_ALLOWEDIPS,
+ .flags = WGPEER_F_REPLACE_ALLOWEDIPS | WGPEER_F_SET_ADVANCED_SECURITY,
.wireguard = w,
.section = TAKE_PTR(n),
};
@@ -191,6 +191,12 @@ static int wireguard_set_peer_one(NetDev *netdev, sd_netlink_message *message, c
if (r < 0)
goto cancel;
}
+
+ if (peer->advanced_security) {
+ r = sd_netlink_message_append_flag(message, WGPEER_A_ADVANCED_SECURITY);
+ if (r < 0)
+ goto cancel;
+ }
}
r = sd_netlink_message_open_container(message, WGPEER_A_ALLOWEDIPS);
@@ -263,6 +269,42 @@ static int wireguard_set_interface(NetDev *netdev) {
r = sd_netlink_message_append_u32(message, WGDEVICE_A_FLAGS, w->flags);
if (r < 0)
return log_netdev_error_errno(netdev, r, "Could not append wireguard flags: %m");
+
+ r = sd_netlink_message_append_u16(message, WGDEVICE_A_JC, w->jc);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard junk packet count: %m");
+
+ r = sd_netlink_message_append_u16(message, WGDEVICE_A_JMIN, w->jmin);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard minimum junk packet size: %m");
+
+ r = sd_netlink_message_append_u16(message, WGDEVICE_A_JMAX, w->jmax);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard maximum junk packet size: %m");
+
+ r = sd_netlink_message_append_u16(message, WGDEVICE_A_S1, w->s1);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard init packet junk size: %m");
+
+ r = sd_netlink_message_append_u16(message, WGDEVICE_A_S2, w->s2);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard response packet junk size: %m");
+
+ r = sd_netlink_message_append_u32(message, WGDEVICE_A_H1, w->h1);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard init packet magic header: %m");
+
+ r = sd_netlink_message_append_u32(message, WGDEVICE_A_H2, w->h2);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard response packet magic header: %m");
+
+ r = sd_netlink_message_append_u32(message, WGDEVICE_A_H3, w->h3);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard cookie packet magic header: %m");
+
+ r = sd_netlink_message_append_u32(message, WGDEVICE_A_H4, w->h4);
+ if (r < 0)
+ return log_netdev_error_errno(netdev, r, "Could not append wireguard transport packet magic header: %m");
}
r = sd_netlink_message_open_container(message, WGDEVICE_A_PEERS);
@@ -1059,6 +1101,41 @@ int config_parse_wireguard_peer_route_priority(
return 0;
}
+int config_parse_wireguard_peer_advanced_security(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ assert(rvalue);
+
+ Wireguard *w = WIREGUARD(data);
+ _cleanup_(wireguard_peer_free_or_set_invalidp) WireguardPeer *peer = NULL;
+ int r;
+
+ r = wireguard_peer_new_static(w, filename, section_line, &peer);
+ if (r < 0)
+ return log_oom();
+
+ r = parse_boolean(rvalue);
+ if (r < 0) {
+ log_syntax(unit, LOG_WARNING, filename, line, r,
+ "Could not parse advanced security \"%s\", ignoring assignment: %m", rvalue);
+ return 0;
+ }
+
+ peer->advanced_security = r > 0;
+
+ TAKE_PTR(peer);
+ return 0;
+}
+
static void wireguard_init(NetDev *netdev) {
Wireguard *w = WIREGUARD(netdev);
diff --git a/src/network/netdev/wireguard.h b/src/network/netdev/wireguard.h
index 8e5b3b58f5..5bc3a6cd9a 100644
--- a/src/network/netdev/wireguard.h
+++ b/src/network/netdev/wireguard.h
@@ -32,6 +32,7 @@ typedef struct WireguardPeer {
char *preshared_key_file;
uint32_t flags;
uint16_t persistent_keepalive_interval;
+ bool advanced_security;
union sockaddr_union endpoint;
char *endpoint_host;
@@ -60,6 +61,16 @@ struct Wireguard {
uint16_t port;
uint32_t fwmark;
+ uint16_t jc;
+ uint16_t jmin;
+ uint16_t jmax;
+ uint16_t s1;
+ uint16_t s2;
+ uint32_t h1;
+ uint32_t h2;
+ uint32_t h3;
+ uint32_t h4;
+
Hashmap *peers_by_section;
LIST_HEAD(WireguardPeer, peers);
@@ -83,3 +94,4 @@ CONFIG_PARSER_PROTOTYPE(config_parse_wireguard_route_table);
CONFIG_PARSER_PROTOTYPE(config_parse_wireguard_peer_route_table);
CONFIG_PARSER_PROTOTYPE(config_parse_wireguard_route_priority);
CONFIG_PARSER_PROTOTYPE(config_parse_wireguard_peer_route_priority);
+CONFIG_PARSER_PROTOTYPE(config_parse_wireguard_peer_advanced_security);
--
2.44.1