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>
23 #include <netinet/in.h>
25 #include "socket-util.h"
29 #include "dhcp6-internal.h"
30 #include "sd-icmp6-nd.h"
32 #define ICMP6_ROUTER_SOLICITATION_INTERVAL 4 * USEC_PER_SEC
33 #define ICMP6_MAX_ROUTER_SOLICITATIONS 3
36 ICMP6_NEIGHBOR_DISCOVERY_IDLE = 0,
37 ICMP6_ROUTER_SOLICITATION_SENT = 10,
38 ICMP6_ROUTER_ADVERTISMENT_LISTEN = 11,
44 enum icmp6_nd_state state;
48 struct ether_addr mac_addr;
50 sd_event_source *recv;
51 sd_event_source *timeout;
53 sd_icmp6_nd_callback_t callback;
57 #define log_icmp6_nd(p, fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "ICMPv6 CLIENT: " fmt, ##__VA_ARGS__)
59 static void icmp6_nd_notify(sd_icmp6_nd *nd, int event)
62 nd->callback(nd, event, nd->userdata);
65 int sd_icmp6_nd_set_callback(sd_icmp6_nd *nd, sd_icmp6_nd_callback_t callback,
69 nd->callback = callback;
70 nd->userdata = userdata;
75 int sd_icmp6_nd_set_index(sd_icmp6_nd *nd, int interface_index) {
77 assert(interface_index >= -1);
79 nd->index = interface_index;
84 int sd_icmp6_nd_set_mac(sd_icmp6_nd *nd, const struct ether_addr *mac_addr) {
88 memcpy(&nd->mac_addr, mac_addr, sizeof(nd->mac_addr));
96 int sd_icmp6_nd_attach_event(sd_icmp6_nd *nd, sd_event *event, int priority) {
99 assert_return(nd, -EINVAL);
100 assert_return(!nd->event, -EBUSY);
103 nd->event = sd_event_ref(event);
105 r = sd_event_default(&nd->event);
110 nd->event_priority = priority;
115 int sd_icmp6_nd_detach_event(sd_icmp6_nd *nd) {
116 assert_return(nd, -EINVAL);
118 nd->event = sd_event_unref(nd->event);
123 sd_event *sd_icmp6_nd_get_event(sd_icmp6_nd *nd) {
129 sd_icmp6_nd *sd_icmp6_nd_ref(sd_icmp6_nd *nd) {
132 assert_se(REFCNT_INC(nd->n_ref) >= 2);
137 static int icmp6_nd_init(sd_icmp6_nd *nd) {
140 nd->recv = sd_event_source_unref(nd->recv);
141 nd->fd = asynchronous_close(nd->fd);
142 nd->timeout = sd_event_source_unref(nd->timeout);
147 sd_icmp6_nd *sd_icmp6_nd_unref(sd_icmp6_nd *nd) {
148 if (nd && REFCNT_DEC(nd->n_ref) == 0) {
151 sd_icmp6_nd_detach_event(nd);
159 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_icmp6_nd*, sd_icmp6_nd_unref);
160 #define _cleanup_sd_icmp6_nd_free_ _cleanup_(sd_icmp6_nd_unrefp)
162 int sd_icmp6_nd_new(sd_icmp6_nd **ret) {
163 _cleanup_sd_icmp6_nd_free_ sd_icmp6_nd *nd = NULL;
167 nd = new0(sd_icmp6_nd, 1);
171 nd->n_ref = REFCNT_INIT;
182 static int icmp6_router_advertisment_recv(sd_event_source *s, int fd,
183 uint32_t revents, void *userdata)
185 sd_icmp6_nd *nd = userdata;
187 struct nd_router_advert ra;
188 int event = ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE;
194 /* only interested in Managed/Other flag */
195 len = read(fd, &ra, sizeof(ra));
196 if ((size_t)len < sizeof(ra))
199 if (ra.nd_ra_type != ND_ROUTER_ADVERT)
202 if (ra.nd_ra_code != 0)
205 nd->timeout = sd_event_source_unref(nd->timeout);
207 nd->state = ICMP6_ROUTER_ADVERTISMENT_LISTEN;
209 if (ra.nd_ra_flags_reserved & ND_RA_FLAG_OTHER )
210 event = ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER;
212 if (ra.nd_ra_flags_reserved & ND_RA_FLAG_MANAGED)
213 event = ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED;
215 log_icmp6_nd(nd, "Received Router Advertisement flags %s/%s",
216 (ra.nd_ra_flags_reserved & ND_RA_FLAG_MANAGED)? "MANAGED":
218 (ra.nd_ra_flags_reserved & ND_RA_FLAG_OTHER)? "OTHER":
221 icmp6_nd_notify(nd, event);
226 static int icmp6_router_solicitation_timeout(sd_event_source *s, uint64_t usec,
229 sd_icmp6_nd *nd = userdata;
230 uint64_t time_now, next_timeout;
231 struct ether_addr unset = { };
232 struct ether_addr *addr = NULL;
239 nd->timeout = sd_event_source_unref(nd->timeout);
241 if (nd->nd_sent >= ICMP6_MAX_ROUTER_SOLICITATIONS) {
242 icmp6_nd_notify(nd, ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT);
243 nd->state = ICMP6_ROUTER_ADVERTISMENT_LISTEN;
245 if (memcmp(&nd->mac_addr, &unset, sizeof(struct ether_addr)))
246 addr = &nd->mac_addr;
248 r = dhcp_network_icmp6_send_router_solicitation(nd->fd, addr);
250 log_icmp6_nd(nd, "Error sending Router Solicitation");
252 nd->state = ICMP6_ROUTER_SOLICITATION_SENT;
253 log_icmp6_nd(nd, "Sent Router Solicitation");
258 r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
260 icmp6_nd_notify(nd, r);
264 next_timeout = time_now + ICMP6_ROUTER_SOLICITATION_INTERVAL;
266 r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
268 icmp6_router_solicitation_timeout, nd);
270 icmp6_nd_notify(nd, r);
274 r = sd_event_source_set_priority(nd->timeout,
277 icmp6_nd_notify(nd, r);
281 r = sd_event_source_set_description(nd->timeout, "icmp6-timeout");
283 icmp6_nd_notify(nd, r);
291 int sd_icmp6_nd_stop(sd_icmp6_nd *nd) {
292 assert_return(nd, -EINVAL);
293 assert_return(nd->event, -EINVAL);
295 log_icmp6_nd(client, "Stop ICMPv6");
299 nd->state = ICMP6_NEIGHBOR_DISCOVERY_IDLE;
304 int sd_icmp6_router_solicitation_start(sd_icmp6_nd *nd) {
310 if (nd->state != ICMP6_NEIGHBOR_DISCOVERY_IDLE)
316 r = dhcp_network_icmp6_bind_router_solicitation(nd->index);
322 r = sd_event_add_io(nd->event, &nd->recv, nd->fd, EPOLLIN,
323 icmp6_router_advertisment_recv, nd);
327 r = sd_event_source_set_priority(nd->recv, nd->event_priority);
331 r = sd_event_source_set_description(nd->recv, "icmp6-receive-message");
335 r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
336 0, 0, icmp6_router_solicitation_timeout, nd);
340 r = sd_event_source_set_priority(nd->timeout, nd->event_priority);
344 r = sd_event_source_set_description(nd->timeout, "icmp6-timeout");
349 log_icmp6_nd(client, "Start Router Solicitation");