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_get(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, &route
->gw
, route
->tos
, route
->priority
, route
->table
, NULL
) <= 0 &&
640 set_size(link
->routes
) >= routes_max())
641 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
642 "Too many routes are configured, refusing: %m");
645 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
646 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
648 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
649 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
650 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
652 if (!in_addr_is_null(route
->family
, &route
->src
))
653 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
654 if (!in_addr_is_null(route
->family
, &route
->gw
))
655 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
656 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
657 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
659 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
660 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
661 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
662 format_route_table(route
->table
, table
, sizeof(table
)),
663 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
664 strna(route_type_to_string(route
->type
)));
667 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
668 RTM_NEWROUTE
, route
->family
,
671 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
673 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
674 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
676 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
678 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
680 return log_link_error_errno(link
, r
, "Could not set route family: %m");
683 if (route
->dst_prefixlen
) {
684 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
686 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
688 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
690 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
693 if (route
->src_prefixlen
) {
694 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
696 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
698 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
700 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
703 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
704 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
706 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
709 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
711 return log_link_error_errno(link
, r
, "Could not set scope: %m");
713 if (route
->gateway_onlink
>= 0)
714 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
716 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
718 return log_link_error_errno(link
, r
, "Could not set flags: %m");
720 if (route
->table
!= RT_TABLE_MAIN
) {
721 if (route
->table
< 256) {
722 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
724 return log_link_error_errno(link
, r
, "Could not set route table: %m");
726 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
728 return log_link_error_errno(link
, r
, "Could not set route table: %m");
730 /* Table attribute to allow more than 256. */
731 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
733 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
737 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
739 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
741 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
743 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
745 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
746 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
747 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
749 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
752 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
754 return log_link_error_errno(link
, r
, "Could not set route type: %m");
756 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
757 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
759 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
762 if (route
->ttl_propagate
>= 0) {
763 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
765 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
768 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
770 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
772 if (route
->mtu
> 0) {
773 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
775 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
778 if (route
->initcwnd
> 0) {
779 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
781 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
784 if (route
->initrwnd
> 0) {
785 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
787 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
790 if (route
->quickack
>= 0) {
791 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
793 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
796 if (route
->fast_open_no_cookie
>= 0) {
797 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
799 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
802 r
= sd_netlink_message_close_container(req
);
804 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
806 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
807 link_netlink_destroy_callback
, link
);
809 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
813 lifetime
= route
->lifetime
;
815 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, &route
->gw
, route
->tos
, route
->priority
, route
->table
, &route
);
817 return log_link_error_errno(link
, r
, "Could not add route: %m");
819 /* TODO: drop expiration handling once it can be pushed into the kernel */
820 route
->lifetime
= lifetime
;
822 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
823 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
824 route
->lifetime
, 0, route_expire_handler
, route
);
826 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
829 sd_event_source_unref(route
->expire
);
830 route
->expire
= TAKE_PTR(expire
);
835 int network_add_ipv4ll_route(Network
*network
) {
836 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
841 if (!network
->ipv4ll_route
)
844 /* IPv4LLRoute= is in [Network] section. */
845 r
= route_new_static(network
, NULL
, 0, &n
);
849 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
854 n
->dst_prefixlen
= 16;
855 n
->scope
= RT_SCOPE_LINK
;
858 n
->priority
= IPV4LL_ROUTE_METRIC
;
859 n
->protocol
= RTPROT_STATIC
;
865 int network_add_default_route_on_device(Network
*network
) {
866 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
871 if (!network
->default_route_on_device
)
874 /* DefaultRouteOnDevice= is in [Network] section. */
875 r
= route_new_static(network
, NULL
, 0, &n
);
879 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
889 static const char * const route_type_table
[__RTN_MAX
] = {
890 [RTN_UNICAST
] = "unicast",
891 [RTN_LOCAL
] = "local",
892 [RTN_BROADCAST
] = "broadcast",
893 [RTN_ANYCAST
] = "anycast",
894 [RTN_MULTICAST
] = "multicast",
895 [RTN_BLACKHOLE
] = "blackhole",
896 [RTN_UNREACHABLE
] = "unreachable",
897 [RTN_PROHIBIT
] = "prohibit",
898 [RTN_THROW
] = "throw",
900 [RTN_XRESOLVE
] = "xresolve",
903 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
904 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
906 static const char * const route_scope_table
[] = {
907 [RT_SCOPE_UNIVERSE
] = "global",
908 [RT_SCOPE_SITE
] = "site",
909 [RT_SCOPE_LINK
] = "link",
910 [RT_SCOPE_HOST
] = "host",
911 [RT_SCOPE_NOWHERE
] = "nowhere",
914 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
916 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
920 s
= route_scope_to_string(scope
);
922 strpcpy(&p
, size
, s
);
924 strpcpyf(&p
, size
, "%d", scope
);
929 static const char * const route_table_table
[] = {
930 [RT_TABLE_DEFAULT
] = "default",
931 [RT_TABLE_MAIN
] = "main",
932 [RT_TABLE_LOCAL
] = "local",
935 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
937 const char *format_route_table(int table
, char *buf
, size_t size
) {
941 s
= route_table_to_string(table
);
943 strpcpy(&p
, size
, s
);
945 strpcpyf(&p
, size
, "%d", table
);
950 static const char * const route_protocol_table
[] = {
951 [RTPROT_KERNEL
] = "kernel",
952 [RTPROT_BOOT
] = "boot",
953 [RTPROT_STATIC
] = "static",
956 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol
, int);
958 static const char * const route_protocol_full_table
[] = {
959 [RTPROT_REDIRECT
] = "redirect",
960 [RTPROT_KERNEL
] = "kernel",
961 [RTPROT_BOOT
] = "boot",
962 [RTPROT_STATIC
] = "static",
963 [RTPROT_GATED
] = "gated",
965 [RTPROT_MRT
] = "mrt",
966 [RTPROT_ZEBRA
] = "zebra",
967 [RTPROT_BIRD
] = "bird",
968 [RTPROT_DNROUTED
] = "dnrouted",
969 [RTPROT_XORP
] = "xorp",
970 [RTPROT_NTK
] = "ntk",
971 [RTPROT_DHCP
] = "dhcp",
972 [RTPROT_MROUTED
] = "mrouted",
973 [RTPROT_BABEL
] = "babel",
974 [RTPROT_BGP
] = "bgp",
975 [RTPROT_ISIS
] = "isis",
976 [RTPROT_OSPF
] = "ospf",
977 [RTPROT_RIP
] = "rip",
978 [RTPROT_EIGRP
] = "eigrp",
981 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full
, int);
983 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
987 s
= route_protocol_full_to_string(protocol
);
989 strpcpy(&p
, size
, s
);
991 strpcpyf(&p
, size
, "%d", protocol
);
996 int config_parse_gateway(
998 const char *filename
,
1000 const char *section
,
1001 unsigned section_line
,
1008 Network
*network
= userdata
;
1009 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1018 if (streq(section
, "Network")) {
1019 /* we are not in an Route section, so treat
1020 * this as the special '0' section */
1021 r
= route_new_static(network
, NULL
, 0, &n
);
1023 r
= route_new_static(network
, filename
, section_line
, &n
);
1027 if (n
->family
== AF_UNSPEC
)
1028 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1030 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
1032 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1033 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1041 int config_parse_preferred_src(
1043 const char *filename
,
1045 const char *section
,
1046 unsigned section_line
,
1053 Network
*network
= userdata
;
1054 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1063 r
= route_new_static(network
, filename
, section_line
, &n
);
1067 if (n
->family
== AF_UNSPEC
)
1068 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
1070 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
1072 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1073 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1081 int config_parse_destination(
1083 const char *filename
,
1085 const char *section
,
1086 unsigned section_line
,
1093 Network
*network
= userdata
;
1094 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1095 union in_addr_union
*buffer
;
1096 unsigned char *prefixlen
;
1105 r
= route_new_static(network
, filename
, section_line
, &n
);
1109 if (streq(lvalue
, "Destination")) {
1111 prefixlen
= &n
->dst_prefixlen
;
1112 } else if (streq(lvalue
, "Source")) {
1114 prefixlen
= &n
->src_prefixlen
;
1116 assert_not_reached(lvalue
);
1118 if (n
->family
== AF_UNSPEC
)
1119 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
1121 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
1123 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1124 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1132 int config_parse_route_priority(
1134 const char *filename
,
1136 const char *section
,
1137 unsigned section_line
,
1144 Network
*network
= userdata
;
1145 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1154 r
= route_new_static(network
, filename
, section_line
, &n
);
1158 r
= safe_atou32(rvalue
, &n
->priority
);
1160 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1161 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1169 int config_parse_route_scope(
1171 const char *filename
,
1173 const char *section
,
1174 unsigned section_line
,
1181 Network
*network
= userdata
;
1182 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1191 r
= route_new_static(network
, filename
, section_line
, &n
);
1195 r
= route_scope_from_string(rvalue
);
1197 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1202 n
->scope_set
= true;
1207 int config_parse_route_table(
1209 const char *filename
,
1211 const char *section
,
1212 unsigned section_line
,
1219 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1220 Network
*network
= userdata
;
1229 r
= route_new_static(network
, filename
, section_line
, &n
);
1233 r
= route_table_from_string(rvalue
);
1237 r
= safe_atou32(rvalue
, &n
->table
);
1239 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1240 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1245 n
->table_set
= true;
1250 int config_parse_gateway_onlink(
1252 const char *filename
,
1254 const char *section
,
1255 unsigned section_line
,
1262 Network
*network
= userdata
;
1263 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1272 r
= route_new_static(network
, filename
, section_line
, &n
);
1276 r
= parse_boolean(rvalue
);
1278 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1279 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1283 n
->gateway_onlink
= r
;
1289 int config_parse_ipv6_route_preference(
1291 const char *filename
,
1293 const char *section
,
1294 unsigned section_line
,
1301 Network
*network
= userdata
;
1302 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1305 r
= route_new_static(network
, filename
, section_line
, &n
);
1309 if (streq(rvalue
, "low"))
1310 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1311 else if (streq(rvalue
, "medium"))
1312 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1313 else if (streq(rvalue
, "high"))
1314 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1316 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1324 int config_parse_route_protocol(
1326 const char *filename
,
1328 const char *section
,
1329 unsigned section_line
,
1336 Network
*network
= userdata
;
1337 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1340 r
= route_new_static(network
, filename
, section_line
, &n
);
1344 r
= route_protocol_from_string(rvalue
);
1348 r
= safe_atou8(rvalue
, &n
->protocol
);
1350 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1351 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1360 int config_parse_route_type(
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 t
= route_type_from_string(rvalue
);
1382 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1383 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1387 n
->type
= (unsigned char) t
;
1393 int config_parse_tcp_window(
1395 const char *filename
,
1397 const char *section
,
1398 unsigned section_line
,
1405 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1406 Network
*network
= userdata
;
1416 r
= route_new_static(network
, filename
, section_line
, &n
);
1420 r
= parse_size(rvalue
, 1024, &k
);
1422 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1423 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1426 if (k
> UINT32_MAX
) {
1427 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1428 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1432 if (streq(lvalue
, "InitialCongestionWindow"))
1434 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1437 assert_not_reached("Invalid TCP window type.");
1443 int config_parse_quickack(
1445 const char *filename
,
1447 const char *section
,
1448 unsigned section_line
,
1455 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1456 Network
*network
= userdata
;
1465 r
= route_new_static(network
, filename
, section_line
, &n
);
1469 k
= parse_boolean(rvalue
);
1471 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1472 "Failed to parse TCP quickack, ignoring: %s", rvalue
);
1481 int config_parse_fast_open_no_cookie(
1483 const char *filename
,
1485 const char *section
,
1486 unsigned section_line
,
1493 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1494 Network
*network
= userdata
;
1503 r
= route_new_static(network
, filename
, section_line
, &n
);
1507 k
= parse_boolean(rvalue
);
1509 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1510 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue
);
1514 n
->fast_open_no_cookie
= k
;
1519 int config_parse_route_mtu(
1521 const char *filename
,
1523 const char *section
,
1524 unsigned section_line
,
1531 Network
*network
= userdata
;
1532 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1541 r
= route_new_static(network
, filename
, section_line
, &n
);
1545 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1553 int config_parse_route_ttl_propagate(
1555 const char *filename
,
1557 const char *section
,
1558 unsigned section_line
,
1565 Network
*network
= userdata
;
1566 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1575 r
= route_new_static(network
, filename
, section_line
, &n
);
1579 k
= parse_boolean(rvalue
);
1581 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1582 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue
);
1586 n
->ttl_propagate
= k
;
1592 int route_section_verify(Route
*route
, Network
*network
) {
1593 if (section_is_invalid(route
->section
))
1596 if (route
->family
== AF_UNSPEC
) {
1597 assert(route
->section
);
1599 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1600 "%s: Route section without Gateway=, Destination=, Source=, "
1601 "or PreferredSource= field configured. "
1602 "Ignoring [Route] section from line %u.",
1603 route
->section
->filename
, route
->section
->line
);
1606 if (route
->family
!= AF_INET6
) {
1607 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1608 route
->table
= RT_TABLE_LOCAL
;
1610 if (!route
->scope_set
) {
1611 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1612 route
->scope
= RT_SCOPE_HOST
;
1613 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
))
1614 route
->scope
= RT_SCOPE_LINK
;
1618 if (network
->n_static_addresses
== 0 &&
1619 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1620 route
->gateway_onlink
< 0) {
1621 log_warning("%s: Gateway= without static address configured. "
1622 "Enabling GatewayOnLink= option.",
1624 route
->gateway_onlink
= true;