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/ip6.h>
24 #include <netinet/in.h>
25 #include <sys/ioctl.h>
29 #include "socket-util.h"
31 #include "icmp6-util.h"
34 #define NDISC_ROUTER_SOLICITATION_INTERVAL 4 * USEC_PER_SEC
35 #define NDISC_MAX_ROUTER_SOLICITATIONS 3
39 NDISC_STATE_SOLICITATION_SENT
,
40 NDISC_STATE_ADVERTISMENT_LISTEN
,
42 _NDISC_STATE_INVALID
= -1,
45 #define IP6_MIN_MTU (unsigned)1280
46 #define ICMP6_RECV_SIZE (IP6_MIN_MTU - sizeof(struct ip6_hdr))
47 #define NDISC_OPT_LEN_UNITS 8
49 typedef struct NDiscPrefix NDiscPrefix
;
56 LIST_FIELDS(NDiscPrefix
, prefixes
);
66 enum NDiscState state
;
70 struct ether_addr mac_addr
;
72 LIST_HEAD(NDiscPrefix
, prefixes
);
74 sd_event_source
*recv
;
75 sd_event_source
*timeout
;
77 sd_ndisc_callback_t callback
;
81 #define log_ndisc(p, fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "NDisc CLIENT: " fmt, ##__VA_ARGS__)
83 static NDiscPrefix
*ndisc_prefix_unref(NDiscPrefix
*prefix
) {
88 assert(prefix
->n_ref
> 0);
91 if (prefix
->n_ref
> 0)
95 LIST_REMOVE(prefixes
, prefix
->nd
->prefixes
, prefix
);
102 static int ndisc_prefix_new(sd_ndisc
*nd
, NDiscPrefix
**ret
) {
103 _cleanup_free_ NDiscPrefix
*prefix
= NULL
;
107 prefix
= new0(NDiscPrefix
, 1);
112 LIST_INIT(prefixes
, prefix
);
121 static void ndisc_notify(sd_ndisc
*nd
, int event
) {
123 nd
->callback(nd
, event
, nd
->userdata
);
126 int sd_ndisc_set_callback(sd_ndisc
*nd
, sd_ndisc_callback_t callback
,
130 nd
->callback
= callback
;
131 nd
->userdata
= userdata
;
136 int sd_ndisc_set_index(sd_ndisc
*nd
, int interface_index
) {
138 assert(interface_index
>= -1);
140 nd
->index
= interface_index
;
145 int sd_ndisc_set_mac(sd_ndisc
*nd
, const struct ether_addr
*mac_addr
) {
149 memcpy(&nd
->mac_addr
, mac_addr
, sizeof(nd
->mac_addr
));
157 int sd_ndisc_attach_event(sd_ndisc
*nd
, sd_event
*event
, int priority
) {
160 assert_return(nd
, -EINVAL
);
161 assert_return(!nd
->event
, -EBUSY
);
164 nd
->event
= sd_event_ref(event
);
166 r
= sd_event_default(&nd
->event
);
171 nd
->event_priority
= priority
;
176 int sd_ndisc_detach_event(sd_ndisc
*nd
) {
177 assert_return(nd
, -EINVAL
);
179 nd
->event
= sd_event_unref(nd
->event
);
184 sd_event
*sd_ndisc_get_event(sd_ndisc
*nd
) {
190 sd_ndisc
*sd_ndisc_ref(sd_ndisc
*nd
) {
195 assert(nd
->n_ref
> 0);
201 static int ndisc_init(sd_ndisc
*nd
) {
204 nd
->recv
= sd_event_source_unref(nd
->recv
);
205 nd
->fd
= asynchronous_close(nd
->fd
);
206 nd
->timeout
= sd_event_source_unref(nd
->timeout
);
211 sd_ndisc
*sd_ndisc_unref(sd_ndisc
*nd
) {
212 NDiscPrefix
*prefix
, *p
;
217 assert(nd
->n_ref
> 0);
224 sd_ndisc_detach_event(nd
);
226 LIST_FOREACH_SAFE(prefixes
, prefix
, p
, nd
->prefixes
)
227 prefix
= ndisc_prefix_unref(prefix
);
234 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_ndisc
*, sd_ndisc_unref
);
235 #define _cleanup_sd_ndisc_free_ _cleanup_(sd_ndisc_unrefp)
237 int sd_ndisc_new(sd_ndisc
**ret
) {
238 _cleanup_sd_ndisc_free_ sd_ndisc
*nd
= NULL
;
242 nd
= new0(sd_ndisc
, 1);
251 LIST_HEAD_INIT(nd
->prefixes
);
259 int sd_ndisc_get_mtu(sd_ndisc
*nd
, uint32_t *mtu
) {
260 assert_return(nd
, -EINVAL
);
261 assert_return(mtu
, -EINVAL
);
271 static int prefix_match(const struct in6_addr
*prefix
, uint8_t prefixlen
,
272 const struct in6_addr
*addr
,
273 uint8_t addr_prefixlen
) {
274 uint8_t bytes
, mask
, len
;
276 assert_return(prefix
, -EINVAL
);
277 assert_return(addr
, -EINVAL
);
279 len
= MIN(prefixlen
, addr_prefixlen
);
282 mask
= 0xff << (8 - len
% 8);
284 if (memcmp(prefix
, addr
, bytes
) != 0 ||
285 (prefix
->s6_addr
[bytes
] & mask
) != (addr
->s6_addr
[bytes
] & mask
))
286 return -EADDRNOTAVAIL
;
291 static int ndisc_prefix_match(sd_ndisc
*nd
, const struct in6_addr
*addr
,
292 uint8_t addr_len
, NDiscPrefix
**result
) {
293 NDiscPrefix
*prefix
, *p
;
299 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
303 LIST_FOREACH_SAFE(prefixes
, prefix
, p
, nd
->prefixes
) {
304 if (prefix
->valid_until
< time_now
) {
305 prefix
= ndisc_prefix_unref(prefix
);
310 if (prefix_match(&prefix
->addr
, prefix
->len
, addr
, addr_len
) >= 0) {
316 return -EADDRNOTAVAIL
;
319 static int ndisc_prefix_update(sd_ndisc
*nd
, ssize_t len
,
320 const struct nd_opt_prefix_info
*prefix_opt
) {
324 char time_string
[FORMAT_TIMESPAN_MAX
];
330 if (len
< prefix_opt
->nd_opt_pi_len
)
333 if (!(prefix_opt
->nd_opt_pi_flags_reserved
& ND_OPT_PI_FLAG_ONLINK
))
336 lifetime
= be32toh(prefix_opt
->nd_opt_pi_valid_time
);
338 r
= ndisc_prefix_match(nd
, &prefix_opt
->nd_opt_pi_prefix
,
339 prefix_opt
->nd_opt_pi_prefix_len
, &prefix
);
341 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
344 /* if router advertisment prefix valid timeout is zero, the timeout
345 callback will be called immediately to clean up the prefix */
347 if (r
== -EADDRNOTAVAIL
) {
348 r
= ndisc_prefix_new(nd
, &prefix
);
352 prefix
->len
= prefix_opt
->nd_opt_pi_prefix_len
;
354 memcpy(&prefix
->addr
, &prefix_opt
->nd_opt_pi_prefix
,
355 sizeof(prefix
->addr
));
357 log_ndisc(nd
, "New prefix "SD_NDISC_ADDRESS_FORMAT_STR
"/%d lifetime %d expires in %s",
358 SD_NDISC_ADDRESS_FORMAT_VAL(prefix
->addr
),
359 prefix
->len
, lifetime
,
360 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, lifetime
* USEC_PER_SEC
, USEC_PER_SEC
));
362 LIST_PREPEND(prefixes
, nd
->prefixes
, prefix
);
365 if (prefix
->len
!= prefix_opt
->nd_opt_pi_prefix_len
) {
368 prefixlen
= MIN(prefix
->len
, prefix_opt
->nd_opt_pi_prefix_len
);
370 log_ndisc(nd
, "Prefix length mismatch %d/%d using %d",
372 prefix_opt
->nd_opt_pi_prefix_len
,
375 prefix
->len
= prefixlen
;
378 log_ndisc(nd
, "Update prefix "SD_NDISC_ADDRESS_FORMAT_STR
"/%d lifetime %d expires in %s",
379 SD_NDISC_ADDRESS_FORMAT_VAL(prefix
->addr
),
380 prefix
->len
, lifetime
,
381 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, lifetime
* USEC_PER_SEC
, USEC_PER_SEC
));
384 r
= sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
);
388 prefix
->valid_until
= time_now
+ lifetime
* USEC_PER_SEC
;
393 static int ndisc_ra_parse(sd_ndisc
*nd
, struct nd_router_advert
*ra
,
396 struct nd_opt_hdr
*opt_hdr
;
398 assert_return(nd
, -EINVAL
);
399 assert_return(ra
, -EINVAL
);
402 if (len
< NDISC_OPT_LEN_UNITS
) {
403 log_ndisc(nd
, "Router Advertisement below minimum length");
411 while (len
!= 0 && len
>= opt_hdr
->nd_opt_len
* NDISC_OPT_LEN_UNITS
) {
412 struct nd_opt_mtu
*opt_mtu
;
414 struct nd_opt_prefix_info
*opt_prefix
;
416 if (opt_hdr
->nd_opt_len
== 0)
419 switch (opt_hdr
->nd_opt_type
) {
423 mtu
= be32toh(opt_mtu
->nd_opt_mtu_mtu
);
425 if (mtu
!= nd
->mtu
) {
426 nd
->mtu
= MAX(mtu
, IP6_MIN_MTU
);
428 log_ndisc(nd
, "Router Advertisement link MTU %d using %d",
434 case ND_OPT_PREFIX_INFORMATION
:
437 ndisc_prefix_update(nd
, len
, opt_prefix
);
442 len
-= opt_hdr
->nd_opt_len
* NDISC_OPT_LEN_UNITS
;
443 opt
= (void *)((char *)opt
+
444 opt_hdr
->nd_opt_len
* NDISC_OPT_LEN_UNITS
);
449 log_ndisc(nd
, "Router Advertisement contains %zd bytes of trailing garbage", len
);
454 static int ndisc_router_advertisment_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
455 sd_ndisc
*nd
= userdata
;
458 _cleanup_free_
struct nd_router_advert
*ra
= NULL
;
459 int event
= SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE
;
465 r
= ioctl(fd
, FIONREAD
, &buflen
);
466 if (r
< 0 || buflen
<= 0)
467 buflen
= ICMP6_RECV_SIZE
;
473 len
= read(fd
, ra
, buflen
);
475 log_ndisc(nd
, "Could not receive message from UDP socket: %m");
479 if (ra
->nd_ra_type
!= ND_ROUTER_ADVERT
)
482 if (ra
->nd_ra_code
!= 0)
485 nd
->timeout
= sd_event_source_unref(nd
->timeout
);
487 nd
->state
= NDISC_STATE_ADVERTISMENT_LISTEN
;
489 if (ra
->nd_ra_flags_reserved
& ND_RA_FLAG_OTHER
)
490 event
= SD_NDISC_EVENT_ROUTER_ADVERTISMENT_OTHER
;
492 if (ra
->nd_ra_flags_reserved
& ND_RA_FLAG_MANAGED
)
493 event
= SD_NDISC_EVENT_ROUTER_ADVERTISMENT_MANAGED
;
495 log_ndisc(nd
, "Received Router Advertisement flags %s/%s",
496 ra
->nd_ra_flags_reserved
& ND_RA_FLAG_MANAGED
? "MANAGED": "none",
497 ra
->nd_ra_flags_reserved
& ND_RA_FLAG_OTHER
? "OTHER": "none");
499 if (event
!= SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE
) {
500 r
= ndisc_ra_parse(nd
, ra
, len
);
502 log_ndisc(nd
, "Could not parse Router Advertisement: %s",
508 ndisc_notify(nd
, event
);
513 static int ndisc_router_solicitation_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
514 sd_ndisc
*nd
= userdata
;
515 uint64_t time_now
, next_timeout
;
516 struct ether_addr unset
= { };
517 struct ether_addr
*addr
= NULL
;
524 nd
->timeout
= sd_event_source_unref(nd
->timeout
);
526 if (nd
->nd_sent
>= NDISC_MAX_ROUTER_SOLICITATIONS
) {
527 ndisc_notify(nd
, SD_NDISC_EVENT_ROUTER_ADVERTISMENT_TIMEOUT
);
528 nd
->state
= NDISC_STATE_ADVERTISMENT_LISTEN
;
530 if (memcmp(&nd
->mac_addr
, &unset
, sizeof(struct ether_addr
)))
531 addr
= &nd
->mac_addr
;
533 r
= icmp6_send_router_solicitation(nd
->fd
, addr
);
535 log_ndisc(nd
, "Error sending Router Solicitation");
537 nd
->state
= NDISC_STATE_SOLICITATION_SENT
;
538 log_ndisc(nd
, "Sent Router Solicitation");
543 assert_se(sd_event_now(nd
->event
, clock_boottime_or_monotonic(), &time_now
) >= 0);
545 next_timeout
= time_now
+ NDISC_ROUTER_SOLICITATION_INTERVAL
;
547 r
= sd_event_add_time(nd
->event
, &nd
->timeout
, clock_boottime_or_monotonic(),
549 ndisc_router_solicitation_timeout
, nd
);
555 r
= sd_event_source_set_priority(nd
->timeout
, nd
->event_priority
);
559 r
= sd_event_source_set_description(nd
->timeout
, "ndisc-timeout");
567 int sd_ndisc_stop(sd_ndisc
*nd
) {
568 assert_return(nd
, -EINVAL
);
569 assert_return(nd
->event
, -EINVAL
);
571 log_ndisc(client
, "Stop NDisc");
575 nd
->state
= NDISC_STATE_IDLE
;
580 int sd_ndisc_router_discovery_start(sd_ndisc
*nd
) {
586 if (nd
->state
!= NDISC_STATE_IDLE
)
592 r
= icmp6_bind_router_solicitation(nd
->index
);
598 r
= sd_event_add_io(nd
->event
, &nd
->recv
, nd
->fd
, EPOLLIN
,
599 ndisc_router_advertisment_recv
, nd
);
603 r
= sd_event_source_set_priority(nd
->recv
, nd
->event_priority
);
607 r
= sd_event_source_set_description(nd
->recv
, "ndisc-receive-message");
611 r
= sd_event_add_time(nd
->event
, &nd
->timeout
, clock_boottime_or_monotonic(),
612 0, 0, ndisc_router_solicitation_timeout
, nd
);
616 r
= sd_event_source_set_priority(nd
->timeout
, nd
->event_priority
);
620 r
= sd_event_source_set_description(nd
->timeout
, "ndisc-timeout");
625 log_ndisc(client
, "Start Router Solicitation");