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 config_parse_prefix(const char *unit
,
105 const char *filename
,
108 unsigned section_line
,
115 Network
*network
= userdata
;
116 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
117 uint8_t prefixlen
= 64;
118 union in_addr_union in6addr
;
127 r
= prefix_new_static(network
, filename
, section_line
, &p
);
131 r
= in_addr_prefix_from_string(rvalue
, AF_INET6
, &in6addr
, &prefixlen
);
133 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Prefix is invalid, ignoring assignment: %s", rvalue
);
137 if (sd_radv_prefix_set_prefix(p
->radv_prefix
, &in6addr
.in6
, prefixlen
) < 0)
138 return -EADDRNOTAVAIL
;
140 log_syntax(unit
, LOG_INFO
, filename
, line
, r
, "Found prefix %s", rvalue
);
147 int config_parse_prefix_flags(const char *unit
,
148 const char *filename
,
151 unsigned section_line
,
157 Network
*network
= userdata
;
158 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
167 r
= prefix_new_static(network
, filename
, section_line
, &p
);
171 r
= parse_boolean(rvalue
);
173 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Failed to parse address flag, ignoring: %s", rvalue
);
179 if (streq(lvalue
, "OnLink"))
180 r
= sd_radv_prefix_set_onlink(p
->radv_prefix
, val
);
181 else if (streq(lvalue
, "AddressAutoconfiguration"))
182 r
= sd_radv_prefix_set_address_autoconfiguration(p
->radv_prefix
, val
);
191 int config_parse_prefix_lifetime(const char *unit
,
192 const char *filename
,
195 unsigned section_line
,
201 Network
*network
= userdata
;
202 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
212 r
= prefix_new_static(network
, filename
, section_line
, &p
);
216 r
= parse_sec(rvalue
, &usec
);
218 log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Lifetime is invalid, ignoring assignment: %s", rvalue
);
222 /* a value of 0xffffffff represents infinity */
223 if (streq(lvalue
, "PreferredLifetimeSec"))
224 r
= sd_radv_prefix_set_preferred_lifetime(p
->radv_prefix
,
225 DIV_ROUND_UP(usec
, USEC_PER_SEC
));
226 else if (streq(lvalue
, "ValidLifetimeSec"))
227 r
= sd_radv_prefix_set_valid_lifetime(p
->radv_prefix
,
228 DIV_ROUND_UP(usec
, USEC_PER_SEC
));
237 static int radv_get_ip6dns(Network
*network
, struct in6_addr
**dns
,
239 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
240 size_t i
, n_addresses
= 0, n_allocated
= 0;
246 for (i
= 0; i
< network
->n_dns
; i
++) {
247 union in_addr_union
*addr
;
249 if (network
->dns
[i
].family
!= AF_INET6
)
252 addr
= &network
->dns
[i
].address
;
254 if (in_addr_is_null(AF_INET6
, addr
) ||
255 in_addr_is_link_local(AF_INET6
, addr
) ||
256 in_addr_is_localhost(AF_INET6
, addr
))
259 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
262 addresses
[n_addresses
++] = addr
->in6
;
266 *dns
= TAKE_PTR(addresses
);
268 *n_dns
= n_addresses
;
274 static int radv_set_dns(Link
*link
, Link
*uplink
) {
275 _cleanup_free_
struct in6_addr
*dns
= NULL
;
277 usec_t lifetime_usec
;
280 if (!link
->network
->router_emit_dns
)
283 if (link
->network
->router_dns
) {
284 dns
= newdup(struct in6_addr
, link
->network
->router_dns
,
285 link
->network
->n_router_dns
);
289 n_dns
= link
->network
->n_router_dns
;
290 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
295 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
297 r
= radv_get_ip6dns(link
->network
, &dns
, &n_dns
);
302 if (!uplink
->network
) {
303 log_link_debug(uplink
, "Cannot fetch DNS servers as uplink interface is not managed by us");
307 r
= radv_get_ip6dns(uplink
->network
, &dns
, &n_dns
);
315 return sd_radv_set_rdnss(link
->radv
,
316 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
320 static int radv_set_domains(Link
*link
, Link
*uplink
) {
321 OrderedSet
*search_domains
;
322 usec_t lifetime_usec
;
323 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
325 if (!link
->network
->router_emit_domains
)
328 search_domains
= link
->network
->router_search_domains
;
329 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
334 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
336 search_domains
= link
->network
->search_domains
;
341 if (!uplink
->network
) {
342 log_link_debug(uplink
, "Cannot fetch DNS search domains as uplink interface is not managed by us");
346 search_domains
= uplink
->network
->search_domains
;
354 s
= ordered_set_get_strv(search_domains
);
358 return sd_radv_set_dnssl(link
->radv
,
359 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
364 int radv_emit_dns(Link
*link
) {
368 uplink
= manager_find_uplink(link
->manager
, link
);
370 r
= radv_set_dns(link
, uplink
);
372 log_link_warning_errno(link
, r
, "Could not set RA DNS: %m");
374 r
= radv_set_domains(link
, uplink
);
376 log_link_warning_errno(link
, r
, "Could not set RA Domains: %m");
381 int radv_configure(Link
*link
) {
386 assert(link
->network
);
388 r
= sd_radv_new(&link
->radv
);
392 r
= sd_radv_attach_event(link
->radv
, NULL
, 0);
396 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
400 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
404 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
408 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
412 /* a value of 0xffffffff represents infinity, 0x0 means this host is
414 r
= sd_radv_set_router_lifetime(link
->radv
,
415 DIV_ROUND_UP(link
->network
->router_lifetime_usec
, USEC_PER_SEC
));
419 if (link
->network
->router_lifetime_usec
> 0) {
420 r
= sd_radv_set_preference(link
->radv
,
421 link
->network
->router_preference
);
426 if (IN_SET(link
->network
->router_prefix_delegation
,
427 RADV_PREFIX_DELEGATION_STATIC
,
428 RADV_PREFIX_DELEGATION_BOTH
)) {
430 LIST_FOREACH(prefixes
, p
, link
->network
->static_prefixes
) {
431 r
= sd_radv_add_prefix(link
->radv
, p
->radv_prefix
, false);
435 log_link_warning_errno(link
, r
, "[IPv6Prefix] section configured without Prefix= setting, ignoring section.");
443 return radv_emit_dns(link
);
446 int config_parse_radv_dns(
448 const char *filename
,
451 unsigned section_line
,
459 const char *p
= rvalue
;
467 _cleanup_free_
char *w
= NULL
;
468 union in_addr_union a
;
470 r
= extract_first_word(&p
, &w
, NULL
, 0);
474 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
475 "Failed to extract word, ignoring: %s", rvalue
);
481 if (in_addr_from_string(AF_INET6
, w
, &a
) >= 0) {
484 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
488 m
[n
->n_router_dns
++] = a
.in6
;
492 log_syntax(unit
, LOG_ERR
, filename
, line
, 0,
493 "Failed to parse DNS server address, ignoring: %s", w
);
499 int config_parse_radv_search_domains(
501 const char *filename
,
504 unsigned section_line
,
512 const char *p
= rvalue
;
520 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
522 r
= extract_first_word(&p
, &w
, NULL
, 0);
526 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
527 "Failed to extract word, ignoring: %s", rvalue
);
533 r
= dns_name_apply_idna(w
, &idna
);
535 log_syntax(unit
, LOG_ERR
, filename
, line
, r
,
536 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
539 /* transfer ownership to simplify subsequent operations */
542 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops
);
546 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
554 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
555 [RADV_PREFIX_DELEGATION_NONE
] = "no",
556 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
557 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
558 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
561 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
562 radv_prefix_delegation
,
563 RADVPrefixDelegation
,
564 RADV_PREFIX_DELEGATION_BOTH
);
566 int config_parse_router_prefix_delegation(
568 const char *filename
,
571 unsigned section_line
,
578 Network
*network
= userdata
;
579 RADVPrefixDelegation d
;
587 d
= radv_prefix_delegation_from_string(rvalue
);
589 log_syntax(unit
, LOG_ERR
, filename
, line
, -EINVAL
, "Invalid router prefix delegation '%s', ignoring assignment.", rvalue
);
593 network
->router_prefix_delegation
= d
;
598 int config_parse_router_preference(const char *unit
,
599 const char *filename
,
602 unsigned section_line
,
608 Network
*network
= userdata
;
616 if (streq(rvalue
, "high"))
617 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
618 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
619 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
620 else if (streq(rvalue
, "low"))
621 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
623 log_syntax(unit
, LOG_ERR
, filename
, line
, -EINVAL
, "Router preference '%s' is invalid, ignoring assignment: %m", rvalue
);