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>
27 #include "socket-util.h"
31 #include "dhcp6-internal.h"
32 #include "sd-icmp6-nd.h"
34 #define ICMP6_ROUTER_SOLICITATION_INTERVAL 4 * USEC_PER_SEC
35 #define ICMP6_MAX_ROUTER_SOLICITATIONS 3
38 ICMP6_NEIGHBOR_DISCOVERY_IDLE = 0,
39 ICMP6_ROUTER_SOLICITATION_SENT = 10,
40 ICMP6_ROUTER_ADVERTISMENT_LISTEN = 11,
43 #define IP6_MIN_MTU (unsigned)1280
44 #define ICMP6_ND_RECV_SIZE (IP6_MIN_MTU - sizeof(struct ip6_hdr))
45 #define ICMP6_OPT_LEN_UNITS 8
50 enum icmp6_nd_state state;
54 struct ether_addr mac_addr;
56 sd_event_source *recv;
57 sd_event_source *timeout;
59 sd_icmp6_nd_callback_t callback;
63 #define log_icmp6_nd(p, fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "ICMPv6 CLIENT: " fmt, ##__VA_ARGS__)
65 static void icmp6_nd_notify(sd_icmp6_nd *nd, int event)
68 nd->callback(nd, event, nd->userdata);
71 int sd_icmp6_nd_set_callback(sd_icmp6_nd *nd, sd_icmp6_nd_callback_t callback,
75 nd->callback = callback;
76 nd->userdata = userdata;
81 int sd_icmp6_nd_set_index(sd_icmp6_nd *nd, int interface_index) {
83 assert(interface_index >= -1);
85 nd->index = interface_index;
90 int sd_icmp6_nd_set_mac(sd_icmp6_nd *nd, const struct ether_addr *mac_addr) {
94 memcpy(&nd->mac_addr, mac_addr, sizeof(nd->mac_addr));
102 int sd_icmp6_nd_attach_event(sd_icmp6_nd *nd, sd_event *event, int priority) {
105 assert_return(nd, -EINVAL);
106 assert_return(!nd->event, -EBUSY);
109 nd->event = sd_event_ref(event);
111 r = sd_event_default(&nd->event);
116 nd->event_priority = priority;
121 int sd_icmp6_nd_detach_event(sd_icmp6_nd *nd) {
122 assert_return(nd, -EINVAL);
124 nd->event = sd_event_unref(nd->event);
129 sd_event *sd_icmp6_nd_get_event(sd_icmp6_nd *nd) {
135 sd_icmp6_nd *sd_icmp6_nd_ref(sd_icmp6_nd *nd) {
138 assert_se(REFCNT_INC(nd->n_ref) >= 2);
143 static int icmp6_nd_init(sd_icmp6_nd *nd) {
146 nd->recv = sd_event_source_unref(nd->recv);
147 nd->fd = asynchronous_close(nd->fd);
148 nd->timeout = sd_event_source_unref(nd->timeout);
153 sd_icmp6_nd *sd_icmp6_nd_unref(sd_icmp6_nd *nd) {
154 if (nd && REFCNT_DEC(nd->n_ref) == 0) {
157 sd_icmp6_nd_detach_event(nd);
165 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_icmp6_nd*, sd_icmp6_nd_unref);
166 #define _cleanup_sd_icmp6_nd_free_ _cleanup_(sd_icmp6_nd_unrefp)
168 int sd_icmp6_nd_new(sd_icmp6_nd **ret) {
169 _cleanup_sd_icmp6_nd_free_ sd_icmp6_nd *nd = NULL;
173 nd = new0(sd_icmp6_nd, 1);
177 nd->n_ref = REFCNT_INIT;
188 static int icmp6_ra_parse(sd_icmp6_nd *nd, struct nd_router_advert *ra,
191 struct nd_opt_hdr *opt_hdr;
193 assert_return(nd, -EINVAL);
194 assert_return(ra, -EINVAL);
197 if (len < ICMP6_OPT_LEN_UNITS) {
198 log_icmp6_nd(nd, "Router Advertisement below minimum length");
206 while (len != 0 && len >= opt_hdr->nd_opt_len * ICMP6_OPT_LEN_UNITS) {
208 if (opt_hdr->nd_opt_len == 0)
211 switch (opt_hdr->nd_opt_type) {
215 len -= opt_hdr->nd_opt_len * ICMP6_OPT_LEN_UNITS;
216 opt = (void *)((char *)opt +
217 opt_hdr->nd_opt_len * ICMP6_OPT_LEN_UNITS);
222 log_icmp6_nd(nd, "Router Advertisement contains %zd bytes of trailing garbage", len);
227 static int icmp6_router_advertisment_recv(sd_event_source *s, int fd,
228 uint32_t revents, void *userdata)
230 sd_icmp6_nd *nd = userdata;
233 _cleanup_free_ struct nd_router_advert *ra = NULL;
234 int event = ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE;
240 r = ioctl(fd, FIONREAD, &buflen);
241 if (r < 0 || buflen <= 0)
242 buflen = ICMP6_ND_RECV_SIZE;
248 len = read(fd, ra, buflen);
250 log_icmp6_nd(nd, "Could not receive message from UDP socket: %m");
254 if (ra->nd_ra_type != ND_ROUTER_ADVERT)
257 if (ra->nd_ra_code != 0)
260 nd->timeout = sd_event_source_unref(nd->timeout);
262 nd->state = ICMP6_ROUTER_ADVERTISMENT_LISTEN;
264 if (ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER )
265 event = ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER;
267 if (ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED)
268 event = ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED;
270 log_icmp6_nd(nd, "Received Router Advertisement flags %s/%s",
271 ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED? "MANAGED": "none",
272 ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER? "OTHER": "none");
274 if (event != ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE) {
275 r = icmp6_ra_parse(nd, ra, len);
277 log_icmp6_nd(nd, "Could not parse Router Advertisement: %s",
283 icmp6_nd_notify(nd, event);
288 static int icmp6_router_solicitation_timeout(sd_event_source *s, uint64_t usec,
291 sd_icmp6_nd *nd = userdata;
292 uint64_t time_now, next_timeout;
293 struct ether_addr unset = { };
294 struct ether_addr *addr = NULL;
301 nd->timeout = sd_event_source_unref(nd->timeout);
303 if (nd->nd_sent >= ICMP6_MAX_ROUTER_SOLICITATIONS) {
304 icmp6_nd_notify(nd, ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT);
305 nd->state = ICMP6_ROUTER_ADVERTISMENT_LISTEN;
307 if (memcmp(&nd->mac_addr, &unset, sizeof(struct ether_addr)))
308 addr = &nd->mac_addr;
310 r = dhcp_network_icmp6_send_router_solicitation(nd->fd, addr);
312 log_icmp6_nd(nd, "Error sending Router Solicitation");
314 nd->state = ICMP6_ROUTER_SOLICITATION_SENT;
315 log_icmp6_nd(nd, "Sent Router Solicitation");
320 r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
322 icmp6_nd_notify(nd, r);
326 next_timeout = time_now + ICMP6_ROUTER_SOLICITATION_INTERVAL;
328 r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
330 icmp6_router_solicitation_timeout, nd);
332 icmp6_nd_notify(nd, r);
336 r = sd_event_source_set_priority(nd->timeout,
339 icmp6_nd_notify(nd, r);
343 r = sd_event_source_set_description(nd->timeout, "icmp6-timeout");
345 icmp6_nd_notify(nd, r);
353 int sd_icmp6_nd_stop(sd_icmp6_nd *nd) {
354 assert_return(nd, -EINVAL);
355 assert_return(nd->event, -EINVAL);
357 log_icmp6_nd(client, "Stop ICMPv6");
361 nd->state = ICMP6_NEIGHBOR_DISCOVERY_IDLE;
366 int sd_icmp6_router_solicitation_start(sd_icmp6_nd *nd) {
372 if (nd->state != ICMP6_NEIGHBOR_DISCOVERY_IDLE)
378 r = dhcp_network_icmp6_bind_router_solicitation(nd->index);
384 r = sd_event_add_io(nd->event, &nd->recv, nd->fd, EPOLLIN,
385 icmp6_router_advertisment_recv, nd);
389 r = sd_event_source_set_priority(nd->recv, nd->event_priority);
393 r = sd_event_source_set_description(nd->recv, "icmp6-receive-message");
397 r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
398 0, 0, icmp6_router_solicitation_timeout, nd);
402 r = sd_event_source_set_priority(nd->timeout, nd->event_priority);
406 r = sd_event_source_set_description(nd->timeout, "icmp6-timeout");
411 log_icmp6_nd(client, "Start Router Solicitation");