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 prefix
->radv_prefix
= sd_radv_prefix_unref(prefix
->radv_prefix
);
39 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 int route_prefix_new(Prefix
**ret
) {
105 _cleanup_(prefix_freep
) Prefix
*prefix
= NULL
;
107 prefix
= new0(Prefix
, 1);
111 if (sd_radv_route_prefix_new(&prefix
->radv_route_prefix
) < 0)
114 *ret
= TAKE_PTR(prefix
);
119 void route_prefix_free(Prefix
*prefix
) {
123 if (prefix
->network
) {
124 LIST_REMOVE(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
);
138 static int route_prefix_new_static(Network
*network
, const char *filename
,
139 unsigned section_line
, Prefix
**ret
) {
140 _cleanup_(network_config_section_freep
) NetworkConfigSection
*n
= NULL
;
141 _cleanup_(prefix_freep
) Prefix
*prefix
= NULL
;
146 assert(!!filename
== (section_line
> 0));
149 r
= network_config_section_new(filename
, section_line
, &n
);
154 prefix
= hashmap_get(network
->route_prefixes_by_section
, n
);
156 *ret
= TAKE_PTR(prefix
);
163 r
= route_prefix_new(&prefix
);
167 prefix
->network
= network
;
168 LIST_APPEND(prefixes
, network
->static_route_prefixes
, prefix
);
169 network
->n_static_route_prefixes
++;
172 prefix
->section
= TAKE_PTR(n
);
174 r
= hashmap_ensure_allocated(&network
->route_prefixes_by_section
, &network_config_hash_ops
);
178 r
= hashmap_put(network
->route_prefixes_by_section
, prefix
->section
, prefix
);
183 *ret
= TAKE_PTR(prefix
);
188 int config_parse_prefix(const char *unit
,
189 const char *filename
,
192 unsigned section_line
,
199 Network
*network
= userdata
;
200 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
201 uint8_t prefixlen
= 64;
202 union in_addr_union in6addr
;
211 r
= prefix_new_static(network
, filename
, section_line
, &p
);
215 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &in6addr
, &prefixlen
);
217 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix is invalid, ignoring assignment: %s", rvalue
);
221 if (sd_radv_prefix_set_prefix(p
->radv_prefix
, &in6addr
.in6
, prefixlen
) < 0)
222 return -EADDRNOTAVAIL
;
224 log_syntax(unit
, LOG_INFO
, filename
, line
, r
, "Found prefix %s", rvalue
);
231 int config_parse_prefix_flags(const char *unit
,
232 const char *filename
,
235 unsigned section_line
,
241 Network
*network
= userdata
;
242 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
251 r
= prefix_new_static(network
, filename
, section_line
, &p
);
255 r
= parse_boolean(rvalue
);
257 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address flag, ignoring: %s", rvalue
);
263 if (streq(lvalue
, "OnLink"))
264 r
= sd_radv_prefix_set_onlink(p
->radv_prefix
, val
);
265 else if (streq(lvalue
, "AddressAutoconfiguration"))
266 r
= sd_radv_prefix_set_address_autoconfiguration(p
->radv_prefix
, val
);
275 int config_parse_prefix_lifetime(const char *unit
,
276 const char *filename
,
279 unsigned section_line
,
285 Network
*network
= userdata
;
286 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
296 r
= prefix_new_static(network
, filename
, section_line
, &p
);
300 r
= parse_sec(rvalue
, &usec
);
302 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Lifetime is invalid, ignoring assignment: %s", rvalue
);
306 /* a value of 0xffffffff represents infinity */
307 if (streq(lvalue
, "PreferredLifetimeSec"))
308 r
= sd_radv_prefix_set_preferred_lifetime(p
->radv_prefix
,
309 DIV_ROUND_UP(usec
, USEC_PER_SEC
));
310 else if (streq(lvalue
, "ValidLifetimeSec"))
311 r
= sd_radv_prefix_set_valid_lifetime(p
->radv_prefix
,
312 DIV_ROUND_UP(usec
, USEC_PER_SEC
));
321 int config_parse_route_prefix(const char *unit
,
322 const char *filename
,
325 unsigned section_line
,
332 Network
*network
= userdata
;
333 _cleanup_(route_prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
334 uint8_t prefixlen
= 64;
335 union in_addr_union in6addr
;
344 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
348 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &in6addr
, &prefixlen
);
350 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Route prefix is invalid, ignoring assignment: %s", rvalue
);
354 if (sd_radv_prefix_set_route_prefix(p
->radv_route_prefix
, &in6addr
.in6
, prefixlen
) < 0)
355 return -EADDRNOTAVAIL
;
357 log_syntax(unit
, LOG_INFO
, filename
, line
, r
, "Found route prefix %s", rvalue
);
364 int config_parse_route_prefix_lifetime(const char *unit
,
365 const char *filename
,
368 unsigned section_line
,
374 Network
*network
= userdata
;
375 _cleanup_(route_prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
385 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
389 r
= parse_sec(rvalue
, &usec
);
391 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Roure lifetime is invalid, ignoring assignment: %s", rvalue
);
395 /* a value of 0xffffffff represents infinity */
396 r
= sd_radv_route_prefix_set_lifetime(p
->radv_route_prefix
, DIV_ROUND_UP(usec
, USEC_PER_SEC
));
405 static int radv_get_ip6dns(Network
*network
, struct in6_addr
**dns
,
407 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
408 size_t i
, n_addresses
= 0, n_allocated
= 0;
414 for (i
= 0; i
< network
->n_dns
; i
++) {
415 union in_addr_union
*addr
;
417 if (network
->dns
[i
].family
!= AF_INET6
)
420 addr
= &network
->dns
[i
].address
;
422 if (in_addr_is_null(AF_INET6
, addr
) ||
423 in_addr_is_link_local(AF_INET6
, addr
) ||
424 in_addr_is_localhost(AF_INET6
, addr
))
427 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
430 addresses
[n_addresses
++] = addr
->in6
;
434 *dns
= TAKE_PTR(addresses
);
436 *n_dns
= n_addresses
;
442 static int radv_set_dns(Link
*link
, Link
*uplink
) {
443 _cleanup_free_
struct in6_addr
*dns
= NULL
;
445 usec_t lifetime_usec
;
448 if (!link
->network
->router_emit_dns
)
451 if (link
->network
->router_dns
) {
452 dns
= newdup(struct in6_addr
, link
->network
->router_dns
,
453 link
->network
->n_router_dns
);
457 n_dns
= link
->network
->n_router_dns
;
458 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
463 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
465 r
= radv_get_ip6dns(link
->network
, &dns
, &n_dns
);
470 if (!uplink
->network
) {
471 log_link_debug(uplink
, "Cannot fetch DNS servers as uplink interface is not managed by us");
475 r
= radv_get_ip6dns(uplink
->network
, &dns
, &n_dns
);
483 return sd_radv_set_rdnss(link
->radv
,
484 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
488 static int radv_set_domains(Link
*link
, Link
*uplink
) {
489 OrderedSet
*search_domains
;
490 usec_t lifetime_usec
;
491 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
493 if (!link
->network
->router_emit_domains
)
496 search_domains
= link
->network
->router_search_domains
;
497 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
502 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
504 search_domains
= link
->network
->search_domains
;
509 if (!uplink
->network
) {
510 log_link_debug(uplink
, "Cannot fetch DNS search domains as uplink interface is not managed by us");
514 search_domains
= uplink
->network
->search_domains
;
522 s
= ordered_set_get_strv(search_domains
);
526 return sd_radv_set_dnssl(link
->radv
,
527 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
532 int radv_emit_dns(Link
*link
) {
536 uplink
= manager_find_uplink(link
->manager
, link
);
538 r
= radv_set_dns(link
, uplink
);
540 log_link_warning_errno(link
, r
, "Could not set RA DNS: %m");
542 r
= radv_set_domains(link
, uplink
);
544 log_link_warning_errno(link
, r
, "Could not set RA Domains: %m");
549 int radv_configure(Link
*link
) {
554 assert(link
->network
);
556 r
= sd_radv_new(&link
->radv
);
560 r
= sd_radv_attach_event(link
->radv
, NULL
, 0);
564 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
568 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
572 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
576 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
580 /* a value of 0xffffffff represents infinity, 0x0 means this host is
582 r
= sd_radv_set_router_lifetime(link
->radv
,
583 DIV_ROUND_UP(link
->network
->router_lifetime_usec
, USEC_PER_SEC
));
587 if (link
->network
->router_lifetime_usec
> 0) {
588 r
= sd_radv_set_preference(link
->radv
,
589 link
->network
->router_preference
);
594 if (IN_SET(link
->network
->router_prefix_delegation
,
595 RADV_PREFIX_DELEGATION_STATIC
,
596 RADV_PREFIX_DELEGATION_BOTH
)) {
598 LIST_FOREACH(prefixes
, p
, link
->network
->static_prefixes
) {
599 r
= sd_radv_add_prefix(link
->radv
, p
->radv_prefix
, false);
603 log_link_warning_errno(link
, r
, "[IPv6Prefix] section configured without Prefix= setting, ignoring section.");
610 LIST_FOREACH(prefixes
, p
, link
->network
->static_route_prefixes
) {
611 r
= sd_radv_add_route_prefix(link
->radv
, p
->radv_route_prefix
, false);
620 return radv_emit_dns(link
);
623 int config_parse_radv_dns(
625 const char *filename
,
628 unsigned section_line
,
636 const char *p
= rvalue
;
644 _cleanup_free_
char *w
= NULL
;
645 union in_addr_union a
;
647 r
= extract_first_word(&p
, &w
, NULL
, 0);
651 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
652 "Failed to extract word, ignoring: %s", rvalue
);
658 if (in_addr_from_string(AF_INET6
, w
, &a
) >= 0) {
661 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
665 m
[n
->n_router_dns
++] = a
.in6
;
669 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
670 "Failed to parse DNS server address, ignoring: %s", w
);
676 int config_parse_radv_search_domains(
678 const char *filename
,
681 unsigned section_line
,
689 const char *p
= rvalue
;
697 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
699 r
= extract_first_word(&p
, &w
, NULL
, 0);
703 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
704 "Failed to extract word, ignoring: %s", rvalue
);
710 r
= dns_name_apply_idna(w
, &idna
);
712 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
713 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
716 /* transfer ownership to simplify subsequent operations */
719 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops
);
723 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
731 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
732 [RADV_PREFIX_DELEGATION_NONE
] = "no",
733 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
734 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
735 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
738 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
739 radv_prefix_delegation
,
740 RADVPrefixDelegation
,
741 RADV_PREFIX_DELEGATION_BOTH
);
743 int config_parse_router_prefix_delegation(
745 const char *filename
,
748 unsigned section_line
,
755 Network
*network
= userdata
;
756 RADVPrefixDelegation d
;
764 d
= radv_prefix_delegation_from_string(rvalue
);
766 log_syntax(unit
, LOG_ERR
, filename
, line
, -EINVAL
, "Invalid router prefix delegation '%s', ignoring assignment.", rvalue
);
770 network
->router_prefix_delegation
= d
;
775 int config_parse_router_preference(const char *unit
,
776 const char *filename
,
779 unsigned section_line
,
785 Network
*network
= userdata
;
793 if (streq(rvalue
, "high"))
794 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
795 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
796 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
797 else if (streq(rvalue
, "low"))
798 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
800 log_syntax(unit
, LOG_ERR
, filename
, line
, -EINVAL
, "Router preference '%s' is invalid, ignoring assignment: %m", rvalue
);