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 return memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
203 /* treat any other address family as AF_UNSPEC */
208 DEFINE_PRIVATE_HASH_OPS(route_hash_ops
, Route
, route_hash_func
, route_compare_func
);
210 static void route_full_hash_func(const Route
*route
, struct siphash
*state
) {
213 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
215 switch (route
->family
) {
218 siphash24_compress(&route
->gw
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
219 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
220 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
221 siphash24_compress(&route
->src
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
222 siphash24_compress(&route
->src_prefixlen
, sizeof(route
->src_prefixlen
), state
);
223 siphash24_compress(&route
->prefsrc
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
225 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
226 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
227 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
228 siphash24_compress(&route
->protocol
, sizeof(route
->protocol
), state
);
229 siphash24_compress(&route
->scope
, sizeof(route
->scope
), state
);
230 siphash24_compress(&route
->type
, sizeof(route
->type
), state
);
234 /* treat any other address family as AF_UNSPEC */
239 static int route_full_compare_func(const Route
*a
, const Route
*b
) {
242 r
= CMP(a
->family
, b
->family
);
249 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
253 r
= CMP(a
->src_prefixlen
, b
->src_prefixlen
);
257 r
= CMP(a
->tos
, b
->tos
);
261 r
= CMP(a
->priority
, b
->priority
);
265 r
= CMP(a
->table
, b
->table
);
269 r
= CMP(a
->protocol
, b
->protocol
);
273 r
= CMP(a
->scope
, b
->scope
);
277 r
= CMP(a
->type
, b
->type
);
281 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
285 r
= memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
289 r
= memcmp(&a
->src
, &b
->src
, FAMILY_ADDRESS_SIZE(a
->family
));
293 return memcmp(&a
->prefsrc
, &b
->prefsrc
, FAMILY_ADDRESS_SIZE(a
->family
));
295 /* treat any other address family as AF_UNSPEC */
300 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
303 route_full_hash_func
,
304 route_full_compare_func
,
307 bool route_equal(Route
*r1
, Route
*r2
) {
314 return route_compare_func(r1
, r2
) == 0;
317 int route_get(Link
*link
,
319 const union in_addr_union
*dst
,
320 unsigned char dst_prefixlen
,
326 Route route
, *existing
;
334 .dst_prefixlen
= dst_prefixlen
,
336 .priority
= priority
,
340 existing
= set_get(link
->routes
, &route
);
347 existing
= set_get(link
->routes_foreign
, &route
);
357 static int route_add_internal(
361 const union in_addr_union
*dst
,
362 unsigned char dst_prefixlen
,
368 _cleanup_(route_freep
) Route
*route
= NULL
;
375 r
= route_new(&route
);
379 route
->family
= family
;
381 route
->dst_prefixlen
= dst_prefixlen
;
383 route
->priority
= priority
;
384 route
->table
= table
;
386 r
= set_ensure_allocated(routes
, &route_hash_ops
);
390 r
= set_put(*routes
, route
);
404 int route_add_foreign(
407 const union in_addr_union
*dst
,
408 unsigned char dst_prefixlen
,
414 return route_add_internal(link
, &link
->routes_foreign
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, ret
);
417 int route_add(Link
*link
,
419 const union in_addr_union
*dst
,
420 unsigned char dst_prefixlen
,
429 r
= route_get(link
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
431 /* Route does not exist, create a new one */
432 r
= route_add_internal(link
, &link
->routes
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
436 /* Take over a foreign route */
437 r
= set_ensure_allocated(&link
->routes
, &route_hash_ops
);
441 r
= set_put(link
->routes
, route
);
445 set_remove(link
->routes_foreign
, route
);
447 /* Route exists, do nothing */
458 void route_update(Route
*route
,
459 const union in_addr_union
*src
,
460 unsigned char src_prefixlen
,
461 const union in_addr_union
*gw
,
462 const union in_addr_union
*prefsrc
,
464 unsigned char protocol
,
465 unsigned char type
) {
468 assert(src
|| src_prefixlen
== 0);
470 route
->src
= src
? *src
: IN_ADDR_NULL
;
471 route
->src_prefixlen
= src_prefixlen
;
472 route
->gw
= gw
? *gw
: IN_ADDR_NULL
;
473 route
->prefsrc
= prefsrc
? *prefsrc
: IN_ADDR_NULL
;
474 route
->scope
= scope
;
475 route
->protocol
= protocol
;
479 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
484 assert(link
->ifname
);
486 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
489 r
= sd_netlink_message_get_errno(m
);
490 if (r
< 0 && r
!= -ESRCH
)
491 log_link_warning_errno(link
, r
, "Could not drop route: %m");
496 int route_remove(Route
*route
, Link
*link
,
497 link_netlink_message_handler_t callback
) {
499 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
503 assert(link
->manager
);
504 assert(link
->manager
->rtnl
);
505 assert(link
->ifindex
> 0);
506 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
508 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
509 RTM_DELROUTE
, route
->family
,
512 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
515 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
516 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
518 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
519 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
520 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
522 if (!in_addr_is_null(route
->family
, &route
->src
))
523 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
524 if (!in_addr_is_null(route
->family
, &route
->gw
))
525 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
526 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
527 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
529 log_link_debug(link
, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
530 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
531 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
532 format_route_table(route
->table
, table
, sizeof(table
)),
533 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
534 strna(route_type_to_string(route
->type
)));
537 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
538 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
540 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
543 if (route
->dst_prefixlen
) {
544 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
546 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
548 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
550 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
553 if (route
->src_prefixlen
) {
554 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
556 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
558 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
560 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
563 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
564 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
566 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
569 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
571 return log_link_error_errno(link
, r
, "Could not set scope: %m");
573 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
575 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
577 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
578 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
580 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
583 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
584 callback
?: route_remove_handler
,
585 link_netlink_destroy_callback
, link
);
587 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
594 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
595 Route
*route
= userdata
;
600 r
= route_remove(route
, route
->link
, NULL
);
602 log_warning_errno(r
, "Could not remove route: %m");
612 link_netlink_message_handler_t callback
) {
614 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
615 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
620 assert(link
->manager
);
621 assert(link
->manager
->rtnl
);
622 assert(link
->ifindex
> 0);
623 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
626 if (route
->family
== AF_INET6
&& link_sysctl_ipv6_enabled(link
) == 0) {
627 log_link_warning(link
, "An IPv6 route is requested, but IPv6 is disabled by sysctl, ignoring.");
631 if (route_get(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
) <= 0 &&
632 set_size(link
->routes
) >= routes_max())
633 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
634 "Too many routes are configured, refusing: %m");
637 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
638 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
640 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
641 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
642 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
644 if (!in_addr_is_null(route
->family
, &route
->src
))
645 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
646 if (!in_addr_is_null(route
->family
, &route
->gw
))
647 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
648 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
649 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
651 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
652 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
653 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
654 format_route_table(route
->table
, table
, sizeof(table
)),
655 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
656 strna(route_type_to_string(route
->type
)));
659 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
660 RTM_NEWROUTE
, route
->family
,
663 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
665 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
666 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
668 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
670 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
672 return log_link_error_errno(link
, r
, "Could not set route family: %m");
675 if (route
->dst_prefixlen
) {
676 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
678 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
680 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
682 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
685 if (route
->src_prefixlen
) {
686 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
688 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
690 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
692 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
695 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
696 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
698 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
701 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
703 return log_link_error_errno(link
, r
, "Could not set scope: %m");
705 if (route
->gateway_onlink
>= 0)
706 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
708 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
710 return log_link_error_errno(link
, r
, "Could not set flags: %m");
712 if (route
->table
!= RT_TABLE_MAIN
) {
713 if (route
->table
< 256) {
714 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
716 return log_link_error_errno(link
, r
, "Could not set route table: %m");
718 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
720 return log_link_error_errno(link
, r
, "Could not set route table: %m");
722 /* Table attribute to allow more than 256. */
723 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
725 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
729 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
731 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
733 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
735 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
737 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
738 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
739 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
741 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
744 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
746 return log_link_error_errno(link
, r
, "Could not set route type: %m");
748 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
749 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
751 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
754 if (route
->ttl_propagate
>= 0) {
755 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
757 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
760 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
762 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
764 if (route
->mtu
> 0) {
765 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
767 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
770 if (route
->initcwnd
> 0) {
771 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
773 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
776 if (route
->initrwnd
> 0) {
777 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
779 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
782 if (route
->quickack
>= 0) {
783 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
785 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
788 if (route
->fast_open_no_cookie
>= 0) {
789 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
791 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
794 r
= sd_netlink_message_close_container(req
);
796 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
798 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
799 link_netlink_destroy_callback
, link
);
801 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
805 lifetime
= route
->lifetime
;
807 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, &route
);
809 return log_link_error_errno(link
, r
, "Could not add route: %m");
811 /* TODO: drop expiration handling once it can be pushed into the kernel */
812 route
->lifetime
= lifetime
;
814 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
815 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
816 route
->lifetime
, 0, route_expire_handler
, route
);
818 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
821 sd_event_source_unref(route
->expire
);
822 route
->expire
= TAKE_PTR(expire
);
827 int network_add_ipv4ll_route(Network
*network
) {
828 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
833 if (!network
->ipv4ll_route
)
836 /* IPv4LLRoute= is in [Network] section. */
837 r
= route_new_static(network
, NULL
, 0, &n
);
841 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
846 n
->dst_prefixlen
= 16;
847 n
->scope
= RT_SCOPE_LINK
;
850 n
->priority
= IPV4LL_ROUTE_METRIC
;
851 n
->protocol
= RTPROT_STATIC
;
857 int network_add_default_route_on_device(Network
*network
) {
858 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
863 if (!network
->default_route_on_device
)
866 /* DefaultRouteOnDevice= is in [Network] section. */
867 r
= route_new_static(network
, NULL
, 0, &n
);
871 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
881 static const char * const route_type_table
[__RTN_MAX
] = {
882 [RTN_UNICAST
] = "unicast",
883 [RTN_LOCAL
] = "local",
884 [RTN_BROADCAST
] = "broadcast",
885 [RTN_ANYCAST
] = "anycast",
886 [RTN_MULTICAST
] = "multicast",
887 [RTN_BLACKHOLE
] = "blackhole",
888 [RTN_UNREACHABLE
] = "unreachable",
889 [RTN_PROHIBIT
] = "prohibit",
890 [RTN_THROW
] = "throw",
892 [RTN_XRESOLVE
] = "xresolve",
895 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
896 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
898 static const char * const route_scope_table
[] = {
899 [RT_SCOPE_UNIVERSE
] = "global",
900 [RT_SCOPE_SITE
] = "site",
901 [RT_SCOPE_LINK
] = "link",
902 [RT_SCOPE_HOST
] = "host",
903 [RT_SCOPE_NOWHERE
] = "nowhere",
906 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
908 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
912 s
= route_scope_to_string(scope
);
914 strpcpy(&p
, size
, s
);
916 strpcpyf(&p
, size
, "%d", scope
);
921 static const char * const route_table_table
[] = {
922 [RT_TABLE_DEFAULT
] = "default",
923 [RT_TABLE_MAIN
] = "main",
924 [RT_TABLE_LOCAL
] = "local",
927 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
929 const char *format_route_table(int table
, char *buf
, size_t size
) {
933 s
= route_table_to_string(table
);
935 strpcpy(&p
, size
, s
);
937 strpcpyf(&p
, size
, "%d", table
);
942 static const char * const route_protocol_table
[] = {
943 [RTPROT_KERNEL
] = "kernel",
944 [RTPROT_BOOT
] = "boot",
945 [RTPROT_STATIC
] = "static",
948 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol
, int);
950 static const char * const route_protocol_full_table
[] = {
951 [RTPROT_REDIRECT
] = "redirect",
952 [RTPROT_KERNEL
] = "kernel",
953 [RTPROT_BOOT
] = "boot",
954 [RTPROT_STATIC
] = "static",
955 [RTPROT_GATED
] = "gated",
957 [RTPROT_MRT
] = "mrt",
958 [RTPROT_ZEBRA
] = "zebra",
959 [RTPROT_BIRD
] = "bird",
960 [RTPROT_DNROUTED
] = "dnrouted",
961 [RTPROT_XORP
] = "xorp",
962 [RTPROT_NTK
] = "ntk",
963 [RTPROT_DHCP
] = "dhcp",
964 [RTPROT_MROUTED
] = "mrouted",
965 [RTPROT_BABEL
] = "babel",
966 [RTPROT_BGP
] = "bgp",
967 [RTPROT_ISIS
] = "isis",
968 [RTPROT_OSPF
] = "ospf",
969 [RTPROT_RIP
] = "rip",
970 [RTPROT_EIGRP
] = "eigrp",
973 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full
, int);
975 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
979 s
= route_protocol_full_to_string(protocol
);
981 strpcpy(&p
, size
, s
);
983 strpcpyf(&p
, size
, "%d", protocol
);
988 int config_parse_gateway(
990 const char *filename
,
993 unsigned section_line
,
1000 Network
*network
= userdata
;
1001 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1010 if (streq(section
, "Network")) {
1011 /* we are not in an Route section, so treat
1012 * this as the special '0' section */
1013 r
= route_new_static(network
, NULL
, 0, &n
);
1015 r
= route_new_static(network
, filename
, section_line
, &n
);
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_ERR
, 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 if (n
->family
== AF_UNSPEC
)
1060 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
1062 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
1064 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1065 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1073 int config_parse_destination(
1075 const char *filename
,
1077 const char *section
,
1078 unsigned section_line
,
1085 Network
*network
= userdata
;
1086 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1087 union in_addr_union
*buffer
;
1088 unsigned char *prefixlen
;
1097 r
= route_new_static(network
, filename
, section_line
, &n
);
1101 if (streq(lvalue
, "Destination")) {
1103 prefixlen
= &n
->dst_prefixlen
;
1104 } else if (streq(lvalue
, "Source")) {
1106 prefixlen
= &n
->src_prefixlen
;
1108 assert_not_reached(lvalue
);
1110 if (n
->family
== AF_UNSPEC
)
1111 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
1113 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
1115 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1116 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1124 int config_parse_route_priority(
1126 const char *filename
,
1128 const char *section
,
1129 unsigned section_line
,
1136 Network
*network
= userdata
;
1137 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1146 r
= route_new_static(network
, filename
, section_line
, &n
);
1150 r
= safe_atou32(rvalue
, &n
->priority
);
1152 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1153 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1161 int config_parse_route_scope(
1163 const char *filename
,
1165 const char *section
,
1166 unsigned section_line
,
1173 Network
*network
= userdata
;
1174 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1183 r
= route_new_static(network
, filename
, section_line
, &n
);
1187 r
= route_scope_from_string(rvalue
);
1189 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1194 n
->scope_set
= true;
1199 int config_parse_route_table(
1201 const char *filename
,
1203 const char *section
,
1204 unsigned section_line
,
1211 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1212 Network
*network
= userdata
;
1221 r
= route_new_static(network
, filename
, section_line
, &n
);
1225 r
= route_table_from_string(rvalue
);
1229 r
= safe_atou32(rvalue
, &n
->table
);
1231 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1232 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1237 n
->table_set
= true;
1242 int config_parse_gateway_onlink(
1244 const char *filename
,
1246 const char *section
,
1247 unsigned section_line
,
1254 Network
*network
= userdata
;
1255 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1264 r
= route_new_static(network
, filename
, section_line
, &n
);
1268 r
= parse_boolean(rvalue
);
1270 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1271 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1275 n
->gateway_onlink
= r
;
1281 int config_parse_ipv6_route_preference(
1283 const char *filename
,
1285 const char *section
,
1286 unsigned section_line
,
1293 Network
*network
= userdata
;
1294 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1297 r
= route_new_static(network
, filename
, section_line
, &n
);
1301 if (streq(rvalue
, "low"))
1302 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1303 else if (streq(rvalue
, "medium"))
1304 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1305 else if (streq(rvalue
, "high"))
1306 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1308 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1316 int config_parse_route_protocol(
1318 const char *filename
,
1320 const char *section
,
1321 unsigned section_line
,
1328 Network
*network
= userdata
;
1329 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1332 r
= route_new_static(network
, filename
, section_line
, &n
);
1336 r
= route_protocol_from_string(rvalue
);
1340 r
= safe_atou8(rvalue
, &n
->protocol
);
1342 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1343 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1352 int config_parse_route_type(
1354 const char *filename
,
1356 const char *section
,
1357 unsigned section_line
,
1364 Network
*network
= userdata
;
1365 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1368 r
= route_new_static(network
, filename
, section_line
, &n
);
1372 t
= route_type_from_string(rvalue
);
1374 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1375 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1379 n
->type
= (unsigned char) t
;
1385 int config_parse_tcp_window(
1387 const char *filename
,
1389 const char *section
,
1390 unsigned section_line
,
1397 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1398 Network
*network
= userdata
;
1408 r
= route_new_static(network
, filename
, section_line
, &n
);
1412 r
= parse_size(rvalue
, 1024, &k
);
1414 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1415 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1418 if (k
> UINT32_MAX
) {
1419 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1420 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1424 if (streq(lvalue
, "InitialCongestionWindow"))
1426 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1429 assert_not_reached("Invalid TCP window type.");
1435 int config_parse_quickack(
1437 const char *filename
,
1439 const char *section
,
1440 unsigned section_line
,
1447 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1448 Network
*network
= userdata
;
1457 r
= route_new_static(network
, filename
, section_line
, &n
);
1461 k
= parse_boolean(rvalue
);
1463 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1464 "Failed to parse TCP quickack, ignoring: %s", rvalue
);
1473 int config_parse_fast_open_no_cookie(
1475 const char *filename
,
1477 const char *section
,
1478 unsigned section_line
,
1485 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1486 Network
*network
= userdata
;
1495 r
= route_new_static(network
, filename
, section_line
, &n
);
1499 k
= parse_boolean(rvalue
);
1501 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1502 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue
);
1506 n
->fast_open_no_cookie
= k
;
1511 int config_parse_route_mtu(
1513 const char *filename
,
1515 const char *section
,
1516 unsigned section_line
,
1523 Network
*network
= userdata
;
1524 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1533 r
= route_new_static(network
, filename
, section_line
, &n
);
1537 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1545 int config_parse_route_ttl_propagate(
1547 const char *filename
,
1549 const char *section
,
1550 unsigned section_line
,
1557 Network
*network
= userdata
;
1558 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1567 r
= route_new_static(network
, filename
, section_line
, &n
);
1571 k
= parse_boolean(rvalue
);
1573 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1574 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue
);
1578 n
->ttl_propagate
= k
;
1584 int route_section_verify(Route
*route
, Network
*network
) {
1585 if (section_is_invalid(route
->section
))
1588 if (route
->family
== AF_UNSPEC
) {
1589 assert(route
->section
);
1591 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1592 "%s: Route section without Gateway=, Destination=, Source=, "
1593 "or PreferredSource= field configured. "
1594 "Ignoring [Route] section from line %u.",
1595 route
->section
->filename
, route
->section
->line
);
1598 if (route
->family
!= AF_INET6
) {
1599 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1600 route
->table
= RT_TABLE_LOCAL
;
1602 if (!route
->scope_set
) {
1603 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1604 route
->scope
= RT_SCOPE_HOST
;
1605 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
))
1606 route
->scope
= RT_SCOPE_LINK
;
1610 if (network
->n_static_addresses
== 0 &&
1611 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1612 route
->gateway_onlink
< 0) {
1613 log_warning("%s: Gateway= without static address configured. "
1614 "Enabling GatewayOnLink= option.",
1616 route
->gateway_onlink
= true;