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