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