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_PREF64_DEFAULT_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 int radv_set_prefix(Link
*link
, Prefix
*prefix
) {
283 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
290 r
= sd_radv_prefix_new(&p
);
294 r
= sd_radv_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
298 r
= sd_radv_prefix_set_preferred_lifetime(p
, prefix
->preferred_lifetime
, USEC_INFINITY
);
302 r
= sd_radv_prefix_set_valid_lifetime(p
, prefix
->valid_lifetime
, USEC_INFINITY
);
306 r
= sd_radv_prefix_set_onlink(p
, prefix
->onlink
);
310 r
= sd_radv_prefix_set_address_autoconfiguration(p
, prefix
->address_auto_configuration
);
314 return sd_radv_add_prefix(link
->radv
, p
);
317 static int radv_set_route_prefix(Link
*link
, RoutePrefix
*prefix
) {
318 _cleanup_(sd_radv_route_prefix_unrefp
) sd_radv_route_prefix
*p
= NULL
;
325 r
= sd_radv_route_prefix_new(&p
);
329 r
= sd_radv_route_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
333 r
= sd_radv_route_prefix_set_lifetime(p
, prefix
->lifetime
, USEC_INFINITY
);
337 return sd_radv_add_route_prefix(link
->radv
, p
);
340 static int radv_set_pref64_prefix(Link
*link
, pref64Prefix
*prefix
) {
341 _cleanup_(sd_radv_pref64_prefix_unrefp
) sd_radv_pref64_prefix
*p
= NULL
;
348 r
= sd_radv_pref64_prefix_new(&p
);
352 r
= sd_radv_pref64_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
, prefix
->lifetime
);
356 return sd_radv_add_pref64_prefix(link
->radv
, p
);
359 static int network_get_ipv6_dns(Network
*network
, struct in6_addr
**ret_addresses
, size_t *ret_size
) {
360 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
361 size_t n_addresses
= 0;
364 assert(ret_addresses
);
367 for (size_t i
= 0; i
< network
->n_dns
; i
++) {
368 union in_addr_union
*addr
;
370 if (network
->dns
[i
]->family
!= AF_INET6
)
373 addr
= &network
->dns
[i
]->address
;
375 if (in_addr_is_null(AF_INET6
, addr
) ||
376 in_addr_is_link_local(AF_INET6
, addr
) ||
377 in_addr_is_localhost(AF_INET6
, addr
))
380 if (!GREEDY_REALLOC(addresses
, n_addresses
+ 1))
383 addresses
[n_addresses
++] = addr
->in6
;
386 *ret_addresses
= TAKE_PTR(addresses
);
387 *ret_size
= n_addresses
;
392 static int radv_set_dns(Link
*link
, Link
*uplink
) {
393 _cleanup_free_
struct in6_addr
*dns
= NULL
;
397 if (!link
->network
->router_emit_dns
)
400 if (link
->network
->router_dns
) {
403 dns
= new(struct in6_addr
, link
->network
->n_router_dns
);
408 for (size_t i
= 0; i
< link
->network
->n_router_dns
; i
++)
409 if (in6_addr_is_null(&link
->network
->router_dns
[i
])) {
410 if (in6_addr_is_set(&link
->ipv6ll_address
))
411 *(p
++) = link
->ipv6ll_address
;
413 *(p
++) = link
->network
->router_dns
[i
];
420 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
425 assert(uplink
->network
);
427 r
= network_get_ipv6_dns(uplink
->network
, &dns
, &n_dns
);
435 return sd_radv_set_rdnss(link
->radv
,
436 link
->network
->router_dns_lifetime_usec
,
440 static int radv_set_domains(Link
*link
, Link
*uplink
) {
441 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
442 OrderedSet
*search_domains
;
444 if (!link
->network
->router_emit_domains
)
447 search_domains
= link
->network
->router_search_domains
;
452 search_domains
= link
->network
->search_domains
;
457 assert(uplink
->network
);
459 search_domains
= uplink
->network
->search_domains
;
467 s
= ordered_set_get_strv(search_domains
);
471 return sd_radv_set_dnssl(link
->radv
,
472 link
->network
->router_dns_lifetime_usec
,
477 static int radv_find_uplink(Link
*link
, Link
**ret
) {
482 if (link
->network
->router_uplink_name
)
483 return link_get_by_name(link
->manager
, link
->network
->router_uplink_name
, ret
);
485 if (link
->network
->router_uplink_index
> 0)
486 return link_get_by_index(link
->manager
, link
->network
->router_uplink_index
, ret
);
488 if (link
->network
->router_uplink_index
== UPLINK_INDEX_AUTO
) {
489 if (link_dhcp_pd_is_enabled(link
))
490 r
= dhcp_pd_find_uplink(link
, ret
); /* When DHCP-PD is enabled, use its uplink. */
492 r
= manager_find_uplink(link
->manager
, AF_INET6
, link
, ret
);
494 /* It is not necessary to propagate error in automatic selection. */
503 static int radv_configure(Link
*link
) {
511 assert(link
->network
);
516 r
= sd_radv_new(&link
->radv
);
520 r
= sd_radv_attach_event(link
->radv
, link
->manager
->event
, 0);
524 if (link
->hw_addr
.length
== ETH_ALEN
) {
525 r
= sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
530 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
534 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
538 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
542 r
= sd_radv_set_router_lifetime(link
->radv
, link
->network
->router_lifetime_usec
);
546 r
= sd_radv_set_hop_limit(link
->radv
, link
->network
->router_hop_limit
);
550 if (link
->network
->router_lifetime_usec
> 0) {
551 r
= sd_radv_set_preference(link
->radv
, link
->network
->router_preference
);
556 if (link
->network
->router_retransmit_usec
> 0) {
557 r
= sd_radv_set_retransmit(link
->radv
, link
->network
->router_retransmit_usec
);
562 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
563 r
= radv_set_prefix(link
, p
);
564 if (r
< 0 && r
!= -EEXIST
)
568 HASHMAP_FOREACH(q
, link
->network
->route_prefixes_by_section
) {
569 r
= radv_set_route_prefix(link
, q
);
570 if (r
< 0 && r
!= -EEXIST
)
574 HASHMAP_FOREACH(n
, link
->network
->pref64_prefixes_by_section
) {
575 r
= radv_set_pref64_prefix(link
, n
);
576 if (r
< 0 && r
!= -EEXIST
)
580 (void) radv_find_uplink(link
, &uplink
);
582 r
= radv_set_dns(link
, uplink
);
584 return log_link_debug_errno(link
, r
, "Could not set RA DNS: %m");
586 r
= radv_set_domains(link
, uplink
);
588 return log_link_debug_errno(link
, r
, "Could not set RA Domains: %m");
590 r
= sd_radv_set_home_agent_information(link
->radv
, link
->network
->router_home_agent_information
);
594 r
= sd_radv_set_home_agent_preference(link
->radv
, link
->network
->router_home_agent_preference
);
598 r
= sd_radv_set_home_agent_lifetime(link
->radv
, link
->network
->home_agent_lifetime_usec
);
605 int radv_update_mac(Link
*link
) {
614 if (link
->hw_addr
.length
!= ETH_ALEN
)
617 restart
= sd_radv_is_running(link
->radv
);
619 r
= sd_radv_stop(link
->radv
);
623 r
= sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
628 r
= sd_radv_start(link
->radv
);
636 static int radv_is_ready_to_configure(Link
*link
) {
637 bool needs_uplink
= false;
641 assert(link
->network
);
643 if (!link_is_ready_to_configure(link
, /* allow_unmanaged = */ false))
646 if (in6_addr_is_null(&link
->ipv6ll_address
))
649 if (link
->hw_addr
.length
!= ETH_ALEN
|| hw_addr_is_null(&link
->hw_addr
))
652 if (link
->network
->router_emit_dns
&& !link
->network
->router_dns
) {
653 _cleanup_free_
struct in6_addr
*dns
= NULL
;
656 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
660 needs_uplink
= r
== 0;
663 if (link
->network
->router_emit_domains
&&
664 !link
->network
->router_search_domains
&&
665 !link
->network
->search_domains
)
671 if (radv_find_uplink(link
, &uplink
) < 0)
674 if (uplink
&& !uplink
->network
)
681 static int radv_process_request(Request
*req
, Link
*link
, void *userdata
) {
686 r
= radv_is_ready_to_configure(link
);
690 r
= radv_configure(link
);
692 return log_link_warning_errno(link
, r
, "Failed to configure IPv6 Router Advertisement engine: %m");
694 if (link_has_carrier(link
)) {
695 r
= radv_start(link
);
697 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Advertisement engine: %m");
700 log_link_debug(link
, "IPv6 Router Advertisement engine is configured%s.",
701 link_has_carrier(link
) ? " and started" : "");
705 int link_request_radv(Link
*link
) {
710 if (!link_radv_enabled(link
))
716 r
= link_queue_request(link
, REQUEST_TYPE_RADV
, radv_process_request
, NULL
);
718 return log_link_warning_errno(link
, r
, "Failed to request configuring of the IPv6 Router Advertisement engine: %m");
720 log_link_debug(link
, "Requested configuring of the IPv6 Router Advertisement engine.");
724 int radv_start(Link
*link
) {
728 assert(link
->network
);
733 if (!link_has_carrier(link
))
736 if (in6_addr_is_null(&link
->ipv6ll_address
))
739 if (sd_radv_is_running(link
->radv
))
742 if (link
->network
->dhcp_pd_announce
) {
743 r
= dhcp_request_prefix_delegation(link
);
745 return log_link_debug_errno(link
, r
, "Failed to request DHCP delegated subnet prefix: %m");
748 log_link_debug(link
, "Starting IPv6 Router Advertisements");
749 return sd_radv_start(link
->radv
);
754 const struct in6_addr
*prefix
,
756 usec_t lifetime_preferred_usec
,
757 usec_t lifetime_valid_usec
) {
759 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
767 r
= sd_radv_prefix_new(&p
);
771 r
= sd_radv_prefix_set_prefix(p
, prefix
, prefix_len
);
775 r
= sd_radv_prefix_set_preferred_lifetime(p
, RADV_DEFAULT_PREFERRED_LIFETIME_USEC
, lifetime_preferred_usec
);
779 r
= sd_radv_prefix_set_valid_lifetime(p
, RADV_DEFAULT_VALID_LIFETIME_USEC
, lifetime_valid_usec
);
783 r
= sd_radv_add_prefix(link
->radv
, p
);
784 if (r
< 0 && r
!= -EEXIST
)
790 static int prefix_section_verify(Prefix
*p
) {
793 if (section_is_invalid(p
->section
))
796 if (in6_addr_is_null(&p
->prefix
))
797 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
798 "%s: [IPv6Prefix] section without Prefix= field configured, "
799 "or specified prefix is the null address. "
800 "Ignoring [IPv6Prefix] section from line %u.",
801 p
->section
->filename
, p
->section
->line
);
803 if (p
->prefixlen
< 3 || p
->prefixlen
> 128)
804 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
805 "%s: Invalid prefix length %u is specified in [IPv6Prefix] section. "
806 "Valid range is 3…128. Ignoring [IPv6Prefix] section from line %u.",
807 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
809 if (p
->prefixlen
> 64) {
810 log_info("%s:%u: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %s%s.",
811 p
->section
->filename
, p
->section
->line
,
813 p
->assign
? ", refusing to assign an address in " : "",
814 p
->assign
? IN6_ADDR_PREFIX_TO_STRING(&p
->prefix
, p
->prefixlen
) : "");
819 if (p
->valid_lifetime
== 0)
820 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
821 "%s: The valid lifetime of prefix cannot be zero. "
822 "Ignoring [IPv6Prefix] section from line %u.",
823 p
->section
->filename
, p
->section
->line
);
825 if (p
->preferred_lifetime
> p
->valid_lifetime
)
826 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
827 "%s: The preferred lifetime %s is longer than the valid lifetime %s. "
828 "Ignoring [IPv6Prefix] section from line %u.",
829 p
->section
->filename
,
830 FORMAT_TIMESPAN(p
->preferred_lifetime
, USEC_PER_SEC
),
831 FORMAT_TIMESPAN(p
->valid_lifetime
, USEC_PER_SEC
),
837 void network_drop_invalid_prefixes(Network
*network
) {
842 HASHMAP_FOREACH(p
, network
->prefixes_by_section
)
843 if (prefix_section_verify(p
) < 0)
847 static int route_prefix_section_verify(RoutePrefix
*p
) {
848 if (section_is_invalid(p
->section
))
851 if (p
->prefixlen
> 128)
852 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
853 "%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. "
854 "Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.",
855 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
857 if (p
->lifetime
== 0)
858 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
859 "%s: The lifetime of route cannot be zero. "
860 "Ignoring [IPv6RoutePrefix] section from line %u.",
861 p
->section
->filename
, p
->section
->line
);
866 void network_drop_invalid_route_prefixes(Network
*network
) {
871 HASHMAP_FOREACH(p
, network
->route_prefixes_by_section
)
872 if (route_prefix_section_verify(p
) < 0)
873 route_prefix_free(p
);
876 void network_drop_invalid_pref64_prefixes(Network
*network
) {
881 HASHMAP_FOREACH(p
, network
->pref64_prefixes_by_section
)
882 if (section_is_invalid(p
->section
))
883 pref64_prefix_free(p
);
886 int config_parse_prefix(
888 const char *filename
,
891 unsigned section_line
,
898 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
899 Network
*network
= ASSERT_PTR(userdata
);
900 union in_addr_union a
;
908 r
= prefix_new_static(network
, filename
, section_line
, &p
);
912 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
914 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
915 "Prefix is invalid, ignoring assignment: %s", rvalue
);
919 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
926 int config_parse_prefix_boolean(
928 const char *filename
,
931 unsigned section_line
,
938 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
939 Network
*network
= ASSERT_PTR(userdata
);
947 r
= prefix_new_static(network
, filename
, section_line
, &p
);
951 r
= parse_boolean(rvalue
);
953 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
954 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
958 if (streq(lvalue
, "OnLink"))
960 else if (streq(lvalue
, "AddressAutoconfiguration"))
961 p
->address_auto_configuration
= r
;
962 else if (streq(lvalue
, "Assign"))
965 assert_not_reached();
971 int config_parse_prefix_lifetime(
973 const char *filename
,
976 unsigned section_line
,
983 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
984 Network
*network
= ASSERT_PTR(userdata
);
993 r
= prefix_new_static(network
, filename
, section_line
, &p
);
997 r
= parse_sec(rvalue
, &usec
);
999 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1000 "Lifetime is invalid, ignoring assignment: %s", rvalue
);
1004 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1005 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1006 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1010 if (streq(lvalue
, "PreferredLifetimeSec"))
1011 p
->preferred_lifetime
= usec
;
1012 else if (streq(lvalue
, "ValidLifetimeSec"))
1013 p
->valid_lifetime
= usec
;
1015 assert_not_reached();
1021 int config_parse_prefix_metric(
1023 const char *filename
,
1025 const char *section
,
1026 unsigned section_line
,
1033 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1034 Network
*network
= ASSERT_PTR(userdata
);
1042 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1046 r
= safe_atou32(rvalue
, &p
->route_metric
);
1048 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1049 "Failed to parse %s=, ignoring assignment: %s",
1058 int config_parse_prefix_token(
1060 const char *filename
,
1062 const char *section
,
1063 unsigned section_line
,
1070 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1071 Network
*network
= ASSERT_PTR(userdata
);
1079 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1083 r
= config_parse_address_generation_type(unit
, filename
, line
, section
, section_line
,
1084 lvalue
, ltype
, rvalue
, &p
->tokens
, userdata
);
1092 int config_parse_route_prefix(
1094 const char *filename
,
1096 const char *section
,
1097 unsigned section_line
,
1104 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1105 Network
*network
= ASSERT_PTR(userdata
);
1106 union in_addr_union a
;
1114 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1118 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
1120 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1121 "Route prefix is invalid, ignoring assignment: %s", rvalue
);
1125 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
1132 int config_parse_route_prefix_lifetime(
1134 const char *filename
,
1136 const char *section
,
1137 unsigned section_line
,
1144 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1145 Network
*network
= ASSERT_PTR(userdata
);
1154 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1158 r
= parse_sec(rvalue
, &usec
);
1160 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1161 "Route lifetime is invalid, ignoring assignment: %s", rvalue
);
1165 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1166 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1167 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1177 int config_parse_pref64_prefix(
1179 const char *filename
,
1181 const char *section
,
1182 unsigned section_line
,
1189 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1190 Network
*network
= ASSERT_PTR(userdata
);
1191 union in_addr_union a
;
1200 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1204 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &prefixlen
);
1206 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1207 "PREF64 prefix is invalid, ignoring assignment: %s", rvalue
);
1211 if (!IN_SET(prefixlen
, 96, 64, 56, 48, 40, 32)) {
1212 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1213 "PREF64 prefixlen is invalid, ignoring assignment: %s", rvalue
);
1217 (void) in6_addr_mask(&a
.in6
,prefixlen
);
1219 p
->prefixlen
= prefixlen
;
1225 int config_parse_pref64_prefix_lifetime(
1227 const char *filename
,
1229 const char *section
,
1230 unsigned section_line
,
1237 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1238 Network
*network
= ASSERT_PTR(userdata
);
1247 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1251 r
= parse_sec(rvalue
, &usec
);
1253 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1254 "PREF64 lifetime is invalid, ignoring assignment: %s", rvalue
);
1258 if (usec
== USEC_INFINITY
|| DIV_ROUND_UP(usec
, 8 * USEC_PER_SEC
) >= UINT64_C(1) << 13) {
1259 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1260 "PREF64 lifetime is too long, ignoring assignment: %s", rvalue
);
1270 int config_parse_radv_dns(
1272 const char *filename
,
1274 const char *section
,
1275 unsigned section_line
,
1289 if (isempty(rvalue
)) {
1290 n
->n_router_dns
= 0;
1291 n
->router_dns
= mfree(n
->router_dns
);
1295 for (const char *p
= rvalue
;;) {
1296 _cleanup_free_
char *w
= NULL
;
1297 union in_addr_union a
;
1299 r
= extract_first_word(&p
, &w
, NULL
, 0);
1303 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1304 "Failed to extract word, ignoring: %s", rvalue
);
1310 if (streq(w
, "_link_local"))
1313 r
= in_addr_from_string(AF_INET6
, w
, &a
);
1315 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1316 "Failed to parse DNS server address, ignoring: %s", w
);
1320 if (in_addr_is_null(AF_INET6
, &a
)) {
1321 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1322 "DNS server address is null, ignoring: %s", w
);
1328 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
1332 m
[n
->n_router_dns
++] = a
.in6
;
1337 int config_parse_radv_search_domains(
1339 const char *filename
,
1341 const char *section
,
1342 unsigned section_line
,
1356 if (isempty(rvalue
)) {
1357 n
->router_search_domains
= ordered_set_free(n
->router_search_domains
);
1361 for (const char *p
= rvalue
;;) {
1362 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
1364 r
= extract_first_word(&p
, &w
, NULL
, 0);
1368 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1369 "Failed to extract word, ignoring: %s", rvalue
);
1375 r
= dns_name_apply_idna(w
, &idna
);
1377 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1378 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
1381 /* transfer ownership to simplify subsequent operations */
1384 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops_free
);
1388 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
1394 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
1395 [RADV_PREFIX_DELEGATION_NONE
] = "no",
1396 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
1397 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
1398 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
1401 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
1402 radv_prefix_delegation
,
1403 RADVPrefixDelegation
,
1404 RADV_PREFIX_DELEGATION_BOTH
);
1406 int config_parse_router_prefix_delegation(
1408 const char *filename
,
1410 const char *section
,
1411 unsigned section_line
,
1418 RADVPrefixDelegation val
, *ra
= ASSERT_PTR(data
);
1425 if (streq(lvalue
, "IPv6SendRA")) {
1426 r
= parse_boolean(rvalue
);
1428 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1429 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1433 /* When IPv6SendRA= is enabled, only static prefixes are sent by default, and users
1434 * need to explicitly enable DHCPv6PrefixDelegation=. */
1435 *ra
= r
? RADV_PREFIX_DELEGATION_STATIC
: RADV_PREFIX_DELEGATION_NONE
;
1439 /* For backward compatibility */
1440 val
= radv_prefix_delegation_from_string(rvalue
);
1442 log_syntax(unit
, LOG_WARNING
, filename
, line
, val
,
1443 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1451 int config_parse_router_lifetime(
1453 const char *filename
,
1455 const char *section
,
1456 unsigned section_line
,
1463 usec_t usec
, *lifetime
= ASSERT_PTR(data
);
1471 if (isempty(rvalue
)) {
1472 *lifetime
= RADV_DEFAULT_ROUTER_LIFETIME_USEC
;
1476 r
= parse_sec(rvalue
, &usec
);
1478 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1479 "Failed to parse router lifetime, ignoring assignment: %s", rvalue
);
1483 if (usec
< RADV_MIN_ROUTER_LIFETIME_USEC
) {
1484 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1485 "Router lifetime %s is too short, using %s.",
1486 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1487 FORMAT_TIMESPAN(RADV_MIN_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1488 usec
= RADV_MIN_ROUTER_LIFETIME_USEC
;
1489 } else if (usec
> RADV_MAX_ROUTER_LIFETIME_USEC
) {
1490 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1491 "Router lifetime %s is too large, using %s.",
1492 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1493 FORMAT_TIMESPAN(RADV_MAX_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1494 usec
= RADV_MAX_ROUTER_LIFETIME_USEC
;
1502 int config_parse_router_retransmit(
1504 const char *filename
,
1506 const char *section
,
1507 unsigned section_line
,
1514 usec_t usec
, *router_retransmit_usec
= ASSERT_PTR(data
);
1522 if (isempty(rvalue
)) {
1523 *router_retransmit_usec
= 0;
1527 r
= parse_sec(rvalue
, &usec
);
1529 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1530 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1534 if (usec
!= USEC_INFINITY
&&
1535 usec
> RADV_MAX_RETRANSMIT_USEC
) {
1536 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1537 "Invalid [%s] %s=, ignoring assignment: %s", section
, lvalue
, rvalue
);
1541 *router_retransmit_usec
= usec
;
1545 int config_parse_router_preference(
1547 const char *filename
,
1549 const char *section
,
1550 unsigned section_line
,
1557 Network
*network
= userdata
;
1565 if (streq(rvalue
, "high"))
1566 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
1567 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
1568 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
1569 else if (streq(rvalue
, "low"))
1570 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
1572 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1573 "Invalid router preference, ignoring assignment: %s", rvalue
);
1578 int config_parse_router_home_agent_lifetime(
1580 const char *filename
,
1582 const char *section
,
1583 unsigned section_line
,
1590 usec_t usec
, *home_agent_lifetime_usec
= ASSERT_PTR(data
);
1598 if (isempty(rvalue
)) {
1599 *home_agent_lifetime_usec
= 0;
1603 r
= parse_sec(rvalue
, &usec
);
1605 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1606 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1610 if (!timestamp_is_set(usec
) ||
1611 usec
> RADV_HOME_AGENT_MAX_LIFETIME_USEC
) {
1612 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1613 "Invalid [%s] %s=, ignoring assignment: %s", section
, lvalue
, rvalue
);
1617 *home_agent_lifetime_usec
= usec
;