]>
Commit | Line | Data |
---|---|---|
3a56e697 SS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
9aa5d8ba | 3 | #include <netinet/in.h> |
3a56e697 SS |
4 | #include <linux/l2tp.h> |
5 | #include <linux/genetlink.h> | |
6 | ||
3a56e697 SS |
7 | #include "conf-parser.h" |
8 | #include "hashmap.h" | |
9 | #include "l2tp-tunnel.h" | |
3a56e697 | 10 | #include "netlink-util.h" |
d053d08a | 11 | #include "networkd-address.h" |
3a56e697 SS |
12 | #include "networkd-manager.h" |
13 | #include "parse-util.h" | |
14 | #include "socket-util.h" | |
15 | #include "string-table.h" | |
16 | #include "string-util.h" | |
17 | #include "util.h" | |
18 | ||
19 | static const char* const l2tp_l2spec_type_table[_NETDEV_L2TP_L2SPECTYPE_MAX] = { | |
20 | [NETDEV_L2TP_L2SPECTYPE_NONE] = "none", | |
21 | [NETDEV_L2TP_L2SPECTYPE_DEFAULT] = "default", | |
22 | }; | |
23 | ||
24 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(l2tp_l2spec_type, L2tpL2specType); | |
25 | ||
26 | static const char* const l2tp_encap_type_table[_NETDEV_L2TP_ENCAPTYPE_MAX] = { | |
27 | [NETDEV_L2TP_ENCAPTYPE_UDP] = "udp", | |
28 | [NETDEV_L2TP_ENCAPTYPE_IP] = "ip", | |
29 | }; | |
30 | ||
31 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(l2tp_encap_type, L2tpEncapType); | |
32 | DEFINE_CONFIG_PARSE_ENUM(config_parse_l2tp_encap_type, l2tp_encap_type, L2tpEncapType, "Failed to parse L2TP Encapsulation Type"); | |
33 | ||
d053d08a YW |
34 | static const char* const l2tp_local_address_type_table[_NETDEV_L2TP_LOCAL_ADDRESS_MAX] = { |
35 | [NETDEV_L2TP_LOCAL_ADDRESS_AUTO] = "auto", | |
36 | [NETDEV_L2TP_LOCAL_ADDRESS_STATIC] = "static", | |
37 | [NETDEV_L2TP_LOCAL_ADDRESS_DYNAMIC] = "dynamic", | |
38 | }; | |
39 | ||
40 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(l2tp_local_address_type, L2tpLocalAddressType); | |
41 | ||
3a56e697 SS |
42 | static void l2tp_session_free(L2tpSession *s) { |
43 | if (!s) | |
44 | return; | |
45 | ||
46 | if (s->tunnel && s->section) | |
a1422af5 | 47 | ordered_hashmap_remove(s->tunnel->sessions_by_section, s->section); |
3a56e697 SS |
48 | |
49 | network_config_section_free(s->section); | |
50 | ||
51 | free(s->name); | |
52 | ||
53 | free(s); | |
54 | } | |
55 | ||
56 | DEFINE_NETWORK_SECTION_FUNCTIONS(L2tpSession, l2tp_session_free); | |
57 | ||
58 | static int l2tp_session_new_static(L2tpTunnel *t, const char *filename, unsigned section_line, L2tpSession **ret) { | |
59 | _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL; | |
60 | _cleanup_(l2tp_session_freep) L2tpSession *s = NULL; | |
61 | int r; | |
62 | ||
63 | assert(t); | |
64 | assert(ret); | |
65 | assert(filename); | |
66 | assert(section_line > 0); | |
67 | ||
68 | r = network_config_section_new(filename, section_line, &n); | |
69 | if (r < 0) | |
70 | return r; | |
71 | ||
72 | s = ordered_hashmap_get(t->sessions_by_section, n); | |
73 | if (s) { | |
74 | *ret = TAKE_PTR(s); | |
75 | return 0; | |
76 | } | |
77 | ||
78 | s = new(L2tpSession, 1); | |
79 | if (!s) | |
80 | return -ENOMEM; | |
81 | ||
82 | *s = (L2tpSession) { | |
83 | .l2tp_l2spec_type = NETDEV_L2TP_L2SPECTYPE_DEFAULT, | |
84 | .tunnel = t, | |
85 | .section = TAKE_PTR(n), | |
86 | }; | |
87 | ||
88 | r = ordered_hashmap_ensure_allocated(&t->sessions_by_section, &network_config_hash_ops); | |
89 | if (r < 0) | |
90 | return r; | |
91 | ||
92 | r = ordered_hashmap_put(t->sessions_by_section, s->section, s); | |
93 | if (r < 0) | |
94 | return r; | |
95 | ||
96 | *ret = TAKE_PTR(s); | |
97 | return 0; | |
98 | } | |
99 | ||
d053d08a | 100 | static int netdev_l2tp_fill_message_tunnel(NetDev *netdev, union in_addr_union *local_address, sd_netlink_message **ret) { |
3a56e697 SS |
101 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
102 | uint16_t encap_type; | |
103 | L2tpTunnel *t; | |
104 | int r; | |
105 | ||
106 | assert(netdev); | |
d053d08a | 107 | assert(local_address); |
3a56e697 SS |
108 | |
109 | t = L2TP(netdev); | |
110 | ||
111 | assert(t); | |
112 | ||
113 | r = sd_genl_message_new(netdev->manager->genl, SD_GENL_L2TP, L2TP_CMD_TUNNEL_CREATE, &m); | |
114 | if (r < 0) | |
115 | return log_netdev_error_errno(netdev, r, "Failed to create generic netlink message: %m"); | |
116 | ||
117 | r = sd_netlink_message_append_u32(m, L2TP_ATTR_CONN_ID, t->tunnel_id); | |
118 | if (r < 0) | |
119 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_CONN_ID attribute: %m"); | |
120 | ||
121 | r = sd_netlink_message_append_u32(m, L2TP_ATTR_PEER_CONN_ID, t->peer_tunnel_id); | |
122 | if (r < 0) | |
123 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_PEER_CONN_ID attribute: %m"); | |
124 | ||
125 | r = sd_netlink_message_append_u8(m, L2TP_ATTR_PROTO_VERSION, 3); | |
126 | if (r < 0) | |
127 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_PROTO_VERSION attribute: %m"); | |
128 | ||
129 | switch(t->l2tp_encap_type) { | |
130 | case NETDEV_L2TP_ENCAPTYPE_IP: | |
131 | encap_type = L2TP_ENCAPTYPE_IP; | |
132 | break; | |
133 | case NETDEV_L2TP_ENCAPTYPE_UDP: | |
134 | default: | |
135 | encap_type = L2TP_ENCAPTYPE_UDP; | |
136 | break; | |
137 | } | |
138 | ||
139 | r = sd_netlink_message_append_u16(m, L2TP_ATTR_ENCAP_TYPE, encap_type); | |
140 | if (r < 0) | |
141 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_ENCAP_TYPE attribute: %m"); | |
142 | ||
143 | if (t->family == AF_INET) { | |
d053d08a | 144 | r = sd_netlink_message_append_in_addr(m, L2TP_ATTR_IP_SADDR, &local_address->in); |
3a56e697 SS |
145 | if (r < 0) |
146 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_IP_SADDR attribute: %m"); | |
147 | ||
148 | r = sd_netlink_message_append_in_addr(m, L2TP_ATTR_IP_DADDR, &t->remote.in); | |
149 | if (r < 0) | |
150 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_IP_DADDR attribute: %m"); | |
151 | } else { | |
d053d08a | 152 | r = sd_netlink_message_append_in6_addr(m, L2TP_ATTR_IP6_SADDR, &local_address->in6); |
3a56e697 SS |
153 | if (r < 0) |
154 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_IP6_SADDR attribute: %m"); | |
155 | ||
156 | r = sd_netlink_message_append_in6_addr(m, L2TP_ATTR_IP6_DADDR, &t->remote.in6); | |
157 | if (r < 0) | |
158 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_IP6_DADDR attribute: %m"); | |
159 | } | |
160 | ||
161 | if (encap_type == L2TP_ENCAPTYPE_UDP) { | |
162 | r = sd_netlink_message_append_u16(m, L2TP_ATTR_UDP_SPORT, t->l2tp_udp_sport); | |
163 | if (r < 0) | |
164 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_UDP_SPORT, attribute: %m"); | |
165 | ||
166 | r = sd_netlink_message_append_u16(m, L2TP_ATTR_UDP_DPORT, t->l2tp_udp_dport); | |
167 | if (r < 0) | |
168 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_UDP_DPORT attribute: %m"); | |
169 | ||
170 | if (t->udp_csum) { | |
171 | r = sd_netlink_message_append_u8(m, L2TP_ATTR_UDP_CSUM, t->udp_csum); | |
172 | if (r < 0) | |
173 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_UDP_CSUM attribute: %m"); | |
174 | } | |
175 | ||
176 | if (t->udp6_csum_tx) { | |
177 | r = sd_netlink_message_append_flag(m, L2TP_ATTR_UDP_ZERO_CSUM6_TX); | |
178 | if (r < 0) | |
179 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_UDP_ZERO_CSUM6_TX attribute: %m"); | |
180 | } | |
181 | ||
182 | if (t->udp6_csum_rx) { | |
183 | r = sd_netlink_message_append_flag(m, L2TP_ATTR_UDP_ZERO_CSUM6_RX); | |
184 | if (r < 0) | |
185 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_UDP_ZERO_CSUM6_RX attribute: %m"); | |
186 | } | |
187 | } | |
188 | ||
189 | *ret = TAKE_PTR(m); | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | static int netdev_l2tp_fill_message_session(NetDev *netdev, L2tpSession *session, sd_netlink_message **ret) { | |
195 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; | |
196 | uint16_t l2_spec_len; | |
197 | uint8_t l2_spec_type; | |
198 | int r; | |
199 | ||
200 | assert(netdev); | |
201 | assert(session); | |
202 | assert(session->tunnel); | |
203 | ||
204 | r = sd_genl_message_new(netdev->manager->genl, SD_GENL_L2TP, L2TP_CMD_SESSION_CREATE, &m); | |
205 | if (r < 0) | |
206 | return log_netdev_error_errno(netdev, r, "Failed to create generic netlink message: %m"); | |
207 | ||
208 | r = sd_netlink_message_append_u32(m, L2TP_ATTR_CONN_ID, session->tunnel->tunnel_id); | |
209 | if (r < 0) | |
210 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_CONN_ID attribute: %m"); | |
211 | ||
212 | r = sd_netlink_message_append_u32(m, L2TP_ATTR_PEER_CONN_ID, session->tunnel->peer_tunnel_id); | |
213 | if (r < 0) | |
214 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_PEER_CONN_ID attribute: %m"); | |
215 | ||
216 | r = sd_netlink_message_append_u32(m, L2TP_ATTR_SESSION_ID, session->session_id); | |
217 | if (r < 0) | |
218 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_SESSION_ID attribute: %m"); | |
219 | ||
220 | r = sd_netlink_message_append_u32(m, L2TP_ATTR_PEER_SESSION_ID, session->peer_session_id); | |
221 | if (r < 0) | |
222 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_PEER_SESSION_ID attribute: %m"); | |
223 | ||
224 | r = sd_netlink_message_append_u16(m, L2TP_ATTR_PW_TYPE, L2TP_PWTYPE_ETH); | |
225 | if (r < 0) | |
226 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_PW_TYPE attribute: %m"); | |
227 | ||
228 | switch (session->l2tp_l2spec_type) { | |
229 | case NETDEV_L2TP_L2SPECTYPE_NONE: | |
230 | l2_spec_type = L2TP_L2SPECTYPE_NONE; | |
231 | l2_spec_len = 0; | |
232 | break; | |
233 | case NETDEV_L2TP_L2SPECTYPE_DEFAULT: | |
234 | default: | |
235 | l2_spec_type = L2TP_L2SPECTYPE_DEFAULT; | |
236 | l2_spec_len = 4; | |
237 | break; | |
238 | } | |
239 | ||
240 | r = sd_netlink_message_append_u8(m, L2TP_ATTR_L2SPEC_TYPE, l2_spec_type); | |
241 | if (r < 0) | |
242 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_L2SPEC_TYPE attribute: %m"); | |
243 | ||
244 | r = sd_netlink_message_append_u8(m, L2TP_ATTR_L2SPEC_LEN, l2_spec_len); | |
245 | if (r < 0) | |
246 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_L2SPEC_LEN attribute: %m"); | |
247 | ||
248 | r = sd_netlink_message_append_string(m, L2TP_ATTR_IFNAME, session->name); | |
249 | if (r < 0) | |
250 | return log_netdev_error_errno(netdev, r, "Could not append L2TP_ATTR_IFNAME attribute: %m"); | |
251 | ||
252 | *ret = TAKE_PTR(m); | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
d053d08a YW |
257 | static int l2tp_acquire_local_address_one(L2tpTunnel *t, Address *a, union in_addr_union *ret) { |
258 | if (a->family != t->family) | |
259 | return -EINVAL; | |
260 | ||
261 | if (in_addr_is_null(a->family, &a->in_addr_peer) <= 0) | |
262 | return -EINVAL; | |
263 | ||
264 | if (t->local_address_type == NETDEV_L2TP_LOCAL_ADDRESS_STATIC && | |
265 | !FLAGS_SET(a->flags, IFA_F_PERMANENT)) | |
266 | return -EINVAL; | |
267 | ||
268 | if (t->local_address_type == NETDEV_L2TP_LOCAL_ADDRESS_DYNAMIC && | |
269 | FLAGS_SET(a->flags, IFA_F_PERMANENT)) | |
270 | return -EINVAL; | |
271 | ||
272 | *ret = a->in_addr; | |
273 | return 0; | |
274 | } | |
275 | ||
276 | static int l2tp_acquire_local_address(L2tpTunnel *t, Link *link, union in_addr_union *ret) { | |
277 | Address *a; | |
278 | Iterator i; | |
279 | ||
280 | assert(t); | |
281 | assert(link); | |
282 | assert(ret); | |
283 | assert(IN_SET(t->family, AF_INET, AF_INET6)); | |
284 | ||
285 | if (!in_addr_is_null(t->family, &t->local)) { | |
286 | /* local address is explicitly specified. */ | |
287 | *ret = t->local; | |
288 | return 0; | |
289 | } | |
290 | ||
291 | SET_FOREACH(a, link->addresses, i) | |
292 | if (l2tp_acquire_local_address_one(t, a, ret) >= 0) | |
293 | return 1; | |
294 | ||
295 | SET_FOREACH(a, link->addresses_foreign, i) | |
296 | if (l2tp_acquire_local_address_one(t, a, ret) >= 0) | |
297 | return 1; | |
298 | ||
299 | return -ENODATA; | |
300 | } | |
301 | ||
3a27af62 YW |
302 | static void l2tp_session_destroy_callback(L2tpSession *session) { |
303 | if (!session) | |
304 | return; | |
305 | ||
306 | netdev_unref(NETDEV(session->tunnel)); | |
307 | } | |
308 | ||
309 | static int l2tp_create_session_handler(sd_netlink *rtnl, sd_netlink_message *m, L2tpSession *session) { | |
310 | NetDev *netdev; | |
311 | int r; | |
312 | ||
313 | assert(session); | |
314 | assert(session->tunnel); | |
315 | ||
316 | netdev = NETDEV(session->tunnel); | |
317 | ||
318 | r = sd_netlink_message_get_errno(m); | |
319 | if (r == -EEXIST) | |
320 | log_netdev_info(netdev, "L2TP session %s exists, using existing without changing its parameters", | |
321 | session->name); | |
322 | else if (r < 0) { | |
323 | log_netdev_warning_errno(netdev, r, "L2TP session %s could not be created: %m", session->name); | |
324 | return 1; | |
325 | } | |
326 | ||
327 | log_netdev_debug(netdev, "L2TP session %s created", session->name); | |
328 | return 1; | |
329 | } | |
330 | ||
331 | static int l2tp_create_session(NetDev *netdev, L2tpSession *session) { | |
332 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *n = NULL; | |
333 | int r; | |
334 | ||
335 | r = netdev_l2tp_fill_message_session(netdev, session, &n); | |
336 | if (r < 0) | |
337 | return r; | |
338 | ||
339 | r = netlink_call_async(netdev->manager->genl, NULL, n, l2tp_create_session_handler, | |
340 | l2tp_session_destroy_callback, session); | |
341 | if (r < 0) | |
342 | return log_netdev_error_errno(netdev, r, "Failed to create L2TP session %s: %m", session->name); | |
343 | ||
344 | netdev_ref(netdev); | |
345 | return 0; | |
346 | } | |
347 | ||
348 | static int l2tp_create_tunnel_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) { | |
349 | L2tpSession *session; | |
350 | L2tpTunnel *t; | |
351 | Iterator i; | |
352 | int r; | |
353 | ||
354 | assert(netdev); | |
355 | assert(netdev->state != _NETDEV_STATE_INVALID); | |
356 | ||
357 | t = L2TP(netdev); | |
358 | ||
359 | assert(t); | |
360 | ||
361 | r = sd_netlink_message_get_errno(m); | |
362 | if (r == -EEXIST) | |
363 | log_netdev_info(netdev, "netdev exists, using existing without changing its parameters"); | |
364 | else if (r < 0) { | |
365 | log_netdev_warning_errno(netdev, r, "netdev could not be created: %m"); | |
366 | netdev_drop(netdev); | |
367 | ||
368 | return 1; | |
369 | } | |
370 | ||
371 | log_netdev_debug(netdev, "L2TP tunnel is created"); | |
372 | ||
373 | ORDERED_HASHMAP_FOREACH(session, t->sessions_by_section, i) | |
374 | (void) l2tp_create_session(netdev, session); | |
375 | ||
376 | return 1; | |
377 | } | |
378 | ||
379 | static int l2tp_create_tunnel(NetDev *netdev, Link *link) { | |
3a56e697 | 380 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; |
d053d08a | 381 | union in_addr_union local_address; |
3a56e697 | 382 | L2tpTunnel *t; |
3a56e697 SS |
383 | int r; |
384 | ||
385 | assert(netdev); | |
386 | ||
387 | t = L2TP(netdev); | |
388 | ||
389 | assert(t); | |
390 | ||
d053d08a YW |
391 | r = l2tp_acquire_local_address(t, link, &local_address); |
392 | if (r < 0) | |
393 | return log_netdev_error_errno(netdev, r, "Could not find local address."); | |
394 | ||
395 | if (r > 0 && DEBUG_LOGGING) { | |
396 | _cleanup_free_ char *str = NULL; | |
397 | ||
398 | (void) in_addr_to_string(t->family, &local_address, &str); | |
399 | log_netdev_debug(netdev, "Local address %s acquired.", strna(str)); | |
400 | } | |
401 | ||
402 | r = netdev_l2tp_fill_message_tunnel(netdev, &local_address, &m); | |
3a56e697 SS |
403 | if (r < 0) |
404 | return r; | |
405 | ||
3a27af62 YW |
406 | r = netlink_call_async(netdev->manager->genl, NULL, m, l2tp_create_tunnel_handler, |
407 | netdev_destroy_callback, netdev); | |
3a56e697 SS |
408 | if (r < 0) |
409 | return log_netdev_error_errno(netdev, r, "Failed to create L2TP tunnel: %m"); | |
410 | ||
3a27af62 | 411 | netdev_ref(netdev); |
3a56e697 SS |
412 | |
413 | return 0; | |
414 | } | |
415 | ||
416 | int config_parse_l2tp_tunnel_address( | |
417 | const char *unit, | |
418 | const char *filename, | |
419 | unsigned line, | |
420 | const char *section, | |
421 | unsigned section_line, | |
422 | const char *lvalue, | |
423 | int ltype, | |
424 | const char *rvalue, | |
425 | void *data, | |
426 | void *userdata) { | |
427 | ||
428 | L2tpTunnel *t = userdata; | |
429 | union in_addr_union *addr = data; | |
430 | int r; | |
431 | ||
432 | assert(filename); | |
433 | assert(lvalue); | |
434 | assert(rvalue); | |
435 | assert(data); | |
436 | ||
d053d08a YW |
437 | if (streq(lvalue, "Local")) { |
438 | L2tpLocalAddressType addr_type; | |
439 | ||
440 | if (isempty(rvalue)) | |
441 | addr_type = NETDEV_L2TP_LOCAL_ADDRESS_AUTO; | |
442 | else | |
443 | addr_type = l2tp_local_address_type_from_string(rvalue); | |
444 | ||
445 | if (addr_type >= 0) { | |
446 | if (in_addr_is_null(t->family, &t->remote) != 0) | |
447 | /* If Remote= is not specified yet, then also clear family. */ | |
448 | t->family = AF_UNSPEC; | |
449 | ||
450 | t->local = IN_ADDR_NULL; | |
451 | t->local_address_type = addr_type; | |
452 | ||
453 | return 0; | |
454 | } | |
455 | } | |
456 | ||
3a56e697 SS |
457 | if (t->family == AF_UNSPEC) |
458 | r = in_addr_from_string_auto(rvalue, &t->family, addr); | |
459 | else | |
460 | r = in_addr_from_string(t->family, rvalue, addr); | |
461 | if (r < 0) { | |
d96edb2c | 462 | log_syntax(unit, LOG_WARNING, filename, line, r, |
3a56e697 SS |
463 | "Invalid L2TP Tunnel address specified in %s='%s', ignoring assignment: %m", lvalue, rvalue); |
464 | return 0; | |
465 | } | |
466 | ||
467 | return 0; | |
468 | } | |
469 | ||
470 | int config_parse_l2tp_tunnel_id( | |
471 | const char *unit, | |
472 | const char *filename, | |
473 | unsigned line, | |
474 | const char *section, | |
475 | unsigned section_line, | |
476 | const char *lvalue, | |
477 | int ltype, | |
478 | const char *rvalue, | |
479 | void *data, | |
480 | void *userdata) { | |
481 | ||
482 | uint32_t *id = data, k; | |
483 | int r; | |
484 | ||
485 | assert(filename); | |
486 | assert(lvalue); | |
487 | assert(rvalue); | |
488 | assert(data); | |
489 | ||
490 | r = safe_atou32(rvalue, &k); | |
491 | if (r < 0) { | |
d96edb2c | 492 | log_syntax(unit, LOG_WARNING, filename, line, r, |
3a56e697 SS |
493 | "Failed to parse L2TP tunnel id. Ignoring assignment: %s", rvalue); |
494 | return 0; | |
495 | } | |
496 | ||
497 | if (k == 0) { | |
d96edb2c | 498 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
3a56e697 SS |
499 | "Invalid L2TP tunnel id. Ignoring assignment: %s", rvalue); |
500 | return 0; | |
501 | } | |
502 | ||
503 | *id = k; | |
504 | ||
505 | return 0; | |
506 | } | |
507 | ||
508 | int config_parse_l2tp_session_id( | |
509 | const char *unit, | |
510 | const char *filename, | |
511 | unsigned line, | |
512 | const char *section, | |
513 | unsigned section_line, | |
514 | const char *lvalue, | |
515 | int ltype, | |
516 | const char *rvalue, | |
517 | void *data, | |
518 | void *userdata) { | |
519 | ||
520 | _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL; | |
521 | L2tpTunnel *t = userdata; | |
522 | uint32_t k; | |
523 | int r; | |
524 | ||
525 | assert(filename); | |
526 | assert(section); | |
527 | assert(lvalue); | |
528 | assert(rvalue); | |
529 | assert(data); | |
530 | ||
531 | r = l2tp_session_new_static(t, filename, section_line, &session); | |
532 | if (r < 0) | |
d96edb2c | 533 | return log_oom(); |
3a56e697 SS |
534 | |
535 | r = safe_atou32(rvalue, &k); | |
536 | if (r < 0) { | |
d96edb2c | 537 | log_syntax(unit, LOG_WARNING, filename, line, r, |
3a56e697 SS |
538 | "Failed to parse L2TP session id. Ignoring assignment: %s", rvalue); |
539 | return 0; | |
540 | } | |
541 | ||
542 | if (k == 0) { | |
d96edb2c | 543 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
3a56e697 SS |
544 | "Invalid L2TP session id. Ignoring assignment: %s", rvalue); |
545 | return 0; | |
546 | } | |
547 | ||
548 | if (streq(lvalue, "SessionId")) | |
549 | session->session_id = k; | |
550 | else | |
551 | session->peer_session_id = k; | |
552 | ||
553 | session = NULL; | |
554 | return 0; | |
555 | } | |
556 | ||
557 | int config_parse_l2tp_session_l2spec( | |
558 | const char *unit, | |
559 | const char *filename, | |
560 | unsigned line, | |
561 | const char *section, | |
562 | unsigned section_line, | |
563 | const char *lvalue, | |
564 | int ltype, | |
565 | const char *rvalue, | |
566 | void *data, | |
567 | void *userdata) { | |
568 | ||
569 | _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL; | |
570 | L2tpTunnel *t = userdata; | |
571 | L2tpL2specType spec; | |
572 | int r; | |
573 | ||
574 | assert(filename); | |
575 | assert(section); | |
576 | assert(lvalue); | |
577 | assert(rvalue); | |
578 | assert(data); | |
579 | ||
580 | r = l2tp_session_new_static(t, filename, section_line, &session); | |
581 | if (r < 0) | |
d96edb2c | 582 | return log_oom(); |
3a56e697 SS |
583 | |
584 | spec = l2tp_l2spec_type_from_string(rvalue); | |
585 | if (spec < 0) { | |
d96edb2c | 586 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
3a56e697 SS |
587 | "Failed to parse layer2 specific header type. Ignoring assignment: %s", rvalue); |
588 | return 0; | |
589 | } | |
590 | ||
591 | session->l2tp_l2spec_type = spec; | |
592 | ||
593 | session = NULL; | |
594 | return 0; | |
595 | } | |
596 | ||
597 | int config_parse_l2tp_session_name( | |
598 | const char *unit, | |
599 | const char *filename, | |
600 | unsigned line, | |
601 | const char *section, | |
602 | unsigned section_line, | |
603 | const char *lvalue, | |
604 | int ltype, | |
605 | const char *rvalue, | |
606 | void *data, | |
607 | void *userdata) { | |
608 | ||
609 | _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL; | |
610 | L2tpTunnel *t = userdata; | |
611 | int r; | |
612 | ||
613 | assert(filename); | |
614 | assert(section); | |
615 | assert(lvalue); | |
616 | assert(rvalue); | |
617 | assert(data); | |
618 | ||
619 | r = l2tp_session_new_static(t, filename, section_line, &session); | |
620 | if (r < 0) | |
d96edb2c | 621 | return log_oom(); |
3a56e697 SS |
622 | |
623 | if (!ifname_valid(rvalue)) { | |
d96edb2c | 624 | log_syntax(unit, LOG_WARNING, filename, line, 0, |
3a56e697 SS |
625 | "Failed to parse L2TP tunnel session name. Ignoring assignment: %s", rvalue); |
626 | return 0; | |
627 | } | |
628 | ||
50969cff YW |
629 | r = free_and_strdup(&session->name, rvalue); |
630 | if (r < 0) | |
631 | return log_oom(); | |
3a56e697 SS |
632 | |
633 | session = NULL; | |
634 | return 0; | |
635 | } | |
636 | ||
637 | static void l2tp_tunnel_init(NetDev *netdev) { | |
638 | L2tpTunnel *t; | |
639 | ||
640 | assert(netdev); | |
641 | ||
642 | t = L2TP(netdev); | |
643 | ||
644 | assert(t); | |
645 | ||
646 | t->l2tp_encap_type = NETDEV_L2TP_ENCAPTYPE_UDP; | |
647 | t->udp6_csum_rx = true; | |
648 | t->udp6_csum_tx = true; | |
649 | } | |
650 | ||
651 | static int l2tp_session_verify(L2tpSession *session) { | |
652 | NetDev *netdev; | |
653 | ||
654 | assert(session); | |
655 | assert(session->tunnel); | |
656 | ||
657 | netdev = NETDEV(session->tunnel); | |
658 | ||
659 | if (section_is_invalid(session->section)) | |
660 | return -EINVAL; | |
661 | ||
662 | if (!session->name) | |
663 | return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), | |
664 | "%s: L2TP session without name configured. " | |
665 | "Ignoring [L2TPSession] section from line %u", | |
666 | session->section->filename, session->section->line); | |
667 | ||
668 | if (session->session_id == 0 || session->peer_session_id == 0) | |
669 | return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), | |
670 | "%s: L2TP session without session IDs configured. " | |
671 | "Ignoring [L2TPSession] section from line %u", | |
672 | session->section->filename, session->section->line); | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
677 | static int netdev_l2tp_tunnel_verify(NetDev *netdev, const char *filename) { | |
678 | L2tpTunnel *t; | |
679 | L2tpSession *session; | |
680 | Iterator i; | |
681 | ||
682 | assert(netdev); | |
683 | assert(filename); | |
684 | ||
685 | t = L2TP(netdev); | |
686 | ||
687 | assert(t); | |
688 | ||
689 | if (!IN_SET(t->family, AF_INET, AF_INET6)) | |
690 | return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), | |
691 | "%s: L2TP tunnel with invalid address family configured. Ignoring", | |
692 | filename); | |
693 | ||
d053d08a | 694 | if (in_addr_is_null(t->family, &t->remote)) |
3a56e697 | 695 | return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), |
d053d08a | 696 | "%s: L2TP tunnel without a remote address configured. Ignoring", |
3a56e697 SS |
697 | filename); |
698 | ||
699 | if (t->tunnel_id == 0 || t->peer_tunnel_id == 0) | |
700 | return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), | |
701 | "%s: L2TP tunnel without tunnel IDs configured. Ignoring", | |
702 | filename); | |
703 | ||
704 | ORDERED_HASHMAP_FOREACH(session, t->sessions_by_section, i) | |
705 | if (l2tp_session_verify(session) < 0) | |
706 | l2tp_session_free(session); | |
707 | ||
708 | return 0; | |
709 | } | |
710 | ||
711 | static void l2tp_tunnel_done(NetDev *netdev) { | |
712 | L2tpTunnel *t; | |
713 | ||
714 | assert(netdev); | |
715 | ||
716 | t = L2TP(netdev); | |
717 | ||
718 | assert(t); | |
719 | ||
720 | ordered_hashmap_free_with_destructor(t->sessions_by_section, l2tp_session_free); | |
721 | } | |
722 | ||
723 | const NetDevVTable l2tptnl_vtable = { | |
724 | .object_size = sizeof(L2tpTunnel), | |
725 | .init = l2tp_tunnel_init, | |
130b812f | 726 | .sections = NETDEV_COMMON_SECTIONS "L2TP\0L2TPSession\0", |
3a27af62 | 727 | .create_after_configured = l2tp_create_tunnel, |
3a56e697 SS |
728 | .done = l2tp_tunnel_done, |
729 | .create_type = NETDEV_CREATE_AFTER_CONFIGURED, | |
730 | .config_verify = netdev_l2tp_tunnel_verify, | |
731 | }; |