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-address.h"
11 #include "networkd-manager.h"
12 #include "networkd-radv.h"
13 #include "parse-util.h"
15 #include "string-util.h"
16 #include "string-table.h"
19 void prefix_free(Prefix
*prefix
) {
23 if (prefix
->network
) {
24 LIST_REMOVE(prefixes
, prefix
->network
->static_prefixes
, prefix
);
25 assert(prefix
->network
->n_static_prefixes
> 0);
26 prefix
->network
->n_static_prefixes
--;
29 hashmap_remove(prefix
->network
->prefixes_by_section
,
33 network_config_section_free(prefix
->section
);
34 sd_radv_prefix_unref(prefix
->radv_prefix
);
39 static int prefix_new(Prefix
**ret
) {
40 _cleanup_(prefix_freep
) Prefix
*prefix
= NULL
;
42 prefix
= new0(Prefix
, 1);
46 if (sd_radv_prefix_new(&prefix
->radv_prefix
) < 0)
49 *ret
= TAKE_PTR(prefix
);
54 static int prefix_new_static(Network
*network
, const char *filename
,
55 unsigned section_line
, Prefix
**ret
) {
56 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
57 _cleanup_(prefix_freep
) Prefix
*prefix
= NULL
;
62 assert(!!filename
== (section_line
> 0));
65 r
= network_config_section_new(filename
, section_line
, &n
);
70 prefix
= hashmap_get(network
->prefixes_by_section
, n
);
72 *ret
= TAKE_PTR(prefix
);
79 r
= prefix_new(&prefix
);
83 prefix
->network
= network
;
84 LIST_APPEND(prefixes
, network
->static_prefixes
, prefix
);
85 network
->n_static_prefixes
++;
88 prefix
->section
= TAKE_PTR(n
);
90 r
= hashmap_ensure_allocated(&network
->prefixes_by_section
, &network_config_hash_ops
);
94 r
= hashmap_put(network
->prefixes_by_section
, prefix
->section
, prefix
);
99 *ret
= TAKE_PTR(prefix
);
104 static int route_prefix_new(RoutePrefix
**ret
) {
105 _cleanup_(route_prefix_freep
) RoutePrefix
*prefix
= NULL
;
107 prefix
= new0(RoutePrefix
, 1);
111 if (sd_radv_route_prefix_new(&prefix
->radv_route_prefix
) < 0)
114 *ret
= TAKE_PTR(prefix
);
119 void route_prefix_free(RoutePrefix
*prefix
) {
123 if (prefix
->network
) {
124 LIST_REMOVE(route_prefixes
, prefix
->network
->static_route_prefixes
, prefix
);
125 assert(prefix
->network
->n_static_route_prefixes
> 0);
126 prefix
->network
->n_static_route_prefixes
--;
129 hashmap_remove(prefix
->network
->route_prefixes_by_section
,
133 network_config_section_free(prefix
->section
);
134 sd_radv_route_prefix_unref(prefix
->radv_route_prefix
);
139 static int route_prefix_new_static(Network
*network
, const char *filename
,
140 unsigned section_line
, RoutePrefix
**ret
) {
141 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
142 _cleanup_(route_prefix_freep
) RoutePrefix
*prefix
= NULL
;
147 assert(!!filename
== (section_line
> 0));
150 r
= network_config_section_new(filename
, section_line
, &n
);
155 prefix
= hashmap_get(network
->route_prefixes_by_section
, n
);
157 *ret
= TAKE_PTR(prefix
);
164 r
= route_prefix_new(&prefix
);
168 prefix
->network
= network
;
169 LIST_APPEND(route_prefixes
, network
->static_route_prefixes
, prefix
);
170 network
->n_static_route_prefixes
++;
173 prefix
->section
= TAKE_PTR(n
);
175 r
= hashmap_ensure_allocated(&network
->route_prefixes_by_section
, &network_config_hash_ops
);
179 r
= hashmap_put(network
->route_prefixes_by_section
, prefix
->section
, prefix
);
184 *ret
= TAKE_PTR(prefix
);
189 int config_parse_prefix(const char *unit
,
190 const char *filename
,
193 unsigned section_line
,
200 Network
*network
= userdata
;
201 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
202 uint8_t prefixlen
= 64;
203 union in_addr_union in6addr
;
212 r
= prefix_new_static(network
, filename
, section_line
, &p
);
216 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &in6addr
, &prefixlen
);
218 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix is invalid, ignoring assignment: %s", rvalue
);
222 if (sd_radv_prefix_set_prefix(p
->radv_prefix
, &in6addr
.in6
, prefixlen
) < 0)
223 return -EADDRNOTAVAIL
;
225 log_syntax(unit
, LOG_INFO
, filename
, line
, r
, "Found prefix %s", rvalue
);
232 int config_parse_prefix_flags(const char *unit
,
233 const char *filename
,
236 unsigned section_line
,
242 Network
*network
= userdata
;
243 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
252 r
= prefix_new_static(network
, filename
, section_line
, &p
);
256 r
= parse_boolean(rvalue
);
258 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address flag, ignoring: %s", rvalue
);
264 if (streq(lvalue
, "OnLink"))
265 r
= sd_radv_prefix_set_onlink(p
->radv_prefix
, val
);
266 else if (streq(lvalue
, "AddressAutoconfiguration"))
267 r
= sd_radv_prefix_set_address_autoconfiguration(p
->radv_prefix
, val
);
276 int config_parse_prefix_lifetime(const char *unit
,
277 const char *filename
,
280 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_ERR
, 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
));
322 int config_parse_route_prefix(const char *unit
,
323 const char *filename
,
326 unsigned section_line
,
333 Network
*network
= userdata
;
334 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
335 uint8_t prefixlen
= 64;
336 union in_addr_union in6addr
;
345 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
349 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &in6addr
, &prefixlen
);
351 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Route prefix is invalid, ignoring assignment: %s", rvalue
);
355 if (sd_radv_prefix_set_route_prefix(p
->radv_route_prefix
, &in6addr
.in6
, prefixlen
) < 0)
356 return -EADDRNOTAVAIL
;
358 log_syntax(unit
, LOG_INFO
, filename
, line
, r
, "Found route prefix %s", rvalue
);
365 int config_parse_route_prefix_lifetime(const char *unit
,
366 const char *filename
,
369 unsigned section_line
,
375 Network
*network
= userdata
;
376 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
386 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
390 r
= parse_sec(rvalue
, &usec
);
392 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
393 "Route lifetime is invalid, ignoring assignment: %s", rvalue
);
397 /* a value of 0xffffffff represents infinity */
398 r
= sd_radv_route_prefix_set_lifetime(p
->radv_route_prefix
, DIV_ROUND_UP(usec
, USEC_PER_SEC
));
407 static int radv_get_ip6dns(Network
*network
, struct in6_addr
**dns
,
409 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
410 size_t i
, n_addresses
= 0, n_allocated
= 0;
416 for (i
= 0; i
< network
->n_dns
; i
++) {
417 union in_addr_union
*addr
;
419 if (network
->dns
[i
].family
!= AF_INET6
)
422 addr
= &network
->dns
[i
].address
;
424 if (in_addr_is_null(AF_INET6
, addr
) ||
425 in_addr_is_link_local(AF_INET6
, addr
) ||
426 in_addr_is_localhost(AF_INET6
, addr
))
429 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
432 addresses
[n_addresses
++] = addr
->in6
;
436 *dns
= TAKE_PTR(addresses
);
438 *n_dns
= n_addresses
;
444 static int radv_set_dns(Link
*link
, Link
*uplink
) {
445 _cleanup_free_
struct in6_addr
*dns
= NULL
;
447 usec_t lifetime_usec
;
450 if (!link
->network
->router_emit_dns
)
453 if (link
->network
->router_dns
) {
454 dns
= newdup(struct in6_addr
, link
->network
->router_dns
,
455 link
->network
->n_router_dns
);
459 n_dns
= link
->network
->n_router_dns
;
460 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
465 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
467 r
= radv_get_ip6dns(link
->network
, &dns
, &n_dns
);
472 if (!uplink
->network
) {
473 log_link_debug(uplink
, "Cannot fetch DNS servers as uplink interface is not managed by us");
477 r
= radv_get_ip6dns(uplink
->network
, &dns
, &n_dns
);
485 return sd_radv_set_rdnss(link
->radv
,
486 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
490 static int radv_set_domains(Link
*link
, Link
*uplink
) {
491 OrderedSet
*search_domains
;
492 usec_t lifetime_usec
;
493 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
495 if (!link
->network
->router_emit_domains
)
498 search_domains
= link
->network
->router_search_domains
;
499 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
504 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
506 search_domains
= link
->network
->search_domains
;
511 if (!uplink
->network
) {
512 log_link_debug(uplink
, "Cannot fetch DNS search domains as uplink interface is not managed by us");
516 search_domains
= uplink
->network
->search_domains
;
524 s
= ordered_set_get_strv(search_domains
);
528 return sd_radv_set_dnssl(link
->radv
,
529 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
534 int radv_emit_dns(Link
*link
) {
538 uplink
= manager_find_uplink(link
->manager
, link
);
540 r
= radv_set_dns(link
, uplink
);
542 log_link_warning_errno(link
, r
, "Could not set RA DNS: %m");
544 r
= radv_set_domains(link
, uplink
);
546 log_link_warning_errno(link
, r
, "Could not set RA Domains: %m");
551 int radv_configure(Link
*link
) {
557 assert(link
->network
);
559 r
= sd_radv_new(&link
->radv
);
563 r
= sd_radv_attach_event(link
->radv
, NULL
, 0);
567 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
571 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
575 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
579 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
583 /* a value of 0xffffffff represents infinity, 0x0 means this host is
585 r
= sd_radv_set_router_lifetime(link
->radv
,
586 DIV_ROUND_UP(link
->network
->router_lifetime_usec
, USEC_PER_SEC
));
590 if (link
->network
->router_lifetime_usec
> 0) {
591 r
= sd_radv_set_preference(link
->radv
,
592 link
->network
->router_preference
);
597 if (IN_SET(link
->network
->router_prefix_delegation
,
598 RADV_PREFIX_DELEGATION_STATIC
,
599 RADV_PREFIX_DELEGATION_BOTH
)) {
601 LIST_FOREACH(prefixes
, p
, link
->network
->static_prefixes
) {
602 r
= sd_radv_add_prefix(link
->radv
, p
->radv_prefix
, false);
606 log_link_warning_errno(link
, r
, "[IPv6Prefix] section configured without Prefix= setting, ignoring section.");
613 LIST_FOREACH(route_prefixes
, q
, link
->network
->static_route_prefixes
) {
614 r
= sd_radv_add_route_prefix(link
->radv
, q
->radv_route_prefix
, false);
623 return radv_emit_dns(link
);
626 int config_parse_radv_dns(
628 const char *filename
,
631 unsigned section_line
,
639 const char *p
= rvalue
;
647 _cleanup_free_
char *w
= NULL
;
648 union in_addr_union a
;
650 r
= extract_first_word(&p
, &w
, NULL
, 0);
654 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
655 "Failed to extract word, ignoring: %s", rvalue
);
661 if (in_addr_from_string(AF_INET6
, w
, &a
) >= 0) {
664 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
668 m
[n
->n_router_dns
++] = a
.in6
;
672 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
673 "Failed to parse DNS server address, ignoring: %s", w
);
679 int config_parse_radv_search_domains(
681 const char *filename
,
684 unsigned section_line
,
692 const char *p
= rvalue
;
700 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
702 r
= extract_first_word(&p
, &w
, NULL
, 0);
706 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
707 "Failed to extract word, ignoring: %s", rvalue
);
713 r
= dns_name_apply_idna(w
, &idna
);
715 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
716 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
719 /* transfer ownership to simplify subsequent operations */
722 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops
);
726 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
734 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
735 [RADV_PREFIX_DELEGATION_NONE
] = "no",
736 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
737 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
738 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
741 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
742 radv_prefix_delegation
,
743 RADVPrefixDelegation
,
744 RADV_PREFIX_DELEGATION_BOTH
);
746 int config_parse_router_prefix_delegation(
748 const char *filename
,
751 unsigned section_line
,
758 Network
*network
= userdata
;
759 RADVPrefixDelegation d
;
767 d
= radv_prefix_delegation_from_string(rvalue
);
769 log_syntax(unit
, LOG_ERR
, filename
, line
, -EINVAL
, "Invalid router prefix delegation '%s', ignoring assignment.", rvalue
);
773 network
->router_prefix_delegation
= d
;
778 int config_parse_router_preference(const char *unit
,
779 const char *filename
,
782 unsigned section_line
,
788 Network
*network
= userdata
;
796 if (streq(rvalue
, "high"))
797 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
798 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
799 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
800 else if (streq(rvalue
, "low"))
801 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
803 log_syntax(unit
, LOG_ERR
, filename
, line
, -EINVAL
, "Router preference '%s' is invalid, ignoring assignment: %m", rvalue
);