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_WARNING
, filename
, line
, r
, "Prefix is invalid, ignoring assignment: %s", rvalue
);
222 r
= sd_radv_prefix_set_prefix(p
->radv_prefix
, &in6addr
.in6
, prefixlen
);
224 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to set radv prefix, ignoring assignment: %s", rvalue
);
233 int config_parse_prefix_flags(const char *unit
,
234 const char *filename
,
237 unsigned section_line
,
243 Network
*network
= userdata
;
244 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
253 r
= prefix_new_static(network
, filename
, section_line
, &p
);
257 r
= parse_boolean(rvalue
);
259 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to parse %s=, ignoring assignment: %s", lvalue
, rvalue
);
263 if (streq(lvalue
, "OnLink"))
264 r
= sd_radv_prefix_set_onlink(p
->radv_prefix
, r
);
265 else if (streq(lvalue
, "AddressAutoconfiguration"))
266 r
= sd_radv_prefix_set_address_autoconfiguration(p
->radv_prefix
, r
);
268 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to set %s=, ignoring assignment: %m", lvalue
);
277 int config_parse_prefix_lifetime(const char *unit
,
278 const char *filename
,
281 unsigned section_line
,
287 Network
*network
= userdata
;
288 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
298 r
= prefix_new_static(network
, filename
, section_line
, &p
);
302 r
= parse_sec(rvalue
, &usec
);
304 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Lifetime is invalid, ignoring assignment: %s", rvalue
);
308 /* a value of 0xffffffff represents infinity */
309 if (streq(lvalue
, "PreferredLifetimeSec"))
310 r
= sd_radv_prefix_set_preferred_lifetime(p
->radv_prefix
,
311 DIV_ROUND_UP(usec
, USEC_PER_SEC
));
312 else if (streq(lvalue
, "ValidLifetimeSec"))
313 r
= sd_radv_prefix_set_valid_lifetime(p
->radv_prefix
,
314 DIV_ROUND_UP(usec
, USEC_PER_SEC
));
316 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Failed to set %s=, ignoring assignment: %m", lvalue
);
325 int config_parse_prefix_assign(
327 const char *filename
,
330 unsigned section_line
,
337 Network
*network
= userdata
;
338 _cleanup_(prefix_free_or_set_invalidp
) Prefix
*p
= NULL
;
347 r
= prefix_new_static(network
, filename
, section_line
, &p
);
351 r
= parse_boolean(rvalue
);
353 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
354 "Failed to parse %s=, ignoring assignment: %s",
365 int config_parse_route_prefix(const char *unit
,
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(const char *unit
,
410 const char *filename
,
413 unsigned section_line
,
419 Network
*network
= userdata
;
420 _cleanup_(route_prefix_free_or_set_invalidp
) RoutePrefix
*p
= NULL
;
430 r
= route_prefix_new_static(network
, filename
, section_line
, &p
);
434 r
= parse_sec(rvalue
, &usec
);
436 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
437 "Route lifetime is invalid, ignoring assignment: %s", rvalue
);
441 /* a value of 0xffffffff represents infinity */
442 r
= sd_radv_route_prefix_set_lifetime(p
->radv_route_prefix
, DIV_ROUND_UP(usec
, USEC_PER_SEC
));
444 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
445 "Failed to set route lifetime, ignoring assignment: %m");
454 static int radv_get_ip6dns(Network
*network
, struct in6_addr
**dns
,
456 _cleanup_free_
struct in6_addr
*addresses
= NULL
;
457 size_t i
, n_addresses
= 0, n_allocated
= 0;
463 for (i
= 0; i
< network
->n_dns
; i
++) {
464 union in_addr_union
*addr
;
466 if (network
->dns
[i
]->family
!= AF_INET6
)
469 addr
= &network
->dns
[i
]->address
;
471 if (in_addr_is_null(AF_INET6
, addr
) ||
472 in_addr_is_link_local(AF_INET6
, addr
) ||
473 in_addr_is_localhost(AF_INET6
, addr
))
476 if (!GREEDY_REALLOC(addresses
, n_allocated
, n_addresses
+ 1))
479 addresses
[n_addresses
++] = addr
->in6
;
483 *dns
= TAKE_PTR(addresses
);
485 *n_dns
= n_addresses
;
491 static int radv_set_dns(Link
*link
, Link
*uplink
) {
492 _cleanup_free_
struct in6_addr
*dns
= NULL
;
493 usec_t lifetime_usec
;
497 if (!link
->network
->router_emit_dns
)
500 if (link
->network
->router_dns
) {
503 dns
= new(struct in6_addr
, link
->network
->n_router_dns
);
508 for (size_t i
= 0; i
< link
->network
->n_router_dns
; i
++)
509 if (IN6_IS_ADDR_UNSPECIFIED(&link
->network
->router_dns
[i
])) {
510 if (!IN6_IS_ADDR_UNSPECIFIED(&link
->ipv6ll_address
))
511 *(p
++) = link
->ipv6ll_address
;
513 *(p
++) = link
->network
->router_dns
[i
];
516 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
521 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
523 r
= radv_get_ip6dns(link
->network
, &dns
, &n_dns
);
528 if (!uplink
->network
) {
529 log_link_debug(uplink
, "Cannot fetch DNS servers as uplink interface is not managed by us");
533 r
= radv_get_ip6dns(uplink
->network
, &dns
, &n_dns
);
541 return sd_radv_set_rdnss(link
->radv
,
542 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
546 static int radv_set_domains(Link
*link
, Link
*uplink
) {
547 OrderedSet
*search_domains
;
548 usec_t lifetime_usec
;
549 _cleanup_free_
char **s
= NULL
; /* just free() because the strings are owned by the set */
551 if (!link
->network
->router_emit_domains
)
554 search_domains
= link
->network
->router_search_domains
;
555 lifetime_usec
= link
->network
->router_dns_lifetime_usec
;
560 lifetime_usec
= SD_RADV_DEFAULT_DNS_LIFETIME_USEC
;
562 search_domains
= link
->network
->search_domains
;
567 if (!uplink
->network
) {
568 log_link_debug(uplink
, "Cannot fetch DNS search domains as uplink interface is not managed by us");
572 search_domains
= uplink
->network
->search_domains
;
580 s
= ordered_set_get_strv(search_domains
);
584 return sd_radv_set_dnssl(link
->radv
,
585 DIV_ROUND_UP(lifetime_usec
, USEC_PER_SEC
),
590 int radv_emit_dns(Link
*link
) {
594 uplink
= manager_find_uplink(link
->manager
, link
);
596 r
= radv_set_dns(link
, uplink
);
598 log_link_warning_errno(link
, r
, "Could not set RA DNS: %m");
600 r
= radv_set_domains(link
, uplink
);
602 log_link_warning_errno(link
, r
, "Could not set RA Domains: %m");
607 int radv_configure(Link
*link
) {
613 assert(link
->network
);
615 r
= sd_radv_new(&link
->radv
);
619 r
= sd_radv_attach_event(link
->radv
, NULL
, 0);
623 r
= sd_radv_set_mac(link
->radv
, &link
->mac
);
627 r
= sd_radv_set_ifindex(link
->radv
, link
->ifindex
);
631 r
= sd_radv_set_managed_information(link
->radv
, link
->network
->router_managed
);
635 r
= sd_radv_set_other_information(link
->radv
, link
->network
->router_other_information
);
639 /* a value of 0xffffffff represents infinity, 0x0 means this host is
641 r
= sd_radv_set_router_lifetime(link
->radv
,
642 DIV_ROUND_UP(link
->network
->router_lifetime_usec
, USEC_PER_SEC
));
646 if (link
->network
->router_lifetime_usec
> 0) {
647 r
= sd_radv_set_preference(link
->radv
,
648 link
->network
->router_preference
);
653 if (link
->network
->router_prefix_delegation
& RADV_PREFIX_DELEGATION_STATIC
) {
654 LIST_FOREACH(prefixes
, p
, link
->network
->static_prefixes
) {
655 r
= sd_radv_add_prefix(link
->radv
, p
->radv_prefix
, false);
659 log_link_warning_errno(link
, r
, "[IPv6Prefix] section configured without Prefix= setting, ignoring section.");
666 LIST_FOREACH(route_prefixes
, q
, link
->network
->static_route_prefixes
) {
667 r
= sd_radv_add_route_prefix(link
->radv
, q
->radv_route_prefix
, false);
678 int radv_add_prefix(Link
*link
, const struct in6_addr
*prefix
, uint8_t prefix_len
,
679 uint32_t lifetime_preferred
, uint32_t lifetime_valid
) {
680 _cleanup_(sd_radv_prefix_unrefp
) sd_radv_prefix
*p
= NULL
;
686 r
= sd_radv_prefix_new(&p
);
690 r
= sd_radv_prefix_set_prefix(p
, prefix
, prefix_len
);
694 r
= sd_radv_prefix_set_preferred_lifetime(p
, lifetime_preferred
);
698 r
= sd_radv_prefix_set_valid_lifetime(p
, lifetime_valid
);
702 r
= sd_radv_add_prefix(link
->radv
, p
, true);
703 if (r
< 0 && r
!= -EEXIST
)
709 int config_parse_radv_dns(
711 const char *filename
,
714 unsigned section_line
,
728 for (const char *p
= rvalue
;;) {
729 _cleanup_free_
char *w
= NULL
;
730 union in_addr_union a
;
732 r
= extract_first_word(&p
, &w
, NULL
, 0);
736 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
737 "Failed to extract word, ignoring: %s", rvalue
);
743 if (streq(w
, "_link_local"))
746 r
= in_addr_from_string(AF_INET6
, w
, &a
);
748 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
749 "Failed to parse DNS server address, ignoring: %s", w
);
753 if (in_addr_is_null(AF_INET6
, &a
)) {
754 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
755 "DNS server address is null, ignoring: %s", w
);
761 m
= reallocarray(n
->router_dns
, n
->n_router_dns
+ 1, sizeof(struct in6_addr
));
765 m
[n
->n_router_dns
++] = a
.in6
;
770 int config_parse_radv_search_domains(
772 const char *filename
,
775 unsigned section_line
,
789 for (const char *p
= rvalue
;;) {
790 _cleanup_free_
char *w
= NULL
, *idna
= NULL
;
792 r
= extract_first_word(&p
, &w
, NULL
, 0);
796 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
797 "Failed to extract word, ignoring: %s", rvalue
);
803 r
= dns_name_apply_idna(w
, &idna
);
805 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
806 "Failed to apply IDNA to domain name '%s', ignoring: %m", w
);
809 /* transfer ownership to simplify subsequent operations */
812 r
= ordered_set_ensure_allocated(&n
->router_search_domains
, &string_hash_ops
);
816 r
= ordered_set_consume(n
->router_search_domains
, TAKE_PTR(idna
));
822 static const char * const radv_prefix_delegation_table
[_RADV_PREFIX_DELEGATION_MAX
] = {
823 [RADV_PREFIX_DELEGATION_NONE
] = "no",
824 [RADV_PREFIX_DELEGATION_STATIC
] = "static",
825 [RADV_PREFIX_DELEGATION_DHCP6
] = "dhcpv6",
826 [RADV_PREFIX_DELEGATION_BOTH
] = "yes",
829 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(
830 radv_prefix_delegation
,
831 RADVPrefixDelegation
,
832 RADV_PREFIX_DELEGATION_BOTH
);
834 DEFINE_CONFIG_PARSE_ENUM(config_parse_router_prefix_delegation
,
835 radv_prefix_delegation
,
836 RADVPrefixDelegation
,
837 "Invalid router prefix delegation");
839 int config_parse_router_preference(const char *unit
,
840 const char *filename
,
843 unsigned section_line
,
849 Network
*network
= userdata
;
857 if (streq(rvalue
, "high"))
858 network
->router_preference
= SD_NDISC_PREFERENCE_HIGH
;
859 else if (STR_IN_SET(rvalue
, "medium", "normal", "default"))
860 network
->router_preference
= SD_NDISC_PREFERENCE_MEDIUM
;
861 else if (streq(rvalue
, "low"))
862 network
->router_preference
= SD_NDISC_PREFERENCE_LOW
;
864 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
865 "Invalid router preference, ignoring assignment: %s", rvalue
);