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-route.h"
13 #include "parse-util.h"
15 #include "string-table.h"
16 #include "string-util.h"
18 #include "sysctl-util.h"
21 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
23 static unsigned routes_max(void) {
24 static thread_local
unsigned cached
= 0;
26 _cleanup_free_
char *s4
= NULL
, *s6
= NULL
;
27 unsigned val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
32 if (sysctl_read("net/ipv4/route/max_size", &s4
) >= 0) {
34 if (safe_atou(s4
, &val4
) >= 0 &&
36 /* This is the default "no limit" value in the kernel */
37 val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
40 if (sysctl_read("net/ipv6/route/max_size", &s6
) >= 0) {
42 (void) safe_atou(s6
, &val6
);
45 cached
= MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val4
) +
46 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
);
50 int route_new(Route
**ret
) {
51 _cleanup_(route_freep
) Route
*route
= NULL
;
53 route
= new(Route
, 1);
59 .scope
= RT_SCOPE_UNIVERSE
,
60 .protocol
= RTPROT_UNSPEC
,
62 .table
= RT_TABLE_MAIN
,
63 .lifetime
= USEC_INFINITY
,
65 .fast_open_no_cookie
= -1,
70 *ret
= TAKE_PTR(route
);
75 static int route_new_static(Network
*network
, const char *filename
, unsigned section_line
, Route
**ret
) {
76 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
77 _cleanup_(route_freep
) Route
*route
= NULL
;
82 assert(!!filename
== (section_line
> 0));
85 r
= network_config_section_new(filename
, section_line
, &n
);
89 route
= hashmap_get(network
->routes_by_section
, n
);
91 *ret
= TAKE_PTR(route
);
97 if (network
->n_static_routes
>= routes_max())
100 r
= route_new(&route
);
104 route
->protocol
= RTPROT_STATIC
;
105 route
->network
= network
;
106 LIST_PREPEND(routes
, network
->static_routes
, route
);
107 network
->n_static_routes
++;
110 route
->section
= TAKE_PTR(n
);
112 r
= hashmap_ensure_allocated(&network
->routes_by_section
, &network_config_hash_ops
);
116 r
= hashmap_put(network
->routes_by_section
, route
->section
, route
);
121 *ret
= TAKE_PTR(route
);
126 void route_free(Route
*route
) {
130 if (route
->network
) {
131 LIST_REMOVE(routes
, route
->network
->static_routes
, route
);
133 assert(route
->network
->n_static_routes
> 0);
134 route
->network
->n_static_routes
--;
137 hashmap_remove(route
->network
->routes_by_section
, route
->section
);
140 network_config_section_free(route
->section
);
143 set_remove(route
->link
->routes
, route
);
144 set_remove(route
->link
->routes_foreign
, route
);
147 ordered_set_free_free(route
->multipath_routes
);
149 sd_event_source_unref(route
->expire
);
154 static void route_hash_func(const Route
*route
, struct siphash
*state
) {
157 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
159 switch (route
->family
) {
162 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
163 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
165 siphash24_compress(&route
->src_prefixlen
, sizeof(route
->src_prefixlen
), state
);
166 siphash24_compress(&route
->src
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
168 siphash24_compress(&route
->gw
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
170 siphash24_compress(&route
->prefsrc
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
172 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
173 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
174 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
175 siphash24_compress(&route
->protocol
, sizeof(route
->protocol
), state
);
176 siphash24_compress(&route
->scope
, sizeof(route
->scope
), state
);
177 siphash24_compress(&route
->type
, sizeof(route
->type
), state
);
179 siphash24_compress(&route
->initcwnd
, sizeof(route
->initcwnd
), state
);
180 siphash24_compress(&route
->initrwnd
, sizeof(route
->initrwnd
), state
);
184 /* treat any other address family as AF_UNSPEC */
189 static int route_compare_func(const Route
*a
, const Route
*b
) {
192 r
= CMP(a
->family
, b
->family
);
199 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
203 r
= memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
207 r
= CMP(a
->src_prefixlen
, b
->src_prefixlen
);
211 r
= memcmp(&a
->src
, &b
->src
, FAMILY_ADDRESS_SIZE(a
->family
));
215 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
219 r
= memcmp(&a
->prefsrc
, &b
->prefsrc
, FAMILY_ADDRESS_SIZE(a
->family
));
223 r
= CMP(a
->tos
, b
->tos
);
227 r
= CMP(a
->priority
, b
->priority
);
231 r
= CMP(a
->table
, b
->table
);
235 r
= CMP(a
->protocol
, b
->protocol
);
239 r
= CMP(a
->scope
, b
->scope
);
243 r
= CMP(a
->type
, b
->type
);
247 r
= CMP(a
->initcwnd
, b
->initcwnd
);
251 r
= CMP(a
->initrwnd
, b
->initrwnd
);
257 /* treat any other address family as AF_UNSPEC */
262 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
269 bool route_equal(Route
*r1
, Route
*r2
) {
276 return route_compare_func(r1
, r2
) == 0;
279 int route_get(Link
*link
, Route
*in
, Route
**ret
) {
286 existing
= set_get(link
->routes
, in
);
293 existing
= set_get(link
->routes_foreign
, in
);
303 static int route_add_internal(Link
*link
, Set
**routes
, Route
*in
, Route
**ret
) {
305 _cleanup_(route_freep
) Route
*route
= NULL
;
312 r
= route_new(&route
);
316 route
->family
= in
->family
;
317 route
->src
= in
->src
;
318 route
->src_prefixlen
= in
->src_prefixlen
;
319 route
->dst
= in
->dst
;
320 route
->dst_prefixlen
= in
->dst_prefixlen
;
322 route
->prefsrc
= in
->prefsrc
;
323 route
->scope
= in
->scope
;
324 route
->protocol
= in
->protocol
;
325 route
->type
= in
->type
;
326 route
->tos
= in
->tos
;
327 route
->priority
= in
->priority
;
328 route
->table
= in
->table
;
329 route
->initcwnd
= in
->initcwnd
;
330 route
->initrwnd
= in
->initrwnd
;
331 route
->lifetime
= in
->lifetime
;
333 r
= set_ensure_allocated(routes
, &route_hash_ops
);
337 r
= set_put(*routes
, route
);
353 int route_add_foreign(Link
*link
, Route
*in
, Route
**ret
) {
354 return route_add_internal(link
, &link
->routes_foreign
, in
, ret
);
357 int route_add(Link
*link
, Route
*in
, Route
**ret
) {
362 r
= route_get(link
, in
, &route
);
364 /* Route does not exist, create a new one */
365 r
= route_add_internal(link
, &link
->routes
, in
, &route
);
369 /* Take over a foreign route */
370 r
= set_ensure_allocated(&link
->routes
, &route_hash_ops
);
374 r
= set_put(link
->routes
, route
);
378 set_remove(link
->routes_foreign
, route
);
380 /* Route exists, do nothing */
391 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
396 assert(link
->ifname
);
398 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
401 r
= sd_netlink_message_get_errno(m
);
402 if (r
< 0 && r
!= -ESRCH
)
403 log_link_message_warning_errno(link
, m
, r
, "Could not drop route, ignoring");
408 int route_remove(Route
*route
, Link
*link
,
409 link_netlink_message_handler_t callback
) {
411 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
415 assert(link
->manager
);
416 assert(link
->manager
->rtnl
);
417 assert(link
->ifindex
> 0);
418 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
420 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
421 RTM_DELROUTE
, route
->family
,
424 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
427 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
428 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
430 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
431 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
432 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
434 if (!in_addr_is_null(route
->family
, &route
->src
))
435 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
436 if (!in_addr_is_null(route
->family
, &route
->gw
))
437 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
438 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
439 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
441 log_link_debug(link
, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
442 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
443 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
444 format_route_table(route
->table
, table
, sizeof(table
)),
445 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
446 strna(route_type_to_string(route
->type
)));
449 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
450 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
452 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
455 if (route
->dst_prefixlen
) {
456 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
458 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
460 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
462 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
465 if (route
->src_prefixlen
) {
466 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
468 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
470 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
472 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
475 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
476 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
478 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
481 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
483 return log_link_error_errno(link
, r
, "Could not set scope: %m");
485 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
487 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
489 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
490 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
492 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
495 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
496 callback
?: route_remove_handler
,
497 link_netlink_destroy_callback
, link
);
499 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
506 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
507 Route
*route
= userdata
;
512 r
= route_remove(route
, route
->link
, NULL
);
514 log_warning_errno(r
, "Could not remove route: %m");
521 static int append_nexthop_one(Route
*route
, MultipathRoute
*m
, struct rtattr
**rta
, size_t offset
) {
522 struct rtnexthop
*rtnh
;
523 struct rtattr
*new_rta
;
531 new_rta
= realloc(*rta
, RTA_ALIGN((*rta
)->rta_len
) + RTA_SPACE(sizeof(struct rtnexthop
)));
536 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
537 *rtnh
= (struct rtnexthop
) {
538 .rtnh_len
= sizeof(*rtnh
),
539 .rtnh_ifindex
= m
->ifindex
,
540 .rtnh_hops
= m
->weight
> 0 ? m
->weight
- 1 : 0,
543 (*rta
)->rta_len
+= sizeof(struct rtnexthop
);
545 if (route
->family
== m
->gateway
.family
) {
546 r
= rtattr_append_attribute(rta
, RTA_GATEWAY
, &m
->gateway
.address
, FAMILY_ADDRESS_SIZE(m
->gateway
.family
));
549 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
550 rtnh
->rtnh_len
+= RTA_SPACE(FAMILY_ADDRESS_SIZE(m
->gateway
.family
));
552 r
= rtattr_append_attribute(rta
, RTA_VIA
, &m
->gateway
, FAMILY_ADDRESS_SIZE(m
->gateway
.family
) + sizeof(m
->gateway
.family
));
555 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
556 rtnh
->rtnh_len
+= RTA_SPACE(FAMILY_ADDRESS_SIZE(m
->gateway
.family
) + sizeof(m
->gateway
.family
));
562 (*rta
)->rta_len
-= sizeof(struct rtnexthop
);
566 static int append_nexthops(Route
*route
, sd_netlink_message
*req
) {
567 _cleanup_free_
struct rtattr
*rta
= NULL
;
568 struct rtnexthop
*rtnh
;
574 if (ordered_set_isempty(route
->multipath_routes
))
577 rta
= new(struct rtattr
, 1);
581 *rta
= (struct rtattr
) {
582 .rta_type
= RTA_MULTIPATH
,
583 .rta_len
= RTA_LENGTH(0),
585 offset
= (uint8_t *) RTA_DATA(rta
) - (uint8_t *) rta
;
587 ORDERED_SET_FOREACH(m
, route
->multipath_routes
, i
) {
588 r
= append_nexthop_one(route
, m
, &rta
, offset
);
592 rtnh
= (struct rtnexthop
*)((uint8_t *) rta
+ offset
);
593 offset
= (uint8_t *) RTNH_NEXT(rtnh
) - (uint8_t *) rta
;
596 r
= sd_netlink_message_append_data(req
, RTA_MULTIPATH
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
606 link_netlink_message_handler_t callback
) {
608 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
609 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
613 assert(link
->manager
);
614 assert(link
->manager
->rtnl
);
615 assert(link
->ifindex
> 0);
616 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
619 if (route_get(link
, route
, NULL
) <= 0 &&
620 set_size(link
->routes
) >= routes_max())
621 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
622 "Too many routes are configured, refusing: %m");
625 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
626 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
628 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
629 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
630 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
632 if (!in_addr_is_null(route
->family
, &route
->src
))
633 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
634 if (!in_addr_is_null(route
->family
, &route
->gw
))
635 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
636 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
637 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
639 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
640 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
641 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
642 format_route_table(route
->table
, table
, sizeof(table
)),
643 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
644 strna(route_type_to_string(route
->type
)));
647 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
648 RTM_NEWROUTE
, route
->family
,
651 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
653 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
654 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
656 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
658 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
660 return log_link_error_errno(link
, r
, "Could not set route family: %m");
663 if (route
->dst_prefixlen
> 0) {
664 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
666 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
668 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
670 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
673 if (route
->src_prefixlen
> 0) {
674 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
676 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
678 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
680 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
683 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
684 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
686 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
689 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
691 return log_link_error_errno(link
, r
, "Could not set scope: %m");
693 if (route
->gateway_onlink
>= 0)
694 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
696 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
698 return log_link_error_errno(link
, r
, "Could not set flags: %m");
700 if (route
->table
!= RT_TABLE_MAIN
) {
701 if (route
->table
< 256) {
702 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
704 return log_link_error_errno(link
, r
, "Could not set route table: %m");
706 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
708 return log_link_error_errno(link
, r
, "Could not set route table: %m");
710 /* Table attribute to allow more than 256. */
711 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
713 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
717 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
719 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
721 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
723 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
725 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
726 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
727 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
729 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
732 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
734 return log_link_error_errno(link
, r
, "Could not set route type: %m");
736 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
737 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
739 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
742 if (route
->ttl_propagate
>= 0) {
743 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
745 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
748 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
750 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
752 if (route
->mtu
> 0) {
753 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
755 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
758 if (route
->initcwnd
> 0) {
759 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
761 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
764 if (route
->initrwnd
> 0) {
765 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
767 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
770 if (route
->quickack
>= 0) {
771 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
773 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
776 if (route
->fast_open_no_cookie
>= 0) {
777 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
779 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
782 r
= sd_netlink_message_close_container(req
);
784 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
786 r
= append_nexthops(route
, req
);
788 return log_link_error_errno(link
, r
, "Could not append RTA_MULTIPATH attribute: %m");
790 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
791 link_netlink_destroy_callback
, link
);
793 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
797 r
= route_add(link
, route
, &route
);
799 return log_link_error_errno(link
, r
, "Could not add route: %m");
801 /* TODO: drop expiration handling once it can be pushed into the kernel */
802 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
803 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
804 route
->lifetime
, 0, route_expire_handler
, route
);
806 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
809 sd_event_source_unref(route
->expire
);
810 route
->expire
= TAKE_PTR(expire
);
815 int network_add_ipv4ll_route(Network
*network
) {
816 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
821 if (!network
->ipv4ll_route
)
824 /* IPv4LLRoute= is in [Network] section. */
825 r
= route_new_static(network
, NULL
, 0, &n
);
829 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
834 n
->dst_prefixlen
= 16;
835 n
->scope
= RT_SCOPE_LINK
;
838 n
->priority
= IPV4LL_ROUTE_METRIC
;
839 n
->protocol
= RTPROT_STATIC
;
845 int network_add_default_route_on_device(Network
*network
) {
846 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
851 if (!network
->default_route_on_device
)
854 /* DefaultRouteOnDevice= is in [Network] section. */
855 r
= route_new_static(network
, NULL
, 0, &n
);
860 n
->scope
= RT_SCOPE_LINK
;
862 n
->protocol
= RTPROT_STATIC
;
868 static const char * const route_type_table
[__RTN_MAX
] = {
869 [RTN_UNICAST
] = "unicast",
870 [RTN_LOCAL
] = "local",
871 [RTN_BROADCAST
] = "broadcast",
872 [RTN_ANYCAST
] = "anycast",
873 [RTN_MULTICAST
] = "multicast",
874 [RTN_BLACKHOLE
] = "blackhole",
875 [RTN_UNREACHABLE
] = "unreachable",
876 [RTN_PROHIBIT
] = "prohibit",
877 [RTN_THROW
] = "throw",
879 [RTN_XRESOLVE
] = "xresolve",
882 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
883 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
885 static const char * const route_scope_table
[] = {
886 [RT_SCOPE_UNIVERSE
] = "global",
887 [RT_SCOPE_SITE
] = "site",
888 [RT_SCOPE_LINK
] = "link",
889 [RT_SCOPE_HOST
] = "host",
890 [RT_SCOPE_NOWHERE
] = "nowhere",
893 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
895 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
899 s
= route_scope_to_string(scope
);
901 strpcpy(&p
, size
, s
);
903 strpcpyf(&p
, size
, "%d", scope
);
908 static const char * const route_table_table
[] = {
909 [RT_TABLE_DEFAULT
] = "default",
910 [RT_TABLE_MAIN
] = "main",
911 [RT_TABLE_LOCAL
] = "local",
914 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
916 const char *format_route_table(int table
, char *buf
, size_t size
) {
920 s
= route_table_to_string(table
);
922 strpcpy(&p
, size
, s
);
924 strpcpyf(&p
, size
, "%d", table
);
929 static const char * const route_protocol_table
[] = {
930 [RTPROT_KERNEL
] = "kernel",
931 [RTPROT_BOOT
] = "boot",
932 [RTPROT_STATIC
] = "static",
935 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol
, int);
937 static const char * const route_protocol_full_table
[] = {
938 [RTPROT_REDIRECT
] = "redirect",
939 [RTPROT_KERNEL
] = "kernel",
940 [RTPROT_BOOT
] = "boot",
941 [RTPROT_STATIC
] = "static",
942 [RTPROT_GATED
] = "gated",
944 [RTPROT_MRT
] = "mrt",
945 [RTPROT_ZEBRA
] = "zebra",
946 [RTPROT_BIRD
] = "bird",
947 [RTPROT_DNROUTED
] = "dnrouted",
948 [RTPROT_XORP
] = "xorp",
949 [RTPROT_NTK
] = "ntk",
950 [RTPROT_DHCP
] = "dhcp",
951 [RTPROT_MROUTED
] = "mrouted",
952 [RTPROT_BABEL
] = "babel",
953 [RTPROT_BGP
] = "bgp",
954 [RTPROT_ISIS
] = "isis",
955 [RTPROT_OSPF
] = "ospf",
956 [RTPROT_RIP
] = "rip",
957 [RTPROT_EIGRP
] = "eigrp",
960 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full
, int);
962 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
966 s
= route_protocol_full_to_string(protocol
);
968 strpcpy(&p
, size
, s
);
970 strpcpyf(&p
, size
, "%d", protocol
);
975 int config_parse_gateway(
977 const char *filename
,
980 unsigned section_line
,
987 Network
*network
= userdata
;
988 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
997 if (streq(section
, "Network")) {
998 /* we are not in an Route section, so treat
999 * this as the special '0' section */
1000 r
= route_new_static(network
, NULL
, 0, &n
);
1004 r
= route_new_static(network
, filename
, section_line
, &n
);
1008 if (streq(rvalue
, "dhcp")) {
1009 n
->gateway_from_dhcp
= true;
1015 if (n
->family
== AF_UNSPEC
)
1016 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1018 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
1020 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1021 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1029 int config_parse_preferred_src(
1031 const char *filename
,
1033 const char *section
,
1034 unsigned section_line
,
1041 Network
*network
= userdata
;
1042 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1051 r
= route_new_static(network
, filename
, section_line
, &n
);
1055 if (n
->family
== AF_UNSPEC
)
1056 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
1058 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
1060 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1061 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1069 int config_parse_destination(
1071 const char *filename
,
1073 const char *section
,
1074 unsigned section_line
,
1081 Network
*network
= userdata
;
1082 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1083 union in_addr_union
*buffer
;
1084 unsigned char *prefixlen
;
1093 r
= route_new_static(network
, filename
, section_line
, &n
);
1097 if (streq(lvalue
, "Destination")) {
1099 prefixlen
= &n
->dst_prefixlen
;
1100 } else if (streq(lvalue
, "Source")) {
1102 prefixlen
= &n
->src_prefixlen
;
1104 assert_not_reached(lvalue
);
1106 if (n
->family
== AF_UNSPEC
)
1107 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
1109 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
1111 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1112 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1120 int config_parse_route_priority(
1122 const char *filename
,
1124 const char *section
,
1125 unsigned section_line
,
1132 Network
*network
= userdata
;
1133 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1142 r
= route_new_static(network
, filename
, section_line
, &n
);
1146 r
= safe_atou32(rvalue
, &n
->priority
);
1148 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1149 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1157 int config_parse_route_scope(
1159 const char *filename
,
1161 const char *section
,
1162 unsigned section_line
,
1169 Network
*network
= userdata
;
1170 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1179 r
= route_new_static(network
, filename
, section_line
, &n
);
1183 r
= route_scope_from_string(rvalue
);
1185 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1190 n
->scope_set
= true;
1195 int config_parse_route_table(
1197 const char *filename
,
1199 const char *section
,
1200 unsigned section_line
,
1207 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1208 Network
*network
= userdata
;
1217 r
= route_new_static(network
, filename
, section_line
, &n
);
1221 r
= route_table_from_string(rvalue
);
1225 r
= safe_atou32(rvalue
, &n
->table
);
1227 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1228 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1233 n
->table_set
= true;
1238 int config_parse_gateway_onlink(
1240 const char *filename
,
1242 const char *section
,
1243 unsigned section_line
,
1250 Network
*network
= userdata
;
1251 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1260 r
= route_new_static(network
, filename
, section_line
, &n
);
1264 r
= parse_boolean(rvalue
);
1266 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1267 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1271 n
->gateway_onlink
= r
;
1277 int config_parse_ipv6_route_preference(
1279 const char *filename
,
1281 const char *section
,
1282 unsigned section_line
,
1289 Network
*network
= userdata
;
1290 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1293 r
= route_new_static(network
, filename
, section_line
, &n
);
1297 if (streq(rvalue
, "low"))
1298 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1299 else if (streq(rvalue
, "medium"))
1300 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1301 else if (streq(rvalue
, "high"))
1302 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1304 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1312 int config_parse_route_protocol(
1314 const char *filename
,
1316 const char *section
,
1317 unsigned section_line
,
1324 Network
*network
= userdata
;
1325 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1328 r
= route_new_static(network
, filename
, section_line
, &n
);
1332 r
= route_protocol_from_string(rvalue
);
1336 r
= safe_atou8(rvalue
, &n
->protocol
);
1338 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1339 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1348 int config_parse_route_type(
1350 const char *filename
,
1352 const char *section
,
1353 unsigned section_line
,
1360 Network
*network
= userdata
;
1361 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1364 r
= route_new_static(network
, filename
, section_line
, &n
);
1368 t
= route_type_from_string(rvalue
);
1370 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1371 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1375 n
->type
= (unsigned char) t
;
1381 int config_parse_tcp_window(
1383 const char *filename
,
1385 const char *section
,
1386 unsigned section_line
,
1393 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1394 Network
*network
= userdata
;
1404 r
= route_new_static(network
, filename
, section_line
, &n
);
1408 r
= parse_size(rvalue
, 1024, &k
);
1410 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1411 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1414 if (k
> UINT32_MAX
) {
1415 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1416 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1420 if (streq(lvalue
, "InitialCongestionWindow"))
1422 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1425 assert_not_reached("Invalid TCP window type.");
1431 int config_parse_quickack(
1433 const char *filename
,
1435 const char *section
,
1436 unsigned section_line
,
1443 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1444 Network
*network
= userdata
;
1453 r
= route_new_static(network
, filename
, section_line
, &n
);
1457 k
= parse_boolean(rvalue
);
1459 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1460 "Failed to parse TCP quickack, ignoring: %s", rvalue
);
1469 int config_parse_fast_open_no_cookie(
1471 const char *filename
,
1473 const char *section
,
1474 unsigned section_line
,
1481 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1482 Network
*network
= userdata
;
1491 r
= route_new_static(network
, filename
, section_line
, &n
);
1495 k
= parse_boolean(rvalue
);
1497 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1498 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue
);
1502 n
->fast_open_no_cookie
= k
;
1507 int config_parse_route_mtu(
1509 const char *filename
,
1511 const char *section
,
1512 unsigned section_line
,
1519 Network
*network
= userdata
;
1520 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1529 r
= route_new_static(network
, filename
, section_line
, &n
);
1533 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1541 int config_parse_route_ttl_propagate(
1543 const char *filename
,
1545 const char *section
,
1546 unsigned section_line
,
1553 Network
*network
= userdata
;
1554 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1563 r
= route_new_static(network
, filename
, section_line
, &n
);
1567 k
= parse_boolean(rvalue
);
1569 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1570 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue
);
1574 n
->ttl_propagate
= k
;
1580 int config_parse_multipath_route(
1582 const char *filename
,
1584 const char *section
,
1585 unsigned section_line
,
1592 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1593 _cleanup_free_
char *word
= NULL
, *buf
= NULL
;
1594 _cleanup_free_ MultipathRoute
*m
= NULL
;
1595 Network
*network
= userdata
;
1596 const char *p
, *ip
, *dev
;
1597 union in_addr_union a
;
1606 r
= route_new_static(network
, filename
, section_line
, &n
);
1610 if (isempty(rvalue
)) {
1611 n
->multipath_routes
= ordered_set_free_free(n
->multipath_routes
);
1615 m
= new0(MultipathRoute
, 1);
1620 r
= extract_first_word(&p
, &word
, NULL
, 0);
1624 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1625 "Invalid multipath route option, ignoring assignment: %s", rvalue
);
1629 dev
= strchr(word
, '@');
1631 buf
= strndup(word
, dev
- word
);
1639 r
= in_addr_from_string_auto(ip
, &family
, &a
);
1641 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1642 "Invalid multipath route gateway '%s', ignoring assignment: %m", rvalue
);
1645 m
->gateway
.address
= a
;
1646 m
->gateway
.family
= family
;
1649 r
= parse_ifindex_or_ifname(dev
, &m
->ifindex
);
1651 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1652 "Invalid interface name or index, ignoring assignment: %s", dev
);
1658 r
= safe_atou32(p
, &m
->weight
);
1660 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1661 "Invalid multipath route weight, ignoring assignment: %s", p
);
1664 if (m
->weight
== 0 || m
->weight
> 256) {
1665 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1666 "Invalid multipath route weight, ignoring assignment: %s", p
);
1671 r
= ordered_set_ensure_allocated(&n
->multipath_routes
, NULL
);
1675 r
= ordered_set_put(n
->multipath_routes
, m
);
1677 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1678 "Failed to store multipath route, ignoring assignment: %m");
1687 int route_section_verify(Route
*route
, Network
*network
) {
1688 if (section_is_invalid(route
->section
))
1691 if (route
->family
== AF_UNSPEC
) {
1692 assert(route
->section
);
1694 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1695 "%s: Route section without Gateway=, Destination=, Source=, "
1696 "or PreferredSource= field configured. "
1697 "Ignoring [Route] section from line %u.",
1698 route
->section
->filename
, route
->section
->line
);
1701 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1702 route
->table
= RT_TABLE_LOCAL
;
1704 if (!route
->scope_set
&& route
->family
!= AF_INET6
) {
1705 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1706 route
->scope
= RT_SCOPE_HOST
;
1707 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_MULTICAST
))
1708 route
->scope
= RT_SCOPE_LINK
;
1711 if (network
->n_static_addresses
== 0 &&
1712 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1713 route
->gateway_onlink
< 0) {
1714 log_warning("%s: Gateway= without static address configured. "
1715 "Enabling GatewayOnLink= option.",
1717 route
->gateway_onlink
= true;