]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/netdev/l2tp-tunnel.c
tree-wide: use ASSERT_PTR more
[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 = ASSERT_PTR(userdata);
485 const char *p = ASSERT_PTR(rvalue);
486 union in_addr_union a;
487 int r, f;
488
489 assert(filename);
490 assert(lvalue);
491
492 if (isempty(rvalue)) {
493 t->local_ifname = mfree(t->local_ifname);
494 t->local_address_type = NETDEV_L2TP_LOCAL_ADDRESS_AUTO;
495 t->local = IN_ADDR_NULL;
496
497 if (!in_addr_is_set(t->family, &t->remote))
498 /* If Remote= is not specified yet, then also clear family. */
499 t->family = AF_UNSPEC;
500
501 return 0;
502 }
503
504 r = extract_first_word(&p, &addr_or_type, "@", 0);
505 if (r < 0)
506 return log_oom();
507 if (r == 0) {
508 log_syntax(unit, LOG_WARNING, filename, line, 0,
509 "Invalid L2TP Tunnel address specified in %s=, ignoring assignment: %s", lvalue, rvalue);
510 return 0;
511 }
512
513 if (!isempty(p)) {
514 if (!ifname_valid_full(p, IFNAME_VALID_ALTERNATIVE)) {
515 log_syntax(unit, LOG_WARNING, filename, line, 0,
516 "Invalid interface name specified in %s=, ignoring assignment: %s", lvalue, rvalue);
517 return 0;
518 }
519
520 ifname = strdup(p);
521 if (!ifname)
522 return log_oom();
523 }
524
525 type = l2tp_local_address_type_from_string(rvalue);
526 if (type >= 0) {
527 free_and_replace(t->local_ifname, ifname);
528 t->local_address_type = type;
529 t->local = IN_ADDR_NULL;
530
531 if (!in_addr_is_set(t->family, &t->remote))
532 /* If Remote= is not specified yet, then also clear family. */
533 t->family = AF_UNSPEC;
534
535 return 0;
536 }
537
538 r = in_addr_from_string_auto(rvalue, &f, &a);
539 if (r < 0) {
540 log_syntax(unit, LOG_WARNING, filename, line, r,
541 "Invalid L2TP Tunnel local address specified, ignoring assignment: %s", rvalue);
542 return 0;
543 }
544
545 if (in_addr_is_null(f, &a)) {
546 log_syntax(unit, LOG_WARNING, filename, line, r,
547 "L2TP Tunnel local address cannot be null, ignoring assignment: %s", rvalue);
548 return 0;
549 }
550
551 if (t->family != AF_UNSPEC && t->family != f) {
552 log_syntax(unit, LOG_WARNING, filename, line, 0,
553 "Address family does not match the previous assignment, ignoring assignment: %s", rvalue);
554 return 0;
555 }
556
557 t->family = f;
558 t->local = a;
559 free_and_replace(t->local_ifname, ifname);
560 t->local_address_type = _NETDEV_L2TP_LOCAL_ADDRESS_INVALID;
561 return 0;
562 }
563
564 int config_parse_l2tp_tunnel_remote_address(
565 const char *unit,
566 const char *filename,
567 unsigned line,
568 const char *section,
569 unsigned section_line,
570 const char *lvalue,
571 int ltype,
572 const char *rvalue,
573 void *data,
574 void *userdata) {
575
576 L2tpTunnel *t = ASSERT_PTR(userdata);
577 union in_addr_union a;
578 int r, f;
579
580 assert(filename);
581 assert(lvalue);
582 assert(rvalue);
583
584 if (isempty(rvalue)) {
585 t->remote = IN_ADDR_NULL;
586
587 if (!in_addr_is_set(t->family, &t->local))
588 /* If Local= is not specified yet, then also clear family. */
589 t->family = AF_UNSPEC;
590
591 return 0;
592 }
593
594 r = in_addr_from_string_auto(rvalue, &f, &a);
595 if (r < 0) {
596 log_syntax(unit, LOG_WARNING, filename, line, r,
597 "Invalid L2TP Tunnel remote address specified, ignoring assignment: %s", rvalue);
598 return 0;
599 }
600
601 if (in_addr_is_null(f, &a)) {
602 log_syntax(unit, LOG_WARNING, filename, line, r,
603 "L2TP Tunnel remote address cannot be null, ignoring assignment: %s", rvalue);
604 return 0;
605 }
606
607 if (t->family != AF_UNSPEC && t->family != f) {
608 log_syntax(unit, LOG_WARNING, filename, line, 0,
609 "Address family does not match the previous assignment, ignoring assignment: %s", rvalue);
610 return 0;
611 }
612
613 t->family = f;
614 t->remote = a;
615 return 0;
616 }
617
618 int config_parse_l2tp_tunnel_id(
619 const char *unit,
620 const char *filename,
621 unsigned line,
622 const char *section,
623 unsigned section_line,
624 const char *lvalue,
625 int ltype,
626 const char *rvalue,
627 void *data,
628 void *userdata) {
629
630 uint32_t *id = data, k;
631 int r;
632
633 assert(filename);
634 assert(lvalue);
635 assert(rvalue);
636 assert(data);
637
638 r = safe_atou32(rvalue, &k);
639 if (r < 0) {
640 log_syntax(unit, LOG_WARNING, filename, line, r,
641 "Failed to parse L2TP tunnel id. Ignoring assignment: %s", rvalue);
642 return 0;
643 }
644
645 if (k == 0) {
646 log_syntax(unit, LOG_WARNING, filename, line, 0,
647 "Invalid L2TP tunnel id. Ignoring assignment: %s", rvalue);
648 return 0;
649 }
650
651 *id = k;
652
653 return 0;
654 }
655
656 int config_parse_l2tp_session_id(
657 const char *unit,
658 const char *filename,
659 unsigned line,
660 const char *section,
661 unsigned section_line,
662 const char *lvalue,
663 int ltype,
664 const char *rvalue,
665 void *data,
666 void *userdata) {
667
668 _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL;
669 L2tpTunnel *t = userdata;
670 uint32_t k;
671 int r;
672
673 assert(filename);
674 assert(section);
675 assert(lvalue);
676 assert(rvalue);
677 assert(data);
678
679 r = l2tp_session_new_static(t, filename, section_line, &session);
680 if (r < 0)
681 return log_oom();
682
683 r = safe_atou32(rvalue, &k);
684 if (r < 0) {
685 log_syntax(unit, LOG_WARNING, filename, line, r,
686 "Failed to parse L2TP session id. Ignoring assignment: %s", rvalue);
687 return 0;
688 }
689
690 if (k == 0) {
691 log_syntax(unit, LOG_WARNING, filename, line, 0,
692 "Invalid L2TP session id. Ignoring assignment: %s", rvalue);
693 return 0;
694 }
695
696 if (streq(lvalue, "SessionId"))
697 session->session_id = k;
698 else
699 session->peer_session_id = k;
700
701 session = NULL;
702 return 0;
703 }
704
705 int config_parse_l2tp_session_l2spec(
706 const char *unit,
707 const char *filename,
708 unsigned line,
709 const char *section,
710 unsigned section_line,
711 const char *lvalue,
712 int ltype,
713 const char *rvalue,
714 void *data,
715 void *userdata) {
716
717 _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL;
718 L2tpTunnel *t = userdata;
719 L2tpL2specType spec;
720 int r;
721
722 assert(filename);
723 assert(section);
724 assert(lvalue);
725 assert(rvalue);
726 assert(data);
727
728 r = l2tp_session_new_static(t, filename, section_line, &session);
729 if (r < 0)
730 return log_oom();
731
732 spec = l2tp_l2spec_type_from_string(rvalue);
733 if (spec < 0) {
734 log_syntax(unit, LOG_WARNING, filename, line, spec,
735 "Failed to parse layer2 specific header type. Ignoring assignment: %s", rvalue);
736 return 0;
737 }
738
739 session->l2tp_l2spec_type = spec;
740
741 session = NULL;
742 return 0;
743 }
744
745 int config_parse_l2tp_session_name(
746 const char *unit,
747 const char *filename,
748 unsigned line,
749 const char *section,
750 unsigned section_line,
751 const char *lvalue,
752 int ltype,
753 const char *rvalue,
754 void *data,
755 void *userdata) {
756
757 _cleanup_(l2tp_session_free_or_set_invalidp) L2tpSession *session = NULL;
758 L2tpTunnel *t = userdata;
759 int r;
760
761 assert(filename);
762 assert(section);
763 assert(lvalue);
764 assert(rvalue);
765 assert(data);
766
767 r = l2tp_session_new_static(t, filename, section_line, &session);
768 if (r < 0)
769 return log_oom();
770
771 if (!ifname_valid(rvalue)) {
772 log_syntax(unit, LOG_WARNING, filename, line, 0,
773 "Failed to parse L2TP tunnel session name. Ignoring assignment: %s", rvalue);
774 return 0;
775 }
776
777 r = free_and_strdup(&session->name, rvalue);
778 if (r < 0)
779 return log_oom();
780
781 session = NULL;
782 return 0;
783 }
784
785 static void l2tp_tunnel_init(NetDev *netdev) {
786 L2tpTunnel *t;
787
788 assert(netdev);
789
790 t = L2TP(netdev);
791
792 assert(t);
793
794 t->l2tp_encap_type = NETDEV_L2TP_ENCAPTYPE_UDP;
795 t->udp6_csum_rx = true;
796 t->udp6_csum_tx = true;
797 }
798
799 static int l2tp_session_verify(L2tpSession *session) {
800 NetDev *netdev;
801
802 assert(session);
803 assert(session->tunnel);
804
805 netdev = NETDEV(session->tunnel);
806
807 if (section_is_invalid(session->section))
808 return -EINVAL;
809
810 if (!session->name)
811 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
812 "%s: L2TP session without name configured. "
813 "Ignoring [L2TPSession] section from line %u",
814 session->section->filename, session->section->line);
815
816 if (session->session_id == 0 || session->peer_session_id == 0)
817 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
818 "%s: L2TP session without session IDs configured. "
819 "Ignoring [L2TPSession] section from line %u",
820 session->section->filename, session->section->line);
821
822 return 0;
823 }
824
825 static int netdev_l2tp_tunnel_verify(NetDev *netdev, const char *filename) {
826 L2tpTunnel *t;
827 L2tpSession *session;
828
829 assert(netdev);
830 assert(filename);
831
832 t = L2TP(netdev);
833
834 assert(t);
835
836 if (!IN_SET(t->family, AF_INET, AF_INET6))
837 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
838 "%s: L2TP tunnel with invalid address family configured. Ignoring",
839 filename);
840
841 if (!in_addr_is_set(t->family, &t->remote))
842 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
843 "%s: L2TP tunnel without a remote address configured. Ignoring",
844 filename);
845
846 if (t->tunnel_id == 0 || t->peer_tunnel_id == 0)
847 return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
848 "%s: L2TP tunnel without tunnel IDs configured. Ignoring",
849 filename);
850
851 ORDERED_HASHMAP_FOREACH(session, t->sessions_by_section)
852 if (l2tp_session_verify(session) < 0)
853 l2tp_session_free(session);
854
855 return 0;
856 }
857
858 static void l2tp_tunnel_done(NetDev *netdev) {
859 L2tpTunnel *t;
860
861 assert(netdev);
862
863 t = L2TP(netdev);
864
865 assert(t);
866
867 ordered_hashmap_free_with_destructor(t->sessions_by_section, l2tp_session_free);
868 free(t->local_ifname);
869 }
870
871 const NetDevVTable l2tptnl_vtable = {
872 .object_size = sizeof(L2tpTunnel),
873 .init = l2tp_tunnel_init,
874 .sections = NETDEV_COMMON_SECTIONS "L2TP\0L2TPSession\0",
875 .create = l2tp_create_tunnel,
876 .done = l2tp_tunnel_done,
877 .create_type = NETDEV_CREATE_INDEPENDENT,
878 .is_ready_to_create = netdev_l2tp_is_ready_to_create,
879 .config_verify = netdev_l2tp_tunnel_verify,
880 };