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 "ndisc-router-internal.h"
11 #include "networkd-address-generation.h"
12 #include "networkd-address.h"
13 #include "networkd-dhcp-prefix-delegation.h"
14 #include "networkd-link.h"
15 #include "networkd-manager.h"
16 #include "networkd-network.h"
17 #include "networkd-queue.h"
18 #include "networkd-radv.h"
19 #include "networkd-route-util.h"
20 #include "parse-util.h"
21 #include "radv-internal.h"
22 #include "string-util.h"
23 #include "string-table.h"
26 bool link_radv_enabled(Link
*link
) {
29 if (!link_may_have_ipv6ll(link
, /* check_multicast = */ true))
32 if (link
->hw_addr
.length
!= ETH_ALEN
)
35 return link
->network
->router_prefix_delegation
;
38 Prefix
*prefix_free(Prefix
*prefix
) {
42 if (prefix
->network
) {
43 assert(prefix
->section
);
44 hashmap_remove(prefix
->network
->prefixes_by_section
, prefix
->section
);
47 config_section_free(prefix
->section
);
48 set_free(prefix
->tokens
);
53 DEFINE_SECTION_CLEANUP_FUNCTIONS(Prefix
, prefix_free
);
55 static int prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, Prefix
**ret
) {
56 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
57 _cleanup_(prefix_freep
) Prefix
*prefix
= NULL
;
63 assert(section_line
> 0);
65 r
= config_section_new(filename
, section_line
, &n
);
69 prefix
= hashmap_get(network
->prefixes_by_section
, n
);
71 *ret
= TAKE_PTR(prefix
);
75 prefix
= new(Prefix
, 1);
81 .section
= TAKE_PTR(n
),
83 .preferred_lifetime
= RADV_DEFAULT_PREFERRED_LIFETIME_USEC
,
84 .valid_lifetime
= RADV_DEFAULT_VALID_LIFETIME_USEC
,
86 .address_auto_configuration
= true,
89 r
= hashmap_ensure_put(&network
->prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
93 *ret
= TAKE_PTR(prefix
);
97 RoutePrefix
*route_prefix_free(RoutePrefix
*prefix
) {
101 if (prefix
->network
) {
102 assert(prefix
->section
);
103 hashmap_remove(prefix
->network
->route_prefixes_by_section
, prefix
->section
);
106 config_section_free(prefix
->section
);
108 return mfree(prefix
);
111 DEFINE_SECTION_CLEANUP_FUNCTIONS(RoutePrefix
, route_prefix_free
);
113 static int route_prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutePrefix
**ret
) {
114 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
115 _cleanup_(route_prefix_freep
) RoutePrefix
*prefix
= NULL
;
121 assert(section_line
> 0);
123 r
= config_section_new(filename
, section_line
, &n
);
127 prefix
= hashmap_get(network
->route_prefixes_by_section
, n
);
129 *ret
= TAKE_PTR(prefix
);
133 prefix
= new(RoutePrefix
, 1);
137 *prefix
= (RoutePrefix
) {
139 .section
= TAKE_PTR(n
),
141 .lifetime
= RADV_DEFAULT_VALID_LIFETIME_USEC
,
144 r
= hashmap_ensure_put(&network
->route_prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
148 *ret
= TAKE_PTR(prefix
);
152 pref64Prefix
*pref64_prefix_free(pref64Prefix
*prefix
) {
156 if (prefix
->network
) {
157 assert(prefix
->section
);
158 hashmap_remove(prefix
->network
->pref64_prefixes_by_section
, prefix
->section
);
161 config_section_free(prefix
->section
);
163 return mfree(prefix
);
166 DEFINE_SECTION_CLEANUP_FUNCTIONS(pref64Prefix
, pref64_prefix_free
);
168 static int pref64_prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, pref64Prefix
**ret
) {
169 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
170 _cleanup_(pref64_prefix_freep
) pref64Prefix
*prefix
= NULL
;
176 assert(section_line
> 0);
178 r
= config_section_new(filename
, section_line
, &n
);
182 prefix
= hashmap_get(network
->pref64_prefixes_by_section
, n
);
184 *ret
= TAKE_PTR(prefix
);
188 prefix
= new(pref64Prefix
, 1);
192 *prefix
= (pref64Prefix
) {
194 .section
= TAKE_PTR(n
),
196 .lifetime
= RADV_PREF64_DEFAULT_LIFETIME_USEC
,
199 r
= hashmap_ensure_put(&network
->pref64_prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
203 *ret
= TAKE_PTR(prefix
);
207 int link_request_radv_addresses(Link
*link
) {
213 if (!link_radv_enabled(link
))
216 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
220 /* radv_generate_addresses() below requires the prefix length <= 64. */
221 if (p
->prefixlen
> 64)
224 _cleanup_hashmap_free_ Hashmap
*tokens_by_address
= NULL
;
225 r
= radv_generate_addresses(link
, p
->tokens
, &p
->prefix
, p
->prefixlen
, &tokens_by_address
);
231 HASHMAP_FOREACH_KEY(token
, a
, tokens_by_address
) {
232 _cleanup_(address_unrefp
) Address
*address
= NULL
;
234 r
= address_new(&address
);
238 address
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
239 address
->family
= AF_INET6
;
240 address
->in_addr
.in6
= *a
;
241 address
->prefixlen
= p
->prefixlen
;
242 address
->route_metric
= p
->route_metric
;
243 address
->token
= ipv6_token_ref(token
);
245 r
= link_request_static_address(link
, address
);
254 int link_reconfigure_radv_address(Address
*address
, Link
*link
) {
258 assert(address
->source
== NETWORK_CONFIG_SOURCE_STATIC
);
261 r
= regenerate_address(address
, link
);
265 r
= link_request_static_address(link
, address
);
269 if (link
->static_address_messages
!= 0) {
270 link
->static_addresses_configured
= false;
271 link_set_state(link
, LINK_STATE_CONFIGURING
);
277 static int radv_set_prefix(Link
*link
, Prefix
*prefix
) {
278 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
285 r
= sd_radv_prefix_new(&p
);
289 r
= sd_radv_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
293 r
= sd_radv_prefix_set_preferred_lifetime(p
, prefix
->preferred_lifetime
, USEC_INFINITY
);
297 r
= sd_radv_prefix_set_valid_lifetime(p
, prefix
->valid_lifetime
, USEC_INFINITY
);
301 r
= sd_radv_prefix_set_onlink(p
, prefix
->onlink
);
305 r
= sd_radv_prefix_set_address_autoconfiguration(p
, prefix
->address_auto_configuration
);
309 return sd_radv_add_prefix(link
->radv
, p
);
312 static int radv_set_route_prefix(Link
*link
, RoutePrefix
*prefix
) {
313 _cleanup_(sd_radv_route_prefix_unrefp
) sd_radv_route_prefix
*p
= NULL
;
320 r
= sd_radv_route_prefix_new(&p
);
324 r
= sd_radv_route_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
328 r
= sd_radv_route_prefix_set_lifetime(p
, prefix
->lifetime
, USEC_INFINITY
);
332 return sd_radv_add_route_prefix(link
->radv
, p
);
335 static int radv_set_pref64_prefix(Link
*link
, pref64Prefix
*prefix
) {
336 _cleanup_(sd_radv_pref64_prefix_unrefp
) sd_radv_pref64_prefix
*p
= NULL
;
343 r
= sd_radv_pref64_prefix_new(&p
);
347 r
= sd_radv_pref64_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
, prefix
->lifetime
);
351 return sd_radv_add_pref64_prefix(link
->radv
, p
);
354 static int network_get_ipv6_dns(Network
*network
, struct in6_addr
**ret_addresses
, size_t *ret_size
) {
355 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
356 size_t n_addresses
= 0;
359 assert(ret_addresses
);
362 for (size_t i
= 0; i
< network
->n_dns
; i
++) {
363 union in_addr_union
*addr
;
365 if (network
->dns
[i
]->family
!= AF_INET6
)
368 addr
= &network
->dns
[i
]->address
;
370 if (in_addr_is_null(AF_INET6
, addr
) ||
371 in_addr_is_link_local(AF_INET6
, addr
) ||
372 in_addr_is_localhost(AF_INET6
, addr
))
375 if (!GREEDY_REALLOC(addresses
, n_addresses
+ 1))
378 addresses
[n_addresses
++] = addr
->in6
;
381 *ret_addresses
= TAKE_PTR(addresses
);
382 *ret_size
= n_addresses
;
387 static int radv_set_dns(Link
*link
, Link
*uplink
) {
388 _cleanup_free_
struct in6_addr
*dns
= NULL
;
392 if (!link
->network
->router_emit_dns
)
395 if (link
->network
->router_dns
) {
398 dns
= new(struct in6_addr
, link
->network
->n_router_dns
);
403 for (size_t i
= 0; i
< link
->network
->n_router_dns
; i
++)
404 if (in6_addr_is_null(&link
->network
->router_dns
[i
])) {
405 if (in6_addr_is_set(&link
->ipv6ll_address
))
406 *(p
++) = link
->ipv6ll_address
;
408 *(p
++) = link
->network
->router_dns
[i
];
415 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
420 assert(uplink
->network
);
422 r
= network_get_ipv6_dns(uplink
->network
, &dns
, &n_dns
);
430 return sd_radv_set_rdnss(link
->radv
,
431 link
->network
->router_dns_lifetime_usec
,
435 static int radv_set_domains(Link
*link
, Link
*uplink
) {
436 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
437 OrderedSet
*search_domains
;
439 if (!link
->network
->router_emit_domains
)
442 search_domains
= link
->network
->router_search_domains
;
447 search_domains
= link
->network
->search_domains
;
452 assert(uplink
->network
);
454 search_domains
= uplink
->network
->search_domains
;
462 s
= ordered_set_get_strv(search_domains
);
466 return sd_radv_set_dnssl(link
->radv
,
467 link
->network
->router_dns_lifetime_usec
,
472 static int radv_find_uplink(Link
*link
, Link
**ret
) {
477 if (link
->network
->router_uplink_name
)
478 return link_get_by_name(link
->manager
, link
->network
->router_uplink_name
, ret
);
480 if (link
->network
->router_uplink_index
> 0)
481 return link_get_by_index(link
->manager
, link
->network
->router_uplink_index
, ret
);
483 if (link
->network
->router_uplink_index
== UPLINK_INDEX_AUTO
) {
484 if (link_dhcp_pd_is_enabled(link
))
485 r
= dhcp_pd_find_uplink(link
, ret
); /* When DHCP-PD is enabled, use its uplink. */
487 r
= manager_find_uplink(link
->manager
, AF_INET6
, link
, ret
);
489 /* It is not necessary to propagate error in automatic selection. */
498 static int radv_configure(Link
*link
) {
506 assert(link
->network
);
511 r
= sd_radv_new(&link
->radv
);
515 r
= sd_radv_attach_event(link
->radv
, link
->manager
->event
, 0);
519 if (link
->hw_addr
.length
== ETH_ALEN
) {
520 r
= sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
525 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
529 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
533 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
537 r
= sd_radv_set_router_lifetime(link
->radv
, link
->network
->router_lifetime_usec
);
541 r
= sd_radv_set_hop_limit(link
->radv
, link
->network
->router_hop_limit
);
545 r
= sd_radv_set_preference(link
->radv
, link
->network
->router_preference
);
549 r
= sd_radv_set_reachable_time(link
->radv
, link
->network
->router_reachable_usec
);
553 r
= sd_radv_set_retransmit(link
->radv
, link
->network
->router_retransmit_usec
);
557 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
558 r
= radv_set_prefix(link
, p
);
559 if (r
< 0 && r
!= -EEXIST
)
563 HASHMAP_FOREACH(q
, link
->network
->route_prefixes_by_section
) {
564 r
= radv_set_route_prefix(link
, q
);
565 if (r
< 0 && r
!= -EEXIST
)
569 HASHMAP_FOREACH(n
, link
->network
->pref64_prefixes_by_section
) {
570 r
= radv_set_pref64_prefix(link
, n
);
571 if (r
< 0 && r
!= -EEXIST
)
575 (void) radv_find_uplink(link
, &uplink
);
577 r
= radv_set_dns(link
, uplink
);
579 return log_link_debug_errno(link
, r
, "Could not set RA DNS: %m");
581 r
= radv_set_domains(link
, uplink
);
583 return log_link_debug_errno(link
, r
, "Could not set RA Domains: %m");
585 r
= sd_radv_set_home_agent_information(link
->radv
, link
->network
->router_home_agent_information
);
589 r
= sd_radv_set_home_agent_preference(link
->radv
, link
->network
->router_home_agent_preference
);
593 r
= sd_radv_set_home_agent_lifetime(link
->radv
, link
->network
->home_agent_lifetime_usec
);
600 int radv_update_mac(Link
*link
) {
606 if (link
->hw_addr
.length
!= ETH_ALEN
)
609 return sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
612 static int radv_is_ready_to_configure(Link
*link
) {
613 bool needs_uplink
= false;
617 assert(link
->network
);
619 if (!link_is_ready_to_configure(link
, /* allow_unmanaged = */ false))
622 if (in6_addr_is_null(&link
->ipv6ll_address
))
625 if (link
->hw_addr
.length
!= ETH_ALEN
|| hw_addr_is_null(&link
->hw_addr
))
628 if (link
->network
->router_emit_dns
&& !link
->network
->router_dns
) {
629 _cleanup_free_
struct in6_addr
*dns
= NULL
;
632 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
636 needs_uplink
= r
== 0;
639 if (link
->network
->router_emit_domains
&&
640 !link
->network
->router_search_domains
&&
641 !link
->network
->search_domains
)
647 if (radv_find_uplink(link
, &uplink
) < 0)
650 if (uplink
&& !uplink
->network
)
657 static int radv_process_request(Request
*req
, Link
*link
, void *userdata
) {
662 r
= radv_is_ready_to_configure(link
);
666 r
= radv_configure(link
);
668 return log_link_warning_errno(link
, r
, "Failed to configure IPv6 Router Advertisement engine: %m");
670 if (link_has_carrier(link
)) {
671 r
= radv_start(link
);
673 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Advertisement engine: %m");
676 log_link_debug(link
, "IPv6 Router Advertisement engine is configured%s.",
677 link_has_carrier(link
) ? " and started" : "");
681 int link_request_radv(Link
*link
) {
686 if (!link_radv_enabled(link
))
692 r
= link_queue_request(link
, REQUEST_TYPE_RADV
, radv_process_request
, NULL
);
694 return log_link_warning_errno(link
, r
, "Failed to request configuring of the IPv6 Router Advertisement engine: %m");
696 log_link_debug(link
, "Requested configuring of the IPv6 Router Advertisement engine.");
700 int radv_start(Link
*link
) {
704 assert(link
->network
);
709 if (!link_has_carrier(link
))
712 if (in6_addr_is_null(&link
->ipv6ll_address
))
715 if (sd_radv_is_running(link
->radv
))
718 if (link
->network
->dhcp_pd_announce
) {
719 r
= dhcp_request_prefix_delegation(link
);
721 return log_link_debug_errno(link
, r
, "Failed to request DHCP delegated subnet prefix: %m");
724 r
= sd_radv_set_link_local_address(link
->radv
, &link
->ipv6ll_address
);
728 log_link_debug(link
, "Starting IPv6 Router Advertisements");
729 return sd_radv_start(link
->radv
);
734 const struct in6_addr
*prefix
,
736 usec_t lifetime_preferred_usec
,
737 usec_t lifetime_valid_usec
) {
739 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
747 r
= sd_radv_prefix_new(&p
);
751 r
= sd_radv_prefix_set_prefix(p
, prefix
, prefix_len
);
755 r
= sd_radv_prefix_set_preferred_lifetime(p
, RADV_DEFAULT_PREFERRED_LIFETIME_USEC
, lifetime_preferred_usec
);
759 r
= sd_radv_prefix_set_valid_lifetime(p
, RADV_DEFAULT_VALID_LIFETIME_USEC
, lifetime_valid_usec
);
763 r
= sd_radv_add_prefix(link
->radv
, p
);
764 if (r
< 0 && r
!= -EEXIST
)
770 static int prefix_section_verify(Prefix
*p
) {
773 if (section_is_invalid(p
->section
))
776 if (in6_addr_is_null(&p
->prefix
))
777 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
778 "%s: [IPv6Prefix] section without Prefix= field configured, "
779 "or specified prefix is the null address. "
780 "Ignoring [IPv6Prefix] section from line %u.",
781 p
->section
->filename
, p
->section
->line
);
783 if (p
->prefixlen
< 3 || p
->prefixlen
> 128)
784 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
785 "%s: Invalid prefix length %u is specified in [IPv6Prefix] section. "
786 "Valid range is 3…128. Ignoring [IPv6Prefix] section from line %u.",
787 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
789 if (p
->prefixlen
> 64) {
790 log_info("%s:%u: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %s%s.",
791 p
->section
->filename
, p
->section
->line
,
793 p
->assign
? ", refusing to assign an address in " : "",
794 p
->assign
? IN6_ADDR_PREFIX_TO_STRING(&p
->prefix
, p
->prefixlen
) : "");
799 if (p
->preferred_lifetime
> p
->valid_lifetime
)
800 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
801 "%s: The preferred lifetime %s is longer than the valid lifetime %s. "
802 "Ignoring [IPv6Prefix] section from line %u.",
803 p
->section
->filename
,
804 FORMAT_TIMESPAN(p
->preferred_lifetime
, USEC_PER_SEC
),
805 FORMAT_TIMESPAN(p
->valid_lifetime
, USEC_PER_SEC
),
811 static int route_prefix_section_verify(RoutePrefix
*p
) {
812 if (section_is_invalid(p
->section
))
815 if (p
->prefixlen
> 128)
816 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
817 "%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. "
818 "Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.",
819 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
824 void network_adjust_radv(Network
*network
) {
827 /* After this function is called, network->router_prefix_delegation can be treated as a boolean. */
829 if (network
->dhcp_pd
< 0)
830 /* For backward compatibility. */
831 network
->dhcp_pd
= FLAGS_SET(network
->router_prefix_delegation
, RADV_PREFIX_DELEGATION_DHCP6
);
833 if (!FLAGS_SET(network
->link_local
, ADDRESS_FAMILY_IPV6
)) {
834 if (network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
)
835 log_warning("%s: IPv6PrefixDelegation= is enabled but IPv6 link-local addressing is disabled. "
836 "Disabling IPv6PrefixDelegation=.", network
->filename
);
838 network
->router_prefix_delegation
= RADV_PREFIX_DELEGATION_NONE
;
841 if (network
->router_prefix_delegation
== RADV_PREFIX_DELEGATION_NONE
) {
842 network
->n_router_dns
= 0;
843 network
->router_dns
= mfree(network
->router_dns
);
844 network
->router_search_domains
= ordered_set_free(network
->router_search_domains
);
847 if (!FLAGS_SET(network
->router_prefix_delegation
, RADV_PREFIX_DELEGATION_STATIC
)) {
848 network
->prefixes_by_section
= hashmap_free_with_destructor(network
->prefixes_by_section
, prefix_free
);
849 network
->route_prefixes_by_section
= hashmap_free_with_destructor(network
->route_prefixes_by_section
, route_prefix_free
);
850 network
->pref64_prefixes_by_section
= hashmap_free_with_destructor(network
->pref64_prefixes_by_section
, pref64_prefix_free
);
853 if (!network
->router_prefix_delegation
)
856 /* Below, let's verify router settings, if enabled. */
858 if (network
->router_lifetime_usec
== 0 && network
->router_preference
!= SD_NDISC_PREFERENCE_MEDIUM
)
859 /* RFC 4191, Section 2.2,
860 * If the Router Lifetime is zero, the preference value MUST be set to (00) by the sender.
862 * Note, radv_send_router() gracefully handle that. So, it is not necessary to refuse, but
863 * let's warn about that. */
864 log_notice("%s: RouterPreference=%s specified with RouterLifetimeSec=0, ignoring RouterPreference= setting.",
865 network
->filename
, ndisc_router_preference_to_string(network
->router_preference
));
868 HASHMAP_FOREACH(prefix
, network
->prefixes_by_section
)
869 if (prefix_section_verify(prefix
) < 0)
874 HASHMAP_FOREACH(route
, network
->route_prefixes_by_section
)
875 if (route_prefix_section_verify(route
) < 0)
876 route_prefix_free(route
);
878 pref64Prefix
*pref64
;
879 HASHMAP_FOREACH(pref64
, network
->pref64_prefixes_by_section
)
880 if (section_is_invalid(pref64
->section
))
881 pref64_prefix_free(pref64
);
884 int config_parse_prefix(
886 const char *filename
,
889 unsigned section_line
,
896 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
897 Network
*network
= ASSERT_PTR(userdata
);
898 union in_addr_union a
;
906 r
= prefix_new_static(network
, filename
, section_line
, &p
);
910 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
912 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
913 "Prefix is invalid, ignoring assignment: %s", rvalue
);
917 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
924 int config_parse_prefix_boolean(
926 const char *filename
,
929 unsigned section_line
,
936 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
937 Network
*network
= ASSERT_PTR(userdata
);
945 r
= prefix_new_static(network
, filename
, section_line
, &p
);
949 r
= parse_boolean(rvalue
);
951 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
952 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
956 if (streq(lvalue
, "OnLink"))
958 else if (streq(lvalue
, "AddressAutoconfiguration"))
959 p
->address_auto_configuration
= r
;
960 else if (streq(lvalue
, "Assign"))
963 assert_not_reached();
969 int config_parse_prefix_lifetime(
971 const char *filename
,
974 unsigned section_line
,
981 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
982 Network
*network
= ASSERT_PTR(userdata
);
991 r
= prefix_new_static(network
, filename
, section_line
, &p
);
995 r
= parse_sec(rvalue
, &usec
);
997 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
998 "Lifetime is invalid, ignoring assignment: %s", rvalue
);
1002 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1003 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1004 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1008 if (streq(lvalue
, "PreferredLifetimeSec"))
1009 p
->preferred_lifetime
= usec
;
1010 else if (streq(lvalue
, "ValidLifetimeSec"))
1011 p
->valid_lifetime
= usec
;
1013 assert_not_reached();
1019 int config_parse_prefix_metric(
1021 const char *filename
,
1023 const char *section
,
1024 unsigned section_line
,
1031 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1032 Network
*network
= ASSERT_PTR(userdata
);
1040 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1044 r
= safe_atou32(rvalue
, &p
->route_metric
);
1046 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1047 "Failed to parse %s=, ignoring assignment: %s",
1056 int config_parse_prefix_token(
1058 const char *filename
,
1060 const char *section
,
1061 unsigned section_line
,
1068 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1069 Network
*network
= ASSERT_PTR(userdata
);
1077 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1081 r
= config_parse_address_generation_type(unit
, filename
, line
, section
, section_line
,
1082 lvalue
, ltype
, rvalue
, &p
->tokens
, userdata
);
1090 int config_parse_route_prefix(
1092 const char *filename
,
1094 const char *section
,
1095 unsigned section_line
,
1102 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1103 Network
*network
= ASSERT_PTR(userdata
);
1104 union in_addr_union a
;
1112 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1116 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
1118 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1119 "Route prefix is invalid, ignoring assignment: %s", rvalue
);
1123 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
1130 int config_parse_route_prefix_lifetime(
1132 const char *filename
,
1134 const char *section
,
1135 unsigned section_line
,
1142 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1143 Network
*network
= ASSERT_PTR(userdata
);
1152 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1156 r
= parse_sec(rvalue
, &usec
);
1158 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1159 "Route lifetime is invalid, ignoring assignment: %s", rvalue
);
1163 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1164 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1165 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1175 int config_parse_pref64_prefix(
1177 const char *filename
,
1179 const char *section
,
1180 unsigned section_line
,
1187 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1188 Network
*network
= ASSERT_PTR(userdata
);
1189 union in_addr_union a
;
1198 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1202 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &prefixlen
);
1204 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1205 "PREF64 prefix is invalid, ignoring assignment: %s", rvalue
);
1209 if (!IN_SET(prefixlen
, 96, 64, 56, 48, 40, 32)) {
1210 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1211 "PREF64 prefixlen is invalid, ignoring assignment: %s", rvalue
);
1215 (void) in6_addr_mask(&a
.in6
, prefixlen
);
1217 p
->prefixlen
= prefixlen
;
1223 int config_parse_pref64_prefix_lifetime(
1225 const char *filename
,
1227 const char *section
,
1228 unsigned section_line
,
1235 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1236 Network
*network
= ASSERT_PTR(userdata
);
1245 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1249 r
= parse_sec(rvalue
, &usec
);
1251 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1252 "PREF64 lifetime is invalid, ignoring assignment: %s", rvalue
);
1256 if (usec
== USEC_INFINITY
|| DIV_ROUND_UP(usec
, 8 * USEC_PER_SEC
) >= UINT64_C(1) << 13) {
1257 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1258 "PREF64 lifetime is too long, ignoring assignment: %s", rvalue
);
1268 int config_parse_radv_dns(
1270 const char *filename
,
1272 const char *section
,
1273 unsigned section_line
,
1287 if (isempty(rvalue
)) {
1288 n
->n_router_dns
= 0;
1289 n
->router_dns
= mfree(n
->router_dns
);
1293 for (const char *p
= rvalue
;;) {
1294 _cleanup_free_
char *w
= NULL
;
1295 union in_addr_union a
;
1297 r
= extract_first_word(&p
, &w
, NULL
, 0);
1301 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1302 "Failed to extract word, ignoring: %s", rvalue
);
1308 if (streq(w
, "_link_local"))
1311 r
= in_addr_from_string(AF_INET6
, w
, &a
);
1313 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1314 "Failed to parse DNS server address, ignoring: %s", w
);
1318 if (in_addr_is_null(AF_INET6
, &a
)) {
1319 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1320 "DNS server address is null, ignoring: %s", w
);
1326 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
1330 m
[n
->n_router_dns
++] = a
.in6
;
1335 int config_parse_radv_search_domains(
1337 const char *filename
,
1339 const char *section
,
1340 unsigned section_line
,
1354 if (isempty(rvalue
)) {
1355 n
->router_search_domains
= ordered_set_free(n
->router_search_domains
);
1359 for (const char *p
= rvalue
;;) {
1360 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
1362 r
= extract_first_word(&p
, &w
, NULL
, 0);
1366 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1367 "Failed to extract word, ignoring: %s", rvalue
);
1373 r
= dns_name_apply_idna(w
, &idna
);
1375 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1376 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
1379 /* transfer ownership to simplify subsequent operations */
1382 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops_free
);
1386 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
1392 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
1393 [RADV_PREFIX_DELEGATION_NONE
] = "no",
1394 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
1395 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
1396 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
1399 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
1400 radv_prefix_delegation
,
1401 RADVPrefixDelegation
,
1402 RADV_PREFIX_DELEGATION_BOTH
);
1404 int config_parse_router_prefix_delegation(
1406 const char *filename
,
1408 const char *section
,
1409 unsigned section_line
,
1416 RADVPrefixDelegation val
, *ra
= ASSERT_PTR(data
);
1423 if (streq(lvalue
, "IPv6SendRA")) {
1424 r
= parse_boolean(rvalue
);
1426 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1427 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1431 /* When IPv6SendRA= is enabled, only static prefixes are sent by default, and users
1432 * need to explicitly enable DHCPv6PrefixDelegation=. */
1433 *ra
= r
? RADV_PREFIX_DELEGATION_STATIC
: RADV_PREFIX_DELEGATION_NONE
;
1437 /* For backward compatibility */
1438 val
= radv_prefix_delegation_from_string(rvalue
);
1440 log_syntax(unit
, LOG_WARNING
, filename
, line
, val
,
1441 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1449 int config_parse_router_lifetime(
1451 const char *filename
,
1453 const char *section
,
1454 unsigned section_line
,
1461 usec_t usec
, *lifetime
= ASSERT_PTR(data
);
1469 if (isempty(rvalue
)) {
1470 *lifetime
= RADV_DEFAULT_ROUTER_LIFETIME_USEC
;
1474 r
= parse_sec(rvalue
, &usec
);
1476 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1477 "Failed to parse router lifetime, ignoring assignment: %s", rvalue
);
1481 if (usec
< RADV_MIN_ROUTER_LIFETIME_USEC
) {
1482 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1483 "Router lifetime %s is too short, using %s.",
1484 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1485 FORMAT_TIMESPAN(RADV_MIN_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1486 usec
= RADV_MIN_ROUTER_LIFETIME_USEC
;
1487 } else if (usec
> RADV_MAX_ROUTER_LIFETIME_USEC
) {
1488 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1489 "Router lifetime %s is too large, using %s.",
1490 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1491 FORMAT_TIMESPAN(RADV_MAX_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1492 usec
= RADV_MAX_ROUTER_LIFETIME_USEC
;
1500 int config_parse_router_uint32_msec_usec(
1502 const char *filename
,
1504 const char *section
,
1505 unsigned section_line
,
1512 usec_t usec
, *router_usec
= ASSERT_PTR(data
);
1520 if (isempty(rvalue
)) {
1525 r
= parse_sec(rvalue
, &usec
);
1527 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1528 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1532 if (usec
!= USEC_INFINITY
&&
1533 usec
> RADV_MAX_UINT32_MSEC_USEC
) {
1534 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1535 "Invalid [%s] %s=, ignoring assignment: %s", section
, lvalue
, rvalue
);
1539 *router_usec
= usec
;
1543 int config_parse_router_preference(
1545 const char *filename
,
1547 const char *section
,
1548 unsigned section_line
,
1555 Network
*network
= userdata
;
1563 if (streq(rvalue
, "high"))
1564 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
1565 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
1566 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
1567 else if (streq(rvalue
, "low"))
1568 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
1570 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1571 "Invalid router preference, ignoring assignment: %s", rvalue
);
1576 int config_parse_router_home_agent_lifetime(
1578 const char *filename
,
1580 const char *section
,
1581 unsigned section_line
,
1588 usec_t usec
, *home_agent_lifetime_usec
= ASSERT_PTR(data
);
1596 if (isempty(rvalue
)) {
1597 *home_agent_lifetime_usec
= 0;
1601 r
= parse_sec(rvalue
, &usec
);
1603 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1604 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1608 if (!timestamp_is_set(usec
) ||
1609 usec
> RADV_HOME_AGENT_MAX_LIFETIME_USEC
) {
1610 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1611 "Invalid [%s] %s=, ignoring assignment: %s", section
, lvalue
, rvalue
);
1615 *home_agent_lifetime_usec
= usec
;