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-manager.h"
11 #include "networkd-route.h"
12 #include "parse-util.h"
14 #include "string-util.h"
15 #include "sysctl-util.h"
18 #define ROUTES_DEFAULT_MAX_PER_FAMILY 4096U
20 static unsigned routes_max(void) {
21 static thread_local
unsigned cached
= 0;
23 _cleanup_free_
char *s4
= NULL
, *s6
= NULL
;
24 unsigned val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
29 if (sysctl_read("net/ipv4/route/max_size", &s4
) >= 0) {
31 if (safe_atou(s4
, &val4
) >= 0 &&
33 /* This is the default "no limit" value in the kernel */
34 val4
= ROUTES_DEFAULT_MAX_PER_FAMILY
;
37 if (sysctl_read("net/ipv6/route/max_size", &s6
) >= 0) {
39 (void) safe_atou(s6
, &val6
);
42 cached
= MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val4
) +
43 MAX(ROUTES_DEFAULT_MAX_PER_FAMILY
, val6
);
47 int route_new(Route
**ret
) {
48 _cleanup_(route_freep
) Route
*route
= NULL
;
50 route
= new(Route
, 1);
56 .scope
= RT_SCOPE_UNIVERSE
,
57 .protocol
= RTPROT_UNSPEC
,
59 .table
= RT_TABLE_MAIN
,
60 .lifetime
= USEC_INFINITY
,
62 .fast_open_no_cookie
= -1,
67 *ret
= TAKE_PTR(route
);
72 static int route_new_static(Network
*network
, const char *filename
, unsigned section_line
, Route
**ret
) {
73 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
74 _cleanup_(route_freep
) Route
*route
= NULL
;
79 assert(!!filename
== (section_line
> 0));
82 r
= network_config_section_new(filename
, section_line
, &n
);
86 route
= hashmap_get(network
->routes_by_section
, n
);
88 *ret
= TAKE_PTR(route
);
94 if (network
->n_static_routes
>= routes_max())
97 r
= route_new(&route
);
101 route
->protocol
= RTPROT_STATIC
;
102 route
->network
= network
;
103 LIST_PREPEND(routes
, network
->static_routes
, route
);
104 network
->n_static_routes
++;
107 route
->section
= TAKE_PTR(n
);
109 r
= hashmap_ensure_allocated(&network
->routes_by_section
, &network_config_hash_ops
);
113 r
= hashmap_put(network
->routes_by_section
, route
->section
, route
);
118 *ret
= TAKE_PTR(route
);
123 void route_free(Route
*route
) {
127 if (route
->network
) {
128 LIST_REMOVE(routes
, route
->network
->static_routes
, route
);
130 assert(route
->network
->n_static_routes
> 0);
131 route
->network
->n_static_routes
--;
134 hashmap_remove(route
->network
->routes_by_section
, route
->section
);
137 network_config_section_free(route
->section
);
140 set_remove(route
->link
->routes
, route
);
141 set_remove(route
->link
->routes_foreign
, route
);
144 sd_event_source_unref(route
->expire
);
149 static void route_hash_func(const Route
*route
, struct siphash
*state
) {
152 siphash24_compress(&route
->family
, sizeof(route
->family
), state
);
154 switch (route
->family
) {
157 /* Equality of routes are given by the 4-touple
158 (dst_prefix,dst_prefixlen,tos,priority,table) */
159 siphash24_compress(&route
->dst
, FAMILY_ADDRESS_SIZE(route
->family
), state
);
160 siphash24_compress(&route
->dst_prefixlen
, sizeof(route
->dst_prefixlen
), state
);
161 siphash24_compress(&route
->tos
, sizeof(route
->tos
), state
);
162 siphash24_compress(&route
->priority
, sizeof(route
->priority
), state
);
163 siphash24_compress(&route
->table
, sizeof(route
->table
), state
);
167 /* treat any other address family as AF_UNSPEC */
172 static int route_compare_func(const Route
*a
, const Route
*b
) {
175 r
= CMP(a
->family
, b
->family
);
182 r
= CMP(a
->dst_prefixlen
, b
->dst_prefixlen
);
186 r
= CMP(a
->tos
, b
->tos
);
190 r
= CMP(a
->priority
, b
->priority
);
194 r
= CMP(a
->table
, b
->table
);
198 return memcmp(&a
->dst
, &b
->dst
, FAMILY_ADDRESS_SIZE(a
->family
));
200 /* treat any other address family as AF_UNSPEC */
205 DEFINE_PRIVATE_HASH_OPS(route_hash_ops
, Route
, route_hash_func
, route_compare_func
);
207 bool route_equal(Route
*r1
, Route
*r2
) {
214 return route_compare_func(r1
, r2
) == 0;
217 int route_get(Link
*link
,
219 const union in_addr_union
*dst
,
220 unsigned char dst_prefixlen
,
226 Route route
, *existing
;
234 .dst_prefixlen
= dst_prefixlen
,
236 .priority
= priority
,
240 existing
= set_get(link
->routes
, &route
);
247 existing
= set_get(link
->routes_foreign
, &route
);
257 static int route_add_internal(
261 const union in_addr_union
*dst
,
262 unsigned char dst_prefixlen
,
268 _cleanup_(route_freep
) Route
*route
= NULL
;
275 r
= route_new(&route
);
279 route
->family
= family
;
281 route
->dst_prefixlen
= dst_prefixlen
;
283 route
->priority
= priority
;
284 route
->table
= table
;
286 r
= set_ensure_allocated(routes
, &route_hash_ops
);
290 r
= set_put(*routes
, route
);
304 int route_add_foreign(
307 const union in_addr_union
*dst
,
308 unsigned char dst_prefixlen
,
314 return route_add_internal(link
, &link
->routes_foreign
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, ret
);
317 int route_add(Link
*link
,
319 const union in_addr_union
*dst
,
320 unsigned char dst_prefixlen
,
329 r
= route_get(link
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
331 /* Route does not exist, create a new one */
332 r
= route_add_internal(link
, &link
->routes
, family
, dst
, dst_prefixlen
, tos
, priority
, table
, &route
);
336 /* Take over a foreign route */
337 r
= set_ensure_allocated(&link
->routes
, &route_hash_ops
);
341 r
= set_put(link
->routes
, route
);
345 set_remove(link
->routes_foreign
, route
);
347 /* Route exists, do nothing */
358 void route_update(Route
*route
,
359 const union in_addr_union
*src
,
360 unsigned char src_prefixlen
,
361 const union in_addr_union
*gw
,
362 const union in_addr_union
*prefsrc
,
364 unsigned char protocol
,
365 unsigned char type
) {
368 assert(src
|| src_prefixlen
== 0);
370 route
->src
= src
? *src
: IN_ADDR_NULL
;
371 route
->src_prefixlen
= src_prefixlen
;
372 route
->gw
= gw
? *gw
: IN_ADDR_NULL
;
373 route
->prefsrc
= prefsrc
? *prefsrc
: IN_ADDR_NULL
;
374 route
->scope
= scope
;
375 route
->protocol
= protocol
;
379 static int route_remove_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
384 assert(link
->ifname
);
386 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
389 r
= sd_netlink_message_get_errno(m
);
390 if (r
< 0 && r
!= -ESRCH
)
391 log_link_warning_errno(link
, r
, "Could not drop route: %m");
396 int route_remove(Route
*route
, Link
*link
,
397 link_netlink_message_handler_t callback
) {
399 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
403 assert(link
->manager
);
404 assert(link
->manager
->rtnl
);
405 assert(link
->ifindex
> 0);
406 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
408 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
409 RTM_DELROUTE
, route
->family
,
412 return log_link_error_errno(link
, r
, "Could not create RTM_DELROUTE message: %m");
414 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
415 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
417 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
420 if (route
->dst_prefixlen
) {
421 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
423 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
425 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
427 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
430 if (route
->src_prefixlen
) {
431 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
433 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
435 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
437 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
440 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
441 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
443 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
446 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
448 return log_link_error_errno(link
, r
, "Could not set scope: %m");
450 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
452 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
454 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
455 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
457 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
460 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
,
461 callback
?: route_remove_handler
,
462 link_netlink_destroy_callback
, link
);
464 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
471 int route_expire_handler(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
472 Route
*route
= userdata
;
477 r
= route_remove(route
, route
->link
, NULL
);
479 log_warning_errno(r
, "Could not remove route: %m");
489 link_netlink_message_handler_t callback
) {
491 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
492 _cleanup_(sd_event_source_unrefp
) sd_event_source
*expire
= NULL
;
497 assert(link
->manager
);
498 assert(link
->manager
->rtnl
);
499 assert(link
->ifindex
> 0);
500 assert(IN_SET(route
->family
, AF_INET
, AF_INET6
));
503 if (route
->family
== AF_INET6
&& link_sysctl_ipv6_enabled(link
) == 0) {
504 log_link_warning(link
, "An IPv6 route is requested, but IPv6 is disabled by sysctl, ignoring.");
508 if (route_get(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, NULL
) <= 0 &&
509 set_size(link
->routes
) >= routes_max())
510 return log_link_error_errno(link
, SYNTHETIC_ERRNO(E2BIG
),
511 "Too many routes are configured, refusing: %m");
514 _cleanup_free_
char *dst
= NULL
, *dst_prefixlen
= NULL
, *src
= NULL
, *gw
= NULL
, *prefsrc
= NULL
;
516 if (!in_addr_is_null(route
->family
, &route
->dst
)) {
517 (void) in_addr_to_string(route
->family
, &route
->dst
, &dst
);
518 (void) asprintf(&dst_prefixlen
, "/%u", route
->dst_prefixlen
);
520 if (!in_addr_is_null(route
->family
, &route
->src
))
521 (void) in_addr_to_string(route
->family
, &route
->src
, &src
);
522 if (!in_addr_is_null(route
->family
, &route
->gw
))
523 (void) in_addr_to_string(route
->family
, &route
->gw
, &gw
);
524 if (!in_addr_is_null(route
->family
, &route
->prefsrc
))
525 (void) in_addr_to_string(route
->family
, &route
->prefsrc
, &prefsrc
);
527 log_link_debug(link
, "Configuring route: dst: %s%s, src: %s, gw: %s, prefsrc: %s",
528 strna(dst
), strempty(dst_prefixlen
), strna(src
), strna(gw
), strna(prefsrc
));
531 r
= sd_rtnl_message_new_route(link
->manager
->rtnl
, &req
,
532 RTM_NEWROUTE
, route
->family
,
535 return log_link_error_errno(link
, r
, "Could not create RTM_NEWROUTE message: %m");
537 if (in_addr_is_null(route
->family
, &route
->gw
) == 0) {
538 r
= netlink_message_append_in_addr_union(req
, RTA_GATEWAY
, route
->family
, &route
->gw
);
540 return log_link_error_errno(link
, r
, "Could not append RTA_GATEWAY attribute: %m");
542 r
= sd_rtnl_message_route_set_family(req
, route
->family
);
544 return log_link_error_errno(link
, r
, "Could not set route family: %m");
547 if (route
->dst_prefixlen
) {
548 r
= netlink_message_append_in_addr_union(req
, RTA_DST
, route
->family
, &route
->dst
);
550 return log_link_error_errno(link
, r
, "Could not append RTA_DST attribute: %m");
552 r
= sd_rtnl_message_route_set_dst_prefixlen(req
, route
->dst_prefixlen
);
554 return log_link_error_errno(link
, r
, "Could not set destination prefix length: %m");
557 if (route
->src_prefixlen
) {
558 r
= netlink_message_append_in_addr_union(req
, RTA_SRC
, route
->family
, &route
->src
);
560 return log_link_error_errno(link
, r
, "Could not append RTA_SRC attribute: %m");
562 r
= sd_rtnl_message_route_set_src_prefixlen(req
, route
->src_prefixlen
);
564 return log_link_error_errno(link
, r
, "Could not set source prefix length: %m");
567 if (in_addr_is_null(route
->family
, &route
->prefsrc
) == 0) {
568 r
= netlink_message_append_in_addr_union(req
, RTA_PREFSRC
, route
->family
, &route
->prefsrc
);
570 return log_link_error_errno(link
, r
, "Could not append RTA_PREFSRC attribute: %m");
573 r
= sd_rtnl_message_route_set_scope(req
, route
->scope
);
575 return log_link_error_errno(link
, r
, "Could not set scope: %m");
577 if (route
->gateway_onlink
>= 0)
578 SET_FLAG(route
->flags
, RTNH_F_ONLINK
, route
->gateway_onlink
);
580 r
= sd_rtnl_message_route_set_flags(req
, route
->flags
);
582 return log_link_error_errno(link
, r
, "Could not set flags: %m");
584 if (route
->table
!= RT_TABLE_MAIN
) {
585 if (route
->table
< 256) {
586 r
= sd_rtnl_message_route_set_table(req
, route
->table
);
588 return log_link_error_errno(link
, r
, "Could not set route table: %m");
590 r
= sd_rtnl_message_route_set_table(req
, RT_TABLE_UNSPEC
);
592 return log_link_error_errno(link
, r
, "Could not set route table: %m");
594 /* Table attribute to allow more than 256. */
595 r
= sd_netlink_message_append_data(req
, RTA_TABLE
, &route
->table
, sizeof(route
->table
));
597 return log_link_error_errno(link
, r
, "Could not append RTA_TABLE attribute: %m");
601 r
= sd_netlink_message_append_u32(req
, RTA_PRIORITY
, route
->priority
);
603 return log_link_error_errno(link
, r
, "Could not append RTA_PRIORITY attribute: %m");
605 r
= sd_netlink_message_append_u8(req
, RTA_PREF
, route
->pref
);
607 return log_link_error_errno(link
, r
, "Could not append RTA_PREF attribute: %m");
609 if (route
->lifetime
!= USEC_INFINITY
&& kernel_route_expiration_supported()) {
610 r
= sd_netlink_message_append_u32(req
, RTA_EXPIRES
,
611 DIV_ROUND_UP(usec_sub_unsigned(route
->lifetime
, now(clock_boottime_or_monotonic())), USEC_PER_SEC
));
613 return log_link_error_errno(link
, r
, "Could not append RTA_EXPIRES attribute: %m");
616 r
= sd_rtnl_message_route_set_type(req
, route
->type
);
618 return log_link_error_errno(link
, r
, "Could not set route type: %m");
620 if (!IN_SET(route
->type
, RTN_UNREACHABLE
, RTN_PROHIBIT
, RTN_BLACKHOLE
, RTN_THROW
)) {
621 r
= sd_netlink_message_append_u32(req
, RTA_OIF
, link
->ifindex
);
623 return log_link_error_errno(link
, r
, "Could not append RTA_OIF attribute: %m");
626 if (route
->ttl_propagate
>= 0) {
627 r
= sd_netlink_message_append_u8(req
, RTA_TTL_PROPAGATE
, route
->ttl_propagate
);
629 return log_link_error_errno(link
, r
, "Could not append RTA_TTL_PROPAGATE attribute: %m");
632 r
= sd_netlink_message_open_container(req
, RTA_METRICS
);
634 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
636 if (route
->mtu
> 0) {
637 r
= sd_netlink_message_append_u32(req
, RTAX_MTU
, route
->mtu
);
639 return log_link_error_errno(link
, r
, "Could not append RTAX_MTU attribute: %m");
642 if (route
->initcwnd
> 0) {
643 r
= sd_netlink_message_append_u32(req
, RTAX_INITCWND
, route
->initcwnd
);
645 return log_link_error_errno(link
, r
, "Could not append RTAX_INITCWND attribute: %m");
648 if (route
->initrwnd
> 0) {
649 r
= sd_netlink_message_append_u32(req
, RTAX_INITRWND
, route
->initrwnd
);
651 return log_link_error_errno(link
, r
, "Could not append RTAX_INITRWND attribute: %m");
654 if (route
->quickack
>= 0) {
655 r
= sd_netlink_message_append_u32(req
, RTAX_QUICKACK
, route
->quickack
);
657 return log_link_error_errno(link
, r
, "Could not append RTAX_QUICKACK attribute: %m");
660 if (route
->fast_open_no_cookie
>= 0) {
661 r
= sd_netlink_message_append_u32(req
, RTAX_FASTOPEN_NO_COOKIE
, route
->fast_open_no_cookie
);
663 return log_link_error_errno(link
, r
, "Could not append RTAX_FASTOPEN_NO_COOKIE attribute: %m");
666 r
= sd_netlink_message_close_container(req
);
668 return log_link_error_errno(link
, r
, "Could not append RTA_METRICS attribute: %m");
670 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, callback
,
671 link_netlink_destroy_callback
, link
);
673 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
677 lifetime
= route
->lifetime
;
679 r
= route_add(link
, route
->family
, &route
->dst
, route
->dst_prefixlen
, route
->tos
, route
->priority
, route
->table
, &route
);
681 return log_link_error_errno(link
, r
, "Could not add route: %m");
683 /* TODO: drop expiration handling once it can be pushed into the kernel */
684 route
->lifetime
= lifetime
;
686 if (route
->lifetime
!= USEC_INFINITY
&& !kernel_route_expiration_supported()) {
687 r
= sd_event_add_time(link
->manager
->event
, &expire
, clock_boottime_or_monotonic(),
688 route
->lifetime
, 0, route_expire_handler
, route
);
690 return log_link_error_errno(link
, r
, "Could not arm expiration timer: %m");
693 sd_event_source_unref(route
->expire
);
694 route
->expire
= TAKE_PTR(expire
);
699 int network_add_ipv4ll_route(Network
*network
) {
700 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
705 if (!network
->ipv4ll_route
)
708 /* IPv4LLRoute= is in [Network] section. */
709 r
= route_new_static(network
, NULL
, 0, &n
);
713 r
= in_addr_from_string(AF_INET
, "169.254.0.0", &n
->dst
);
718 n
->dst_prefixlen
= 16;
719 n
->scope
= RT_SCOPE_LINK
;
720 n
->priority
= IPV4LL_ROUTE_METRIC
;
721 n
->protocol
= RTPROT_STATIC
;
727 int network_add_default_route_on_device(Network
*network
) {
728 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
733 if (!network
->default_route_on_device
)
736 /* DefaultRouteOnDevice= 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
);
751 int config_parse_gateway(
753 const char *filename
,
756 unsigned section_line
,
763 Network
*network
= userdata
;
764 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
773 if (streq(section
, "Network")) {
774 /* we are not in an Route section, so treat
775 * this as the special '0' section */
776 r
= route_new_static(network
, NULL
, 0, &n
);
778 r
= route_new_static(network
, filename
, section_line
, &n
);
782 if (n
->family
== AF_UNSPEC
)
783 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->gw
);
785 r
= in_addr_from_string(n
->family
, rvalue
, &n
->gw
);
787 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
788 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
796 int config_parse_preferred_src(
798 const char *filename
,
801 unsigned section_line
,
808 Network
*network
= userdata
;
809 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
818 r
= route_new_static(network
, filename
, section_line
, &n
);
822 if (n
->family
== AF_UNSPEC
)
823 r
= in_addr_from_string_auto(rvalue
, &n
->family
, &n
->prefsrc
);
825 r
= in_addr_from_string(n
->family
, rvalue
, &n
->prefsrc
);
827 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
828 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
836 int config_parse_destination(
838 const char *filename
,
841 unsigned section_line
,
848 Network
*network
= userdata
;
849 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
850 union in_addr_union
*buffer
;
851 unsigned char *prefixlen
;
860 r
= route_new_static(network
, filename
, section_line
, &n
);
864 if (streq(lvalue
, "Destination")) {
866 prefixlen
= &n
->dst_prefixlen
;
867 } else if (streq(lvalue
, "Source")) {
869 prefixlen
= &n
->src_prefixlen
;
871 assert_not_reached(lvalue
);
873 if (n
->family
== AF_UNSPEC
)
874 r
= in_addr_prefix_from_string_auto(rvalue
, &n
->family
, buffer
, prefixlen
);
876 r
= in_addr_prefix_from_string(rvalue
, n
->family
, buffer
, prefixlen
);
878 log_syntax(unit
, LOG_ERR
, filename
, line
, EINVAL
,
879 "Invalid %s='%s', ignoring assignment: %m", lvalue
, rvalue
);
887 int config_parse_route_priority(
889 const char *filename
,
892 unsigned section_line
,
899 Network
*network
= userdata
;
900 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
909 r
= route_new_static(network
, filename
, section_line
, &n
);
913 r
= safe_atou32(rvalue
, &n
->priority
);
915 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
916 "Could not parse route priority \"%s\", ignoring assignment: %m", rvalue
);
924 int config_parse_route_scope(
926 const char *filename
,
929 unsigned section_line
,
936 Network
*network
= userdata
;
937 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
946 r
= route_new_static(network
, filename
, section_line
, &n
);
950 if (streq(rvalue
, "host"))
951 n
->scope
= RT_SCOPE_HOST
;
952 else if (streq(rvalue
, "link"))
953 n
->scope
= RT_SCOPE_LINK
;
954 else if (streq(rvalue
, "global"))
955 n
->scope
= RT_SCOPE_UNIVERSE
;
957 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route scope: %s", rvalue
);
965 int config_parse_route_table(
967 const char *filename
,
970 unsigned section_line
,
977 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
978 Network
*network
= userdata
;
987 r
= route_new_static(network
, filename
, section_line
, &n
);
991 r
= safe_atou32(rvalue
, &n
->table
);
993 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
994 "Could not parse route table number \"%s\", ignoring assignment: %m", rvalue
);
1002 int config_parse_gateway_onlink(
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
= parse_boolean(rvalue
);
1030 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1031 "Could not parse %s=\"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1035 n
->gateway_onlink
= r
;
1041 int config_parse_ipv6_route_preference(
1043 const char *filename
,
1045 const char *section
,
1046 unsigned section_line
,
1053 Network
*network
= userdata
;
1054 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1057 r
= route_new_static(network
, filename
, section_line
, &n
);
1061 if (streq(rvalue
, "low"))
1062 n
->pref
= ICMPV6_ROUTER_PREF_LOW
;
1063 else if (streq(rvalue
, "medium"))
1064 n
->pref
= ICMPV6_ROUTER_PREF_MEDIUM
;
1065 else if (streq(rvalue
, "high"))
1066 n
->pref
= ICMPV6_ROUTER_PREF_HIGH
;
1068 log_syntax(unit
, LOG_ERR
, filename
, line
, 0, "Unknown route preference: %s", rvalue
);
1076 int config_parse_route_protocol(
1078 const char *filename
,
1080 const char *section
,
1081 unsigned section_line
,
1088 Network
*network
= userdata
;
1089 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1092 r
= route_new_static(network
, filename
, section_line
, &n
);
1096 if (streq(rvalue
, "kernel"))
1097 n
->protocol
= RTPROT_KERNEL
;
1098 else if (streq(rvalue
, "boot"))
1099 n
->protocol
= RTPROT_BOOT
;
1100 else if (streq(rvalue
, "static"))
1101 n
->protocol
= RTPROT_STATIC
;
1103 r
= safe_atou8(rvalue
, &n
->protocol
);
1105 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1106 "Could not parse route protocol \"%s\", ignoring assignment: %m", rvalue
);
1115 int config_parse_route_type(
1117 const char *filename
,
1119 const char *section
,
1120 unsigned section_line
,
1127 Network
*network
= userdata
;
1128 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1131 r
= route_new_static(network
, filename
, section_line
, &n
);
1135 if (streq(rvalue
, "unicast"))
1136 n
->type
= RTN_UNICAST
;
1137 else if (streq(rvalue
, "blackhole"))
1138 n
->type
= RTN_BLACKHOLE
;
1139 else if (streq(rvalue
, "unreachable"))
1140 n
->type
= RTN_UNREACHABLE
;
1141 else if (streq(rvalue
, "prohibit"))
1142 n
->type
= RTN_PROHIBIT
;
1143 else if (streq(rvalue
, "throw"))
1144 n
->type
= RTN_THROW
;
1146 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1147 "Could not parse route type \"%s\", ignoring assignment: %m", rvalue
);
1155 int config_parse_tcp_window(
1157 const char *filename
,
1159 const char *section
,
1160 unsigned section_line
,
1167 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1168 Network
*network
= userdata
;
1178 r
= route_new_static(network
, filename
, section_line
, &n
);
1182 r
= parse_size(rvalue
, 1024, &k
);
1184 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
1185 "Could not parse TCP %s \"%s\", ignoring assignment: %m", lvalue
, rvalue
);
1188 if (k
> UINT32_MAX
) {
1189 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
1190 "Specified TCP %s \"%s\" is too large, ignoring assignment: %m", lvalue
, rvalue
);
1194 if (streq(lvalue
, "InitialCongestionWindow"))
1196 else if (streq(lvalue
, "InitialAdvertisedReceiveWindow"))
1199 assert_not_reached("Invalid TCP window type.");
1205 int config_parse_quickack(
1207 const char *filename
,
1209 const char *section
,
1210 unsigned section_line
,
1217 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1218 Network
*network
= userdata
;
1227 r
= route_new_static(network
, filename
, section_line
, &n
);
1231 k
= parse_boolean(rvalue
);
1233 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1234 "Failed to parse TCP quickack, ignoring: %s", rvalue
);
1243 int config_parse_fast_open_no_cookie(
1245 const char *filename
,
1247 const char *section
,
1248 unsigned section_line
,
1255 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1256 Network
*network
= userdata
;
1265 r
= route_new_static(network
, filename
, section_line
, &n
);
1269 k
= parse_boolean(rvalue
);
1271 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1272 "Failed to parse TCP fastopen no cookie, ignoring: %s", rvalue
);
1276 n
->fast_open_no_cookie
= k
;
1281 int config_parse_route_mtu(
1283 const char *filename
,
1285 const char *section
,
1286 unsigned section_line
,
1293 Network
*network
= userdata
;
1294 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1303 r
= route_new_static(network
, filename
, section_line
, &n
);
1307 r
= config_parse_mtu(unit
, filename
, line
, section
, section_line
, lvalue
, ltype
, rvalue
, &n
->mtu
, userdata
);
1315 int config_parse_route_ttl_propagate(
1317 const char *filename
,
1319 const char *section
,
1320 unsigned section_line
,
1327 Network
*network
= userdata
;
1328 _cleanup_(route_free_or_set_invalidp
) Route
*n
= NULL
;
1337 r
= route_new_static(network
, filename
, section_line
, &n
);
1341 k
= parse_boolean(rvalue
);
1343 log_syntax(unit
, LOG_ERR
, filename
, line
, k
,
1344 "Failed to parse TTLPropagate= value, ignoring: %s", rvalue
);
1348 n
->ttl_propagate
= k
;
1354 int route_section_verify(Route
*route
, Network
*network
) {
1355 if (section_is_invalid(route
->section
))
1358 if (route
->family
== AF_UNSPEC
) {
1359 assert(route
->section
);
1361 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
1362 "%s: Route section without Gateway=, Destination=, Source=, "
1363 "or PreferredSource= field configured. "
1364 "Ignoring [Route] section from line %u.",
1365 route
->section
->filename
, route
->section
->line
);
1368 if (network
->n_static_addresses
== 0 &&
1369 in_addr_is_null(route
->family
, &route
->gw
) == 0 &&
1370 route
->gateway_onlink
< 0) {
1371 log_warning("%s: Gateway= without static address configured. "
1372 "Enabling GatewayOnLink= option.",
1374 route
->gateway_onlink
= true;