]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-radv.c
Merge pull request #6746 from yuwata/parse-empty-string
[thirdparty/systemd.git] / src / libsystemd-network / sd-radv.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2017 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/in.h>
22 #include <arpa/inet.h>
23 #include <linux/in6.h>
24
25 #include "sd-radv.h"
26
27 #include "macro.h"
28 #include "alloc-util.h"
29 #include "fd-util.h"
30 #include "icmp6-util.h"
31 #include "in-addr-util.h"
32 #include "radv-internal.h"
33 #include "socket-util.h"
34 #include "string-util.h"
35 #include "util.h"
36 #include "random-util.h"
37
38 _public_ int sd_radv_new(sd_radv **ret) {
39 _cleanup_(sd_radv_unrefp) sd_radv *ra = NULL;
40
41 assert_return(ret, -EINVAL);
42
43 ra = new0(sd_radv, 1);
44 if (!ra)
45 return -ENOMEM;
46
47 ra->n_ref = 1;
48 ra->fd = -1;
49
50 LIST_HEAD_INIT(ra->prefixes);
51
52 *ret = ra;
53 ra = NULL;
54
55 return 0;
56 }
57
58 _public_ int sd_radv_attach_event(sd_radv *ra, sd_event *event, int64_t priority) {
59 int r;
60
61 assert_return(ra, -EINVAL);
62 assert_return(!ra->event, -EBUSY);
63
64 if (event)
65 ra->event = sd_event_ref(event);
66 else {
67 r = sd_event_default(&ra->event);
68 if (r < 0)
69 return 0;
70 }
71
72 ra->event_priority = priority;
73
74 return 0;
75 }
76
77 _public_ int sd_radv_detach_event(sd_radv *ra) {
78
79 assert_return(ra, -EINVAL);
80
81 ra->event = sd_event_unref(ra->event);
82 return 0;
83 }
84
85 _public_ sd_event *sd_radv_get_event(sd_radv *ra) {
86 assert_return(ra, NULL);
87
88 return ra->event;
89 }
90
91 static void radv_reset(sd_radv *ra) {
92
93 ra->timeout_event_source =
94 sd_event_source_unref(ra->timeout_event_source);
95
96 ra->recv_event_source =
97 sd_event_source_unref(ra->recv_event_source);
98
99 ra->ra_sent = 0;
100 }
101
102 _public_ sd_radv *sd_radv_ref(sd_radv *ra) {
103 if (!ra)
104 return NULL;
105
106 assert(ra->n_ref > 0);
107 ra->n_ref++;
108
109 return ra;
110 }
111
112 _public_ sd_radv *sd_radv_unref(sd_radv *ra) {
113 if (!ra)
114 return NULL;
115
116 assert(ra->n_ref > 0);
117 ra->n_ref--;
118
119 if (ra->n_ref > 0)
120 return NULL;
121
122 while (ra->prefixes) {
123 sd_radv_prefix *p = ra->prefixes;
124
125 LIST_REMOVE(prefix, ra->prefixes, p);
126 sd_radv_prefix_unref(p);
127 }
128
129 free(ra->rdnss);
130
131 radv_reset(ra);
132
133 sd_radv_detach_event(ra);
134 return mfree(ra);
135 }
136
137 static int radv_send(sd_radv *ra, const struct in6_addr *dst,
138 const uint32_t router_lifetime) {
139 static const struct ether_addr mac_zero = {};
140 sd_radv_prefix *p;
141 struct sockaddr_in6 dst_addr = {
142 .sin6_family = AF_INET6,
143 .sin6_addr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
144 };
145 struct nd_router_advert adv = {};
146 struct {
147 struct nd_opt_hdr opthdr;
148 struct ether_addr slladdr;
149 } _packed_ opt_mac = {
150 .opthdr = {
151 .nd_opt_type = ND_OPT_SOURCE_LINKADDR,
152 .nd_opt_len = (sizeof(struct nd_opt_hdr) +
153 sizeof(struct ether_addr) - 1) /8 + 1,
154 },
155 };
156 struct nd_opt_mtu opt_mtu = {
157 .nd_opt_mtu_type = ND_OPT_MTU,
158 .nd_opt_mtu_len = 1,
159 };
160 /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, RDNSS */
161 struct iovec iov[4 + ra->n_prefixes];
162 struct msghdr msg = {
163 .msg_name = &dst_addr,
164 .msg_namelen = sizeof(dst_addr),
165 .msg_iov = iov,
166 };
167
168 if (dst && !in_addr_is_null(AF_INET6, (union in_addr_union*) dst))
169 dst_addr.sin6_addr = *dst;
170
171 adv.nd_ra_type = ND_ROUTER_ADVERT;
172 adv.nd_ra_curhoplimit = ra->hop_limit;
173 adv.nd_ra_flags_reserved = ra->flags;
174 adv.nd_ra_router_lifetime = htobe16(router_lifetime);
175 iov[msg.msg_iovlen].iov_base = &adv;
176 iov[msg.msg_iovlen].iov_len = sizeof(adv);
177 msg.msg_iovlen++;
178
179 /* MAC address is optional, either because the link does not use L2
180 addresses or load sharing is desired. See RFC 4861, Section 4.2 */
181 if (memcmp(&mac_zero, &ra->mac_addr, sizeof(mac_zero))) {
182 opt_mac.slladdr = ra->mac_addr;
183 iov[msg.msg_iovlen].iov_base = &opt_mac;
184 iov[msg.msg_iovlen].iov_len = sizeof(opt_mac);
185 msg.msg_iovlen++;
186 }
187
188 if (ra->mtu) {
189 opt_mtu.nd_opt_mtu_mtu = htobe32(ra->mtu);
190 iov[msg.msg_iovlen].iov_base = &opt_mtu;
191 iov[msg.msg_iovlen].iov_len = sizeof(opt_mtu);
192 msg.msg_iovlen++;
193 }
194
195 LIST_FOREACH(prefix, p, ra->prefixes) {
196 iov[msg.msg_iovlen].iov_base = &p->opt;
197 iov[msg.msg_iovlen].iov_len = sizeof(p->opt);
198 msg.msg_iovlen++;
199 }
200
201 if (ra->rdnss) {
202 iov[msg.msg_iovlen].iov_base = ra->rdnss;
203 iov[msg.msg_iovlen].iov_len = ra->rdnss->length * 8;
204 msg.msg_iovlen++;
205 }
206
207 if (sendmsg(ra->fd, &msg, 0) < 0)
208 return -errno;
209
210 return 0;
211 }
212
213 static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
214 sd_radv *ra = userdata;
215 _cleanup_free_ char *addr = NULL;
216 struct in6_addr src;
217 triple_timestamp timestamp;
218 int r;
219 ssize_t buflen;
220 _cleanup_free_ char *buf = NULL;
221
222 assert(s);
223 assert(ra);
224 assert(ra->event);
225
226 buflen = next_datagram_size_fd(fd);
227
228 if ((unsigned) buflen < sizeof(struct nd_router_solicit))
229 return log_radv("Too short packet received");
230
231 buf = new0(char, buflen);
232 if (!buf)
233 return 0;
234
235 r = icmp6_receive(fd, buf, buflen, &src, &timestamp);
236 if (r < 0) {
237 switch (r) {
238 case -EADDRNOTAVAIL:
239 (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &src, &addr);
240 log_radv("Received RS from non-link-local address %s. Ignoring", addr);
241 break;
242
243 case -EMULTIHOP:
244 log_radv("Received RS with invalid hop limit. Ignoring.");
245 break;
246
247 case -EPFNOSUPPORT:
248 log_radv("Received invalid source address from ICMPv6 socket. Ignoring.");
249 break;
250
251 default:
252 log_radv_warning_errno(r, "Error receiving from ICMPv6 socket: %m");
253 break;
254 }
255
256 return 0;
257 }
258
259 (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &src, &addr);
260
261 r = radv_send(ra, &src, ra->lifetime);
262 if (r < 0)
263 log_radv_warning_errno(r, "Unable to send solicited Router Advertisment to %s: %m", addr);
264 else
265 log_radv("Sent solicited Router Advertisement to %s", addr);
266
267 return 0;
268 }
269
270 static usec_t radv_compute_timeout(usec_t min, usec_t max) {
271 assert_return(min <= max, SD_RADV_DEFAULT_MIN_TIMEOUT_USEC);
272
273 return min + (random_u32() % (max - min));
274 }
275
276 static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
277 int r;
278 sd_radv *ra = userdata;
279 usec_t min_timeout = SD_RADV_DEFAULT_MIN_TIMEOUT_USEC;
280 usec_t max_timeout = SD_RADV_DEFAULT_MAX_TIMEOUT_USEC;
281 usec_t time_now, timeout;
282 char time_string[FORMAT_TIMESPAN_MAX];
283
284 assert(s);
285 assert(ra);
286 assert(ra->event);
287
288 ra->timeout_event_source = sd_event_source_unref(ra->timeout_event_source);
289
290 r = sd_event_now(ra->event, clock_boottime_or_monotonic(), &time_now);
291 if (r < 0)
292 goto fail;
293
294 r = radv_send(ra, NULL, ra->lifetime);
295 if (r < 0)
296 log_radv_warning_errno(r, "Unable to send Router Advertisement: %m");
297
298 /* RFC 4861, Section 6.2.4, sending initial Router Advertisements */
299 if (ra->ra_sent < SD_RADV_MAX_INITIAL_RTR_ADVERTISEMENTS) {
300 max_timeout = SD_RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC;
301 min_timeout = SD_RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC / 3;
302 }
303
304 timeout = radv_compute_timeout(min_timeout, max_timeout);
305
306 log_radv("Next Router Advertisement in %s",
307 format_timespan(time_string, FORMAT_TIMESPAN_MAX,
308 timeout, USEC_PER_SEC));
309
310 r = sd_event_add_time(ra->event, &ra->timeout_event_source,
311 clock_boottime_or_monotonic(),
312 time_now + timeout, MSEC_PER_SEC,
313 radv_timeout, ra);
314 if (r < 0)
315 goto fail;
316
317 r = sd_event_source_set_priority(ra->timeout_event_source,
318 ra->event_priority);
319 if (r < 0)
320 goto fail;
321
322 r = sd_event_source_set_description(ra->timeout_event_source,
323 "radv-timeout");
324 if (r < 0)
325 goto fail;
326
327 ra->ra_sent++;
328
329 fail:
330 if (r < 0)
331 sd_radv_stop(ra);
332
333 return 0;
334 }
335
336 _public_ int sd_radv_stop(sd_radv *ra) {
337 int r;
338
339 assert_return(ra, -EINVAL);
340
341 log_radv("Stopping IPv6 Router Advertisement daemon");
342
343 /* RFC 4861, Section 6.2.5, send at least one Router Advertisement
344 with zero lifetime */
345 r = radv_send(ra, NULL, 0);
346 if (r < 0)
347 log_radv_warning_errno(r, "Unable to send last Router Advertisement with router lifetime set to zero: %m");
348
349 radv_reset(ra);
350 ra->fd = safe_close(ra->fd);
351 ra->state = SD_RADV_STATE_IDLE;
352
353 return 0;
354 }
355
356 _public_ int sd_radv_start(sd_radv *ra) {
357 int r = 0;
358
359 assert_return(ra, -EINVAL);
360 assert_return(ra->event, -EINVAL);
361 assert_return(ra->ifindex > 0, -EINVAL);
362
363 if (ra->state != SD_RADV_STATE_IDLE)
364 return 0;
365
366 r = sd_event_add_time(ra->event, &ra->timeout_event_source,
367 clock_boottime_or_monotonic(), 0, 0,
368 radv_timeout, ra);
369 if (r < 0)
370 goto fail;
371
372 r = sd_event_source_set_priority(ra->timeout_event_source,
373 ra->event_priority);
374 if (r < 0)
375 goto fail;
376
377 (void) sd_event_source_set_description(ra->timeout_event_source,
378 "radv-timeout");
379
380 r = icmp6_bind_router_advertisement(ra->ifindex);
381 if (r < 0)
382 goto fail;
383
384 ra->fd = r;
385
386 r = sd_event_add_io(ra->event, &ra->recv_event_source, ra->fd, EPOLLIN, radv_recv, ra);
387 if (r < 0)
388 goto fail;
389
390 r = sd_event_source_set_priority(ra->recv_event_source, ra->event_priority);
391 if (r < 0)
392 goto fail;
393
394 (void) sd_event_source_set_description(ra->recv_event_source, "radv-receive-message");
395
396 ra->state = SD_RADV_STATE_ADVERTISING;
397
398 log_radv("Started IPv6 Router Advertisement daemon");
399
400 return 0;
401
402 fail:
403 radv_reset(ra);
404
405 return r;
406 }
407
408 _public_ int sd_radv_set_ifindex(sd_radv *ra, int ifindex) {
409 assert_return(ra, -EINVAL);
410 assert_return(ifindex >= -1, -EINVAL);
411
412 if (ra->state != SD_RADV_STATE_IDLE)
413 return -EBUSY;
414
415 ra->ifindex = ifindex;
416
417 return 0;
418 }
419
420 _public_ int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) {
421 assert_return(ra, -EINVAL);
422
423 if (ra->state != SD_RADV_STATE_IDLE)
424 return -EBUSY;
425
426 if (mac_addr)
427 ra->mac_addr = *mac_addr;
428 else
429 zero(ra->mac_addr);
430
431 return 0;
432 }
433
434 _public_ int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu) {
435 assert_return(ra, -EINVAL);
436 assert_return(mtu >= 1280, -EINVAL);
437
438 if (ra->state != SD_RADV_STATE_IDLE)
439 return -EBUSY;
440
441 ra->mtu = mtu;
442
443 return 0;
444 }
445
446 _public_ int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit) {
447 assert_return(ra, -EINVAL);
448
449 if (ra->state != SD_RADV_STATE_IDLE)
450 return -EBUSY;
451
452 ra->hop_limit = hop_limit;
453
454 return 0;
455 }
456
457 _public_ int sd_radv_set_router_lifetime(sd_radv *ra, uint32_t router_lifetime) {
458 assert_return(ra, -EINVAL);
459
460 if (ra->state != SD_RADV_STATE_IDLE)
461 return -EBUSY;
462
463 /* RFC 4191, Section 2.2, "...If the Router Lifetime is zero, the
464 preference value MUST be set to (00) by the sender..." */
465 if (router_lifetime == 0 &&
466 (ra->flags & (0x3 << 3)) != (SD_NDISC_PREFERENCE_MEDIUM << 3))
467 return -ETIME;
468
469 ra->lifetime = router_lifetime;
470
471 return 0;
472 }
473
474 _public_ int sd_radv_set_managed_information(sd_radv *ra, int managed) {
475 assert_return(ra, -EINVAL);
476
477 if (ra->state != SD_RADV_STATE_IDLE)
478 return -EBUSY;
479
480 SET_FLAG(ra->flags, ND_RA_FLAG_MANAGED, managed);
481
482 return 0;
483 }
484
485 _public_ int sd_radv_set_other_information(sd_radv *ra, int other) {
486 assert_return(ra, -EINVAL);
487
488 if (ra->state != SD_RADV_STATE_IDLE)
489 return -EBUSY;
490
491 SET_FLAG(ra->flags, ND_RA_FLAG_OTHER, other);
492
493 return 0;
494 }
495
496 _public_ int sd_radv_set_preference(sd_radv *ra, unsigned preference) {
497 int r = 0;
498
499 assert_return(ra, -EINVAL);
500 assert_return(IN_SET(preference,
501 SD_NDISC_PREFERENCE_LOW,
502 SD_NDISC_PREFERENCE_MEDIUM,
503 SD_NDISC_PREFERENCE_HIGH), -EINVAL);
504
505 ra->flags = (ra->flags & ~(0x3 << 3)) | (preference << 3);
506
507 return r;
508 }
509
510 _public_ int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
511 sd_radv_prefix *cur;
512 _cleanup_free_ char *addr_p = NULL;
513
514 assert_return(ra, -EINVAL);
515
516 if (!p)
517 return -EINVAL;
518
519 LIST_FOREACH(prefix, cur, ra->prefixes) {
520 int r;
521
522 r = in_addr_prefix_intersect(AF_INET6,
523 (union in_addr_union*) &cur->opt.in6_addr,
524 cur->opt.prefixlen,
525 (union in_addr_union*) &p->opt.in6_addr,
526 p->opt.prefixlen);
527 if (r > 0) {
528 _cleanup_free_ char *addr_cur = NULL;
529
530 (void) in_addr_to_string(AF_INET6,
531 (union in_addr_union*) &cur->opt.in6_addr,
532 &addr_cur);
533 (void) in_addr_to_string(AF_INET6,
534 (union in_addr_union*) &p->opt.in6_addr,
535 &addr_p);
536
537 log_radv("IPv6 prefix %s/%u already configured, ignoring %s/%u",
538 addr_cur, cur->opt.prefixlen,
539 addr_p, p->opt.prefixlen);
540
541 return -EEXIST;
542 }
543 }
544
545 p = sd_radv_prefix_ref(p);
546
547 LIST_APPEND(prefix, ra->prefixes, p);
548
549 ra->n_prefixes++;
550
551 (void) in_addr_to_string(AF_INET6, (union in_addr_union*) &p->opt.in6_addr, &addr_p);
552 log_radv("Added prefix %s/%d", addr_p, p->opt.prefixlen);
553
554 return 0;
555 }
556
557 _public_ int sd_radv_set_rdnss(sd_radv *ra, uint32_t lifetime,
558 const struct in6_addr *dns, size_t n_dns) {
559 _cleanup_free_ struct sd_radv_opt_dns *opt_rdnss = NULL;
560 size_t len;
561
562 assert_return(ra, -EINVAL);
563 assert_return(n_dns < 128, -EINVAL);
564
565 if (!dns || n_dns == 0) {
566 ra->rdnss = mfree(ra->rdnss);
567 ra->n_rdnss = 0;
568
569 return 0;
570 }
571
572 len = sizeof(struct sd_radv_opt_dns) + sizeof(struct in6_addr) * n_dns;
573
574 opt_rdnss = malloc0(len);
575 if (!opt_rdnss)
576 return -ENOMEM;
577
578 opt_rdnss->type = SD_RADV_OPT_RDNSS;
579 opt_rdnss->length = len / 8;
580 opt_rdnss->lifetime = htobe32(lifetime);
581
582 memcpy(opt_rdnss + 1, dns, n_dns * sizeof(struct in6_addr));
583
584 free(ra->rdnss);
585 ra->rdnss = opt_rdnss;
586 opt_rdnss = NULL;
587
588 ra->n_rdnss = n_dns;
589
590 return 0;
591 }
592
593 _public_ int sd_radv_prefix_new(sd_radv_prefix **ret) {
594 _cleanup_(sd_radv_prefix_unrefp) sd_radv_prefix *p = NULL;
595
596 assert_return(ret, -EINVAL);
597
598 p = new0(sd_radv_prefix, 1);
599 if (!p)
600 return -ENOMEM;
601
602 p->n_ref = 1;
603
604 p->opt.type = ND_OPT_PREFIX_INFORMATION;
605 p->opt.length = (sizeof(p->opt) - 1) /8 + 1;
606
607 p->opt.prefixlen = 64;
608
609 /* RFC 4861, Section 6.2.1 */
610 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_ONLINK, true);
611 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_AUTO, true);
612 p->opt.preferred_lifetime = htobe32(604800);
613 p->opt.valid_lifetime = htobe32(2592000);
614
615 LIST_INIT(prefix, p);
616
617 *ret = p;
618 p = NULL;
619
620 return 0;
621 }
622
623 _public_ sd_radv_prefix *sd_radv_prefix_ref(sd_radv_prefix *p) {
624 if (!p)
625 return NULL;
626
627 assert(p->n_ref > 0);
628 p->n_ref++;
629
630 return p;
631 }
632
633 _public_ sd_radv_prefix *sd_radv_prefix_unref(sd_radv_prefix *p) {
634 if (!p)
635 return NULL;
636
637 assert(p->n_ref > 0);
638 p->n_ref--;
639
640 if (p->n_ref > 0)
641 return NULL;
642
643 return mfree(p);
644 }
645
646 _public_ int sd_radv_prefix_set_prefix(sd_radv_prefix *p, struct in6_addr *in6_addr,
647 unsigned char prefixlen) {
648 assert_return(p, -EINVAL);
649 assert_return(in6_addr, -EINVAL);
650
651 if (prefixlen < 3 || prefixlen > 128)
652 return -EINVAL;
653
654 if (prefixlen > 64)
655 /* unusual but allowed, log it */
656 log_radv("Unusual prefix length %d greater than 64", prefixlen);
657
658 p->opt.in6_addr = *in6_addr;
659 p->opt.prefixlen = prefixlen;
660
661 return 0;
662 }
663
664 _public_ int sd_radv_prefix_set_onlink(sd_radv_prefix *p, int onlink) {
665 assert_return(p, -EINVAL);
666
667 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_ONLINK, onlink);
668
669 return 0;
670 }
671
672 _public_ int sd_radv_prefix_set_address_autoconfiguration(sd_radv_prefix *p,
673 int address_autoconfiguration) {
674 assert_return(p, -EINVAL);
675
676 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_AUTO, address_autoconfiguration);
677
678 return 0;
679 }
680
681 _public_ int sd_radv_prefix_set_valid_lifetime(sd_radv_prefix *p,
682 uint32_t valid_lifetime) {
683 assert_return(p, -EINVAL);
684
685 p->opt.valid_lifetime = htobe32(valid_lifetime);
686
687 return 0;
688 }
689
690 _public_ int sd_radv_prefix_set_preferred_lifetime(sd_radv_prefix *p,
691 uint32_t preferred_lifetime) {
692 assert_return(p, -EINVAL);
693
694 p->opt.preferred_lifetime = htobe32(preferred_lifetime);
695
696 return 0;
697 }