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