2 This file is part of systemd.
4 Copyright (C) 2014 Intel Corporation. All rights reserved.
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <netinet/icmp6.h>
21 #include <netinet/in.h>
22 #include <netinet/ip6.h>
25 #include <sys/ioctl.h>
29 #include "alloc-util.h"
31 #include "icmp6-util.h"
32 #include "in-addr-util.h"
34 #include "socket-util.h"
35 #include "string-util.h"
37 #define NDISC_ROUTER_SOLICITATION_INTERVAL 4 * USEC_PER_SEC
38 #define NDISC_MAX_ROUTER_SOLICITATIONS 3
42 NDISC_STATE_SOLICITATION_SENT
,
43 NDISC_STATE_ADVERTISMENT_LISTEN
,
45 _NDISC_STATE_INVALID
= -1,
48 #define IP6_MIN_MTU (unsigned)1280
49 #define ICMP6_RECV_SIZE (IP6_MIN_MTU - sizeof(struct ip6_hdr))
50 #define NDISC_OPT_LEN_UNITS 8
52 #define ND_RA_FLAG_PREF 0x18
53 #define ND_RA_FLAG_PREF_LOW 0x03
54 #define ND_RA_FLAG_PREF_MEDIUM 0x0
55 #define ND_RA_FLAG_PREF_HIGH 0x1
56 #define ND_RA_FLAG_PREF_INVALID 0x2
58 typedef struct NDiscPrefix NDiscPrefix
;
65 LIST_FIELDS(NDiscPrefix
, prefixes
);
75 enum NDiscState state
;
79 struct ether_addr mac_addr
;
81 LIST_HEAD(NDiscPrefix
, prefixes
);
83 sd_event_source
*recv
;
84 sd_event_source
*timeout
;
86 sd_ndisc_router_callback_t router_callback
;
87 sd_ndisc_prefix_autonomous_callback_t prefix_autonomous_callback
;
88 sd_ndisc_prefix_onlink_callback_t prefix_onlink_callback
;
89 sd_ndisc_callback_t callback
;
93 #define log_ndisc(p, fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "NDisc CLIENT: " fmt, ##__VA_ARGS__)
95 static NDiscPrefix
*ndisc_prefix_unref(NDiscPrefix
*prefix
) {
100 assert(prefix
->n_ref
> 0);
103 if (prefix
->n_ref
> 0)
107 LIST_REMOVE(prefixes
, prefix
->nd
->prefixes
, prefix
);
114 static int ndisc_prefix_new(sd_ndisc
*nd
, NDiscPrefix
**ret
) {
119 prefix
= new0(NDiscPrefix
, 1);
124 LIST_INIT(prefixes
, prefix
);
131 int sd_ndisc_set_callback(sd_ndisc
*nd
,
132 sd_ndisc_router_callback_t router_callback
,
133 sd_ndisc_prefix_onlink_callback_t prefix_onlink_callback
,
134 sd_ndisc_prefix_autonomous_callback_t prefix_autonomous_callback
,
135 sd_ndisc_callback_t callback
,
139 nd
->router_callback
= router_callback
;
140 nd
->prefix_onlink_callback
= prefix_onlink_callback
;
141 nd
->prefix_autonomous_callback
= prefix_autonomous_callback
;
142 nd
->callback
= callback
;
143 nd
->userdata
= userdata
;
148 int sd_ndisc_set_index(sd_ndisc
*nd
, int interface_index
) {
150 assert(interface_index
>= -1);
152 nd
->index
= interface_index
;
157 int sd_ndisc_set_mac(sd_ndisc
*nd
, const struct ether_addr
*mac_addr
) {
161 memcpy(&nd
->mac_addr
, mac_addr
, sizeof(nd
->mac_addr
));
169 int sd_ndisc_attach_event(sd_ndisc
*nd
, sd_event
*event
, int priority
) {
172 assert_return(nd
, -EINVAL
);
173 assert_return(!nd
->event
, -EBUSY
);
176 nd
->event
= sd_event_ref(event
);
178 r
= sd_event_default(&nd
->event
);
183 nd
->event_priority
= priority
;
188 int sd_ndisc_detach_event(sd_ndisc
*nd
) {
189 assert_return(nd
, -EINVAL
);
191 nd
->event
= sd_event_unref(nd
->event
);
196 sd_event
*sd_ndisc_get_event(sd_ndisc
*nd
) {
202 sd_ndisc
*sd_ndisc_ref(sd_ndisc
*nd
) {
207 assert(nd
->n_ref
> 0);
213 static int ndisc_init(sd_ndisc
*nd
) {
216 nd
->recv
= sd_event_source_unref(nd
->recv
);
217 nd
->fd
= asynchronous_close(nd
->fd
);
218 nd
->timeout
= sd_event_source_unref(nd
->timeout
);
223 sd_ndisc
*sd_ndisc_unref(sd_ndisc
*nd
) {
224 NDiscPrefix
*prefix
, *p
;
229 assert(nd
->n_ref
> 0);
236 sd_ndisc_detach_event(nd
);
238 LIST_FOREACH_SAFE(prefixes
, prefix
, p
, nd
->prefixes
)
239 prefix
= ndisc_prefix_unref(prefix
);
246 int sd_ndisc_new(sd_ndisc
**ret
) {
247 _cleanup_(sd_ndisc_unrefp
) sd_ndisc
*nd
= NULL
;
251 nd
= new0(sd_ndisc
, 1);
260 LIST_HEAD_INIT(nd
->prefixes
);
268 int sd_ndisc_get_mtu(sd_ndisc
*nd
, uint32_t *mtu
) {
269 assert_return(nd
, -EINVAL
);
270 assert_return(mtu
, -EINVAL
);
280 static int prefix_match(const struct in6_addr
*prefix
, uint8_t prefixlen
,
281 const struct in6_addr
*addr
,
282 uint8_t addr_prefixlen
) {
283 uint8_t bytes
, mask
, len
;
285 assert_return(prefix
, -EINVAL
);
286 assert_return(addr
, -EINVAL
);
288 len
= MIN(prefixlen
, addr_prefixlen
);
291 mask
= 0xff << (8 - len
% 8);
293 if (memcmp(prefix
, addr
, bytes
) != 0 ||
294 (prefix
->s6_addr
[bytes
] & mask
) != (addr
->s6_addr
[bytes
] & mask
))
295 return -EADDRNOTAVAIL
;
300 static int ndisc_prefix_match(sd_ndisc
*nd
, const struct in6_addr
*addr
,
301 uint8_t addr_len
, NDiscPrefix
**result
) {
302 NDiscPrefix
*prefix
, *p
;
308 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
312 LIST_FOREACH_SAFE(prefixes
, prefix
, p
, nd
->prefixes
) {
313 if (prefix
->valid_until
< time_now
) {
314 prefix
= ndisc_prefix_unref(prefix
);
318 if (prefix_match(&prefix
->addr
, prefix
->len
, addr
, addr_len
) >= 0) {
324 return -EADDRNOTAVAIL
;
327 static int ndisc_prefix_update(sd_ndisc
*nd
, ssize_t len
,
328 const struct nd_opt_prefix_info
*prefix_opt
) {
330 uint32_t lifetime_valid
, lifetime_preferred
;
332 char time_string
[FORMAT_TIMESPAN_MAX
];
338 if (len
< prefix_opt
->nd_opt_pi_len
)
341 if (!(prefix_opt
->nd_opt_pi_flags_reserved
& (ND_OPT_PI_FLAG_ONLINK
| ND_OPT_PI_FLAG_AUTO
)))
344 if (in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*) &prefix_opt
->nd_opt_pi_prefix
) > 0)
347 lifetime_valid
= be32toh(prefix_opt
->nd_opt_pi_valid_time
);
348 lifetime_preferred
= be32toh(prefix_opt
->nd_opt_pi_preferred_time
);
350 if (lifetime_valid
< lifetime_preferred
)
353 r
= ndisc_prefix_match(nd
, &prefix_opt
->nd_opt_pi_prefix
,
354 prefix_opt
->nd_opt_pi_prefix_len
, &prefix
);
356 if (r
!= -EADDRNOTAVAIL
)
359 /* if router advertisment prefix valid timeout is zero, the timeout
360 callback will be called immediately to clean up the prefix */
362 r
= ndisc_prefix_new(nd
, &prefix
);
366 prefix
->len
= prefix_opt
->nd_opt_pi_prefix_len
;
368 memcpy(&prefix
->addr
, &prefix_opt
->nd_opt_pi_prefix
,
369 sizeof(prefix
->addr
));
371 log_ndisc(nd
, "New prefix "SD_NDISC_ADDRESS_FORMAT_STR
"/%d lifetime %d expires in %s",
372 SD_NDISC_ADDRESS_FORMAT_VAL(prefix
->addr
),
373 prefix
->len
, lifetime_valid
,
374 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, lifetime_valid
* USEC_PER_SEC
, USEC_PER_SEC
));
376 LIST_PREPEND(prefixes
, nd
->prefixes
, prefix
);
379 if (prefix
->len
!= prefix_opt
->nd_opt_pi_prefix_len
) {
382 prefixlen
= MIN(prefix
->len
, prefix_opt
->nd_opt_pi_prefix_len
);
384 log_ndisc(nd
, "Prefix length mismatch %d/%d using %d",
386 prefix_opt
->nd_opt_pi_prefix_len
,
389 prefix
->len
= prefixlen
;
392 log_ndisc(nd
, "Update prefix "SD_NDISC_ADDRESS_FORMAT_STR
"/%d lifetime %d expires in %s",
393 SD_NDISC_ADDRESS_FORMAT_VAL(prefix
->addr
),
394 prefix
->len
, lifetime_valid
,
395 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, lifetime_valid
* USEC_PER_SEC
, USEC_PER_SEC
));
398 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
402 prefix
->valid_until
= time_now
+ lifetime_valid
* USEC_PER_SEC
;
404 if ((prefix_opt
->nd_opt_pi_flags_reserved
& ND_OPT_PI_FLAG_ONLINK
) && nd
->prefix_onlink_callback
)
405 nd
->prefix_onlink_callback(nd
, &prefix
->addr
, prefix
->len
, prefix
->valid_until
, nd
->userdata
);
407 if ((prefix_opt
->nd_opt_pi_flags_reserved
& ND_OPT_PI_FLAG_AUTO
) && nd
->prefix_autonomous_callback
)
408 nd
->prefix_autonomous_callback(nd
, &prefix
->addr
, prefix
->len
, lifetime_preferred
, lifetime_valid
,
414 static int ndisc_ra_parse(sd_ndisc
*nd
, struct nd_router_advert
*ra
, ssize_t len
) {
416 struct nd_opt_hdr
*opt_hdr
;
418 assert_return(nd
, -EINVAL
);
419 assert_return(ra
, -EINVAL
);
422 if (len
< NDISC_OPT_LEN_UNITS
) {
423 log_ndisc(nd
, "Router Advertisement below minimum length");
431 while (len
!= 0 && len
>= opt_hdr
->nd_opt_len
* NDISC_OPT_LEN_UNITS
) {
432 struct nd_opt_mtu
*opt_mtu
;
434 struct nd_opt_prefix_info
*opt_prefix
;
436 if (opt_hdr
->nd_opt_len
== 0)
439 switch (opt_hdr
->nd_opt_type
) {
443 mtu
= be32toh(opt_mtu
->nd_opt_mtu_mtu
);
445 if (mtu
!= nd
->mtu
) {
446 nd
->mtu
= MAX(mtu
, IP6_MIN_MTU
);
448 log_ndisc(nd
, "Router Advertisement link MTU %d using %d",
454 case ND_OPT_PREFIX_INFORMATION
:
457 ndisc_prefix_update(nd
, len
, opt_prefix
);
462 len
-= opt_hdr
->nd_opt_len
* NDISC_OPT_LEN_UNITS
;
463 opt
= (void *)((char *)opt
+
464 opt_hdr
->nd_opt_len
* NDISC_OPT_LEN_UNITS
);
469 log_ndisc(nd
, "Router Advertisement contains %zd bytes of trailing garbage", len
);
474 static int ndisc_router_advertisment_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
475 _cleanup_free_
struct nd_router_advert
*ra
= NULL
;
476 sd_ndisc
*nd
= userdata
;
478 struct cmsghdr cmsghdr
;
479 uint8_t buf
[CMSG_LEN(sizeof(int))];
481 struct iovec iov
= {};
482 union sockaddr_union sa
= {};
483 struct msghdr msg
= {
485 .msg_namelen
= sizeof(sa
),
488 .msg_control
= &control
,
489 .msg_controllen
= sizeof(control
),
491 struct cmsghdr
*cmsg
;
495 int r
, pref
, stateful
, buflen
= 0;
501 r
= ioctl(fd
, FIONREAD
, &buflen
);
505 /* This really should not happen */
508 iov
.iov_len
= buflen
;
510 ra
= malloc(iov
.iov_len
);
516 len
= recvmsg(fd
, &msg
, 0);
518 if (errno
== EAGAIN
|| errno
== EINTR
)
521 log_ndisc(nd
, "Could not receive message from ICMPv6 socket: %m");
523 } else if ((size_t)len
< sizeof(struct nd_router_advert
)) {
525 } else if (msg
.msg_namelen
== 0)
526 gw
= NULL
; /* only happens when running the test-suite over a socketpair */
527 else if (msg
.msg_namelen
!= sizeof(sa
.in6
)) {
528 log_ndisc(nd
, "Received invalid source address size from ICMPv6 socket: %zu bytes", (size_t)msg
.msg_namelen
);
531 gw
= &sa
.in6
.sin6_addr
;
533 assert(!(msg
.msg_flags
& MSG_CTRUNC
));
534 assert(!(msg
.msg_flags
& MSG_TRUNC
));
536 CMSG_FOREACH(cmsg
, &msg
) {
537 if (cmsg
->cmsg_level
== SOL_IPV6
&&
538 cmsg
->cmsg_type
== IPV6_HOPLIMIT
&&
539 cmsg
->cmsg_len
== CMSG_LEN(sizeof(int))) {
540 int hops
= *(int*)CMSG_DATA(cmsg
);
543 log_ndisc(nd
, "Received RA with invalid hop limit %d. Ignoring.", hops
);
551 if (gw
&& !in_addr_is_link_local(AF_INET6
, (const union in_addr_union
*) gw
)) {
552 _cleanup_free_
char *addr
= NULL
;
554 (void)in_addr_to_string(AF_INET6
, (const union in_addr_union
*) gw
, &addr
);
556 log_ndisc(nd
, "Received RA from non-link-local address %s. Ignoring.", strna(addr
));
560 if (ra
->nd_ra_type
!= ND_ROUTER_ADVERT
)
563 if (ra
->nd_ra_code
!= 0)
566 nd
->timeout
= sd_event_source_unref(nd
->timeout
);
568 nd
->state
= NDISC_STATE_ADVERTISMENT_LISTEN
;
570 stateful
= ra
->nd_ra_flags_reserved
& (ND_RA_FLAG_MANAGED
| ND_RA_FLAG_OTHER
);
571 pref
= (ra
->nd_ra_flags_reserved
& ND_RA_FLAG_PREF
) >> 3;
574 case ND_RA_FLAG_PREF_LOW
:
575 case ND_RA_FLAG_PREF_HIGH
:
578 pref
= ND_RA_FLAG_PREF_MEDIUM
;
582 lifetime
= be16toh(ra
->nd_ra_router_lifetime
);
584 log_ndisc(nd
, "Received Router Advertisement: flags %s preference %s lifetime %u sec",
585 stateful
& ND_RA_FLAG_MANAGED
? "MANAGED" : stateful
& ND_RA_FLAG_OTHER
? "OTHER" : "none",
586 pref
== ND_RA_FLAG_PREF_HIGH
? "high" : pref
== ND_RA_FLAG_PREF_LOW
? "low" : "medium",
589 r
= ndisc_ra_parse(nd
, ra
, len
);
591 log_ndisc(nd
, "Could not parse Router Advertisement: %s", strerror(-r
));
595 if (nd
->router_callback
)
596 nd
->router_callback(nd
, stateful
, gw
, lifetime
, pref
, nd
->userdata
);
601 static int ndisc_router_solicitation_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
602 sd_ndisc
*nd
= userdata
;
603 uint64_t time_now
, next_timeout
;
610 nd
->timeout
= sd_event_source_unref(nd
->timeout
);
612 if (nd
->nd_sent
>= NDISC_MAX_ROUTER_SOLICITATIONS
) {
614 nd
->callback(nd
, SD_NDISC_EVENT_TIMEOUT
, nd
->userdata
);
615 nd
->state
= NDISC_STATE_ADVERTISMENT_LISTEN
;
617 r
= icmp6_send_router_solicitation(nd
->fd
, &nd
->mac_addr
);
619 log_ndisc(nd
, "Error sending Router Solicitation");
621 nd
->state
= NDISC_STATE_SOLICITATION_SENT
;
622 log_ndisc(nd
, "Sent Router Solicitation");
627 assert_se(sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
629 next_timeout
= time_now
+ NDISC_ROUTER_SOLICITATION_INTERVAL
;
631 r
= sd_event_add_time(nd
->event
, &nd
->timeout
, clock_boottime_or_monotonic(),
633 ndisc_router_solicitation_timeout
, nd
);
635 /* we cannot continue if we are unable to rearm the timer */
640 r
= sd_event_source_set_priority(nd
->timeout
, nd
->event_priority
);
644 r
= sd_event_source_set_description(nd
->timeout
, "ndisc-timeout");
652 int sd_ndisc_stop(sd_ndisc
*nd
) {
653 assert_return(nd
, -EINVAL
);
654 assert_return(nd
->event
, -EINVAL
);
656 log_ndisc(client
, "Stop NDisc");
660 nd
->state
= NDISC_STATE_IDLE
;
663 nd
->callback(nd
, SD_NDISC_EVENT_STOP
, nd
->userdata
);
668 int sd_ndisc_router_discovery_start(sd_ndisc
*nd
) {
674 if (nd
->state
!= NDISC_STATE_IDLE
)
680 r
= icmp6_bind_router_solicitation(nd
->index
);
686 r
= sd_event_add_io(nd
->event
, &nd
->recv
, nd
->fd
, EPOLLIN
,
687 ndisc_router_advertisment_recv
, nd
);
691 r
= sd_event_source_set_priority(nd
->recv
, nd
->event_priority
);
695 r
= sd_event_source_set_description(nd
->recv
, "ndisc-receive-message");
699 r
= sd_event_add_time(nd
->event
, &nd
->timeout
, clock_boottime_or_monotonic(),
700 0, 0, ndisc_router_solicitation_timeout
, nd
);
704 r
= sd_event_source_set_priority(nd
->timeout
, nd
->event_priority
);
708 r
= sd_event_source_set_description(nd
->timeout
, "ndisc-timeout");
713 log_ndisc(client
, "Start Router Solicitation");