1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Intel Corporation. All rights reserved.
7 #include <netinet/icmp6.h>
12 #include "missing_network.h"
13 #include "networkd-address.h"
14 #include "networkd-dhcp6.h"
15 #include "networkd-manager.h"
16 #include "networkd-ndisc.h"
17 #include "networkd-sysctl.h"
18 #include "string-table.h"
19 #include "string-util.h"
22 #define NDISC_DNSSL_MAX 64U
23 #define NDISC_RDNSS_MAX 64U
24 #define NDISC_PREFIX_LFT_MIN 7200U
26 #define DAD_CONFLICTS_IDGEN_RETRIES_RFC7217 3
28 /* https://tools.ietf.org/html/rfc5453 */
29 /* https://www.iana.org/assignments/ipv6-interface-ids/ipv6-interface-ids.xml */
31 #define SUBNET_ROUTER_ANYCAST_ADDRESS_RFC4291 ((struct in6_addr) { .s6_addr = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } })
32 #define SUBNET_ROUTER_ANYCAST_PREFIXLEN 8
33 #define RESERVED_IPV6_INTERFACE_IDENTIFIERS_ADDRESS_RFC4291 ((struct in6_addr) { .s6_addr = { 0x02, 0x00, 0x5E, 0xFF, 0xFE } })
34 #define RESERVED_IPV6_INTERFACE_IDENTIFIERS_PREFIXLEN 5
35 #define RESERVED_SUBNET_ANYCAST_ADDRESSES_RFC4291 ((struct in6_addr) { .s6_addr = { 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } })
36 #define RESERVED_SUBNET_ANYCAST_PREFIXLEN 7
38 #define NDISC_APP_ID SD_ID128_MAKE(13,ac,81,a7,d5,3f,49,78,92,79,5d,0c,29,3a,bc,7e)
40 bool link_ipv6_accept_ra_enabled(Link
*link
) {
43 if (!socket_ipv6_is_supported())
46 if (link
->flags
& IFF_LOOPBACK
)
52 if (!link_ipv6ll_enabled(link
))
55 /* If unset use system default (enabled if local forwarding is disabled.
56 * disabled if local forwarding is enabled).
57 * If set, ignore or enforce RA independent of local forwarding state.
59 if (link
->network
->ipv6_accept_ra
< 0)
60 /* default to accept RA if ip_forward is disabled and ignore RA if ip_forward is enabled */
61 return !link_ip_forward_enabled(link
, AF_INET6
);
62 else if (link
->network
->ipv6_accept_ra
> 0)
63 /* accept RA even if ip_forward is enabled */
70 static int ndisc_remove_old_one(Link
*link
, const struct in6_addr
*router
, bool force
);
72 static int ndisc_address_callback(Address
*address
) {
73 struct in6_addr router
= {};
77 assert(address
->link
);
79 SET_FOREACH(n
, address
->link
->ndisc_addresses
)
80 if (n
->address
== address
) {
85 if (IN6_IS_ADDR_UNSPECIFIED(&router
)) {
86 _cleanup_free_
char *buf
= NULL
;
88 (void) in_addr_to_string(address
->family
, &address
->in_addr
, &buf
);
89 log_link_debug(address
->link
, "%s is called for %s/%u, but it is already removed, ignoring.",
90 __func__
, strna(buf
), address
->prefixlen
);
94 /* Make this called only once */
95 SET_FOREACH(n
, address
->link
->ndisc_addresses
)
96 if (IN6_ARE_ADDR_EQUAL(&n
->router
, &router
))
97 n
->address
->callback
= NULL
;
99 return ndisc_remove_old_one(address
->link
, &router
, true);
102 static int ndisc_remove_old_one(Link
*link
, const struct in6_addr
*router
, bool force
) {
113 bool set_callback
= false;
115 if (!link
->ndisc_addresses_configured
|| !link
->ndisc_routes_configured
)
118 SET_FOREACH(na
, link
->ndisc_addresses
)
119 if (!na
->marked
&& IN6_ARE_ADDR_EQUAL(&na
->router
, router
)) {
125 SET_FOREACH(na
, link
->ndisc_addresses
)
126 if (!na
->marked
&& address_is_ready(na
->address
)) {
127 set_callback
= false;
132 SET_FOREACH(na
, link
->ndisc_addresses
)
133 if (!na
->marked
&& IN6_ARE_ADDR_EQUAL(&na
->router
, router
))
134 na
->address
->callback
= ndisc_address_callback
;
137 _cleanup_free_
char *buf
= NULL
;
139 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) router
, &buf
);
140 log_link_debug(link
, "No SLAAC address obtained from %s is ready. "
141 "The old NDisc information will be removed later.",
149 _cleanup_free_
char *buf
= NULL
;
151 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) router
, &buf
);
152 log_link_debug(link
, "Removing old NDisc information obtained from %s.", strna(buf
));
157 SET_FOREACH(na
, link
->ndisc_addresses
)
158 if (na
->marked
&& IN6_ARE_ADDR_EQUAL(&na
->router
, router
)) {
159 k
= address_remove(na
->address
, link
, NULL
);
164 SET_FOREACH(nr
, link
->ndisc_routes
)
165 if (nr
->marked
&& IN6_ARE_ADDR_EQUAL(&nr
->router
, router
)) {
166 k
= route_remove(nr
->route
, link
, NULL
);
171 SET_FOREACH(rdnss
, link
->ndisc_rdnss
)
172 if (rdnss
->marked
&& IN6_ARE_ADDR_EQUAL(&rdnss
->router
, router
))
173 free(set_remove(link
->ndisc_rdnss
, rdnss
));
175 SET_FOREACH(dnssl
, link
->ndisc_dnssl
)
176 if (dnssl
->marked
&& IN6_ARE_ADDR_EQUAL(&dnssl
->router
, router
))
177 free(set_remove(link
->ndisc_dnssl
, dnssl
));
182 static int ndisc_remove_old(Link
*link
) {
183 _cleanup_set_free_free_ Set
*routers
= NULL
;
184 _cleanup_free_
struct in6_addr
*router
= NULL
;
194 routers
= set_new(&in6_addr_hash_ops
);
198 SET_FOREACH(na
, link
->ndisc_addresses
)
199 if (!set_contains(routers
, &na
->router
)) {
200 router
= newdup(struct in6_addr
, &na
->router
, 1);
204 r
= set_put(routers
, router
);
212 SET_FOREACH(nr
, link
->ndisc_routes
)
213 if (!set_contains(routers
, &nr
->router
)) {
214 router
= newdup(struct in6_addr
, &nr
->router
, 1);
218 r
= set_put(routers
, router
);
226 SET_FOREACH(rdnss
, link
->ndisc_rdnss
)
227 if (!set_contains(routers
, &rdnss
->router
)) {
228 router
= newdup(struct in6_addr
, &rdnss
->router
, 1);
232 r
= set_put(routers
, router
);
240 SET_FOREACH(dnssl
, link
->ndisc_dnssl
)
241 if (!set_contains(routers
, &dnssl
->router
)) {
242 router
= newdup(struct in6_addr
, &dnssl
->router
, 1);
246 r
= set_put(routers
, router
);
255 SET_FOREACH(a
, routers
) {
256 k
= ndisc_remove_old_one(link
, a
, false);
264 static void ndisc_route_hash_func(const NDiscRoute
*x
, struct siphash
*state
) {
265 route_hash_func(x
->route
, state
);
268 static int ndisc_route_compare_func(const NDiscRoute
*a
, const NDiscRoute
*b
) {
269 return route_compare_func(a
->route
, b
->route
);
272 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
273 ndisc_route_hash_ops
,
275 ndisc_route_hash_func
,
276 ndisc_route_compare_func
,
279 static int ndisc_route_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
283 assert(link
->ndisc_routes_messages
> 0);
285 link
->ndisc_routes_messages
--;
287 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
290 r
= sd_netlink_message_get_errno(m
);
291 if (r
< 0 && r
!= -EEXIST
) {
292 log_link_message_error_errno(link
, m
, r
, "Could not set NDisc route");
293 link_enter_failed(link
);
297 if (link
->ndisc_routes_messages
== 0) {
298 log_link_debug(link
, "NDisc routes set.");
299 link
->ndisc_routes_configured
= true;
301 r
= ndisc_remove_old(link
);
303 link_enter_failed(link
);
307 link_check_ready(link
);
313 static int ndisc_route_configure(Route
*route
, Link
*link
, sd_ndisc_router
*rt
) {
314 _cleanup_free_ NDiscRoute
*nr
= NULL
;
315 NDiscRoute
*nr_exist
;
316 struct in6_addr router
;
324 r
= route_configure(route
, link
, ndisc_route_handler
, &ret
);
326 return log_link_error_errno(link
, r
, "Failed to set NDisc route: %m");
328 link
->ndisc_routes_messages
++;
330 r
= sd_ndisc_router_get_address(rt
, &router
);
332 return log_link_error_errno(link
, r
, "Failed to get router address from RA: %m");
334 nr
= new(NDiscRoute
, 1);
343 nr_exist
= set_get(link
->ndisc_routes
, nr
);
345 nr_exist
->marked
= false;
346 nr_exist
->router
= router
;
350 r
= set_ensure_put(&link
->ndisc_routes
, &ndisc_route_hash_ops
, nr
);
352 return log_link_error_errno(link
, r
, "Failed to store NDisc SLAAC route: %m");
359 static void ndisc_address_hash_func(const NDiscAddress
*x
, struct siphash
*state
) {
360 address_hash_func(x
->address
, state
);
363 static int ndisc_address_compare_func(const NDiscAddress
*a
, const NDiscAddress
*b
) {
364 return address_compare_func(a
->address
, b
->address
);
367 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
368 ndisc_address_hash_ops
,
370 ndisc_address_hash_func
,
371 ndisc_address_compare_func
,
374 static int ndisc_address_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
378 assert(link
->ndisc_addresses_messages
> 0);
380 link
->ndisc_addresses_messages
--;
382 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
385 r
= sd_netlink_message_get_errno(m
);
386 if (r
< 0 && r
!= -EEXIST
) {
387 log_link_message_error_errno(link
, m
, r
, "Could not set NDisc address");
388 link_enter_failed(link
);
391 (void) manager_rtnl_process_address(rtnl
, m
, link
->manager
);
393 if (link
->ndisc_addresses_messages
== 0) {
394 log_link_debug(link
, "NDisc SLAAC addresses set.");
395 link
->ndisc_addresses_configured
= true;
397 r
= ndisc_remove_old(link
);
399 link_enter_failed(link
);
403 r
= link_set_routes(link
);
405 link_enter_failed(link
);
413 static int ndisc_address_configure(Address
*address
, Link
*link
, sd_ndisc_router
*rt
) {
414 _cleanup_free_ NDiscAddress
*na
= NULL
;
415 NDiscAddress
*na_exist
;
416 struct in6_addr router
;
424 r
= address_configure(address
, link
, ndisc_address_handler
, true, &ret
);
426 return log_link_error_errno(link
, r
, "Failed to set NDisc SLAAC address: %m");
428 link
->ndisc_addresses_messages
++;
430 r
= sd_ndisc_router_get_address(rt
, &router
);
432 return log_link_error_errno(link
, r
, "Failed to get router address from RA: %m");
434 na
= new(NDiscAddress
, 1);
438 *na
= (NDiscAddress
) {
443 na_exist
= set_get(link
->ndisc_addresses
, na
);
445 na_exist
->marked
= false;
446 na_exist
->router
= router
;
450 r
= set_ensure_put(&link
->ndisc_addresses
, &ndisc_address_hash_ops
, na
);
452 return log_link_error_errno(link
, r
, "Failed to store NDisc SLAAC address: %m");
459 static int ndisc_router_process_default(Link
*link
, sd_ndisc_router
*rt
) {
460 _cleanup_(route_freep
) Route
*route
= NULL
;
461 union in_addr_union gateway
;
471 r
= sd_ndisc_router_get_lifetime(rt
, &lifetime
);
473 return log_link_error_errno(link
, r
, "Failed to get gateway lifetime from RA: %m");
475 if (lifetime
== 0) /* not a default router */
478 r
= sd_ndisc_router_get_address(rt
, &gateway
.in6
);
480 return log_link_error_errno(link
, r
, "Failed to get gateway address from RA: %m");
482 if (address_exists(link
, AF_INET6
, &gateway
)) {
484 _cleanup_free_
char *buffer
= NULL
;
486 (void) in_addr_to_string(AF_INET6
, &gateway
, &buffer
);
487 log_link_debug(link
, "No NDisc route added, gateway %s matches local address",
493 r
= sd_ndisc_router_get_preference(rt
, &preference
);
495 return log_link_error_errno(link
, r
, "Failed to get default router preference from RA: %m");
497 r
= sd_ndisc_router_get_timestamp(rt
, clock_boottime_or_monotonic(), &time_now
);
499 return log_link_error_errno(link
, r
, "Failed to get RA timestamp: %m");
501 r
= sd_ndisc_router_get_mtu(rt
, &mtu
);
505 return log_link_error_errno(link
, r
, "Failed to get default router MTU from RA: %m");
507 r
= route_new(&route
);
511 route
->family
= AF_INET6
;
512 route
->table
= link_get_ipv6_accept_ra_route_table(link
);
513 route
->priority
= link
->network
->dhcp6_route_metric
;
514 route
->protocol
= RTPROT_RA
;
515 route
->pref
= preference
;
517 route
->lifetime
= time_now
+ lifetime
* USEC_PER_SEC
;
520 r
= ndisc_route_configure(route
, link
, rt
);
522 return log_link_error_errno(link
, r
, "Could not set default route: %m");
525 HASHMAP_FOREACH(route_gw
, link
->network
->routes_by_section
) {
526 if (!route_gw
->gateway_from_dhcp
)
529 if (route_gw
->family
!= AF_INET6
)
532 route_gw
->gw
= gateway
;
534 r
= ndisc_route_configure(route_gw
, link
, rt
);
536 return log_link_error_errno(link
, r
, "Could not set gateway: %m");
542 static bool stableprivate_address_is_valid(const struct in6_addr
*addr
) {
545 /* According to rfc4291, generated address should not be in the following ranges. */
547 if (memcmp(addr
, &SUBNET_ROUTER_ANYCAST_ADDRESS_RFC4291
, SUBNET_ROUTER_ANYCAST_PREFIXLEN
) == 0)
550 if (memcmp(addr
, &RESERVED_IPV6_INTERFACE_IDENTIFIERS_ADDRESS_RFC4291
, RESERVED_IPV6_INTERFACE_IDENTIFIERS_PREFIXLEN
) == 0)
553 if (memcmp(addr
, &RESERVED_SUBNET_ANYCAST_ADDRESSES_RFC4291
, RESERVED_SUBNET_ANYCAST_PREFIXLEN
) == 0)
559 static int make_stableprivate_address(Link
*link
, const struct in6_addr
*prefix
, uint8_t prefix_len
, uint8_t dad_counter
, struct in6_addr
**ret
) {
560 _cleanup_free_
struct in6_addr
*addr
= NULL
;
561 sd_id128_t secret_key
;
562 struct siphash state
;
567 /* According to rfc7217 section 5.1
568 * RID = F(Prefix, Net_Iface, Network_ID, DAD_Counter, secret_key) */
570 r
= sd_id128_get_machine_app_specific(NDISC_APP_ID
, &secret_key
);
572 return log_error_errno(r
, "Failed to generate key: %m");
574 siphash24_init(&state
, secret_key
.bytes
);
576 l
= MAX(DIV_ROUND_UP(prefix_len
, 8), 8);
577 siphash24_compress(prefix
, l
, &state
);
578 siphash24_compress_string(link
->ifname
, &state
);
579 siphash24_compress(&link
->mac
, sizeof(struct ether_addr
), &state
);
580 siphash24_compress(&dad_counter
, sizeof(uint8_t), &state
);
582 rid
= htole64(siphash24_finalize(&state
));
584 addr
= new(struct in6_addr
, 1);
588 memcpy(addr
->s6_addr
, prefix
->s6_addr
, l
);
589 memcpy(addr
->s6_addr
+ l
, &rid
, 16 - l
);
591 if (!stableprivate_address_is_valid(addr
)) {
596 *ret
= TAKE_PTR(addr
);
600 static int ndisc_router_generate_addresses(Link
*link
, struct in6_addr
*address
, uint8_t prefixlen
, Set
**ret
) {
601 _cleanup_set_free_free_ Set
*addresses
= NULL
;
609 addresses
= set_new(&in6_addr_hash_ops
);
613 ORDERED_SET_FOREACH(j
, link
->network
->ipv6_tokens
) {
614 _cleanup_free_
struct in6_addr
*new_address
= NULL
;
616 if (j
->address_generation_type
== IPV6_TOKEN_ADDRESS_GENERATION_PREFIXSTABLE
617 && (IN6_IS_ADDR_UNSPECIFIED(&j
->prefix
) || IN6_ARE_ADDR_EQUAL(&j
->prefix
, address
))) {
618 /* While this loop uses dad_counter and a retry limit as specified in RFC 7217, the loop
619 does not actually attempt Duplicate Address Detection; the counter will be incremented
620 only when the address generation algorithm produces an invalid address, and the loop
621 may exit with an address which ends up being unusable due to duplication on the link.
623 for (; j
->dad_counter
< DAD_CONFLICTS_IDGEN_RETRIES_RFC7217
; j
->dad_counter
++) {
624 r
= make_stableprivate_address(link
, address
, prefixlen
, j
->dad_counter
, &new_address
);
630 } else if (j
->address_generation_type
== IPV6_TOKEN_ADDRESS_GENERATION_STATIC
) {
631 new_address
= new(struct in6_addr
, 1);
635 memcpy(new_address
->s6_addr
, address
->s6_addr
, 8);
636 memcpy(new_address
->s6_addr
+ 8, j
->prefix
.s6_addr
+ 8, 8);
640 r
= set_put(addresses
, new_address
);
642 return log_link_error_errno(link
, r
, "Failed to store SLAAC address: %m");
644 log_link_debug_errno(link
, r
, "Generated SLAAC address is duplicated, ignoring.");
646 TAKE_PTR(new_address
);
650 /* fall back to EUI-64 if no tokens provided addresses */
651 if (set_isempty(addresses
)) {
652 _cleanup_free_
struct in6_addr
*new_address
= NULL
;
654 new_address
= newdup(struct in6_addr
, address
, 1);
658 r
= generate_ipv6_eui_64_address(link
, new_address
);
660 return log_link_error_errno(link
, r
, "Failed to generate EUI64 address: %m");
662 r
= set_put(addresses
, new_address
);
664 return log_link_error_errno(link
, r
, "Failed to store SLAAC address: %m");
666 TAKE_PTR(new_address
);
669 *ret
= TAKE_PTR(addresses
);
674 static int ndisc_router_process_autonomous_prefix(Link
*link
, sd_ndisc_router
*rt
) {
675 uint32_t lifetime_valid
, lifetime_preferred
, lifetime_remaining
;
676 _cleanup_set_free_free_ Set
*addresses
= NULL
;
677 _cleanup_(address_freep
) Address
*address
= NULL
;
678 struct in6_addr addr
, *a
;
686 r
= sd_ndisc_router_get_timestamp(rt
, clock_boottime_or_monotonic(), &time_now
);
688 return log_link_error_errno(link
, r
, "Failed to get RA timestamp: %m");
690 r
= sd_ndisc_router_prefix_get_prefixlen(rt
, &prefixlen
);
692 return log_link_error_errno(link
, r
, "Failed to get prefix length: %m");
694 r
= sd_ndisc_router_prefix_get_valid_lifetime(rt
, &lifetime_valid
);
696 return log_link_error_errno(link
, r
, "Failed to get prefix valid lifetime: %m");
698 r
= sd_ndisc_router_prefix_get_preferred_lifetime(rt
, &lifetime_preferred
);
700 return log_link_error_errno(link
, r
, "Failed to get prefix preferred lifetime: %m");
702 /* The preferred lifetime is never greater than the valid lifetime */
703 if (lifetime_preferred
> lifetime_valid
)
706 r
= sd_ndisc_router_prefix_get_address(rt
, &addr
);
708 return log_link_error_errno(link
, r
, "Failed to get prefix address: %m");
710 r
= ndisc_router_generate_addresses(link
, &addr
, prefixlen
, &addresses
);
714 r
= address_new(&address
);
718 address
->family
= AF_INET6
;
719 address
->prefixlen
= prefixlen
;
720 address
->flags
= IFA_F_NOPREFIXROUTE
|IFA_F_MANAGETEMPADDR
;
721 address
->cinfo
.ifa_prefered
= lifetime_preferred
;
723 SET_FOREACH(a
, addresses
) {
724 Address
*existing_address
;
726 /* see RFC4862 section 5.5.3.e */
727 r
= address_get(link
, AF_INET6
, (union in_addr_union
*) a
, prefixlen
, &existing_address
);
729 lifetime_remaining
= existing_address
->cinfo
.tstamp
/ 100 + existing_address
->cinfo
.ifa_valid
- time_now
/ USEC_PER_SEC
;
730 if (lifetime_valid
> NDISC_PREFIX_LFT_MIN
|| lifetime_valid
> lifetime_remaining
)
731 address
->cinfo
.ifa_valid
= lifetime_valid
;
732 else if (lifetime_remaining
<= NDISC_PREFIX_LFT_MIN
)
733 address
->cinfo
.ifa_valid
= lifetime_remaining
;
735 address
->cinfo
.ifa_valid
= NDISC_PREFIX_LFT_MIN
;
736 } else if (lifetime_valid
> 0)
737 address
->cinfo
.ifa_valid
= lifetime_valid
;
739 continue; /* see RFC4862 section 5.5.3.d */
741 if (address
->cinfo
.ifa_valid
== 0)
744 address
->in_addr
.in6
= *a
;
746 r
= ndisc_address_configure(address
, link
, rt
);
748 return log_link_error_errno(link
, r
, "Could not set SLAAC address: %m");
754 static int ndisc_router_process_onlink_prefix(Link
*link
, sd_ndisc_router
*rt
) {
755 _cleanup_(route_freep
) Route
*route
= NULL
;
764 r
= sd_ndisc_router_get_timestamp(rt
, clock_boottime_or_monotonic(), &time_now
);
766 return log_link_error_errno(link
, r
, "Failed to get RA timestamp: %m");
768 r
= sd_ndisc_router_prefix_get_prefixlen(rt
, &prefixlen
);
770 return log_link_error_errno(link
, r
, "Failed to get prefix length: %m");
772 r
= sd_ndisc_router_prefix_get_valid_lifetime(rt
, &lifetime
);
774 return log_link_error_errno(link
, r
, "Failed to get prefix lifetime: %m");
776 r
= route_new(&route
);
780 route
->family
= AF_INET6
;
781 route
->table
= link_get_ipv6_accept_ra_route_table(link
);
782 route
->priority
= link
->network
->dhcp6_route_metric
;
783 route
->protocol
= RTPROT_RA
;
784 route
->flags
= RTM_F_PREFIX
;
785 route
->dst_prefixlen
= prefixlen
;
786 route
->lifetime
= time_now
+ lifetime
* USEC_PER_SEC
;
788 r
= sd_ndisc_router_prefix_get_address(rt
, &route
->dst
.in6
);
790 return log_link_error_errno(link
, r
, "Failed to get prefix address: %m");
792 r
= ndisc_route_configure(route
, link
, rt
);
794 return log_link_error_errno(link
, r
, "Could not set prefix route: %m");;
799 static int ndisc_router_process_route(Link
*link
, sd_ndisc_router
*rt
) {
800 _cleanup_(route_freep
) Route
*route
= NULL
;
801 struct in6_addr gateway
;
803 unsigned preference
, prefixlen
;
809 r
= sd_ndisc_router_route_get_lifetime(rt
, &lifetime
);
811 return log_link_error_errno(link
, r
, "Failed to get gateway lifetime from RA: %m");
816 r
= sd_ndisc_router_get_address(rt
, &gateway
);
818 return log_link_error_errno(link
, r
, "Failed to get gateway address from RA: %m");
820 r
= sd_ndisc_router_route_get_prefixlen(rt
, &prefixlen
);
822 return log_link_error_errno(link
, r
, "Failed to get route prefix length: %m");
824 r
= sd_ndisc_router_route_get_preference(rt
, &preference
);
826 return log_link_error_errno(link
, r
, "Failed to get default router preference from RA: %m");
828 r
= sd_ndisc_router_get_timestamp(rt
, clock_boottime_or_monotonic(), &time_now
);
830 return log_link_error_errno(link
, r
, "Failed to get RA timestamp: %m");
832 r
= route_new(&route
);
836 route
->family
= AF_INET6
;
837 route
->table
= link_get_ipv6_accept_ra_route_table(link
);
838 route
->priority
= link
->network
->dhcp6_route_metric
;
839 route
->protocol
= RTPROT_RA
;
840 route
->pref
= preference
;
841 route
->gw
.in6
= gateway
;
842 route
->dst_prefixlen
= prefixlen
;
843 route
->lifetime
= time_now
+ lifetime
* USEC_PER_SEC
;
845 r
= sd_ndisc_router_route_get_address(rt
, &route
->dst
.in6
);
847 return log_link_error_errno(link
, r
, "Failed to get route address: %m");
849 r
= ndisc_route_configure(route
, link
, rt
);
851 return log_link_error_errno(link
, r
, "Could not set additional route: %m");
856 static void ndisc_rdnss_hash_func(const NDiscRDNSS
*x
, struct siphash
*state
) {
857 siphash24_compress(&x
->address
, sizeof(x
->address
), state
);
860 static int ndisc_rdnss_compare_func(const NDiscRDNSS
*a
, const NDiscRDNSS
*b
) {
861 return memcmp(&a
->address
, &b
->address
, sizeof(a
->address
));
864 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
865 ndisc_rdnss_hash_ops
,
867 ndisc_rdnss_hash_func
,
868 ndisc_rdnss_compare_func
,
871 static int ndisc_router_process_rdnss(Link
*link
, sd_ndisc_router
*rt
) {
873 const struct in6_addr
*a
;
874 struct in6_addr router
;
882 r
= sd_ndisc_router_get_address(rt
, &router
);
884 return log_link_error_errno(link
, r
, "Failed to get router address from RA: %m");
886 r
= sd_ndisc_router_get_timestamp(rt
, clock_boottime_or_monotonic(), &time_now
);
888 return log_link_error_errno(link
, r
, "Failed to get RA timestamp: %m");
890 r
= sd_ndisc_router_rdnss_get_lifetime(rt
, &lifetime
);
892 return log_link_error_errno(link
, r
, "Failed to get RDNSS lifetime: %m");
894 n
= sd_ndisc_router_rdnss_get_addresses(rt
, &a
);
896 return log_link_error_errno(link
, n
, "Failed to get RDNSS addresses: %m");
898 SET_FOREACH(rdnss
, link
->ndisc_rdnss
)
899 if (IN6_ARE_ADDR_EQUAL(&rdnss
->router
, &router
))
900 rdnss
->marked
= true;
905 if (n
>= (int) NDISC_RDNSS_MAX
) {
906 log_link_warning(link
, "Too many RDNSS records per link. Only first %i records will be used.", NDISC_RDNSS_MAX
);
910 for (int j
= 0; j
< n
; j
++) {
911 _cleanup_free_ NDiscRDNSS
*x
= NULL
;
916 rdnss
= set_get(link
->ndisc_rdnss
, &d
);
918 rdnss
->marked
= false;
919 rdnss
->router
= router
;
920 rdnss
->valid_until
= time_now
+ lifetime
* USEC_PER_SEC
;
924 x
= new(NDiscRDNSS
, 1);
931 .valid_until
= time_now
+ lifetime
* USEC_PER_SEC
,
934 r
= set_ensure_consume(&link
->ndisc_rdnss
, &ndisc_rdnss_hash_ops
, TAKE_PTR(x
));
943 static void ndisc_dnssl_hash_func(const NDiscDNSSL
*x
, struct siphash
*state
) {
944 siphash24_compress_string(NDISC_DNSSL_DOMAIN(x
), state
);
947 static int ndisc_dnssl_compare_func(const NDiscDNSSL
*a
, const NDiscDNSSL
*b
) {
948 return strcmp(NDISC_DNSSL_DOMAIN(a
), NDISC_DNSSL_DOMAIN(b
));
951 DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
952 ndisc_dnssl_hash_ops
,
954 ndisc_dnssl_hash_func
,
955 ndisc_dnssl_compare_func
,
958 static int ndisc_router_process_dnssl(Link
*link
, sd_ndisc_router
*rt
) {
959 _cleanup_strv_free_
char **l
= NULL
;
960 struct in6_addr router
;
970 r
= sd_ndisc_router_get_address(rt
, &router
);
972 return log_link_error_errno(link
, r
, "Failed to get router address from RA: %m");
974 r
= sd_ndisc_router_get_timestamp(rt
, clock_boottime_or_monotonic(), &time_now
);
976 return log_link_error_errno(link
, r
, "Failed to get RA timestamp: %m");
978 r
= sd_ndisc_router_dnssl_get_lifetime(rt
, &lifetime
);
980 return log_link_error_errno(link
, r
, "Failed to get DNSSL lifetime: %m");
982 r
= sd_ndisc_router_dnssl_get_domains(rt
, &l
);
984 return log_link_error_errno(link
, r
, "Failed to get DNSSL addresses: %m");
986 SET_FOREACH(dnssl
, link
->ndisc_dnssl
)
987 if (IN6_ARE_ADDR_EQUAL(&dnssl
->router
, &router
))
988 dnssl
->marked
= true;
993 if (strv_length(l
) >= NDISC_DNSSL_MAX
) {
994 log_link_warning(link
, "Too many DNSSL records per link. Only first %i records will be used.", NDISC_DNSSL_MAX
);
995 STRV_FOREACH(j
, l
+ NDISC_DNSSL_MAX
)
1000 _cleanup_free_ NDiscDNSSL
*s
= NULL
;
1002 s
= malloc0(ALIGN(sizeof(NDiscDNSSL
)) + strlen(*j
) + 1);
1006 strcpy(NDISC_DNSSL_DOMAIN(s
), *j
);
1008 dnssl
= set_get(link
->ndisc_dnssl
, s
);
1010 dnssl
->marked
= false;
1011 dnssl
->router
= router
;
1012 dnssl
->valid_until
= time_now
+ lifetime
* USEC_PER_SEC
;
1017 s
->valid_until
= time_now
+ lifetime
* USEC_PER_SEC
;
1019 r
= set_ensure_consume(&link
->ndisc_dnssl
, &ndisc_dnssl_hash_ops
, TAKE_PTR(s
));
1028 static int ndisc_router_process_options(Link
*link
, sd_ndisc_router
*rt
) {
1030 assert(link
->network
);
1033 for (int r
= sd_ndisc_router_option_rewind(rt
); ; r
= sd_ndisc_router_option_next(rt
)) {
1037 return log_link_error_errno(link
, r
, "Failed to iterate through options: %m");
1038 if (r
== 0) /* EOF */
1041 r
= sd_ndisc_router_option_get_type(rt
, &type
);
1043 return log_link_error_errno(link
, r
, "Failed to get RA option type: %m");
1047 case SD_NDISC_OPTION_PREFIX_INFORMATION
: {
1048 union in_addr_union a
;
1051 r
= sd_ndisc_router_prefix_get_address(rt
, &a
.in6
);
1053 return log_link_error_errno(link
, r
, "Failed to get prefix address: %m");
1055 if (set_contains(link
->network
->ndisc_deny_listed_prefix
, &a
.in6
)) {
1056 if (DEBUG_LOGGING
) {
1057 _cleanup_free_
char *b
= NULL
;
1059 (void) in_addr_to_string(AF_INET6
, &a
, &b
);
1060 log_link_debug(link
, "Prefix '%s' is deny-listed, ignoring", strna(b
));
1065 r
= sd_ndisc_router_prefix_get_flags(rt
, &flags
);
1067 return log_link_error_errno(link
, r
, "Failed to get RA prefix flags: %m");
1069 if (link
->network
->ipv6_accept_ra_use_onlink_prefix
&&
1070 FLAGS_SET(flags
, ND_OPT_PI_FLAG_ONLINK
)) {
1071 r
= ndisc_router_process_onlink_prefix(link
, rt
);
1076 if (link
->network
->ipv6_accept_ra_use_autonomous_prefix
&&
1077 FLAGS_SET(flags
, ND_OPT_PI_FLAG_AUTO
)) {
1078 r
= ndisc_router_process_autonomous_prefix(link
, rt
);
1085 case SD_NDISC_OPTION_ROUTE_INFORMATION
:
1086 r
= ndisc_router_process_route(link
, rt
);
1091 case SD_NDISC_OPTION_RDNSS
:
1092 if (link
->network
->ipv6_accept_ra_use_dns
) {
1093 r
= ndisc_router_process_rdnss(link
, rt
);
1099 case SD_NDISC_OPTION_DNSSL
:
1100 if (link
->network
->ipv6_accept_ra_use_dns
) {
1101 r
= ndisc_router_process_dnssl(link
, rt
);
1110 static int ndisc_router_handler(Link
*link
, sd_ndisc_router
*rt
) {
1111 struct in6_addr router
;
1118 assert(link
->network
);
1119 assert(link
->manager
);
1122 link
->ndisc_addresses_configured
= false;
1123 link
->ndisc_routes_configured
= false;
1127 r
= sd_ndisc_router_get_address(rt
, &router
);
1129 return log_link_error_errno(link
, r
, "Failed to get router address from RA: %m");
1131 SET_FOREACH(na
, link
->ndisc_addresses
)
1132 if (IN6_ARE_ADDR_EQUAL(&na
->router
, &router
))
1135 SET_FOREACH(nr
, link
->ndisc_routes
)
1136 if (IN6_ARE_ADDR_EQUAL(&nr
->router
, &router
))
1139 r
= sd_ndisc_router_get_flags(rt
, &flags
);
1141 return log_link_error_errno(link
, r
, "Failed to get RA flags: %m");
1143 if ((flags
& (ND_RA_FLAG_MANAGED
| ND_RA_FLAG_OTHER
) && link
->network
->ipv6_accept_ra_start_dhcp6_client
)) {
1145 if (link
->network
->ipv6_accept_ra_start_dhcp6_client
== IPV6_ACCEPT_RA_START_DHCP6_CLIENT_ALWAYS
)
1146 r
= dhcp6_request_address(link
, false);
1148 /* (re)start DHCPv6 client in stateful or stateless mode according to RA flags */
1149 r
= dhcp6_request_address(link
, !(flags
& ND_RA_FLAG_MANAGED
));
1150 if (r
< 0 && r
!= -EBUSY
)
1151 return log_link_error_errno(link
, r
, "Could not acquire DHCPv6 lease on NDisc request: %m");
1153 log_link_debug(link
, "Acquiring DHCPv6 lease on NDisc request");
1156 r
= ndisc_router_process_default(link
, rt
);
1159 r
= ndisc_router_process_options(link
, rt
);
1163 if (link
->ndisc_addresses_messages
== 0)
1164 link
->ndisc_addresses_configured
= true;
1166 log_link_debug(link
, "Setting SLAAC addresses.");
1168 /* address_handler calls link_set_routes() and link_set_nexthop(). Before they are
1169 * called, the related flags must be cleared. Otherwise, the link becomes configured
1170 * state before routes are configured. */
1171 link
->static_routes_configured
= false;
1172 link
->static_nexthops_configured
= false;
1175 if (link
->ndisc_routes_messages
== 0)
1176 link
->ndisc_routes_configured
= true;
1178 log_link_debug(link
, "Setting NDisc routes.");
1180 r
= ndisc_remove_old(link
);
1184 if (link
->ndisc_addresses_configured
&& link
->ndisc_routes_configured
)
1185 link_check_ready(link
);
1187 link_set_state(link
, LINK_STATE_CONFIGURING
);
1192 static void ndisc_handler(sd_ndisc
*nd
, sd_ndisc_event event
, sd_ndisc_router
*rt
, void *userdata
) {
1193 Link
*link
= userdata
;
1198 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
1203 case SD_NDISC_EVENT_ROUTER
:
1204 r
= ndisc_router_handler(link
, rt
);
1206 link_enter_failed(link
);
1211 case SD_NDISC_EVENT_TIMEOUT
:
1212 log_link_debug(link
, "NDisc handler get timeout event");
1213 if (link
->ndisc_addresses_messages
== 0 && link
->ndisc_routes_messages
== 0) {
1214 link
->ndisc_addresses_configured
= true;
1215 link
->ndisc_routes_configured
= true;
1216 link_check_ready(link
);
1220 assert_not_reached("Unknown NDisc event");
1224 int ndisc_configure(Link
*link
) {
1229 if (!link_ipv6_accept_ra_enabled(link
))
1233 r
= sd_ndisc_new(&link
->ndisc
);
1237 r
= sd_ndisc_attach_event(link
->ndisc
, link
->manager
->event
, 0);
1242 r
= sd_ndisc_set_mac(link
->ndisc
, &link
->mac
);
1246 r
= sd_ndisc_set_ifindex(link
->ndisc
, link
->ifindex
);
1250 r
= sd_ndisc_set_callback(link
->ndisc
, ndisc_handler
, link
);
1257 void ndisc_vacuum(Link
*link
) {
1261 bool updated
= false;
1265 /* Removes all RDNSS and DNSSL entries whose validity time has passed */
1267 time_now
= now(clock_boottime_or_monotonic());
1269 SET_FOREACH(r
, link
->ndisc_rdnss
)
1270 if (r
->valid_until
< time_now
) {
1271 free(set_remove(link
->ndisc_rdnss
, r
));
1275 SET_FOREACH(d
, link
->ndisc_dnssl
)
1276 if (d
->valid_until
< time_now
) {
1277 free(set_remove(link
->ndisc_dnssl
, d
));
1285 void ndisc_flush(Link
*link
) {
1288 /* Removes all RDNSS and DNSSL entries, without exception */
1290 link
->ndisc_rdnss
= set_free(link
->ndisc_rdnss
);
1291 link
->ndisc_dnssl
= set_free(link
->ndisc_dnssl
);
1294 int ipv6token_new(IPv6Token
**ret
) {
1297 p
= new(IPv6Token
, 1);
1302 .address_generation_type
= IPV6_TOKEN_ADDRESS_GENERATION_NONE
,
1310 static void ipv6_token_hash_func(const IPv6Token
*p
, struct siphash
*state
) {
1311 siphash24_compress(&p
->address_generation_type
, sizeof(p
->address_generation_type
), state
);
1312 siphash24_compress(&p
->prefix
, sizeof(p
->prefix
), state
);
1315 static int ipv6_token_compare_func(const IPv6Token
*a
, const IPv6Token
*b
) {
1318 r
= CMP(a
->address_generation_type
, b
->address_generation_type
);
1322 return memcmp(&a
->prefix
, &b
->prefix
, sizeof(struct in6_addr
));
1325 DEFINE_HASH_OPS_WITH_KEY_DESTRUCTOR(
1326 ipv6_token_hash_ops
,
1328 ipv6_token_hash_func
,
1329 ipv6_token_compare_func
,
1332 int config_parse_ndisc_deny_listed_prefix(
1334 const char *filename
,
1336 const char *section
,
1337 unsigned section_line
,
1344 Network
*network
= data
;
1353 if (isempty(rvalue
)) {
1354 network
->ndisc_deny_listed_prefix
= set_free_free(network
->ndisc_deny_listed_prefix
);
1358 for (p
= rvalue
;;) {
1359 _cleanup_free_
char *n
= NULL
;
1360 _cleanup_free_
struct in6_addr
*a
= NULL
;
1361 union in_addr_union ip
;
1363 r
= extract_first_word(&p
, &n
, NULL
, 0);
1367 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1368 "Failed to parse NDisc deny-listed prefix, ignoring assignment: %s",
1375 r
= in_addr_from_string(AF_INET6
, n
, &ip
);
1377 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1378 "NDisc deny-listed prefix is invalid, ignoring assignment: %s", n
);
1382 if (set_contains(network
->ndisc_deny_listed_prefix
, &ip
.in6
))
1385 a
= newdup(struct in6_addr
, &ip
.in6
, 1);
1389 r
= set_ensure_consume(&network
->ndisc_deny_listed_prefix
, &in6_addr_hash_ops
, TAKE_PTR(a
));
1395 int config_parse_address_generation_type(
1397 const char *filename
,
1399 const char *section
,
1400 unsigned section_line
,
1407 _cleanup_free_ IPv6Token
*token
= NULL
;
1408 union in_addr_union buffer
;
1409 Network
*network
= data
;
1418 if (isempty(rvalue
)) {
1419 network
->ipv6_tokens
= ordered_set_free(network
->ipv6_tokens
);
1423 r
= ipv6token_new(&token
);
1427 if ((p
= startswith(rvalue
, "prefixstable"))) {
1428 token
->address_generation_type
= IPV6_TOKEN_ADDRESS_GENERATION_PREFIXSTABLE
;
1431 else if (*p
== '\0')
1434 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1435 "Invalid IPv6 token mode in %s=, ignoring assignment: %s",
1440 token
->address_generation_type
= IPV6_TOKEN_ADDRESS_GENERATION_STATIC
;
1441 p
= startswith(rvalue
, "static:");
1447 r
= in_addr_from_string(AF_INET6
, p
, &buffer
);
1449 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1450 "Failed to parse IP address in %s=, ignoring assignment: %s",
1454 if (token
->address_generation_type
== IPV6_TOKEN_ADDRESS_GENERATION_STATIC
&&
1455 in_addr_is_null(AF_INET6
, &buffer
)) {
1456 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
1457 "IPv6 address in %s= cannot be the ANY address, ignoring assignment: %s",
1461 token
->prefix
= buffer
.in6
;
1464 r
= ordered_set_ensure_allocated(&network
->ipv6_tokens
, &ipv6_token_hash_ops
);
1468 r
= ordered_set_put(network
->ipv6_tokens
, token
);
1470 log_syntax(unit
, LOG_DEBUG
, filename
, line
, r
,
1471 "IPv6 token '%s' is duplicated, ignoring: %m", rvalue
);
1473 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
1474 "Failed to store IPv6 token '%s', ignoring: %m", rvalue
);
1481 DEFINE_CONFIG_PARSE_ENUM(config_parse_ipv6_accept_ra_start_dhcp6_client
, ipv6_accept_ra_start_dhcp6_client
, IPv6AcceptRAStartDHCP6Client
,
1482 "Failed to parse DHCPv6Client= setting")
1483 static const char* const ipv6_accept_ra_start_dhcp6_client_table
[_IPV6_ACCEPT_RA_START_DHCP6_CLIENT_MAX
] = {
1484 [IPV6_ACCEPT_RA_START_DHCP6_CLIENT_NO
] = "no",
1485 [IPV6_ACCEPT_RA_START_DHCP6_CLIENT_ALWAYS
] = "always",
1486 [IPV6_ACCEPT_RA_START_DHCP6_CLIENT_YES
] = "yes",
1489 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(ipv6_accept_ra_start_dhcp6_client
, IPv6AcceptRAStartDHCP6Client
, IPV6_ACCEPT_RA_START_DHCP6_CLIENT_YES
);