1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <linux/icmpv6.h>
5 #include "alloc-util.h"
6 #include "conf-parser.h"
7 #include "in-addr-util.h"
8 #include "missing_network.h"
9 #include "netlink-util.h"
10 #include "networkd-ipv4ll.h"
11 #include "networkd-manager.h"
12 #include "networkd-ndisc.h"
13 #include "networkd-route.h"
14 #include "parse-util.h"
16 #include "socket-netlink.h"
17 #include "string-table.h"
18 #include "string-util.h"
21 #include "sysctl-util.h"
24 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
26 static unsigned routes_max(void) {
27 static thread_local
unsigned cached
= 0;
29 _cleanup_free_
char *s4
= NULL
, *s6
= NULL
;
30 unsigned val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
35 if (sysctl_read("net/ipv4/route/max_size", &s4
) >= 0) {
37 if (safe_atou(s4
, &val4
) >= 0 &&
39 /* This is the default "no limit" value in the kernel */
40 val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
43 if (sysctl_read("net/ipv6/route/max_size", &s6
) >= 0) {
45 (void) safe_atou(s6
, &val6
);
48 cached
= MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val4
) +
49 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
);
53 int route_new(Route
**ret
) {
54 _cleanup_(route_freep
) Route
*route
= NULL
;
56 route
= new(Route
, 1);
62 .scope
= RT_SCOPE_UNIVERSE
,
63 .protocol
= RTPROT_UNSPEC
,
65 .table
= RT_TABLE_MAIN
,
66 .lifetime
= USEC_INFINITY
,
68 .fast_open_no_cookie
= -1,
73 *ret
= TAKE_PTR(route
);
78 static int route_new_static(Network
*network
, const char *filename
, unsigned section_line
, Route
**ret
) {
79 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
80 _cleanup_(route_freep
) Route
*route
= NULL
;
85 assert(!!filename
== (section_line
> 0));
88 r
= network_config_section_new(filename
, section_line
, &n
);
92 route
= hashmap_get(network
->routes_by_section
, n
);
94 *ret
= TAKE_PTR(route
);
100 if (network
->n_static_routes
>= routes_max())
103 r
= route_new(&route
);
107 route
->protocol
= RTPROT_STATIC
;
108 route
->network
= network
;
109 LIST_PREPEND(routes
, network
->static_routes
, route
);
110 network
->n_static_routes
++;
113 route
->section
= TAKE_PTR(n
);
115 r
= hashmap_ensure_allocated(&network
->routes_by_section
, &network_config_hash_ops
);
119 r
= hashmap_put(network
->routes_by_section
, route
->section
, route
);
124 *ret
= TAKE_PTR(route
);
129 void route_free(Route
*route
) {
133 if (route
->network
) {
134 LIST_REMOVE(routes
, route
->network
->static_routes
, route
);
136 assert(route
->network
->n_static_routes
> 0);
137 route
->network
->n_static_routes
--;
140 hashmap_remove(route
->network
->routes_by_section
, route
->section
);
143 network_config_section_free(route
->section
);
148 set_remove(route
->link
->routes
, route
);
149 set_remove(route
->link
->routes_foreign
, route
);
150 set_remove(route
->link
->dhcp_routes
, route
);
151 set_remove(route
->link
->dhcp_routes_old
, route
);
152 set_remove(route
->link
->dhcp6_routes
, route
);
153 set_remove(route
->link
->dhcp6_routes_old
, route
);
154 set_remove(route
->link
->dhcp6_pd_routes
, route
);
155 set_remove(route
->link
->dhcp6_pd_routes_old
, route
);
156 SET_FOREACH(n
, route
->link
->ndisc_routes
)
157 if (n
->route
== route
)
158 free(set_remove(route
->link
->ndisc_routes
, n
));
161 ordered_set_free_free(route
->multipath_routes
);
163 sd_event_source_unref(route
->expire
);
168 void route_hash_func(const Route
*route
, struct siphash
*state
) {
171 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
173 switch (route
->family
) {
176 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
177 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
179 siphash24_compress(&route
->src_prefixlen
, sizeof(route
->src_prefixlen
), state
);
180 siphash24_compress(&route
->src
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
182 siphash24_compress(&route
->gw
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
184 siphash24_compress(&route
->prefsrc
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
186 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
187 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
188 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
189 siphash24_compress(&route
->protocol
, sizeof(route
->protocol
), state
);
190 siphash24_compress(&route
->scope
, sizeof(route
->scope
), state
);
191 siphash24_compress(&route
->type
, sizeof(route
->type
), state
);
193 siphash24_compress(&route
->initcwnd
, sizeof(route
->initcwnd
), state
);
194 siphash24_compress(&route
->initrwnd
, sizeof(route
->initrwnd
), state
);
198 /* treat any other address family as AF_UNSPEC */
203 int route_compare_func(const Route
*a
, const Route
*b
) {
206 r
= CMP(a
->family
, b
->family
);
213 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
217 r
= memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
221 r
= CMP(a
->src_prefixlen
, b
->src_prefixlen
);
225 r
= memcmp(&a
->src
, &b
->src
, FAMILY_ADDRESS_SIZE(a
->family
));
229 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
233 r
= memcmp(&a
->prefsrc
, &b
->prefsrc
, FAMILY_ADDRESS_SIZE(a
->family
));
237 r
= CMP(a
->tos
, b
->tos
);
241 r
= CMP(a
->priority
, b
->priority
);
245 r
= CMP(a
->table
, b
->table
);
249 r
= CMP(a
->protocol
, b
->protocol
);
253 r
= CMP(a
->scope
, b
->scope
);
257 r
= CMP(a
->type
, b
->type
);
261 r
= CMP(a
->initcwnd
, b
->initcwnd
);
265 r
= CMP(a
->initrwnd
, b
->initrwnd
);
271 /* treat any other address family as AF_UNSPEC */
276 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
283 bool route_equal(Route
*r1
, Route
*r2
) {
290 return route_compare_func(r1
, r2
) == 0;
293 int route_get(Link
*link
, Route
*in
, Route
**ret
) {
300 existing
= set_get(link
->routes
, in
);
307 existing
= set_get(link
->routes_foreign
, in
);
317 static int route_add_internal(Link
*link
, Set
**routes
, Route
*in
, Route
**ret
) {
319 _cleanup_(route_freep
) Route
*route
= NULL
;
326 r
= route_new(&route
);
330 route
->family
= in
->family
;
331 route
->src
= in
->src
;
332 route
->src_prefixlen
= in
->src_prefixlen
;
333 route
->dst
= in
->dst
;
334 route
->dst_prefixlen
= in
->dst_prefixlen
;
336 route
->prefsrc
= in
->prefsrc
;
337 route
->scope
= in
->scope
;
338 route
->protocol
= in
->protocol
;
339 route
->type
= in
->type
;
340 route
->tos
= in
->tos
;
341 route
->priority
= in
->priority
;
342 route
->table
= in
->table
;
343 route
->initcwnd
= in
->initcwnd
;
344 route
->initrwnd
= in
->initrwnd
;
345 route
->lifetime
= in
->lifetime
;
347 r
= set_ensure_put(routes
, &route_hash_ops
, route
);
363 int route_add_foreign(Link
*link
, Route
*in
, Route
**ret
) {
364 return route_add_internal(link
, &link
->routes_foreign
, in
, ret
);
367 int route_add(Link
*link
, Route
*in
, Route
**ret
) {
372 r
= route_get(link
, in
, &route
);
374 /* Route does not exist, create a new one */
375 r
= route_add_internal(link
, &link
->routes
, in
, &route
);
379 /* Take over a foreign route */
380 r
= set_ensure_put(&link
->routes
, &route_hash_ops
, route
);
384 set_remove(link
->routes_foreign
, route
);
386 /* Route exists, do nothing */
397 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
402 assert(link
->ifname
);
404 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
407 r
= sd_netlink_message_get_errno(m
);
408 if (r
< 0 && r
!= -ESRCH
)
409 log_link_message_warning_errno(link
, m
, r
, "Could not drop route, ignoring");
414 int route_remove(Route
*route
, Link
*link
,
415 link_netlink_message_handler_t callback
) {
417 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
421 assert(link
->manager
);
422 assert(link
->manager
->rtnl
);
423 assert(link
->ifindex
> 0);
424 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
426 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
427 RTM_DELROUTE
, route
->family
,
430 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
433 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
434 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
436 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
437 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
438 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
440 if (!in_addr_is_null(route
->family
, &route
->src
))
441 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
442 if (!in_addr_is_null(route
->family
, &route
->gw
))
443 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
444 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
445 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
447 log_link_debug(link
, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
448 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
449 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
450 format_route_table(route
->table
, table
, sizeof(table
)),
451 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
452 strna(route_type_to_string(route
->type
)));
455 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
456 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
458 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
461 if (route
->dst_prefixlen
) {
462 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
464 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
466 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
468 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
471 if (route
->src_prefixlen
) {
472 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
474 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
476 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
478 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
481 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
482 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
484 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
487 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
489 return log_link_error_errno(link
, r
, "Could not set scope: %m");
491 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
493 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
495 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
496 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
498 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
501 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
502 callback
?: route_remove_handler
,
503 link_netlink_destroy_callback
, link
);
505 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
512 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
513 Route
*route
= userdata
;
518 r
= route_remove(route
, route
->link
, NULL
);
520 log_link_warning_errno(route
->link
, r
, "Could not remove route: %m");
527 static int append_nexthop_one(Route
*route
, MultipathRoute
*m
, struct rtattr
**rta
, size_t offset
) {
528 struct rtnexthop
*rtnh
;
529 struct rtattr
*new_rta
;
537 new_rta
= realloc(*rta
, RTA_ALIGN((*rta
)->rta_len
) + RTA_SPACE(sizeof(struct rtnexthop
)));
542 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
543 *rtnh
= (struct rtnexthop
) {
544 .rtnh_len
= sizeof(*rtnh
),
545 .rtnh_ifindex
= m
->ifindex
,
546 .rtnh_hops
= m
->weight
> 0 ? m
->weight
- 1 : 0,
549 (*rta
)->rta_len
+= sizeof(struct rtnexthop
);
551 if (route
->family
== m
->gateway
.family
) {
552 r
= rtattr_append_attribute(rta
, RTA_GATEWAY
, &m
->gateway
.address
, FAMILY_ADDRESS_SIZE(m
->gateway
.family
));
555 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
556 rtnh
->rtnh_len
+= RTA_SPACE(FAMILY_ADDRESS_SIZE(m
->gateway
.family
));
558 r
= rtattr_append_attribute(rta
, RTA_VIA
, &m
->gateway
, FAMILY_ADDRESS_SIZE(m
->gateway
.family
) + sizeof(m
->gateway
.family
));
561 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
562 rtnh
->rtnh_len
+= RTA_SPACE(FAMILY_ADDRESS_SIZE(m
->gateway
.family
) + sizeof(m
->gateway
.family
));
568 (*rta
)->rta_len
-= sizeof(struct rtnexthop
);
572 static int append_nexthops(Route
*route
, sd_netlink_message
*req
) {
573 _cleanup_free_
struct rtattr
*rta
= NULL
;
574 struct rtnexthop
*rtnh
;
579 if (ordered_set_isempty(route
->multipath_routes
))
582 rta
= new(struct rtattr
, 1);
586 *rta
= (struct rtattr
) {
587 .rta_type
= RTA_MULTIPATH
,
588 .rta_len
= RTA_LENGTH(0),
590 offset
= (uint8_t *) RTA_DATA(rta
) - (uint8_t *) rta
;
592 ORDERED_SET_FOREACH(m
, route
->multipath_routes
) {
593 r
= append_nexthop_one(route
, m
, &rta
, offset
);
597 rtnh
= (struct rtnexthop
*)((uint8_t *) rta
+ offset
);
598 offset
= (uint8_t *) RTNH_NEXT(rtnh
) - (uint8_t *) rta
;
601 r
= sd_netlink_message_append_data(req
, RTA_MULTIPATH
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
611 link_netlink_message_handler_t callback
,
614 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
615 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
619 assert(link
->manager
);
620 assert(link
->manager
->rtnl
);
621 assert(link
->ifindex
> 0);
622 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
625 if (route_get(link
, route
, NULL
) <= 0 &&
626 set_size(link
->routes
) >= routes_max())
627 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
628 "Too many routes are configured, refusing: %m");
631 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
632 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
634 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
635 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
636 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
638 if (!in_addr_is_null(route
->family
, &route
->src
))
639 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
640 if (!in_addr_is_null(route
->family
, &route
->gw
))
641 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
642 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
643 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
645 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
646 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
647 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
648 format_route_table(route
->table
, table
, sizeof(table
)),
649 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
650 strna(route_type_to_string(route
->type
)));
653 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
654 RTM_NEWROUTE
, route
->family
,
657 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
659 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
660 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
662 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
664 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
666 return log_link_error_errno(link
, r
, "Could not set route family: %m");
669 if (route
->dst_prefixlen
> 0) {
670 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
672 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
674 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
676 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
679 if (route
->src_prefixlen
> 0) {
680 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
682 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
684 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
686 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
689 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
690 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
692 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
695 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
697 return log_link_error_errno(link
, r
, "Could not set scope: %m");
699 if (route
->gateway_onlink
>= 0)
700 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
702 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
704 return log_link_error_errno(link
, r
, "Could not set flags: %m");
706 if (route
->table
!= RT_TABLE_MAIN
) {
707 if (route
->table
< 256) {
708 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
710 return log_link_error_errno(link
, r
, "Could not set route table: %m");
712 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
714 return log_link_error_errno(link
, r
, "Could not set route table: %m");
716 /* Table attribute to allow more than 256. */
717 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
719 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
723 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
725 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
727 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
729 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
731 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
732 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
733 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
735 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
738 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
740 return log_link_error_errno(link
, r
, "Could not set route type: %m");
742 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
743 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
745 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
748 if (route
->ttl_propagate
>= 0) {
749 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
751 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
754 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
756 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
758 if (route
->mtu
> 0) {
759 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
761 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
764 if (route
->initcwnd
> 0) {
765 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
767 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
770 if (route
->initrwnd
> 0) {
771 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
773 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
776 if (route
->quickack
>= 0) {
777 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
779 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
782 if (route
->fast_open_no_cookie
>= 0) {
783 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
785 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
788 r
= sd_netlink_message_close_container(req
);
790 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
792 r
= append_nexthops(route
, req
);
794 return log_link_error_errno(link
, r
, "Could not append RTA_MULTIPATH attribute: %m");
796 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
797 link_netlink_destroy_callback
, link
);
799 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
803 r
= route_add(link
, route
, &route
);
805 return log_link_error_errno(link
, r
, "Could not add route: %m");
807 /* TODO: drop expiration handling once it can be pushed into the kernel */
808 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
809 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
810 route
->lifetime
, 0, route_expire_handler
, route
);
812 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
815 sd_event_source_unref(route
->expire
);
816 route
->expire
= TAKE_PTR(expire
);
824 int network_add_ipv4ll_route(Network
*network
) {
825 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
830 if (!network
->ipv4ll_route
)
833 /* IPv4LLRoute= is in [Network] section. */
834 r
= route_new_static(network
, NULL
, 0, &n
);
838 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
843 n
->dst_prefixlen
= 16;
844 n
->scope
= RT_SCOPE_LINK
;
847 n
->priority
= IPV4LL_ROUTE_METRIC
;
848 n
->protocol
= RTPROT_STATIC
;
854 int network_add_default_route_on_device(Network
*network
) {
855 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
860 if (!network
->default_route_on_device
)
863 /* DefaultRouteOnDevice= is in [Network] section. */
864 r
= route_new_static(network
, NULL
, 0, &n
);
869 n
->scope
= RT_SCOPE_LINK
;
871 n
->protocol
= RTPROT_STATIC
;
877 static const char * const route_type_table
[__RTN_MAX
] = {
878 [RTN_UNICAST
] = "unicast",
879 [RTN_LOCAL
] = "local",
880 [RTN_BROADCAST
] = "broadcast",
881 [RTN_ANYCAST
] = "anycast",
882 [RTN_MULTICAST
] = "multicast",
883 [RTN_BLACKHOLE
] = "blackhole",
884 [RTN_UNREACHABLE
] = "unreachable",
885 [RTN_PROHIBIT
] = "prohibit",
886 [RTN_THROW
] = "throw",
888 [RTN_XRESOLVE
] = "xresolve",
891 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
892 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
894 static const char * const route_scope_table
[] = {
895 [RT_SCOPE_UNIVERSE
] = "global",
896 [RT_SCOPE_SITE
] = "site",
897 [RT_SCOPE_LINK
] = "link",
898 [RT_SCOPE_HOST
] = "host",
899 [RT_SCOPE_NOWHERE
] = "nowhere",
902 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
904 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
908 s
= route_scope_to_string(scope
);
910 strpcpy(&p
, size
, s
);
912 strpcpyf(&p
, size
, "%d", scope
);
917 static const char * const route_table_table
[] = {
918 [RT_TABLE_DEFAULT
] = "default",
919 [RT_TABLE_MAIN
] = "main",
920 [RT_TABLE_LOCAL
] = "local",
923 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
925 const char *format_route_table(int table
, char *buf
, size_t size
) {
929 s
= route_table_to_string(table
);
931 strpcpy(&p
, size
, s
);
933 strpcpyf(&p
, size
, "%d", table
);
938 static const char * const route_protocol_table
[] = {
939 [RTPROT_KERNEL
] = "kernel",
940 [RTPROT_BOOT
] = "boot",
941 [RTPROT_STATIC
] = "static",
944 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol
, int);
946 static const char * const route_protocol_full_table
[] = {
947 [RTPROT_REDIRECT
] = "redirect",
948 [RTPROT_KERNEL
] = "kernel",
949 [RTPROT_BOOT
] = "boot",
950 [RTPROT_STATIC
] = "static",
951 [RTPROT_GATED
] = "gated",
953 [RTPROT_MRT
] = "mrt",
954 [RTPROT_ZEBRA
] = "zebra",
955 [RTPROT_BIRD
] = "bird",
956 [RTPROT_DNROUTED
] = "dnrouted",
957 [RTPROT_XORP
] = "xorp",
958 [RTPROT_NTK
] = "ntk",
959 [RTPROT_DHCP
] = "dhcp",
960 [RTPROT_MROUTED
] = "mrouted",
961 [RTPROT_BABEL
] = "babel",
962 [RTPROT_BGP
] = "bgp",
963 [RTPROT_ISIS
] = "isis",
964 [RTPROT_OSPF
] = "ospf",
965 [RTPROT_RIP
] = "rip",
966 [RTPROT_EIGRP
] = "eigrp",
969 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full
, int);
971 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
975 s
= route_protocol_full_to_string(protocol
);
977 strpcpy(&p
, size
, s
);
979 strpcpyf(&p
, size
, "%d", protocol
);
984 int config_parse_gateway(
986 const char *filename
,
989 unsigned section_line
,
996 Network
*network
= userdata
;
997 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1006 if (streq(section
, "Network")) {
1007 /* we are not in an Route section, so treat
1008 * this as the special '0' section */
1009 r
= route_new_static(network
, NULL
, 0, &n
);
1013 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1014 "Failed to allocate route, ignoring assignment: %m");
1018 r
= route_new_static(network
, filename
, section_line
, &n
);
1022 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1023 "Failed to allocate route, ignoring assignment: %m");
1027 if (streq(rvalue
, "_dhcp")) {
1028 n
->gateway_from_dhcp
= true;
1034 if (n
->family
== AF_UNSPEC
)
1035 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1037 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
1039 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1040 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1048 int config_parse_preferred_src(
1050 const char *filename
,
1052 const char *section
,
1053 unsigned section_line
,
1060 Network
*network
= userdata
;
1061 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1070 r
= route_new_static(network
, filename
, section_line
, &n
);
1074 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1075 "Failed to allocate route, ignoring assignment: %m");
1079 if (n
->family
== AF_UNSPEC
)
1080 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
1082 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
1084 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
,
1085 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1093 int config_parse_destination(
1095 const char *filename
,
1097 const char *section
,
1098 unsigned section_line
,
1105 Network
*network
= userdata
;
1106 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1107 union in_addr_union
*buffer
;
1108 unsigned char *prefixlen
;
1117 r
= route_new_static(network
, filename
, section_line
, &n
);
1121 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1122 "Failed to allocate route, ignoring assignment: %m");
1126 if (streq(lvalue
, "Destination")) {
1128 prefixlen
= &n
->dst_prefixlen
;
1129 } else if (streq(lvalue
, "Source")) {
1131 prefixlen
= &n
->src_prefixlen
;
1133 assert_not_reached(lvalue
);
1135 if (n
->family
== AF_UNSPEC
)
1136 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
1138 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
1140 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
,
1141 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1149 int config_parse_route_priority(
1151 const char *filename
,
1153 const char *section
,
1154 unsigned section_line
,
1161 Network
*network
= userdata
;
1162 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1171 r
= route_new_static(network
, filename
, section_line
, &n
);
1175 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1176 "Failed to allocate route, ignoring assignment: %m");
1180 r
= safe_atou32(rvalue
, &n
->priority
);
1182 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1183 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1191 int config_parse_route_scope(
1193 const char *filename
,
1195 const char *section
,
1196 unsigned section_line
,
1203 Network
*network
= userdata
;
1204 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1213 r
= route_new_static(network
, filename
, section_line
, &n
);
1217 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1218 "Failed to allocate route, ignoring assignment: %m");
1222 r
= route_scope_from_string(rvalue
);
1224 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1229 n
->scope_set
= true;
1234 int config_parse_route_table(
1236 const char *filename
,
1238 const char *section
,
1239 unsigned section_line
,
1246 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1247 Network
*network
= userdata
;
1256 r
= route_new_static(network
, filename
, section_line
, &n
);
1260 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1261 "Failed to allocate route, ignoring assignment: %m");
1265 r
= route_table_from_string(rvalue
);
1269 r
= safe_atou32(rvalue
, &n
->table
);
1271 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1272 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1277 n
->table_set
= true;
1282 int config_parse_route_boolean(
1284 const char *filename
,
1286 const char *section
,
1287 unsigned section_line
,
1294 Network
*network
= userdata
;
1295 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1304 r
= route_new_static(network
, filename
, section_line
, &n
);
1308 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1309 "Failed to allocate route, ignoring assignment: %m");
1313 r
= parse_boolean(rvalue
);
1315 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1316 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1320 if (STR_IN_SET(lvalue
, "GatewayOnLink", "GatewayOnlink"))
1321 n
->gateway_onlink
= r
;
1322 else if (streq(lvalue
, "QuickAck"))
1324 else if (streq(lvalue
, "FastOpenNoCookie"))
1325 n
->fast_open_no_cookie
= r
;
1326 else if (streq(lvalue
, "TTLPropagate"))
1327 n
->ttl_propagate
= r
;
1329 assert_not_reached("Invalid lvalue");
1335 int config_parse_ipv6_route_preference(
1337 const char *filename
,
1339 const char *section
,
1340 unsigned section_line
,
1347 Network
*network
= userdata
;
1348 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1351 r
= route_new_static(network
, filename
, section_line
, &n
);
1355 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1356 "Failed to allocate route, ignoring assignment: %m");
1360 if (streq(rvalue
, "low"))
1361 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1362 else if (streq(rvalue
, "medium"))
1363 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1364 else if (streq(rvalue
, "high"))
1365 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1367 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1375 int config_parse_route_protocol(
1377 const char *filename
,
1379 const char *section
,
1380 unsigned section_line
,
1387 Network
*network
= userdata
;
1388 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1391 r
= route_new_static(network
, filename
, section_line
, &n
);
1395 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1396 "Failed to allocate route, ignoring assignment: %m");
1400 r
= route_protocol_from_string(rvalue
);
1404 r
= safe_atou8(rvalue
, &n
->protocol
);
1406 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1407 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1416 int config_parse_route_type(
1418 const char *filename
,
1420 const char *section
,
1421 unsigned section_line
,
1428 Network
*network
= userdata
;
1429 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1432 r
= route_new_static(network
, filename
, section_line
, &n
);
1436 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1437 "Failed to allocate route, ignoring assignment: %m");
1441 t
= route_type_from_string(rvalue
);
1443 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1444 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1448 n
->type
= (unsigned char) t
;
1454 int config_parse_tcp_window(
1456 const char *filename
,
1458 const char *section
,
1459 unsigned section_line
,
1466 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1467 Network
*network
= userdata
;
1477 r
= route_new_static(network
, filename
, section_line
, &n
);
1481 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1482 "Failed to allocate route, ignoring assignment: %m");
1486 r
= parse_size(rvalue
, 1024, &k
);
1488 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1489 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1492 if (k
> UINT32_MAX
) {
1493 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1494 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1498 if (streq(lvalue
, "InitialCongestionWindow"))
1500 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1503 assert_not_reached("Invalid TCP window type.");
1509 int config_parse_route_mtu(
1511 const char *filename
,
1513 const char *section
,
1514 unsigned section_line
,
1521 Network
*network
= userdata
;
1522 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1531 r
= route_new_static(network
, filename
, section_line
, &n
);
1535 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1536 "Failed to allocate route, ignoring assignment: %m");
1540 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1548 int config_parse_multipath_route(
1550 const char *filename
,
1552 const char *section
,
1553 unsigned section_line
,
1560 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1561 _cleanup_free_
char *word
= NULL
, *buf
= NULL
;
1562 _cleanup_free_ MultipathRoute
*m
= NULL
;
1563 Network
*network
= userdata
;
1564 const char *p
, *ip
, *dev
;
1565 union in_addr_union a
;
1574 r
= route_new_static(network
, filename
, section_line
, &n
);
1578 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1579 "Failed to allocate route, ignoring assignment: %m");
1583 if (isempty(rvalue
)) {
1584 n
->multipath_routes
= ordered_set_free_free(n
->multipath_routes
);
1588 m
= new0(MultipathRoute
, 1);
1593 r
= extract_first_word(&p
, &word
, NULL
, 0);
1597 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1598 "Invalid multipath route option, ignoring assignment: %s", rvalue
);
1602 dev
= strchr(word
, '@');
1604 buf
= strndup(word
, dev
- word
);
1612 r
= in_addr_from_string_auto(ip
, &family
, &a
);
1614 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1615 "Invalid multipath route gateway '%s', ignoring assignment: %m", rvalue
);
1618 m
->gateway
.address
= a
;
1619 m
->gateway
.family
= family
;
1622 r
= resolve_interface(NULL
, dev
);
1624 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1625 "Invalid interface name or index, ignoring assignment: %s", dev
);
1632 r
= safe_atou32(p
, &m
->weight
);
1634 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1635 "Invalid multipath route weight, ignoring assignment: %s", p
);
1638 if (m
->weight
== 0 || m
->weight
> 256) {
1639 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1640 "Invalid multipath route weight, ignoring assignment: %s", p
);
1645 r
= ordered_set_ensure_allocated(&n
->multipath_routes
, NULL
);
1649 r
= ordered_set_put(n
->multipath_routes
, m
);
1651 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1652 "Failed to store multipath route, ignoring assignment: %m");
1661 int route_section_verify(Route
*route
, Network
*network
) {
1662 if (section_is_invalid(route
->section
))
1665 if (route
->family
== AF_UNSPEC
) {
1666 assert(route
->section
);
1668 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1669 "%s: Route section without Gateway=, Destination=, Source=, "
1670 "or PreferredSource= field configured. "
1671 "Ignoring [Route] section from line %u.",
1672 route
->section
->filename
, route
->section
->line
);
1675 if (!route
->table_set
&& network
->vrf
) {
1676 route
->table
= VRF(network
->vrf
)->table
;
1677 route
->table_set
= true;
1680 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1681 route
->table
= RT_TABLE_LOCAL
;
1683 if (!route
->scope_set
&& route
->family
!= AF_INET6
) {
1684 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1685 route
->scope
= RT_SCOPE_HOST
;
1686 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_MULTICAST
))
1687 route
->scope
= RT_SCOPE_LINK
;
1690 if (network
->n_static_addresses
== 0 &&
1691 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1692 route
->gateway_onlink
< 0) {
1693 log_warning("%s: Gateway= without static address configured. "
1694 "Enabling GatewayOnLink= option.",
1696 route
->gateway_onlink
= true;