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 "socket-netlink.h"
16 #include "string-table.h"
17 #include "string-util.h"
20 #include "sysctl-util.h"
23 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
25 static unsigned routes_max(void) {
26 static thread_local
unsigned cached
= 0;
28 _cleanup_free_
char *s4
= NULL
, *s6
= NULL
;
29 unsigned val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
34 if (sysctl_read("net/ipv4/route/max_size", &s4
) >= 0) {
36 if (safe_atou(s4
, &val4
) >= 0 &&
38 /* This is the default "no limit" value in the kernel */
39 val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
42 if (sysctl_read("net/ipv6/route/max_size", &s6
) >= 0) {
44 (void) safe_atou(s6
, &val6
);
47 cached
= MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val4
) +
48 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
);
52 int route_new(Route
**ret
) {
53 _cleanup_(route_freep
) Route
*route
= NULL
;
55 route
= new(Route
, 1);
61 .scope
= RT_SCOPE_UNIVERSE
,
62 .protocol
= RTPROT_UNSPEC
,
64 .table
= RT_TABLE_MAIN
,
65 .lifetime
= USEC_INFINITY
,
67 .fast_open_no_cookie
= -1,
72 *ret
= TAKE_PTR(route
);
77 static int route_new_static(Network
*network
, const char *filename
, unsigned section_line
, Route
**ret
) {
78 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
79 _cleanup_(route_freep
) Route
*route
= NULL
;
84 assert(!!filename
== (section_line
> 0));
87 r
= network_config_section_new(filename
, section_line
, &n
);
91 route
= hashmap_get(network
->routes_by_section
, n
);
93 *ret
= TAKE_PTR(route
);
99 if (network
->n_static_routes
>= routes_max())
102 r
= route_new(&route
);
106 route
->protocol
= RTPROT_STATIC
;
107 route
->network
= network
;
108 LIST_PREPEND(routes
, network
->static_routes
, route
);
109 network
->n_static_routes
++;
112 route
->section
= TAKE_PTR(n
);
114 r
= hashmap_ensure_allocated(&network
->routes_by_section
, &network_config_hash_ops
);
118 r
= hashmap_put(network
->routes_by_section
, route
->section
, route
);
123 *ret
= TAKE_PTR(route
);
128 void route_free(Route
*route
) {
132 if (route
->network
) {
133 LIST_REMOVE(routes
, route
->network
->static_routes
, route
);
135 assert(route
->network
->n_static_routes
> 0);
136 route
->network
->n_static_routes
--;
139 hashmap_remove(route
->network
->routes_by_section
, route
->section
);
142 network_config_section_free(route
->section
);
145 set_remove(route
->link
->routes
, route
);
146 set_remove(route
->link
->routes_foreign
, route
);
149 ordered_set_free_free(route
->multipath_routes
);
151 sd_event_source_unref(route
->expire
);
156 static void route_hash_func(const Route
*route
, struct siphash
*state
) {
159 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
161 switch (route
->family
) {
164 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
165 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
167 siphash24_compress(&route
->src_prefixlen
, sizeof(route
->src_prefixlen
), state
);
168 siphash24_compress(&route
->src
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
170 siphash24_compress(&route
->gw
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
172 siphash24_compress(&route
->prefsrc
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
174 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
175 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
176 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
177 siphash24_compress(&route
->protocol
, sizeof(route
->protocol
), state
);
178 siphash24_compress(&route
->scope
, sizeof(route
->scope
), state
);
179 siphash24_compress(&route
->type
, sizeof(route
->type
), state
);
181 siphash24_compress(&route
->initcwnd
, sizeof(route
->initcwnd
), state
);
182 siphash24_compress(&route
->initrwnd
, sizeof(route
->initrwnd
), state
);
186 /* treat any other address family as AF_UNSPEC */
191 static int route_compare_func(const Route
*a
, const Route
*b
) {
194 r
= CMP(a
->family
, b
->family
);
201 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
205 r
= memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
209 r
= CMP(a
->src_prefixlen
, b
->src_prefixlen
);
213 r
= memcmp(&a
->src
, &b
->src
, FAMILY_ADDRESS_SIZE(a
->family
));
217 r
= memcmp(&a
->gw
, &b
->gw
, FAMILY_ADDRESS_SIZE(a
->family
));
221 r
= memcmp(&a
->prefsrc
, &b
->prefsrc
, FAMILY_ADDRESS_SIZE(a
->family
));
225 r
= CMP(a
->tos
, b
->tos
);
229 r
= CMP(a
->priority
, b
->priority
);
233 r
= CMP(a
->table
, b
->table
);
237 r
= CMP(a
->protocol
, b
->protocol
);
241 r
= CMP(a
->scope
, b
->scope
);
245 r
= CMP(a
->type
, b
->type
);
249 r
= CMP(a
->initcwnd
, b
->initcwnd
);
253 r
= CMP(a
->initrwnd
, b
->initrwnd
);
259 /* treat any other address family as AF_UNSPEC */
264 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
271 bool route_equal(Route
*r1
, Route
*r2
) {
278 return route_compare_func(r1
, r2
) == 0;
281 int route_get(Link
*link
, Route
*in
, Route
**ret
) {
288 existing
= set_get(link
->routes
, in
);
295 existing
= set_get(link
->routes_foreign
, in
);
305 static int route_add_internal(Link
*link
, Set
**routes
, Route
*in
, Route
**ret
) {
307 _cleanup_(route_freep
) Route
*route
= NULL
;
314 r
= route_new(&route
);
318 route
->family
= in
->family
;
319 route
->src
= in
->src
;
320 route
->src_prefixlen
= in
->src_prefixlen
;
321 route
->dst
= in
->dst
;
322 route
->dst_prefixlen
= in
->dst_prefixlen
;
324 route
->prefsrc
= in
->prefsrc
;
325 route
->scope
= in
->scope
;
326 route
->protocol
= in
->protocol
;
327 route
->type
= in
->type
;
328 route
->tos
= in
->tos
;
329 route
->priority
= in
->priority
;
330 route
->table
= in
->table
;
331 route
->initcwnd
= in
->initcwnd
;
332 route
->initrwnd
= in
->initrwnd
;
333 route
->lifetime
= in
->lifetime
;
335 r
= set_ensure_put(routes
, &route_hash_ops
, 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_put(&link
->routes
, &route_hash_ops
, route
);
372 set_remove(link
->routes_foreign
, route
);
374 /* Route exists, do nothing */
385 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
390 assert(link
->ifname
);
392 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
395 r
= sd_netlink_message_get_errno(m
);
396 if (r
< 0 && r
!= -ESRCH
)
397 log_link_message_warning_errno(link
, m
, r
, "Could not drop route, ignoring");
402 int route_remove(Route
*route
, Link
*link
,
403 link_netlink_message_handler_t callback
) {
405 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
409 assert(link
->manager
);
410 assert(link
->manager
->rtnl
);
411 assert(link
->ifindex
> 0);
412 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
414 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
415 RTM_DELROUTE
, route
->family
,
418 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
421 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
422 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
424 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
425 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
426 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
428 if (!in_addr_is_null(route
->family
, &route
->src
))
429 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
430 if (!in_addr_is_null(route
->family
, &route
->gw
))
431 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
432 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
433 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
435 log_link_debug(link
, "Removing route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
436 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
437 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
438 format_route_table(route
->table
, table
, sizeof(table
)),
439 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
440 strna(route_type_to_string(route
->type
)));
443 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
444 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
446 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
449 if (route
->dst_prefixlen
) {
450 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
452 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
454 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
456 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
459 if (route
->src_prefixlen
) {
460 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
462 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
464 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
466 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
469 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
470 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
472 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
475 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
477 return log_link_error_errno(link
, r
, "Could not set scope: %m");
479 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
481 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
483 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
484 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
486 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
489 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
490 callback
?: route_remove_handler
,
491 link_netlink_destroy_callback
, link
);
493 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
500 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
501 Route
*route
= userdata
;
506 r
= route_remove(route
, route
->link
, NULL
);
508 log_link_warning_errno(route
->link
, r
, "Could not remove route: %m");
515 static int append_nexthop_one(Route
*route
, MultipathRoute
*m
, struct rtattr
**rta
, size_t offset
) {
516 struct rtnexthop
*rtnh
;
517 struct rtattr
*new_rta
;
525 new_rta
= realloc(*rta
, RTA_ALIGN((*rta
)->rta_len
) + RTA_SPACE(sizeof(struct rtnexthop
)));
530 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
531 *rtnh
= (struct rtnexthop
) {
532 .rtnh_len
= sizeof(*rtnh
),
533 .rtnh_ifindex
= m
->ifindex
,
534 .rtnh_hops
= m
->weight
> 0 ? m
->weight
- 1 : 0,
537 (*rta
)->rta_len
+= sizeof(struct rtnexthop
);
539 if (route
->family
== m
->gateway
.family
) {
540 r
= rtattr_append_attribute(rta
, RTA_GATEWAY
, &m
->gateway
.address
, FAMILY_ADDRESS_SIZE(m
->gateway
.family
));
543 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
544 rtnh
->rtnh_len
+= RTA_SPACE(FAMILY_ADDRESS_SIZE(m
->gateway
.family
));
546 r
= rtattr_append_attribute(rta
, RTA_VIA
, &m
->gateway
, FAMILY_ADDRESS_SIZE(m
->gateway
.family
) + sizeof(m
->gateway
.family
));
549 rtnh
= (struct rtnexthop
*)((uint8_t *) *rta
+ offset
);
550 rtnh
->rtnh_len
+= RTA_SPACE(FAMILY_ADDRESS_SIZE(m
->gateway
.family
) + sizeof(m
->gateway
.family
));
556 (*rta
)->rta_len
-= sizeof(struct rtnexthop
);
560 static int append_nexthops(Route
*route
, sd_netlink_message
*req
) {
561 _cleanup_free_
struct rtattr
*rta
= NULL
;
562 struct rtnexthop
*rtnh
;
568 if (ordered_set_isempty(route
->multipath_routes
))
571 rta
= new(struct rtattr
, 1);
575 *rta
= (struct rtattr
) {
576 .rta_type
= RTA_MULTIPATH
,
577 .rta_len
= RTA_LENGTH(0),
579 offset
= (uint8_t *) RTA_DATA(rta
) - (uint8_t *) rta
;
581 ORDERED_SET_FOREACH(m
, route
->multipath_routes
, i
) {
582 r
= append_nexthop_one(route
, m
, &rta
, offset
);
586 rtnh
= (struct rtnexthop
*)((uint8_t *) rta
+ offset
);
587 offset
= (uint8_t *) RTNH_NEXT(rtnh
) - (uint8_t *) rta
;
590 r
= sd_netlink_message_append_data(req
, RTA_MULTIPATH
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
600 link_netlink_message_handler_t callback
,
603 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
604 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
608 assert(link
->manager
);
609 assert(link
->manager
->rtnl
);
610 assert(link
->ifindex
> 0);
611 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
614 if (route_get(link
, route
, NULL
) <= 0 &&
615 set_size(link
->routes
) >= routes_max())
616 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
617 "Too many routes are configured, refusing: %m");
620 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
621 char scope
[ROUTE_SCOPE_STR_MAX
], table
[ROUTE_TABLE_STR_MAX
], protocol
[ROUTE_PROTOCOL_STR_MAX
];
623 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
624 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
625 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
627 if (!in_addr_is_null(route
->family
, &route
->src
))
628 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
629 if (!in_addr_is_null(route
->family
, &route
->gw
))
630 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
631 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
632 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
634 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, proto: %s, type: %s",
635 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
),
636 format_route_scope(route
->scope
, scope
, sizeof(scope
)),
637 format_route_table(route
->table
, table
, sizeof(table
)),
638 format_route_protocol(route
->protocol
, protocol
, sizeof(protocol
)),
639 strna(route_type_to_string(route
->type
)));
642 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
643 RTM_NEWROUTE
, route
->family
,
646 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
648 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
649 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
651 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
653 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
655 return log_link_error_errno(link
, r
, "Could not set route family: %m");
658 if (route
->dst_prefixlen
> 0) {
659 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
661 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
663 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
665 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
668 if (route
->src_prefixlen
> 0) {
669 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
671 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
673 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
675 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
678 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
679 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
681 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
684 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
686 return log_link_error_errno(link
, r
, "Could not set scope: %m");
688 if (route
->gateway_onlink
>= 0)
689 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
691 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
693 return log_link_error_errno(link
, r
, "Could not set flags: %m");
695 if (route
->table
!= RT_TABLE_MAIN
) {
696 if (route
->table
< 256) {
697 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
699 return log_link_error_errno(link
, r
, "Could not set route table: %m");
701 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
703 return log_link_error_errno(link
, r
, "Could not set route table: %m");
705 /* Table attribute to allow more than 256. */
706 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
708 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
712 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
714 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
716 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
718 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
720 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
721 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
722 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
724 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
727 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
729 return log_link_error_errno(link
, r
, "Could not set route type: %m");
731 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
732 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
734 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
737 if (route
->ttl_propagate
>= 0) {
738 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
740 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
743 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
745 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
747 if (route
->mtu
> 0) {
748 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
750 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
753 if (route
->initcwnd
> 0) {
754 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
756 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
759 if (route
->initrwnd
> 0) {
760 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
762 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
765 if (route
->quickack
>= 0) {
766 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
768 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
771 if (route
->fast_open_no_cookie
>= 0) {
772 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
774 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
777 r
= sd_netlink_message_close_container(req
);
779 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
781 r
= append_nexthops(route
, req
);
783 return log_link_error_errno(link
, r
, "Could not append RTA_MULTIPATH attribute: %m");
785 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
786 link_netlink_destroy_callback
, link
);
788 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
792 r
= route_add(link
, route
, &route
);
794 return log_link_error_errno(link
, r
, "Could not add route: %m");
796 /* TODO: drop expiration handling once it can be pushed into the kernel */
797 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
798 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
799 route
->lifetime
, 0, route_expire_handler
, route
);
801 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
804 sd_event_source_unref(route
->expire
);
805 route
->expire
= TAKE_PTR(expire
);
813 int network_add_ipv4ll_route(Network
*network
) {
814 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
819 if (!network
->ipv4ll_route
)
822 /* IPv4LLRoute= is in [Network] section. */
823 r
= route_new_static(network
, NULL
, 0, &n
);
827 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
832 n
->dst_prefixlen
= 16;
833 n
->scope
= RT_SCOPE_LINK
;
836 n
->priority
= IPV4LL_ROUTE_METRIC
;
837 n
->protocol
= RTPROT_STATIC
;
843 int network_add_default_route_on_device(Network
*network
) {
844 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
849 if (!network
->default_route_on_device
)
852 /* DefaultRouteOnDevice= is in [Network] section. */
853 r
= route_new_static(network
, NULL
, 0, &n
);
858 n
->scope
= RT_SCOPE_LINK
;
860 n
->protocol
= RTPROT_STATIC
;
866 static const char * const route_type_table
[__RTN_MAX
] = {
867 [RTN_UNICAST
] = "unicast",
868 [RTN_LOCAL
] = "local",
869 [RTN_BROADCAST
] = "broadcast",
870 [RTN_ANYCAST
] = "anycast",
871 [RTN_MULTICAST
] = "multicast",
872 [RTN_BLACKHOLE
] = "blackhole",
873 [RTN_UNREACHABLE
] = "unreachable",
874 [RTN_PROHIBIT
] = "prohibit",
875 [RTN_THROW
] = "throw",
877 [RTN_XRESOLVE
] = "xresolve",
880 assert_cc(__RTN_MAX
<= UCHAR_MAX
);
881 DEFINE_STRING_TABLE_LOOKUP(route_type
, int);
883 static const char * const route_scope_table
[] = {
884 [RT_SCOPE_UNIVERSE
] = "global",
885 [RT_SCOPE_SITE
] = "site",
886 [RT_SCOPE_LINK
] = "link",
887 [RT_SCOPE_HOST
] = "host",
888 [RT_SCOPE_NOWHERE
] = "nowhere",
891 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_scope
, int);
893 const char *format_route_scope(int scope
, char *buf
, size_t size
) {
897 s
= route_scope_to_string(scope
);
899 strpcpy(&p
, size
, s
);
901 strpcpyf(&p
, size
, "%d", scope
);
906 static const char * const route_table_table
[] = {
907 [RT_TABLE_DEFAULT
] = "default",
908 [RT_TABLE_MAIN
] = "main",
909 [RT_TABLE_LOCAL
] = "local",
912 DEFINE_PRIVATE_STRING_TABLE_LOOKUP(route_table
, int);
914 const char *format_route_table(int table
, char *buf
, size_t size
) {
918 s
= route_table_to_string(table
);
920 strpcpy(&p
, size
, s
);
922 strpcpyf(&p
, size
, "%d", table
);
927 static const char * const route_protocol_table
[] = {
928 [RTPROT_KERNEL
] = "kernel",
929 [RTPROT_BOOT
] = "boot",
930 [RTPROT_STATIC
] = "static",
933 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(route_protocol
, int);
935 static const char * const route_protocol_full_table
[] = {
936 [RTPROT_REDIRECT
] = "redirect",
937 [RTPROT_KERNEL
] = "kernel",
938 [RTPROT_BOOT
] = "boot",
939 [RTPROT_STATIC
] = "static",
940 [RTPROT_GATED
] = "gated",
942 [RTPROT_MRT
] = "mrt",
943 [RTPROT_ZEBRA
] = "zebra",
944 [RTPROT_BIRD
] = "bird",
945 [RTPROT_DNROUTED
] = "dnrouted",
946 [RTPROT_XORP
] = "xorp",
947 [RTPROT_NTK
] = "ntk",
948 [RTPROT_DHCP
] = "dhcp",
949 [RTPROT_MROUTED
] = "mrouted",
950 [RTPROT_BABEL
] = "babel",
951 [RTPROT_BGP
] = "bgp",
952 [RTPROT_ISIS
] = "isis",
953 [RTPROT_OSPF
] = "ospf",
954 [RTPROT_RIP
] = "rip",
955 [RTPROT_EIGRP
] = "eigrp",
958 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(route_protocol_full
, int);
960 const char *format_route_protocol(int protocol
, char *buf
, size_t size
) {
964 s
= route_protocol_full_to_string(protocol
);
966 strpcpy(&p
, size
, s
);
968 strpcpyf(&p
, size
, "%d", protocol
);
973 int config_parse_gateway(
975 const char *filename
,
978 unsigned section_line
,
985 Network
*network
= userdata
;
986 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
995 if (streq(section
, "Network")) {
996 /* we are not in an Route section, so treat
997 * this as the special '0' section */
998 r
= route_new_static(network
, NULL
, 0, &n
);
1002 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1003 "Failed to allocate route, ignoring assignment: %m");
1007 r
= route_new_static(network
, filename
, section_line
, &n
);
1011 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1012 "Failed to allocate route, ignoring assignment: %m");
1016 if (streq(rvalue
, "_dhcp")) {
1017 n
->gateway_from_dhcp
= true;
1023 if (n
->family
== AF_UNSPEC
)
1024 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
1026 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
1028 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1029 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1037 int config_parse_preferred_src(
1039 const char *filename
,
1041 const char *section
,
1042 unsigned section_line
,
1049 Network
*network
= userdata
;
1050 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1059 r
= route_new_static(network
, filename
, section_line
, &n
);
1063 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1064 "Failed to allocate route, ignoring assignment: %m");
1068 if (n
->family
== AF_UNSPEC
)
1069 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
1071 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
1073 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
,
1074 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1082 int config_parse_destination(
1084 const char *filename
,
1086 const char *section
,
1087 unsigned section_line
,
1094 Network
*network
= userdata
;
1095 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1096 union in_addr_union
*buffer
;
1097 unsigned char *prefixlen
;
1106 r
= route_new_static(network
, filename
, section_line
, &n
);
1110 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1111 "Failed to allocate route, ignoring assignment: %m");
1115 if (streq(lvalue
, "Destination")) {
1117 prefixlen
= &n
->dst_prefixlen
;
1118 } else if (streq(lvalue
, "Source")) {
1120 prefixlen
= &n
->src_prefixlen
;
1122 assert_not_reached(lvalue
);
1124 if (n
->family
== AF_UNSPEC
)
1125 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
1127 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
1129 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
,
1130 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
1138 int config_parse_route_priority(
1140 const char *filename
,
1142 const char *section
,
1143 unsigned section_line
,
1150 Network
*network
= userdata
;
1151 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1160 r
= route_new_static(network
, filename
, section_line
, &n
);
1164 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1165 "Failed to allocate route, ignoring assignment: %m");
1169 r
= safe_atou32(rvalue
, &n
->priority
);
1171 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1172 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
1180 int config_parse_route_scope(
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
;
1202 r
= route_new_static(network
, filename
, section_line
, &n
);
1206 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1207 "Failed to allocate route, ignoring assignment: %m");
1211 r
= route_scope_from_string(rvalue
);
1213 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
1218 n
->scope_set
= true;
1223 int config_parse_route_table(
1225 const char *filename
,
1227 const char *section
,
1228 unsigned section_line
,
1235 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1236 Network
*network
= userdata
;
1245 r
= route_new_static(network
, filename
, section_line
, &n
);
1249 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1250 "Failed to allocate route, ignoring assignment: %m");
1254 r
= route_table_from_string(rvalue
);
1258 r
= safe_atou32(rvalue
, &n
->table
);
1260 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1261 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1266 n
->table_set
= true;
1271 int config_parse_route_boolean(
1273 const char *filename
,
1275 const char *section
,
1276 unsigned section_line
,
1283 Network
*network
= userdata
;
1284 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1293 r
= route_new_static(network
, filename
, section_line
, &n
);
1297 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1298 "Failed to allocate route, ignoring assignment: %m");
1302 r
= parse_boolean(rvalue
);
1304 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1305 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1309 if (STR_IN_SET(lvalue
, "GatewayOnLink", "GatewayOnlink"))
1310 n
->gateway_onlink
= r
;
1311 else if (streq(lvalue
, "QuickAck"))
1313 else if (streq(lvalue
, "FastOpenNoCookie"))
1314 n
->fast_open_no_cookie
= r
;
1315 else if (streq(lvalue
, "TTLPropagate"))
1316 n
->ttl_propagate
= r
;
1318 assert_not_reached("Invalid lvalue");
1324 int config_parse_ipv6_route_preference(
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 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1345 "Failed to allocate route, ignoring assignment: %m");
1349 if (streq(rvalue
, "low"))
1350 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1351 else if (streq(rvalue
, "medium"))
1352 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1353 else if (streq(rvalue
, "high"))
1354 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1356 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1364 int config_parse_route_protocol(
1366 const char *filename
,
1368 const char *section
,
1369 unsigned section_line
,
1376 Network
*network
= userdata
;
1377 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1380 r
= route_new_static(network
, filename
, section_line
, &n
);
1384 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1385 "Failed to allocate route, ignoring assignment: %m");
1389 r
= route_protocol_from_string(rvalue
);
1393 r
= safe_atou8(rvalue
, &n
->protocol
);
1395 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1396 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1405 int config_parse_route_type(
1407 const char *filename
,
1409 const char *section
,
1410 unsigned section_line
,
1417 Network
*network
= userdata
;
1418 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1421 r
= route_new_static(network
, filename
, section_line
, &n
);
1425 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1426 "Failed to allocate route, ignoring assignment: %m");
1430 t
= route_type_from_string(rvalue
);
1432 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1433 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1437 n
->type
= (unsigned char) t
;
1443 int config_parse_tcp_window(
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
;
1466 r
= route_new_static(network
, filename
, section_line
, &n
);
1470 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1471 "Failed to allocate route, ignoring assignment: %m");
1475 r
= parse_size(rvalue
, 1024, &k
);
1477 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1478 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1481 if (k
> UINT32_MAX
) {
1482 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1483 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1487 if (streq(lvalue
, "InitialCongestionWindow"))
1489 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1492 assert_not_reached("Invalid TCP window type.");
1498 int config_parse_route_mtu(
1500 const char *filename
,
1502 const char *section
,
1503 unsigned section_line
,
1510 Network
*network
= userdata
;
1511 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1520 r
= route_new_static(network
, filename
, section_line
, &n
);
1524 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1525 "Failed to allocate route, ignoring assignment: %m");
1529 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1537 int config_parse_multipath_route(
1539 const char *filename
,
1541 const char *section
,
1542 unsigned section_line
,
1549 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1550 _cleanup_free_
char *word
= NULL
, *buf
= NULL
;
1551 _cleanup_free_ MultipathRoute
*m
= NULL
;
1552 Network
*network
= userdata
;
1553 const char *p
, *ip
, *dev
;
1554 union in_addr_union a
;
1563 r
= route_new_static(network
, filename
, section_line
, &n
);
1567 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1568 "Failed to allocate route, ignoring assignment: %m");
1572 if (isempty(rvalue
)) {
1573 n
->multipath_routes
= ordered_set_free_free(n
->multipath_routes
);
1577 m
= new0(MultipathRoute
, 1);
1582 r
= extract_first_word(&p
, &word
, NULL
, 0);
1586 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1587 "Invalid multipath route option, ignoring assignment: %s", rvalue
);
1591 dev
= strchr(word
, '@');
1593 buf
= strndup(word
, dev
- word
);
1601 r
= in_addr_from_string_auto(ip
, &family
, &a
);
1603 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1604 "Invalid multipath route gateway '%s', ignoring assignment: %m", rvalue
);
1607 m
->gateway
.address
= a
;
1608 m
->gateway
.family
= family
;
1611 r
= resolve_interface(NULL
, dev
);
1613 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1614 "Invalid interface name or index, ignoring assignment: %s", dev
);
1621 r
= safe_atou32(p
, &m
->weight
);
1623 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1624 "Invalid multipath route weight, ignoring assignment: %s", p
);
1627 if (m
->weight
== 0 || m
->weight
> 256) {
1628 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1629 "Invalid multipath route weight, ignoring assignment: %s", p
);
1634 r
= ordered_set_ensure_allocated(&n
->multipath_routes
, NULL
);
1638 r
= ordered_set_put(n
->multipath_routes
, m
);
1640 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1641 "Failed to store multipath route, ignoring assignment: %m");
1650 int route_section_verify(Route
*route
, Network
*network
) {
1651 if (section_is_invalid(route
->section
))
1654 if (route
->family
== AF_UNSPEC
) {
1655 assert(route
->section
);
1657 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1658 "%s: Route section without Gateway=, Destination=, Source=, "
1659 "or PreferredSource= field configured. "
1660 "Ignoring [Route] section from line %u.",
1661 route
->section
->filename
, route
->section
->line
);
1664 if (!route
->table_set
&& network
->vrf
) {
1665 route
->table
= VRF(network
->vrf
)->table
;
1666 route
->table_set
= true;
1669 if (!route
->table_set
&& IN_SET(route
->type
, RTN_LOCAL
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_NAT
))
1670 route
->table
= RT_TABLE_LOCAL
;
1672 if (!route
->scope_set
&& route
->family
!= AF_INET6
) {
1673 if (IN_SET(route
->type
, RTN_LOCAL
, RTN_NAT
))
1674 route
->scope
= RT_SCOPE_HOST
;
1675 else if (IN_SET(route
->type
, RTN_BROADCAST
, RTN_ANYCAST
, RTN_MULTICAST
))
1676 route
->scope
= RT_SCOPE_LINK
;
1679 if (network
->n_static_addresses
== 0 &&
1680 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1681 route
->gateway_onlink
< 0) {
1682 log_warning("%s: Gateway= without static address configured. "
1683 "Enabling GatewayOnLink= option.",
1685 route
->gateway_onlink
= true;