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
) {
249 /* radv_generate_addresses() below requires the prefix length <= 64. */
250 if (p
->prefixlen
> 64)
253 _cleanup_hashmap_free_ Hashmap
*tokens_by_address
= NULL
;
254 r
= radv_generate_addresses(link
, p
->tokens
, &p
->prefix
, p
->prefixlen
, &tokens_by_address
);
260 HASHMAP_FOREACH_KEY(token
, a
, tokens_by_address
) {
261 _cleanup_(address_unrefp
) 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
;
272 address
->token
= ipv6_token_ref(token
);
274 r
= link_request_static_address(link
, address
);
283 static int radv_set_prefix(Link
*link
, Prefix
*prefix
) {
284 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
291 r
= sd_radv_prefix_new(&p
);
295 r
= sd_radv_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
299 r
= sd_radv_prefix_set_preferred_lifetime(p
, prefix
->preferred_lifetime
, USEC_INFINITY
);
303 r
= sd_radv_prefix_set_valid_lifetime(p
, prefix
->valid_lifetime
, USEC_INFINITY
);
307 r
= sd_radv_prefix_set_onlink(p
, prefix
->onlink
);
311 r
= sd_radv_prefix_set_address_autoconfiguration(p
, prefix
->address_auto_configuration
);
315 return sd_radv_add_prefix(link
->radv
, p
);
318 static int radv_set_route_prefix(Link
*link
, RoutePrefix
*prefix
) {
319 _cleanup_(sd_radv_route_prefix_unrefp
) sd_radv_route_prefix
*p
= NULL
;
326 r
= sd_radv_route_prefix_new(&p
);
330 r
= sd_radv_route_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
);
334 r
= sd_radv_route_prefix_set_lifetime(p
, prefix
->lifetime
, USEC_INFINITY
);
338 return sd_radv_add_route_prefix(link
->radv
, p
);
341 static int radv_set_pref64_prefix(Link
*link
, pref64Prefix
*prefix
) {
342 _cleanup_(sd_radv_pref64_prefix_unrefp
) sd_radv_pref64_prefix
*p
= NULL
;
349 r
= sd_radv_pref64_prefix_new(&p
);
353 r
= sd_radv_pref64_prefix_set_prefix(p
, &prefix
->prefix
, prefix
->prefixlen
, prefix
->lifetime
);
357 return sd_radv_add_pref64_prefix(link
->radv
, p
);
360 static int network_get_ipv6_dns(Network
*network
, struct in6_addr
**ret_addresses
, size_t *ret_size
) {
361 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
362 size_t n_addresses
= 0;
365 assert(ret_addresses
);
368 for (size_t i
= 0; i
< network
->n_dns
; i
++) {
369 union in_addr_union
*addr
;
371 if (network
->dns
[i
]->family
!= AF_INET6
)
374 addr
= &network
->dns
[i
]->address
;
376 if (in_addr_is_null(AF_INET6
, addr
) ||
377 in_addr_is_link_local(AF_INET6
, addr
) ||
378 in_addr_is_localhost(AF_INET6
, addr
))
381 if (!GREEDY_REALLOC(addresses
, n_addresses
+ 1))
384 addresses
[n_addresses
++] = addr
->in6
;
387 *ret_addresses
= TAKE_PTR(addresses
);
388 *ret_size
= n_addresses
;
393 static int radv_set_dns(Link
*link
, Link
*uplink
) {
394 _cleanup_free_
struct in6_addr
*dns
= NULL
;
398 if (!link
->network
->router_emit_dns
)
401 if (link
->network
->router_dns
) {
404 dns
= new(struct in6_addr
, link
->network
->n_router_dns
);
409 for (size_t i
= 0; i
< link
->network
->n_router_dns
; i
++)
410 if (in6_addr_is_null(&link
->network
->router_dns
[i
])) {
411 if (in6_addr_is_set(&link
->ipv6ll_address
))
412 *(p
++) = link
->ipv6ll_address
;
414 *(p
++) = link
->network
->router_dns
[i
];
421 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
426 assert(uplink
->network
);
428 r
= network_get_ipv6_dns(uplink
->network
, &dns
, &n_dns
);
436 return sd_radv_set_rdnss(link
->radv
,
437 link
->network
->router_dns_lifetime_usec
,
441 static int radv_set_domains(Link
*link
, Link
*uplink
) {
442 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
443 OrderedSet
*search_domains
;
445 if (!link
->network
->router_emit_domains
)
448 search_domains
= link
->network
->router_search_domains
;
453 search_domains
= link
->network
->search_domains
;
458 assert(uplink
->network
);
460 search_domains
= uplink
->network
->search_domains
;
468 s
= ordered_set_get_strv(search_domains
);
472 return sd_radv_set_dnssl(link
->radv
,
473 link
->network
->router_dns_lifetime_usec
,
478 static int radv_find_uplink(Link
*link
, Link
**ret
) {
483 if (link
->network
->router_uplink_name
)
484 return link_get_by_name(link
->manager
, link
->network
->router_uplink_name
, ret
);
486 if (link
->network
->router_uplink_index
> 0)
487 return link_get_by_index(link
->manager
, link
->network
->router_uplink_index
, ret
);
489 if (link
->network
->router_uplink_index
== UPLINK_INDEX_AUTO
) {
490 if (link_dhcp_pd_is_enabled(link
))
491 r
= dhcp_pd_find_uplink(link
, ret
); /* When DHCP-PD is enabled, use its uplink. */
493 r
= manager_find_uplink(link
->manager
, AF_INET6
, link
, ret
);
495 /* It is not necessary to propagate error in automatic selection. */
504 static int radv_configure(Link
*link
) {
512 assert(link
->network
);
517 r
= sd_radv_new(&link
->radv
);
521 r
= sd_radv_attach_event(link
->radv
, link
->manager
->event
, 0);
525 if (link
->hw_addr
.length
== ETH_ALEN
) {
526 r
= sd_radv_set_mac(link
->radv
, &link
->hw_addr
.ether
);
531 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
535 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
539 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
543 r
= sd_radv_set_router_lifetime(link
->radv
, link
->network
->router_lifetime_usec
);
547 r
= sd_radv_set_hop_limit(link
->radv
, link
->network
->router_hop_limit
);
551 if (link
->network
->router_lifetime_usec
> 0) {
552 r
= sd_radv_set_preference(link
->radv
, link
->network
->router_preference
);
557 if (link
->network
->router_retransmit_usec
> 0) {
558 r
= sd_radv_set_retransmit(link
->radv
, link
->network
->router_retransmit_usec
);
563 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
564 r
= radv_set_prefix(link
, p
);
565 if (r
< 0 && r
!= -EEXIST
)
569 HASHMAP_FOREACH(q
, link
->network
->route_prefixes_by_section
) {
570 r
= radv_set_route_prefix(link
, q
);
571 if (r
< 0 && r
!= -EEXIST
)
575 HASHMAP_FOREACH(n
, link
->network
->pref64_prefixes_by_section
) {
576 r
= radv_set_pref64_prefix(link
, n
);
577 if (r
< 0 && r
!= -EEXIST
)
581 (void) radv_find_uplink(link
, &uplink
);
583 r
= radv_set_dns(link
, uplink
);
585 return log_link_debug_errno(link
, r
, "Could not set RA DNS: %m");
587 r
= radv_set_domains(link
, uplink
);
589 return log_link_debug_errno(link
, r
, "Could not set RA Domains: %m");
591 r
= sd_radv_set_home_agent_information(link
->radv
, link
->network
->router_home_agent_information
);
595 r
= sd_radv_set_home_agent_preference(link
->radv
, link
->network
->router_home_agent_preference
);
599 r
= sd_radv_set_home_agent_lifetime(link
->radv
, link
->network
->home_agent_lifetime_usec
);
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
->preferred_lifetime
> p
->valid_lifetime
)
821 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
822 "%s: The preferred lifetime %s is longer than the valid lifetime %s. "
823 "Ignoring [IPv6Prefix] section from line %u.",
824 p
->section
->filename
,
825 FORMAT_TIMESPAN(p
->preferred_lifetime
, USEC_PER_SEC
),
826 FORMAT_TIMESPAN(p
->valid_lifetime
, USEC_PER_SEC
),
832 void network_drop_invalid_prefixes(Network
*network
) {
837 HASHMAP_FOREACH(p
, network
->prefixes_by_section
)
838 if (prefix_section_verify(p
) < 0)
842 static int route_prefix_section_verify(RoutePrefix
*p
) {
843 if (section_is_invalid(p
->section
))
846 if (p
->prefixlen
> 128)
847 return log_warning_errno(SYNTHETIC_ERRNO(EINVAL
),
848 "%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. "
849 "Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.",
850 p
->section
->filename
, p
->prefixlen
, p
->section
->line
);
855 void network_drop_invalid_route_prefixes(Network
*network
) {
860 HASHMAP_FOREACH(p
, network
->route_prefixes_by_section
)
861 if (route_prefix_section_verify(p
) < 0)
862 route_prefix_free(p
);
865 void network_drop_invalid_pref64_prefixes(Network
*network
) {
870 HASHMAP_FOREACH(p
, network
->pref64_prefixes_by_section
)
871 if (section_is_invalid(p
->section
))
872 pref64_prefix_free(p
);
875 int config_parse_prefix(
877 const char *filename
,
880 unsigned section_line
,
887 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
888 Network
*network
= ASSERT_PTR(userdata
);
889 union in_addr_union a
;
897 r
= prefix_new_static(network
, filename
, section_line
, &p
);
901 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
903 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
904 "Prefix is invalid, ignoring assignment: %s", rvalue
);
908 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
915 int config_parse_prefix_boolean(
917 const char *filename
,
920 unsigned section_line
,
927 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
928 Network
*network
= ASSERT_PTR(userdata
);
936 r
= prefix_new_static(network
, filename
, section_line
, &p
);
940 r
= parse_boolean(rvalue
);
942 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
943 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
947 if (streq(lvalue
, "OnLink"))
949 else if (streq(lvalue
, "AddressAutoconfiguration"))
950 p
->address_auto_configuration
= r
;
951 else if (streq(lvalue
, "Assign"))
954 assert_not_reached();
960 int config_parse_prefix_lifetime(
962 const char *filename
,
965 unsigned section_line
,
972 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
973 Network
*network
= ASSERT_PTR(userdata
);
982 r
= prefix_new_static(network
, filename
, section_line
, &p
);
986 r
= parse_sec(rvalue
, &usec
);
988 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
989 "Lifetime is invalid, ignoring assignment: %s", rvalue
);
993 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
994 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
995 "Lifetime is too long, ignoring assignment: %s", rvalue
);
999 if (streq(lvalue
, "PreferredLifetimeSec"))
1000 p
->preferred_lifetime
= usec
;
1001 else if (streq(lvalue
, "ValidLifetimeSec"))
1002 p
->valid_lifetime
= usec
;
1004 assert_not_reached();
1010 int config_parse_prefix_metric(
1012 const char *filename
,
1014 const char *section
,
1015 unsigned section_line
,
1022 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1023 Network
*network
= ASSERT_PTR(userdata
);
1031 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1035 r
= safe_atou32(rvalue
, &p
->route_metric
);
1037 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1038 "Failed to parse %s=, ignoring assignment: %s",
1047 int config_parse_prefix_token(
1049 const char *filename
,
1051 const char *section
,
1052 unsigned section_line
,
1059 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
1060 Network
*network
= ASSERT_PTR(userdata
);
1068 r
= prefix_new_static(network
, filename
, section_line
, &p
);
1072 r
= config_parse_address_generation_type(unit
, filename
, line
, section
, section_line
,
1073 lvalue
, ltype
, rvalue
, &p
->tokens
, userdata
);
1081 int config_parse_route_prefix(
1083 const char *filename
,
1085 const char *section
,
1086 unsigned section_line
,
1093 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1094 Network
*network
= ASSERT_PTR(userdata
);
1095 union in_addr_union a
;
1103 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1107 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &p
->prefixlen
);
1109 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1110 "Route prefix is invalid, ignoring assignment: %s", rvalue
);
1114 (void) in6_addr_mask(&a
.in6
, p
->prefixlen
);
1121 int config_parse_route_prefix_lifetime(
1123 const char *filename
,
1125 const char *section
,
1126 unsigned section_line
,
1133 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
1134 Network
*network
= ASSERT_PTR(userdata
);
1143 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
1147 r
= parse_sec(rvalue
, &usec
);
1149 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1150 "Route lifetime is invalid, ignoring assignment: %s", rvalue
);
1154 if (usec
!= USEC_INFINITY
&& DIV_ROUND_UP(usec
, USEC_PER_SEC
) >= UINT32_MAX
) {
1155 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1156 "Lifetime is too long, ignoring assignment: %s", rvalue
);
1166 int config_parse_pref64_prefix(
1168 const char *filename
,
1170 const char *section
,
1171 unsigned section_line
,
1178 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1179 Network
*network
= ASSERT_PTR(userdata
);
1180 union in_addr_union a
;
1189 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1193 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &a
, &prefixlen
);
1195 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1196 "PREF64 prefix is invalid, ignoring assignment: %s", rvalue
);
1200 if (!IN_SET(prefixlen
, 96, 64, 56, 48, 40, 32)) {
1201 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1202 "PREF64 prefixlen is invalid, ignoring assignment: %s", rvalue
);
1206 (void) in6_addr_mask(&a
.in6
,prefixlen
);
1208 p
->prefixlen
= prefixlen
;
1214 int config_parse_pref64_prefix_lifetime(
1216 const char *filename
,
1218 const char *section
,
1219 unsigned section_line
,
1226 _cleanup_(pref64_prefix_free_or_set_invalidp
) pref64Prefix
*p
= NULL
;
1227 Network
*network
= ASSERT_PTR(userdata
);
1236 r
= pref64_prefix_new_static(network
, filename
, section_line
, &p
);
1240 r
= parse_sec(rvalue
, &usec
);
1242 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1243 "PREF64 lifetime is invalid, ignoring assignment: %s", rvalue
);
1247 if (usec
== USEC_INFINITY
|| DIV_ROUND_UP(usec
, 8 * USEC_PER_SEC
) >= UINT64_C(1) << 13) {
1248 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1249 "PREF64 lifetime is too long, ignoring assignment: %s", rvalue
);
1259 int config_parse_radv_dns(
1261 const char *filename
,
1263 const char *section
,
1264 unsigned section_line
,
1278 if (isempty(rvalue
)) {
1279 n
->n_router_dns
= 0;
1280 n
->router_dns
= mfree(n
->router_dns
);
1284 for (const char *p
= rvalue
;;) {
1285 _cleanup_free_
char *w
= NULL
;
1286 union in_addr_union a
;
1288 r
= extract_first_word(&p
, &w
, NULL
, 0);
1292 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1293 "Failed to extract word, ignoring: %s", rvalue
);
1299 if (streq(w
, "_link_local"))
1302 r
= in_addr_from_string(AF_INET6
, w
, &a
);
1304 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1305 "Failed to parse DNS server address, ignoring: %s", w
);
1309 if (in_addr_is_null(AF_INET6
, &a
)) {
1310 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1311 "DNS server address is null, ignoring: %s", w
);
1317 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
1321 m
[n
->n_router_dns
++] = a
.in6
;
1326 int config_parse_radv_search_domains(
1328 const char *filename
,
1330 const char *section
,
1331 unsigned section_line
,
1345 if (isempty(rvalue
)) {
1346 n
->router_search_domains
= ordered_set_free(n
->router_search_domains
);
1350 for (const char *p
= rvalue
;;) {
1351 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
1353 r
= extract_first_word(&p
, &w
, NULL
, 0);
1357 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1358 "Failed to extract word, ignoring: %s", rvalue
);
1364 r
= dns_name_apply_idna(w
, &idna
);
1366 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1367 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
1370 /* transfer ownership to simplify subsequent operations */
1373 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops_free
);
1377 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
1383 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
1384 [RADV_PREFIX_DELEGATION_NONE
] = "no",
1385 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
1386 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
1387 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
1390 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
1391 radv_prefix_delegation
,
1392 RADVPrefixDelegation
,
1393 RADV_PREFIX_DELEGATION_BOTH
);
1395 int config_parse_router_prefix_delegation(
1397 const char *filename
,
1399 const char *section
,
1400 unsigned section_line
,
1407 RADVPrefixDelegation val
, *ra
= ASSERT_PTR(data
);
1414 if (streq(lvalue
, "IPv6SendRA")) {
1415 r
= parse_boolean(rvalue
);
1417 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1418 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1422 /* When IPv6SendRA= is enabled, only static prefixes are sent by default, and users
1423 * need to explicitly enable DHCPv6PrefixDelegation=. */
1424 *ra
= r
? RADV_PREFIX_DELEGATION_STATIC
: RADV_PREFIX_DELEGATION_NONE
;
1428 /* For backward compatibility */
1429 val
= radv_prefix_delegation_from_string(rvalue
);
1431 log_syntax(unit
, LOG_WARNING
, filename
, line
, val
,
1432 "Invalid %s= setting, ignoring assignment: %s", lvalue
, rvalue
);
1440 int config_parse_router_lifetime(
1442 const char *filename
,
1444 const char *section
,
1445 unsigned section_line
,
1452 usec_t usec
, *lifetime
= ASSERT_PTR(data
);
1460 if (isempty(rvalue
)) {
1461 *lifetime
= RADV_DEFAULT_ROUTER_LIFETIME_USEC
;
1465 r
= parse_sec(rvalue
, &usec
);
1467 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1468 "Failed to parse router lifetime, ignoring assignment: %s", rvalue
);
1472 if (usec
< RADV_MIN_ROUTER_LIFETIME_USEC
) {
1473 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1474 "Router lifetime %s is too short, using %s.",
1475 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1476 FORMAT_TIMESPAN(RADV_MIN_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1477 usec
= RADV_MIN_ROUTER_LIFETIME_USEC
;
1478 } else if (usec
> RADV_MAX_ROUTER_LIFETIME_USEC
) {
1479 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1480 "Router lifetime %s is too large, using %s.",
1481 FORMAT_TIMESPAN(usec
, USEC_PER_SEC
),
1482 FORMAT_TIMESPAN(RADV_MAX_ROUTER_LIFETIME_USEC
, USEC_PER_SEC
));
1483 usec
= RADV_MAX_ROUTER_LIFETIME_USEC
;
1491 int config_parse_router_retransmit(
1493 const char *filename
,
1495 const char *section
,
1496 unsigned section_line
,
1503 usec_t usec
, *router_retransmit_usec
= ASSERT_PTR(data
);
1511 if (isempty(rvalue
)) {
1512 *router_retransmit_usec
= 0;
1516 r
= parse_sec(rvalue
, &usec
);
1518 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1519 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1523 if (usec
!= USEC_INFINITY
&&
1524 usec
> RADV_MAX_RETRANSMIT_USEC
) {
1525 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1526 "Invalid [%s] %s=, ignoring assignment: %s", section
, lvalue
, rvalue
);
1530 *router_retransmit_usec
= usec
;
1534 int config_parse_router_preference(
1536 const char *filename
,
1538 const char *section
,
1539 unsigned section_line
,
1546 Network
*network
= userdata
;
1554 if (streq(rvalue
, "high"))
1555 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
1556 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
1557 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
1558 else if (streq(rvalue
, "low"))
1559 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
1561 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1562 "Invalid router preference, ignoring assignment: %s", rvalue
);
1567 int config_parse_router_home_agent_lifetime(
1569 const char *filename
,
1571 const char *section
,
1572 unsigned section_line
,
1579 usec_t usec
, *home_agent_lifetime_usec
= ASSERT_PTR(data
);
1587 if (isempty(rvalue
)) {
1588 *home_agent_lifetime_usec
= 0;
1592 r
= parse_sec(rvalue
, &usec
);
1594 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1595 "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
1599 if (!timestamp_is_set(usec
) ||
1600 usec
> RADV_HOME_AGENT_MAX_LIFETIME_USEC
) {
1601 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1602 "Invalid [%s] %s=, ignoring assignment: %s", section
, lvalue
, rvalue
);
1606 *home_agent_lifetime_usec
= usec
;