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 sd_event_source_unref(route
->expire
);
152 static void route_hash_func(const Route
*route
, struct siphash
*state
) {
155 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
157 switch (route
->family
) {
160 /* Equality of routes are given by the 4-touple
161 (dst_prefix,dst_prefixlen,tos,priority,table) */
162 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
163 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
164 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
165 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
166 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
170 /* treat any other address family as AF_UNSPEC */
175 static int route_compare_func(const Route
*a
, const Route
*b
) {
178 r
= CMP(a
->family
, b
->family
);
185 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
189 r
= CMP(a
->tos
, b
->tos
);
193 r
= CMP(a
->priority
, b
->priority
);
197 r
= CMP(a
->table
, b
->table
);
201 r
= memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
205 return memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
207 /* treat any other address family as AF_UNSPEC */
212 DEFINE_PRIVATE_HASH_OPS(route_hash_ops
, Route
, route_hash_func
, route_compare_func
);
214 static void route_full_hash_func(const Route
*route
, struct siphash
*state
) {
217 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
219 switch (route
->family
) {
222 siphash24_compress(&route
->gw
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
223 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
224 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
225 siphash24_compress(&route
->src
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
226 siphash24_compress(&route
->src_prefixlen
, sizeof(route
->src_prefixlen
), state
);
227 siphash24_compress(&route
->prefsrc
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
229 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
230 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
231 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
232 siphash24_compress(&route
->protocol
, sizeof(route
->protocol
), state
);
233 siphash24_compress(&route
->scope
, sizeof(route
->scope
), state
);
234 siphash24_compress(&route
->type
, sizeof(route
->type
), state
);
238 /* treat any other address family as AF_UNSPEC */
243 static int route_full_compare_func(const Route
*a
, const Route
*b
) {
246 r
= CMP(a
->family
, b
->family
);
253 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
257 r
= CMP(a
->src_prefixlen
, b
->src_prefixlen
);
261 r
= CMP(a
->tos
, b
->tos
);
265 r
= CMP(a
->priority
, b
->priority
);
269 r
= CMP(a
->table
, b
->table
);
273 r
= CMP(a
->protocol
, b
->protocol
);
277 r
= CMP(a
->scope
, b
->scope
);
281 r
= CMP(a
->type
, b
->type
);
285 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
289 r
= memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
293 r
= memcmp(&a
->src
, &b
->src
, FAMILY_ADDRESS_SIZE(a
->family
));
297 return memcmp(&a
->prefsrc
, &b
->prefsrc
, FAMILY_ADDRESS_SIZE(a
->family
));
299 /* treat any other address family as AF_UNSPEC */
304 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
307 route_full_hash_func
,
308 route_full_compare_func
,
311 bool route_equal(Route
*r1
, Route
*r2
) {
318 return route_compare_func(r1
, r2
) == 0;
321 int route_get(Link
*link
,
323 const union in_addr_union
*dst
,
324 unsigned char dst_prefixlen
,
325 const union in_addr_union
*gw
,
331 Route route
, *existing
;
339 .dst_prefixlen
= dst_prefixlen
,
340 .gw
= gw
? *gw
: IN_ADDR_NULL
,
342 .priority
= priority
,
346 existing
= set_get(link
->routes
, &route
);
353 existing
= set_get(link
->routes_foreign
, &route
);
363 static int route_add_internal(
367 const union in_addr_union
*dst
,
368 unsigned char dst_prefixlen
,
369 const union in_addr_union
*gw
,
375 _cleanup_(route_freep
) Route
*route
= NULL
;
382 r
= route_new(&route
);
386 route
->family
= family
;
388 route
->dst_prefixlen
= dst_prefixlen
;
390 route
->gw
= gw
? *gw
: IN_ADDR_NULL
;
392 route
->priority
= priority
;
393 route
->table
= table
;
395 r
= set_ensure_allocated(routes
, &route_hash_ops
);
399 r
= set_put(*routes
, route
);
415 int route_add_foreign(
418 const union in_addr_union
*dst
,
419 unsigned char dst_prefixlen
,
420 const union in_addr_union
*gw
,
426 return route_add_internal(link
, &link
->routes_foreign
, family
, dst
, dst_prefixlen
, gw
, tos
, priority
, table
, ret
);
429 int route_add(Link
*link
,
431 const union in_addr_union
*dst
,
432 unsigned char dst_prefixlen
,
433 const union in_addr_union
*gw
,
442 r
= route_get(link
, family
, dst
, dst_prefixlen
, gw
, tos
, priority
, table
, &route
);
444 /* Route does not exist, create a new one */
445 r
= route_add_internal(link
, &link
->routes
, family
, dst
, dst_prefixlen
, gw
, tos
, priority
, table
, &route
);
449 /* Take over a foreign route */
450 r
= set_ensure_allocated(&link
->routes
, &route_hash_ops
);
454 r
= set_put(link
->routes
, route
);
458 set_remove(link
->routes_foreign
, route
);
460 /* Route exists, do nothing */
471 void route_update(Route
*route
,
472 const union in_addr_union
*src
,
473 unsigned char src_prefixlen
,
474 const union in_addr_union
*gw
,
475 const union in_addr_union
*prefsrc
,
477 unsigned char protocol
,
478 unsigned char type
) {
481 assert(src
|| src_prefixlen
== 0);
483 route
->src
= src
? *src
: IN_ADDR_NULL
;
484 route
->src_prefixlen
= src_prefixlen
;
485 route
->gw
= gw
? *gw
: IN_ADDR_NULL
;
486 route
->prefsrc
= prefsrc
? *prefsrc
: IN_ADDR_NULL
;
487 route
->scope
= scope
;
488 route
->protocol
= protocol
;
492 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
497 assert(link
->ifname
);
499 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
502 r
= sd_netlink_message_get_errno(m
);
503 if (r
< 0 && r
!= -ESRCH
)
504 log_link_warning_errno(link
, r
, "Could not drop route: %m");
509 int route_remove(Route
*route
, Link
*link
,
510 link_netlink_message_handler_t callback
) {
512 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
516 assert(link
->manager
);
517 assert(link
->manager
->rtnl
);
518 assert(link
->ifindex
> 0);
519 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
521 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
522 RTM_DELROUTE
, route
->family
,
525 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
528 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
529 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
531 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
532 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
533 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
535 if (!in_addr_is_null(route
->family
, &route
->src
))
536 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
537 if (!in_addr_is_null(route
->family
, &route
->gw
))
538 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
539 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
540 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
542 log_link_debug(link
, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
543 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
544 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
545 format_route_table(route
->table
, table
, sizeof(table
)),
546 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
547 strna(route_type_to_string(route
->type
)));
550 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
551 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
553 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
556 if (route
->dst_prefixlen
) {
557 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
559 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
561 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
563 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
566 if (route
->src_prefixlen
) {
567 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
569 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
571 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
573 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
576 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
577 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
579 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
582 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
584 return log_link_error_errno(link
, r
, "Could not set scope: %m");
586 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
588 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
590 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
591 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
593 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
596 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
597 callback
?: route_remove_handler
,
598 link_netlink_destroy_callback
, link
);
600 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
607 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
608 Route
*route
= userdata
;
613 r
= route_remove(route
, route
->link
, NULL
);
615 log_warning_errno(r
, "Could not remove route: %m");
625 link_netlink_message_handler_t callback
) {
627 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
628 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
633 assert(link
->manager
);
634 assert(link
->manager
->rtnl
);
635 assert(link
->ifindex
> 0);
636 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
639 if (route
->family
== AF_INET6
&& link_sysctl_ipv6_enabled(link
) == 0) {
640 log_link_warning(link
, "An IPv6 route is requested, but IPv6 is disabled by sysctl, ignoring.");
644 if (route_get(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, &route
->gw
, route
->tos
, route
->priority
, route
->table
, NULL
) <= 0 &&
645 set_size(link
->routes
) >= routes_max())
646 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
647 "Too many routes are configured, refusing: %m");
650 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
651 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
653 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
654 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
655 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
657 if (!in_addr_is_null(route
->family
, &route
->src
))
658 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
659 if (!in_addr_is_null(route
->family
, &route
->gw
))
660 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
661 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
662 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
664 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
665 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
666 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
667 format_route_table(route
->table
, table
, sizeof(table
)),
668 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
669 strna(route_type_to_string(route
->type
)));
672 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
673 RTM_NEWROUTE
, route
->family
,
676 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
678 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
679 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
681 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
683 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
685 return log_link_error_errno(link
, r
, "Could not set route family: %m");
688 if (route
->dst_prefixlen
) {
689 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
691 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
693 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
695 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
698 if (route
->src_prefixlen
) {
699 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
701 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
703 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
705 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
708 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
709 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
711 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
714 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
716 return log_link_error_errno(link
, r
, "Could not set scope: %m");
718 if (route
->gateway_onlink
>= 0)
719 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
721 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
723 return log_link_error_errno(link
, r
, "Could not set flags: %m");
725 if (route
->table
!= RT_TABLE_MAIN
) {
726 if (route
->table
< 256) {
727 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
729 return log_link_error_errno(link
, r
, "Could not set route table: %m");
731 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
733 return log_link_error_errno(link
, r
, "Could not set route table: %m");
735 /* Table attribute to allow more than 256. */
736 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
738 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
742 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
744 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
746 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
748 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
750 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
751 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
752 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
754 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
757 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
759 return log_link_error_errno(link
, r
, "Could not set route type: %m");
761 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
762 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
764 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
767 if (route
->ttl_propagate
>= 0) {
768 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
770 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
773 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
775 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
777 if (route
->mtu
> 0) {
778 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
780 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
783 if (route
->initcwnd
> 0) {
784 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
786 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
789 if (route
->initrwnd
> 0) {
790 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
792 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
795 if (route
->quickack
>= 0) {
796 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
798 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
801 if (route
->fast_open_no_cookie
>= 0) {
802 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
804 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
807 r
= sd_netlink_message_close_container(req
);
809 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
811 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
812 link_netlink_destroy_callback
, link
);
814 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
818 lifetime
= route
->lifetime
;
820 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, &route
->gw
, route
->tos
, route
->priority
, route
->table
, &route
);
822 return log_link_error_errno(link
, r
, "Could not add route: %m");
824 /* TODO: drop expiration handling once it can be pushed into the kernel */
825 route
->lifetime
= lifetime
;
827 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
828 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
829 route
->lifetime
, 0, route_expire_handler
, route
);
831 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
834 sd_event_source_unref(route
->expire
);
835 route
->expire
= TAKE_PTR(expire
);
840 int network_add_ipv4ll_route(Network
*network
) {
841 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
846 if (!network
->ipv4ll_route
)
849 /* IPv4LLRoute= is in [Network] section. */
850 r
= route_new_static(network
, NULL
, 0, &n
);
854 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
859 n
->dst_prefixlen
= 16;
860 n
->scope
= RT_SCOPE_LINK
;
863 n
->priority
= IPV4LL_ROUTE_METRIC
;
864 n
->protocol
= RTPROT_STATIC
;
870 int network_add_default_route_on_device(Network
*network
) {
871 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
876 if (!network
->default_route_on_device
)
879 /* DefaultRouteOnDevice= is in [Network] section. */
880 r
= route_new_static(network
, NULL
, 0, &n
);
884 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
894 static const char * const route_type_table
[__RTN_MAX
] = {
895 [RTN_UNICAST
] = "unicast",
896 [RTN_LOCAL
] = "local",
897 [RTN_BROADCAST
] = "broadcast",
898 [RTN_ANYCAST
] = "anycast",
899 [RTN_MULTICAST
] = "multicast",
900 [RTN_BLACKHOLE
] = "blackhole",
901 [RTN_UNREACHABLE
] = "unreachable",
902 [RTN_PROHIBIT
] = "prohibit",
903 [RTN_THROW
] = "throw",
905 [RTN_XRESOLVE
] = "xresolve",
908 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
909 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
911 static const char * const route_scope_table
[] = {
912 [RT_SCOPE_UNIVERSE
] = "global",
913 [RT_SCOPE_SITE
] = "site",
914 [RT_SCOPE_LINK
] = "link",
915 [RT_SCOPE_HOST
] = "host",
916 [RT_SCOPE_NOWHERE
] = "nowhere",
919 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
921 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
925 s
= route_scope_to_string(scope
);
927 strpcpy(&p
, size
, s
);
929 strpcpyf(&p
, size
, "%d", scope
);
934 static const char * const route_table_table
[] = {
935 [RT_TABLE_DEFAULT
] = "default",
936 [RT_TABLE_MAIN
] = "main",
937 [RT_TABLE_LOCAL
] = "local",
940 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
942 const char *format_route_table(int table
, char *buf
, size_t size
) {
946 s
= route_table_to_string(table
);
948 strpcpy(&p
, size
, s
);
950 strpcpyf(&p
, size
, "%d", table
);
955 static const char * const route_protocol_table
[] = {
956 [RTPROT_KERNEL
] = "kernel",
957 [RTPROT_BOOT
] = "boot",
958 [RTPROT_STATIC
] = "static",
961 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol
, int);
963 static const char * const route_protocol_full_table
[] = {
964 [RTPROT_REDIRECT
] = "redirect",
965 [RTPROT_KERNEL
] = "kernel",
966 [RTPROT_BOOT
] = "boot",
967 [RTPROT_STATIC
] = "static",
968 [RTPROT_GATED
] = "gated",
970 [RTPROT_MRT
] = "mrt",
971 [RTPROT_ZEBRA
] = "zebra",
972 [RTPROT_BIRD
] = "bird",
973 [RTPROT_DNROUTED
] = "dnrouted",
974 [RTPROT_XORP
] = "xorp",
975 [RTPROT_NTK
] = "ntk",
976 [RTPROT_DHCP
] = "dhcp",
977 [RTPROT_MROUTED
] = "mrouted",
978 [RTPROT_BABEL
] = "babel",
979 [RTPROT_BGP
] = "bgp",
980 [RTPROT_ISIS
] = "isis",
981 [RTPROT_OSPF
] = "ospf",
982 [RTPROT_RIP
] = "rip",
983 [RTPROT_EIGRP
] = "eigrp",
986 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full
, int);
988 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
992 s
= route_protocol_full_to_string(protocol
);
994 strpcpy(&p
, size
, s
);
996 strpcpyf(&p
, size
, "%d", protocol
);
1001 int config_parse_gateway(
1003 const char *filename
,
1005 const char *section
,
1006 unsigned section_line
,
1013 Network
*network
= userdata
;
1014 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1023 if (streq(section
, "Network")) {
1024 /* we are not in an Route section, so treat
1025 * this as the special '0' section */
1026 r
= route_new_static(network
, NULL
, 0, &n
);
1028 r
= route_new_static(network
, filename
, section_line
, &n
);
1032 if (n
->family
== AF_UNSPEC
)
1033 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1035 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
1037 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1038 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1046 int config_parse_preferred_src(
1048 const char *filename
,
1050 const char *section
,
1051 unsigned section_line
,
1058 Network
*network
= userdata
;
1059 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1068 r
= route_new_static(network
, filename
, section_line
, &n
);
1072 if (n
->family
== AF_UNSPEC
)
1073 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
1075 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
1077 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1078 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1086 int config_parse_destination(
1088 const char *filename
,
1090 const char *section
,
1091 unsigned section_line
,
1098 Network
*network
= userdata
;
1099 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1100 union in_addr_union
*buffer
;
1101 unsigned char *prefixlen
;
1110 r
= route_new_static(network
, filename
, section_line
, &n
);
1114 if (streq(lvalue
, "Destination")) {
1116 prefixlen
= &n
->dst_prefixlen
;
1117 } else if (streq(lvalue
, "Source")) {
1119 prefixlen
= &n
->src_prefixlen
;
1121 assert_not_reached(lvalue
);
1123 if (n
->family
== AF_UNSPEC
)
1124 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
1126 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
1128 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1129 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1137 int config_parse_route_priority(
1139 const char *filename
,
1141 const char *section
,
1142 unsigned section_line
,
1149 Network
*network
= userdata
;
1150 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1159 r
= route_new_static(network
, filename
, section_line
, &n
);
1163 r
= safe_atou32(rvalue
, &n
->priority
);
1165 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1166 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1174 int config_parse_route_scope(
1176 const char *filename
,
1178 const char *section
,
1179 unsigned section_line
,
1186 Network
*network
= userdata
;
1187 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1196 r
= route_new_static(network
, filename
, section_line
, &n
);
1200 r
= route_scope_from_string(rvalue
);
1202 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1207 n
->scope_set
= true;
1212 int config_parse_route_table(
1214 const char *filename
,
1216 const char *section
,
1217 unsigned section_line
,
1224 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1225 Network
*network
= userdata
;
1234 r
= route_new_static(network
, filename
, section_line
, &n
);
1238 r
= route_table_from_string(rvalue
);
1242 r
= safe_atou32(rvalue
, &n
->table
);
1244 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1245 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1250 n
->table_set
= true;
1255 int config_parse_gateway_onlink(
1257 const char *filename
,
1259 const char *section
,
1260 unsigned section_line
,
1267 Network
*network
= userdata
;
1268 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1277 r
= route_new_static(network
, filename
, section_line
, &n
);
1281 r
= parse_boolean(rvalue
);
1283 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1284 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1288 n
->gateway_onlink
= r
;
1294 int config_parse_ipv6_route_preference(
1296 const char *filename
,
1298 const char *section
,
1299 unsigned section_line
,
1306 Network
*network
= userdata
;
1307 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1310 r
= route_new_static(network
, filename
, section_line
, &n
);
1314 if (streq(rvalue
, "low"))
1315 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1316 else if (streq(rvalue
, "medium"))
1317 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1318 else if (streq(rvalue
, "high"))
1319 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1321 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1329 int config_parse_route_protocol(
1331 const char *filename
,
1333 const char *section
,
1334 unsigned section_line
,
1341 Network
*network
= userdata
;
1342 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1345 r
= route_new_static(network
, filename
, section_line
, &n
);
1349 r
= route_protocol_from_string(rvalue
);
1353 r
= safe_atou8(rvalue
, &n
->protocol
);
1355 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1356 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1365 int config_parse_route_type(
1367 const char *filename
,
1369 const char *section
,
1370 unsigned section_line
,
1377 Network
*network
= userdata
;
1378 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1381 r
= route_new_static(network
, filename
, section_line
, &n
);
1385 t
= route_type_from_string(rvalue
);
1387 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1388 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1392 n
->type
= (unsigned char) t
;
1398 int config_parse_tcp_window(
1400 const char *filename
,
1402 const char *section
,
1403 unsigned section_line
,
1410 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1411 Network
*network
= userdata
;
1421 r
= route_new_static(network
, filename
, section_line
, &n
);
1425 r
= parse_size(rvalue
, 1024, &k
);
1427 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1428 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1431 if (k
> UINT32_MAX
) {
1432 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1433 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1437 if (streq(lvalue
, "InitialCongestionWindow"))
1439 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1442 assert_not_reached("Invalid TCP window type.");
1448 int config_parse_quickack(
1450 const char *filename
,
1452 const char *section
,
1453 unsigned section_line
,
1460 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1461 Network
*network
= userdata
;
1470 r
= route_new_static(network
, filename
, section_line
, &n
);
1474 k
= parse_boolean(rvalue
);
1476 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1477 "Failed to parse TCP quickack, ignoring: %s", rvalue
);
1486 int config_parse_fast_open_no_cookie(
1488 const char *filename
,
1490 const char *section
,
1491 unsigned section_line
,
1498 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1499 Network
*network
= userdata
;
1508 r
= route_new_static(network
, filename
, section_line
, &n
);
1512 k
= parse_boolean(rvalue
);
1514 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1515 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue
);
1519 n
->fast_open_no_cookie
= k
;
1524 int config_parse_route_mtu(
1526 const char *filename
,
1528 const char *section
,
1529 unsigned section_line
,
1536 Network
*network
= userdata
;
1537 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1546 r
= route_new_static(network
, filename
, section_line
, &n
);
1550 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1558 int config_parse_route_ttl_propagate(
1560 const char *filename
,
1562 const char *section
,
1563 unsigned section_line
,
1570 Network
*network
= userdata
;
1571 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1580 r
= route_new_static(network
, filename
, section_line
, &n
);
1584 k
= parse_boolean(rvalue
);
1586 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1587 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue
);
1591 n
->ttl_propagate
= k
;
1597 int route_section_verify(Route
*route
, Network
*network
) {
1598 if (section_is_invalid(route
->section
))
1601 if (route
->family
== AF_UNSPEC
) {
1602 assert(route
->section
);
1604 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1605 "%s: Route section without Gateway=, Destination=, Source=, "
1606 "or PreferredSource= field configured. "
1607 "Ignoring [Route] section from line %u.",
1608 route
->section
->filename
, route
->section
->line
);
1611 if (route
->family
!= AF_INET6
) {
1612 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1613 route
->table
= RT_TABLE_LOCAL
;
1615 if (!route
->scope_set
) {
1616 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1617 route
->scope
= RT_SCOPE_HOST
;
1618 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
))
1619 route
->scope
= RT_SCOPE_LINK
;
1623 if (network
->n_static_addresses
== 0 &&
1624 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1625 route
->gateway_onlink
< 0) {
1626 log_warning("%s: Gateway= without static address configured. "
1627 "Enabling GatewayOnLink= option.",
1629 route
->gateway_onlink
= true;