1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2017 Intel Corporation. All rights reserved.
6 #include <netinet/icmp6.h>
9 #include "dns-domain.h"
10 #include "networkd-link.h"
11 #include "networkd-manager.h"
12 #include "networkd-network.h"
13 #include "networkd-radv.h"
14 #include "parse-util.h"
15 #include "string-util.h"
16 #include "string-table.h"
19 Prefix
*prefix_free(Prefix
*prefix
) {
23 if (prefix
->network
) {
24 assert(prefix
->section
);
25 hashmap_remove(prefix
->network
->prefixes_by_section
, prefix
->section
);
28 network_config_section_free(prefix
->section
);
29 sd_radv_prefix_unref(prefix
->radv_prefix
);
34 DEFINE_NETWORK_SECTION_FUNCTIONS(Prefix
, prefix_free
);
36 static int prefix_new(Prefix
**ret
) {
37 _cleanup_(prefix_freep
) Prefix
*prefix
= NULL
;
39 prefix
= new0(Prefix
, 1);
43 if (sd_radv_prefix_new(&prefix
->radv_prefix
) < 0)
46 *ret
= TAKE_PTR(prefix
);
51 static int prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, Prefix
**ret
) {
52 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
53 _cleanup_(prefix_freep
) Prefix
*prefix
= NULL
;
59 assert(section_line
> 0);
61 r
= network_config_section_new(filename
, section_line
, &n
);
65 prefix
= hashmap_get(network
->prefixes_by_section
, n
);
67 *ret
= TAKE_PTR(prefix
);
71 r
= prefix_new(&prefix
);
75 prefix
->network
= network
;
76 prefix
->section
= TAKE_PTR(n
);
78 r
= hashmap_ensure_allocated(&network
->prefixes_by_section
, &network_config_hash_ops
);
82 r
= hashmap_put(network
->prefixes_by_section
, prefix
->section
, prefix
);
86 *ret
= TAKE_PTR(prefix
);
91 RoutePrefix
*route_prefix_free(RoutePrefix
*prefix
) {
95 if (prefix
->network
) {
96 assert(prefix
->section
);
97 hashmap_remove(prefix
->network
->route_prefixes_by_section
, prefix
->section
);
100 network_config_section_free(prefix
->section
);
101 sd_radv_route_prefix_unref(prefix
->radv_route_prefix
);
103 return mfree(prefix
);
106 DEFINE_NETWORK_SECTION_FUNCTIONS(RoutePrefix
, route_prefix_free
);
108 static int route_prefix_new(RoutePrefix
**ret
) {
109 _cleanup_(route_prefix_freep
) RoutePrefix
*prefix
= NULL
;
111 prefix
= new0(RoutePrefix
, 1);
115 if (sd_radv_route_prefix_new(&prefix
->radv_route_prefix
) < 0)
118 *ret
= TAKE_PTR(prefix
);
123 static int route_prefix_new_static(Network
*network
, const char *filename
, unsigned section_line
, RoutePrefix
**ret
) {
124 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
125 _cleanup_(route_prefix_freep
) RoutePrefix
*prefix
= NULL
;
131 assert(section_line
> 0);
133 r
= network_config_section_new(filename
, section_line
, &n
);
137 prefix
= hashmap_get(network
->route_prefixes_by_section
, n
);
139 *ret
= TAKE_PTR(prefix
);
143 r
= route_prefix_new(&prefix
);
147 prefix
->network
= network
;
148 prefix
->section
= TAKE_PTR(n
);
150 r
= hashmap_ensure_allocated(&network
->route_prefixes_by_section
, &network_config_hash_ops
);
154 r
= hashmap_put(network
->route_prefixes_by_section
, prefix
->section
, prefix
);
158 *ret
= TAKE_PTR(prefix
);
163 void network_verify_prefixes(Network
*network
) {
168 HASHMAP_FOREACH(prefix
, network
->prefixes_by_section
)
169 if (section_is_invalid(prefix
->section
))
173 void network_verify_route_prefixes(Network
*network
) {
178 HASHMAP_FOREACH(prefix
, network
->route_prefixes_by_section
)
179 if (section_is_invalid(prefix
->section
))
180 route_prefix_free(prefix
);
183 int config_parse_prefix(
185 const char *filename
,
188 unsigned section_line
,
195 Network
*network
= userdata
;
196 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
197 uint8_t prefixlen
= 64;
198 union in_addr_union in6addr
;
207 r
= prefix_new_static(network
, filename
, section_line
, &p
);
211 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &in6addr
, &prefixlen
);
213 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Prefix is invalid, ignoring assignment: %s", rvalue
);
217 r
= sd_radv_prefix_set_prefix(p
->radv_prefix
, &in6addr
.in6
, prefixlen
);
219 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to set radv prefix, ignoring assignment: %s", rvalue
);
228 int config_parse_prefix_flags(
230 const char *filename
,
233 unsigned section_line
,
240 Network
*network
= userdata
;
241 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
250 r
= prefix_new_static(network
, filename
, section_line
, &p
);
254 r
= parse_boolean(rvalue
);
256 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
260 if (streq(lvalue
, "OnLink"))
261 r
= sd_radv_prefix_set_onlink(p
->radv_prefix
, r
);
262 else if (streq(lvalue
, "AddressAutoconfiguration"))
263 r
= sd_radv_prefix_set_address_autoconfiguration(p
->radv_prefix
, r
);
265 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to set %s=, ignoring assignment: %m", lvalue
);
274 int config_parse_prefix_lifetime(
276 const char *filename
,
279 unsigned section_line
,
286 Network
*network
= userdata
;
287 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
297 r
= prefix_new_static(network
, filename
, section_line
, &p
);
301 r
= parse_sec(rvalue
, &usec
);
303 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Lifetime is invalid, ignoring assignment: %s", rvalue
);
307 /* a value of 0xffffffff represents infinity */
308 if (streq(lvalue
, "PreferredLifetimeSec"))
309 r
= sd_radv_prefix_set_preferred_lifetime(p
->radv_prefix
,
310 DIV_ROUND_UP(usec
, USEC_PER_SEC
));
311 else if (streq(lvalue
, "ValidLifetimeSec"))
312 r
= sd_radv_prefix_set_valid_lifetime(p
->radv_prefix
,
313 DIV_ROUND_UP(usec
, USEC_PER_SEC
));
315 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to set %s=, ignoring assignment: %m", lvalue
);
324 int config_parse_prefix_assign(
326 const char *filename
,
329 unsigned section_line
,
336 Network
*network
= userdata
;
337 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
346 r
= prefix_new_static(network
, filename
, section_line
, &p
);
350 r
= parse_boolean(rvalue
);
352 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
353 "Failed to parse %s=, ignoring assignment: %s",
364 int config_parse_route_prefix(
366 const char *filename
,
369 unsigned section_line
,
376 Network
*network
= userdata
;
377 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
378 uint8_t prefixlen
= 64;
379 union in_addr_union in6addr
;
388 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
392 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &in6addr
, &prefixlen
);
394 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Route prefix is invalid, ignoring assignment: %s", rvalue
);
398 r
= sd_radv_prefix_set_route_prefix(p
->radv_route_prefix
, &in6addr
.in6
, prefixlen
);
400 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to set route prefix, ignoring assignment: %m");
409 int config_parse_route_prefix_lifetime(
411 const char *filename
,
414 unsigned section_line
,
421 Network
*network
= userdata
;
422 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
432 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
436 r
= parse_sec(rvalue
, &usec
);
438 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
439 "Route lifetime is invalid, ignoring assignment: %s", rvalue
);
443 /* a value of 0xffffffff represents infinity */
444 r
= sd_radv_route_prefix_set_lifetime(p
->radv_route_prefix
, DIV_ROUND_UP(usec
, USEC_PER_SEC
));
446 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
447 "Failed to set route lifetime, ignoring assignment: %m");
456 static int network_get_ipv6_dns(Network
*network
, struct in6_addr
**ret_addresses
, size_t *ret_size
) {
457 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
458 size_t n_addresses
= 0, n_allocated
= 0;
461 assert(ret_addresses
);
464 for (size_t i
= 0; i
< network
->n_dns
; i
++) {
465 union in_addr_union
*addr
;
467 if (network
->dns
[i
]->family
!= AF_INET6
)
470 addr
= &network
->dns
[i
]->address
;
472 if (in_addr_is_null(AF_INET6
, addr
) ||
473 in_addr_is_link_local(AF_INET6
, addr
) ||
474 in_addr_is_localhost(AF_INET6
, addr
))
477 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
480 addresses
[n_addresses
++] = addr
->in6
;
483 *ret_addresses
= TAKE_PTR(addresses
);
484 *ret_size
= n_addresses
;
489 static int radv_set_dns(Link
*link
, Link
*uplink
) {
490 _cleanup_free_
struct in6_addr
*dns
= NULL
;
491 usec_t lifetime_usec
;
495 if (!link
->network
->router_emit_dns
)
498 if (link
->network
->router_dns
) {
501 dns
= new(struct in6_addr
, link
->network
->n_router_dns
);
506 for (size_t i
= 0; i
< link
->network
->n_router_dns
; i
++)
507 if (IN6_IS_ADDR_UNSPECIFIED(&link
->network
->router_dns
[i
])) {
508 if (!IN6_IS_ADDR_UNSPECIFIED(&link
->ipv6ll_address
))
509 *(p
++) = link
->ipv6ll_address
;
511 *(p
++) = link
->network
->router_dns
[i
];
514 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
519 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
521 r
= network_get_ipv6_dns(link
->network
, &dns
, &n_dns
);
526 if (!uplink
->network
) {
527 log_link_debug(uplink
, "Cannot fetch DNS servers as uplink interface is not managed by us");
531 r
= network_get_ipv6_dns(uplink
->network
, &dns
, &n_dns
);
539 return sd_radv_set_rdnss(link
->radv
,
540 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
544 static int radv_set_domains(Link
*link
, Link
*uplink
) {
545 OrderedSet
*search_domains
;
546 usec_t lifetime_usec
;
547 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
549 if (!link
->network
->router_emit_domains
)
552 search_domains
= link
->network
->router_search_domains
;
553 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
558 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
560 search_domains
= link
->network
->search_domains
;
565 if (!uplink
->network
) {
566 log_link_debug(uplink
, "Cannot fetch DNS search domains as uplink interface is not managed by us");
570 search_domains
= uplink
->network
->search_domains
;
578 s
= ordered_set_get_strv(search_domains
);
582 return sd_radv_set_dnssl(link
->radv
,
583 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
588 int radv_emit_dns(Link
*link
) {
592 uplink
= manager_find_uplink(link
->manager
, link
);
594 r
= radv_set_dns(link
, uplink
);
596 log_link_warning_errno(link
, r
, "Could not set RA DNS: %m");
598 r
= radv_set_domains(link
, uplink
);
600 log_link_warning_errno(link
, r
, "Could not set RA Domains: %m");
605 int radv_configure(Link
*link
) {
609 assert(link
->network
);
611 r
= sd_radv_new(&link
->radv
);
615 r
= sd_radv_attach_event(link
->radv
, NULL
, 0);
619 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
623 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
627 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
631 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
635 /* a value of 0xffffffff represents infinity, 0x0 means this host is
637 r
= sd_radv_set_router_lifetime(link
->radv
,
638 DIV_ROUND_UP(link
->network
->router_lifetime_usec
, USEC_PER_SEC
));
642 if (link
->network
->router_lifetime_usec
> 0) {
643 r
= sd_radv_set_preference(link
->radv
,
644 link
->network
->router_preference
);
649 if (link
->network
->router_prefix_delegation
& RADV_PREFIX_DELEGATION_STATIC
) {
653 HASHMAP_FOREACH(p
, link
->network
->prefixes_by_section
) {
654 r
= sd_radv_add_prefix(link
->radv
, p
->radv_prefix
, false);
658 log_link_warning_errno(link
, r
, "[IPv6Prefix] section configured without Prefix= setting, ignoring section.");
665 HASHMAP_FOREACH(q
, link
->network
->route_prefixes_by_section
) {
666 r
= sd_radv_add_route_prefix(link
->radv
, q
->radv_route_prefix
, false);
679 const struct in6_addr
*prefix
,
681 uint32_t lifetime_preferred
,
682 uint32_t lifetime_valid
) {
684 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
692 r
= sd_radv_prefix_new(&p
);
696 r
= sd_radv_prefix_set_prefix(p
, prefix
, prefix_len
);
700 r
= sd_radv_prefix_set_preferred_lifetime(p
, lifetime_preferred
);
704 r
= sd_radv_prefix_set_valid_lifetime(p
, lifetime_valid
);
708 r
= sd_radv_add_prefix(link
->radv
, p
, true);
709 if (r
< 0 && r
!= -EEXIST
)
715 int config_parse_radv_dns(
717 const char *filename
,
720 unsigned section_line
,
734 for (const char *p
= rvalue
;;) {
735 _cleanup_free_
char *w
= NULL
;
736 union in_addr_union a
;
738 r
= extract_first_word(&p
, &w
, NULL
, 0);
742 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
743 "Failed to extract word, ignoring: %s", rvalue
);
749 if (streq(w
, "_link_local"))
752 r
= in_addr_from_string(AF_INET6
, w
, &a
);
754 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
755 "Failed to parse DNS server address, ignoring: %s", w
);
759 if (in_addr_is_null(AF_INET6
, &a
)) {
760 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
761 "DNS server address is null, ignoring: %s", w
);
767 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
771 m
[n
->n_router_dns
++] = a
.in6
;
776 int config_parse_radv_search_domains(
778 const char *filename
,
781 unsigned section_line
,
795 for (const char *p
= rvalue
;;) {
796 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
798 r
= extract_first_word(&p
, &w
, NULL
, 0);
802 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
803 "Failed to extract word, ignoring: %s", rvalue
);
809 r
= dns_name_apply_idna(w
, &idna
);
811 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
812 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
815 /* transfer ownership to simplify subsequent operations */
818 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops
);
822 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
828 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
829 [RADV_PREFIX_DELEGATION_NONE
] = "no",
830 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
831 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
832 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
835 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
836 radv_prefix_delegation
,
837 RADVPrefixDelegation
,
838 RADV_PREFIX_DELEGATION_BOTH
);
840 DEFINE_CONFIG_PARSE_ENUM(
841 config_parse_router_prefix_delegation
,
842 radv_prefix_delegation
,
843 RADVPrefixDelegation
,
844 "Invalid router prefix delegation");
846 int config_parse_router_preference(
848 const char *filename
,
851 unsigned section_line
,
858 Network
*network
= userdata
;
866 if (streq(rvalue
, "high"))
867 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
868 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
869 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
870 else if (streq(rvalue
, "low"))
871 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
873 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
874 "Invalid router preference, ignoring assignment: %s", rvalue
);