]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-route.c
Merge pull request #12753 from jrouleau/fix/hibernate-resume-timeout
[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->family == AF_INET6 && link_sysctl_ipv6_enabled(link) == 0) {
504 log_link_warning(link, "An IPv6 route is requested, but IPv6 is disabled by sysctl, ignoring.");
505 return 0;
506 }
507
508 if (route_get(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, NULL) <= 0 &&
509 set_size(link->routes) >= routes_max())
510 return log_link_error_errno(link, SYNTHETIC_ERRNO(E2BIG),
511 "Too many routes are configured, refusing: %m");
512
513 if (DEBUG_LOGGING) {
514 _cleanup_free_ char *dst = NULL, *dst_prefixlen = NULL, *src = NULL, *gw = NULL, *prefsrc = NULL;
515
516 if (!in_addr_is_null(route->family, &route->dst)) {
517 (void) in_addr_to_string(route->family, &route->dst, &dst);
518 (void) asprintf(&dst_prefixlen, "/%u", route->dst_prefixlen);
519 }
520 if (!in_addr_is_null(route->family, &route->src))
521 (void) in_addr_to_string(route->family, &route->src, &src);
522 if (!in_addr_is_null(route->family, &route->gw))
523 (void) in_addr_to_string(route->family, &route->gw, &gw);
524 if (!in_addr_is_null(route->family, &route->prefsrc))
525 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
526
527 log_link_debug(link, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s",
528 strna(dst), strempty(dst_prefixlen), strna(src), strna(gw), strna(prefsrc));
529 }
530
531 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
532 RTM_NEWROUTE, route->family,
533 route->protocol);
534 if (r < 0)
535 return log_link_error_errno(link, r, "Could not create RTM_NEWROUTE message: %m");
536
537 if (in_addr_is_null(route->family, &route->gw) == 0) {
538 r = netlink_message_append_in_addr_union(req, RTA_GATEWAY, route->family, &route->gw);
539 if (r < 0)
540 return log_link_error_errno(link, r, "Could not append RTA_GATEWAY attribute: %m");
541
542 r = sd_rtnl_message_route_set_family(req, route->family);
543 if (r < 0)
544 return log_link_error_errno(link, r, "Could not set route family: %m");
545 }
546
547 if (route->dst_prefixlen) {
548 r = netlink_message_append_in_addr_union(req, RTA_DST, route->family, &route->dst);
549 if (r < 0)
550 return log_link_error_errno(link, r, "Could not append RTA_DST attribute: %m");
551
552 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
553 if (r < 0)
554 return log_link_error_errno(link, r, "Could not set destination prefix length: %m");
555 }
556
557 if (route->src_prefixlen) {
558 r = netlink_message_append_in_addr_union(req, RTA_SRC, route->family, &route->src);
559 if (r < 0)
560 return log_link_error_errno(link, r, "Could not append RTA_SRC attribute: %m");
561
562 r = sd_rtnl_message_route_set_src_prefixlen(req, route->src_prefixlen);
563 if (r < 0)
564 return log_link_error_errno(link, r, "Could not set source prefix length: %m");
565 }
566
567 if (in_addr_is_null(route->family, &route->prefsrc) == 0) {
568 r = netlink_message_append_in_addr_union(req, RTA_PREFSRC, route->family, &route->prefsrc);
569 if (r < 0)
570 return log_link_error_errno(link, r, "Could not append RTA_PREFSRC attribute: %m");
571 }
572
573 r = sd_rtnl_message_route_set_scope(req, route->scope);
574 if (r < 0)
575 return log_link_error_errno(link, r, "Could not set scope: %m");
576
577 if (route->gateway_onlink >= 0)
578 SET_FLAG(route->flags, RTNH_F_ONLINK, route->gateway_onlink);
579
580 r = sd_rtnl_message_route_set_flags(req, route->flags);
581 if (r < 0)
582 return log_link_error_errno(link, r, "Could not set flags: %m");
583
584 if (route->table != RT_TABLE_MAIN) {
585 if (route->table < 256) {
586 r = sd_rtnl_message_route_set_table(req, route->table);
587 if (r < 0)
588 return log_link_error_errno(link, r, "Could not set route table: %m");
589 } else {
590 r = sd_rtnl_message_route_set_table(req, RT_TABLE_UNSPEC);
591 if (r < 0)
592 return log_link_error_errno(link, r, "Could not set route table: %m");
593
594 /* Table attribute to allow more than 256. */
595 r = sd_netlink_message_append_data(req, RTA_TABLE, &route->table, sizeof(route->table));
596 if (r < 0)
597 return log_link_error_errno(link, r, "Could not append RTA_TABLE attribute: %m");
598 }
599 }
600
601 r = sd_netlink_message_append_u32(req, RTA_PRIORITY, route->priority);
602 if (r < 0)
603 return log_link_error_errno(link, r, "Could not append RTA_PRIORITY attribute: %m");
604
605 r = sd_netlink_message_append_u8(req, RTA_PREF, route->pref);
606 if (r < 0)
607 return log_link_error_errno(link, r, "Could not append RTA_PREF attribute: %m");
608
609 if (route->lifetime != USEC_INFINITY && kernel_route_expiration_supported()) {
610 r = sd_netlink_message_append_u32(req, RTA_EXPIRES,
611 DIV_ROUND_UP(usec_sub_unsigned(route->lifetime, now(clock_boottime_or_monotonic())), USEC_PER_SEC));
612 if (r < 0)
613 return log_link_error_errno(link, r, "Could not append RTA_EXPIRES attribute: %m");
614 }
615
616 r = sd_rtnl_message_route_set_type(req, route->type);
617 if (r < 0)
618 return log_link_error_errno(link, r, "Could not set route type: %m");
619
620 if (!IN_SET(route->type, RTN_UNREACHABLE, RTN_PROHIBIT, RTN_BLACKHOLE, RTN_THROW)) {
621 r = sd_netlink_message_append_u32(req, RTA_OIF, link->ifindex);
622 if (r < 0)
623 return log_link_error_errno(link, r, "Could not append RTA_OIF attribute: %m");
624 }
625
626 if (route->ttl_propagate >= 0) {
627 r = sd_netlink_message_append_u8(req, RTA_TTL_PROPAGATE, route->ttl_propagate);
628 if (r < 0)
629 return log_link_error_errno(link, r, "Could not append RTA_TTL_PROPAGATE attribute: %m");
630 }
631
632 r = sd_netlink_message_open_container(req, RTA_METRICS);
633 if (r < 0)
634 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
635
636 if (route->mtu > 0) {
637 r = sd_netlink_message_append_u32(req, RTAX_MTU, route->mtu);
638 if (r < 0)
639 return log_link_error_errno(link, r, "Could not append RTAX_MTU attribute: %m");
640 }
641
642 if (route->initcwnd > 0) {
643 r = sd_netlink_message_append_u32(req, RTAX_INITCWND, route->initcwnd);
644 if (r < 0)
645 return log_link_error_errno(link, r, "Could not append RTAX_INITCWND attribute: %m");
646 }
647
648 if (route->initrwnd > 0) {
649 r = sd_netlink_message_append_u32(req, RTAX_INITRWND, route->initrwnd);
650 if (r < 0)
651 return log_link_error_errno(link, r, "Could not append RTAX_INITRWND attribute: %m");
652 }
653
654 if (route->quickack >= 0) {
655 r = sd_netlink_message_append_u32(req, RTAX_QUICKACK, route->quickack);
656 if (r < 0)
657 return log_link_error_errno(link, r, "Could not append RTAX_QUICKACK attribute: %m");
658 }
659
660 if (route->fast_open_no_cookie >= 0) {
661 r = sd_netlink_message_append_u32(req, RTAX_FASTOPEN_NO_COOKIE, route->fast_open_no_cookie);
662 if (r < 0)
663 return log_link_error_errno(link, r, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
664 }
665
666 r = sd_netlink_message_close_container(req);
667 if (r < 0)
668 return log_link_error_errno(link, r, "Could not append RTA_METRICS attribute: %m");
669
670 r = netlink_call_async(link->manager->rtnl, NULL, req, callback,
671 link_netlink_destroy_callback, link);
672 if (r < 0)
673 return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
674
675 link_ref(link);
676
677 lifetime = route->lifetime;
678
679 r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, &route);
680 if (r < 0)
681 return log_link_error_errno(link, r, "Could not add route: %m");
682
683 /* TODO: drop expiration handling once it can be pushed into the kernel */
684 route->lifetime = lifetime;
685
686 if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) {
687 r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(),
688 route->lifetime, 0, route_expire_handler, route);
689 if (r < 0)
690 return log_link_error_errno(link, r, "Could not arm expiration timer: %m");
691 }
692
693 sd_event_source_unref(route->expire);
694 route->expire = TAKE_PTR(expire);
695
696 return 1;
697 }
698
699 int network_add_ipv4ll_route(Network *network) {
700 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
701 int r;
702
703 assert(network);
704
705 if (!network->ipv4ll_route)
706 return 0;
707
708 /* IPv4LLRoute= is in [Network] section. */
709 r = route_new_static(network, NULL, 0, &n);
710 if (r < 0)
711 return r;
712
713 r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst);
714 if (r < 0)
715 return r;
716
717 n->family = AF_INET;
718 n->dst_prefixlen = 16;
719 n->scope = RT_SCOPE_LINK;
720 n->priority = IPV4LL_ROUTE_METRIC;
721 n->protocol = RTPROT_STATIC;
722
723 TAKE_PTR(n);
724 return 0;
725 }
726
727 int network_add_default_route_on_device(Network *network) {
728 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
729 int r;
730
731 assert(network);
732
733 if (!network->default_route_on_device)
734 return 0;
735
736 /* DefaultRouteOnDevice= is in [Network] section. */
737 r = route_new_static(network, NULL, 0, &n);
738 if (r < 0)
739 return r;
740
741 r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst);
742 if (r < 0)
743 return r;
744
745 n->family = AF_INET;
746
747 TAKE_PTR(n);
748 return 0;
749 }
750
751 int config_parse_gateway(
752 const char *unit,
753 const char *filename,
754 unsigned line,
755 const char *section,
756 unsigned section_line,
757 const char *lvalue,
758 int ltype,
759 const char *rvalue,
760 void *data,
761 void *userdata) {
762
763 Network *network = userdata;
764 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
765 int r;
766
767 assert(filename);
768 assert(section);
769 assert(lvalue);
770 assert(rvalue);
771 assert(data);
772
773 if (streq(section, "Network")) {
774 /* we are not in an Route section, so treat
775 * this as the special '0' section */
776 r = route_new_static(network, NULL, 0, &n);
777 } else
778 r = route_new_static(network, filename, section_line, &n);
779 if (r < 0)
780 return r;
781
782 if (n->family == AF_UNSPEC)
783 r = in_addr_from_string_auto(rvalue, &n->family, &n->gw);
784 else
785 r = in_addr_from_string(n->family, rvalue, &n->gw);
786 if (r < 0) {
787 log_syntax(unit, LOG_ERR, filename, line, r,
788 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
789 return 0;
790 }
791
792 TAKE_PTR(n);
793 return 0;
794 }
795
796 int config_parse_preferred_src(
797 const char *unit,
798 const char *filename,
799 unsigned line,
800 const char *section,
801 unsigned section_line,
802 const char *lvalue,
803 int ltype,
804 const char *rvalue,
805 void *data,
806 void *userdata) {
807
808 Network *network = userdata;
809 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
810 int r;
811
812 assert(filename);
813 assert(section);
814 assert(lvalue);
815 assert(rvalue);
816 assert(data);
817
818 r = route_new_static(network, filename, section_line, &n);
819 if (r < 0)
820 return r;
821
822 if (n->family == AF_UNSPEC)
823 r = in_addr_from_string_auto(rvalue, &n->family, &n->prefsrc);
824 else
825 r = in_addr_from_string(n->family, rvalue, &n->prefsrc);
826 if (r < 0) {
827 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
828 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
829 return 0;
830 }
831
832 TAKE_PTR(n);
833 return 0;
834 }
835
836 int config_parse_destination(
837 const char *unit,
838 const char *filename,
839 unsigned line,
840 const char *section,
841 unsigned section_line,
842 const char *lvalue,
843 int ltype,
844 const char *rvalue,
845 void *data,
846 void *userdata) {
847
848 Network *network = userdata;
849 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
850 union in_addr_union *buffer;
851 unsigned char *prefixlen;
852 int r;
853
854 assert(filename);
855 assert(section);
856 assert(lvalue);
857 assert(rvalue);
858 assert(data);
859
860 r = route_new_static(network, filename, section_line, &n);
861 if (r < 0)
862 return r;
863
864 if (streq(lvalue, "Destination")) {
865 buffer = &n->dst;
866 prefixlen = &n->dst_prefixlen;
867 } else if (streq(lvalue, "Source")) {
868 buffer = &n->src;
869 prefixlen = &n->src_prefixlen;
870 } else
871 assert_not_reached(lvalue);
872
873 if (n->family == AF_UNSPEC)
874 r = in_addr_prefix_from_string_auto(rvalue, &n->family, buffer, prefixlen);
875 else
876 r = in_addr_prefix_from_string(rvalue, n->family, buffer, prefixlen);
877 if (r < 0) {
878 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
879 "Invalid %s='%s', ignoring assignment: %m", lvalue, rvalue);
880 return 0;
881 }
882
883 TAKE_PTR(n);
884 return 0;
885 }
886
887 int config_parse_route_priority(
888 const char *unit,
889 const char *filename,
890 unsigned line,
891 const char *section,
892 unsigned section_line,
893 const char *lvalue,
894 int ltype,
895 const char *rvalue,
896 void *data,
897 void *userdata) {
898
899 Network *network = userdata;
900 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
901 int r;
902
903 assert(filename);
904 assert(section);
905 assert(lvalue);
906 assert(rvalue);
907 assert(data);
908
909 r = route_new_static(network, filename, section_line, &n);
910 if (r < 0)
911 return r;
912
913 r = safe_atou32(rvalue, &n->priority);
914 if (r < 0) {
915 log_syntax(unit, LOG_ERR, filename, line, r,
916 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue);
917 return 0;
918 }
919
920 TAKE_PTR(n);
921 return 0;
922 }
923
924 int config_parse_route_scope(
925 const char *unit,
926 const char *filename,
927 unsigned line,
928 const char *section,
929 unsigned section_line,
930 const char *lvalue,
931 int ltype,
932 const char *rvalue,
933 void *data,
934 void *userdata) {
935
936 Network *network = userdata;
937 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
938 int r;
939
940 assert(filename);
941 assert(section);
942 assert(lvalue);
943 assert(rvalue);
944 assert(data);
945
946 r = route_new_static(network, filename, section_line, &n);
947 if (r < 0)
948 return r;
949
950 if (streq(rvalue, "host"))
951 n->scope = RT_SCOPE_HOST;
952 else if (streq(rvalue, "link"))
953 n->scope = RT_SCOPE_LINK;
954 else if (streq(rvalue, "global"))
955 n->scope = RT_SCOPE_UNIVERSE;
956 else {
957 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route scope: %s", rvalue);
958 return 0;
959 }
960
961 TAKE_PTR(n);
962 return 0;
963 }
964
965 int config_parse_route_table(
966 const char *unit,
967 const char *filename,
968 unsigned line,
969 const char *section,
970 unsigned section_line,
971 const char *lvalue,
972 int ltype,
973 const char *rvalue,
974 void *data,
975 void *userdata) {
976
977 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
978 Network *network = userdata;
979 int r;
980
981 assert(filename);
982 assert(section);
983 assert(lvalue);
984 assert(rvalue);
985 assert(data);
986
987 r = route_new_static(network, filename, section_line, &n);
988 if (r < 0)
989 return r;
990
991 r = safe_atou32(rvalue, &n->table);
992 if (r < 0) {
993 log_syntax(unit, LOG_ERR, filename, line, r,
994 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue);
995 return 0;
996 }
997
998 TAKE_PTR(n);
999 return 0;
1000 }
1001
1002 int config_parse_gateway_onlink(
1003 const char *unit,
1004 const char *filename,
1005 unsigned line,
1006 const char *section,
1007 unsigned section_line,
1008 const char *lvalue,
1009 int ltype,
1010 const char *rvalue,
1011 void *data,
1012 void *userdata) {
1013
1014 Network *network = userdata;
1015 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1016 int r;
1017
1018 assert(filename);
1019 assert(section);
1020 assert(lvalue);
1021 assert(rvalue);
1022 assert(data);
1023
1024 r = route_new_static(network, filename, section_line, &n);
1025 if (r < 0)
1026 return r;
1027
1028 r = parse_boolean(rvalue);
1029 if (r < 0) {
1030 log_syntax(unit, LOG_ERR, filename, line, r,
1031 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue, rvalue);
1032 return 0;
1033 }
1034
1035 n->gateway_onlink = r;
1036
1037 TAKE_PTR(n);
1038 return 0;
1039 }
1040
1041 int config_parse_ipv6_route_preference(
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 r = route_new_static(network, filename, section_line, &n);
1058 if (r < 0)
1059 return r;
1060
1061 if (streq(rvalue, "low"))
1062 n->pref = ICMPV6_ROUTER_PREF_LOW;
1063 else if (streq(rvalue, "medium"))
1064 n->pref = ICMPV6_ROUTER_PREF_MEDIUM;
1065 else if (streq(rvalue, "high"))
1066 n->pref = ICMPV6_ROUTER_PREF_HIGH;
1067 else {
1068 log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown route preference: %s", rvalue);
1069 return 0;
1070 }
1071
1072 TAKE_PTR(n);
1073 return 0;
1074 }
1075
1076 int config_parse_route_protocol(
1077 const char *unit,
1078 const char *filename,
1079 unsigned line,
1080 const char *section,
1081 unsigned section_line,
1082 const char *lvalue,
1083 int ltype,
1084 const char *rvalue,
1085 void *data,
1086 void *userdata) {
1087
1088 Network *network = userdata;
1089 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1090 int r;
1091
1092 r = route_new_static(network, filename, section_line, &n);
1093 if (r < 0)
1094 return r;
1095
1096 if (streq(rvalue, "kernel"))
1097 n->protocol = RTPROT_KERNEL;
1098 else if (streq(rvalue, "boot"))
1099 n->protocol = RTPROT_BOOT;
1100 else if (streq(rvalue, "static"))
1101 n->protocol = RTPROT_STATIC;
1102 else {
1103 r = safe_atou8(rvalue , &n->protocol);
1104 if (r < 0) {
1105 log_syntax(unit, LOG_ERR, filename, line, r,
1106 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue);
1107 return 0;
1108 }
1109 }
1110
1111 TAKE_PTR(n);
1112 return 0;
1113 }
1114
1115 int config_parse_route_type(
1116 const char *unit,
1117 const char *filename,
1118 unsigned line,
1119 const char *section,
1120 unsigned section_line,
1121 const char *lvalue,
1122 int ltype,
1123 const char *rvalue,
1124 void *data,
1125 void *userdata) {
1126
1127 Network *network = userdata;
1128 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1129 int r;
1130
1131 r = route_new_static(network, filename, section_line, &n);
1132 if (r < 0)
1133 return r;
1134
1135 if (streq(rvalue, "unicast"))
1136 n->type = RTN_UNICAST;
1137 else if (streq(rvalue, "blackhole"))
1138 n->type = RTN_BLACKHOLE;
1139 else if (streq(rvalue, "unreachable"))
1140 n->type = RTN_UNREACHABLE;
1141 else if (streq(rvalue, "prohibit"))
1142 n->type = RTN_PROHIBIT;
1143 else if (streq(rvalue, "throw"))
1144 n->type = RTN_THROW;
1145 else {
1146 log_syntax(unit, LOG_ERR, filename, line, r,
1147 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue);
1148 return 0;
1149 }
1150
1151 TAKE_PTR(n);
1152 return 0;
1153 }
1154
1155 int config_parse_tcp_window(
1156 const char *unit,
1157 const char *filename,
1158 unsigned line,
1159 const char *section,
1160 unsigned section_line,
1161 const char *lvalue,
1162 int ltype,
1163 const char *rvalue,
1164 void *data,
1165 void *userdata) {
1166
1167 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1168 Network *network = userdata;
1169 uint64_t k;
1170 int r;
1171
1172 assert(filename);
1173 assert(section);
1174 assert(lvalue);
1175 assert(rvalue);
1176 assert(data);
1177
1178 r = route_new_static(network, filename, section_line, &n);
1179 if (r < 0)
1180 return r;
1181
1182 r = parse_size(rvalue, 1024, &k);
1183 if (r < 0) {
1184 log_syntax(unit, LOG_ERR, filename, line, r,
1185 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue, rvalue);
1186 return 0;
1187 }
1188 if (k > UINT32_MAX) {
1189 log_syntax(unit, LOG_ERR, filename, line, 0,
1190 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue, rvalue);
1191 return 0;
1192 }
1193
1194 if (streq(lvalue, "InitialCongestionWindow"))
1195 n->initcwnd = k;
1196 else if (streq(lvalue, "InitialAdvertisedReceiveWindow"))
1197 n->initrwnd = k;
1198 else
1199 assert_not_reached("Invalid TCP window type.");
1200
1201 TAKE_PTR(n);
1202 return 0;
1203 }
1204
1205 int config_parse_quickack(
1206 const char *unit,
1207 const char *filename,
1208 unsigned line,
1209 const char *section,
1210 unsigned section_line,
1211 const char *lvalue,
1212 int ltype,
1213 const char *rvalue,
1214 void *data,
1215 void *userdata) {
1216
1217 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1218 Network *network = userdata;
1219 int k, r;
1220
1221 assert(filename);
1222 assert(section);
1223 assert(lvalue);
1224 assert(rvalue);
1225 assert(data);
1226
1227 r = route_new_static(network, filename, section_line, &n);
1228 if (r < 0)
1229 return r;
1230
1231 k = parse_boolean(rvalue);
1232 if (k < 0) {
1233 log_syntax(unit, LOG_ERR, filename, line, k,
1234 "Failed to parse TCP quickack, ignoring: %s", rvalue);
1235 return 0;
1236 }
1237
1238 n->quickack = !!k;
1239 TAKE_PTR(n);
1240 return 0;
1241 }
1242
1243 int config_parse_fast_open_no_cookie(
1244 const char *unit,
1245 const char *filename,
1246 unsigned line,
1247 const char *section,
1248 unsigned section_line,
1249 const char *lvalue,
1250 int ltype,
1251 const char *rvalue,
1252 void *data,
1253 void *userdata) {
1254
1255 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1256 Network *network = userdata;
1257 int k, r;
1258
1259 assert(filename);
1260 assert(section);
1261 assert(lvalue);
1262 assert(rvalue);
1263 assert(data);
1264
1265 r = route_new_static(network, filename, section_line, &n);
1266 if (r < 0)
1267 return r;
1268
1269 k = parse_boolean(rvalue);
1270 if (k < 0) {
1271 log_syntax(unit, LOG_ERR, filename, line, k,
1272 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue);
1273 return 0;
1274 }
1275
1276 n->fast_open_no_cookie = k;
1277 TAKE_PTR(n);
1278 return 0;
1279 }
1280
1281 int config_parse_route_mtu(
1282 const char *unit,
1283 const char *filename,
1284 unsigned line,
1285 const char *section,
1286 unsigned section_line,
1287 const char *lvalue,
1288 int ltype,
1289 const char *rvalue,
1290 void *data,
1291 void *userdata) {
1292
1293 Network *network = userdata;
1294 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1295 int r;
1296
1297 assert(filename);
1298 assert(section);
1299 assert(lvalue);
1300 assert(rvalue);
1301 assert(data);
1302
1303 r = route_new_static(network, filename, section_line, &n);
1304 if (r < 0)
1305 return r;
1306
1307 r = config_parse_mtu(unit, filename, line, section, section_line, lvalue, ltype, rvalue, &n->mtu, userdata);
1308 if (r < 0)
1309 return r;
1310
1311 TAKE_PTR(n);
1312 return 0;
1313 }
1314
1315 int config_parse_route_ttl_propagate(
1316 const char *unit,
1317 const char *filename,
1318 unsigned line,
1319 const char *section,
1320 unsigned section_line,
1321 const char *lvalue,
1322 int ltype,
1323 const char *rvalue,
1324 void *data,
1325 void *userdata) {
1326
1327 Network *network = userdata;
1328 _cleanup_(route_free_or_set_invalidp) Route *n = NULL;
1329 int r, k;
1330
1331 assert(filename);
1332 assert(section);
1333 assert(lvalue);
1334 assert(rvalue);
1335 assert(data);
1336
1337 r = route_new_static(network, filename, section_line, &n);
1338 if (r < 0)
1339 return r;
1340
1341 k = parse_boolean(rvalue);
1342 if (k < 0) {
1343 log_syntax(unit, LOG_ERR, filename, line, k,
1344 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue);
1345 return 0;
1346 }
1347
1348 n->ttl_propagate = k;
1349
1350 TAKE_PTR(n);
1351 return 0;
1352 }
1353
1354 int route_section_verify(Route *route, Network *network) {
1355 if (section_is_invalid(route->section))
1356 return -EINVAL;
1357
1358 if (route->family == AF_UNSPEC) {
1359 assert(route->section);
1360
1361 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
1362 "%s: Route section without Gateway=, Destination=, Source=, "
1363 "or PreferredSource= field configured. "
1364 "Ignoring [Route] section from line %u.",
1365 route->section->filename, route->section->line);
1366 }
1367
1368 if (network->n_static_addresses == 0 &&
1369 in_addr_is_null(route->family, &route->gw) == 0 &&
1370 route->gateway_onlink < 0) {
1371 log_warning("%s: Gateway= without static address configured. "
1372 "Enabling GatewayOnLink= option.",
1373 network->filename);
1374 route->gateway_onlink = true;
1375 }
1376
1377 return 0;
1378 }