1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2017 Intel Corporation. All rights reserved.
6 #include <netinet/icmp6.h>
9 #include "dns-domain.h"
10 #include "networkd-address-generation.h"
11 #include "networkd-address.h"
12 #include "networkd-dhcp-prefix-delegation.h"
13 #include "networkd-link.h"
14 #include "networkd-manager.h"
15 #include "networkd-network.h"
16 #include "networkd-queue.h"
17 #include "networkd-radv.h"
18 #include "networkd-route-util.h"
19 #include "parse-util.h"
20 #include "radv-internal.h"
21 #include "string-util.h"
22 #include "string-table.h"
25 void network_adjust_radv(Network
*network
) {
28 /* After this function is called, network->router_prefix_delegation can be treated as a boolean. */
30 if (network
->dhcp_pd
< 0)
31 /* For backward compatibility. */
32 network
->dhcp_pd
= FLAGS_SET(network
->router_prefix_delegation
, RADV_PREFIX_DELEGATION_DHCP6
);
34 if (!FLAGS_SET(network
->link_local
, ADDRESS_FAMILY_IPV6
)) {
35 if (network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
)
36 log_warning("%s: IPv6PrefixDelegation= is enabled but IPv6 link-local addressing is disabled. "
37 "Disabling IPv6PrefixDelegation=.", network
->filename
);
39 network
->router_prefix_delegation
= RADV_PREFIX_DELEGATION_NONE
;
42 if (network
->router_prefix_delegation
== RADV_PREFIX_DELEGATION_NONE
) {
43 network
->n_router_dns
= 0;
44 network
->router_dns
= mfree(network
->router_dns
);
45 network
->router_search_domains
= ordered_set_free(network
->router_search_domains
);
48 if (!FLAGS_SET(network
->router_prefix_delegation
, RADV_PREFIX_DELEGATION_STATIC
)) {
49 network
->prefixes_by_section
= hashmap_free_with_destructor(network
->prefixes_by_section
, prefix_free
);
50 network
->route_prefixes_by_section
= hashmap_free_with_destructor(network
->route_prefixes_by_section
, route_prefix_free
);
51 network
->pref64_prefixes_by_section
= hashmap_free_with_destructor(network
->pref64_prefixes_by_section
, pref64_prefix_free
);
55 bool link_radv_enabled(Link
*link
) {
58 if (!link_may_have_ipv6ll(link
, /* check_multicast = */ true))
61 if (link
->hw_addr
.length
!= ETH_ALEN
)
64 return link
->network
->router_prefix_delegation
;
67 Prefix
*prefix_free(Prefix
*prefix
) {
71 if (prefix
->network
) {
72 assert(prefix
->section
);
73 hashmap_remove(prefix
->network
->prefixes_by_section
, prefix
->section
);
76 config_section_free(prefix
->section
);
77 set_free(prefix
->tokens
);
82 DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix
, prefix_free
);
84 static int prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, Prefix
**ret
) {
85 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
86 _cleanup_(prefix_freep
) Prefix
*prefix
= NULL
;
92 assert(section_line
> 0);
94 r
= config_section_new(filename
, section_line
, &n
);
98 prefix
= hashmap_get(network
->prefixes_by_section
, n
);
100 *ret
= TAKE_PTR(prefix
);
104 prefix
= new(Prefix
, 1);
110 .section
= TAKE_PTR(n
),
112 .preferred_lifetime
= RADV_DEFAULT_PREFERRED_LIFETIME_USEC
,
113 .valid_lifetime
= RADV_DEFAULT_VALID_LIFETIME_USEC
,
115 .address_auto_configuration
= true,
118 r
= hashmap_ensure_put(&network
->prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
122 *ret
= TAKE_PTR(prefix
);
126 RoutePrefix
*route_prefix_free(RoutePrefix
*prefix
) {
130 if (prefix
->network
) {
131 assert(prefix
->section
);
132 hashmap_remove(prefix
->network
->route_prefixes_by_section
, prefix
->section
);
135 config_section_free(prefix
->section
);
137 return mfree(prefix
);
140 DEFINE_SECTION_CLEANUP_FUNCTIONS(RoutePrefix
, route_prefix_free
);
142 static int route_prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutePrefix
**ret
) {
143 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
144 _cleanup_(route_prefix_freep
) RoutePrefix
*prefix
= NULL
;
150 assert(section_line
> 0);
152 r
= config_section_new(filename
, section_line
, &n
);
156 prefix
= hashmap_get(network
->route_prefixes_by_section
, n
);
158 *ret
= TAKE_PTR(prefix
);
162 prefix
= new(RoutePrefix
, 1);
166 *prefix
= (RoutePrefix
) {
168 .section
= TAKE_PTR(n
),
170 .lifetime
= RADV_DEFAULT_VALID_LIFETIME_USEC
,
173 r
= hashmap_ensure_put(&network
->route_prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
177 *ret
= TAKE_PTR(prefix
);
181 pref64Prefix
*pref64_prefix_free(pref64Prefix
*prefix
) {
185 if (prefix
->network
) {
186 assert(prefix
->section
);
187 hashmap_remove(prefix
->network
->pref64_prefixes_by_section
, prefix
->section
);
190 config_section_free(prefix
->section
);
192 return mfree(prefix
);
195 DEFINE_SECTION_CLEANUP_FUNCTIONS(pref64Prefix
, pref64_prefix_free
);
197 static int pref64_prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, pref64Prefix
**ret
) {
198 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
199 _cleanup_(pref64_prefix_freep
) pref64Prefix
*prefix
= NULL
;
205 assert(section_line
> 0);
207 r
= config_section_new(filename
, section_line
, &n
);
211 prefix
= hashmap_get(network
->pref64_prefixes_by_section
, n
);
213 *ret
= TAKE_PTR(prefix
);
217 prefix
= new(pref64Prefix
, 1);
221 *prefix
= (pref64Prefix
) {
223 .section
= TAKE_PTR(n
),
225 .lifetime
= RADV_DEFAULT_PRE64_LIFETIME_USEC
,
228 r
= hashmap_ensure_put(&network
->pref64_prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
232 *ret
= TAKE_PTR(prefix
);
236 int link_request_radv_addresses(Link
*link
) {
242 if (!link_radv_enabled(link
))
245 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
246 _cleanup_set_free_ Set
*addresses
= NULL
;
252 /* radv_generate_addresses() below requires the prefix length <= 64. */
253 if (p
->prefixlen
> 64)
256 r
= radv_generate_addresses(link
, p
->tokens
, &p
->prefix
, p
->prefixlen
, &addresses
);
260 SET_FOREACH(a
, addresses
) {
261 _cleanup_(address_freep
) Address
*address
= NULL
;
263 r
= address_new(&address
);
267 address
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
268 address
->family
= AF_INET6
;
269 address
->in_addr
.in6
= *a
;
270 address
->prefixlen
= p
->prefixlen
;
271 address
->route_metric
= p
->route_metric
;
273 r
= link_request_static_address(link
, address
);
282 static uint32_t usec_to_lifetime(usec_t usec
) {
285 if (usec
== USEC_INFINITY
)
288 t
= DIV_ROUND_UP(usec
, USEC_PER_SEC
);
295 static int radv_set_prefix(Link
*link
, Prefix
*prefix
) {
296 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
303 r
= sd_radv_prefix_new(&p
);
307 r
= sd_radv_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
311 r
= sd_radv_prefix_set_preferred_lifetime(p
, prefix
->preferred_lifetime
, USEC_INFINITY
);
315 r
= sd_radv_prefix_set_valid_lifetime(p
, prefix
->valid_lifetime
, USEC_INFINITY
);
319 r
= sd_radv_prefix_set_onlink(p
, prefix
->onlink
);
323 r
= sd_radv_prefix_set_address_autoconfiguration(p
, prefix
->address_auto_configuration
);
327 return sd_radv_add_prefix(link
->radv
, p
);
330 static int radv_set_route_prefix(Link
*link
, RoutePrefix
*prefix
) {
331 _cleanup_(sd_radv_route_prefix_unrefp
) sd_radv_route_prefix
*p
= NULL
;
338 r
= sd_radv_route_prefix_new(&p
);
342 r
= sd_radv_route_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
346 r
= sd_radv_route_prefix_set_lifetime(p
, prefix
->lifetime
, USEC_INFINITY
);
350 return sd_radv_add_route_prefix(link
->radv
, p
);
353 static int radv_set_pref64_prefix(Link
*link
, pref64Prefix
*prefix
) {
354 _cleanup_(sd_radv_pref64_prefix_unrefp
) sd_radv_pref64_prefix
*p
= NULL
;
361 r
= sd_radv_pref64_prefix_new(&p
);
365 r
= sd_radv_pref64_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
, prefix
->lifetime
);
369 return sd_radv_add_pref64_prefix(link
->radv
, p
);
372 static int network_get_ipv6_dns(Network
*network
, struct in6_addr
**ret_addresses
, size_t *ret_size
) {
373 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
374 size_t n_addresses
= 0;
377 assert(ret_addresses
);
380 for (size_t i
= 0; i
< network
->n_dns
; i
++) {
381 union in_addr_union
*addr
;
383 if (network
->dns
[i
]->family
!= AF_INET6
)
386 addr
= &network
->dns
[i
]->address
;
388 if (in_addr_is_null(AF_INET6
, addr
) ||
389 in_addr_is_link_local(AF_INET6
, addr
) ||
390 in_addr_is_localhost(AF_INET6
, addr
))
393 if (!GREEDY_REALLOC(addresses
, n_addresses
+ 1))
396 addresses
[n_addresses
++] = addr
->in6
;
399 *ret_addresses
= TAKE_PTR(addresses
);
400 *ret_size
= n_addresses
;
405 static int radv_set_dns(Link
*link
, Link
*uplink
) {
406 _cleanup_free_
struct in6_addr
*dns
= NULL
;
410 if (!link
->network
->router_emit_dns
)
413 if (link
->network
->router_dns
) {
416 dns
= new(struct in6_addr
, link
->network
->n_router_dns
);
421 for (size_t i
= 0; i
< link
->network
->n_router_dns
; i
++)
422 if (in6_addr_is_null(&link
->network
->router_dns
[i
])) {
423 if (in6_addr_is_set(&link
->ipv6ll_address
))
424 *(p
++) = link
->ipv6ll_address
;
426 *(p
++) = link
->network
->router_dns
[i
];
433 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
438 assert(uplink
->network
);
440 r
= network_get_ipv6_dns(uplink
->network
, &dns
, &n_dns
);
448 return sd_radv_set_rdnss(link
->radv
,
449 usec_to_lifetime(link
->network
->router_dns_lifetime_usec
),
453 static int radv_set_domains(Link
*link
, Link
*uplink
) {
454 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
455 OrderedSet
*search_domains
;
457 if (!link
->network
->router_emit_domains
)
460 search_domains
= link
->network
->router_search_domains
;
465 search_domains
= link
->network
->search_domains
;
470 assert(uplink
->network
);
472 search_domains
= uplink
->network
->search_domains
;
480 s
= ordered_set_get_strv(search_domains
);
484 return sd_radv_set_dnssl(link
->radv
,
485 usec_to_lifetime(link
->network
->router_dns_lifetime_usec
),
490 static int radv_find_uplink(Link
*link
, Link
**ret
) {
495 if (link
->network
->router_uplink_name
)
496 return link_get_by_name(link
->manager
, link
->network
->router_uplink_name
, ret
);
498 if (link
->network
->router_uplink_index
> 0)
499 return link_get_by_index(link
->manager
, link
->network
->router_uplink_index
, ret
);
501 if (link
->network
->router_uplink_index
== UPLINK_INDEX_AUTO
) {
502 if (link_dhcp_pd_is_enabled(link
))
503 r
= dhcp_pd_find_uplink(link
, ret
); /* When DHCP-PD is enabled, use its uplink. */
505 r
= manager_find_uplink(link
->manager
, AF_INET6
, link
, ret
);
507 /* It is not necessary to propagate error in automatic selection. */
516 static int radv_configure(Link
*link
) {
524 assert(link
->network
);
529 r
= sd_radv_new(&link
->radv
);
533 r
= sd_radv_attach_event(link
->radv
, link
->manager
->event
, 0);
537 if (link
->hw_addr
.length
== ETH_ALEN
) {
538 r
= sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
543 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
547 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
551 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
555 r
= sd_radv_set_router_lifetime(link
->radv
, link
->network
->router_lifetime_usec
);
559 r
= sd_radv_set_hop_limit(link
->radv
, link
->network
->router_hop_limit
);
563 if (link
->network
->router_lifetime_usec
> 0) {
564 r
= sd_radv_set_preference(link
->radv
, link
->network
->router_preference
);
569 if (link
->network
->router_retransmit_usec
> 0) {
570 r
= sd_radv_set_retransmit(link
->radv
, DIV_ROUND_UP(link
->network
->router_retransmit_usec
, USEC_PER_MSEC
));
575 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
576 r
= radv_set_prefix(link
, p
);
577 if (r
< 0 && r
!= -EEXIST
)
581 HASHMAP_FOREACH(q
, link
->network
->route_prefixes_by_section
) {
582 r
= radv_set_route_prefix(link
, q
);
583 if (r
< 0 && r
!= -EEXIST
)
587 HASHMAP_FOREACH(n
, link
->network
->pref64_prefixes_by_section
) {
588 r
= radv_set_pref64_prefix(link
, n
);
589 if (r
< 0 && r
!= -EEXIST
)
593 (void) radv_find_uplink(link
, &uplink
);
595 r
= radv_set_dns(link
, uplink
);
597 return log_link_debug_errno(link
, r
, "Could not set RA DNS: %m");
599 r
= radv_set_domains(link
, uplink
);
601 return log_link_debug_errno(link
, r
, "Could not set RA Domains: %m");
606 int radv_update_mac(Link
*link
) {
615 if (link
->hw_addr
.length
!= ETH_ALEN
)
618 restart
= sd_radv_is_running(link
->radv
);
620 r
= sd_radv_stop(link
->radv
);
624 r
= sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
629 r
= sd_radv_start(link
->radv
);
637 static int radv_is_ready_to_configure(Link
*link
) {
638 bool needs_uplink
= false;
642 assert(link
->network
);
644 if (!link_is_ready_to_configure(link
, /* allow_unmanaged = */ false))
647 if (in6_addr_is_null(&link
->ipv6ll_address
))
650 if (link
->hw_addr
.length
!= ETH_ALEN
|| hw_addr_is_null(&link
->hw_addr
))
653 if (link
->network
->router_emit_dns
&& !link
->network
->router_dns
) {
654 _cleanup_free_
struct in6_addr
*dns
= NULL
;
657 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
661 needs_uplink
= r
== 0;
664 if (link
->network
->router_emit_domains
&&
665 !link
->network
->router_search_domains
&&
666 !link
->network
->search_domains
)
672 if (radv_find_uplink(link
, &uplink
) < 0)
675 if (uplink
&& !uplink
->network
)
682 static int radv_process_request(Request
*req
, Link
*link
, void *userdata
) {
687 r
= radv_is_ready_to_configure(link
);
691 r
= radv_configure(link
);
693 return log_link_warning_errno(link
, r
, "Failed to configure IPv6 Router Advertisement engine: %m");
695 if (link_has_carrier(link
)) {
696 r
= radv_start(link
);
698 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Advertisement engine: %m");
701 log_link_debug(link
, "IPv6 Router Advertisement engine is configured%s.",
702 link_has_carrier(link
) ? " and started" : "");
706 int link_request_radv(Link
*link
) {
711 if (!link_radv_enabled(link
))
717 r
= link_queue_request(link
, REQUEST_TYPE_RADV
, radv_process_request
, NULL
);
719 return log_link_warning_errno(link
, r
, "Failed to request configuring of the IPv6 Router Advertisement engine: %m");
721 log_link_debug(link
, "Requested configuring of the IPv6 Router Advertisement engine.");
725 int radv_start(Link
*link
) {
729 assert(link
->network
);
734 if (!link_has_carrier(link
))
737 if (in6_addr_is_null(&link
->ipv6ll_address
))
740 if (sd_radv_is_running(link
->radv
))
743 if (link
->network
->dhcp_pd_announce
) {
744 r
= dhcp_request_prefix_delegation(link
);
746 return log_link_debug_errno(link
, r
, "Failed to request DHCP delegated subnet prefix: %m");
749 log_link_debug(link
, "Starting IPv6 Router Advertisements");
750 return sd_radv_start(link
->radv
);
755 const struct in6_addr
*prefix
,
757 usec_t lifetime_preferred_usec
,
758 usec_t lifetime_valid_usec
) {
760 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
768 r
= sd_radv_prefix_new(&p
);
772 r
= sd_radv_prefix_set_prefix(p
, prefix
, prefix_len
);
776 r
= sd_radv_prefix_set_preferred_lifetime(p
, RADV_DEFAULT_PREFERRED_LIFETIME_USEC
, lifetime_preferred_usec
);
780 r
= sd_radv_prefix_set_valid_lifetime(p
, RADV_DEFAULT_VALID_LIFETIME_USEC
, lifetime_valid_usec
);
784 r
= sd_radv_add_prefix(link
->radv
, p
);
785 if (r
< 0 && r
!= -EEXIST
)
791 static int prefix_section_verify(Prefix
*p
) {
794 if (section_is_invalid(p
->section
))
797 if (in6_addr_is_null(&p
->prefix
))
798 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
799 "%s: [IPv6Prefix] section without Prefix= field configured, "
800 "or specified prefix is the null address. "
801 "Ignoring [IPv6Prefix] section from line %u.",
802 p
->section
->filename
, p
->section
->line
);
804 if (p
->prefixlen
< 3 || p
->prefixlen
> 128)
805 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
806 "%s: Invalid prefix length %u is specified in [IPv6Prefix] section. "
807 "Valid range is 3…128. Ignoring [IPv6Prefix] section from line %u.",
808 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
810 if (p
->prefixlen
> 64) {
811 log_info("%s:%u: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %s%s.",
812 p
->section
->filename
, p
->section
->line
,
814 p
->assign
? ", refusing to assign an address in " : "",
815 p
->assign
? IN6_ADDR_PREFIX_TO_STRING(&p
->prefix
, p
->prefixlen
) : "");
820 if (p
->valid_lifetime
== 0)
821 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
822 "%s: The valid lifetime of prefix cannot be zero. "
823 "Ignoring [IPv6Prefix] section from line %u.",
824 p
->section
->filename
, p
->section
->line
);
826 if (p
->preferred_lifetime
> p
->valid_lifetime
)
827 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
828 "%s: The preferred lifetime %s is longer than the valid lifetime %s. "
829 "Ignoring [IPv6Prefix] section from line %u.",
830 p
->section
->filename
,
831 FORMAT_TIMESPAN(p
->preferred_lifetime
, USEC_PER_SEC
),
832 FORMAT_TIMESPAN(p
->valid_lifetime
, USEC_PER_SEC
),
838 void network_drop_invalid_prefixes(Network
*network
) {
843 HASHMAP_FOREACH(p
, network
->prefixes_by_section
)
844 if (prefix_section_verify(p
) < 0)
848 static int route_prefix_section_verify(RoutePrefix
*p
) {
849 if (section_is_invalid(p
->section
))
852 if (p
->prefixlen
> 128)
853 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
854 "%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. "
855 "Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.",
856 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
858 if (p
->lifetime
== 0)
859 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
860 "%s: The lifetime of route cannot be zero. "
861 "Ignoring [IPv6RoutePrefix] section from line %u.",
862 p
->section
->filename
, p
->section
->line
);
867 void network_drop_invalid_route_prefixes(Network
*network
) {
872 HASHMAP_FOREACH(p
, network
->route_prefixes_by_section
)
873 if (route_prefix_section_verify(p
) < 0)
874 route_prefix_free(p
);
877 void network_drop_invalid_pref64_prefixes(Network
*network
) {
882 HASHMAP_FOREACH(p
, network
->pref64_prefixes_by_section
)
883 if (section_is_invalid(p
->section
))
884 pref64_prefix_free(p
);
887 int config_parse_prefix(
889 const char *filename
,
892 unsigned section_line
,
899 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
900 Network
*network
= ASSERT_PTR(userdata
);
901 union in_addr_union a
;
909 r
= prefix_new_static(network
, filename
, section_line
, &p
);
913 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
915 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
916 "Prefix is invalid, ignoring assignment: %s", rvalue
);
920 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
927 int config_parse_prefix_boolean(
929 const char *filename
,
932 unsigned section_line
,
939 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
940 Network
*network
= ASSERT_PTR(userdata
);
948 r
= prefix_new_static(network
, filename
, section_line
, &p
);
952 r
= parse_boolean(rvalue
);
954 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
955 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
959 if (streq(lvalue
, "OnLink"))
961 else if (streq(lvalue
, "AddressAutoconfiguration"))
962 p
->address_auto_configuration
= r
;
963 else if (streq(lvalue
, "Assign"))
966 assert_not_reached();
972 int config_parse_prefix_lifetime(
974 const char *filename
,
977 unsigned section_line
,
984 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
985 Network
*network
= ASSERT_PTR(userdata
);
994 r
= prefix_new_static(network
, filename
, section_line
, &p
);
998 r
= parse_sec(rvalue
, &usec
);
1000 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1001 "Lifetime is invalid, ignoring assignment: %s", rvalue
);
1005 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1006 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1007 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1011 if (streq(lvalue
, "PreferredLifetimeSec"))
1012 p
->preferred_lifetime
= usec
;
1013 else if (streq(lvalue
, "ValidLifetimeSec"))
1014 p
->valid_lifetime
= usec
;
1016 assert_not_reached();
1022 int config_parse_prefix_metric(
1024 const char *filename
,
1026 const char *section
,
1027 unsigned section_line
,
1034 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1035 Network
*network
= ASSERT_PTR(userdata
);
1043 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1047 r
= safe_atou32(rvalue
, &p
->route_metric
);
1049 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1050 "Failed to parse %s=, ignoring assignment: %s",
1059 int config_parse_prefix_token(
1061 const char *filename
,
1063 const char *section
,
1064 unsigned section_line
,
1071 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1072 Network
*network
= ASSERT_PTR(userdata
);
1080 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1084 r
= config_parse_address_generation_type(unit
, filename
, line
, section
, section_line
,
1085 lvalue
, ltype
, rvalue
, &p
->tokens
, userdata
);
1093 int config_parse_route_prefix(
1095 const char *filename
,
1097 const char *section
,
1098 unsigned section_line
,
1105 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1106 Network
*network
= ASSERT_PTR(userdata
);
1107 union in_addr_union a
;
1115 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1119 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
1121 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1122 "Route prefix is invalid, ignoring assignment: %s", rvalue
);
1126 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
1133 int config_parse_route_prefix_lifetime(
1135 const char *filename
,
1137 const char *section
,
1138 unsigned section_line
,
1145 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1146 Network
*network
= ASSERT_PTR(userdata
);
1155 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1159 r
= parse_sec(rvalue
, &usec
);
1161 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1162 "Route lifetime is invalid, ignoring assignment: %s", rvalue
);
1166 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1167 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1168 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1178 int config_parse_pref64_prefix(
1180 const char *filename
,
1182 const char *section
,
1183 unsigned section_line
,
1190 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1191 Network
*network
= ASSERT_PTR(userdata
);
1192 union in_addr_union a
;
1201 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1205 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &prefixlen
);
1207 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1208 "PREF64 prefix is invalid, ignoring assignment: %s", rvalue
);
1212 if (!IN_SET(prefixlen
, 96, 64, 56, 48, 40, 32)) {
1213 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1214 "PREF64 prefixlen is invalid, ignoring assignment: %s", rvalue
);
1218 (void) in6_addr_mask(&a
.in6
,prefixlen
);
1220 p
->prefixlen
= prefixlen
;
1226 int config_parse_pref64_prefix_lifetime(
1228 const char *filename
,
1230 const char *section
,
1231 unsigned section_line
,
1238 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1239 Network
*network
= ASSERT_PTR(userdata
);
1248 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1252 r
= parse_sec(rvalue
, &usec
);
1254 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1255 "PREF64 lifetime is invalid, ignoring assignment: %s", rvalue
);
1259 if (usec
== USEC_INFINITY
|| DIV_ROUND_UP(usec
, 8 * USEC_PER_SEC
) >= UINT64_C(1) << 13) {
1260 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1261 "PREF64 lifetime is too long, ignoring assignment: %s", rvalue
);
1271 int config_parse_radv_dns(
1273 const char *filename
,
1275 const char *section
,
1276 unsigned section_line
,
1290 if (isempty(rvalue
)) {
1291 n
->n_router_dns
= 0;
1292 n
->router_dns
= mfree(n
->router_dns
);
1296 for (const char *p
= rvalue
;;) {
1297 _cleanup_free_
char *w
= NULL
;
1298 union in_addr_union a
;
1300 r
= extract_first_word(&p
, &w
, NULL
, 0);
1304 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1305 "Failed to extract word, ignoring: %s", rvalue
);
1311 if (streq(w
, "_link_local"))
1314 r
= in_addr_from_string(AF_INET6
, w
, &a
);
1316 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1317 "Failed to parse DNS server address, ignoring: %s", w
);
1321 if (in_addr_is_null(AF_INET6
, &a
)) {
1322 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1323 "DNS server address is null, ignoring: %s", w
);
1329 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
1333 m
[n
->n_router_dns
++] = a
.in6
;
1338 int config_parse_radv_search_domains(
1340 const char *filename
,
1342 const char *section
,
1343 unsigned section_line
,
1357 if (isempty(rvalue
)) {
1358 n
->router_search_domains
= ordered_set_free(n
->router_search_domains
);
1362 for (const char *p
= rvalue
;;) {
1363 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
1365 r
= extract_first_word(&p
, &w
, NULL
, 0);
1369 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1370 "Failed to extract word, ignoring: %s", rvalue
);
1376 r
= dns_name_apply_idna(w
, &idna
);
1378 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1379 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
1382 /* transfer ownership to simplify subsequent operations */
1385 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops_free
);
1389 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
1395 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
1396 [RADV_PREFIX_DELEGATION_NONE
] = "no",
1397 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
1398 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
1399 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
1402 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
1403 radv_prefix_delegation
,
1404 RADVPrefixDelegation
,
1405 RADV_PREFIX_DELEGATION_BOTH
);
1407 int config_parse_router_prefix_delegation(
1409 const char *filename
,
1411 const char *section
,
1412 unsigned section_line
,
1419 RADVPrefixDelegation val
, *ra
= ASSERT_PTR(data
);
1426 if (streq(lvalue
, "IPv6SendRA")) {
1427 r
= parse_boolean(rvalue
);
1429 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1430 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1434 /* When IPv6SendRA= is enabled, only static prefixes are sent by default, and users
1435 * need to explicitly enable DHCPv6PrefixDelegation=. */
1436 *ra
= r
? RADV_PREFIX_DELEGATION_STATIC
: RADV_PREFIX_DELEGATION_NONE
;
1440 /* For backward compatibility */
1441 val
= radv_prefix_delegation_from_string(rvalue
);
1443 log_syntax(unit
, LOG_WARNING
, filename
, line
, val
,
1444 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1452 int config_parse_router_lifetime(
1454 const char *filename
,
1456 const char *section
,
1457 unsigned section_line
,
1464 usec_t usec
, *lifetime
= ASSERT_PTR(data
);
1472 if (isempty(rvalue
)) {
1473 *lifetime
= RADV_DEFAULT_ROUTER_LIFETIME_USEC
;
1477 r
= parse_sec(rvalue
, &usec
);
1479 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1480 "Failed to parse router lifetime, ignoring assignment: %s", rvalue
);
1484 if (usec
< RADV_MIN_ROUTER_LIFETIME_USEC
) {
1485 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1486 "Router lifetime %s is too short, using %s.",
1487 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1488 FORMAT_TIMESPAN(RADV_MIN_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1489 usec
= RADV_MIN_ROUTER_LIFETIME_USEC
;
1490 } else if (usec
> RADV_MAX_ROUTER_LIFETIME_USEC
) {
1491 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1492 "Router lifetime %s is too large, using %s.",
1493 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1494 FORMAT_TIMESPAN(RADV_MAX_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1495 usec
= RADV_MAX_ROUTER_LIFETIME_USEC
;
1503 int config_parse_router_retransmit(
1505 const char *filename
,
1507 const char *section
,
1508 unsigned section_line
,
1515 usec_t usec
, *router_retransmit_usec
= ASSERT_PTR(data
);
1523 if (isempty(rvalue
)) {
1524 *router_retransmit_usec
= 0;
1528 r
= parse_sec(rvalue
, &usec
);
1530 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1531 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1535 if (usec
!= USEC_INFINITY
&&
1536 DIV_ROUND_UP(usec
, USEC_PER_MSEC
) > UINT32_MAX
) {
1537 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1538 "Invalid %s= must be in the range 0...%"PRIu32
"Sec, ignoring: %s", lvalue
, UINT32_MAX
, rvalue
);
1542 *router_retransmit_usec
= usec
;
1546 int config_parse_router_preference(
1548 const char *filename
,
1550 const char *section
,
1551 unsigned section_line
,
1558 Network
*network
= userdata
;
1566 if (streq(rvalue
, "high"))
1567 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
1568 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
1569 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
1570 else if (streq(rvalue
, "low"))
1571 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
1573 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1574 "Invalid router preference, ignoring assignment: %s", rvalue
);