]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-route.c
Merge pull request #13354 from keszybz/two-refactoring-patches
[thirdparty/systemd.git] / src / network / networkd-route.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <linux/icmpv6.h>
4
5 #include "alloc-util.h"
6 #include "conf-parser.h"
7 #include "in-addr-util.h"
8 #include "missing_network.h"
9 #include "netlink-util.h"
10 #include "networkd-ipv4ll.h"
11 #include "networkd-manager.h"
12 #include "networkd-route.h"
13 #include "parse-util.h"
14 #include "set.h"
15 #include "string-table.h"
16 #include "string-util.h"
17 #include "strxcpyx.h"
18 #include "sysctl-util.h"
19 #include "util.h"
20
21 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
22
23 static unsigned routes_max(void) {
24 static thread_local unsigned cached = 0;
25
26 _cleanup_free_ char *s4 = NULL, *s6 = NULL;
27 unsigned val4 = ROUTES_DEFAULT_MAX_PER_FAMILY, val6 = ROUTES_DEFAULT_MAX_PER_FAMILY;
28
29 if (cached > 0)
30 return cached;
31
32 if (sysctl_read("net/ipv4/route/max_size", &s4) >= 0) {
33 truncate_nl(s4);
34 if (safe_atou(s4, &val4) >= 0 &&
35 val4 == 2147483647U)
36 /* This is the default "no limit" value in the kernel */
37 val4 = ROUTES_DEFAULT_MAX_PER_FAMILY;
38 }
39
40 if (sysctl_read("net/ipv6/route/max_size", &s6) >= 0) {
41 truncate_nl(s6);
42 (void) safe_atou(s6, &val6);
43 }
44
45 cached = MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val4) +
46 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY, val6);
47 return cached;
48 }
49
50 int route_new(Route **ret) {
51 _cleanup_(route_freep) Route *route = NULL;
52
53 route = new(Route, 1);
54 if (!route)
55 return -ENOMEM;
56
57 *route = (Route) {
58 .family = AF_UNSPEC,
59 .scope = RT_SCOPE_UNIVERSE,
60 .protocol = RTPROT_UNSPEC,
61 .type = RTN_UNICAST,
62 .table = RT_TABLE_MAIN,
63 .lifetime = USEC_INFINITY,
64 .quickack = -1,
65 .fast_open_no_cookie = -1,
66 .gateway_onlink = -1,
67 .ttl_propagate = -1,
68 };
69
70 *ret = TAKE_PTR(route);
71
72 return 0;
73 }
74
75 static int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) {
76 _cleanup_(network_config_section_freep) NetworkConfigSection *n = NULL;
77 _cleanup_(route_freep) Route *route = NULL;
78 int r;
79
80 assert(network);
81 assert(ret);
82 assert(!!filename == (section_line > 0));
83
84 if (filename) {
85 r = network_config_section_new(filename, section_line, &n);
86 if (r < 0)
87 return r;
88
89 route = hashmap_get(network->routes_by_section, n);
90 if (route) {
91 *ret = TAKE_PTR(route);
92
93 return 0;
94 }
95 }
96
97 if (network->n_static_routes >= routes_max())
98 return -E2BIG;
99
100 r = route_new(&route);
101 if (r < 0)
102 return r;
103
104 route->protocol = RTPROT_STATIC;
105 route->network = network;
106 LIST_PREPEND(routes, network->static_routes, route);
107 network->n_static_routes++;
108
109 if (filename) {
110 route->section = TAKE_PTR(n);
111
112 r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops);
113 if (r < 0)
114 return r;
115
116 r = hashmap_put(network->routes_by_section, route->section, route);
117 if (r < 0)
118 return r;
119 }
120
121 *ret = TAKE_PTR(route);
122
123 return 0;
124 }
125
126 void route_free(Route *route) {
127 if (!route)
128 return;
129
130 if (route->network) {
131 LIST_REMOVE(routes, route->network->static_routes, route);
132
133 assert(route->network->n_static_routes > 0);
134 route->network->n_static_routes--;
135
136 if (route->section)
137 hashmap_remove(route->network->routes_by_section, route->section);
138 }
139
140 network_config_section_free(route->section);
141
142 if (route->link) {
143 set_remove(route->link->routes, route);
144 set_remove(route->link->routes_foreign, route);
145 }
146
147 sd_event_source_unref(route->expire);
148
149 free(route);
150 }
151
152 static void route_hash_func(const Route *route, struct siphash *state) {
153 assert(route);
154
155 siphash24_compress(&route->family, sizeof(route->family), state);
156
157 switch (route->family) {
158 case AF_INET:
159 case AF_INET6:
160 /* Equality of routes are given by the 4-touple
161 (dst_prefix,dst_prefixlen,tos,priority,table) */
162 siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state);
163 siphash24_compress(&route->dst_prefixlen, sizeof(route->dst_prefixlen), state);
164 siphash24_compress(&route->tos, sizeof(route->tos), state);
165 siphash24_compress(&route->priority, sizeof(route->priority), state);
166 siphash24_compress(&route->table, sizeof(route->table), state);
167
168 break;
169 default:
170 /* treat any other address family as AF_UNSPEC */
171 break;
172 }
173 }
174
175 static int route_compare_func(const Route *a, const Route *b) {
176 int r;
177
178 r = CMP(a->family, b->family);
179 if (r != 0)
180 return r;
181
182 switch (a->family) {
183 case AF_INET:
184 case AF_INET6:
185 r = CMP(a->dst_prefixlen, b->dst_prefixlen);
186 if (r != 0)
187 return r;
188
189 r = CMP(a->tos, b->tos);
190 if (r != 0)
191 return r;
192
193 r = CMP(a->priority, b->priority);
194 if (r != 0)
195 return r;
196
197 r = CMP(a->table, b->table);
198 if (r != 0)
199 return r;
200
201 r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
202 if (r != 0)
203 return r;
204
205 return memcmp(&a->gw, &b->gw, FAMILY_ADDRESS_SIZE(a->family));
206 default:
207 /* treat any other address family as AF_UNSPEC */
208 return 0;
209 }
210 }
211
212 DEFINE_PRIVATE_HASH_OPS(route_hash_ops, Route, route_hash_func, route_compare_func);
213
214 static void route_full_hash_func(const Route *route, struct siphash *state) {
215 assert(route);
216
217 siphash24_compress(&route->family, sizeof(route->family), state);
218
219 switch (route->family) {
220 case AF_INET:
221 case AF_INET6:
222 siphash24_compress(&route->gw, FAMILY_ADDRESS_SIZE(route->family), state);
223 siphash24_compress(&route->dst, FAMILY_ADDRESS_SIZE(route->family), state);
224 siphash24_compress(&route->dst_prefixlen, sizeof(route->dst_prefixlen), state);
225 siphash24_compress(&route->src, FAMILY_ADDRESS_SIZE(route->family), state);
226 siphash24_compress(&route->src_prefixlen, sizeof(route->src_prefixlen), state);
227 siphash24_compress(&route->prefsrc, FAMILY_ADDRESS_SIZE(route->family), state);
228
229 siphash24_compress(&route->tos, sizeof(route->tos), state);
230 siphash24_compress(&route->priority, sizeof(route->priority), state);
231 siphash24_compress(&route->table, sizeof(route->table), state);
232 siphash24_compress(&route->protocol, sizeof(route->protocol), state);
233 siphash24_compress(&route->scope, sizeof(route->scope), state);
234 siphash24_compress(&route->type, sizeof(route->type), state);
235
236 break;
237 default:
238 /* treat any other address family as AF_UNSPEC */
239 break;
240 }
241 }
242
243 static int route_full_compare_func(const Route *a, const Route *b) {
244 int r;
245
246 r = CMP(a->family, b->family);
247 if (r != 0)
248 return r;
249
250 switch (a->family) {
251 case AF_INET:
252 case AF_INET6:
253 r = CMP(a->dst_prefixlen, b->dst_prefixlen);
254 if (r != 0)
255 return r;
256
257 r = CMP(a->src_prefixlen, b->src_prefixlen);
258 if (r != 0)
259 return r;
260
261 r = CMP(a->tos, b->tos);
262 if (r != 0)
263 return r;
264
265 r = CMP(a->priority, b->priority);
266 if (r != 0)
267 return r;
268
269 r = CMP(a->table, b->table);
270 if (r != 0)
271 return r;
272
273 r = CMP(a->protocol, b->protocol);
274 if (r != 0)
275 return r;
276
277 r = CMP(a->scope, b->scope);
278 if (r != 0)
279 return r;
280
281 r = CMP(a->type, b->type);
282 if (r != 0)
283 return r;
284
285 r = memcmp(&a->gw, &b->gw, FAMILY_ADDRESS_SIZE(a->family));
286 if (r != 0)
287 return r;
288
289 r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
290 if (r != 0)
291 return r;
292
293 r = memcmp(&a->src, &b->src, FAMILY_ADDRESS_SIZE(a->family));
294 if (r != 0)
295 return r;
296
297 return memcmp(&a->prefsrc, &b->prefsrc, FAMILY_ADDRESS_SIZE(a->family));
298 default:
299 /* treat any other address family as AF_UNSPEC */
300 return 0;
301 }
302 }
303
304 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
305 route_full_hash_ops,
306 Route,
307 route_full_hash_func,
308 route_full_compare_func,
309 route_free);
310
311 bool route_equal(Route *r1, Route *r2) {
312 if (r1 == r2)
313 return true;
314
315 if (!r1 || !r2)
316 return false;
317
318 return route_compare_func(r1, r2) == 0;
319 }
320
321 int route_get(Link *link,
322 int family,
323 const union in_addr_union *dst,
324 unsigned char dst_prefixlen,
325 const union in_addr_union *gw,
326 unsigned char tos,
327 uint32_t priority,
328 uint32_t table,
329 Route **ret) {
330
331 Route route, *existing;
332
333 assert(link);
334 assert(dst);
335
336 route = (Route) {
337 .family = family,
338 .dst = *dst,
339 .dst_prefixlen = dst_prefixlen,
340 .gw = gw ? *gw : IN_ADDR_NULL,
341 .tos = tos,
342 .priority = priority,
343 .table = table,
344 };
345
346 existing = set_get(link->routes, &route);
347 if (existing) {
348 if (ret)
349 *ret = existing;
350 return 1;
351 }
352
353 existing = set_get(link->routes_foreign, &route);
354 if (existing) {
355 if (ret)
356 *ret = existing;
357 return 0;
358 }
359
360 return -ENOENT;
361 }
362
363 static int route_add_internal(
364 Link *link,
365 Set **routes,
366 int family,
367 const union in_addr_union *dst,
368 unsigned char dst_prefixlen,
369 const union in_addr_union *gw,
370 unsigned char tos,
371 uint32_t priority,
372 uint32_t table,
373 Route **ret) {
374
375 _cleanup_(route_freep) Route *route = NULL;
376 int r;
377
378 assert(link);
379 assert(routes);
380 assert(dst);
381
382 r = route_new(&route);
383 if (r < 0)
384 return r;
385
386 route->family = family;
387 route->dst = *dst;
388 route->dst_prefixlen = dst_prefixlen;
389 route->dst = *dst;
390 route->gw = gw ? *gw : IN_ADDR_NULL;
391 route->tos = tos;
392 route->priority = priority;
393 route->table = table;
394
395 r = set_ensure_allocated(routes, &route_hash_ops);
396 if (r < 0)
397 return r;
398
399 r = set_put(*routes, route);
400 if (r < 0)
401 return r;
402 if (r == 0)
403 return -EEXIST;
404
405 route->link = link;
406
407 if (ret)
408 *ret = route;
409
410 route = NULL;
411
412 return 0;
413 }
414
415 int route_add_foreign(
416 Link *link,
417 int family,
418 const union in_addr_union *dst,
419 unsigned char dst_prefixlen,
420 const union in_addr_union *gw,
421 unsigned char tos,
422 uint32_t priority,
423 uint32_t table,
424 Route **ret) {
425
426 return route_add_internal(link, &link->routes_foreign, family, dst, dst_prefixlen, gw, tos, priority, table, ret);
427 }
428
429 int route_add(Link *link,
430 int family,
431 const union in_addr_union *dst,
432 unsigned char dst_prefixlen,
433 const union in_addr_union *gw,
434 unsigned char tos,
435 uint32_t priority,
436 uint32_t table,
437 Route **ret) {
438
439 Route *route;
440 int r;
441
442 r = route_get(link, family, dst, dst_prefixlen, gw, tos, priority, table, &route);
443 if (r == -ENOENT) {
444 /* Route does not exist, create a new one */
445 r = route_add_internal(link, &link->routes, family, dst, dst_prefixlen, gw, tos, priority, table, &route);
446 if (r < 0)
447 return r;
448 } else if (r == 0) {
449 /* Take over a foreign route */
450 r = set_ensure_allocated(&link->routes, &route_hash_ops);
451 if (r < 0)
452 return r;
453
454 r = set_put(link->routes, route);
455 if (r < 0)
456 return r;
457
458 set_remove(link->routes_foreign, route);
459 } else if (r == 1) {
460 /* Route exists, do nothing */
461 ;
462 } else
463 return r;
464
465 if (ret)
466 *ret = route;
467
468 return 0;
469 }
470
471 void route_update(Route *route,
472 const union in_addr_union *src,
473 unsigned char src_prefixlen,
474 const union in_addr_union *gw,
475 const union in_addr_union *prefsrc,
476 unsigned char scope,
477 unsigned char protocol,
478 unsigned char type) {
479
480 assert(route);
481 assert(src || src_prefixlen == 0);
482
483 route->src = src ? *src : IN_ADDR_NULL;
484 route->src_prefixlen = src_prefixlen;
485 route->gw = gw ? *gw : IN_ADDR_NULL;
486 route->prefsrc = prefsrc ? *prefsrc : IN_ADDR_NULL;
487 route->scope = scope;
488 route->protocol = protocol;
489 route->type = type;
490 }
491
492 static int route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
493 int r;
494
495 assert(m);
496 assert(link);
497 assert(link->ifname);
498
499 if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
500 return 1;
501
502 r = sd_netlink_message_get_errno(m);
503 if (r < 0 && r != -ESRCH)
504 log_link_warning_errno(link, r, "Could not drop route: %m");
505
506 return 1;
507 }
508
509 int route_remove(Route *route, Link *link,
510 link_netlink_message_handler_t callback) {
511
512 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
513 int r;
514
515 assert(link);
516 assert(link->manager);
517 assert(link->manager->rtnl);
518 assert(link->ifindex > 0);
519 assert(IN_SET(route->family, AF_INET, AF_INET6));
520
521 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
522 RTM_DELROUTE, route->family,
523 route->protocol);
524 if (r < 0)
525 return log_link_error_errno(link, r, "Could not create RTM_DELROUTE message: %m");
526
527 if (DEBUG_LOGGING) {
528 _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL;
529 char scope[ROUTE_SCOPE_STR_MAX], table[ROUTE_TABLE_STR_MAX], protocol[ROUTE_PROTOCOL_STR_MAX];
530
531 if (!in_addr_is_null(route->family, &route->dst)) {
532 (void) in_addr_to_string(route->family, &route->dst, &dst);
533 (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
534 }
535 if (!in_addr_is_null(route->family, &route->src))
536 (void) in_addr_to_string(route->family, &route->src, &src);
537 if (!in_addr_is_null(route->family, &route->gw))
538 (void) in_addr_to_string(route->family, &route->gw, &gw);
539 if (!in_addr_is_null(route->family, &route->prefsrc))
540 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
541
542 log_link_debug(link, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
543 strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc),
544 format_route_scope(route->scope, scope, sizeof(scope)),
545 format_route_table(route->table, table, sizeof(table)),
546 format_route_protocol(route->protocol, protocol, sizeof(protocol)),
547 strna(route_type_to_string(route->type)));
548 }
549
550 if (in_addr_is_null(route->family, &route->gw) == 0) {
551 r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
552 if (r < 0)
553 return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m");
554 }
555
556 if (route->dst_prefixlen) {
557 r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
558 if (r < 0)
559 return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m");
560
561 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
562 if (r < 0)
563 return log_link_error_errno(link, r, "Could not set destination prefix length: %m");
564 }
565
566 if (route->src_prefixlen) {
567 r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
568 if (r < 0)
569 return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m");
570
571 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
572 if (r < 0)
573 return log_link_error_errno(link, r, "Could not set source prefix length: %m");
574 }
575
576 if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
577 r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
578 if (r < 0)
579 return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m");
580 }
581
582 r = sd_rtnl_message_route_set_scope(req, route->scope);
583 if (r < 0)
584 return log_link_error_errno(link, r, "Could not set scope: %m");
585
586 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
587 if (r < 0)
588 return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m");
589
590 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) {
591 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
592 if (r < 0)
593 return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m");
594 }
595
596 r = netlink_call_async(link->manager->rtnl, NULL, req,
597 callback ?: route_remove_handler,
598 link_netlink_destroy_callback, link);
599 if (r < 0)
600 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
601
602 link_ref(link);
603
604 return 0;
605 }
606
607 int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) {
608 Route *route = userdata;
609 int r;
610
611 assert(route);
612
613 r = route_remove(route, route->link, NULL);
614 if (r < 0)
615 log_warning_errno(r, "Could not remove route: %m");
616 else
617 route_free(route);
618
619 return 1;
620 }
621
622 int route_configure(
623 Route *route,
624 Link *link,
625 link_netlink_message_handler_t callback) {
626
627 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
628 _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL;
629 usec_t lifetime;
630 int r;
631
632 assert(link);
633 assert(link->manager);
634 assert(link->manager->rtnl);
635 assert(link->ifindex > 0);
636 assert(IN_SET(route->family, AF_INET, AF_INET6));
637 assert(callback);
638
639 if (route_get(link, route->family, &route->dst, route->dst_prefixlen, &route->gw, route->tos, route->priority, route->table, NULL) <= 0 &&
640 set_size(link->routes) >= routes_max())
641 return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG),
642 "Too many routes are configured, refusing: %m");
643
644 if (DEBUG_LOGGING) {
645 _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL;
646 char scope[ROUTE_SCOPE_STR_MAX], table[ROUTE_TABLE_STR_MAX], protocol[ROUTE_PROTOCOL_STR_MAX];
647
648 if (!in_addr_is_null(route->family, &route->dst)) {
649 (void) in_addr_to_string(route->family, &route->dst, &dst);
650 (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
651 }
652 if (!in_addr_is_null(route->family, &route->src))
653 (void) in_addr_to_string(route->family, &route->src, &src);
654 if (!in_addr_is_null(route->family, &route->gw))
655 (void) in_addr_to_string(route->family, &route->gw, &gw);
656 if (!in_addr_is_null(route->family, &route->prefsrc))
657 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
658
659 log_link_debug(link, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
660 strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc),
661 format_route_scope(route->scope, scope, sizeof(scope)),
662 format_route_table(route->table, table, sizeof(table)),
663 format_route_protocol(route->protocol, protocol, sizeof(protocol)),
664 strna(route_type_to_string(route->type)));
665 }
666
667 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
668 RTM_NEWROUTE, route->family,
669 route->protocol);
670 if (r < 0)
671 return log_link_error_errno(link, r, "Could not create RTM_NEWROUTE message: %m");
672
673 if (in_addr_is_null(route->family, &route->gw) == 0) {
674 r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
675 if (r < 0)
676 return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m");
677
678 r = sd_rtnl_message_route_set_family(req, route->family);
679 if (r < 0)
680 return log_link_error_errno(link, r, "Could not set route family: %m");
681 }
682
683 if (route->dst_prefixlen) {
684 r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
685 if (r < 0)
686 return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m");
687
688 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
689 if (r < 0)
690 return log_link_error_errno(link, r, "Could not set destination prefix length: %m");
691 }
692
693 if (route->src_prefixlen) {
694 r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
695 if (r < 0)
696 return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m");
697
698 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
699 if (r < 0)
700 return log_link_error_errno(link, r, "Could not set source prefix length: %m");
701 }
702
703 if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
704 r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
705 if (r < 0)
706 return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m");
707 }
708
709 r = sd_rtnl_message_route_set_scope(req, route->scope);
710 if (r < 0)
711 return log_link_error_errno(link, r, "Could not set scope: %m");
712
713 if (route->gateway_onlink >= 0)
714 SET_FLAG(route->flags, RTNH_F_ONLINK, route->gateway_onlink);
715
716 r = sd_rtnl_message_route_set_flags(req, route->flags);
717 if (r < 0)
718 return log_link_error_errno(link, r, "Could not set flags: %m");
719
720 if (route->table != RT_TABLE_MAIN) {
721 if (route->table < 256) {
722 r = sd_rtnl_message_route_set_table(req, route->table);
723 if (r < 0)
724 return log_link_error_errno(link, r, "Could not set route table: %m");
725 } else {
726 r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC);
727 if (r < 0)
728 return log_link_error_errno(link, r, "Could not set route table: %m");
729
730 /* Table attribute to allow more than 256. */
731 r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table));
732 if (r < 0)
733 return log_link_error_errno(link, r, "Could not append RTA_TABLE attribute: %m");
734 }
735 }
736
737 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
738 if (r < 0)
739 return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m");
740
741 r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref);
742 if (r < 0)
743 return log_link_error_errno(link, r, "Could not append RTA_PREF attribute: %m");
744
745 if (route->lifetime != USEC_INFINITY && kernel_route_expiration_supported()) {
746 r = sd_netlink_message_append_u32(req, RTA_EXPIRES,
747 DIV_ROUND_UP(usec_sub_unsigned(route->lifetime, now(clock_boottime_or_monotonic())), USEC_PER_SEC));
748 if (r < 0)
749 return log_link_error_errno(link, r, "Could not append RTA_EXPIRES attribute: %m");
750 }
751
752 r = sd_rtnl_message_route_set_type(req, route->type);
753 if (r < 0)
754 return log_link_error_errno(link, r, "Could not set route type: %m");
755
756 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) {
757 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
758 if (r < 0)
759 return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m");
760 }
761
762 if (route->ttl_propagate >= 0) {
763 r = sd_netlink_message_append_u8(req, RTA_TTL_PROPAGATE, route->ttl_propagate);
764 if (r < 0)
765 return log_link_error_errno(link, r, "Could not append RTA_TTL_PROPAGATE attribute: %m");
766 }
767
768 r = sd_netlink_message_open_container(req, RTA_METRICS);
769 if (r < 0)
770 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
771
772 if (route->mtu > 0) {
773 r = sd_netlink_message_append_u32(req, RTAX_MTU, route->mtu);
774 if (r < 0)
775 return log_link_error_errno(link, r, "Could not append RTAX_MTU attribute: %m");
776 }
777
778 if (route->initcwnd > 0) {
779 r = sd_netlink_message_append_u32(req, RTAX_INITCWND, route->initcwnd);
780 if (r < 0)
781 return log_link_error_errno(link, r, "Could not append RTAX_INITCWND attribute: %m");
782 }
783
784 if (route->initrwnd > 0) {
785 r = sd_netlink_message_append_u32(req, RTAX_INITRWND, route->initrwnd);
786 if (r < 0)
787 return log_link_error_errno(link, r, "Could not append RTAX_INITRWND attribute: %m");
788 }
789
790 if (route->quickack >= 0) {
791 r = sd_netlink_message_append_u32(req, RTAX_QUICKACK, route->quickack);
792 if (r < 0)
793 return log_link_error_errno(link, r, "Could not append RTAX_QUICKACK attribute: %m");
794 }
795
796 if (route->fast_open_no_cookie >= 0) {
797 r = sd_netlink_message_append_u32(req, RTAX_FASTOPEN_NO_COOKIE, route->fast_open_no_cookie);
798 if (r < 0)
799 return log_link_error_errno(link, r, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
800 }
801
802 r = sd_netlink_message_close_container(req);
803 if (r < 0)
804 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
805
806 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
807 link_netlink_destroy_callback, link);
808 if (r < 0)
809 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
810
811 link_ref(link);
812
813 lifetime = route->lifetime;
814
815 r = route_add(link, route->family, &route->dst, route->dst_prefixlen, &route->gw, route->tos, route->priority, route->table, &route);
816 if (r < 0)
817 return log_link_error_errno(link, r, "Could not add route: %m");
818
819 /* TODO: drop expiration handling once it can be pushed into the kernel */
820 route->lifetime = lifetime;
821
822 if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
823 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(),
824 route->lifetime, 0, route_expire_handler, route);
825 if (r < 0)
826 return log_link_error_errno(link, r, "Could not arm expiration timer: %m");
827 }
828
829 sd_event_source_unref(route->expire);
830 route->expire = TAKE_PTR(expire);
831
832 return 1;
833 }
834
835 int network_add_ipv4ll_route(Network *network) {
836 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
837 int r;
838
839 assert(network);
840
841 if (!network->ipv4ll_route)
842 return 0;
843
844 /* IPv4LLRoute= is in [Network] section. */
845 r = route_new_static(network, NULL, 0, &n);
846 if (r < 0)
847 return r;
848
849 r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst);
850 if (r < 0)
851 return r;
852
853 n->family = AF_INET;
854 n->dst_prefixlen = 16;
855 n->scope = RT_SCOPE_LINK;
856 n->scope_set = true;
857 n->table_set = true;
858 n->priority = IPV4LL_ROUTE_METRIC;
859 n->protocol = RTPROT_STATIC;
860
861 TAKE_PTR(n);
862 return 0;
863 }
864
865 int network_add_default_route_on_device(Network *network) {
866 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
867 int r;
868
869 assert(network);
870
871 if (!network->default_route_on_device)
872 return 0;
873
874 /* DefaultRouteOnDevice= is in [Network] section. */
875 r = route_new_static(network, NULL, 0, &n);
876 if (r < 0)
877 return r;
878
879 r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst);
880 if (r < 0)
881 return r;
882
883 n->family = AF_INET;
884
885 TAKE_PTR(n);
886 return 0;
887 }
888
889 static const char * const route_type_table[__RTN_MAX] = {
890 [RTN_UNICAST] = "unicast",
891 [RTN_LOCAL] = "local",
892 [RTN_BROADCAST] = "broadcast",
893 [RTN_ANYCAST] = "anycast",
894 [RTN_MULTICAST] = "multicast",
895 [RTN_BLACKHOLE] = "blackhole",
896 [RTN_UNREACHABLE] = "unreachable",
897 [RTN_PROHIBIT] = "prohibit",
898 [RTN_THROW] = "throw",
899 [RTN_NAT] = "nat",
900 [RTN_XRESOLVE] = "xresolve",
901 };
902
903 assert_cc(__RTN_MAX <= UCHAR_MAX);
904 DEFINE_STRING_TABLE_LOOKUP(route_type, int);
905
906 static const char * const route_scope_table[] = {
907 [RT_SCOPE_UNIVERSE] = "global",
908 [RT_SCOPE_SITE] = "site",
909 [RT_SCOPE_LINK] = "link",
910 [RT_SCOPE_HOST] = "host",
911 [RT_SCOPE_NOWHERE] = "nowhere",
912 };
913
914 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope, int);
915
916 const char *format_route_scope(int scope, char *buf, size_t size) {
917 const char *s;
918 char *p = buf;
919
920 s = route_scope_to_string(scope);
921 if (s)
922 strpcpy(&p, size, s);
923 else
924 strpcpyf(&p, size, "%d", scope);
925
926 return buf;
927 }
928
929 static const char * const route_table_table[] = {
930 [RT_TABLE_DEFAULT] = "default",
931 [RT_TABLE_MAIN] = "main",
932 [RT_TABLE_LOCAL] = "local",
933 };
934
935 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table, int);
936
937 const char *format_route_table(int table, char *buf, size_t size) {
938 const char *s;
939 char *p = buf;
940
941 s = route_table_to_string(table);
942 if (s)
943 strpcpy(&p, size, s);
944 else
945 strpcpyf(&p, size, "%d", table);
946
947 return buf;
948 }
949
950 static const char * const route_protocol_table[] = {
951 [RTPROT_KERNEL] = "kernel",
952 [RTPROT_BOOT] = "boot",
953 [RTPROT_STATIC] = "static",
954 };
955
956 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol, int);
957
958 static const char * const route_protocol_full_table[] = {
959 [RTPROT_REDIRECT] = "redirect",
960 [RTPROT_KERNEL] = "kernel",
961 [RTPROT_BOOT] = "boot",
962 [RTPROT_STATIC] = "static",
963 [RTPROT_GATED] = "gated",
964 [RTPROT_RA] = "ra",
965 [RTPROT_MRT] = "mrt",
966 [RTPROT_ZEBRA] = "zebra",
967 [RTPROT_BIRD] = "bird",
968 [RTPROT_DNROUTED] = "dnrouted",
969 [RTPROT_XORP] = "xorp",
970 [RTPROT_NTK] = "ntk",
971 [RTPROT_DHCP] = "dhcp",
972 [RTPROT_MROUTED] = "mrouted",
973 [RTPROT_BABEL] = "babel",
974 [RTPROT_BGP] = "bgp",
975 [RTPROT_ISIS] = "isis",
976 [RTPROT_OSPF] = "ospf",
977 [RTPROT_RIP] = "rip",
978 [RTPROT_EIGRP] = "eigrp",
979 };
980
981 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full, int);
982
983 const char *format_route_protocol(int protocol, char *buf, size_t size) {
984 const char *s;
985 char *p = buf;
986
987 s = route_protocol_full_to_string(protocol);
988 if (s)
989 strpcpy(&p, size, s);
990 else
991 strpcpyf(&p, size, "%d", protocol);
992
993 return buf;
994 }
995
996 int config_parse_gateway(
997 const char *unit,
998 const char *filename,
999 unsigned line,
1000 const char *section,
1001 unsigned section_line,
1002 const char *lvalue,
1003 int ltype,
1004 const char *rvalue,
1005 void *data,
1006 void *userdata) {
1007
1008 Network *network = userdata;
1009 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1010 int r;
1011
1012 assert(filename);
1013 assert(section);
1014 assert(lvalue);
1015 assert(rvalue);
1016 assert(data);
1017
1018 if (streq(section, "Network")) {
1019 /* we are not in an Route section, so treat
1020 * this as the special '0' section */
1021 r = route_new_static(network, NULL, 0, &n);
1022 } else
1023 r = route_new_static(network, filename, section_line, &n);
1024 if (r < 0)
1025 return r;
1026
1027 if (n->family == AF_UNSPEC)
1028 r = in_addr_from_string_auto(rvalue, &n->family, &n->gw);
1029 else
1030 r = in_addr_from_string(n->family, rvalue, &n->gw);
1031 if (r < 0) {
1032 log_syntax(unit, LOG_ERR, filename, line, r,
1033 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
1034 return 0;
1035 }
1036
1037 TAKE_PTR(n);
1038 return 0;
1039 }
1040
1041 int config_parse_preferred_src(
1042 const char *unit,
1043 const char *filename,
1044 unsigned line,
1045 const char *section,
1046 unsigned section_line,
1047 const char *lvalue,
1048 int ltype,
1049 const char *rvalue,
1050 void *data,
1051 void *userdata) {
1052
1053 Network *network = userdata;
1054 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1055 int r;
1056
1057 assert(filename);
1058 assert(section);
1059 assert(lvalue);
1060 assert(rvalue);
1061 assert(data);
1062
1063 r = route_new_static(network, filename, section_line, &n);
1064 if (r < 0)
1065 return r;
1066
1067 if (n->family == AF_UNSPEC)
1068 r = in_addr_from_string_auto(rvalue, &n->family, &n->prefsrc);
1069 else
1070 r = in_addr_from_string(n->family, rvalue, &n->prefsrc);
1071 if (r < 0) {
1072 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
1073 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
1074 return 0;
1075 }
1076
1077 TAKE_PTR(n);
1078 return 0;
1079 }
1080
1081 int config_parse_destination(
1082 const char *unit,
1083 const char *filename,
1084 unsigned line,
1085 const char *section,
1086 unsigned section_line,
1087 const char *lvalue,
1088 int ltype,
1089 const char *rvalue,
1090 void *data,
1091 void *userdata) {
1092
1093 Network *network = userdata;
1094 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1095 union in_addr_union *buffer;
1096 unsigned char *prefixlen;
1097 int r;
1098
1099 assert(filename);
1100 assert(section);
1101 assert(lvalue);
1102 assert(rvalue);
1103 assert(data);
1104
1105 r = route_new_static(network, filename, section_line, &n);
1106 if (r < 0)
1107 return r;
1108
1109 if (streq(lvalue, "Destination")) {
1110 buffer = &n->dst;
1111 prefixlen = &n->dst_prefixlen;
1112 } else if (streq(lvalue, "Source")) {
1113 buffer = &n->src;
1114 prefixlen = &n->src_prefixlen;
1115 } else
1116 assert_not_reached(lvalue);
1117
1118 if (n->family == AF_UNSPEC)
1119 r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen);
1120 else
1121 r = in_addr_prefix_from_string(rvalue, n->family, buffer, prefixlen);
1122 if (r < 0) {
1123 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
1124 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
1125 return 0;
1126 }
1127
1128 TAKE_PTR(n);
1129 return 0;
1130 }
1131
1132 int config_parse_route_priority(
1133 const char *unit,
1134 const char *filename,
1135 unsigned line,
1136 const char *section,
1137 unsigned section_line,
1138 const char *lvalue,
1139 int ltype,
1140 const char *rvalue,
1141 void *data,
1142 void *userdata) {
1143
1144 Network *network = userdata;
1145 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1146 int r;
1147
1148 assert(filename);
1149 assert(section);
1150 assert(lvalue);
1151 assert(rvalue);
1152 assert(data);
1153
1154 r = route_new_static(network, filename, section_line, &n);
1155 if (r < 0)
1156 return r;
1157
1158 r = safe_atou32(rvalue, &n->priority);
1159 if (r < 0) {
1160 log_syntax(unit, LOG_ERR, filename, line, r,
1161 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue);
1162 return 0;
1163 }
1164
1165 TAKE_PTR(n);
1166 return 0;
1167 }
1168
1169 int config_parse_route_scope(
1170 const char *unit,
1171 const char *filename,
1172 unsigned line,
1173 const char *section,
1174 unsigned section_line,
1175 const char *lvalue,
1176 int ltype,
1177 const char *rvalue,
1178 void *data,
1179 void *userdata) {
1180
1181 Network *network = userdata;
1182 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1183 int r;
1184
1185 assert(filename);
1186 assert(section);
1187 assert(lvalue);
1188 assert(rvalue);
1189 assert(data);
1190
1191 r = route_new_static(network, filename, section_line, &n);
1192 if (r < 0)
1193 return r;
1194
1195 r = route_scope_from_string(rvalue);
1196 if (r < 0) {
1197 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route scope: %s", rvalue);
1198 return 0;
1199 }
1200
1201 n->scope = r;
1202 n->scope_set = true;
1203 TAKE_PTR(n);
1204 return 0;
1205 }
1206
1207 int config_parse_route_table(
1208 const char *unit,
1209 const char *filename,
1210 unsigned line,
1211 const char *section,
1212 unsigned section_line,
1213 const char *lvalue,
1214 int ltype,
1215 const char *rvalue,
1216 void *data,
1217 void *userdata) {
1218
1219 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1220 Network *network = userdata;
1221 int r;
1222
1223 assert(filename);
1224 assert(section);
1225 assert(lvalue);
1226 assert(rvalue);
1227 assert(data);
1228
1229 r = route_new_static(network, filename, section_line, &n);
1230 if (r < 0)
1231 return r;
1232
1233 r = route_table_from_string(rvalue);
1234 if (r >= 0)
1235 n->table = r;
1236 else {
1237 r = safe_atou32(rvalue, &n->table);
1238 if (r < 0) {
1239 log_syntax(unit, LOG_ERR, filename, line, r,
1240 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
1241 return 0;
1242 }
1243 }
1244
1245 n->table_set = true;
1246 TAKE_PTR(n);
1247 return 0;
1248 }
1249
1250 int config_parse_gateway_onlink(
1251 const char *unit,
1252 const char *filename,
1253 unsigned line,
1254 const char *section,
1255 unsigned section_line,
1256 const char *lvalue,
1257 int ltype,
1258 const char *rvalue,
1259 void *data,
1260 void *userdata) {
1261
1262 Network *network = userdata;
1263 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1264 int r;
1265
1266 assert(filename);
1267 assert(section);
1268 assert(lvalue);
1269 assert(rvalue);
1270 assert(data);
1271
1272 r = route_new_static(network, filename, section_line, &n);
1273 if (r < 0)
1274 return r;
1275
1276 r = parse_boolean(rvalue);
1277 if (r < 0) {
1278 log_syntax(unit, LOG_ERR, filename, line, r,
1279 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue, rvalue);
1280 return 0;
1281 }
1282
1283 n->gateway_onlink = r;
1284
1285 TAKE_PTR(n);
1286 return 0;
1287 }
1288
1289 int config_parse_ipv6_route_preference(
1290 const char *unit,
1291 const char *filename,
1292 unsigned line,
1293 const char *section,
1294 unsigned section_line,
1295 const char *lvalue,
1296 int ltype,
1297 const char *rvalue,
1298 void *data,
1299 void *userdata) {
1300
1301 Network *network = userdata;
1302 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1303 int r;
1304
1305 r = route_new_static(network, filename, section_line, &n);
1306 if (r < 0)
1307 return r;
1308
1309 if (streq(rvalue, "low"))
1310 n->pref = ICMPV6_ROUTER_PREF_LOW;
1311 else if (streq(rvalue, "medium"))
1312 n->pref = ICMPV6_ROUTER_PREF_MEDIUM;
1313 else if (streq(rvalue, "high"))
1314 n->pref = ICMPV6_ROUTER_PREF_HIGH;
1315 else {
1316 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route preference: %s", rvalue);
1317 return 0;
1318 }
1319
1320 TAKE_PTR(n);
1321 return 0;
1322 }
1323
1324 int config_parse_route_protocol(
1325 const char *unit,
1326 const char *filename,
1327 unsigned line,
1328 const char *section,
1329 unsigned section_line,
1330 const char *lvalue,
1331 int ltype,
1332 const char *rvalue,
1333 void *data,
1334 void *userdata) {
1335
1336 Network *network = userdata;
1337 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1338 int r;
1339
1340 r = route_new_static(network, filename, section_line, &n);
1341 if (r < 0)
1342 return r;
1343
1344 r = route_protocol_from_string(rvalue);
1345 if (r >= 0)
1346 n->protocol = r;
1347 else {
1348 r = safe_atou8(rvalue , &n->protocol);
1349 if (r < 0) {
1350 log_syntax(unit, LOG_ERR, filename, line, r,
1351 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue);
1352 return 0;
1353 }
1354 }
1355
1356 TAKE_PTR(n);
1357 return 0;
1358 }
1359
1360 int config_parse_route_type(
1361 const char *unit,
1362 const char *filename,
1363 unsigned line,
1364 const char *section,
1365 unsigned section_line,
1366 const char *lvalue,
1367 int ltype,
1368 const char *rvalue,
1369 void *data,
1370 void *userdata) {
1371
1372 Network *network = userdata;
1373 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1374 int t, r;
1375
1376 r = route_new_static(network, filename, section_line, &n);
1377 if (r < 0)
1378 return r;
1379
1380 t = route_type_from_string(rvalue);
1381 if (t < 0) {
1382 log_syntax(unit, LOG_ERR, filename, line, 0,
1383 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
1384 return 0;
1385 }
1386
1387 n->type = (unsigned char) t;
1388
1389 TAKE_PTR(n);
1390 return 0;
1391 }
1392
1393 int config_parse_tcp_window(
1394 const char *unit,
1395 const char *filename,
1396 unsigned line,
1397 const char *section,
1398 unsigned section_line,
1399 const char *lvalue,
1400 int ltype,
1401 const char *rvalue,
1402 void *data,
1403 void *userdata) {
1404
1405 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1406 Network *network = userdata;
1407 uint64_t k;
1408 int r;
1409
1410 assert(filename);
1411 assert(section);
1412 assert(lvalue);
1413 assert(rvalue);
1414 assert(data);
1415
1416 r = route_new_static(network, filename, section_line, &n);
1417 if (r < 0)
1418 return r;
1419
1420 r = parse_size(rvalue, 1024, &k);
1421 if (r < 0) {
1422 log_syntax(unit, LOG_ERR, filename, line, r,
1423 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue, rvalue);
1424 return 0;
1425 }
1426 if (k > UINT32_MAX) {
1427 log_syntax(unit, LOG_ERR, filename, line, 0,
1428 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue, rvalue);
1429 return 0;
1430 }
1431
1432 if (streq(lvalue, "InitialCongestionWindow"))
1433 n->initcwnd = k;
1434 else if (streq(lvalue, "InitialAdvertisedReceiveWindow"))
1435 n->initrwnd = k;
1436 else
1437 assert_not_reached("Invalid TCP window type.");
1438
1439 TAKE_PTR(n);
1440 return 0;
1441 }
1442
1443 int config_parse_quickack(
1444 const char *unit,
1445 const char *filename,
1446 unsigned line,
1447 const char *section,
1448 unsigned section_line,
1449 const char *lvalue,
1450 int ltype,
1451 const char *rvalue,
1452 void *data,
1453 void *userdata) {
1454
1455 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1456 Network *network = userdata;
1457 int k, r;
1458
1459 assert(filename);
1460 assert(section);
1461 assert(lvalue);
1462 assert(rvalue);
1463 assert(data);
1464
1465 r = route_new_static(network, filename, section_line, &n);
1466 if (r < 0)
1467 return r;
1468
1469 k = parse_boolean(rvalue);
1470 if (k < 0) {
1471 log_syntax(unit, LOG_ERR, filename, line, k,
1472 "Failed to parse TCP quickack, ignoring: %s", rvalue);
1473 return 0;
1474 }
1475
1476 n->quickack = !!k;
1477 TAKE_PTR(n);
1478 return 0;
1479 }
1480
1481 int config_parse_fast_open_no_cookie(
1482 const char *unit,
1483 const char *filename,
1484 unsigned line,
1485 const char *section,
1486 unsigned section_line,
1487 const char *lvalue,
1488 int ltype,
1489 const char *rvalue,
1490 void *data,
1491 void *userdata) {
1492
1493 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1494 Network *network = userdata;
1495 int k, r;
1496
1497 assert(filename);
1498 assert(section);
1499 assert(lvalue);
1500 assert(rvalue);
1501 assert(data);
1502
1503 r = route_new_static(network, filename, section_line, &n);
1504 if (r < 0)
1505 return r;
1506
1507 k = parse_boolean(rvalue);
1508 if (k < 0) {
1509 log_syntax(unit, LOG_ERR, filename, line, k,
1510 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue);
1511 return 0;
1512 }
1513
1514 n->fast_open_no_cookie = k;
1515 TAKE_PTR(n);
1516 return 0;
1517 }
1518
1519 int config_parse_route_mtu(
1520 const char *unit,
1521 const char *filename,
1522 unsigned line,
1523 const char *section,
1524 unsigned section_line,
1525 const char *lvalue,
1526 int ltype,
1527 const char *rvalue,
1528 void *data,
1529 void *userdata) {
1530
1531 Network *network = userdata;
1532 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1533 int r;
1534
1535 assert(filename);
1536 assert(section);
1537 assert(lvalue);
1538 assert(rvalue);
1539 assert(data);
1540
1541 r = route_new_static(network, filename, section_line, &n);
1542 if (r < 0)
1543 return r;
1544
1545 r = config_parse_mtu(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->mtu, userdata);
1546 if (r < 0)
1547 return r;
1548
1549 TAKE_PTR(n);
1550 return 0;
1551 }
1552
1553 int config_parse_route_ttl_propagate(
1554 const char *unit,
1555 const char *filename,
1556 unsigned line,
1557 const char *section,
1558 unsigned section_line,
1559 const char *lvalue,
1560 int ltype,
1561 const char *rvalue,
1562 void *data,
1563 void *userdata) {
1564
1565 Network *network = userdata;
1566 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1567 int r, k;
1568
1569 assert(filename);
1570 assert(section);
1571 assert(lvalue);
1572 assert(rvalue);
1573 assert(data);
1574
1575 r = route_new_static(network, filename, section_line, &n);
1576 if (r < 0)
1577 return r;
1578
1579 k = parse_boolean(rvalue);
1580 if (k < 0) {
1581 log_syntax(unit, LOG_ERR, filename, line, k,
1582 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue);
1583 return 0;
1584 }
1585
1586 n->ttl_propagate = k;
1587
1588 TAKE_PTR(n);
1589 return 0;
1590 }
1591
1592 int route_section_verify(Route *route, Network *network) {
1593 if (section_is_invalid(route->section))
1594 return -EINVAL;
1595
1596 if (route->family == AF_UNSPEC) {
1597 assert(route->section);
1598
1599 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1600 "%s: Route section without Gateway=, Destination=, Source=, "
1601 "or PreferredSource= field configured. "
1602 "Ignoring [Route] section from line %u.",
1603 route->section->filename, route->section->line);
1604 }
1605
1606 if (route->family != AF_INET6) {
1607 if (!route->table_set && IN_SET(route->type, RTN_LOCAL, RTN_BROADCAST, RTN_ANYCAST, RTN_NAT))
1608 route->table = RT_TABLE_LOCAL;
1609
1610 if (!route->scope_set) {
1611 if (IN_SET(route->type, RTN_LOCAL, RTN_NAT))
1612 route->scope = RT_SCOPE_HOST;
1613 else if (IN_SET(route->type, RTN_BROADCAST, RTN_ANYCAST))
1614 route->scope = RT_SCOPE_LINK;
1615 }
1616 }
1617
1618 if (network->n_static_addresses == 0 &&
1619 in_addr_is_null(route->family, &route->gw) == 0 &&
1620 route->gateway_onlink < 0) {
1621 log_warning("%s: Gateway= without static address configured. "
1622 "Enabling GatewayOnLink= option.",
1623 network->filename);
1624 route->gateway_onlink = true;
1625 }
1626
1627 return 0;
1628 }