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 "socket-netlink.h"
16 #include "string-table.h"
17 #include "string-util.h"
20 #include "sysctl-util.h"
23 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
25 static unsigned routes_max(void) {
26 static thread_local
unsigned cached
= 0;
28 _cleanup_free_
char *s4
= NULL
, *s6
= NULL
;
29 unsigned val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
34 if (sysctl_read("net/ipv4/route/max_size", &s4
) >= 0) {
36 if (safe_atou(s4
, &val4
) >= 0 &&
38 /* This is the default "no limit" value in the kernel */
39 val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
42 if (sysctl_read("net/ipv6/route/max_size", &s6
) >= 0) {
44 (void) safe_atou(s6
, &val6
);
47 cached
= MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val4
) +
48 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
);
52 int route_new(Route
**ret
) {
53 _cleanup_(route_freep
) Route
*route
= NULL
;
55 route
= new(Route
, 1);
61 .scope
= RT_SCOPE_UNIVERSE
,
62 .protocol
= RTPROT_UNSPEC
,
64 .table
= RT_TABLE_MAIN
,
65 .lifetime
= USEC_INFINITY
,
67 .fast_open_no_cookie
= -1,
72 *ret
= TAKE_PTR(route
);
77 static int route_new_static(Network
*network
, const char *filename
, unsigned section_line
, Route
**ret
) {
78 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
79 _cleanup_(route_freep
) Route
*route
= NULL
;
84 assert(!!filename
== (section_line
> 0));
87 r
= network_config_section_new(filename
, section_line
, &n
);
91 route
= hashmap_get(network
->routes_by_section
, n
);
93 *ret
= TAKE_PTR(route
);
99 if (network
->n_static_routes
>= routes_max())
102 r
= route_new(&route
);
106 route
->protocol
= RTPROT_STATIC
;
107 route
->network
= network
;
108 LIST_PREPEND(routes
, network
->static_routes
, route
);
109 network
->n_static_routes
++;
112 route
->section
= TAKE_PTR(n
);
114 r
= hashmap_ensure_allocated(&network
->routes_by_section
, &network_config_hash_ops
);
118 r
= hashmap_put(network
->routes_by_section
, route
->section
, route
);
123 *ret
= TAKE_PTR(route
);
128 void route_free(Route
*route
) {
132 if (route
->network
) {
133 LIST_REMOVE(routes
, route
->network
->static_routes
, route
);
135 assert(route
->network
->n_static_routes
> 0);
136 route
->network
->n_static_routes
--;
139 hashmap_remove(route
->network
->routes_by_section
, route
->section
);
142 network_config_section_free(route
->section
);
145 set_remove(route
->link
->routes
, route
);
146 set_remove(route
->link
->routes_foreign
, route
);
149 ordered_set_free_free(route
->multipath_routes
);
151 sd_event_source_unref(route
->expire
);
156 static void route_hash_func(const Route
*route
, struct siphash
*state
) {
159 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
161 switch (route
->family
) {
164 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
165 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
167 siphash24_compress(&route
->src_prefixlen
, sizeof(route
->src_prefixlen
), state
);
168 siphash24_compress(&route
->src
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
170 siphash24_compress(&route
->gw
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
172 siphash24_compress(&route
->prefsrc
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
174 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
175 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
176 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
177 siphash24_compress(&route
->protocol
, sizeof(route
->protocol
), state
);
178 siphash24_compress(&route
->scope
, sizeof(route
->scope
), state
);
179 siphash24_compress(&route
->type
, sizeof(route
->type
), state
);
181 siphash24_compress(&route
->initcwnd
, sizeof(route
->initcwnd
), state
);
182 siphash24_compress(&route
->initrwnd
, sizeof(route
->initrwnd
), state
);
186 /* treat any other address family as AF_UNSPEC */
191 static int route_compare_func(const Route
*a
, const Route
*b
) {
194 r
= CMP(a
->family
, b
->family
);
201 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
205 r
= memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
209 r
= CMP(a
->src_prefixlen
, b
->src_prefixlen
);
213 r
= memcmp(&a
->src
, &b
->src
, FAMILY_ADDRESS_SIZE(a
->family
));
217 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
221 r
= memcmp(&a
->prefsrc
, &b
->prefsrc
, FAMILY_ADDRESS_SIZE(a
->family
));
225 r
= CMP(a
->tos
, b
->tos
);
229 r
= CMP(a
->priority
, b
->priority
);
233 r
= CMP(a
->table
, b
->table
);
237 r
= CMP(a
->protocol
, b
->protocol
);
241 r
= CMP(a
->scope
, b
->scope
);
245 r
= CMP(a
->type
, b
->type
);
249 r
= CMP(a
->initcwnd
, b
->initcwnd
);
253 r
= CMP(a
->initrwnd
, b
->initrwnd
);
259 /* treat any other address family as AF_UNSPEC */
264 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
271 bool route_equal(Route
*r1
, Route
*r2
) {
278 return route_compare_func(r1
, r2
) == 0;
281 int route_get(Link
*link
, Route
*in
, Route
**ret
) {
288 existing
= set_get(link
->routes
, in
);
295 existing
= set_get(link
->routes_foreign
, in
);
305 static int route_add_internal(Link
*link
, Set
**routes
, Route
*in
, Route
**ret
) {
307 _cleanup_(route_freep
) Route
*route
= NULL
;
314 r
= route_new(&route
);
318 route
->family
= in
->family
;
319 route
->src
= in
->src
;
320 route
->src_prefixlen
= in
->src_prefixlen
;
321 route
->dst
= in
->dst
;
322 route
->dst_prefixlen
= in
->dst_prefixlen
;
324 route
->prefsrc
= in
->prefsrc
;
325 route
->scope
= in
->scope
;
326 route
->protocol
= in
->protocol
;
327 route
->type
= in
->type
;
328 route
->tos
= in
->tos
;
329 route
->priority
= in
->priority
;
330 route
->table
= in
->table
;
331 route
->initcwnd
= in
->initcwnd
;
332 route
->initrwnd
= in
->initrwnd
;
333 route
->lifetime
= in
->lifetime
;
335 r
= set_ensure_put(routes
, &route_hash_ops
, route
);
351 int route_add_foreign(Link
*link
, Route
*in
, Route
**ret
) {
352 return route_add_internal(link
, &link
->routes_foreign
, in
, ret
);
355 int route_add(Link
*link
, Route
*in
, Route
**ret
) {
360 r
= route_get(link
, in
, &route
);
362 /* Route does not exist, create a new one */
363 r
= route_add_internal(link
, &link
->routes
, in
, &route
);
367 /* Take over a foreign route */
368 r
= set_ensure_put(&link
->routes
, &route_hash_ops
, route
);
372 set_remove(link
->routes_foreign
, route
);
374 /* Route exists, do nothing */
385 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
390 assert(link
->ifname
);
392 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
395 r
= sd_netlink_message_get_errno(m
);
396 if (r
< 0 && r
!= -ESRCH
)
397 log_link_message_warning_errno(link
, m
, r
, "Could not drop route, ignoring");
402 int route_remove(Route
*route
, Link
*link
,
403 link_netlink_message_handler_t callback
) {
405 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
409 assert(link
->manager
);
410 assert(link
->manager
->rtnl
);
411 assert(link
->ifindex
> 0);
412 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
414 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
415 RTM_DELROUTE
, route
->family
,
418 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
421 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
422 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
424 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
425 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
426 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
428 if (!in_addr_is_null(route
->family
, &route
->src
))
429 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
430 if (!in_addr_is_null(route
->family
, &route
->gw
))
431 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
432 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
433 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
435 log_link_debug(link
, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
436 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
437 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
438 format_route_table(route
->table
, table
, sizeof(table
)),
439 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
440 strna(route_type_to_string(route
->type
)));
443 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
444 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
446 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
449 if (route
->dst_prefixlen
) {
450 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
452 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
454 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
456 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
459 if (route
->src_prefixlen
) {
460 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
462 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
464 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
466 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
469 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
470 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
472 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
475 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
477 return log_link_error_errno(link
, r
, "Could not set scope: %m");
479 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
481 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
483 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
484 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
486 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
489 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
490 callback
?: route_remove_handler
,
491 link_netlink_destroy_callback
, link
);
493 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
500 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
501 Route
*route
= userdata
;
506 r
= route_remove(route
, route
->link
, NULL
);
508 log_link_warning_errno(route
->link
, r
, "Could not remove route: %m");
515 static int append_nexthop_one(Route
*route
, MultipathRoute
*m
, struct rtattr
**rta
, size_t offset
) {
516 struct rtnexthop
*rtnh
;
517 struct rtattr
*new_rta
;
525 new_rta
= realloc(*rta
, RTA_ALIGN((*rta
)->rta_len
) + RTA_SPACE(sizeof(struct rtnexthop
)));
530 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
531 *rtnh
= (struct rtnexthop
) {
532 .rtnh_len
= sizeof(*rtnh
),
533 .rtnh_ifindex
= m
->ifindex
,
534 .rtnh_hops
= m
->weight
> 0 ? m
->weight
- 1 : 0,
537 (*rta
)->rta_len
+= sizeof(struct rtnexthop
);
539 if (route
->family
== m
->gateway
.family
) {
540 r
= rtattr_append_attribute(rta
, RTA_GATEWAY
, &m
->gateway
.address
, FAMILY_ADDRESS_SIZE(m
->gateway
.family
));
543 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
544 rtnh
->rtnh_len
+= RTA_SPACE(FAMILY_ADDRESS_SIZE(m
->gateway
.family
));
546 r
= rtattr_append_attribute(rta
, RTA_VIA
, &m
->gateway
, FAMILY_ADDRESS_SIZE(m
->gateway
.family
) + sizeof(m
->gateway
.family
));
549 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
550 rtnh
->rtnh_len
+= RTA_SPACE(FAMILY_ADDRESS_SIZE(m
->gateway
.family
) + sizeof(m
->gateway
.family
));
556 (*rta
)->rta_len
-= sizeof(struct rtnexthop
);
560 static int append_nexthops(Route
*route
, sd_netlink_message
*req
) {
561 _cleanup_free_
struct rtattr
*rta
= NULL
;
562 struct rtnexthop
*rtnh
;
568 if (ordered_set_isempty(route
->multipath_routes
))
571 rta
= new(struct rtattr
, 1);
575 *rta
= (struct rtattr
) {
576 .rta_type
= RTA_MULTIPATH
,
577 .rta_len
= RTA_LENGTH(0),
579 offset
= (uint8_t *) RTA_DATA(rta
) - (uint8_t *) rta
;
581 ORDERED_SET_FOREACH(m
, route
->multipath_routes
, i
) {
582 r
= append_nexthop_one(route
, m
, &rta
, offset
);
586 rtnh
= (struct rtnexthop
*)((uint8_t *) rta
+ offset
);
587 offset
= (uint8_t *) RTNH_NEXT(rtnh
) - (uint8_t *) rta
;
590 r
= sd_netlink_message_append_data(req
, RTA_MULTIPATH
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
600 link_netlink_message_handler_t callback
) {
602 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
603 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
607 assert(link
->manager
);
608 assert(link
->manager
->rtnl
);
609 assert(link
->ifindex
> 0);
610 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
613 if (route_get(link
, route
, NULL
) <= 0 &&
614 set_size(link
->routes
) >= routes_max())
615 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
616 "Too many routes are configured, refusing: %m");
619 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
620 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
622 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
623 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
624 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
626 if (!in_addr_is_null(route
->family
, &route
->src
))
627 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
628 if (!in_addr_is_null(route
->family
, &route
->gw
))
629 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
630 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
631 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
633 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
634 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
635 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
636 format_route_table(route
->table
, table
, sizeof(table
)),
637 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
638 strna(route_type_to_string(route
->type
)));
641 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
642 RTM_NEWROUTE
, route
->family
,
645 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
647 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
648 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
650 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
652 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
654 return log_link_error_errno(link
, r
, "Could not set route family: %m");
657 if (route
->dst_prefixlen
> 0) {
658 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
660 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
662 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
664 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
667 if (route
->src_prefixlen
> 0) {
668 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
670 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
672 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
674 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
677 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
678 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
680 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
683 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
685 return log_link_error_errno(link
, r
, "Could not set scope: %m");
687 if (route
->gateway_onlink
>= 0)
688 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
690 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
692 return log_link_error_errno(link
, r
, "Could not set flags: %m");
694 if (route
->table
!= RT_TABLE_MAIN
) {
695 if (route
->table
< 256) {
696 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
698 return log_link_error_errno(link
, r
, "Could not set route table: %m");
700 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
702 return log_link_error_errno(link
, r
, "Could not set route table: %m");
704 /* Table attribute to allow more than 256. */
705 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
707 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
711 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
713 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
715 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
717 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
719 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
720 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
721 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
723 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
726 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
728 return log_link_error_errno(link
, r
, "Could not set route type: %m");
730 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
731 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
733 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
736 if (route
->ttl_propagate
>= 0) {
737 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
739 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
742 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
744 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
746 if (route
->mtu
> 0) {
747 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
749 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
752 if (route
->initcwnd
> 0) {
753 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
755 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
758 if (route
->initrwnd
> 0) {
759 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
761 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
764 if (route
->quickack
>= 0) {
765 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
767 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
770 if (route
->fast_open_no_cookie
>= 0) {
771 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
773 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
776 r
= sd_netlink_message_close_container(req
);
778 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
780 r
= append_nexthops(route
, req
);
782 return log_link_error_errno(link
, r
, "Could not append RTA_MULTIPATH attribute: %m");
784 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
785 link_netlink_destroy_callback
, link
);
787 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
791 r
= route_add(link
, route
, &route
);
793 return log_link_error_errno(link
, r
, "Could not add route: %m");
795 /* TODO: drop expiration handling once it can be pushed into the kernel */
796 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
797 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
798 route
->lifetime
, 0, route_expire_handler
, route
);
800 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
803 sd_event_source_unref(route
->expire
);
804 route
->expire
= TAKE_PTR(expire
);
809 int network_add_ipv4ll_route(Network
*network
) {
810 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
815 if (!network
->ipv4ll_route
)
818 /* IPv4LLRoute= is in [Network] section. */
819 r
= route_new_static(network
, NULL
, 0, &n
);
823 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
828 n
->dst_prefixlen
= 16;
829 n
->scope
= RT_SCOPE_LINK
;
832 n
->priority
= IPV4LL_ROUTE_METRIC
;
833 n
->protocol
= RTPROT_STATIC
;
839 int network_add_default_route_on_device(Network
*network
) {
840 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
845 if (!network
->default_route_on_device
)
848 /* DefaultRouteOnDevice= is in [Network] section. */
849 r
= route_new_static(network
, NULL
, 0, &n
);
854 n
->scope
= RT_SCOPE_LINK
;
856 n
->protocol
= RTPROT_STATIC
;
862 static const char * const route_type_table
[__RTN_MAX
] = {
863 [RTN_UNICAST
] = "unicast",
864 [RTN_LOCAL
] = "local",
865 [RTN_BROADCAST
] = "broadcast",
866 [RTN_ANYCAST
] = "anycast",
867 [RTN_MULTICAST
] = "multicast",
868 [RTN_BLACKHOLE
] = "blackhole",
869 [RTN_UNREACHABLE
] = "unreachable",
870 [RTN_PROHIBIT
] = "prohibit",
871 [RTN_THROW
] = "throw",
873 [RTN_XRESOLVE
] = "xresolve",
876 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
877 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
879 static const char * const route_scope_table
[] = {
880 [RT_SCOPE_UNIVERSE
] = "global",
881 [RT_SCOPE_SITE
] = "site",
882 [RT_SCOPE_LINK
] = "link",
883 [RT_SCOPE_HOST
] = "host",
884 [RT_SCOPE_NOWHERE
] = "nowhere",
887 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
889 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
893 s
= route_scope_to_string(scope
);
895 strpcpy(&p
, size
, s
);
897 strpcpyf(&p
, size
, "%d", scope
);
902 static const char * const route_table_table
[] = {
903 [RT_TABLE_DEFAULT
] = "default",
904 [RT_TABLE_MAIN
] = "main",
905 [RT_TABLE_LOCAL
] = "local",
908 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
910 const char *format_route_table(int table
, char *buf
, size_t size
) {
914 s
= route_table_to_string(table
);
916 strpcpy(&p
, size
, s
);
918 strpcpyf(&p
, size
, "%d", table
);
923 static const char * const route_protocol_table
[] = {
924 [RTPROT_KERNEL
] = "kernel",
925 [RTPROT_BOOT
] = "boot",
926 [RTPROT_STATIC
] = "static",
929 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol
, int);
931 static const char * const route_protocol_full_table
[] = {
932 [RTPROT_REDIRECT
] = "redirect",
933 [RTPROT_KERNEL
] = "kernel",
934 [RTPROT_BOOT
] = "boot",
935 [RTPROT_STATIC
] = "static",
936 [RTPROT_GATED
] = "gated",
938 [RTPROT_MRT
] = "mrt",
939 [RTPROT_ZEBRA
] = "zebra",
940 [RTPROT_BIRD
] = "bird",
941 [RTPROT_DNROUTED
] = "dnrouted",
942 [RTPROT_XORP
] = "xorp",
943 [RTPROT_NTK
] = "ntk",
944 [RTPROT_DHCP
] = "dhcp",
945 [RTPROT_MROUTED
] = "mrouted",
946 [RTPROT_BABEL
] = "babel",
947 [RTPROT_BGP
] = "bgp",
948 [RTPROT_ISIS
] = "isis",
949 [RTPROT_OSPF
] = "ospf",
950 [RTPROT_RIP
] = "rip",
951 [RTPROT_EIGRP
] = "eigrp",
954 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full
, int);
956 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
960 s
= route_protocol_full_to_string(protocol
);
962 strpcpy(&p
, size
, s
);
964 strpcpyf(&p
, size
, "%d", protocol
);
969 int config_parse_gateway(
971 const char *filename
,
974 unsigned section_line
,
981 Network
*network
= userdata
;
982 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
991 if (streq(section
, "Network")) {
992 /* we are not in an Route section, so treat
993 * this as the special '0' section */
994 r
= route_new_static(network
, NULL
, 0, &n
);
998 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
999 "Failed to allocate route, ignoring assignment: %m");
1003 r
= route_new_static(network
, filename
, section_line
, &n
);
1007 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1008 "Failed to allocate route, ignoring assignment: %m");
1012 if (streq(rvalue
, "_dhcp")) {
1013 n
->gateway_from_dhcp
= true;
1019 if (n
->family
== AF_UNSPEC
)
1020 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1022 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
1024 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1025 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1033 int config_parse_preferred_src(
1035 const char *filename
,
1037 const char *section
,
1038 unsigned section_line
,
1045 Network
*network
= userdata
;
1046 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1055 r
= route_new_static(network
, filename
, section_line
, &n
);
1059 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1060 "Failed to allocate route, ignoring assignment: %m");
1064 if (n
->family
== AF_UNSPEC
)
1065 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
1067 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
1069 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
,
1070 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1078 int config_parse_destination(
1080 const char *filename
,
1082 const char *section
,
1083 unsigned section_line
,
1090 Network
*network
= userdata
;
1091 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1092 union in_addr_union
*buffer
;
1093 unsigned char *prefixlen
;
1102 r
= route_new_static(network
, filename
, section_line
, &n
);
1106 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1107 "Failed to allocate route, ignoring assignment: %m");
1111 if (streq(lvalue
, "Destination")) {
1113 prefixlen
= &n
->dst_prefixlen
;
1114 } else if (streq(lvalue
, "Source")) {
1116 prefixlen
= &n
->src_prefixlen
;
1118 assert_not_reached(lvalue
);
1120 if (n
->family
== AF_UNSPEC
)
1121 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
1123 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
1125 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
,
1126 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1134 int config_parse_route_priority(
1136 const char *filename
,
1138 const char *section
,
1139 unsigned section_line
,
1146 Network
*network
= userdata
;
1147 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1156 r
= route_new_static(network
, filename
, section_line
, &n
);
1160 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1161 "Failed to allocate route, ignoring assignment: %m");
1165 r
= safe_atou32(rvalue
, &n
->priority
);
1167 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1168 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1176 int config_parse_route_scope(
1178 const char *filename
,
1180 const char *section
,
1181 unsigned section_line
,
1188 Network
*network
= userdata
;
1189 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1198 r
= route_new_static(network
, filename
, section_line
, &n
);
1202 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1203 "Failed to allocate route, ignoring assignment: %m");
1207 r
= route_scope_from_string(rvalue
);
1209 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1214 n
->scope_set
= true;
1219 int config_parse_route_table(
1221 const char *filename
,
1223 const char *section
,
1224 unsigned section_line
,
1231 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1232 Network
*network
= userdata
;
1241 r
= route_new_static(network
, filename
, section_line
, &n
);
1245 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1246 "Failed to allocate route, ignoring assignment: %m");
1250 r
= route_table_from_string(rvalue
);
1254 r
= safe_atou32(rvalue
, &n
->table
);
1256 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1257 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1262 n
->table_set
= true;
1267 int config_parse_route_boolean(
1269 const char *filename
,
1271 const char *section
,
1272 unsigned section_line
,
1279 Network
*network
= userdata
;
1280 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1289 r
= route_new_static(network
, filename
, section_line
, &n
);
1293 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1294 "Failed to allocate route, ignoring assignment: %m");
1298 r
= parse_boolean(rvalue
);
1300 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1301 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1305 if (STR_IN_SET(lvalue
, "GatewayOnLink", "GatewayOnlink"))
1306 n
->gateway_onlink
= r
;
1307 else if (streq(lvalue
, "QuickAck"))
1309 else if (streq(lvalue
, "FastOpenNoCookie"))
1310 n
->fast_open_no_cookie
= r
;
1311 else if (streq(lvalue
, "TTLPropagate"))
1312 n
->ttl_propagate
= r
;
1314 assert_not_reached("Invalid lvalue");
1320 int config_parse_ipv6_route_preference(
1322 const char *filename
,
1324 const char *section
,
1325 unsigned section_line
,
1332 Network
*network
= userdata
;
1333 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1336 r
= route_new_static(network
, filename
, section_line
, &n
);
1340 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1341 "Failed to allocate route, ignoring assignment: %m");
1345 if (streq(rvalue
, "low"))
1346 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1347 else if (streq(rvalue
, "medium"))
1348 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1349 else if (streq(rvalue
, "high"))
1350 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1352 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1360 int config_parse_route_protocol(
1362 const char *filename
,
1364 const char *section
,
1365 unsigned section_line
,
1372 Network
*network
= userdata
;
1373 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1376 r
= route_new_static(network
, filename
, section_line
, &n
);
1380 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1381 "Failed to allocate route, ignoring assignment: %m");
1385 r
= route_protocol_from_string(rvalue
);
1389 r
= safe_atou8(rvalue
, &n
->protocol
);
1391 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1392 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1401 int config_parse_route_type(
1403 const char *filename
,
1405 const char *section
,
1406 unsigned section_line
,
1413 Network
*network
= userdata
;
1414 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1417 r
= route_new_static(network
, filename
, section_line
, &n
);
1421 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1422 "Failed to allocate route, ignoring assignment: %m");
1426 t
= route_type_from_string(rvalue
);
1428 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1429 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1433 n
->type
= (unsigned char) t
;
1439 int config_parse_tcp_window(
1441 const char *filename
,
1443 const char *section
,
1444 unsigned section_line
,
1451 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1452 Network
*network
= userdata
;
1462 r
= route_new_static(network
, filename
, section_line
, &n
);
1466 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1467 "Failed to allocate route, ignoring assignment: %m");
1471 r
= parse_size(rvalue
, 1024, &k
);
1473 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1474 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1477 if (k
> UINT32_MAX
) {
1478 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1479 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1483 if (streq(lvalue
, "InitialCongestionWindow"))
1485 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1488 assert_not_reached("Invalid TCP window type.");
1494 int config_parse_route_mtu(
1496 const char *filename
,
1498 const char *section
,
1499 unsigned section_line
,
1506 Network
*network
= userdata
;
1507 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1516 r
= route_new_static(network
, filename
, section_line
, &n
);
1520 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1521 "Failed to allocate route, ignoring assignment: %m");
1525 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1533 int config_parse_multipath_route(
1535 const char *filename
,
1537 const char *section
,
1538 unsigned section_line
,
1545 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1546 _cleanup_free_
char *word
= NULL
, *buf
= NULL
;
1547 _cleanup_free_ MultipathRoute
*m
= NULL
;
1548 Network
*network
= userdata
;
1549 const char *p
, *ip
, *dev
;
1550 union in_addr_union a
;
1559 r
= route_new_static(network
, filename
, section_line
, &n
);
1563 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1564 "Failed to allocate route, ignoring assignment: %m");
1568 if (isempty(rvalue
)) {
1569 n
->multipath_routes
= ordered_set_free_free(n
->multipath_routes
);
1573 m
= new0(MultipathRoute
, 1);
1578 r
= extract_first_word(&p
, &word
, NULL
, 0);
1582 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1583 "Invalid multipath route option, ignoring assignment: %s", rvalue
);
1587 dev
= strchr(word
, '@');
1589 buf
= strndup(word
, dev
- word
);
1597 r
= in_addr_from_string_auto(ip
, &family
, &a
);
1599 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1600 "Invalid multipath route gateway '%s', ignoring assignment: %m", rvalue
);
1603 m
->gateway
.address
= a
;
1604 m
->gateway
.family
= family
;
1607 r
= resolve_interface(NULL
, dev
);
1609 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1610 "Invalid interface name or index, ignoring assignment: %s", dev
);
1617 r
= safe_atou32(p
, &m
->weight
);
1619 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1620 "Invalid multipath route weight, ignoring assignment: %s", p
);
1623 if (m
->weight
== 0 || m
->weight
> 256) {
1624 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1625 "Invalid multipath route weight, ignoring assignment: %s", p
);
1630 r
= ordered_set_ensure_allocated(&n
->multipath_routes
, NULL
);
1634 r
= ordered_set_put(n
->multipath_routes
, m
);
1636 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1637 "Failed to store multipath route, ignoring assignment: %m");
1646 int route_section_verify(Route
*route
, Network
*network
) {
1647 if (section_is_invalid(route
->section
))
1650 if (route
->family
== AF_UNSPEC
) {
1651 assert(route
->section
);
1653 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1654 "%s: Route section without Gateway=, Destination=, Source=, "
1655 "or PreferredSource= field configured. "
1656 "Ignoring [Route] section from line %u.",
1657 route
->section
->filename
, route
->section
->line
);
1660 if (!route
->table_set
&& network
->vrf
) {
1661 route
->table
= VRF(network
->vrf
)->table
;
1662 route
->table_set
= true;
1665 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1666 route
->table
= RT_TABLE_LOCAL
;
1668 if (!route
->scope_set
&& route
->family
!= AF_INET6
) {
1669 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1670 route
->scope
= RT_SCOPE_HOST
;
1671 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_MULTICAST
))
1672 route
->scope
= RT_SCOPE_LINK
;
1675 if (network
->n_static_addresses
== 0 &&
1676 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1677 route
->gateway_onlink
< 0) {
1678 log_warning("%s: Gateway= without static address configured. "
1679 "Enabling GatewayOnLink= option.",
1681 route
->gateway_onlink
= true;