Debian Codegraph @

src/network/netdev/l2tp-tunnel.c

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