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 bool route_equal(Route
*r1
, Route
*r2
) {
217 return route_compare_func(r1
, r2
) == 0;
220 int route_get(Link
*link
,
222 const union in_addr_union
*dst
,
223 unsigned char dst_prefixlen
,
229 Route route
, *existing
;
237 .dst_prefixlen
= dst_prefixlen
,
239 .priority
= priority
,
243 existing
= set_get(link
->routes
, &route
);
250 existing
= set_get(link
->routes_foreign
, &route
);
260 static int route_add_internal(
264 const union in_addr_union
*dst
,
265 unsigned char dst_prefixlen
,
271 _cleanup_(route_freep
) Route
*route
= NULL
;
278 r
= route_new(&route
);
282 route
->family
= family
;
284 route
->dst_prefixlen
= dst_prefixlen
;
286 route
->priority
= priority
;
287 route
->table
= table
;
289 r
= set_ensure_allocated(routes
, &route_hash_ops
);
293 r
= set_put(*routes
, route
);
307 int route_add_foreign(
310 const union in_addr_union
*dst
,
311 unsigned char dst_prefixlen
,
317 return route_add_internal(link
, &link
->routes_foreign
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, ret
);
320 int route_add(Link
*link
,
322 const union in_addr_union
*dst
,
323 unsigned char dst_prefixlen
,
332 r
= route_get(link
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
334 /* Route does not exist, create a new one */
335 r
= route_add_internal(link
, &link
->routes
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
339 /* Take over a foreign route */
340 r
= set_ensure_allocated(&link
->routes
, &route_hash_ops
);
344 r
= set_put(link
->routes
, route
);
348 set_remove(link
->routes_foreign
, route
);
350 /* Route exists, do nothing */
361 void route_update(Route
*route
,
362 const union in_addr_union
*src
,
363 unsigned char src_prefixlen
,
364 const union in_addr_union
*gw
,
365 const union in_addr_union
*prefsrc
,
367 unsigned char protocol
,
368 unsigned char type
) {
371 assert(src
|| src_prefixlen
== 0);
373 route
->src
= src
? *src
: IN_ADDR_NULL
;
374 route
->src_prefixlen
= src_prefixlen
;
375 route
->gw
= gw
? *gw
: IN_ADDR_NULL
;
376 route
->prefsrc
= prefsrc
? *prefsrc
: IN_ADDR_NULL
;
377 route
->scope
= scope
;
378 route
->protocol
= protocol
;
382 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
387 assert(link
->ifname
);
389 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
392 r
= sd_netlink_message_get_errno(m
);
393 if (r
< 0 && r
!= -ESRCH
)
394 log_link_warning_errno(link
, r
, "Could not drop route: %m");
399 int route_remove(Route
*route
, Link
*link
,
400 link_netlink_message_handler_t callback
) {
402 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
406 assert(link
->manager
);
407 assert(link
->manager
->rtnl
);
408 assert(link
->ifindex
> 0);
409 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
411 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
412 RTM_DELROUTE
, route
->family
,
415 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
418 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
419 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
421 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
422 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
423 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
425 if (!in_addr_is_null(route
->family
, &route
->src
))
426 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
427 if (!in_addr_is_null(route
->family
, &route
->gw
))
428 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
429 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
430 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
432 log_link_debug(link
, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
433 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
434 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
435 format_route_table(route
->table
, table
, sizeof(table
)),
436 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
437 strna(route_type_to_string(route
->type
)));
440 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
441 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
443 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
446 if (route
->dst_prefixlen
) {
447 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
449 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
451 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
453 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
456 if (route
->src_prefixlen
) {
457 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
459 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
461 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
463 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
466 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
467 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
469 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
472 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
474 return log_link_error_errno(link
, r
, "Could not set scope: %m");
476 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
478 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
480 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
481 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
483 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
486 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
487 callback
?: route_remove_handler
,
488 link_netlink_destroy_callback
, link
);
490 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
497 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
498 Route
*route
= userdata
;
503 r
= route_remove(route
, route
->link
, NULL
);
505 log_warning_errno(r
, "Could not remove route: %m");
515 link_netlink_message_handler_t callback
) {
517 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
518 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
523 assert(link
->manager
);
524 assert(link
->manager
->rtnl
);
525 assert(link
->ifindex
> 0);
526 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
529 if (route
->family
== AF_INET6
&& link_sysctl_ipv6_enabled(link
) == 0) {
530 log_link_warning(link
, "An IPv6 route is requested, but IPv6 is disabled by sysctl, ignoring.");
534 if (route_get(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
) <= 0 &&
535 set_size(link
->routes
) >= routes_max())
536 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
537 "Too many routes are configured, refusing: %m");
540 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
541 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
543 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
544 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
545 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
547 if (!in_addr_is_null(route
->family
, &route
->src
))
548 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
549 if (!in_addr_is_null(route
->family
, &route
->gw
))
550 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
551 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
552 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
554 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
555 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
556 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
557 format_route_table(route
->table
, table
, sizeof(table
)),
558 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
559 strna(route_type_to_string(route
->type
)));
562 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
563 RTM_NEWROUTE
, route
->family
,
566 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
568 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
569 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
571 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
573 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
575 return log_link_error_errno(link
, r
, "Could not set route family: %m");
578 if (route
->dst_prefixlen
) {
579 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
581 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
583 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
585 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
588 if (route
->src_prefixlen
) {
589 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
591 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
593 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
595 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
598 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
599 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
601 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
604 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
606 return log_link_error_errno(link
, r
, "Could not set scope: %m");
608 if (route
->gateway_onlink
>= 0)
609 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
611 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
613 return log_link_error_errno(link
, r
, "Could not set flags: %m");
615 if (route
->table
!= RT_TABLE_MAIN
) {
616 if (route
->table
< 256) {
617 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
619 return log_link_error_errno(link
, r
, "Could not set route table: %m");
621 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
623 return log_link_error_errno(link
, r
, "Could not set route table: %m");
625 /* Table attribute to allow more than 256. */
626 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
628 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
632 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
634 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
636 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
638 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
640 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
641 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
642 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
644 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
647 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
649 return log_link_error_errno(link
, r
, "Could not set route type: %m");
651 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
652 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
654 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
657 if (route
->ttl_propagate
>= 0) {
658 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
660 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
663 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
665 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
667 if (route
->mtu
> 0) {
668 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
670 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
673 if (route
->initcwnd
> 0) {
674 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
676 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
679 if (route
->initrwnd
> 0) {
680 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
682 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
685 if (route
->quickack
>= 0) {
686 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
688 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
691 if (route
->fast_open_no_cookie
>= 0) {
692 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
694 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
697 r
= sd_netlink_message_close_container(req
);
699 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
701 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
702 link_netlink_destroy_callback
, link
);
704 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
708 lifetime
= route
->lifetime
;
710 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, &route
);
712 return log_link_error_errno(link
, r
, "Could not add route: %m");
714 /* TODO: drop expiration handling once it can be pushed into the kernel */
715 route
->lifetime
= lifetime
;
717 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
718 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
719 route
->lifetime
, 0, route_expire_handler
, route
);
721 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
724 sd_event_source_unref(route
->expire
);
725 route
->expire
= TAKE_PTR(expire
);
730 int network_add_ipv4ll_route(Network
*network
) {
731 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
736 if (!network
->ipv4ll_route
)
739 /* IPv4LLRoute= is in [Network] section. */
740 r
= route_new_static(network
, NULL
, 0, &n
);
744 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
749 n
->dst_prefixlen
= 16;
750 n
->scope
= RT_SCOPE_LINK
;
753 n
->priority
= IPV4LL_ROUTE_METRIC
;
754 n
->protocol
= RTPROT_STATIC
;
760 int network_add_default_route_on_device(Network
*network
) {
761 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
766 if (!network
->default_route_on_device
)
769 /* DefaultRouteOnDevice= is in [Network] section. */
770 r
= route_new_static(network
, NULL
, 0, &n
);
774 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
784 static const char * const route_type_table
[__RTN_MAX
] = {
785 [RTN_UNICAST
] = "unicast",
786 [RTN_LOCAL
] = "local",
787 [RTN_BROADCAST
] = "broadcast",
788 [RTN_ANYCAST
] = "anycast",
789 [RTN_MULTICAST
] = "multicast",
790 [RTN_BLACKHOLE
] = "blackhole",
791 [RTN_UNREACHABLE
] = "unreachable",
792 [RTN_PROHIBIT
] = "prohibit",
793 [RTN_THROW
] = "throw",
795 [RTN_XRESOLVE
] = "xresolve",
798 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
799 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
801 static const char * const route_scope_table
[] = {
802 [RT_SCOPE_UNIVERSE
] = "global",
803 [RT_SCOPE_SITE
] = "site",
804 [RT_SCOPE_LINK
] = "link",
805 [RT_SCOPE_HOST
] = "host",
806 [RT_SCOPE_NOWHERE
] = "nowhere",
809 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
811 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
815 s
= route_scope_to_string(scope
);
817 strpcpy(&p
, size
, s
);
819 strpcpyf(&p
, size
, "%d", scope
);
824 static const char * const route_table_table
[] = {
825 [RT_TABLE_DEFAULT
] = "default",
826 [RT_TABLE_MAIN
] = "main",
827 [RT_TABLE_LOCAL
] = "local",
830 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
832 const char *format_route_table(int table
, char *buf
, size_t size
) {
836 s
= route_table_to_string(table
);
838 strpcpy(&p
, size
, s
);
840 strpcpyf(&p
, size
, "%d", table
);
845 static const char * const route_protocol_table
[] = {
846 [RTPROT_KERNEL
] = "kernel",
847 [RTPROT_BOOT
] = "boot",
848 [RTPROT_STATIC
] = "static",
851 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_protocol
, int);
853 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
857 s
= route_protocol_to_string(protocol
);
859 strpcpy(&p
, size
, s
);
861 strpcpyf(&p
, size
, "%d", protocol
);
866 int config_parse_gateway(
868 const char *filename
,
871 unsigned section_line
,
878 Network
*network
= userdata
;
879 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
888 if (streq(section
, "Network")) {
889 /* we are not in an Route section, so treat
890 * this as the special '0' section */
891 r
= route_new_static(network
, NULL
, 0, &n
);
893 r
= route_new_static(network
, filename
, section_line
, &n
);
897 if (n
->family
== AF_UNSPEC
)
898 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
900 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
902 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
903 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
911 int config_parse_preferred_src(
913 const char *filename
,
916 unsigned section_line
,
923 Network
*network
= userdata
;
924 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
933 r
= route_new_static(network
, filename
, section_line
, &n
);
937 if (n
->family
== AF_UNSPEC
)
938 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
940 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
942 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
943 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
951 int config_parse_destination(
953 const char *filename
,
956 unsigned section_line
,
963 Network
*network
= userdata
;
964 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
965 union in_addr_union
*buffer
;
966 unsigned char *prefixlen
;
975 r
= route_new_static(network
, filename
, section_line
, &n
);
979 if (streq(lvalue
, "Destination")) {
981 prefixlen
= &n
->dst_prefixlen
;
982 } else if (streq(lvalue
, "Source")) {
984 prefixlen
= &n
->src_prefixlen
;
986 assert_not_reached(lvalue
);
988 if (n
->family
== AF_UNSPEC
)
989 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
991 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
993 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
994 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1002 int config_parse_route_priority(
1004 const char *filename
,
1006 const char *section
,
1007 unsigned section_line
,
1014 Network
*network
= userdata
;
1015 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1024 r
= route_new_static(network
, filename
, section_line
, &n
);
1028 r
= safe_atou32(rvalue
, &n
->priority
);
1030 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1031 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1039 int config_parse_route_scope(
1041 const char *filename
,
1043 const char *section
,
1044 unsigned section_line
,
1051 Network
*network
= userdata
;
1052 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1061 r
= route_new_static(network
, filename
, section_line
, &n
);
1065 r
= route_scope_from_string(rvalue
);
1067 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1072 n
->scope_set
= true;
1077 int config_parse_route_table(
1079 const char *filename
,
1081 const char *section
,
1082 unsigned section_line
,
1089 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1090 Network
*network
= userdata
;
1099 r
= route_new_static(network
, filename
, section_line
, &n
);
1103 r
= route_table_from_string(rvalue
);
1107 r
= safe_atou32(rvalue
, &n
->table
);
1109 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1110 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1115 n
->table_set
= true;
1120 int config_parse_gateway_onlink(
1122 const char *filename
,
1124 const char *section
,
1125 unsigned section_line
,
1132 Network
*network
= userdata
;
1133 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1142 r
= route_new_static(network
, filename
, section_line
, &n
);
1146 r
= parse_boolean(rvalue
);
1148 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1149 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1153 n
->gateway_onlink
= r
;
1159 int config_parse_ipv6_route_preference(
1161 const char *filename
,
1163 const char *section
,
1164 unsigned section_line
,
1171 Network
*network
= userdata
;
1172 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1175 r
= route_new_static(network
, filename
, section_line
, &n
);
1179 if (streq(rvalue
, "low"))
1180 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1181 else if (streq(rvalue
, "medium"))
1182 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1183 else if (streq(rvalue
, "high"))
1184 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1186 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1194 int config_parse_route_protocol(
1196 const char *filename
,
1198 const char *section
,
1199 unsigned section_line
,
1206 Network
*network
= userdata
;
1207 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1210 r
= route_new_static(network
, filename
, section_line
, &n
);
1214 r
= route_protocol_from_string(rvalue
);
1218 r
= safe_atou8(rvalue
, &n
->protocol
);
1220 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1221 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1230 int config_parse_route_type(
1232 const char *filename
,
1234 const char *section
,
1235 unsigned section_line
,
1242 Network
*network
= userdata
;
1243 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1246 r
= route_new_static(network
, filename
, section_line
, &n
);
1250 t
= route_type_from_string(rvalue
);
1252 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1253 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1257 n
->type
= (unsigned char) t
;
1263 int config_parse_tcp_window(
1265 const char *filename
,
1267 const char *section
,
1268 unsigned section_line
,
1275 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1276 Network
*network
= userdata
;
1286 r
= route_new_static(network
, filename
, section_line
, &n
);
1290 r
= parse_size(rvalue
, 1024, &k
);
1292 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1293 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1296 if (k
> UINT32_MAX
) {
1297 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1298 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1302 if (streq(lvalue
, "InitialCongestionWindow"))
1304 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1307 assert_not_reached("Invalid TCP window type.");
1313 int config_parse_quickack(
1315 const char *filename
,
1317 const char *section
,
1318 unsigned section_line
,
1325 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1326 Network
*network
= userdata
;
1335 r
= route_new_static(network
, filename
, section_line
, &n
);
1339 k
= parse_boolean(rvalue
);
1341 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1342 "Failed to parse TCP quickack, ignoring: %s", rvalue
);
1351 int config_parse_fast_open_no_cookie(
1353 const char *filename
,
1355 const char *section
,
1356 unsigned section_line
,
1363 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1364 Network
*network
= userdata
;
1373 r
= route_new_static(network
, filename
, section_line
, &n
);
1377 k
= parse_boolean(rvalue
);
1379 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1380 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue
);
1384 n
->fast_open_no_cookie
= k
;
1389 int config_parse_route_mtu(
1391 const char *filename
,
1393 const char *section
,
1394 unsigned section_line
,
1401 Network
*network
= userdata
;
1402 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1411 r
= route_new_static(network
, filename
, section_line
, &n
);
1415 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1423 int config_parse_route_ttl_propagate(
1425 const char *filename
,
1427 const char *section
,
1428 unsigned section_line
,
1435 Network
*network
= userdata
;
1436 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1445 r
= route_new_static(network
, filename
, section_line
, &n
);
1449 k
= parse_boolean(rvalue
);
1451 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1452 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue
);
1456 n
->ttl_propagate
= k
;
1462 int route_section_verify(Route
*route
, Network
*network
) {
1463 if (section_is_invalid(route
->section
))
1466 if (route
->family
== AF_UNSPEC
) {
1467 assert(route
->section
);
1469 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1470 "%s: Route section without Gateway=, Destination=, Source=, "
1471 "or PreferredSource= field configured. "
1472 "Ignoring [Route] section from line %u.",
1473 route
->section
->filename
, route
->section
->line
);
1476 if (route
->family
!= AF_INET6
) {
1477 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1478 route
->table
= RT_TABLE_LOCAL
;
1480 if (!route
->scope_set
) {
1481 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1482 route
->scope
= RT_SCOPE_HOST
;
1483 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
))
1484 route
->scope
= RT_SCOPE_LINK
;
1488 if (network
->n_static_addresses
== 0 &&
1489 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1490 route
->gateway_onlink
< 0) {
1491 log_warning("%s: Gateway= without static address configured. "
1492 "Enabling GatewayOnLink= option.",
1494 route
->gateway_onlink
= true;