]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/l2tp-tunnel.c
7e7d1679282d1cc777af07b6750704821a31a4e8
[thirdparty/systemd.git] / src / network / netdev / l2tp-tunnel.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <netinet/in.h>
4 #include <linux/l2tp.h>
5 #include <linux/genetlink.h>
6
7 #include "conf-parser.h"
8 #include "hashmap.h"
9 #include "l2tp-tunnel.h"
10 #include "netlink-util.h"
11 #include "networkd-address.h"
12 #include "networkd-manager.h"
13 #include "networkd-route-util.h"
14 #include "parse-util.h"
15 #include "socket-util.h"
16 #include "string-table.h"
17 #include "string-util.h"
18 #include "util.h"
19
20 static const char* const l2tp_l2spec_type_table[_NETDEV_L2TP_L2SPECTYPE_MAX] = {
21 [NETDEV_L2TP_L2SPECTYPE_NONE] = "none",
22 [NETDEV_L2TP_L2SPECTYPE_DEFAULT] = "default",
23 };
24
25 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(l2tp_l2spec_type, L2tpL2specType);
26
27 static const char* const l2tp_encap_type_table[_NETDEV_L2TP_ENCAPTYPE_MAX] = {
28 [NETDEV_L2TP_ENCAPTYPE_UDP] = "udp",
29 [NETDEV_L2TP_ENCAPTYPE_IP] = "ip",
30 };
31
32 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(l2tp_encap_type, L2tpEncapType);
33 DEFINE_CONFIG_PARSE_ENUM(config_parse_l2tp_encap_type, l2tp_encap_type, L2tpEncapType, "Failed to parse L2TP Encapsulation Type");
34
35 static const char* const l2tp_local_address_type_table[_NETDEV_L2TP_LOCAL_ADDRESS_MAX] = {
36 [NETDEV_L2TP_LOCAL_ADDRESS_AUTO] = "auto",
37 [NETDEV_L2TP_LOCAL_ADDRESS_STATIC] = "static",
38 [NETDEV_L2TP_LOCAL_ADDRESS_DYNAMIC] = "dynamic",
39 };
40
41 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(l2tp_local_address_type, L2tpLocalAddressType);
42
43 static L2tpSession* l2tp_session_free(L2tpSession *s) {
44 if (!s)
45 return NULL;
46
47 if (s->tunnel && s->section)
48 ordered_hashmap_remove(s->tunnel->sessions_by_section, s->section);
49
50 config_section_free(s->section);
51 free(s->name);
52 return mfree(s);
53 }
54
55 DEFINE_SECTION_CLEANUP_FUNCTIONS(L2tpSession, l2tp_session_free);
56
57 static int l2tp_session_new_static(L2tpTunnel *t, const char *filename, unsigned section_line, L2tpSession **ret) {
58 _cleanup_(config_section_freep) ConfigSection *n = NULL;
59 _cleanup_(l2tp_session_freep) L2tpSession *s = NULL;
60 int r;
61
62 assert(t);
63 assert(ret);
64 assert(filename);
65 assert(section_line > 0);
66
67 r = config_section_new(filename, section_line, &n);
68 if (r < 0)
69 return r;
70
71 s = ordered_hashmap_get(t->sessions_by_section, n);
72 if (s) {
73 *ret = TAKE_PTR(s);
74 return 0;
75 }
76
77 s = new(L2tpSession, 1);
78 if (!s)
79 return -ENOMEM;
80
81 *s = (L2tpSession) {
82 .l2tp_l2spec_type = NETDEV_L2TP_L2SPECTYPE_DEFAULT,
83 .tunnel = t,
84 .section = TAKE_PTR(n),
85 };
86
87 r = ordered_hashmap_ensure_put(&t->sessions_by_section, &config_section_hash_ops, s->section, s);
88 if (r < 0)
89 return r;
90
91 *ret = TAKE_PTR(s);
92 return 0;
93 }
94
95 static int netdev_l2tp_create_message_tunnel(NetDev *netdev, union in_addr_union *local_address, sd_netlink_message **ret) {
96 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
97 uint16_t encap_type;
98 L2tpTunnel *t;
99 int r;
100
101 assert(netdev);
102 assert(local_address);
103 assert_se(t = L2TP(netdev));
104
105 r = sd_genl_message_new(netdev->manager->genl, L2TP_GENL_NAME, L2TP_CMD_TUNNEL_CREATE, &m);
106 if (r < 0)
107 return r;
108
109 r = sd_netlink_message_append_u32(m, L2TP_ATTR_CONN_ID, t->tunnel_id);
110 if (r < 0)
111 return r;
112
113 r = sd_netlink_message_append_u32(m, L2TP_ATTR_PEER_CONN_ID, t->peer_tunnel_id);
114 if (r < 0)
115 return r;
116
117 r = sd_netlink_message_append_u8(m, L2TP_ATTR_PROTO_VERSION, 3);
118 if (r < 0)
119 return r;
120
121 switch (t->l2tp_encap_type) {
122 case NETDEV_L2TP_ENCAPTYPE_IP:
123 encap_type = L2TP_ENCAPTYPE_IP;
124 break;
125 case NETDEV_L2TP_ENCAPTYPE_UDP:
126 default:
127 encap_type = L2TP_ENCAPTYPE_UDP;
128 break;
129 }
130
131 r = sd_netlink_message_append_u16(m, L2TP_ATTR_ENCAP_TYPE, encap_type);
132 if (r < 0)
133 return r;
134
135 if (t->family == AF_INET) {
136 r = sd_netlink_message_append_in_addr(m, L2TP_ATTR_IP_SADDR, &local_address->in);
137 if (r < 0)
138 return r;
139
140 r = sd_netlink_message_append_in_addr(m, L2TP_ATTR_IP_DADDR, &t->remote.in);
141 if (r < 0)
142 return r;
143 } else {
144 r = sd_netlink_message_append_in6_addr(m, L2TP_ATTR_IP6_SADDR, &local_address->in6);
145 if (r < 0)
146 return r;
147
148 r = sd_netlink_message_append_in6_addr(m, L2TP_ATTR_IP6_DADDR, &t->remote.in6);
149 if (r < 0)
150 return r;
151 }
152
153 if (encap_type == L2TP_ENCAPTYPE_UDP) {
154 r = sd_netlink_message_append_u16(m, L2TP_ATTR_UDP_SPORT, t->l2tp_udp_sport);
155 if (r < 0)
156 return r;
157
158 r = sd_netlink_message_append_u16(m, L2TP_ATTR_UDP_DPORT, t->l2tp_udp_dport);
159 if (r < 0)
160 return r;
161
162 if (t->udp_csum) {
163 r = sd_netlink_message_append_u8(m, L2TP_ATTR_UDP_CSUM, t->udp_csum);
164 if (r < 0)
165 return r;
166 }
167
168 if (t->udp6_csum_tx) {
169 r = sd_netlink_message_append_flag(m, L2TP_ATTR_UDP_ZERO_CSUM6_TX);
170 if (r < 0)
171 return r;
172 }
173
174 if (t->udp6_csum_rx) {
175 r = sd_netlink_message_append_flag(m, L2TP_ATTR_UDP_ZERO_CSUM6_RX);
176 if (r < 0)
177 return r;
178 }
179 }
180
181 *ret = TAKE_PTR(m);
182
183 return 0;
184 }
185
186 static int netdev_l2tp_create_message_session(NetDev *netdev, L2tpSession *session, sd_netlink_message **ret) {
187 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
188 uint16_t l2_spec_len;
189 uint8_t l2_spec_type;
190 int r;
191
192 assert(netdev);
193 assert(session);
194 assert(session->tunnel);
195
196 r = sd_genl_message_new(netdev->manager->genl, L2TP_GENL_NAME, L2TP_CMD_SESSION_CREATE, &m);
197 if (r < 0)
198 return r;
199
200 r = sd_netlink_message_append_u32(m, L2TP_ATTR_CONN_ID, session->tunnel->tunnel_id);
201 if (r < 0)
202 return r;
203
204 r = sd_netlink_message_append_u32(m, L2TP_ATTR_PEER_CONN_ID, session->tunnel->peer_tunnel_id);
205 if (r < 0)
206 return r;
207
208 r = sd_netlink_message_append_u32(m, L2TP_ATTR_SESSION_ID, session->session_id);
209 if (r < 0)
210 return r;
211
212 r = sd_netlink_message_append_u32(m, L2TP_ATTR_PEER_SESSION_ID, session->peer_session_id);
213 if (r < 0)
214 return r;
215
216 r = sd_netlink_message_append_u16(m, L2TP_ATTR_PW_TYPE, L2TP_PWTYPE_ETH);
217 if (r < 0)
218 return r;
219
220 switch (session->l2tp_l2spec_type) {
221 case NETDEV_L2TP_L2SPECTYPE_NONE:
222 l2_spec_type = L2TP_L2SPECTYPE_NONE;
223 l2_spec_len = 0;
224 break;
225 case NETDEV_L2TP_L2SPECTYPE_DEFAULT:
226 default:
227 l2_spec_type = L2TP_L2SPECTYPE_DEFAULT;
228 l2_spec_len = 4;
229 break;
230 }
231
232 r = sd_netlink_message_append_u8(m, L2TP_ATTR_L2SPEC_TYPE, l2_spec_type);
233 if (r < 0)
234 return r;
235
236 r = sd_netlink_message_append_u8(m, L2TP_ATTR_L2SPEC_LEN, l2_spec_len);
237 if (r < 0)
238 return r;
239
240 r = sd_netlink_message_append_string(m, L2TP_ATTR_IFNAME, session->name);
241 if (r < 0)
242 return r;
243
244 *ret = TAKE_PTR(m);
245
246 return 0;
247 }
248
249 static int link_get_l2tp_local_address(Link *link, L2tpTunnel *t, union in_addr_union *ret) {
250 Address *a;
251
252 assert(link);
253 assert(t);
254
255 SET_FOREACH(a, link->addresses) {
256 if (!address_is_ready(a))
257 continue;
258
259 if (a->family != t->family)
260 continue;
261
262 if (in_addr_is_set(a->family, &a->in_addr_peer))
263 continue;
264
265 if (t->local_address_type == NETDEV_L2TP_LOCAL_ADDRESS_STATIC &&
266 !FLAGS_SET(a->flags, IFA_F_PERMANENT))
267 continue;
268
269 if (t->local_address_type == NETDEV_L2TP_LOCAL_ADDRESS_DYNAMIC &&
270 FLAGS_SET(a->flags, IFA_F_PERMANENT))
271 continue;
272
273 if (ret)
274 *ret = a->in_addr;
275 }
276
277 return -ENOENT;
278 }
279
280 static int l2tp_get_local_address(NetDev *netdev, union in_addr_union *ret) {
281 Link *link = NULL;
282 L2tpTunnel *t;
283 Address *a = NULL;
284 int r;
285
286 assert(netdev);
287 assert(netdev->manager);
288 assert_se(t = L2TP(netdev));
289
290 if (t->local_ifname) {
291 r = link_get_by_name(netdev->manager, t->local_ifname, &link);
292 if (r < 0)
293 return r;
294
295 if (!link_is_ready_to_configure(link, /* allow_unmanaged = */ false))
296 return -EBUSY;
297 }
298
299 if (netdev->manager->manage_foreign_routes) {
300 /* First, check if the remote address is accessible. */
301 if (link)
302 r = link_address_is_reachable(link, t->family, &t->remote, &t->local, &a);
303 else
304 r = manager_address_is_reachable(netdev->manager, t->family, &t->remote, &t->local, &a);
305 if (r < 0)
306 return r;
307 }
308
309 if (in_addr_is_set(t->family, &t->local)) {
310 /* local address is explicitly specified. */
311
312 if (!a) {
313 if (link)
314 r = link_get_address(link, t->family, &t->local, 0, &a);
315 else
316 r = manager_get_address(netdev->manager, t->family, &t->local, 0, &a);
317 if (r < 0)
318 return r;
319
320 if (!address_is_ready(a))
321 return -EBUSY;
322 }
323
324 if (ret)
325 *ret = a->in_addr;
326
327 return 0;
328 }
329
330 if (a) {
331 if (t->local_address_type == NETDEV_L2TP_LOCAL_ADDRESS_STATIC &&
332 !FLAGS_SET(a->flags, IFA_F_PERMANENT))
333 return -EINVAL;
334
335 if (t->local_address_type == NETDEV_L2TP_LOCAL_ADDRESS_DYNAMIC &&
336 FLAGS_SET(a->flags, IFA_F_PERMANENT))
337 return -EINVAL;
338
339 if (ret)
340 *ret = a->in_addr;
341
342 return 0;
343 }
344
345 if (link)
346 return link_get_l2tp_local_address(link, t, ret);
347
348 HASHMAP_FOREACH(link, netdev->manager->links_by_index) {
349 if (!link_is_ready_to_configure(link, /* allow_unmanaged = */ false))
350 continue;
351
352 if (link_get_l2tp_local_address(link, t, ret) >= 0)
353 return 0;
354 }
355
356 return -ENOENT;
357 }
358
359 static void l2tp_session_destroy_callback(L2tpSession *session) {
360 if (!session)
361 return;
362
363 netdev_unref(NETDEV(session->tunnel));
364 }
365
366 static int l2tp_create_session_handler(sd_netlink *rtnl, sd_netlink_message *m, L2tpSession *session) {
367 NetDev *netdev;
368 int r;
369
370 assert(session);
371 assert(session->tunnel);
372
373 netdev = NETDEV(session->tunnel);
374
375 r = sd_netlink_message_get_errno(m);
376 if (r == -EEXIST)
377 log_netdev_info(netdev, "L2TP session %s exists, using existing without changing its parameters",
378 session->name);
379 else if (r < 0) {
380 log_netdev_warning_errno(netdev, r, "L2TP session %s could not be created: %m", session->name);
381 return 1;
382 }
383
384 log_netdev_debug(netdev, "L2TP session %s created", session->name);
385 return 1;
386 }
387
388 static int l2tp_create_session(NetDev *netdev, L2tpSession *session) {
389 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *n = NULL;
390 int r;
391
392 r = netdev_l2tp_create_message_session(netdev, session, &n);
393 if (r < 0)
394 return log_netdev_error_errno(netdev, r, "Failed to create netlink message: %m");
395
396 r = netlink_call_async(netdev->manager->genl, NULL, n, l2tp_create_session_handler,
397 l2tp_session_destroy_callback, session);
398 if (r < 0)
399 return log_netdev_error_errno(netdev, r, "Failed to create L2TP session %s: %m", session->name);
400
401 netdev_ref(netdev);
402 return 0;
403 }
404
405 static int l2tp_create_tunnel_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) {
406 L2tpSession *session;
407 L2tpTunnel *t;
408 int r;
409
410 assert(netdev);
411 assert(netdev->state != _NETDEV_STATE_INVALID);
412
413 t = L2TP(netdev);
414
415 assert(t);
416
417 r = sd_netlink_message_get_errno(m);
418 if (r == -EEXIST)
419 log_netdev_info(netdev, "netdev exists, using existing without changing its parameters");
420 else if (r < 0) {
421 log_netdev_warning_errno(netdev, r, "netdev could not be created: %m");
422 netdev_enter_failed(netdev);
423
424 return 1;
425 }
426
427 log_netdev_debug(netdev, "L2TP tunnel is created");
428
429 ORDERED_HASHMAP_FOREACH(session, t->sessions_by_section)
430 (void) l2tp_create_session(netdev, session);
431
432 return 1;
433 }
434
435 static int l2tp_create_tunnel(NetDev *netdev) {
436 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
437 union in_addr_union local_address;
438 L2tpTunnel *t;
439 int r;
440
441 assert(netdev);
442 assert_se(t = L2TP(netdev));
443
444 r = l2tp_get_local_address(netdev, &local_address);
445 if (r < 0)
446 return log_netdev_error_errno(netdev, r, "Could not find local address.");
447
448 if (t->local_address_type >= 0 && DEBUG_LOGGING)
449 log_netdev_debug(netdev, "Local address %s acquired.",
450 IN_ADDR_TO_STRING(t->family, &local_address));
451
452 r = netdev_l2tp_create_message_tunnel(netdev, &local_address, &m);
453 if (r < 0)
454 return log_netdev_error_errno(netdev, r, "Failed to create netlink message: %m");
455
456 r = netlink_call_async(netdev->manager->genl, NULL, m, l2tp_create_tunnel_handler,
457 netdev_destroy_callback, netdev);
458 if (r < 0)
459 return log_netdev_error_errno(netdev, r, "Failed to create L2TP tunnel: %m");
460
461 netdev_ref(netdev);
462
463 return 0;
464 }
465
466 static int netdev_l2tp_is_ready_to_create(NetDev *netdev, Link *link) {
467 return l2tp_get_local_address(netdev, NULL) >= 0;
468 }
469
470 int config_parse_l2tp_tunnel_local_address(
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 _cleanup_free_ char *addr_or_type = NULL, *ifname = NULL;
483 L2tpLocalAddressType type;
484 L2tpTunnel *t = userdata;
485 const char *p = rvalue;
486 union in_addr_union a;
487 int r, f;
488
489 assert(filename);
490 assert(lvalue);
491 assert(rvalue);
492 assert(t);
493
494 if (isempty(rvalue)) {
495 t->local_ifname = mfree(t->local_ifname);
496 t->local_address_type = NETDEV_L2TP_LOCAL_ADDRESS_AUTO;
497 t->local = IN_ADDR_NULL;
498
499 if (!in_addr_is_set(t->family, &t->remote))
500 /* If Remote= is not specified yet, then also clear family. */
501 t->family = AF_UNSPEC;
502
503 return 0;
504 }
505
506 r = extract_first_word(&p, &addr_or_type, "@", 0);
507 if (r < 0)
508 return log_oom();
509 if (r == 0) {
510 log_syntax(unit, LOG_WARNING, filename, line, 0,
511 "Invalid L2TP Tunnel address specified in %s=, ignoring assignment: %s", lvalue, rvalue);
512 return 0;
513 }
514
515 if (!isempty(p)) {
516 if (!ifname_valid_full(p, IFNAME_VALID_ALTERNATIVE)) {
517 log_syntax(unit, LOG_WARNING, filename, line, 0,
518 "Invalid interface name specified in %s=, ignoring assignment: %s", lvalue, rvalue);
519 return 0;
520 }
521
522 ifname = strdup(p);
523 if (!ifname)
524 return log_oom();
525 }
526
527 type = l2tp_local_address_type_from_string(rvalue);
528 if (type >= 0) {
529 free_and_replace(t->local_ifname, ifname);
530 t->local_address_type = type;
531 t->local = IN_ADDR_NULL;
532
533 if (!in_addr_is_set(t->family, &t->remote))
534 /* If Remote= is not specified yet, then also clear family. */
535 t->family = AF_UNSPEC;
536
537 return 0;
538 }
539
540 r = in_addr_from_string_auto(rvalue, &f, &a);
541 if (r < 0) {
542 log_syntax(unit, LOG_WARNING, filename, line, r,
543 "Invalid L2TP Tunnel local address specified, ignoring assignment: %s", rvalue);
544 return 0;
545 }
546
547 if (in_addr_is_null(f, &a)) {
548 log_syntax(unit, LOG_WARNING, filename, line, r,
549 "L2TP Tunnel local address cannot be null, ignoring assignment: %s", rvalue);
550 return 0;
551 }
552
553 if (t->family != AF_UNSPEC && t->family != f) {
554 log_syntax(unit, LOG_WARNING, filename, line, 0,
555 "Address family does not match the previous assignment, ignoring assignment: %s", rvalue);
556 return 0;
557 }
558
559 t->family = f;
560 t->local = a;
561 free_and_replace(t->local_ifname, ifname);
562 t->local_address_type = _NETDEV_L2TP_LOCAL_ADDRESS_INVALID;
563 return 0;
564 }
565
566 int config_parse_l2tp_tunnel_remote_address(
567 const char *unit,
568 const char *filename,
569 unsigned line,
570 const char *section,
571 unsigned section_line,
572 const char *lvalue,
573 int ltype,
574 const char *rvalue,
575 void *data,
576 void *userdata) {
577
578 L2tpTunnel *t = userdata;
579 union in_addr_union a;
580 int r, f;
581
582 assert(filename);
583 assert(lvalue);
584 assert(rvalue);
585 assert(t);
586
587 if (isempty(rvalue)) {
588 t->remote = IN_ADDR_NULL;
589
590 if (!in_addr_is_set(t->family, &t->local))
591 /* If Local= is not specified yet, then also clear family. */
592 t->family = AF_UNSPEC;
593
594 return 0;
595 }
596
597 r = in_addr_from_string_auto(rvalue, &f, &a);
598 if (r < 0) {
599 log_syntax(unit, LOG_WARNING, filename, line, r,
600 "Invalid L2TP Tunnel remote address specified, ignoring assignment: %s", rvalue);
601 return 0;
602 }
603
604 if (in_addr_is_null(f, &a)) {
605 log_syntax(unit, LOG_WARNING, filename, line, r,
606 "L2TP Tunnel remote address cannot be null, ignoring assignment: %s", rvalue);
607 return 0;
608 }
609
610 if (t->family != AF_UNSPEC && t->family != f) {
611 log_syntax(unit, LOG_WARNING, filename, line, 0,
612 "Address family does not match the previous assignment, ignoring assignment: %s", rvalue);
613 return 0;
614 }
615
616 t->family = f;
617 t->remote = a;
618 return 0;
619 }
620
621 int config_parse_l2tp_tunnel_id(
622 const char *unit,
623 const char *filename,
624 unsigned line,
625 const char *section,
626 unsigned section_line,
627 const char *lvalue,
628 int ltype,
629 const char *rvalue,
630 void *data,
631 void *userdata) {
632
633 uint32_t *id = data, k;
634 int r;
635
636 assert(filename);
637 assert(lvalue);
638 assert(rvalue);
639 assert(data);
640
641 r = safe_atou32(rvalue, &k);
642 if (r < 0) {
643 log_syntax(unit, LOG_WARNING, filename, line, r,
644 "Failed to parse L2TP tunnel id. Ignoring assignment: %s", rvalue);
645 return 0;
646 }
647
648 if (k == 0) {
649 log_syntax(unit, LOG_WARNING, filename, line, 0,
650 "Invalid L2TP tunnel id. Ignoring assignment: %s", rvalue);
651 return 0;
652 }
653
654 *id = k;
655
656 return 0;
657 }
658
659 int config_parse_l2tp_session_id(
660 const char *unit,
661 const char *filename,
662 unsigned line,
663 const char *section,
664 unsigned section_line,
665 const char *lvalue,
666 int ltype,
667 const char *rvalue,
668 void *data,
669 void *userdata) {
670
671 _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL;
672 L2tpTunnel *t = userdata;
673 uint32_t k;
674 int r;
675
676 assert(filename);
677 assert(section);
678 assert(lvalue);
679 assert(rvalue);
680 assert(data);
681
682 r = l2tp_session_new_static(t, filename, section_line, &session);
683 if (r < 0)
684 return log_oom();
685
686 r = safe_atou32(rvalue, &k);
687 if (r < 0) {
688 log_syntax(unit, LOG_WARNING, filename, line, r,
689 "Failed to parse L2TP session id. Ignoring assignment: %s", rvalue);
690 return 0;
691 }
692
693 if (k == 0) {
694 log_syntax(unit, LOG_WARNING, filename, line, 0,
695 "Invalid L2TP session id. Ignoring assignment: %s", rvalue);
696 return 0;
697 }
698
699 if (streq(lvalue, "SessionId"))
700 session->session_id = k;
701 else
702 session->peer_session_id = k;
703
704 session = NULL;
705 return 0;
706 }
707
708 int config_parse_l2tp_session_l2spec(
709 const char *unit,
710 const char *filename,
711 unsigned line,
712 const char *section,
713 unsigned section_line,
714 const char *lvalue,
715 int ltype,
716 const char *rvalue,
717 void *data,
718 void *userdata) {
719
720 _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL;
721 L2tpTunnel *t = userdata;
722 L2tpL2specType spec;
723 int r;
724
725 assert(filename);
726 assert(section);
727 assert(lvalue);
728 assert(rvalue);
729 assert(data);
730
731 r = l2tp_session_new_static(t, filename, section_line, &session);
732 if (r < 0)
733 return log_oom();
734
735 spec = l2tp_l2spec_type_from_string(rvalue);
736 if (spec < 0) {
737 log_syntax(unit, LOG_WARNING, filename, line, spec,
738 "Failed to parse layer2 specific header type. Ignoring assignment: %s", rvalue);
739 return 0;
740 }
741
742 session->l2tp_l2spec_type = spec;
743
744 session = NULL;
745 return 0;
746 }
747
748 int config_parse_l2tp_session_name(
749 const char *unit,
750 const char *filename,
751 unsigned line,
752 const char *section,
753 unsigned section_line,
754 const char *lvalue,
755 int ltype,
756 const char *rvalue,
757 void *data,
758 void *userdata) {
759
760 _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL;
761 L2tpTunnel *t = userdata;
762 int r;
763
764 assert(filename);
765 assert(section);
766 assert(lvalue);
767 assert(rvalue);
768 assert(data);
769
770 r = l2tp_session_new_static(t, filename, section_line, &session);
771 if (r < 0)
772 return log_oom();
773
774 if (!ifname_valid(rvalue)) {
775 log_syntax(unit, LOG_WARNING, filename, line, 0,
776 "Failed to parse L2TP tunnel session name. Ignoring assignment: %s", rvalue);
777 return 0;
778 }
779
780 r = free_and_strdup(&session->name, rvalue);
781 if (r < 0)
782 return log_oom();
783
784 session = NULL;
785 return 0;
786 }
787
788 static void l2tp_tunnel_init(NetDev *netdev) {
789 L2tpTunnel *t;
790
791 assert(netdev);
792
793 t = L2TP(netdev);
794
795 assert(t);
796
797 t->l2tp_encap_type = NETDEV_L2TP_ENCAPTYPE_UDP;
798 t->udp6_csum_rx = true;
799 t->udp6_csum_tx = true;
800 }
801
802 static int l2tp_session_verify(L2tpSession *session) {
803 NetDev *netdev;
804
805 assert(session);
806 assert(session->tunnel);
807
808 netdev = NETDEV(session->tunnel);
809
810 if (section_is_invalid(session->section))
811 return -EINVAL;
812
813 if (!session->name)
814 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
815 "%s: L2TP session without name configured. "
816 "Ignoring [L2TPSession] section from line %u",
817 session->section->filename, session->section->line);
818
819 if (session->session_id == 0 || session->peer_session_id == 0)
820 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
821 "%s: L2TP session without session IDs configured. "
822 "Ignoring [L2TPSession] section from line %u",
823 session->section->filename, session->section->line);
824
825 return 0;
826 }
827
828 static int netdev_l2tp_tunnel_verify(NetDev *netdev, const char *filename) {
829 L2tpTunnel *t;
830 L2tpSession *session;
831
832 assert(netdev);
833 assert(filename);
834
835 t = L2TP(netdev);
836
837 assert(t);
838
839 if (!IN_SET(t->family, AF_INET, AF_INET6))
840 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
841 "%s: L2TP tunnel with invalid address family configured. Ignoring",
842 filename);
843
844 if (!in_addr_is_set(t->family, &t->remote))
845 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
846 "%s: L2TP tunnel without a remote address configured. Ignoring",
847 filename);
848
849 if (t->tunnel_id == 0 || t->peer_tunnel_id == 0)
850 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
851 "%s: L2TP tunnel without tunnel IDs configured. Ignoring",
852 filename);
853
854 ORDERED_HASHMAP_FOREACH(session, t->sessions_by_section)
855 if (l2tp_session_verify(session) < 0)
856 l2tp_session_free(session);
857
858 return 0;
859 }
860
861 static void l2tp_tunnel_done(NetDev *netdev) {
862 L2tpTunnel *t;
863
864 assert(netdev);
865
866 t = L2TP(netdev);
867
868 assert(t);
869
870 ordered_hashmap_free_with_destructor(t->sessions_by_section, l2tp_session_free);
871 free(t->local_ifname);
872 }
873
874 const NetDevVTable l2tptnl_vtable = {
875 .object_size = sizeof(L2tpTunnel),
876 .init = l2tp_tunnel_init,
877 .sections = NETDEV_COMMON_SECTIONS "L2TP\0L2TPSession\0",
878 .create = l2tp_create_tunnel,
879 .done = l2tp_tunnel_done,
880 .create_type = NETDEV_CREATE_INDEPENDENT,
881 .is_ready_to_create = netdev_l2tp_is_ready_to_create,
882 .config_verify = netdev_l2tp_tunnel_verify,
883 };