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