1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/***
This file is part of systemd.
Copyright (C) 2014 Intel Corporation. All rights reserved.
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <netinet/icmp6.h>
#include <netinet/ip6.h>
#include <string.h>
#include <stdbool.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include "socket-util.h"
#include "refcnt.h"
#include "async.h"
#include "dhcp6-internal.h"
#include "sd-icmp6-nd.h"
#define ICMP6_ROUTER_SOLICITATION_INTERVAL 4 * USEC_PER_SEC
#define ICMP6_MAX_ROUTER_SOLICITATIONS 3
enum icmp6_nd_state {
ICMP6_NEIGHBOR_DISCOVERY_IDLE = 0,
ICMP6_ROUTER_SOLICITATION_SENT = 10,
ICMP6_ROUTER_ADVERTISMENT_LISTEN = 11,
};
#define IP6_MIN_MTU (unsigned)1280
#define ICMP6_ND_RECV_SIZE (IP6_MIN_MTU - sizeof(struct ip6_hdr))
#define ICMP6_OPT_LEN_UNITS 8
typedef struct ICMP6Prefix ICMP6Prefix;
struct ICMP6Prefix {
RefCount n_ref;
LIST_FIELDS(ICMP6Prefix, prefixes);
uint8_t len;
sd_event_source *timeout_valid;
struct in6_addr addr;
};
struct sd_icmp6_nd {
RefCount n_ref;
enum icmp6_nd_state state;
sd_event *event;
int event_priority;
int index;
struct ether_addr mac_addr;
uint32_t mtu;
LIST_HEAD(ICMP6Prefix, prefixes);
int fd;
sd_event_source *recv;
sd_event_source *timeout;
int nd_sent;
sd_icmp6_nd_callback_t callback;
void *userdata;
};
#define log_icmp6_nd(p, fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "ICMPv6 CLIENT: " fmt, ##__VA_ARGS__)
static ICMP6Prefix *icmp6_prefix_unref(ICMP6Prefix *prefix) {
if (prefix && REFCNT_DEC(prefix->n_ref) <= 0) {
prefix->timeout_valid =
sd_event_source_unref(prefix->timeout_valid);
free(prefix);
}
return NULL;
}
static int icmp6_prefix_new(ICMP6Prefix **ret) {
_cleanup_free_ ICMP6Prefix *prefix = NULL;
assert(ret);
prefix = new0(ICMP6Prefix, 1);
if (!prefix)
return -ENOMEM;
prefix->n_ref = REFCNT_INIT;
LIST_INIT(prefixes, prefix);
*ret = prefix;
prefix = NULL;
return 0;
}
static void icmp6_nd_notify(sd_icmp6_nd *nd, int event)
{
if (nd->callback)
nd->callback(nd, event, nd->userdata);
}
int sd_icmp6_nd_set_callback(sd_icmp6_nd *nd, sd_icmp6_nd_callback_t callback,
void *userdata) {
assert(nd);
nd->callback = callback;
nd->userdata = userdata;
return 0;
}
int sd_icmp6_nd_set_index(sd_icmp6_nd *nd, int interface_index) {
assert(nd);
assert(interface_index >= -1);
nd->index = interface_index;
return 0;
}
int sd_icmp6_nd_set_mac(sd_icmp6_nd *nd, const struct ether_addr *mac_addr) {
assert(nd);
if (mac_addr)
memcpy(&nd->mac_addr, mac_addr, sizeof(nd->mac_addr));
else
zero(nd->mac_addr);
return 0;
}
int sd_icmp6_nd_attach_event(sd_icmp6_nd *nd, sd_event *event, int priority) {
int r;
assert_return(nd, -EINVAL);
assert_return(!nd->event, -EBUSY);
if (event)
nd->event = sd_event_ref(event);
else {
r = sd_event_default(&nd->event);
if (r < 0)
return 0;
}
nd->event_priority = priority;
return 0;
}
int sd_icmp6_nd_detach_event(sd_icmp6_nd *nd) {
assert_return(nd, -EINVAL);
nd->event = sd_event_unref(nd->event);
return 0;
}
sd_event *sd_icmp6_nd_get_event(sd_icmp6_nd *nd) {
assert(nd);
return nd->event;
}
sd_icmp6_nd *sd_icmp6_nd_ref(sd_icmp6_nd *nd) {
assert (nd);
assert_se(REFCNT_INC(nd->n_ref) >= 2);
return nd;
}
static int icmp6_nd_init(sd_icmp6_nd *nd) {
assert(nd);
nd->recv = sd_event_source_unref(nd->recv);
nd->fd = asynchronous_close(nd->fd);
nd->timeout = sd_event_source_unref(nd->timeout);
return 0;
}
sd_icmp6_nd *sd_icmp6_nd_unref(sd_icmp6_nd *nd) {
if (nd && REFCNT_DEC(nd->n_ref) == 0) {
ICMP6Prefix *prefix, *p;
icmp6_nd_init(nd);
sd_icmp6_nd_detach_event(nd);
LIST_FOREACH_SAFE(prefixes, prefix, p, nd->prefixes) {
LIST_REMOVE(prefixes, nd->prefixes, prefix);
prefix = icmp6_prefix_unref(prefix);
}
free(nd);
}
return NULL;
}
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_icmp6_nd*, sd_icmp6_nd_unref);
#define _cleanup_sd_icmp6_nd_free_ _cleanup_(sd_icmp6_nd_unrefp)
int sd_icmp6_nd_new(sd_icmp6_nd **ret) {
_cleanup_sd_icmp6_nd_free_ sd_icmp6_nd *nd = NULL;
assert(ret);
nd = new0(sd_icmp6_nd, 1);
if (!nd)
return -ENOMEM;
nd->n_ref = REFCNT_INIT;
nd->index = -1;
nd->fd = -1;
LIST_HEAD_INIT(nd->prefixes);
*ret = nd;
nd = NULL;
return 0;
}
static int icmp6_ra_parse(sd_icmp6_nd *nd, struct nd_router_advert *ra,
ssize_t len) {
void *opt;
struct nd_opt_hdr *opt_hdr;
assert_return(nd, -EINVAL);
assert_return(ra, -EINVAL);
len -= sizeof(*ra);
if (len < ICMP6_OPT_LEN_UNITS) {
log_icmp6_nd(nd, "Router Advertisement below minimum length");
return -ENOMSG;
}
opt = ra + 1;
opt_hdr = opt;
while (len != 0 && len >= opt_hdr->nd_opt_len * ICMP6_OPT_LEN_UNITS) {
if (opt_hdr->nd_opt_len == 0)
return -ENOMSG;
switch (opt_hdr->nd_opt_type) {
}
len -= opt_hdr->nd_opt_len * ICMP6_OPT_LEN_UNITS;
opt = (void *)((char *)opt +
opt_hdr->nd_opt_len * ICMP6_OPT_LEN_UNITS);
opt_hdr = opt;
}
if (len > 0)
log_icmp6_nd(nd, "Router Advertisement contains %zd bytes of trailing garbage", len);
return 0;
}
static int icmp6_router_advertisment_recv(sd_event_source *s, int fd,
uint32_t revents, void *userdata)
{
sd_icmp6_nd *nd = userdata;
int r, buflen = 0;
ssize_t len;
_cleanup_free_ struct nd_router_advert *ra = NULL;
int event = ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE;
assert(s);
assert(nd);
assert(nd->event);
r = ioctl(fd, FIONREAD, &buflen);
if (r < 0 || buflen <= 0)
buflen = ICMP6_ND_RECV_SIZE;
ra = malloc(buflen);
if (!ra)
return -ENOMEM;
len = read(fd, ra, buflen);
if (len < 0) {
log_icmp6_nd(nd, "Could not receive message from UDP socket: %m");
return 0;
}
if (ra->nd_ra_type != ND_ROUTER_ADVERT)
return 0;
if (ra->nd_ra_code != 0)
return 0;
nd->timeout = sd_event_source_unref(nd->timeout);
nd->state = ICMP6_ROUTER_ADVERTISMENT_LISTEN;
if (ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER )
event = ICMP6_EVENT_ROUTER_ADVERTISMENT_OTHER;
if (ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED)
event = ICMP6_EVENT_ROUTER_ADVERTISMENT_MANAGED;
log_icmp6_nd(nd, "Received Router Advertisement flags %s/%s",
ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED? "MANAGED": "none",
ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER? "OTHER": "none");
if (event != ICMP6_EVENT_ROUTER_ADVERTISMENT_NONE) {
r = icmp6_ra_parse(nd, ra, len);
if (r < 0) {
log_icmp6_nd(nd, "Could not parse Router Advertisement: %s",
strerror(-r));
return 0;
}
}
icmp6_nd_notify(nd, event);
return 0;
}
static int icmp6_router_solicitation_timeout(sd_event_source *s, uint64_t usec,
void *userdata)
{
sd_icmp6_nd *nd = userdata;
uint64_t time_now, next_timeout;
struct ether_addr unset = { };
struct ether_addr *addr = NULL;
int r;
assert(s);
assert(nd);
assert(nd->event);
nd->timeout = sd_event_source_unref(nd->timeout);
if (nd->nd_sent >= ICMP6_MAX_ROUTER_SOLICITATIONS) {
icmp6_nd_notify(nd, ICMP6_EVENT_ROUTER_ADVERTISMENT_TIMEOUT);
nd->state = ICMP6_ROUTER_ADVERTISMENT_LISTEN;
} else {
if (memcmp(&nd->mac_addr, &unset, sizeof(struct ether_addr)))
addr = &nd->mac_addr;
r = dhcp_network_icmp6_send_router_solicitation(nd->fd, addr);
if (r < 0)
log_icmp6_nd(nd, "Error sending Router Solicitation");
else {
nd->state = ICMP6_ROUTER_SOLICITATION_SENT;
log_icmp6_nd(nd, "Sent Router Solicitation");
}
nd->nd_sent++;
r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
if (r < 0) {
icmp6_nd_notify(nd, r);
return 0;
}
next_timeout = time_now + ICMP6_ROUTER_SOLICITATION_INTERVAL;
r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
next_timeout, 0,
icmp6_router_solicitation_timeout, nd);
if (r < 0) {
icmp6_nd_notify(nd, r);
return 0;
}
r = sd_event_source_set_priority(nd->timeout,
nd->event_priority);
if (r < 0) {
icmp6_nd_notify(nd, r);
return 0;
}
r = sd_event_source_set_description(nd->timeout, "icmp6-timeout");
if (r < 0) {
icmp6_nd_notify(nd, r);
return 0;
}
}
return 0;
}
int sd_icmp6_nd_stop(sd_icmp6_nd *nd) {
assert_return(nd, -EINVAL);
assert_return(nd->event, -EINVAL);
log_icmp6_nd(client, "Stop ICMPv6");
icmp6_nd_init(nd);
nd->state = ICMP6_NEIGHBOR_DISCOVERY_IDLE;
return 0;
}
int sd_icmp6_router_solicitation_start(sd_icmp6_nd *nd) {
int r;
assert(nd);
assert(nd->event);
if (nd->state != ICMP6_NEIGHBOR_DISCOVERY_IDLE)
return -EINVAL;
if (nd->index < 1)
return -EINVAL;
r = dhcp_network_icmp6_bind_router_solicitation(nd->index);
if (r < 0)
return r;
nd->fd = r;
r = sd_event_add_io(nd->event, &nd->recv, nd->fd, EPOLLIN,
icmp6_router_advertisment_recv, nd);
if (r < 0)
goto error;
r = sd_event_source_set_priority(nd->recv, nd->event_priority);
if (r < 0)
goto error;
r = sd_event_source_set_description(nd->recv, "icmp6-receive-message");
if (r < 0)
goto error;
r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
0, 0, icmp6_router_solicitation_timeout, nd);
if (r < 0)
goto error;
r = sd_event_source_set_priority(nd->timeout, nd->event_priority);
if (r < 0)
goto error;
r = sd_event_source_set_description(nd->timeout, "icmp6-timeout");
error:
if (r < 0)
icmp6_nd_init(nd);
else
log_icmp6_nd(client, "Start Router Solicitation");
return r;
}
|