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 .flags
= ND_OPT_PI_FLAG_ONLINK
| ND_OPT_PI_FLAG_AUTO
,
84 .preferred_lifetime
= RADV_DEFAULT_PREFERRED_LIFETIME_USEC
,
85 .valid_lifetime
= RADV_DEFAULT_VALID_LIFETIME_USEC
,
88 r
= hashmap_ensure_put(&network
->prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
92 *ret
= TAKE_PTR(prefix
);
96 RoutePrefix
*route_prefix_free(RoutePrefix
*prefix
) {
100 if (prefix
->network
) {
101 assert(prefix
->section
);
102 hashmap_remove(prefix
->network
->route_prefixes_by_section
, prefix
->section
);
105 config_section_free(prefix
->section
);
107 return mfree(prefix
);
110 DEFINE_SECTION_CLEANUP_FUNCTIONS(RoutePrefix
, route_prefix_free
);
112 static int route_prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutePrefix
**ret
) {
113 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
114 _cleanup_(route_prefix_freep
) RoutePrefix
*prefix
= NULL
;
120 assert(section_line
> 0);
122 r
= config_section_new(filename
, section_line
, &n
);
126 prefix
= hashmap_get(network
->route_prefixes_by_section
, n
);
128 *ret
= TAKE_PTR(prefix
);
132 prefix
= new(RoutePrefix
, 1);
136 *prefix
= (RoutePrefix
) {
138 .section
= TAKE_PTR(n
),
140 .lifetime
= RADV_DEFAULT_VALID_LIFETIME_USEC
,
143 r
= hashmap_ensure_put(&network
->route_prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
147 *ret
= TAKE_PTR(prefix
);
151 pref64Prefix
*pref64_prefix_free(pref64Prefix
*prefix
) {
155 if (prefix
->network
) {
156 assert(prefix
->section
);
157 hashmap_remove(prefix
->network
->pref64_prefixes_by_section
, prefix
->section
);
160 config_section_free(prefix
->section
);
162 return mfree(prefix
);
165 DEFINE_SECTION_CLEANUP_FUNCTIONS(pref64Prefix
, pref64_prefix_free
);
167 static int pref64_prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, pref64Prefix
**ret
) {
168 _cleanup_(config_section_freep
) ConfigSection
*n
= NULL
;
169 _cleanup_(pref64_prefix_freep
) pref64Prefix
*prefix
= NULL
;
175 assert(section_line
> 0);
177 r
= config_section_new(filename
, section_line
, &n
);
181 prefix
= hashmap_get(network
->pref64_prefixes_by_section
, n
);
183 *ret
= TAKE_PTR(prefix
);
187 prefix
= new(pref64Prefix
, 1);
191 *prefix
= (pref64Prefix
) {
193 .section
= TAKE_PTR(n
),
195 .lifetime
= RADV_PREF64_DEFAULT_LIFETIME_USEC
,
198 r
= hashmap_ensure_put(&network
->pref64_prefixes_by_section
, &config_section_hash_ops
, prefix
->section
, prefix
);
202 *ret
= TAKE_PTR(prefix
);
206 int link_request_radv_addresses(Link
*link
) {
212 if (!link_radv_enabled(link
))
215 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
219 /* radv_generate_addresses() below requires the prefix length <= 64. */
220 if (p
->prefixlen
> 64)
223 _cleanup_hashmap_free_ Hashmap
*tokens_by_address
= NULL
;
224 r
= radv_generate_addresses(link
, p
->tokens
, &p
->prefix
, p
->prefixlen
, &tokens_by_address
);
230 HASHMAP_FOREACH_KEY(token
, a
, tokens_by_address
) {
231 _cleanup_(address_unrefp
) Address
*address
= NULL
;
233 r
= address_new(&address
);
237 address
->source
= NETWORK_CONFIG_SOURCE_STATIC
;
238 address
->family
= AF_INET6
;
239 address
->in_addr
.in6
= *a
;
240 address
->prefixlen
= p
->prefixlen
;
241 address
->route_metric
= p
->route_metric
;
242 address
->token
= ipv6_token_ref(token
);
244 r
= link_request_static_address(link
, address
);
253 int link_reconfigure_radv_address(Address
*address
, Link
*link
) {
257 assert(address
->source
== NETWORK_CONFIG_SOURCE_STATIC
);
260 r
= regenerate_address(address
, link
);
264 r
= link_request_static_address(link
, address
);
268 if (link
->static_address_messages
!= 0) {
269 link
->static_addresses_configured
= false;
270 link_set_state(link
, LINK_STATE_CONFIGURING
);
276 static int radv_set_prefix(Link
*link
, Prefix
*prefix
) {
277 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
284 r
= sd_radv_prefix_new(&p
);
288 r
= sd_radv_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
292 r
= sd_radv_prefix_set_preferred_lifetime(p
, prefix
->preferred_lifetime
, USEC_INFINITY
);
296 r
= sd_radv_prefix_set_valid_lifetime(p
, prefix
->valid_lifetime
, USEC_INFINITY
);
300 r
= sd_radv_prefix_set_onlink(p
, FLAGS_SET(prefix
->flags
, ND_OPT_PI_FLAG_ONLINK
));
304 r
= sd_radv_prefix_set_address_autoconfiguration(p
, FLAGS_SET(prefix
->flags
, ND_OPT_PI_FLAG_AUTO
));
308 return sd_radv_add_prefix(link
->radv
, p
);
311 static int radv_set_route_prefix(Link
*link
, RoutePrefix
*prefix
) {
312 _cleanup_(sd_radv_route_prefix_unrefp
) sd_radv_route_prefix
*p
= NULL
;
319 r
= sd_radv_route_prefix_new(&p
);
323 r
= sd_radv_route_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
327 r
= sd_radv_route_prefix_set_lifetime(p
, prefix
->lifetime
, USEC_INFINITY
);
331 return sd_radv_add_route_prefix(link
->radv
, p
);
334 static int radv_set_pref64_prefix(Link
*link
, pref64Prefix
*prefix
) {
335 _cleanup_(sd_radv_pref64_prefix_unrefp
) sd_radv_pref64_prefix
*p
= NULL
;
342 r
= sd_radv_pref64_prefix_new(&p
);
346 r
= sd_radv_pref64_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
, prefix
->lifetime
);
350 return sd_radv_add_pref64_prefix(link
->radv
, p
);
353 static int network_get_ipv6_dns(Network
*network
, struct in6_addr
**ret_addresses
, size_t *ret_size
) {
354 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
355 size_t n_addresses
= 0;
358 assert(ret_addresses
);
361 for (size_t i
= 0; i
< network
->n_dns
; i
++) {
362 union in_addr_union
*addr
;
364 if (network
->dns
[i
]->family
!= AF_INET6
)
367 addr
= &network
->dns
[i
]->address
;
369 if (in_addr_is_null(AF_INET6
, addr
) ||
370 in_addr_is_link_local(AF_INET6
, addr
) ||
371 in_addr_is_localhost(AF_INET6
, addr
))
374 if (!GREEDY_REALLOC(addresses
, n_addresses
+ 1))
377 addresses
[n_addresses
++] = addr
->in6
;
380 *ret_addresses
= TAKE_PTR(addresses
);
381 *ret_size
= n_addresses
;
386 static int radv_set_dns(Link
*link
, Link
*uplink
) {
387 _cleanup_free_
struct in6_addr
*dns
= NULL
;
391 if (!link
->network
->router_emit_dns
)
394 if (link
->network
->router_dns
) {
397 dns
= new(struct in6_addr
, link
->network
->n_router_dns
);
402 for (size_t i
= 0; i
< link
->network
->n_router_dns
; i
++)
403 if (in6_addr_is_null(&link
->network
->router_dns
[i
])) {
404 if (in6_addr_is_set(&link
->ipv6ll_address
))
405 *(p
++) = link
->ipv6ll_address
;
407 *(p
++) = link
->network
->router_dns
[i
];
414 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
419 assert(uplink
->network
);
421 r
= network_get_ipv6_dns(uplink
->network
, &dns
, &n_dns
);
429 return sd_radv_set_rdnss(link
->radv
,
430 link
->network
->router_dns_lifetime_usec
,
434 static int radv_set_domains(Link
*link
, Link
*uplink
) {
435 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
436 OrderedSet
*search_domains
;
438 if (!link
->network
->router_emit_domains
)
441 search_domains
= link
->network
->router_search_domains
;
446 search_domains
= link
->network
->search_domains
;
451 assert(uplink
->network
);
453 search_domains
= uplink
->network
->search_domains
;
461 s
= ordered_set_get_strv(search_domains
);
465 return sd_radv_set_dnssl(link
->radv
,
466 link
->network
->router_dns_lifetime_usec
,
471 static int radv_find_uplink(Link
*link
, Link
**ret
) {
476 if (link
->network
->router_uplink_name
)
477 return link_get_by_name(link
->manager
, link
->network
->router_uplink_name
, ret
);
479 if (link
->network
->router_uplink_index
> 0)
480 return link_get_by_index(link
->manager
, link
->network
->router_uplink_index
, ret
);
482 if (link
->network
->router_uplink_index
== UPLINK_INDEX_AUTO
) {
483 if (link_dhcp_pd_is_enabled(link
))
484 r
= dhcp_pd_find_uplink(link
, ret
); /* When DHCP-PD is enabled, use its uplink. */
486 r
= manager_find_uplink(link
->manager
, AF_INET6
, link
, ret
);
488 /* It is not necessary to propagate error in automatic selection. */
497 static int radv_configure(Link
*link
) {
505 assert(link
->network
);
510 r
= sd_radv_new(&link
->radv
);
514 r
= sd_radv_attach_event(link
->radv
, link
->manager
->event
, 0);
518 if (link
->hw_addr
.length
== ETH_ALEN
) {
519 r
= sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
524 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
528 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
532 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
536 r
= sd_radv_set_router_lifetime(link
->radv
, link
->network
->router_lifetime_usec
);
540 r
= sd_radv_set_hop_limit(link
->radv
, link
->network
->router_hop_limit
);
544 r
= sd_radv_set_preference(link
->radv
, link
->network
->router_preference
);
548 r
= sd_radv_set_reachable_time(link
->radv
, link
->network
->router_reachable_usec
);
552 r
= sd_radv_set_retransmit(link
->radv
, link
->network
->router_retransmit_usec
);
556 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
557 r
= radv_set_prefix(link
, p
);
558 if (r
< 0 && r
!= -EEXIST
)
562 HASHMAP_FOREACH(q
, link
->network
->route_prefixes_by_section
) {
563 r
= radv_set_route_prefix(link
, q
);
564 if (r
< 0 && r
!= -EEXIST
)
568 HASHMAP_FOREACH(n
, link
->network
->pref64_prefixes_by_section
) {
569 r
= radv_set_pref64_prefix(link
, n
);
570 if (r
< 0 && r
!= -EEXIST
)
574 (void) radv_find_uplink(link
, &uplink
);
576 r
= radv_set_dns(link
, uplink
);
578 return log_link_debug_errno(link
, r
, "Could not set RA DNS: %m");
580 r
= radv_set_domains(link
, uplink
);
582 return log_link_debug_errno(link
, r
, "Could not set RA Domains: %m");
584 r
= sd_radv_set_home_agent_information(link
->radv
, link
->network
->router_home_agent_information
);
588 r
= sd_radv_set_home_agent_preference(link
->radv
, link
->network
->router_home_agent_preference
);
592 r
= sd_radv_set_home_agent_lifetime(link
->radv
, link
->network
->home_agent_lifetime_usec
);
599 int radv_update_mac(Link
*link
) {
605 if (link
->hw_addr
.length
!= ETH_ALEN
)
608 return sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
611 static int radv_is_ready_to_configure(Link
*link
) {
612 bool needs_uplink
= false;
616 assert(link
->network
);
618 if (!link_is_ready_to_configure(link
, /* allow_unmanaged = */ false))
621 if (in6_addr_is_null(&link
->ipv6ll_address
))
624 if (link
->hw_addr
.length
!= ETH_ALEN
|| hw_addr_is_null(&link
->hw_addr
))
627 if (link
->network
->router_emit_dns
&& !link
->network
->router_dns
) {
628 _cleanup_free_
struct in6_addr
*dns
= NULL
;
631 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
635 needs_uplink
= r
== 0;
638 if (link
->network
->router_emit_domains
&&
639 !link
->network
->router_search_domains
&&
640 !link
->network
->search_domains
)
646 if (radv_find_uplink(link
, &uplink
) < 0)
649 if (uplink
&& !uplink
->network
)
656 static int radv_process_request(Request
*req
, Link
*link
, void *userdata
) {
661 r
= radv_is_ready_to_configure(link
);
665 r
= radv_configure(link
);
667 return log_link_warning_errno(link
, r
, "Failed to configure IPv6 Router Advertisement engine: %m");
669 if (link_has_carrier(link
)) {
670 r
= radv_start(link
);
672 return log_link_warning_errno(link
, r
, "Failed to start IPv6 Router Advertisement engine: %m");
675 log_link_debug(link
, "IPv6 Router Advertisement engine is configured%s.",
676 link_has_carrier(link
) ? " and started" : "");
680 int link_request_radv(Link
*link
) {
685 if (!link_radv_enabled(link
))
691 r
= link_queue_request(link
, REQUEST_TYPE_RADV
, radv_process_request
, NULL
);
693 return log_link_warning_errno(link
, r
, "Failed to request configuring of the IPv6 Router Advertisement engine: %m");
695 log_link_debug(link
, "Requested configuring of the IPv6 Router Advertisement engine.");
699 int radv_start(Link
*link
) {
703 assert(link
->network
);
708 if (!link_has_carrier(link
))
711 if (in6_addr_is_null(&link
->ipv6ll_address
))
714 if (sd_radv_is_running(link
->radv
))
717 if (link
->network
->dhcp_pd_announce
) {
718 r
= dhcp_request_prefix_delegation(link
);
720 return log_link_debug_errno(link
, r
, "Failed to request DHCP delegated subnet prefix: %m");
723 r
= sd_radv_set_link_local_address(link
->radv
, &link
->ipv6ll_address
);
727 log_link_debug(link
, "Starting IPv6 Router Advertisements");
728 return sd_radv_start(link
->radv
);
733 const struct in6_addr
*prefix
,
735 usec_t lifetime_preferred_usec
,
736 usec_t lifetime_valid_usec
) {
738 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
746 r
= sd_radv_prefix_new(&p
);
750 r
= sd_radv_prefix_set_prefix(p
, prefix
, prefix_len
);
754 r
= sd_radv_prefix_set_preferred_lifetime(p
, RADV_DEFAULT_PREFERRED_LIFETIME_USEC
, lifetime_preferred_usec
);
758 r
= sd_radv_prefix_set_valid_lifetime(p
, RADV_DEFAULT_VALID_LIFETIME_USEC
, lifetime_valid_usec
);
762 r
= sd_radv_add_prefix(link
->radv
, p
);
768 if (sd_radv_is_running(link
->radv
)) {
769 /* Announce updated prefixe now. */
770 r
= sd_radv_send(link
->radv
);
778 static int prefix_section_verify(Prefix
*p
) {
781 if (section_is_invalid(p
->section
))
784 if (in6_addr_is_null(&p
->prefix
))
785 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
786 "%s: [IPv6Prefix] section without Prefix= field configured, "
787 "or specified prefix is the null address. "
788 "Ignoring [IPv6Prefix] section from line %u.",
789 p
->section
->filename
, p
->section
->line
);
791 if (p
->prefixlen
< 3 || p
->prefixlen
> 128)
792 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
793 "%s: Invalid prefix length %u is specified in [IPv6Prefix] section. "
794 "Valid range is 3…128. Ignoring [IPv6Prefix] section from line %u.",
795 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
797 if (p
->prefixlen
> 64) {
798 log_info("%s:%u: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %s%s.",
799 p
->section
->filename
, p
->section
->line
,
801 p
->assign
? ", refusing to assign an address in " : "",
802 p
->assign
? IN6_ADDR_PREFIX_TO_STRING(&p
->prefix
, p
->prefixlen
) : "");
807 if (p
->preferred_lifetime
> p
->valid_lifetime
)
808 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
809 "%s: The preferred lifetime %s is longer than the valid lifetime %s. "
810 "Ignoring [IPv6Prefix] section from line %u.",
811 p
->section
->filename
,
812 FORMAT_TIMESPAN(p
->preferred_lifetime
, USEC_PER_SEC
),
813 FORMAT_TIMESPAN(p
->valid_lifetime
, USEC_PER_SEC
),
819 static int route_prefix_section_verify(RoutePrefix
*p
) {
820 if (section_is_invalid(p
->section
))
823 if (p
->prefixlen
> 128)
824 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
825 "%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. "
826 "Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.",
827 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
832 void network_adjust_radv(Network
*network
) {
835 /* After this function is called, network->router_prefix_delegation can be treated as a boolean. */
837 if (network
->dhcp_pd
< 0)
838 /* For backward compatibility. */
839 network
->dhcp_pd
= FLAGS_SET(network
->router_prefix_delegation
, RADV_PREFIX_DELEGATION_DHCP6
);
841 if (!FLAGS_SET(network
->link_local
, ADDRESS_FAMILY_IPV6
)) {
842 if (network
->router_prefix_delegation
!= RADV_PREFIX_DELEGATION_NONE
)
843 log_warning("%s: IPv6PrefixDelegation= is enabled but IPv6 link-local addressing is disabled. "
844 "Disabling IPv6PrefixDelegation=.", network
->filename
);
846 network
->router_prefix_delegation
= RADV_PREFIX_DELEGATION_NONE
;
849 if (network
->router_prefix_delegation
== RADV_PREFIX_DELEGATION_NONE
) {
850 network
->n_router_dns
= 0;
851 network
->router_dns
= mfree(network
->router_dns
);
852 network
->router_search_domains
= ordered_set_free(network
->router_search_domains
);
855 if (!FLAGS_SET(network
->router_prefix_delegation
, RADV_PREFIX_DELEGATION_STATIC
)) {
856 network
->prefixes_by_section
= hashmap_free_with_destructor(network
->prefixes_by_section
, prefix_free
);
857 network
->route_prefixes_by_section
= hashmap_free_with_destructor(network
->route_prefixes_by_section
, route_prefix_free
);
858 network
->pref64_prefixes_by_section
= hashmap_free_with_destructor(network
->pref64_prefixes_by_section
, pref64_prefix_free
);
861 if (!network
->router_prefix_delegation
)
864 /* Below, let's verify router settings, if enabled. */
866 if (network
->router_lifetime_usec
== 0 && network
->router_preference
!= SD_NDISC_PREFERENCE_MEDIUM
)
867 /* RFC 4191, Section 2.2,
868 * If the Router Lifetime is zero, the preference value MUST be set to (00) by the sender.
870 * Note, radv_send_router() gracefully handle that. So, it is not necessary to refuse, but
871 * let's warn about that. */
872 log_notice("%s: RouterPreference=%s specified with RouterLifetimeSec=0, ignoring RouterPreference= setting.",
873 network
->filename
, ndisc_router_preference_to_string(network
->router_preference
));
876 HASHMAP_FOREACH(prefix
, network
->prefixes_by_section
)
877 if (prefix_section_verify(prefix
) < 0)
882 HASHMAP_FOREACH(route
, network
->route_prefixes_by_section
)
883 if (route_prefix_section_verify(route
) < 0)
884 route_prefix_free(route
);
886 pref64Prefix
*pref64
;
887 HASHMAP_FOREACH(pref64
, network
->pref64_prefixes_by_section
)
888 if (section_is_invalid(pref64
->section
))
889 pref64_prefix_free(pref64
);
892 int config_parse_prefix(
894 const char *filename
,
897 unsigned section_line
,
904 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
905 Network
*network
= ASSERT_PTR(userdata
);
906 union in_addr_union a
;
914 r
= prefix_new_static(network
, filename
, section_line
, &p
);
918 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
920 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
921 "Prefix is invalid, ignoring assignment: %s", rvalue
);
925 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
932 int config_parse_prefix_boolean(
934 const char *filename
,
937 unsigned section_line
,
944 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
945 Network
*network
= ASSERT_PTR(userdata
);
953 r
= prefix_new_static(network
, filename
, section_line
, &p
);
957 r
= parse_boolean(rvalue
);
959 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
960 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
965 SET_FLAG(p
->flags
, ltype
, r
);
967 assert(streq(lvalue
, "Assign"));
975 int config_parse_prefix_lifetime(
977 const char *filename
,
980 unsigned section_line
,
987 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
988 Network
*network
= ASSERT_PTR(userdata
);
997 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1001 r
= parse_sec(rvalue
, &usec
);
1003 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1004 "Lifetime is invalid, ignoring assignment: %s", rvalue
);
1008 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1009 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1010 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1014 if (streq(lvalue
, "PreferredLifetimeSec"))
1015 p
->preferred_lifetime
= usec
;
1016 else if (streq(lvalue
, "ValidLifetimeSec"))
1017 p
->valid_lifetime
= usec
;
1019 assert_not_reached();
1025 int config_parse_prefix_metric(
1027 const char *filename
,
1029 const char *section
,
1030 unsigned section_line
,
1037 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1038 Network
*network
= ASSERT_PTR(userdata
);
1046 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1050 r
= safe_atou32(rvalue
, &p
->route_metric
);
1052 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1053 "Failed to parse %s=, ignoring assignment: %s",
1062 int config_parse_prefix_token(
1064 const char *filename
,
1066 const char *section
,
1067 unsigned section_line
,
1074 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1075 Network
*network
= ASSERT_PTR(userdata
);
1083 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1087 r
= config_parse_address_generation_type(unit
, filename
, line
, section
, section_line
,
1088 lvalue
, ltype
, rvalue
, &p
->tokens
, userdata
);
1096 int config_parse_route_prefix(
1098 const char *filename
,
1100 const char *section
,
1101 unsigned section_line
,
1108 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1109 Network
*network
= ASSERT_PTR(userdata
);
1110 union in_addr_union a
;
1118 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1122 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
1124 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1125 "Route prefix is invalid, ignoring assignment: %s", rvalue
);
1129 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
1136 int config_parse_route_prefix_lifetime(
1138 const char *filename
,
1140 const char *section
,
1141 unsigned section_line
,
1148 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1149 Network
*network
= ASSERT_PTR(userdata
);
1158 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1162 r
= parse_sec(rvalue
, &usec
);
1164 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1165 "Route lifetime is invalid, ignoring assignment: %s", rvalue
);
1169 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1170 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1171 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1181 int config_parse_pref64_prefix(
1183 const char *filename
,
1185 const char *section
,
1186 unsigned section_line
,
1193 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1194 Network
*network
= ASSERT_PTR(userdata
);
1195 union in_addr_union a
;
1204 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1208 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &prefixlen
);
1210 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1211 "PREF64 prefix is invalid, ignoring assignment: %s", rvalue
);
1215 if (!IN_SET(prefixlen
, 96, 64, 56, 48, 40, 32)) {
1216 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1217 "PREF64 prefixlen is invalid, ignoring assignment: %s", rvalue
);
1221 (void) in6_addr_mask(&a
.in6
, prefixlen
);
1223 p
->prefixlen
= prefixlen
;
1229 int config_parse_pref64_prefix_lifetime(
1231 const char *filename
,
1233 const char *section
,
1234 unsigned section_line
,
1241 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1242 Network
*network
= ASSERT_PTR(userdata
);
1251 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1255 r
= parse_sec(rvalue
, &usec
);
1257 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1258 "PREF64 lifetime is invalid, ignoring assignment: %s", rvalue
);
1262 if (usec
== USEC_INFINITY
|| DIV_ROUND_UP(usec
, 8 * USEC_PER_SEC
) >= UINT64_C(1) << 13) {
1263 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1264 "PREF64 lifetime is too long, ignoring assignment: %s", rvalue
);
1274 int config_parse_radv_dns(
1276 const char *filename
,
1278 const char *section
,
1279 unsigned section_line
,
1293 if (isempty(rvalue
)) {
1294 n
->n_router_dns
= 0;
1295 n
->router_dns
= mfree(n
->router_dns
);
1299 for (const char *p
= rvalue
;;) {
1300 _cleanup_free_
char *w
= NULL
;
1301 union in_addr_union a
;
1303 r
= extract_first_word(&p
, &w
, NULL
, 0);
1307 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1308 "Failed to extract word, ignoring: %s", rvalue
);
1314 if (streq(w
, "_link_local"))
1317 r
= in_addr_from_string(AF_INET6
, w
, &a
);
1319 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1320 "Failed to parse DNS server address, ignoring: %s", w
);
1324 if (in_addr_is_null(AF_INET6
, &a
)) {
1325 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1326 "DNS server address is null, ignoring: %s", w
);
1332 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
1336 m
[n
->n_router_dns
++] = a
.in6
;
1341 int config_parse_radv_search_domains(
1343 const char *filename
,
1345 const char *section
,
1346 unsigned section_line
,
1360 if (isempty(rvalue
)) {
1361 n
->router_search_domains
= ordered_set_free(n
->router_search_domains
);
1365 for (const char *p
= rvalue
;;) {
1366 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
1368 r
= extract_first_word(&p
, &w
, NULL
, 0);
1372 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1373 "Failed to extract word, ignoring: %s", rvalue
);
1379 r
= dns_name_apply_idna(w
, &idna
);
1381 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1382 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
1385 /* transfer ownership to simplify subsequent operations */
1388 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops_free
);
1392 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
1398 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
1399 [RADV_PREFIX_DELEGATION_NONE
] = "no",
1400 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
1401 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
1402 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
1405 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
1406 radv_prefix_delegation
,
1407 RADVPrefixDelegation
,
1408 RADV_PREFIX_DELEGATION_BOTH
);
1410 int config_parse_router_prefix_delegation(
1412 const char *filename
,
1414 const char *section
,
1415 unsigned section_line
,
1422 RADVPrefixDelegation val
, *ra
= ASSERT_PTR(data
);
1429 if (streq(lvalue
, "IPv6SendRA")) {
1430 r
= parse_boolean(rvalue
);
1432 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1433 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1437 /* When IPv6SendRA= is enabled, only static prefixes are sent by default, and users
1438 * need to explicitly enable DHCPv6PrefixDelegation=. */
1439 *ra
= r
? RADV_PREFIX_DELEGATION_STATIC
: RADV_PREFIX_DELEGATION_NONE
;
1443 /* For backward compatibility */
1444 val
= radv_prefix_delegation_from_string(rvalue
);
1446 log_syntax(unit
, LOG_WARNING
, filename
, line
, val
,
1447 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1455 int config_parse_router_lifetime(
1457 const char *filename
,
1459 const char *section
,
1460 unsigned section_line
,
1467 usec_t usec
, *lifetime
= ASSERT_PTR(data
);
1475 if (isempty(rvalue
)) {
1476 *lifetime
= RADV_DEFAULT_ROUTER_LIFETIME_USEC
;
1480 r
= parse_sec(rvalue
, &usec
);
1482 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1483 "Failed to parse router lifetime, ignoring assignment: %s", rvalue
);
1487 if (usec
< RADV_MIN_ROUTER_LIFETIME_USEC
) {
1488 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1489 "Router lifetime %s is too short, using %s.",
1490 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1491 FORMAT_TIMESPAN(RADV_MIN_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1492 usec
= RADV_MIN_ROUTER_LIFETIME_USEC
;
1493 } else if (usec
> RADV_MAX_ROUTER_LIFETIME_USEC
) {
1494 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1495 "Router lifetime %s is too large, using %s.",
1496 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1497 FORMAT_TIMESPAN(RADV_MAX_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1498 usec
= RADV_MAX_ROUTER_LIFETIME_USEC
;
1506 int config_parse_router_uint32_msec_usec(
1508 const char *filename
,
1510 const char *section
,
1511 unsigned section_line
,
1518 usec_t usec
, *router_usec
= ASSERT_PTR(data
);
1526 if (isempty(rvalue
)) {
1531 r
= parse_sec(rvalue
, &usec
);
1533 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1534 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1538 if (usec
!= USEC_INFINITY
&&
1539 usec
> RADV_MAX_UINT32_MSEC_USEC
) {
1540 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1541 "Invalid [%s] %s=, ignoring assignment: %s", section
, lvalue
, rvalue
);
1545 *router_usec
= usec
;
1549 int config_parse_router_preference(
1551 const char *filename
,
1553 const char *section
,
1554 unsigned section_line
,
1561 Network
*network
= userdata
;
1569 if (streq(rvalue
, "high"))
1570 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
1571 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
1572 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
1573 else if (streq(rvalue
, "low"))
1574 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
1576 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1577 "Invalid router preference, ignoring assignment: %s", rvalue
);
1582 int config_parse_router_home_agent_lifetime(
1584 const char *filename
,
1586 const char *section
,
1587 unsigned section_line
,
1594 usec_t usec
, *home_agent_lifetime_usec
= ASSERT_PTR(data
);
1602 if (isempty(rvalue
)) {
1603 *home_agent_lifetime_usec
= 0;
1607 r
= parse_sec(rvalue
, &usec
);
1609 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1610 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1614 if (!timestamp_is_set(usec
) ||
1615 usec
> RADV_HOME_AGENT_MAX_LIFETIME_USEC
) {
1616 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1617 "Invalid [%s] %s=, ignoring assignment: %s", section
, lvalue
, rvalue
);
1621 *home_agent_lifetime_usec
= usec
;