]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-ndisc.c
sd-ndisc: don't inform the caller of expired prefixes
[thirdparty/systemd.git] / src / libsystemd-network / sd-ndisc.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2014 Intel Corporation. All rights reserved.
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <netinet/icmp6.h>
21 #include <netinet/ip6.h>
22 #include <string.h>
23 #include <stdbool.h>
24 #include <netinet/in.h>
25 #include <sys/ioctl.h>
26
27 #include "async.h"
28 #include "list.h"
29 #include "socket-util.h"
30
31 #include "icmp6-util.h"
32 #include "sd-ndisc.h"
33
34 #define NDISC_ROUTER_SOLICITATION_INTERVAL 4 * USEC_PER_SEC
35 #define NDISC_MAX_ROUTER_SOLICITATIONS 3
36
37 enum NDiscState {
38 NDISC_STATE_IDLE,
39 NDISC_STATE_SOLICITATION_SENT,
40 NDISC_STATE_ADVERTISMENT_LISTEN,
41 _NDISC_STATE_MAX,
42 _NDISC_STATE_INVALID = -1,
43 };
44
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
48
49 typedef struct NDiscPrefix NDiscPrefix;
50
51 struct NDiscPrefix {
52 unsigned n_ref;
53
54 LIST_FIELDS(NDiscPrefix, prefixes);
55
56 uint8_t len;
57 sd_event_source *timeout_valid;
58 struct in6_addr addr;
59 };
60
61 struct sd_ndisc {
62 unsigned n_ref;
63
64 enum NDiscState state;
65 sd_event *event;
66 int event_priority;
67 int index;
68 struct ether_addr mac_addr;
69 uint32_t mtu;
70 LIST_HEAD(NDiscPrefix, prefixes);
71 int fd;
72 sd_event_source *recv;
73 sd_event_source *timeout;
74 int nd_sent;
75 sd_ndisc_callback_t callback;
76 void *userdata;
77 };
78
79 #define log_ndisc(p, fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "NDisc CLIENT: " fmt, ##__VA_ARGS__)
80
81 static NDiscPrefix *ndisc_prefix_unref(NDiscPrefix *prefix) {
82
83 if (!prefix)
84 return NULL;
85
86 assert(prefix->n_ref > 0);
87 prefix->n_ref--;
88
89 if (prefix->n_ref > 0)
90 return NULL;
91
92 prefix->timeout_valid = sd_event_source_unref(prefix->timeout_valid);
93 free(prefix);
94 return NULL;
95 }
96
97 static int ndisc_prefix_new(NDiscPrefix **ret) {
98 _cleanup_free_ NDiscPrefix *prefix = NULL;
99
100 assert(ret);
101
102 prefix = new0(NDiscPrefix, 1);
103 if (!prefix)
104 return -ENOMEM;
105
106 prefix->n_ref = 1;
107 LIST_INIT(prefixes, prefix);
108
109 *ret = prefix;
110 prefix = NULL;
111
112 return 0;
113 }
114
115 static void ndisc_notify(sd_ndisc *nd, int event) {
116 if (nd->callback)
117 nd->callback(nd, event, nd->userdata);
118 }
119
120 int sd_ndisc_set_callback(sd_ndisc *nd, sd_ndisc_callback_t callback,
121 void *userdata) {
122 assert(nd);
123
124 nd->callback = callback;
125 nd->userdata = userdata;
126
127 return 0;
128 }
129
130 int sd_ndisc_set_index(sd_ndisc *nd, int interface_index) {
131 assert(nd);
132 assert(interface_index >= -1);
133
134 nd->index = interface_index;
135
136 return 0;
137 }
138
139 int sd_ndisc_set_mac(sd_ndisc *nd, const struct ether_addr *mac_addr) {
140 assert(nd);
141
142 if (mac_addr)
143 memcpy(&nd->mac_addr, mac_addr, sizeof(nd->mac_addr));
144 else
145 zero(nd->mac_addr);
146
147 return 0;
148
149 }
150
151 int sd_ndisc_attach_event(sd_ndisc *nd, sd_event *event, int priority) {
152 int r;
153
154 assert_return(nd, -EINVAL);
155 assert_return(!nd->event, -EBUSY);
156
157 if (event)
158 nd->event = sd_event_ref(event);
159 else {
160 r = sd_event_default(&nd->event);
161 if (r < 0)
162 return 0;
163 }
164
165 nd->event_priority = priority;
166
167 return 0;
168 }
169
170 int sd_ndisc_detach_event(sd_ndisc *nd) {
171 assert_return(nd, -EINVAL);
172
173 nd->event = sd_event_unref(nd->event);
174
175 return 0;
176 }
177
178 sd_event *sd_ndisc_get_event(sd_ndisc *nd) {
179 assert(nd);
180
181 return nd->event;
182 }
183
184 sd_ndisc *sd_ndisc_ref(sd_ndisc *nd) {
185
186 if (!nd)
187 return NULL;
188
189 assert(nd->n_ref > 0);
190 nd->n_ref++;
191
192 return nd;
193 }
194
195 static int ndisc_init(sd_ndisc *nd) {
196 assert(nd);
197
198 nd->recv = sd_event_source_unref(nd->recv);
199 nd->fd = asynchronous_close(nd->fd);
200 nd->timeout = sd_event_source_unref(nd->timeout);
201
202 return 0;
203 }
204
205 sd_ndisc *sd_ndisc_unref(sd_ndisc *nd) {
206 NDiscPrefix *prefix, *p;
207
208 if (!nd)
209 return NULL;
210
211 assert(nd->n_ref > 0);
212 nd->n_ref--;
213
214 if (nd->n_ref > 0)
215 return NULL;
216
217 ndisc_init(nd);
218 sd_ndisc_detach_event(nd);
219
220 LIST_FOREACH_SAFE(prefixes, prefix, p, nd->prefixes) {
221 LIST_REMOVE(prefixes, nd->prefixes, prefix);
222
223 prefix = ndisc_prefix_unref(prefix);
224 }
225
226 free(nd);
227
228 return NULL;
229 }
230
231 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_ndisc*, sd_ndisc_unref);
232 #define _cleanup_sd_ndisc_free_ _cleanup_(sd_ndisc_unrefp)
233
234 int sd_ndisc_new(sd_ndisc **ret) {
235 _cleanup_sd_ndisc_free_ sd_ndisc *nd = NULL;
236
237 assert(ret);
238
239 nd = new0(sd_ndisc, 1);
240 if (!nd)
241 return -ENOMEM;
242
243 nd->n_ref = 1;
244
245 nd->index = -1;
246 nd->fd = -1;
247
248 LIST_HEAD_INIT(nd->prefixes);
249
250 *ret = nd;
251 nd = NULL;
252
253 return 0;
254 }
255
256 int sd_ndisc_get_mtu(sd_ndisc *nd, uint32_t *mtu) {
257 assert_return(nd, -EINVAL);
258 assert_return(mtu, -EINVAL);
259
260 if (nd->mtu == 0)
261 return -ENOMSG;
262
263 *mtu = nd->mtu;
264
265 return 0;
266 }
267
268 static int ndisc_prefix_timeout(sd_event_source *s, uint64_t usec,
269 void *userdata) {
270 sd_ndisc *nd = userdata;
271 NDiscPrefix *prefix, *p;
272
273 assert(nd);
274
275 LIST_FOREACH_SAFE(prefixes, prefix, p, nd->prefixes) {
276 if (prefix->timeout_valid != s)
277 continue;
278
279 log_ndisc(nd, "Prefix expired "SD_NDISC_ADDRESS_FORMAT_STR"/%d",
280 SD_NDISC_ADDRESS_FORMAT_VAL(prefix->addr),
281 prefix->len);
282
283 LIST_REMOVE(prefixes, nd->prefixes, prefix);
284
285 prefix = ndisc_prefix_unref(prefix);
286
287 break;
288 }
289
290 return 0;
291 }
292
293 static int ndisc_prefix_set_timeout(sd_ndisc *nd,
294 NDiscPrefix *prefix,
295 usec_t valid) {
296 usec_t time_now;
297 int r;
298
299 assert_return(prefix, -EINVAL);
300
301 r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
302 if (r < 0)
303 return r;
304
305 prefix->timeout_valid = sd_event_source_unref(prefix->timeout_valid);
306
307 r = sd_event_add_time(nd->event, &prefix->timeout_valid,
308 clock_boottime_or_monotonic(), time_now + valid,
309 USEC_PER_SEC, ndisc_prefix_timeout, nd);
310 if (r < 0)
311 goto error;
312
313 r = sd_event_source_set_priority(prefix->timeout_valid,
314 nd->event_priority);
315 if (r < 0)
316 goto error;
317
318 r = sd_event_source_set_description(prefix->timeout_valid,
319 "ndisc-prefix-timeout");
320
321 error:
322 if (r < 0)
323 prefix->timeout_valid =
324 sd_event_source_unref(prefix->timeout_valid);
325
326 return r;
327 }
328
329 static int prefix_match(const struct in6_addr *prefix, uint8_t prefixlen,
330 const struct in6_addr *addr,
331 uint8_t addr_prefixlen) {
332 uint8_t bytes, mask, len;
333
334 assert_return(prefix, -EINVAL);
335 assert_return(addr, -EINVAL);
336
337 len = MIN(prefixlen, addr_prefixlen);
338
339 bytes = len / 8;
340 mask = 0xff << (8 - len % 8);
341
342 if (memcmp(prefix, addr, bytes) != 0 ||
343 (prefix->s6_addr[bytes] & mask) != (addr->s6_addr[bytes] & mask))
344 return -EADDRNOTAVAIL;
345
346 return 0;
347 }
348
349 static int ndisc_prefix_match(NDiscPrefix *head, const struct in6_addr *addr,
350 uint8_t addr_len, NDiscPrefix **result) {
351 NDiscPrefix *prefix;
352
353 LIST_FOREACH(prefixes, prefix, head) {
354 if (prefix_match(&prefix->addr, prefix->len, addr, addr_len) >= 0) {
355 *result = prefix;
356 return 0;
357 }
358 }
359
360 return -EADDRNOTAVAIL;
361 }
362
363 int sd_ndisc_prefix_match(struct in6_addr *prefix, uint8_t prefixlen,
364 struct in6_addr *addr) {
365 return prefix_match(prefix, prefixlen, addr, sizeof(addr->s6_addr) * 8);
366 }
367
368 int sd_ndisc_get_prefixlen(sd_ndisc *nd, const struct in6_addr *addr,
369 uint8_t *prefixlen) {
370 int r;
371 NDiscPrefix *prefix;
372
373 assert_return(nd, -EINVAL);
374 assert_return(addr, -EINVAL);
375 assert_return(prefixlen, -EINVAL);
376
377 r = ndisc_prefix_match(nd->prefixes, addr,
378 sizeof(addr->s6_addr) * 8, &prefix);
379 if (r < 0)
380 return r;
381
382 *prefixlen = prefix->len;
383
384 return 0;
385 }
386
387 static int ndisc_prefix_update(sd_ndisc *nd, ssize_t len,
388 const struct nd_opt_prefix_info *prefix_opt) {
389 int r;
390 NDiscPrefix *prefix;
391 uint32_t lifetime;
392 char time_string[FORMAT_TIMESPAN_MAX];
393
394 assert_return(nd, -EINVAL);
395 assert_return(prefix_opt, -EINVAL);
396
397 if (len < prefix_opt->nd_opt_pi_len)
398 return -ENOMSG;
399
400 if (!(prefix_opt->nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ONLINK))
401 return 0;
402
403 lifetime = be32toh(prefix_opt->nd_opt_pi_valid_time);
404
405 r = ndisc_prefix_match(nd->prefixes,
406 &prefix_opt->nd_opt_pi_prefix,
407 prefix_opt->nd_opt_pi_prefix_len, &prefix);
408
409 if (r < 0 && r != -EADDRNOTAVAIL)
410 return r;
411
412 /* if router advertisment prefix valid timeout is zero, the timeout
413 callback will be called immediately to clean up the prefix */
414
415 if (r == -EADDRNOTAVAIL) {
416 r = ndisc_prefix_new(&prefix);
417 if (r < 0)
418 return r;
419
420 prefix->len = prefix_opt->nd_opt_pi_prefix_len;
421
422 memcpy(&prefix->addr, &prefix_opt->nd_opt_pi_prefix,
423 sizeof(prefix->addr));
424
425 log_ndisc(nd, "New prefix "SD_NDISC_ADDRESS_FORMAT_STR"/%d lifetime %d expires in %s",
426 SD_NDISC_ADDRESS_FORMAT_VAL(prefix->addr),
427 prefix->len, lifetime,
428 format_timespan(time_string, FORMAT_TIMESPAN_MAX, lifetime * USEC_PER_SEC, USEC_PER_SEC));
429
430 LIST_PREPEND(prefixes, nd->prefixes, prefix);
431
432 } else {
433 if (prefix->len != prefix_opt->nd_opt_pi_prefix_len) {
434 uint8_t prefixlen;
435
436 prefixlen = MIN(prefix->len, prefix_opt->nd_opt_pi_prefix_len);
437
438 log_ndisc(nd, "Prefix length mismatch %d/%d using %d",
439 prefix->len,
440 prefix_opt->nd_opt_pi_prefix_len,
441 prefixlen);
442
443 prefix->len = prefixlen;
444 }
445
446 log_ndisc(nd, "Update prefix "SD_NDISC_ADDRESS_FORMAT_STR"/%d lifetime %d expires in %s",
447 SD_NDISC_ADDRESS_FORMAT_VAL(prefix->addr),
448 prefix->len, lifetime,
449 format_timespan(time_string, FORMAT_TIMESPAN_MAX, lifetime * USEC_PER_SEC, USEC_PER_SEC));
450 }
451
452 r = ndisc_prefix_set_timeout(nd, prefix, lifetime * USEC_PER_SEC);
453
454 return r;
455 }
456
457 static int ndisc_ra_parse(sd_ndisc *nd, struct nd_router_advert *ra,
458 ssize_t len) {
459 void *opt;
460 struct nd_opt_hdr *opt_hdr;
461
462 assert_return(nd, -EINVAL);
463 assert_return(ra, -EINVAL);
464
465 len -= sizeof(*ra);
466 if (len < NDISC_OPT_LEN_UNITS) {
467 log_ndisc(nd, "Router Advertisement below minimum length");
468
469 return -ENOMSG;
470 }
471
472 opt = ra + 1;
473 opt_hdr = opt;
474
475 while (len != 0 && len >= opt_hdr->nd_opt_len * NDISC_OPT_LEN_UNITS) {
476 struct nd_opt_mtu *opt_mtu;
477 uint32_t mtu;
478 struct nd_opt_prefix_info *opt_prefix;
479
480 if (opt_hdr->nd_opt_len == 0)
481 return -ENOMSG;
482
483 switch (opt_hdr->nd_opt_type) {
484 case ND_OPT_MTU:
485 opt_mtu = opt;
486
487 mtu = be32toh(opt_mtu->nd_opt_mtu_mtu);
488
489 if (mtu != nd->mtu) {
490 nd->mtu = MAX(mtu, IP6_MIN_MTU);
491
492 log_ndisc(nd, "Router Advertisement link MTU %d using %d",
493 mtu, nd->mtu);
494 }
495
496 break;
497
498 case ND_OPT_PREFIX_INFORMATION:
499 opt_prefix = opt;
500
501 ndisc_prefix_update(nd, len, opt_prefix);
502
503 break;
504 }
505
506 len -= opt_hdr->nd_opt_len * NDISC_OPT_LEN_UNITS;
507 opt = (void *)((char *)opt +
508 opt_hdr->nd_opt_len * NDISC_OPT_LEN_UNITS);
509 opt_hdr = opt;
510 }
511
512 if (len > 0)
513 log_ndisc(nd, "Router Advertisement contains %zd bytes of trailing garbage", len);
514
515 return 0;
516 }
517
518 static int ndisc_router_advertisment_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
519 sd_ndisc *nd = userdata;
520 int r, buflen = 0;
521 ssize_t len;
522 _cleanup_free_ struct nd_router_advert *ra = NULL;
523 int event = SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE;
524
525 assert(s);
526 assert(nd);
527 assert(nd->event);
528
529 r = ioctl(fd, FIONREAD, &buflen);
530 if (r < 0 || buflen <= 0)
531 buflen = ICMP6_RECV_SIZE;
532
533 ra = malloc(buflen);
534 if (!ra)
535 return -ENOMEM;
536
537 len = read(fd, ra, buflen);
538 if (len < 0) {
539 log_ndisc(nd, "Could not receive message from UDP socket: %m");
540 return 0;
541 }
542
543 if (ra->nd_ra_type != ND_ROUTER_ADVERT)
544 return 0;
545
546 if (ra->nd_ra_code != 0)
547 return 0;
548
549 nd->timeout = sd_event_source_unref(nd->timeout);
550
551 nd->state = NDISC_STATE_ADVERTISMENT_LISTEN;
552
553 if (ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER )
554 event = SD_NDISC_EVENT_ROUTER_ADVERTISMENT_OTHER;
555
556 if (ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED)
557 event = SD_NDISC_EVENT_ROUTER_ADVERTISMENT_MANAGED;
558
559 log_ndisc(nd, "Received Router Advertisement flags %s/%s",
560 ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED? "MANAGED": "none",
561 ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER? "OTHER": "none");
562
563 if (event != SD_NDISC_EVENT_ROUTER_ADVERTISMENT_NONE) {
564 r = ndisc_ra_parse(nd, ra, len);
565 if (r < 0) {
566 log_ndisc(nd, "Could not parse Router Advertisement: %s",
567 strerror(-r));
568 return 0;
569 }
570 }
571
572 ndisc_notify(nd, event);
573
574 return 0;
575 }
576
577 static int ndisc_router_solicitation_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
578 sd_ndisc *nd = userdata;
579 uint64_t time_now, next_timeout;
580 struct ether_addr unset = { };
581 struct ether_addr *addr = NULL;
582 int r;
583
584 assert(s);
585 assert(nd);
586 assert(nd->event);
587
588 nd->timeout = sd_event_source_unref(nd->timeout);
589
590 if (nd->nd_sent >= NDISC_MAX_ROUTER_SOLICITATIONS) {
591 ndisc_notify(nd, SD_NDISC_EVENT_ROUTER_ADVERTISMENT_TIMEOUT);
592 nd->state = NDISC_STATE_ADVERTISMENT_LISTEN;
593 } else {
594 if (memcmp(&nd->mac_addr, &unset, sizeof(struct ether_addr)))
595 addr = &nd->mac_addr;
596
597 r = icmp6_send_router_solicitation(nd->fd, addr);
598 if (r < 0)
599 log_ndisc(nd, "Error sending Router Solicitation");
600 else {
601 nd->state = NDISC_STATE_SOLICITATION_SENT;
602 log_ndisc(nd, "Sent Router Solicitation");
603 }
604
605 nd->nd_sent++;
606
607 r = sd_event_now(nd->event, clock_boottime_or_monotonic(), &time_now);
608 if (r < 0) {
609 ndisc_notify(nd, r);
610 return 0;
611 }
612
613 next_timeout = time_now + NDISC_ROUTER_SOLICITATION_INTERVAL;
614
615 r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
616 next_timeout, 0,
617 ndisc_router_solicitation_timeout, nd);
618 if (r < 0) {
619 ndisc_notify(nd, r);
620 return 0;
621 }
622
623 r = sd_event_source_set_priority(nd->timeout,
624 nd->event_priority);
625 if (r < 0) {
626 ndisc_notify(nd, r);
627 return 0;
628 }
629
630 r = sd_event_source_set_description(nd->timeout, "ndisc-timeout");
631 if (r < 0) {
632 ndisc_notify(nd, r);
633 return 0;
634 }
635 }
636
637 return 0;
638 }
639
640 int sd_ndisc_stop(sd_ndisc *nd) {
641 assert_return(nd, -EINVAL);
642 assert_return(nd->event, -EINVAL);
643
644 log_ndisc(client, "Stop NDisc");
645
646 ndisc_init(nd);
647
648 nd->state = NDISC_STATE_IDLE;
649
650 return 0;
651 }
652
653 int sd_ndisc_router_discovery_start(sd_ndisc *nd) {
654 int r;
655
656 assert(nd);
657 assert(nd->event);
658
659 if (nd->state != NDISC_STATE_IDLE)
660 return -EINVAL;
661
662 if (nd->index < 1)
663 return -EINVAL;
664
665 r = icmp6_bind_router_solicitation(nd->index);
666 if (r < 0)
667 return r;
668
669 nd->fd = r;
670
671 r = sd_event_add_io(nd->event, &nd->recv, nd->fd, EPOLLIN,
672 ndisc_router_advertisment_recv, nd);
673 if (r < 0)
674 goto error;
675
676 r = sd_event_source_set_priority(nd->recv, nd->event_priority);
677 if (r < 0)
678 goto error;
679
680 r = sd_event_source_set_description(nd->recv, "ndisc-receive-message");
681 if (r < 0)
682 goto error;
683
684 r = sd_event_add_time(nd->event, &nd->timeout, clock_boottime_or_monotonic(),
685 0, 0, ndisc_router_solicitation_timeout, nd);
686 if (r < 0)
687 goto error;
688
689 r = sd_event_source_set_priority(nd->timeout, nd->event_priority);
690 if (r < 0)
691 goto error;
692
693 r = sd_event_source_set_description(nd->timeout, "ndisc-timeout");
694 error:
695 if (r < 0)
696 ndisc_init(nd);
697 else
698 log_ndisc(client, "Start Router Solicitation");
699
700 return r;
701 }