Debian Codegraph @

src/network/networkd-route.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/ipv6_route.h>
4 #include <net/if.h>
5 #include <stdio.h>
6
7 #include "sd-ndisc-protocol.h"
8 #include "sd-netlink.h"
9
10 #include "alloc-util.h"
11 #include "conf-parser.h"
12 #include "errno-util.h"
13 #include "event-util.h"
14 #include "netlink-util.h"
15 #include "networkd-address.h"
16 #include "networkd-ipv4ll.h"
17 #include "networkd-link.h"
18 #include "networkd-manager.h"
19 #include "networkd-network.h"
20 #include "networkd-nexthop.h"
21 #include "networkd-queue.h"
22 #include "networkd-route.h"
23 #include "networkd-route-util.h"
24 #include "ordered-set.h"
25 #include "parse-util.h"
26 #include "set.h"
27 #include "siphash24.h"
28 #include "string-util.h"
29 #include "vrf.h"
30 #include "wireguard.h"
31
32 static Route* route_detach_impl(Route *route) {
33 assert(route);
34 assert(!!route->network + !!route->manager + !!route->wireguard <= 1);
35
36 if (route->network) {
37 assert(route->section);
38 hashmap_remove(route->network->routes_by_section, route->section);
39 route->network = NULL;
40 return route;
41 }
42
43 if (route->manager) {
44 route_detach_from_nexthop(route);
45 set_remove(route->manager->routes, route);
46 route->manager = NULL;
47 return route;
48 }
49
50 if (route->wireguard) {
51 set_remove(route->wireguard->routes, route);
52 route->wireguard = NULL;
53 return route;
54 }
55
56 return NULL;
57 }
58
59 void route_detach(Route *route) {
60 route_unref(route_detach_impl(route));
61 }
62
63 static Route* route_free(Route *route) {
64 if (!route)
65 return NULL;
66
67 route_detach_impl(route);
68
69 config_section_free(route->section);
70 route_nexthops_done(route);
71 route_metric_done(&route->metric);
72 sd_event_source_disable_unref(route->expire);
73
74 return mfree(route);
75 }
76
77 DEFINE_TRIVIAL_REF_UNREF_FUNC(Route, route, route_free);
78
79 static void route_hash_func(const Route *route, struct siphash *state) {
80 assert(route);
81
82 siphash24_compress_typesafe(route->family, state);
83
84 switch (route->family) {
85 case AF_INET:
86 /* First, the table, destination prefix, priority, and tos (dscp), are used to find routes.
87 * See fib_table_insert(), fib_find_node(), and fib_find_alias() in net/ipv4/fib_trie.c of the kernel. */
88 siphash24_compress_typesafe(route->table, state);
89 in_addr_hash_func(&route->dst, route->family, state);
90 siphash24_compress_typesafe(route->dst_prefixlen, state);
91 siphash24_compress_typesafe(route->priority, state);
92 siphash24_compress_typesafe(route->tos, state);
93
94 /* Then, protocol, scope, type, flags, prefsrc, metrics (RTAX_* attributes), and nexthops (gateways)
95 * are used to find routes. See fib_find_info() in net/ipv4/fib_semantics.c of the kernel. */
96 siphash24_compress_typesafe(route->protocol, state);
97 siphash24_compress_typesafe(route->scope, state);
98 siphash24_compress_typesafe(route->type, state);
99 unsigned flags = route->flags & ~RTNH_COMPARE_MASK;
100 siphash24_compress_typesafe(flags, state);
101 in_addr_hash_func(&route->prefsrc, route->family, state);
102
103 /* nexthops (id, number of nexthops, nexthop) */
104 route_nexthops_hash_func(route, state);
105
106 /* metrics */
107 route_metric_hash_func(&route->metric, state);
108 break;
109
110 case AF_INET6:
111 /* First, table and destination prefix are used for classifying routes.
112 * See fib6_add() and fib6_add_1() in net/ipv6/ip6_fib.c of the kernel. */
113 siphash24_compress_typesafe(route->table, state);
114 in_addr_hash_func(&route->dst, route->family, state);
115 siphash24_compress_typesafe(route->dst_prefixlen, state);
116
117 /* Then, source prefix is used. See fib6_add(). */
118 in_addr_hash_func(&route->src, route->family, state);
119 siphash24_compress_typesafe(route->src_prefixlen, state);
120
121 /* See fib6_add_rt2node(). */
122 siphash24_compress_typesafe(route->priority, state);
123
124 /* See rt6_duplicate_nexthop() in include/net/ip6_route.h of the kernel.
125 * Here, we hash nexthop in a similar way as the one for IPv4. */
126 route_nexthops_hash_func(route, state);
127
128 /* Unlike IPv4 routes, metrics are not taken into account. */
129 break;
130
131 default:
132 assert_not_reached();
133 }
134 }
135
136 static int route_compare_func(const Route *a, const Route *b) {
137 int r;
138
139 r = CMP(a->family, b->family);
140 if (r != 0)
141 return r;
142
143 switch (a->family) {
144 case AF_INET:
145 r = CMP(a->table, b->table);
146 if (r != 0)
147 return r;
148
149 r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
150 if (r != 0)
151 return r;
152
153 r = CMP(a->dst_prefixlen, b->dst_prefixlen);
154 if (r != 0)
155 return r;
156
157 r = CMP(a->priority, b->priority);
158 if (r != 0)
159 return r;
160
161 r = CMP(a->tos, b->tos);
162 if (r != 0)
163 return r;
164
165 r = CMP(a->protocol, b->protocol);
166 if (r != 0)
167 return r;
168
169 r = CMP(a->scope, b->scope);
170 if (r != 0)
171 return r;
172
173 r = CMP(a->type, b->type);
174 if (r != 0)
175 return r;
176
177 r = CMP(a->flags & ~RTNH_COMPARE_MASK, b->flags & ~RTNH_COMPARE_MASK);
178 if (r != 0)
179 return r;
180
181 r = memcmp(&a->prefsrc, &b->prefsrc, FAMILY_ADDRESS_SIZE(a->family));
182 if (r != 0)
183 return r;
184
185 r = route_nexthops_compare_func(a, b);
186 if (r != 0)
187 return r;
188
189 return route_metric_compare_func(&a->metric, &b->metric);
190
191 case AF_INET6:
192 r = CMP(a->table, b->table);
193 if (r != 0)
194 return r;
195
196 r = memcmp(&a->dst, &b->dst, FAMILY_ADDRESS_SIZE(a->family));
197 if (r != 0)
198 return r;
199
200 r = CMP(a->dst_prefixlen, b->dst_prefixlen);
201 if (r != 0)
202 return r;
203
204 r = memcmp(&a->src, &b->src, FAMILY_ADDRESS_SIZE(a->family));
205 if (r != 0)
206 return r;
207
208 r = CMP(a->src_prefixlen, b->src_prefixlen);
209 if (r != 0)
210 return r;
211
212 r = CMP(a->priority, b->priority);
213 if (r != 0)
214 return r;
215
216 return route_nexthops_compare_func(a, b);
217
218 default:
219 assert_not_reached();
220 }
221 }
222
223 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
224 route_hash_ops,
225 Route,
226 route_hash_func,
227 route_compare_func,
228 route_detach);
229
230 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
231 route_hash_ops_unref,
232 Route,
233 route_hash_func,
234 route_compare_func,
235 route_unref);
236
237 DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
238 route_section_hash_ops,
239 ConfigSection,
240 config_section_hash_func,
241 config_section_compare_func,
242 Route,
243 route_detach);
244
245 int route_new(Route **ret) {
246 _cleanup_(route_unrefp) Route *route = NULL;
247
248 assert(ret);
249
250 route = new(Route, 1);
251 if (!route)
252 return -ENOMEM;
253
254 *route = (Route) {
255 .n_ref = 1,
256 .family = AF_UNSPEC,
257 .scope = RT_SCOPE_UNIVERSE,
258 .protocol = RTPROT_UNSPEC,
259 .type = RTN_UNICAST,
260 .table = RT_TABLE_MAIN,
261 .lifetime_usec = USEC_INFINITY,
262 .gateway_onlink = -1,
263 };
264
265 *ret = TAKE_PTR(route);
266
267 return 0;
268 }
269
270 int route_new_static(Network *network, const char *filename, unsigned section_line, Route **ret) {
271 _cleanup_(config_section_freep) ConfigSection *n = NULL;
272 _cleanup_(route_unrefp) Route *route = NULL;
273 int r;
274
275 assert(network);
276 assert(ret);
277 assert(filename);
278 assert(section_line > 0);
279
280 r = config_section_new(filename, section_line, &n);
281 if (r < 0)
282 return r;
283
284 route = hashmap_get(network->routes_by_section, n);
285 if (route) {
286 *ret = TAKE_PTR(route);
287 return 0;
288 }
289
290 if (hashmap_size(network->routes_by_section) >= routes_max())
291 return -E2BIG;
292
293 r = route_new(&route);
294 if (r < 0)
295 return r;
296
297 route->protocol = RTPROT_STATIC;
298 route->network = network;
299 route->section = TAKE_PTR(n);
300 route->source = NETWORK_CONFIG_SOURCE_STATIC;
301
302 r = hashmap_ensure_put(&network->routes_by_section, &route_section_hash_ops, route->section, route);
303 if (r < 0)
304 return r;
305
306 *ret = TAKE_PTR(route);
307 return 0;
308 }
309
310 static int route_attach(Manager *manager, Route *route) {
311 int r;
312
313 assert(manager);
314 assert(route);
315 assert(!route->network);
316 assert(!route->wireguard);
317
318 r = set_ensure_put(&manager->routes, &route_hash_ops, route);
319 if (r < 0)
320 return r;
321 if (r == 0)
322 return -EEXIST;
323
324 route->manager = manager;
325 return 0;
326 }
327
328 int route_get(Manager *manager, const Route *route, Route **ret) {
329 Route *existing;
330
331 assert(manager);
332 assert(route);
333
334 existing = set_get(manager->routes, route);
335 if (!existing)
336 return -ENOENT;
337
338 if (ret)
339 *ret = existing;
340
341 return 0;
342 }
343
344 static int route_get_link(Manager *manager, const Route *route, Link **ret) {
345 int r;
346
347 assert(manager);
348 assert(route);
349
350 if (route->nexthop_id != 0) {
351 NextHop *nh;
352
353 r = nexthop_get_by_id(manager, route->nexthop_id, &nh);
354 if (r < 0)
355 return r;
356
357 return link_get_by_index(manager, nh->ifindex, ret);
358 }
359
360 return route_nexthop_get_link(manager, &route->nexthop, ret);
361 }
362
363 bool route_is_bound_to_link(const Route *route, Link *link) {
364 assert(route);
365 assert(link);
366 assert(link->manager);
367
368 Link *route_link;
369 if (route_get_link(link->manager, route, &route_link) < 0)
370 return false;
371
372 return route_link->ifindex == link->ifindex;
373 }
374
375 int route_get_request(Manager *manager, const Route *route, Request **ret) {
376 Request *req;
377
378 assert(manager);
379 assert(route);
380
381 req = ordered_set_get(manager->request_queue,
382 &(const Request) {
383 .type = REQUEST_TYPE_ROUTE,
384 .userdata = (void*) route,
385 .hash_func = (hash_func_t) route_hash_func,
386 .compare_func = (compare_func_t) route_compare_func,
387 });
388 if (!req)
389 return -ENOENT;
390
391 if (ret)
392 *ret = req;
393 return 0;
394 }
395
396 int route_dup(const Route *src, const RouteNextHop *nh, Route **ret) {
397 _cleanup_(route_unrefp) Route *dest = NULL;
398 int r;
399
400 assert(src);
401 assert(ret);
402
403 dest = newdup(Route, src, 1);
404 if (!dest)
405 return -ENOMEM;
406
407 /* Unset number of reference and all pointers */
408 dest->n_ref = 1;
409 dest->manager = NULL;
410 dest->network = NULL;
411 dest->wireguard = NULL;
412 dest->section = NULL;
413 dest->nexthop = ROUTE_NEXTHOP_NULL;
414 dest->nexthops = NULL;
415 dest->metric = ROUTE_METRIC_NULL;
416 dest->expire = NULL;
417
418 r = route_nexthops_copy(src, nh, dest);
419 if (r < 0)
420 return r;
421
422 r = route_metric_copy(&src->metric, &dest->metric);
423 if (r < 0)
424 return r;
425
426 *ret = TAKE_PTR(dest);
427 return 0;
428 }
429
430 static int route_to_string(const Route *route, Manager *manager, char **ret) {
431 _cleanup_free_ char *nexthop = NULL, *prefsrc = NULL,
432 *table = NULL, *scope = NULL, *proto = NULL, *flags = NULL;
433 const char *dst, *src;
434
435 assert(route);
436 assert(manager);
437 assert(ret);
438
439 dst = in_addr_is_set(route->family, &route->dst) || route->dst_prefixlen > 0 ?
440 IN_ADDR_PREFIX_TO_STRING(route->family, &route->dst, route->dst_prefixlen) : NULL;
441 src = in_addr_is_set(route->family, &route->src) || route->src_prefixlen > 0 ?
442 IN_ADDR_PREFIX_TO_STRING(route->family, &route->src, route->src_prefixlen) : NULL;
443
444 (void) route_nexthops_to_string(route, &nexthop);
445
446 if (in_addr_is_set(route->family, &route->prefsrc))
447 (void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
448 (void) route_scope_to_string_alloc(route->scope, &scope);
449 (void) manager_get_route_table_to_string(manager, route->table, /* append_num= */ true, &table);
450 (void) route_protocol_full_to_string_alloc(route->protocol, &proto);
451 (void) route_flags_to_string_alloc(route->flags, &flags);
452
453 if (asprintf(ret,
454 "dst: %s, src: %s, %s, prefsrc: %s, "
455 "table: %s, priority: %"PRIu32", "
456 "proto: %s, scope: %s, "
457 "type: %s, flags: %s",
458 strna(dst), strna(src), strna(nexthop), strna(prefsrc),
459 strna(table), route->priority,
460 strna(proto), strna(scope),
461 strna(route_type_to_string(route->type)), strna(flags)) < 0)
462 return -ENOMEM;
463
464 return 0;
465 }
466
467 void log_route_debug(const Route *route, const char *str, Manager *manager) {
468 _cleanup_free_ char *state = NULL, *route_str = NULL;
469 Link *link = NULL;
470
471 assert(route);
472 assert(str);
473 assert(manager);
474
475 if (!DEBUG_LOGGING)
476 return;
477
478 (void) route_get_link(manager, route, &link);
479 (void) network_config_state_to_string_alloc(route->state, &state);
480 (void) route_to_string(route, manager, &route_str);
481
482 log_link_debug(link, "%s %s route (%s): %s",
483 str, strna(network_config_source_to_string(route->source)), strna(state), strna(route_str));
484 }
485
486 static void route_forget(Manager *manager, Route *route, const char *msg) {
487 assert(manager);
488 assert(route);
489 assert(msg);
490
491 Request *req;
492 if (route_get_request(manager, route, &req) >= 0)
493 route_enter_removed(req->userdata);
494
495 if (!route->manager && route_get(manager, route, &route) < 0)
496 return;
497
498 route_enter_removed(route);
499 log_route_debug(route, msg, manager);
500 route_detach(route);
501 }
502
503 static int route_set_netlink_message(const Route *route, sd_netlink_message *m) {
504 int r;
505
506 assert(route);
507 assert(m);
508
509 /* rtmsg header (and relevant attributes) */
510 if (route->dst_prefixlen > 0) {
511 r = netlink_message_append_in_addr_union(m, RTA_DST, route->family, &route->dst);
512 if (r < 0)
513 return r;
514
515 r = sd_rtnl_message_route_set_dst_prefixlen(m, route->dst_prefixlen);
516 if (r < 0)
517 return r;
518 }
519
520 if (route->src_prefixlen > 0) {
521 r = netlink_message_append_in_addr_union(m, RTA_SRC, route->family, &route->src);
522 if (r < 0)
523 return r;
524
525 r = sd_rtnl_message_route_set_src_prefixlen(m, route->src_prefixlen);
526 if (r < 0)
527 return r;
528 }
529
530 r = sd_rtnl_message_route_set_tos(m, route->tos);
531 if (r < 0)
532 return r;
533
534 r = sd_rtnl_message_route_set_scope(m, route->scope);
535 if (r < 0)
536 return r;
537
538 r = sd_rtnl_message_route_set_type(m, route->type);
539 if (r < 0)
540 return r;
541
542 r = sd_rtnl_message_route_set_flags(m, route->flags & ~RTNH_COMPARE_MASK);
543 if (r < 0)
544 return r;
545
546 /* attributes */
547 r = sd_netlink_message_append_u32(m, RTA_PRIORITY, route->priority);
548 if (r < 0)
549 return r;
550
551 if (in_addr_is_set(route->family, &route->prefsrc)) {
552 r = netlink_message_append_in_addr_union(m, RTA_PREFSRC, route->family, &route->prefsrc);
553 if (r < 0)
554 return r;
555 }
556
557 if (route->table < 256) {
558 r = sd_rtnl_message_route_set_table(m, route->table);
559 if (r < 0)
560 return r;
561 } else {
562 r = sd_rtnl_message_route_set_table(m, RT_TABLE_UNSPEC);
563 if (r < 0)
564 return r;
565
566 /* Table attribute to allow more than 256. */
567 r = sd_netlink_message_append_u32(m, RTA_TABLE, route->table);
568 if (r < 0)
569 return r;
570 }
571
572 r = sd_netlink_message_append_u8(m, RTA_PREF, route->pref);
573 if (r < 0)
574 return r;
575
576 /* nexthops */
577 r = route_nexthops_set_netlink_message(route, m);
578 if (r < 0)
579 return r;
580
581 /* metrics */
582 r = route_metric_set_netlink_message(&route->metric, m);
583 if (r < 0)
584 return r;
585
586 return 0;
587 }
588
589 static int route_remove_handler(sd_netlink *rtnl, sd_netlink_message *m, RemoveRequest *rreq) {
590 int r;
591
592 assert(m);
593 assert(rreq);
594
595 Manager *manager = ASSERT_PTR(rreq->manager);
596 Route *route = ASSERT_PTR(rreq->userdata);
597
598 r = sd_netlink_message_get_errno(m);
599 if (r < 0) {
600 log_message_full_errno(m,
601 (r == -ESRCH || /* the route is already removed? */
602 (r == -EINVAL && route->nexthop_id != 0) || /* The nexthop is already removed? */
603 !route->manager) ? /* already detached? */
604 LOG_DEBUG : LOG_WARNING,
605 r, "Could not drop route, ignoring");
606
607 /* If the route cannot be removed, then assume the route is already removed. */
608 route_forget(manager, route, "Forgetting");
609 }
610
611 return 1;
612 }
613
614 int route_remove(Route *route, Manager *manager) {
615 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
616 Link *link = NULL;
617 int r;
618
619 assert(route);
620 assert(manager);
621
622 if (manager->state == MANAGER_STOPPED)
623 return 0; /* The remove request will not be queued anyway. Suppress logging below. */
624
625 /* If the route is remembered, then use the remembered object. */
626 (void) route_get(manager, route, &route);
627
628 log_route_debug(route, "Removing", manager);
629
630 /* For logging. */
631 (void) route_get_link(manager, route, &link);
632
633 r = sd_rtnl_message_new_route(manager->rtnl, &m, RTM_DELROUTE, route->family, route->protocol);
634 if (r < 0)
635 return log_link_warning_errno(link, r, "Could not create netlink message: %m");
636
637 r = route_set_netlink_message(route, m);
638 if (r < 0)
639 return log_link_warning_errno(link, r, "Could not fill netlink message: %m");
640
641 r = manager_remove_request_add(manager, route, route, manager->rtnl, m, route_remove_handler);
642 if (r < 0)
643 return log_link_warning_errno(link, r, "Could not queue rtnetlink message: %m");
644
645 route_enter_removing(route);
646 return 0;
647 }
648
649 int route_remove_and_cancel(Route *route, Manager *manager) {
650 _cleanup_(request_unrefp) Request *req = NULL;
651 bool waiting = false;
652
653 assert(route);
654 assert(manager);
655
656 /* If the route is remembered by the manager, then use the remembered object. */
657 (void) route_get(manager, route, &route);
658
659 /* Cancel the request for the route. If the request is already called but we have not received the
660 * notification about the request, then explicitly remove the route. */
661 if (route_get_request(manager, route, &req) >= 0) {
662 request_ref(req); /* avoid the request freed by request_detach() */
663 waiting = req->waiting_reply;
664 request_detach(req);
665 route_cancel_requesting(route);
666 }
667
668 /* If we know that the route will come or already exists, remove it. */
669 if (waiting || (route->manager && route_exists(route)))
670 return route_remove(route, manager);
671
672 return 0;
673 }
674
675 static int route_expire_handler(sd_event_source *s, uint64_t usec, void *userdata) {
676 Route *route = ASSERT_PTR(userdata);
677 int r;
678
679 if (!route->manager)
680 return 0; /* already detached. */
681
682 r = route_remove(route, route->manager);
683 if (r < 0) {
684 Link *link = NULL;
685 (void) route_get_link(route->manager, route, &link);
686 log_link_warning_errno(link, r, "Could not remove route: %m");
687 if (link)
688 link_enter_failed(link);
689 }
690
691 return 1;
692 }
693
694 static int route_setup_timer(Route *route, const struct rta_cacheinfo *cacheinfo) {
695 int r;
696
697 assert(route);
698
699 if (cacheinfo && cacheinfo->rta_expires != 0)
700 route->expiration_managed_by_kernel = true;
701
702 if (route->lifetime_usec == USEC_INFINITY || /* We do not request expiration for the route. */
703 route->expiration_managed_by_kernel) { /* We have received nonzero expiration previously. The expiration is managed by the kernel. */
704 route->expire = sd_event_source_disable_unref(route->expire);
705 return 0;
706 }
707
708 Manager *manager = ASSERT_PTR(route->manager);
709 r = event_reset_time(manager->event, &route->expire, CLOCK_BOOTTIME,
710 route->lifetime_usec, 0, route_expire_handler, route, 0, "route-expiration", true);
711 if (r < 0) {
712 Link *link = NULL;
713 (void) route_get_link(manager, route, &link);
714 return log_link_warning_errno(link, r, "Failed to configure expiration timer for route, ignoring: %m");
715 }
716
717 log_route_debug(route, "Configured expiration timer for", manager);
718 return 1;
719 }
720
721 static int route_update_by_request(Route *route, Request *req) {
722 assert(route);
723 assert(req);
724
725 Route *rt = ASSERT_PTR(req->userdata);
726
727 route->provider = rt->provider;
728 route->source = rt->source;
729 route->lifetime_usec = rt->lifetime_usec;
730
731 return 0;
732 }
733
734 static int route_update_on_existing_one(Request *req, Route *requested) {
735 Manager *manager = ASSERT_PTR(ASSERT_PTR(req)->manager);
736 Route *existing;
737 int r;
738
739 assert(requested);
740
741 if (route_get(manager, requested, &existing) < 0)
742 return 0;
743
744 r = route_update_by_request(existing, req);
745 if (r < 0)
746 return r;
747
748 r = route_setup_timer(existing, NULL);
749 if (r < 0)
750 return r;
751
752 /* This may be a bug in the kernel, but the MTU of an IPv6 route can be updated only when the
753 * route has an expiration timer managed by the kernel (not by us). See fib6_add_rt2node() in
754 * net/ipv6/ip6_fib.c of the kernel. */
755 if (existing->family == AF_INET6 &&
756 existing->expiration_managed_by_kernel) {
757 r = route_metric_set(&existing->metric, RTAX_MTU, route_metric_get(&requested->metric, RTAX_MTU));
758 if (r < 0)
759 return r;
760 }
761
762 route_enter_configured(existing);
763 return 0;
764 }
765
766 static int route_update_on_existing(Request *req) {
767 Route *rt = ASSERT_PTR(ASSERT_PTR(req)->userdata);
768 int r;
769
770 if (!req->manager)
771 /* Already detached? At least there are two possibilities then.
772 * 1) The interface is removed, and all queued requests for the interface are cancelled.
773 * 2) networkd is now stopping, hence all queued requests are cancelled.
774 * Anyway, we can ignore the request, and there is nothing we can do. */
775 return 0;
776
777 if (rt->family == AF_INET || ordered_set_isempty(rt->nexthops))
778 return route_update_on_existing_one(req, rt);
779
780 RouteNextHop *nh;
781 ORDERED_SET_FOREACH(nh, rt->nexthops) {
782 _cleanup_(route_unrefp) Route *dup = NULL;
783
784 r = route_dup(rt, nh, &dup);
785 if (r < 0)
786 return r;
787
788 r = route_update_on_existing_one(req, dup);
789 if (r < 0)
790 return r;
791 }
792
793 return 0;
794 }
795
796 int route_configure_handler_internal(sd_netlink_message *m, Request *req, Route *route) {
797 int r;
798
799 assert(m);
800 assert(req);
801 assert(route);
802
803 Link *link = ASSERT_PTR(req->link);
804
805 r = sd_netlink_message_get_errno(m);
806 if (r == -EEXIST) {
807 /* When re-configuring an existing route, kernel does not send RTM_NEWROUTE notification, so
808 * here we need to update the state, provider, source, timer, and so on. */
809 r = route_update_on_existing(req);
810 if (r < 0) {
811 log_link_warning_errno(link, r, "Failed to update existing route: %m");
812 link_enter_failed(link);
813 return 0;
814 }
815
816 return 1;
817 }
818 if (r < 0) {
819 _cleanup_free_ char *str = NULL;
820 (void) route_to_string(route, link->manager, &str);
821 log_link_message_warning_errno(link, m, r, "Failed to configure %s route (%s)",
822 network_config_source_to_string(route->source), strna(str));
823 link_enter_failed(link);
824 return 0;
825 }
826
827 return 1;
828 }
829
830 static int route_configure(const Route *route, uint32_t lifetime_sec, Link *link, Request *req) {
831 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
832 int r;
833
834 assert(route);
835 assert(link);
836 assert(link->manager);
837 assert(req);
838
839 log_route_debug(route, "Configuring", link->manager);
840
841 r = sd_rtnl_message_new_route(link->manager->rtnl, &m, RTM_NEWROUTE, route->family, route->protocol);
842 if (r < 0)
843 return r;
844
845 r = route_set_netlink_message(route, m);
846 if (r < 0)
847 return r;
848
849 if (lifetime_sec != UINT32_MAX) {
850 r = sd_netlink_message_append_u32(m, RTA_EXPIRES, lifetime_sec);
851 if (r < 0)
852 return r;
853 }
854
855 return request_call_netlink_async(link->manager->rtnl, m, req);
856 }
857
858 static int route_requeue_request(Request *req, Link *link, const Route *route) {
859 _unused_ _cleanup_(request_unrefp) Request *req_unref = NULL;
860 _cleanup_(route_unrefp) Route *tmp = NULL;
861 int r;
862
863 assert(req);
864 assert(link);
865 assert(link->manager);
866 assert(route);
867
868 /* It is not possible to adjust the Route object owned by Request, as it is used as a key to manage
869 * Request objects in the queue. Hence, we need to re-request with the updated Route object. */
870
871 if (!route_nexthops_needs_adjust(route))
872 return 0; /* The Route object does not need the adjustment. Continue with it. */
873
874 r = route_dup(route, NULL, &tmp);
875 if (r < 0)
876 return r;
877
878 r = route_adjust_nexthops(tmp, link);
879 if (r <= 0)
880 return r;
881
882 if (route_compare_func(route, tmp) == 0 && route->type == tmp->type)
883 return 0; /* No effective change?? That's OK. */
884
885 /* Avoid the request to be freed by request_detach(). */
886 req_unref = request_ref(req);
887
888 /* Detach the request from the queue, to make not the new request is deduped.
889 * Why this is necessary? IPv6 routes with different type may be handled as the same,
890 * As commented in route_adjust_nexthops(), we need to configure the adjusted type,
891 * otherwise we cannot remove the route on reconfigure or so. If we request the new Route object
892 * without detaching the current request, the new request is deduped, and the route is configured
893 * with unmodified type. */
894 request_detach(req);
895
896 /* Request the route with the adjusted Route object combined with the same other parameters. */
897 r = link_requeue_request(link, req, tmp, NULL);
898 if (r < 0)
899 return r;
900 if (r == 0)
901 return 1; /* Already queued?? That's OK. Maybe, [Route] section is effectively duplicated. */
902
903 TAKE_PTR(tmp);
904 return 1; /* New request is queued. Finish to process this request. */
905 }
906
907 static int route_is_ready_to_configure(const Route *route, Link *link) {
908 assert(route);
909 assert(link);
910
911 if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false))
912 return false;
913
914 if (in_addr_is_set(route->family, &route->prefsrc) > 0) {
915 Address *a;
916
917 if (manager_get_address(link->manager, route->family, &route->prefsrc, &a) < 0)
918 return false;
919
920 if (!address_is_ready(a))
921 return false;
922 }
923
924 return route_nexthops_is_ready_to_configure(route, link->manager);
925 }
926
927 static int route_process_request(Request *req, Link *link, Route *route) {
928 Route *existing;
929 int r;
930
931 assert(req);
932 assert(link);
933 assert(link->manager);
934 assert(route);
935
936 r = route_is_ready_to_configure(route, link);
937 if (r < 0)
938 return log_link_warning_errno(link, r, "Failed to check if route is ready to configure: %m");
939 if (r == 0)
940 return 0;
941
942 usec_t now_usec;
943 assert_se(sd_event_now(link->manager->event, CLOCK_BOOTTIME, &now_usec) >= 0);
944 uint32_t sec = usec_to_sec(route->lifetime_usec, now_usec);
945 if (sec == 0) {
946 log_link_debug(link, "Refuse to configure %s route with zero lifetime.",
947 network_config_source_to_string(route->source));
948
949 route_cancel_requesting(route);
950 if (route_get(link->manager, route, &existing) >= 0)
951 route_cancel_requesting(existing);
952 return 1;
953 }
954
955 r = route_requeue_request(req, link, route);
956 if (r != 0)
957 return r;
958
959 r = route_configure(route, sec, link, req);
960 if (r < 0)
961 return log_link_warning_errno(link, r, "Failed to configure route: %m");
962
963 route_enter_configuring(route);
964 if (route_get(link->manager, route, &existing) >= 0)
965 route_enter_configuring(existing);
966 return 1;
967 }
968
969 static int link_request_route_one(
970 Link *link,
971 const Route *route,
972 const RouteNextHop *nh,
973 unsigned *message_counter,
974 route_netlink_handler_t netlink_handler) {
975
976 _cleanup_(route_unrefp) Route *tmp = NULL;
977 Route *existing = NULL;
978 int r;
979
980 assert(link);
981 assert(link->manager);
982 assert(route);
983
984 r = route_dup(route, nh, &tmp);
985 if (r < 0)
986 return r;
987
988 r = route_adjust_nexthops(tmp, link);
989 if (r < 0)
990 return r;
991
992 if (route_get(link->manager, tmp, &existing) >= 0)
993 /* Copy state for logging below. */
994 tmp->state = existing->state;
995
996 log_route_debug(tmp, "Requesting", link->manager);
997 r = link_queue_request_safe(link, REQUEST_TYPE_ROUTE,
998 tmp,
999 route_unref,
1000 route_hash_func,
1001 route_compare_func,
1002 route_process_request,
1003 message_counter,
1004 netlink_handler,
1005 NULL);
1006 if (r <= 0)
1007 return r;
1008
1009 route_enter_requesting(tmp);
1010 if (existing)
1011 route_enter_requesting(existing);
1012
1013 TAKE_PTR(tmp);
1014 return 1;
1015 }
1016
1017 int link_request_route(
1018 Link *link,
1019 const Route *route,
1020 unsigned *message_counter,
1021 route_netlink_handler_t netlink_handler) {
1022
1023 int r;
1024
1025 assert(link);
1026 assert(link->manager);
1027 assert(route);
1028 assert(route->source != NETWORK_CONFIG_SOURCE_FOREIGN);
1029
1030 if (route->family == AF_INET || route_is_reject(route) || ordered_set_isempty(route->nexthops))
1031 return link_request_route_one(link, route, NULL, message_counter, netlink_handler);
1032
1033 RouteNextHop *nh;
1034 ORDERED_SET_FOREACH(nh, route->nexthops) {
1035 r = link_request_route_one(link, route, nh, message_counter, netlink_handler);
1036 if (r < 0)
1037 return r;
1038 }
1039
1040 return 0;
1041 }
1042
1043 static int static_route_handler(sd_netlink *rtnl, sd_netlink_message *m, Request *req, Link *link, Route *route) {
1044 int r;
1045
1046 assert(req);
1047 assert(link);
1048 assert(route);
1049
1050 r = route_configure_handler_internal(m, req, route);
1051 if (r <= 0)
1052 return r;
1053
1054 if (link->static_route_messages == 0) {
1055 log_link_debug(link, "Routes set");
1056 link->static_routes_configured = true;
1057 link_check_ready(link);
1058 }
1059
1060 return 1;
1061 }
1062
1063 static int link_request_wireguard_routes(Link *link, bool only_ipv4) {
1064 Route *route;
1065 int r;
1066
1067 assert(link);
1068
1069 if (!link->netdev || link->netdev->kind != NETDEV_KIND_WIREGUARD)
1070 return 0;
1071
1072 Wireguard *w = WIREGUARD(link->netdev);
1073
1074 SET_FOREACH(route, w->routes) {
1075 if (only_ipv4 && route->family != AF_INET)
1076 continue;
1077
1078 r = link_request_route(link, route, &link->static_route_messages, static_route_handler);
1079 if (r < 0)
1080 return r;
1081 }
1082
1083 return 0;
1084 }
1085
1086 int link_request_static_routes(Link *link, bool only_ipv4) {
1087 Route *route;
1088 int r;
1089
1090 assert(link);
1091 assert(link->network);
1092
1093 link->static_routes_configured = false;
1094
1095 HASHMAP_FOREACH(route, link->network->routes_by_section) {
1096 if (route->source != NETWORK_CONFIG_SOURCE_STATIC)
1097 continue;
1098
1099 if (only_ipv4 && route->family != AF_INET)
1100 continue;
1101
1102 r = link_request_route(link, route, &link->static_route_messages, static_route_handler);
1103 if (r < 0)
1104 return r;
1105 }
1106
1107 r = link_request_wireguard_routes(link, only_ipv4);
1108 if (r < 0)
1109 return r;
1110
1111 if (link->static_route_messages == 0) {
1112 link->static_routes_configured = true;
1113 link_check_ready(link);
1114 } else {
1115 log_link_debug(link, "Requesting routes");
1116 link_set_state(link, LINK_STATE_CONFIGURING);
1117 }
1118
1119 return 0;
1120 }
1121
1122 static int process_route_one(
1123 Manager *manager,
1124 uint16_t type,
1125 Route *tmp,
1126 const struct rta_cacheinfo *cacheinfo) {
1127
1128 Route *route = NULL;
1129 Link *link = NULL;
1130 bool is_new = false, update_dhcp4;
1131 int r;
1132
1133 assert(manager);
1134 assert(tmp);
1135 assert(IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE));
1136
1137 (void) route_get(manager, tmp, &route);
1138 (void) route_get_link(manager, tmp, &link);
1139
1140 update_dhcp4 = link && tmp->family == AF_INET6 && tmp->dst_prefixlen == 0;
1141
1142 switch (type) {
1143 case RTM_NEWROUTE: {
1144 Request *req = NULL;
1145 (void) route_get_request(manager, tmp, &req);
1146
1147 if (!route) {
1148 if (!manager->manage_foreign_routes && !(req && req->waiting_reply)) {
1149 route_enter_configured(tmp);
1150 log_route_debug(tmp, "Ignoring received", manager);
1151 return 0;
1152 }
1153
1154 /* If we do not know the route, then save it. */
1155 r = route_attach(manager, tmp);
1156 if (r < 0) {
1157 log_link_warning_errno(link, r, "Failed to remember foreign route, ignoring: %m");
1158 return 0;
1159 }
1160
1161 route = route_ref(tmp);
1162 is_new = true;
1163
1164 } else {
1165 /* Update remembered route with the received notification. */
1166
1167 /* Here, update weight only when a non-zero weight is received. As the kernel does
1168 * not provide the weight of a single-path route. In such case, tmp->nexthop.weight
1169 * is zero, hence we should not overwrite the known weight of the route. */
1170 if (tmp->nexthop.weight != 0)
1171 route->nexthop.weight = tmp->nexthop.weight;
1172 }
1173
1174 /* Also update information that cannot be obtained through netlink notification. */
1175 if (req && req->waiting_reply) {
1176 r = route_update_by_request(route, req);
1177 if (r < 0) {
1178 log_link_warning_errno(link, r, "Failed to update route by request: %m");
1179 link_enter_failed(link);
1180 return 0;
1181 }
1182
1183 /* We configure IPv6 multipath route separately. When the first path is configured,
1184 * the kernel does not provide the weight of the path. So, we need to adjust it here.
1185 * Hopefully, the weight is assigned correctly. */
1186 if (route->nexthop.weight == 0) {
1187 Route *rt = ASSERT_PTR(req->userdata);
1188 route->nexthop.weight = rt->nexthop.weight;
1189 }
1190 }
1191
1192 route_attach_to_nexthop(route);
1193
1194 route_enter_configured(route);
1195 log_route_debug(route, is_new ? "Received new" : "Received remembered", manager);
1196
1197 (void) route_setup_timer(route, cacheinfo);
1198
1199 break;
1200 }
1201 case RTM_DELROUTE:
1202 if (route)
1203 route_forget(manager, route, "Forgetting removed");
1204 else
1205 log_route_debug(tmp,
1206 manager->manage_foreign_routes ? "Kernel removed unknown" : "Ignoring removed",
1207 manager);
1208 break;
1209
1210 default:
1211 assert_not_reached();
1212 }
1213
1214 if (update_dhcp4) {
1215 r = dhcp4_update_ipv6_connectivity(link);
1216 if (r < 0) {
1217 log_link_warning_errno(link, r, "Failed to notify IPv6 connectivity to DHCPv4 client: %m");
1218 link_enter_failed(link);
1219 }
1220 }
1221
1222 return 1;
1223 }
1224
1225 int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
1226 _cleanup_(route_unrefp) Route *tmp = NULL;
1227 int r;
1228
1229 assert(rtnl);
1230 assert(message);
1231 assert(m);
1232
1233 if (sd_netlink_message_is_error(message)) {
1234 r = sd_netlink_message_get_errno(message);
1235 if (r < 0)
1236 log_message_warning_errno(message, r, "rtnl: failed to receive route message, ignoring");
1237
1238 return 0;
1239 }
1240
1241 uint16_t type;
1242 r = sd_netlink_message_get_type(message, &type);
1243 if (r < 0) {
1244 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
1245 return 0;
1246 } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) {
1247 log_warning("rtnl: received unexpected message type %u when processing route, ignoring.", type);
1248 return 0;
1249 }
1250
1251 r = route_new(&tmp);
1252 if (r < 0)
1253 return log_oom();
1254
1255 /* rtmsg header */
1256 r = sd_rtnl_message_route_get_family(message, &tmp->family);
1257 if (r < 0) {
1258 log_warning_errno(r, "rtnl: received route message without family, ignoring: %m");
1259 return 0;
1260 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
1261 log_debug("rtnl: received route message with invalid family '%i', ignoring.", tmp->family);
1262 return 0;
1263 }
1264
1265 r = sd_rtnl_message_route_get_dst_prefixlen(message, &tmp->dst_prefixlen);
1266 if (r < 0) {
1267 log_warning_errno(r, "rtnl: received route message with invalid destination prefixlen, ignoring: %m");
1268 return 0;
1269 }
1270
1271 r = sd_rtnl_message_route_get_src_prefixlen(message, &tmp->src_prefixlen);
1272 if (r < 0) {
1273 log_warning_errno(r, "rtnl: received route message with invalid source prefixlen, ignoring: %m");
1274 return 0;
1275 }
1276
1277 r = sd_rtnl_message_route_get_tos(message, &tmp->tos);
1278 if (r < 0) {
1279 log_warning_errno(r, "rtnl: received route message with invalid tos, ignoring: %m");
1280 return 0;
1281 }
1282
1283 r = sd_rtnl_message_route_get_protocol(message, &tmp->protocol);
1284 if (r < 0) {
1285 log_warning_errno(r, "rtnl: received route message without route protocol, ignoring: %m");
1286 return 0;
1287 }
1288
1289 r = sd_rtnl_message_route_get_scope(message, &tmp->scope);
1290 if (r < 0) {
1291 log_warning_errno(r, "rtnl: received route message with invalid scope, ignoring: %m");
1292 return 0;
1293 }
1294
1295 r = sd_rtnl_message_route_get_type(message, &tmp->type);
1296 if (r < 0) {
1297 log_warning_errno(r, "rtnl: received route message with invalid type, ignoring: %m");
1298 return 0;
1299 }
1300
1301 r = sd_rtnl_message_route_get_flags(message, &tmp->flags);
1302 if (r < 0) {
1303 log_warning_errno(r, "rtnl: received route message without route flags, ignoring: %m");
1304 return 0;
1305 }
1306
1307 /* attributes */
1308 r = netlink_message_read_in_addr_union(message, RTA_DST, tmp->family, &tmp->dst);
1309 if (r < 0 && r != -ENODATA) {
1310 log_warning_errno(r, "rtnl: received route message without valid destination, ignoring: %m");
1311 return 0;
1312 }
1313
1314 r = netlink_message_read_in_addr_union(message, RTA_SRC, tmp->family, &tmp->src);
1315 if (r < 0 && r != -ENODATA) {
1316 log_warning_errno(r, "rtnl: received route message without valid source, ignoring: %m");
1317 return 0;
1318 }
1319
1320 r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &tmp->priority);
1321 if (r < 0 && r != -ENODATA) {
1322 log_warning_errno(r, "rtnl: received route message with invalid priority, ignoring: %m");
1323 return 0;
1324 }
1325
1326 r = netlink_message_read_in_addr_union(message, RTA_PREFSRC, tmp->family, &tmp->prefsrc);
1327 if (r < 0 && r != -ENODATA) {
1328 log_warning_errno(r, "rtnl: received route message without valid preferred source, ignoring: %m");
1329 return 0;
1330 }
1331
1332 r = sd_netlink_message_read_u32(message, RTA_TABLE, &tmp->table);
1333 if (r == -ENODATA) {
1334 unsigned char table;
1335
1336 r = sd_rtnl_message_route_get_table(message, &table);
1337 if (r >= 0)
1338 tmp->table = table;
1339 }
1340 if (r < 0) {
1341 log_warning_errno(r, "rtnl: received route message with invalid table, ignoring: %m");
1342 return 0;
1343 }
1344
1345 r = sd_netlink_message_read_u8(message, RTA_PREF, &tmp->pref);
1346 if (r < 0 && r != -ENODATA) {
1347 log_warning_errno(r, "rtnl: received route message with invalid preference, ignoring: %m");
1348 return 0;
1349 }
1350
1351 /* nexthops */
1352 if (route_nexthops_read_netlink_message(tmp, message) < 0)
1353 return 0;
1354
1355 /* metrics */
1356 if (route_metric_read_netlink_message(&tmp->metric, message) < 0)
1357 return 0;
1358
1359 bool has_cacheinfo;
1360 struct rta_cacheinfo cacheinfo;
1361 r = sd_netlink_message_read(message, RTA_CACHEINFO, sizeof(cacheinfo), &cacheinfo);
1362 if (r < 0 && r != -ENODATA) {
1363 log_warning_errno(r, "rtnl: failed to read RTA_CACHEINFO attribute, ignoring: %m");
1364 return 0;
1365 }
1366 has_cacheinfo = r >= 0;
1367
1368 if (tmp->family == AF_INET || ordered_set_isempty(tmp->nexthops))
1369 return process_route_one(m, type, tmp, has_cacheinfo ? &cacheinfo : NULL);
1370
1371 RouteNextHop *nh;
1372 ORDERED_SET_FOREACH(nh, tmp->nexthops) {
1373 _cleanup_(route_unrefp) Route *dup = NULL;
1374
1375 r = route_dup(tmp, nh, &dup);
1376 if (r < 0)
1377 return log_oom();
1378
1379 r = process_route_one(m, type, dup, has_cacheinfo ? &cacheinfo : NULL);
1380 if (r < 0)
1381 return r;
1382 }
1383
1384 return 1;
1385 }
1386
1387 void manager_mark_routes(Manager *manager, Link *link, NetworkConfigSource source) {
1388 Route *route;
1389
1390 assert(manager);
1391
1392 SET_FOREACH(route, manager->routes) {
1393 if (route->source != source)
1394 continue;
1395
1396 if (link) {
1397 Link *route_link;
1398
1399 if (route_get_link(manager, route, &route_link) < 0)
1400 continue;
1401 if (route_link != link)
1402 continue;
1403 }
1404
1405 route_mark(route);
1406 }
1407 }
1408
1409 static bool route_by_kernel(const Route *route) {
1410 assert(route);
1411
1412 if (route->protocol == RTPROT_KERNEL)
1413 return true;
1414
1415 /* The kernels older than a826b04303a40d52439aa141035fca5654ccaccd (v5.11) create the IPv6
1416 * multicast with RTPROT_BOOT. Do not touch it. */
1417 if (route->protocol == RTPROT_BOOT &&
1418 route->family == AF_INET6 &&
1419 route->dst_prefixlen == 8 &&
1420 in6_addr_equal(&route->dst.in6, & (struct in6_addr) {{{ 0xff,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }}}))
1421 return true;
1422
1423 return false;
1424 }
1425
1426 bool route_can_update(Manager *manager, const Route *existing, const Route *requesting) {
1427 int r;
1428
1429 assert(manager);
1430 assert(existing);
1431 assert(requesting);
1432
1433 if (route_compare_func(existing, requesting) != 0) {
1434 log_route_debug(existing, "Cannot update route, as the existing route is different", manager);
1435 return false;
1436 }
1437
1438 switch (existing->family) {
1439 case AF_INET:
1440 if (existing->nexthop.weight != requesting->nexthop.weight) {
1441 log_debug("Cannot update route: existing weight: %u, requesting weight: %u",
1442 existing->nexthop.weight, requesting->nexthop.weight);
1443 return false;
1444 }
1445 return true;
1446
1447 case AF_INET6:
1448 if (existing->protocol != requesting->protocol) {
1449 if (DEBUG_LOGGING) {
1450 _cleanup_free_ char *ex = NULL, *req = NULL;
1451
1452 r = route_protocol_to_string_alloc(existing->protocol, &ex);
1453 if (r < 0)
1454 return false;
1455
1456 r = route_protocol_to_string_alloc(requesting->protocol, &req);
1457 if (r < 0)
1458 return false;
1459
1460 log_debug("Cannot update route: existing protocol: %s, requesting protocol: %s", ex, req);
1461 }
1462
1463 return false;
1464 }
1465 if (existing->type != requesting->type) {
1466 log_debug("Cannot update route: existing type: %s, requesting type: %s",
1467 route_type_to_string(existing->type),
1468 route_type_to_string(requesting->type));
1469
1470 return false;
1471 }
1472 if ((existing->flags & ~RTNH_COMPARE_MASK) != (requesting->flags & ~RTNH_COMPARE_MASK)) {
1473 if (DEBUG_LOGGING) {
1474 _cleanup_free_ char *ex = NULL, *req = NULL;
1475
1476 r = route_flags_to_string_alloc(existing->flags, &ex);
1477 if (r < 0)
1478 return false;
1479
1480 r = route_flags_to_string_alloc(requesting->flags, &req);
1481 if (r < 0)
1482 return false;
1483
1484 log_debug("Cannot update route: existing flags: %s, requesting flags: %s", ex, req);
1485 }
1486
1487 return false;
1488 }
1489 if (!in6_addr_equal(&existing->prefsrc.in6, &requesting->prefsrc.in6)) {
1490 log_debug("Cannot update route: existing preferred source: %s, requesting preferred source: %s",
1491 IN6_ADDR_TO_STRING(&existing->prefsrc.in6),
1492 IN6_ADDR_TO_STRING(&requesting->prefsrc.in6));
1493 return false;
1494 }
1495 if (existing->pref != requesting->pref) {
1496 log_debug("Cannot update route: existing preference: %u, requesting preference: %u",
1497 existing->pref, requesting->pref);
1498 return false;
1499 }
1500 if (existing->expiration_managed_by_kernel && requesting->lifetime_usec == USEC_INFINITY) {
1501 log_route_debug(existing,
1502 "Cannot update route: the expiration is managed by the kernel and requested lifetime is infinite",
1503 manager);
1504 return false; /* We cannot disable expiration timer in the kernel. */
1505 }
1506 if (!route_metric_can_update(&existing->metric, &requesting->metric, existing->expiration_managed_by_kernel)) {
1507 log_route_debug(existing,
1508 "Cannot update route: expiration is managed by the kernel or metrics differ",
1509 manager);
1510 return false;
1511 }
1512 if (existing->nexthop.weight != requesting->nexthop.weight) {
1513 log_debug("Cannot update route: existing weight: %u, requesting weight: %u",
1514 existing->nexthop.weight, requesting->nexthop.weight);
1515 return false;
1516 }
1517 return true;
1518
1519 default:
1520 assert_not_reached();
1521 }
1522 }
1523
1524 static int link_unmark_route(Link *link, const Route *route, const RouteNextHop *nh) {
1525 _cleanup_(route_unrefp) Route *tmp = NULL;
1526 Route *existing;
1527 int r;
1528
1529 assert(link);
1530 assert(route);
1531
1532 r = route_dup(route, nh, &tmp);
1533 if (r < 0)
1534 return r;
1535
1536 r = route_adjust_nexthops(tmp, link);
1537 if (r < 0)
1538 return r;
1539
1540 if (route_get(link->manager, tmp, &existing) < 0)
1541 return 0;
1542
1543 if (!route_can_update(link->manager, existing, tmp))
1544 return 0;
1545
1546 route_unmark(existing);
1547 return 1;
1548 }
1549
1550 int link_drop_routes(Link *link, bool only_static) {
1551 Route *route;
1552 Link *other;
1553 int r = 0;
1554
1555 assert(link);
1556 assert(link->manager);
1557
1558 /* First, mark all routes. */
1559 SET_FOREACH(route, link->manager->routes) {
1560 /* Do not touch routes managed by the kernel. */
1561 if (route_by_kernel(route))
1562 continue;
1563
1564 /* Ignore routes not assigned yet or already removed. */
1565 if (!route_exists(route))
1566 continue;
1567
1568 if (!link_should_mark_config(link, only_static, route->source, route->protocol))
1569 continue;
1570
1571 Link *route_link = NULL;
1572 if (route_get_link(link->manager, route, &route_link) >= 0 && route_link != link) {
1573 /* When we also mark foreign routes, do not mark routes assigned to other interfaces.
1574 * Otherwise, routes assigned to unmanaged interfaces will be dropped.
1575 * Note, route_get_link() does not provide assigned link for routes with an
1576 * unreachable type or IPv4 multipath routes. So, the current implementation does not
1577 * support managing such routes by other daemon or so, unless ManageForeignRoutes=no. */
1578 if (!only_static)
1579 continue;
1580
1581 /* When we mark only static routes, do not mark routes assigned to links that we do
1582 * not know the assignment of .network files to the interfaces. Otherwise, if an
1583 * interface is in the pending state, even if the .network file to be assigned to the
1584 * interface has KeepConfiguration=yes, routes on the interface will be removed.
1585 * This is especially important when systemd-networkd is restarted. */
1586 if (!IN_SET(route_link->state, LINK_STATE_UNMANAGED, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
1587 continue;
1588 }
1589
1590 route_mark(route);
1591 }
1592
1593 /* Then, unmark all routes requested by active links. */
1594 HASHMAP_FOREACH(other, link->manager->links_by_index) {
1595 if (only_static && other == link)
1596 continue;
1597
1598 if (!IN_SET(other->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
1599 continue;
1600
1601 HASHMAP_FOREACH(route, other->network->routes_by_section) {
1602 if (route->source != NETWORK_CONFIG_SOURCE_STATIC)
1603 continue;
1604
1605 if (route->family == AF_INET || ordered_set_isempty(route->nexthops)) {
1606 r = link_unmark_route(other, route, NULL);
1607 if (r < 0)
1608 return r;
1609
1610 } else {
1611 RouteNextHop *nh;
1612 ORDERED_SET_FOREACH(nh, route->nexthops) {
1613 r = link_unmark_route(other, route, nh);
1614 if (r < 0)
1615 return r;
1616 }
1617 }
1618 }
1619
1620 /* Also unmark routes requested in .netdev file. */
1621 if (other->netdev && other->netdev->kind == NETDEV_KIND_WIREGUARD) {
1622 Wireguard *w = WIREGUARD(other->netdev);
1623
1624 SET_FOREACH(route, w->routes) {
1625 r = link_unmark_route(other, route, NULL);
1626 if (r < 0)
1627 return r;
1628 }
1629 }
1630 }
1631
1632 /* Finally, remove all marked routes. */
1633 SET_FOREACH(route, link->manager->routes) {
1634 if (!route_is_marked(route))
1635 continue;
1636
1637 RET_GATHER(r, route_remove(route, link->manager));
1638 }
1639
1640 return r;
1641 }
1642
1643 void link_forget_routes(Link *link) {
1644 assert(link);
1645 assert(link->ifindex > 0);
1646 assert(!link_is_up(link));
1647
1648 /* When an interface went down, IPv4 non-local routes bound to the interface are silently removed by
1649 * the kernel, without any notifications. Let's forget them in that case. Otherwise, when the link
1650 * goes up later, the configuration order of routes may be confused by the nonexistent routes.
1651 * See issue #35047. */
1652
1653 Route *route;
1654 SET_FOREACH(route, link->manager->routes) {
1655 // TODO: handle multipath routes
1656 if (route->nexthop.ifindex != link->ifindex)
1657 continue;
1658 if (route->family != AF_INET)
1659 continue;
1660 // TODO: check RTN_NAT and RTN_XRESOLVE
1661 if (!IN_SET(route->type, RTN_UNICAST, RTN_BROADCAST, RTN_ANYCAST, RTN_MULTICAST))
1662 continue;
1663
1664 route_forget(link->manager, route, "Forgetting silently removed");
1665 }
1666 }
1667
1668 int network_add_ipv4ll_route(Network *network) {
1669 _cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
1670 unsigned section_line;
1671 int r;
1672
1673 assert(network);
1674
1675 if (!network->ipv4ll_route)
1676 return 0;
1677
1678 r = hashmap_by_section_find_unused_line(network->routes_by_section, network->filename, &section_line);
1679 if (r < 0)
1680 return r;
1681
1682 /* IPv4LLRoute= is in [Network] section. */
1683 r = route_new_static(network, network->filename, section_line, &route);
1684 if (r < 0)
1685 return r;
1686
1687 r = in_addr_from_string(AF_INET, "169.254.0.0", &route->dst);
1688 if (r < 0)
1689 return r;
1690
1691 route->family = AF_INET;
1692 route->dst_prefixlen = 16;
1693 route->scope = RT_SCOPE_LINK;
1694 route->scope_set = true;
1695 route->table_set = true;
1696 route->priority = IPV4LL_ROUTE_METRIC;
1697 route->protocol = RTPROT_STATIC;
1698
1699 TAKE_PTR(route);
1700 return 0;
1701 }
1702
1703 int network_add_default_route_on_device(Network *network) {
1704 _cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
1705 unsigned section_line;
1706 int r;
1707
1708 assert(network);
1709
1710 if (!network->default_route_on_device)
1711 return 0;
1712
1713 r = hashmap_by_section_find_unused_line(network->routes_by_section, network->filename, &section_line);
1714 if (r < 0)
1715 return r;
1716
1717 /* DefaultRouteOnDevice= is in [Network] section. */
1718 r = route_new_static(network, network->filename, section_line, &route);
1719 if (r < 0)
1720 return r;
1721
1722 route->family = AF_INET;
1723 route->scope = RT_SCOPE_LINK;
1724 route->scope_set = true;
1725 route->protocol = RTPROT_STATIC;
1726
1727 TAKE_PTR(route);
1728 return 0;
1729 }
1730
1731 static int config_parse_preferred_src(
1732 const char *unit,
1733 const char *filename,
1734 unsigned line,
1735 const char *section,
1736 unsigned section_line,
1737 const char *lvalue,
1738 int ltype,
1739 const char *rvalue,
1740 void *data,
1741 void *userdata) {
1742
1743 Route *route = ASSERT_PTR(userdata);
1744 int r;
1745
1746 if (isempty(rvalue)) {
1747 route->prefsrc_set = false;
1748 route->prefsrc = IN_ADDR_NULL;
1749 return 1;
1750 }
1751
1752 r = parse_boolean(rvalue);
1753 if (r == 0) {
1754 /* Accepts only no. That prohibits prefsrc set by DHCP lease. */
1755 route->prefsrc_set = true;
1756 route->prefsrc = IN_ADDR_NULL;
1757 return 1;
1758 }
1759
1760 if (route->family == AF_UNSPEC)
1761 r = in_addr_from_string_auto(rvalue, &route->family, &route->prefsrc);
1762 else
1763 r = in_addr_from_string(route->family, rvalue, &route->prefsrc);
1764 if (r < 0)
1765 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1766
1767 route->prefsrc_set = true;
1768 return 1;
1769 }
1770
1771 static int config_parse_route_destination(
1772 const char *unit,
1773 const char *filename,
1774 unsigned line,
1775 const char *section,
1776 unsigned section_line,
1777 const char *lvalue,
1778 int ltype,
1779 const char *rvalue,
1780 void *data,
1781 void *userdata) {
1782
1783 Route *route = ASSERT_PTR(userdata);
1784 union in_addr_union *buffer;
1785 unsigned char *prefixlen;
1786 int r;
1787
1788 assert(lvalue);
1789 assert(rvalue);
1790
1791 if (streq(lvalue, "Destination")) {
1792 buffer = &route->dst;
1793 prefixlen = &route->dst_prefixlen;
1794 } else if (streq(lvalue, "Source")) {
1795 buffer = &route->src;
1796 prefixlen = &route->src_prefixlen;
1797 } else
1798 assert_not_reached();
1799
1800 if (route->family == AF_UNSPEC)
1801 r = in_addr_prefix_from_string_auto(rvalue, &route->family, buffer, prefixlen);
1802 else
1803 r = in_addr_prefix_from_string(rvalue, route->family, buffer, prefixlen);
1804 if (r < 0)
1805 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1806
1807 (void) in_addr_mask(route->family, buffer, *prefixlen);
1808 return 1;
1809 }
1810
1811 static int config_parse_route_priority(
1812 const char *unit,
1813 const char *filename,
1814 unsigned line,
1815 const char *section,
1816 unsigned section_line,
1817 const char *lvalue,
1818 int ltype,
1819 const char *rvalue,
1820 void *data,
1821 void *userdata) {
1822
1823 Route *route = ASSERT_PTR(userdata);
1824 int r;
1825
1826 assert(rvalue);
1827
1828 r = safe_atou32(rvalue, &route->priority);
1829 if (r < 0)
1830 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1831
1832 route->priority_set = true;
1833 return 1;
1834 }
1835
1836 static int config_parse_route_scope(
1837 const char *unit,
1838 const char *filename,
1839 unsigned line,
1840 const char *section,
1841 unsigned section_line,
1842 const char *lvalue,
1843 int ltype,
1844 const char *rvalue,
1845 void *data,
1846 void *userdata) {
1847
1848 Route *route = ASSERT_PTR(userdata);
1849 int r;
1850
1851 assert(rvalue);
1852
1853 r = route_scope_from_string(rvalue);
1854 if (r < 0)
1855 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1856
1857 route->scope = r;
1858 route->scope_set = true;
1859 return 1;
1860 }
1861
1862 static int config_parse_route_table(
1863 const char *unit,
1864 const char *filename,
1865 unsigned line,
1866 const char *section,
1867 unsigned section_line,
1868 const char *lvalue,
1869 int ltype,
1870 const char *rvalue,
1871 void *data,
1872 void *userdata) {
1873
1874 Route *route = ASSERT_PTR(userdata);
1875 Manager *manager = ASSERT_PTR(ASSERT_PTR(route->network)->manager);
1876 int r;
1877
1878 assert(rvalue);
1879
1880 r = manager_get_route_table_from_string(manager, rvalue, &route->table);
1881 if (r < 0)
1882 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1883
1884 route->table_set = true;
1885 return 1;
1886 }
1887
1888 static int config_parse_route_preference(
1889 const char *unit,
1890 const char *filename,
1891 unsigned line,
1892 const char *section,
1893 unsigned section_line,
1894 const char *lvalue,
1895 int ltype,
1896 const char *rvalue,
1897 void *data,
1898 void *userdata) {
1899
1900 Route *route = ASSERT_PTR(userdata);
1901
1902 assert(rvalue);
1903
1904 if (streq(rvalue, "low"))
1905 route->pref = SD_NDISC_PREFERENCE_LOW;
1906 else if (streq(rvalue, "medium"))
1907 route->pref = SD_NDISC_PREFERENCE_MEDIUM;
1908 else if (streq(rvalue, "high"))
1909 route->pref = SD_NDISC_PREFERENCE_HIGH;
1910 else
1911 return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
1912
1913 route->pref_set = true;
1914 return 1;
1915 }
1916
1917 static int config_parse_route_protocol(
1918 const char *unit,
1919 const char *filename,
1920 unsigned line,
1921 const char *section,
1922 unsigned section_line,
1923 const char *lvalue,
1924 int ltype,
1925 const char *rvalue,
1926 void *data,
1927 void *userdata) {
1928
1929 unsigned char *p = ASSERT_PTR(data);
1930 int r;
1931
1932 assert(rvalue);
1933
1934 r = route_protocol_from_string(rvalue);
1935 if (r < 0)
1936 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1937
1938 *p = (unsigned char) r;
1939 return 1;
1940 }
1941
1942 static int config_parse_route_type(
1943 const char *unit,
1944 const char *filename,
1945 unsigned line,
1946 const char *section,
1947 unsigned section_line,
1948 const char *lvalue,
1949 int ltype,
1950 const char *rvalue,
1951 void *data,
1952 void *userdata) {
1953
1954 unsigned char *p = ASSERT_PTR(data);
1955 int r;
1956
1957 assert(rvalue);
1958
1959 r = route_type_from_string(rvalue);
1960 if (r < 0)
1961 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
1962
1963 *p = (unsigned char) r;
1964 return 1;
1965 }
1966
1967 int config_parse_route_section(
1968 const char *unit,
1969 const char *filename,
1970 unsigned line,
1971 const char *section,
1972 unsigned section_line,
1973 const char *lvalue,
1974 int ltype,
1975 const char *rvalue,
1976 void *data,
1977 void *userdata) {
1978
1979 static const ConfigSectionParser table[_ROUTE_CONF_PARSER_MAX] = {
1980 [ROUTE_DESTINATION] = { .parser = config_parse_route_destination, .ltype = 0, .offset = 0, },
1981 [ROUTE_PREFERRED_SOURCE] = { .parser = config_parse_preferred_src, .ltype = 0, .offset = 0, },
1982 [ROUTE_PRIORITY] = { .parser = config_parse_route_priority, .ltype = 0, .offset = 0, },
1983 [ROUTE_SCOPE] = { .parser = config_parse_route_scope, .ltype = 0, .offset = 0, },
1984 [ROUTE_TABLE] = { .parser = config_parse_route_table, .ltype = 0, .offset = 0, },
1985 [ROUTE_PREFERENCE] = { .parser = config_parse_route_preference, .ltype = 0, .offset = 0, },
1986 [ROUTE_PROTOCOL] = { .parser = config_parse_route_protocol, .ltype = 0, .offset = offsetof(Route, protocol), },
1987 [ROUTE_TYPE] = { .parser = config_parse_route_type, .ltype = 0, .offset = offsetof(Route, type), },
1988 [ROUTE_GATEWAY_NETWORK] = { .parser = config_parse_gateway, .ltype = 0, .offset = 0, },
1989 [ROUTE_GATEWAY] = { .parser = config_parse_gateway, .ltype = 1, .offset = 0, },
1990 [ROUTE_GATEWAY_ONLINK] = { .parser = config_parse_tristate, .ltype = 0, .offset = offsetof(Route, gateway_onlink), },
1991 [ROUTE_MULTIPATH] = { .parser = config_parse_multipath_route, .ltype = 0, .offset = offsetof(Route, nexthops), },
1992 [ROUTE_NEXTHOP] = { .parser = config_parse_route_nexthop, .ltype = 0, .offset = offsetof(Route, nexthop_id), },
1993 [ROUTE_METRIC_MTU] = { .parser = config_parse_route_metric, .ltype = RTAX_MTU, .offset = 0, },
1994 [ROUTE_METRIC_ADVMSS] = { .parser = config_parse_route_metric, .ltype = RTAX_ADVMSS, .offset = 0, },
1995 [ROUTE_METRIC_HOPLIMIT] = { .parser = config_parse_route_metric, .ltype = RTAX_HOPLIMIT, .offset = 0, },
1996 [ROUTE_METRIC_INITCWND] = { .parser = config_parse_route_metric, .ltype = RTAX_INITCWND, .offset = 0, },
1997 [ROUTE_METRIC_RTO_MIN] = { .parser = config_parse_route_metric, .ltype = RTAX_RTO_MIN, .offset = 0, },
1998 [ROUTE_METRIC_INITRWND] = { .parser = config_parse_route_metric, .ltype = RTAX_INITRWND, .offset = 0, },
1999 [ROUTE_METRIC_QUICKACK] = { .parser = config_parse_route_metric, .ltype = RTAX_QUICKACK, .offset = 0, },
2000 [ROUTE_METRIC_CC_ALGO] = { .parser = config_parse_string, .ltype = 0, .offset = offsetof(Route, metric.tcp_congestion_control_algo), },
2001 [ROUTE_METRIC_FASTOPEN_NO_COOKIE] = { .parser = config_parse_route_metric, .ltype = RTAX_FASTOPEN_NO_COOKIE, .offset = 0, },
2002 };
2003
2004 _cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
2005 Network *network = ASSERT_PTR(userdata);
2006 int r;
2007
2008 assert(filename);
2009
2010 if (streq(section, "Network")) {
2011 assert(streq_ptr(lvalue, "Gateway"));
2012
2013 /* Clear all previously defined routes when Gateway= (empty) is set in [Network] section */
2014 if (isempty(rvalue)) {
2015 network->routes_by_section = hashmap_free(network->routes_by_section);
2016 return 0;
2017 }
2018
2019 /* we are not in an Route section, so use line number instead */
2020 r = route_new_static(network, filename, line, &route);
2021 } else
2022 r = route_new_static(network, filename, section_line, &route);
2023 if (r == -ENOMEM)
2024 return log_oom();
2025 if (r < 0) {
2026 log_syntax(unit, LOG_WARNING, filename, line, r,
2027 "Failed to allocate route, ignoring assignment: %m");
2028 return 0;
2029 }
2030
2031 r = config_section_parse(table, ELEMENTSOF(table),
2032 unit, filename, line, section, section_line, lvalue, ltype, rvalue, route);
2033 if (r <= 0)
2034 return r;
2035
2036 TAKE_PTR(route);
2037 return 0;
2038 }
2039
2040 int route_section_verify(Route *route) {
2041 int r;
2042
2043 assert(route);
2044 assert(route->section);
2045
2046 if (section_is_invalid(route->section))
2047 return -EINVAL;
2048
2049 /* Currently, we do not support static route with finite lifetime. */
2050 assert(route->lifetime_usec == USEC_INFINITY);
2051
2052 r = route_section_verify_nexthops(route);
2053 if (r < 0)
2054 return r;
2055
2056 /* table */
2057 if (!route->table_set && route->network && route->network->vrf) {
2058 route->table = VRF(route->network->vrf)->table;
2059 route->table_set = true;
2060 }
2061
2062 if (!route->table_set && IN_SET(route->type, RTN_LOCAL, RTN_BROADCAST, RTN_ANYCAST, RTN_NAT))
2063 route->table = RT_TABLE_LOCAL;
2064
2065 /* scope */
2066 if (!route->scope_set && route->family == AF_INET) {
2067 if (IN_SET(route->type, RTN_LOCAL, RTN_NAT))
2068 route->scope = RT_SCOPE_HOST;
2069 else if (IN_SET(route->type, RTN_BROADCAST, RTN_ANYCAST, RTN_MULTICAST))
2070 route->scope = RT_SCOPE_LINK;
2071 else if (IN_SET(route->type, RTN_UNICAST, RTN_UNSPEC) &&
2072 !route->gateway_from_dhcp_or_ra &&
2073 !in_addr_is_set(route->nexthop.family, &route->nexthop.gw) &&
2074 ordered_set_isempty(route->nexthops) &&
2075 route->nexthop_id == 0)
2076 route->scope = RT_SCOPE_LINK;
2077 }
2078
2079 /* IPv6 route */
2080 if (route->family == AF_INET6) {
2081 if (route->scope != RT_SCOPE_UNIVERSE) {
2082 log_section_warning(route->section, "Scope= is specified for IPv6 route. It will be ignored.");
2083 route->scope = RT_SCOPE_UNIVERSE;
2084 }
2085
2086 if (route->priority == 0)
2087 route->priority = IP6_RT_PRIO_USER;
2088 }
2089
2090 return 0;
2091 }
2092
2093 void network_drop_invalid_routes(Network *network) {
2094 Route *route;
2095
2096 assert(network);
2097
2098 HASHMAP_FOREACH(route, network->routes_by_section)
2099 if (route_section_verify(route) < 0)
2100 route_detach(route);
2101 }