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 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
161 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
163 siphash24_compress(&route
->src_prefixlen
, sizeof(route
->src_prefixlen
), state
);
164 siphash24_compress(&route
->src
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
166 siphash24_compress(&route
->gw
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
168 siphash24_compress(&route
->prefsrc
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
170 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
171 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
172 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
173 siphash24_compress(&route
->protocol
, sizeof(route
->protocol
), state
);
174 siphash24_compress(&route
->scope
, sizeof(route
->scope
), state
);
175 siphash24_compress(&route
->type
, sizeof(route
->type
), state
);
177 siphash24_compress(&route
->initcwnd
, sizeof(route
->initcwnd
), state
);
178 siphash24_compress(&route
->initrwnd
, sizeof(route
->initrwnd
), state
);
182 /* treat any other address family as AF_UNSPEC */
187 static int route_compare_func(const Route
*a
, const Route
*b
) {
190 r
= CMP(a
->family
, b
->family
);
197 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
201 r
= memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
205 r
= CMP(a
->src_prefixlen
, b
->src_prefixlen
);
209 r
= memcmp(&a
->src
, &b
->src
, FAMILY_ADDRESS_SIZE(a
->family
));
213 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
217 r
= memcmp(&a
->prefsrc
, &b
->prefsrc
, FAMILY_ADDRESS_SIZE(a
->family
));
221 r
= CMP(a
->tos
, b
->tos
);
225 r
= CMP(a
->priority
, b
->priority
);
229 r
= CMP(a
->table
, b
->table
);
233 r
= CMP(a
->protocol
, b
->protocol
);
237 r
= CMP(a
->scope
, b
->scope
);
241 r
= CMP(a
->type
, b
->type
);
245 r
= CMP(a
->initcwnd
, b
->initcwnd
);
249 r
= CMP(a
->initrwnd
, b
->initrwnd
);
255 /* treat any other address family as AF_UNSPEC */
260 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
267 bool route_equal(Route
*r1
, Route
*r2
) {
274 return route_compare_func(r1
, r2
) == 0;
277 int route_get(Link
*link
, Route
*in
, Route
**ret
) {
284 existing
= set_get(link
->routes
, in
);
291 existing
= set_get(link
->routes_foreign
, in
);
301 static int route_add_internal(Link
*link
, Set
**routes
, Route
*in
, Route
**ret
) {
303 _cleanup_(route_freep
) Route
*route
= NULL
;
310 r
= route_new(&route
);
314 route
->family
= in
->family
;
315 route
->src
= in
->src
;
316 route
->src_prefixlen
= in
->src_prefixlen
;
317 route
->dst
= in
->dst
;
318 route
->dst_prefixlen
= in
->dst_prefixlen
;
320 route
->prefsrc
= in
->prefsrc
;
321 route
->scope
= in
->scope
;
322 route
->protocol
= in
->protocol
;
323 route
->type
= in
->type
;
324 route
->tos
= in
->tos
;
325 route
->priority
= in
->priority
;
326 route
->table
= in
->table
;
327 route
->initcwnd
= in
->initcwnd
;
328 route
->initrwnd
= in
->initrwnd
;
329 route
->lifetime
= in
->lifetime
;
331 r
= set_ensure_allocated(routes
, &route_hash_ops
);
335 r
= set_put(*routes
, route
);
351 int route_add_foreign(Link
*link
, Route
*in
, Route
**ret
) {
352 return route_add_internal(link
, &link
->routes_foreign
, in
, ret
);
355 int route_add(Link
*link
, Route
*in
, Route
**ret
) {
360 r
= route_get(link
, in
, &route
);
362 /* Route does not exist, create a new one */
363 r
= route_add_internal(link
, &link
->routes
, in
, &route
);
367 /* Take over a foreign route */
368 r
= set_ensure_allocated(&link
->routes
, &route_hash_ops
);
372 r
= set_put(link
->routes
, route
);
376 set_remove(link
->routes_foreign
, route
);
378 /* Route exists, do nothing */
389 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
394 assert(link
->ifname
);
396 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
399 r
= sd_netlink_message_get_errno(m
);
400 if (r
< 0 && r
!= -ESRCH
)
401 log_link_message_warning_errno(link
, m
, r
, "Could not drop route, ignoring");
406 int route_remove(Route
*route
, Link
*link
,
407 link_netlink_message_handler_t callback
) {
409 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
413 assert(link
->manager
);
414 assert(link
->manager
->rtnl
);
415 assert(link
->ifindex
> 0);
416 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
418 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
419 RTM_DELROUTE
, route
->family
,
422 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
425 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
426 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
428 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
429 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
430 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
432 if (!in_addr_is_null(route
->family
, &route
->src
))
433 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
434 if (!in_addr_is_null(route
->family
, &route
->gw
))
435 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
436 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
437 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
439 log_link_debug(link
, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
440 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
441 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
442 format_route_table(route
->table
, table
, sizeof(table
)),
443 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
444 strna(route_type_to_string(route
->type
)));
447 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
448 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
450 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
453 if (route
->dst_prefixlen
) {
454 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
456 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
458 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
460 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
463 if (route
->src_prefixlen
) {
464 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
466 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
468 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
470 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
473 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
474 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
476 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
479 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
481 return log_link_error_errno(link
, r
, "Could not set scope: %m");
483 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
485 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
487 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
488 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
490 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
493 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
494 callback
?: route_remove_handler
,
495 link_netlink_destroy_callback
, link
);
497 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
504 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
505 Route
*route
= userdata
;
510 r
= route_remove(route
, route
->link
, NULL
);
512 log_warning_errno(r
, "Could not remove route: %m");
522 link_netlink_message_handler_t callback
) {
524 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
525 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
529 assert(link
->manager
);
530 assert(link
->manager
->rtnl
);
531 assert(link
->ifindex
> 0);
532 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
535 if (route_get(link
, route
, NULL
) <= 0 &&
536 set_size(link
->routes
) >= routes_max())
537 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
538 "Too many routes are configured, refusing: %m");
541 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
542 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
544 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
545 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
546 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
548 if (!in_addr_is_null(route
->family
, &route
->src
))
549 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
550 if (!in_addr_is_null(route
->family
, &route
->gw
))
551 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
552 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
553 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
555 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
556 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
557 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
558 format_route_table(route
->table
, table
, sizeof(table
)),
559 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
560 strna(route_type_to_string(route
->type
)));
563 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
564 RTM_NEWROUTE
, route
->family
,
567 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
569 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
570 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
572 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
574 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
576 return log_link_error_errno(link
, r
, "Could not set route family: %m");
579 if (route
->dst_prefixlen
> 0) {
580 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
582 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
584 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
586 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
589 if (route
->src_prefixlen
> 0) {
590 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
592 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
594 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
596 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
599 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
600 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
602 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
605 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
607 return log_link_error_errno(link
, r
, "Could not set scope: %m");
609 if (route
->gateway_onlink
>= 0)
610 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
612 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
614 return log_link_error_errno(link
, r
, "Could not set flags: %m");
616 if (route
->table
!= RT_TABLE_MAIN
) {
617 if (route
->table
< 256) {
618 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
620 return log_link_error_errno(link
, r
, "Could not set route table: %m");
622 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
624 return log_link_error_errno(link
, r
, "Could not set route table: %m");
626 /* Table attribute to allow more than 256. */
627 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
629 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
633 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
635 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
637 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
639 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
641 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
642 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
643 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
645 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
648 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
650 return log_link_error_errno(link
, r
, "Could not set route type: %m");
652 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
653 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
655 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
658 if (route
->ttl_propagate
>= 0) {
659 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
661 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
664 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
666 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
668 if (route
->mtu
> 0) {
669 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
671 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
674 if (route
->initcwnd
> 0) {
675 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
677 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
680 if (route
->initrwnd
> 0) {
681 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
683 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
686 if (route
->quickack
>= 0) {
687 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
689 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
692 if (route
->fast_open_no_cookie
>= 0) {
693 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
695 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
698 r
= sd_netlink_message_close_container(req
);
700 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
702 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
703 link_netlink_destroy_callback
, link
);
705 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
709 r
= route_add(link
, route
, &route
);
711 return log_link_error_errno(link
, r
, "Could not add route: %m");
713 /* TODO: drop expiration handling once it can be pushed into the kernel */
714 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
715 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
716 route
->lifetime
, 0, route_expire_handler
, route
);
718 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
721 sd_event_source_unref(route
->expire
);
722 route
->expire
= TAKE_PTR(expire
);
727 int network_add_ipv4ll_route(Network
*network
) {
728 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
733 if (!network
->ipv4ll_route
)
736 /* IPv4LLRoute= is in [Network] section. */
737 r
= route_new_static(network
, NULL
, 0, &n
);
741 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
746 n
->dst_prefixlen
= 16;
747 n
->scope
= RT_SCOPE_LINK
;
750 n
->priority
= IPV4LL_ROUTE_METRIC
;
751 n
->protocol
= RTPROT_STATIC
;
757 int network_add_default_route_on_device(Network
*network
) {
758 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
763 if (!network
->default_route_on_device
)
766 /* DefaultRouteOnDevice= is in [Network] section. */
767 r
= route_new_static(network
, NULL
, 0, &n
);
772 n
->scope
= RT_SCOPE_LINK
;
774 n
->protocol
= RTPROT_STATIC
;
780 static const char * const route_type_table
[__RTN_MAX
] = {
781 [RTN_UNICAST
] = "unicast",
782 [RTN_LOCAL
] = "local",
783 [RTN_BROADCAST
] = "broadcast",
784 [RTN_ANYCAST
] = "anycast",
785 [RTN_MULTICAST
] = "multicast",
786 [RTN_BLACKHOLE
] = "blackhole",
787 [RTN_UNREACHABLE
] = "unreachable",
788 [RTN_PROHIBIT
] = "prohibit",
789 [RTN_THROW
] = "throw",
791 [RTN_XRESOLVE
] = "xresolve",
794 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
795 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
797 static const char * const route_scope_table
[] = {
798 [RT_SCOPE_UNIVERSE
] = "global",
799 [RT_SCOPE_SITE
] = "site",
800 [RT_SCOPE_LINK
] = "link",
801 [RT_SCOPE_HOST
] = "host",
802 [RT_SCOPE_NOWHERE
] = "nowhere",
805 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
807 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
811 s
= route_scope_to_string(scope
);
813 strpcpy(&p
, size
, s
);
815 strpcpyf(&p
, size
, "%d", scope
);
820 static const char * const route_table_table
[] = {
821 [RT_TABLE_DEFAULT
] = "default",
822 [RT_TABLE_MAIN
] = "main",
823 [RT_TABLE_LOCAL
] = "local",
826 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
828 const char *format_route_table(int table
, char *buf
, size_t size
) {
832 s
= route_table_to_string(table
);
834 strpcpy(&p
, size
, s
);
836 strpcpyf(&p
, size
, "%d", table
);
841 static const char * const route_protocol_table
[] = {
842 [RTPROT_KERNEL
] = "kernel",
843 [RTPROT_BOOT
] = "boot",
844 [RTPROT_STATIC
] = "static",
847 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol
, int);
849 static const char * const route_protocol_full_table
[] = {
850 [RTPROT_REDIRECT
] = "redirect",
851 [RTPROT_KERNEL
] = "kernel",
852 [RTPROT_BOOT
] = "boot",
853 [RTPROT_STATIC
] = "static",
854 [RTPROT_GATED
] = "gated",
856 [RTPROT_MRT
] = "mrt",
857 [RTPROT_ZEBRA
] = "zebra",
858 [RTPROT_BIRD
] = "bird",
859 [RTPROT_DNROUTED
] = "dnrouted",
860 [RTPROT_XORP
] = "xorp",
861 [RTPROT_NTK
] = "ntk",
862 [RTPROT_DHCP
] = "dhcp",
863 [RTPROT_MROUTED
] = "mrouted",
864 [RTPROT_BABEL
] = "babel",
865 [RTPROT_BGP
] = "bgp",
866 [RTPROT_ISIS
] = "isis",
867 [RTPROT_OSPF
] = "ospf",
868 [RTPROT_RIP
] = "rip",
869 [RTPROT_EIGRP
] = "eigrp",
872 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full
, int);
874 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
878 s
= route_protocol_full_to_string(protocol
);
880 strpcpy(&p
, size
, s
);
882 strpcpyf(&p
, size
, "%d", protocol
);
887 int config_parse_gateway(
889 const char *filename
,
892 unsigned section_line
,
899 Network
*network
= userdata
;
900 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
909 if (streq(section
, "Network")) {
910 /* we are not in an Route section, so treat
911 * this as the special '0' section */
912 r
= route_new_static(network
, NULL
, 0, &n
);
914 r
= route_new_static(network
, filename
, section_line
, &n
);
918 if (n
->family
== AF_UNSPEC
)
919 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
921 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
923 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
924 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
932 int config_parse_preferred_src(
934 const char *filename
,
937 unsigned section_line
,
944 Network
*network
= userdata
;
945 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
954 r
= route_new_static(network
, filename
, section_line
, &n
);
958 if (n
->family
== AF_UNSPEC
)
959 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
961 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
963 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
964 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
972 int config_parse_destination(
974 const char *filename
,
977 unsigned section_line
,
984 Network
*network
= userdata
;
985 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
986 union in_addr_union
*buffer
;
987 unsigned char *prefixlen
;
996 r
= route_new_static(network
, filename
, section_line
, &n
);
1000 if (streq(lvalue
, "Destination")) {
1002 prefixlen
= &n
->dst_prefixlen
;
1003 } else if (streq(lvalue
, "Source")) {
1005 prefixlen
= &n
->src_prefixlen
;
1007 assert_not_reached(lvalue
);
1009 if (n
->family
== AF_UNSPEC
)
1010 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
1012 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
1014 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
1015 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1023 int config_parse_route_priority(
1025 const char *filename
,
1027 const char *section
,
1028 unsigned section_line
,
1035 Network
*network
= userdata
;
1036 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1045 r
= route_new_static(network
, filename
, section_line
, &n
);
1049 r
= safe_atou32(rvalue
, &n
->priority
);
1051 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1052 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1060 int config_parse_route_scope(
1062 const char *filename
,
1064 const char *section
,
1065 unsigned section_line
,
1072 Network
*network
= userdata
;
1073 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1082 r
= route_new_static(network
, filename
, section_line
, &n
);
1086 r
= route_scope_from_string(rvalue
);
1088 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1093 n
->scope_set
= true;
1098 int config_parse_route_table(
1100 const char *filename
,
1102 const char *section
,
1103 unsigned section_line
,
1110 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1111 Network
*network
= userdata
;
1120 r
= route_new_static(network
, filename
, section_line
, &n
);
1124 r
= route_table_from_string(rvalue
);
1128 r
= safe_atou32(rvalue
, &n
->table
);
1130 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1131 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1136 n
->table_set
= true;
1141 int config_parse_gateway_onlink(
1143 const char *filename
,
1145 const char *section
,
1146 unsigned section_line
,
1153 Network
*network
= userdata
;
1154 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1163 r
= route_new_static(network
, filename
, section_line
, &n
);
1167 r
= parse_boolean(rvalue
);
1169 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1170 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1174 n
->gateway_onlink
= r
;
1180 int config_parse_ipv6_route_preference(
1182 const char *filename
,
1184 const char *section
,
1185 unsigned section_line
,
1192 Network
*network
= userdata
;
1193 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1196 r
= route_new_static(network
, filename
, section_line
, &n
);
1200 if (streq(rvalue
, "low"))
1201 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1202 else if (streq(rvalue
, "medium"))
1203 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1204 else if (streq(rvalue
, "high"))
1205 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1207 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1215 int config_parse_route_protocol(
1217 const char *filename
,
1219 const char *section
,
1220 unsigned section_line
,
1227 Network
*network
= userdata
;
1228 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1231 r
= route_new_static(network
, filename
, section_line
, &n
);
1235 r
= route_protocol_from_string(rvalue
);
1239 r
= safe_atou8(rvalue
, &n
->protocol
);
1241 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1242 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1251 int config_parse_route_type(
1253 const char *filename
,
1255 const char *section
,
1256 unsigned section_line
,
1263 Network
*network
= userdata
;
1264 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1267 r
= route_new_static(network
, filename
, section_line
, &n
);
1271 t
= route_type_from_string(rvalue
);
1273 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1274 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1278 n
->type
= (unsigned char) t
;
1284 int config_parse_tcp_window(
1286 const char *filename
,
1288 const char *section
,
1289 unsigned section_line
,
1296 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1297 Network
*network
= userdata
;
1307 r
= route_new_static(network
, filename
, section_line
, &n
);
1311 r
= parse_size(rvalue
, 1024, &k
);
1313 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1314 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1317 if (k
> UINT32_MAX
) {
1318 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1319 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1323 if (streq(lvalue
, "InitialCongestionWindow"))
1325 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1328 assert_not_reached("Invalid TCP window type.");
1334 int config_parse_quickack(
1336 const char *filename
,
1338 const char *section
,
1339 unsigned section_line
,
1346 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1347 Network
*network
= userdata
;
1356 r
= route_new_static(network
, filename
, section_line
, &n
);
1360 k
= parse_boolean(rvalue
);
1362 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1363 "Failed to parse TCP quickack, ignoring: %s", rvalue
);
1372 int config_parse_fast_open_no_cookie(
1374 const char *filename
,
1376 const char *section
,
1377 unsigned section_line
,
1384 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1385 Network
*network
= userdata
;
1394 r
= route_new_static(network
, filename
, section_line
, &n
);
1398 k
= parse_boolean(rvalue
);
1400 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1401 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue
);
1405 n
->fast_open_no_cookie
= k
;
1410 int config_parse_route_mtu(
1412 const char *filename
,
1414 const char *section
,
1415 unsigned section_line
,
1422 Network
*network
= userdata
;
1423 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1432 r
= route_new_static(network
, filename
, section_line
, &n
);
1436 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1444 int config_parse_route_ttl_propagate(
1446 const char *filename
,
1448 const char *section
,
1449 unsigned section_line
,
1456 Network
*network
= userdata
;
1457 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1466 r
= route_new_static(network
, filename
, section_line
, &n
);
1470 k
= parse_boolean(rvalue
);
1472 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1473 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue
);
1477 n
->ttl_propagate
= k
;
1483 int route_section_verify(Route
*route
, Network
*network
) {
1484 if (section_is_invalid(route
->section
))
1487 if (route
->family
== AF_UNSPEC
) {
1488 assert(route
->section
);
1490 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1491 "%s: Route section without Gateway=, Destination=, Source=, "
1492 "or PreferredSource= field configured. "
1493 "Ignoring [Route] section from line %u.",
1494 route
->section
->filename
, route
->section
->line
);
1497 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1498 route
->table
= RT_TABLE_LOCAL
;
1500 if (!route
->scope_set
&& route
->family
!= AF_INET6
) {
1501 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1502 route
->scope
= RT_SCOPE_HOST
;
1503 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_MULTICAST
))
1504 route
->scope
= RT_SCOPE_LINK
;
1507 if (network
->n_static_addresses
== 0 &&
1508 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1509 route
->gateway_onlink
< 0) {
1510 log_warning("%s: Gateway= without static address configured. "
1511 "Enabling GatewayOnLink= option.",
1513 route
->gateway_onlink
= true;