]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-radv.c
sd-radv: do not use iterater outside of the loop
[thirdparty/systemd.git] / src / libsystemd-network / sd-radv.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2017 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <netinet/icmp6.h>
7 #include <netinet/in.h>
8 #include <arpa/inet.h>
9
10 #include "sd-radv.h"
11
12 #include "alloc-util.h"
13 #include "dns-domain.h"
14 #include "ether-addr-util.h"
15 #include "event-util.h"
16 #include "fd-util.h"
17 #include "icmp6-util.h"
18 #include "in-addr-util.h"
19 #include "io-util.h"
20 #include "macro.h"
21 #include "memory-util.h"
22 #include "network-common.h"
23 #include "radv-internal.h"
24 #include "random-util.h"
25 #include "socket-util.h"
26 #include "string-util.h"
27 #include "strv.h"
28
29 int sd_radv_new(sd_radv **ret) {
30 _cleanup_(sd_radv_unrefp) sd_radv *ra = NULL;
31
32 assert_return(ret, -EINVAL);
33
34 ra = new(sd_radv, 1);
35 if (!ra)
36 return -ENOMEM;
37
38 *ra = (sd_radv) {
39 .n_ref = 1,
40 .fd = -1,
41 .lifetime_usec = RADV_DEFAULT_ROUTER_LIFETIME_USEC,
42 };
43
44 *ret = TAKE_PTR(ra);
45
46 return 0;
47 }
48
49 int sd_radv_attach_event(sd_radv *ra, sd_event *event, int64_t priority) {
50 int r;
51
52 assert_return(ra, -EINVAL);
53 assert_return(!ra->event, -EBUSY);
54
55 if (event)
56 ra->event = sd_event_ref(event);
57 else {
58 r = sd_event_default(&ra->event);
59 if (r < 0)
60 return 0;
61 }
62
63 ra->event_priority = priority;
64
65 return 0;
66 }
67
68 int sd_radv_detach_event(sd_radv *ra) {
69
70 assert_return(ra, -EINVAL);
71
72 ra->event = sd_event_unref(ra->event);
73 return 0;
74 }
75
76 sd_event *sd_radv_get_event(sd_radv *ra) {
77 assert_return(ra, NULL);
78
79 return ra->event;
80 }
81
82 int sd_radv_is_running(sd_radv *ra) {
83 assert_return(ra, false);
84
85 return ra->state != RADV_STATE_IDLE;
86 }
87
88 static void radv_reset(sd_radv *ra) {
89 assert(ra);
90
91 (void) event_source_disable(ra->timeout_event_source);
92
93 ra->recv_event_source = sd_event_source_disable_unref(ra->recv_event_source);
94
95 ra->ra_sent = 0;
96 }
97
98 static sd_radv *radv_free(sd_radv *ra) {
99 if (!ra)
100 return NULL;
101
102 while (ra->prefixes) {
103 sd_radv_prefix *p = ra->prefixes;
104
105 LIST_REMOVE(prefix, ra->prefixes, p);
106 sd_radv_prefix_unref(p);
107 }
108
109 while (ra->route_prefixes) {
110 sd_radv_route_prefix *p = ra->route_prefixes;
111
112 LIST_REMOVE(prefix, ra->route_prefixes, p);
113 sd_radv_route_prefix_unref(p);
114 }
115
116 free(ra->rdnss);
117 free(ra->dnssl);
118
119 radv_reset(ra);
120
121 sd_event_source_unref(ra->timeout_event_source);
122 sd_radv_detach_event(ra);
123
124 ra->fd = safe_close(ra->fd);
125 free(ra->ifname);
126
127 return mfree(ra);
128 }
129
130 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv, sd_radv, radv_free);
131
132 static bool router_lifetime_is_valid(usec_t lifetime_usec) {
133 return lifetime_usec == 0 ||
134 (lifetime_usec >= RADV_MIN_ROUTER_LIFETIME_USEC &&
135 lifetime_usec <= RADV_MAX_ROUTER_LIFETIME_USEC);
136 }
137
138 static be32_t usec_to_be32_sec(usec_t usec) {
139 if (usec == USEC_INFINITY)
140 /* UINT32_MAX is handled as infinity. */
141 return htobe32(UINT32_MAX);
142
143 if (usec >= UINT32_MAX * USEC_PER_SEC)
144 /* Finite but too large. Let's use the largest finite value. */
145 return htobe32(UINT32_MAX - 1);
146
147 return htobe32(usec / USEC_PER_SEC);
148 }
149
150 static int radv_send(sd_radv *ra, const struct in6_addr *dst, usec_t lifetime_usec) {
151 sd_radv_route_prefix *rt;
152 sd_radv_prefix *p;
153 struct sockaddr_in6 dst_addr = {
154 .sin6_family = AF_INET6,
155 .sin6_addr = IN6ADDR_ALL_NODES_MULTICAST_INIT,
156 };
157 struct nd_router_advert adv = {};
158 struct {
159 struct nd_opt_hdr opthdr;
160 struct ether_addr slladdr;
161 } _packed_ opt_mac = {
162 .opthdr = {
163 .nd_opt_type = ND_OPT_SOURCE_LINKADDR,
164 .nd_opt_len = (sizeof(struct nd_opt_hdr) +
165 sizeof(struct ether_addr) - 1) /8 + 1,
166 },
167 };
168 struct nd_opt_mtu opt_mtu = {
169 .nd_opt_mtu_type = ND_OPT_MTU,
170 .nd_opt_mtu_len = 1,
171 };
172 /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, N routes, RDNSS
173 and DNSSL */
174 struct iovec iov[5 + ra->n_prefixes + ra->n_route_prefixes];
175 struct msghdr msg = {
176 .msg_name = &dst_addr,
177 .msg_namelen = sizeof(dst_addr),
178 .msg_iov = iov,
179 };
180 usec_t time_now;
181 int r;
182
183 assert(ra);
184 assert(router_lifetime_is_valid(lifetime_usec));
185
186 r = sd_event_now(ra->event, clock_boottime_or_monotonic(), &time_now);
187 if (r < 0)
188 return r;
189
190 if (dst && in6_addr_is_set(dst))
191 dst_addr.sin6_addr = *dst;
192
193 adv.nd_ra_type = ND_ROUTER_ADVERT;
194 adv.nd_ra_curhoplimit = ra->hop_limit;
195 adv.nd_ra_flags_reserved = ra->flags;
196 assert_cc(RADV_MAX_ROUTER_LIFETIME_USEC <= UINT16_MAX * USEC_PER_SEC);
197 adv.nd_ra_router_lifetime = htobe16(DIV_ROUND_UP(lifetime_usec, USEC_PER_SEC));
198 iov[msg.msg_iovlen++] = IOVEC_MAKE(&adv, sizeof(adv));
199
200 /* MAC address is optional, either because the link does not use L2
201 addresses or load sharing is desired. See RFC 4861, Section 4.2 */
202 if (!ether_addr_is_null(&ra->mac_addr)) {
203 opt_mac.slladdr = ra->mac_addr;
204 iov[msg.msg_iovlen++] = IOVEC_MAKE(&opt_mac, sizeof(opt_mac));
205 }
206
207 if (ra->mtu > 0) {
208 opt_mtu.nd_opt_mtu_mtu = htobe32(ra->mtu);
209 iov[msg.msg_iovlen++] = IOVEC_MAKE(&opt_mtu, sizeof(opt_mtu));
210 }
211
212 LIST_FOREACH(prefix, p, ra->prefixes) {
213 usec_t lifetime_valid_usec, lifetime_preferred_usec;
214
215 lifetime_valid_usec = MIN(usec_sub_unsigned(p->valid_until, time_now),
216 p->lifetime_valid_usec);
217
218 lifetime_preferred_usec = MIN3(usec_sub_unsigned(p->preferred_until, time_now),
219 p->lifetime_preferred_usec,
220 lifetime_valid_usec);
221
222 p->opt.lifetime_valid = usec_to_be32_sec(lifetime_valid_usec);
223 p->opt.lifetime_preferred = usec_to_be32_sec(lifetime_preferred_usec);
224
225 iov[msg.msg_iovlen++] = IOVEC_MAKE(&p->opt, sizeof(p->opt));
226 }
227
228 LIST_FOREACH(prefix, rt, ra->route_prefixes) {
229 rt->opt.lifetime = usec_to_be32_sec(MIN(usec_sub_unsigned(rt->valid_until, time_now),
230 rt->lifetime_usec));
231
232 iov[msg.msg_iovlen++] = IOVEC_MAKE(&rt->opt, sizeof(rt->opt));
233 }
234
235 if (ra->rdnss)
236 iov[msg.msg_iovlen++] = IOVEC_MAKE(ra->rdnss, ra->rdnss->length * 8);
237
238 if (ra->dnssl)
239 iov[msg.msg_iovlen++] = IOVEC_MAKE(ra->dnssl, ra->dnssl->length * 8);
240
241 if (sendmsg(ra->fd, &msg, 0) < 0)
242 return -errno;
243
244 return 0;
245 }
246
247 static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
248 sd_radv *ra = userdata;
249 _cleanup_free_ char *addr = NULL;
250 struct in6_addr src;
251 triple_timestamp timestamp;
252 int r;
253 ssize_t buflen;
254 _cleanup_free_ char *buf = NULL;
255
256 assert(s);
257 assert(ra);
258 assert(ra->event);
259
260 buflen = next_datagram_size_fd(fd);
261 if (buflen < 0) {
262 if (ERRNO_IS_TRANSIENT(buflen) || ERRNO_IS_DISCONNECT(buflen))
263 return 0;
264
265 log_radv_errno(ra, buflen, "Failed to determine datagram size to read, ignoring: %m");
266 return 0;
267 }
268
269 buf = new0(char, buflen);
270 if (!buf)
271 return -ENOMEM;
272
273 r = icmp6_receive(fd, buf, buflen, &src, &timestamp);
274 if (r < 0) {
275 if (ERRNO_IS_TRANSIENT(r) || ERRNO_IS_DISCONNECT(r))
276 return 0;
277
278 switch (r) {
279 case -EADDRNOTAVAIL:
280 (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &src, &addr);
281 log_radv(ra, "Received RS from non-link-local address %s. Ignoring", addr);
282 break;
283
284 case -EMULTIHOP:
285 log_radv(ra, "Received RS with invalid hop limit. Ignoring.");
286 break;
287
288 case -EPFNOSUPPORT:
289 log_radv(ra, "Received invalid source address from ICMPv6 socket. Ignoring.");
290 break;
291
292 default:
293 log_radv_errno(ra, r, "Unexpected error receiving from ICMPv6 socket, ignoring: %m");
294 break;
295 }
296
297 return 0;
298 }
299
300 if ((size_t) buflen < sizeof(struct nd_router_solicit)) {
301 log_radv(ra, "Too short packet received, ignoring");
302 return 0;
303 }
304
305 (void) in_addr_to_string(AF_INET6, (const union in_addr_union*) &src, &addr);
306
307 r = radv_send(ra, &src, ra->lifetime_usec);
308 if (r < 0)
309 log_radv_errno(ra, r, "Unable to send solicited Router Advertisement to %s, ignoring: %m", strnull(addr));
310 else
311 log_radv(ra, "Sent solicited Router Advertisement to %s", strnull(addr));
312
313 return 0;
314 }
315
316 static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
317 usec_t min_timeout, max_timeout, time_now, timeout;
318 sd_radv *ra = userdata;
319 int r;
320
321 assert(s);
322 assert(ra);
323 assert(ra->event);
324 assert(router_lifetime_is_valid(ra->lifetime_usec));
325
326 r = sd_event_now(ra->event, clock_boottime_or_monotonic(), &time_now);
327 if (r < 0)
328 goto fail;
329
330 r = radv_send(ra, NULL, ra->lifetime_usec);
331 if (r < 0)
332 log_radv_errno(ra, r, "Unable to send Router Advertisement: %m");
333
334 /* RFC 4861, Section 6.2.4, sending initial Router Advertisements */
335 if (ra->ra_sent < RADV_MAX_INITIAL_RTR_ADVERTISEMENTS)
336 max_timeout = RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC;
337 else
338 max_timeout = RADV_DEFAULT_MAX_TIMEOUT_USEC;
339
340 /* RFC 4861, Section 6.2.1, lifetime must be at least MaxRtrAdvInterval,
341 * so lower the interval here */
342 if (ra->lifetime_usec > 0)
343 max_timeout = MIN(max_timeout, ra->lifetime_usec);
344
345 if (max_timeout >= 9 * USEC_PER_SEC)
346 min_timeout = max_timeout / 3;
347 else
348 min_timeout = max_timeout * 3 / 4;
349
350 /* RFC 4861, Section 6.2.1.
351 * MaxRtrAdvInterval MUST be no less than 4 seconds and no greater than 1800 seconds.
352 * MinRtrAdvInterval MUST be no less than 3 seconds and no greater than .75 * MaxRtrAdvInterval. */
353 assert(max_timeout >= RADV_MIN_MAX_TIMEOUT_USEC);
354 assert(max_timeout <= RADV_MAX_MAX_TIMEOUT_USEC);
355 assert(min_timeout >= RADV_MIN_MIN_TIMEOUT_USEC);
356 assert(min_timeout <= max_timeout * 3 / 4);
357
358 timeout = min_timeout + random_u64_range(max_timeout - min_timeout);
359 log_radv(ra, "Next Router Advertisement in %s", FORMAT_TIMESPAN(timeout, USEC_PER_SEC));
360
361 r = event_reset_time(ra->event, &ra->timeout_event_source,
362 clock_boottime_or_monotonic(),
363 usec_add(time_now, timeout), MSEC_PER_SEC,
364 radv_timeout, ra,
365 ra->event_priority, "radv-timeout", true);
366 if (r < 0)
367 goto fail;
368
369 ra->ra_sent++;
370
371 return 0;
372
373 fail:
374 sd_radv_stop(ra);
375
376 return 0;
377 }
378
379 int sd_radv_stop(sd_radv *ra) {
380 int r;
381
382 if (!ra)
383 return 0;
384
385 if (ra->state == RADV_STATE_IDLE)
386 return 0;
387
388 log_radv(ra, "Stopping IPv6 Router Advertisement daemon");
389
390 /* RFC 4861, Section 6.2.5, send at least one Router Advertisement
391 with zero lifetime */
392 r = radv_send(ra, NULL, 0);
393 if (r < 0)
394 log_radv_errno(ra, r, "Unable to send last Router Advertisement with router lifetime set to zero: %m");
395
396 radv_reset(ra);
397 ra->fd = safe_close(ra->fd);
398 ra->state = RADV_STATE_IDLE;
399
400 return 0;
401 }
402
403 int sd_radv_start(sd_radv *ra) {
404 int r;
405
406 assert_return(ra, -EINVAL);
407 assert_return(ra->event, -EINVAL);
408 assert_return(ra->ifindex > 0, -EINVAL);
409
410 if (ra->state != RADV_STATE_IDLE)
411 return 0;
412
413 r = event_reset_time(ra->event, &ra->timeout_event_source,
414 clock_boottime_or_monotonic(),
415 0, 0,
416 radv_timeout, ra,
417 ra->event_priority, "radv-timeout", true);
418 if (r < 0)
419 goto fail;
420
421 r = icmp6_bind_router_advertisement(ra->ifindex);
422 if (r < 0)
423 goto fail;
424
425 ra->fd = r;
426
427 r = sd_event_add_io(ra->event, &ra->recv_event_source, ra->fd, EPOLLIN, radv_recv, ra);
428 if (r < 0)
429 goto fail;
430
431 r = sd_event_source_set_priority(ra->recv_event_source, ra->event_priority);
432 if (r < 0)
433 goto fail;
434
435 (void) sd_event_source_set_description(ra->recv_event_source, "radv-receive-message");
436
437 ra->state = RADV_STATE_ADVERTISING;
438
439 log_radv(ra, "Started IPv6 Router Advertisement daemon");
440
441 return 0;
442
443 fail:
444 radv_reset(ra);
445
446 return r;
447 }
448
449 int sd_radv_set_ifindex(sd_radv *ra, int ifindex) {
450 assert_return(ra, -EINVAL);
451 assert_return(ifindex > 0, -EINVAL);
452
453 if (ra->state != RADV_STATE_IDLE)
454 return -EBUSY;
455
456 ra->ifindex = ifindex;
457
458 return 0;
459 }
460
461 int sd_radv_set_ifname(sd_radv *ra, const char *ifname) {
462 assert_return(ra, -EINVAL);
463 assert_return(ifname, -EINVAL);
464
465 if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
466 return -EINVAL;
467
468 return free_and_strdup(&ra->ifname, ifname);
469 }
470
471 int sd_radv_get_ifname(sd_radv *ra, const char **ret) {
472 int r;
473
474 assert_return(ra, -EINVAL);
475
476 r = get_ifname(ra->ifindex, &ra->ifname);
477 if (r < 0)
478 return r;
479
480 if (ret)
481 *ret = ra->ifname;
482
483 return 0;
484 }
485
486 int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) {
487 assert_return(ra, -EINVAL);
488
489 if (ra->state != RADV_STATE_IDLE)
490 return -EBUSY;
491
492 if (mac_addr)
493 ra->mac_addr = *mac_addr;
494 else
495 zero(ra->mac_addr);
496
497 return 0;
498 }
499
500 int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu) {
501 assert_return(ra, -EINVAL);
502 assert_return(mtu >= 1280, -EINVAL);
503
504 ra->mtu = mtu;
505
506 return 0;
507 }
508
509 int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit) {
510 assert_return(ra, -EINVAL);
511
512 if (ra->state != RADV_STATE_IDLE)
513 return -EBUSY;
514
515 ra->hop_limit = hop_limit;
516
517 return 0;
518 }
519
520 int sd_radv_set_router_lifetime(sd_radv *ra, uint64_t lifetime_usec) {
521 assert_return(ra, -EINVAL);
522
523 if (ra->state != RADV_STATE_IDLE)
524 return -EBUSY;
525
526 if (!router_lifetime_is_valid(lifetime_usec))
527 return -EINVAL;
528
529 /* RFC 4191, Section 2.2, "...If the Router Lifetime is zero, the preference value MUST be set
530 * to (00) by the sender..." */
531 if (lifetime_usec == 0 &&
532 (ra->flags & (0x3 << 3)) != (SD_NDISC_PREFERENCE_MEDIUM << 3))
533 return -EINVAL;
534
535 ra->lifetime_usec = lifetime_usec;
536
537 return 0;
538 }
539
540 int sd_radv_set_managed_information(sd_radv *ra, int managed) {
541 assert_return(ra, -EINVAL);
542
543 if (ra->state != RADV_STATE_IDLE)
544 return -EBUSY;
545
546 SET_FLAG(ra->flags, ND_RA_FLAG_MANAGED, managed);
547
548 return 0;
549 }
550
551 int sd_radv_set_other_information(sd_radv *ra, int other) {
552 assert_return(ra, -EINVAL);
553
554 if (ra->state != RADV_STATE_IDLE)
555 return -EBUSY;
556
557 SET_FLAG(ra->flags, ND_RA_FLAG_OTHER, other);
558
559 return 0;
560 }
561
562 int sd_radv_set_preference(sd_radv *ra, unsigned preference) {
563 assert_return(ra, -EINVAL);
564 assert_return(IN_SET(preference,
565 SD_NDISC_PREFERENCE_LOW,
566 SD_NDISC_PREFERENCE_MEDIUM,
567 SD_NDISC_PREFERENCE_HIGH), -EINVAL);
568
569 /* RFC 4191, Section 2.2, "...If the Router Lifetime is zero, the preference value MUST be set
570 * to (00) by the sender..." */
571 if (ra->lifetime_usec == 0 && preference != SD_NDISC_PREFERENCE_MEDIUM)
572 return -EINVAL;
573
574 ra->flags = (ra->flags & ~(0x3 << 3)) | (preference << 3);
575
576 return 0;
577 }
578
579 int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
580 _cleanup_free_ char *addr_p = NULL;
581 sd_radv_prefix *cur, *found = NULL;
582 int r;
583
584 assert_return(ra, -EINVAL);
585 assert_return(p, -EINVAL);
586
587 /* Refuse prefixes that don't have a prefix set */
588 if (in6_addr_is_null(&p->opt.in6_addr))
589 return -ENOEXEC;
590
591 (void) in6_addr_prefix_to_string(&p->opt.in6_addr, p->opt.prefixlen, &addr_p);
592
593 LIST_FOREACH(prefix, cur, ra->prefixes) {
594
595 r = in_addr_prefix_intersect(AF_INET6,
596 (const union in_addr_union*) &cur->opt.in6_addr,
597 cur->opt.prefixlen,
598 (const union in_addr_union*) &p->opt.in6_addr,
599 p->opt.prefixlen);
600 if (r < 0)
601 return r;
602 if (r == 0)
603 continue;
604
605 if (cur->opt.prefixlen == p->opt.prefixlen) {
606 found = cur;
607 break;
608 }
609
610 _cleanup_free_ char *addr_cur = NULL;
611 (void) in6_addr_prefix_to_string(&cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur);
612 return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
613 "IPv6 prefix %s conflicts with %s, ignoring.",
614 strna(addr_p), strna(addr_cur));
615 }
616
617 if (found) {
618 /* p and cur may be equivalent. First increment the reference counter. */
619 sd_radv_prefix_ref(p);
620
621 /* Then, remove the old entry. */
622 LIST_REMOVE(prefix, ra->prefixes, found);
623 sd_radv_prefix_unref(found);
624
625 /* Finally, add the new entry. */
626 LIST_APPEND(prefix, ra->prefixes, p);
627
628 log_radv(ra, "Updated/replaced IPv6 prefix %s (preferred: %s, valid: %s)",
629 strna(addr_p),
630 FORMAT_TIMESPAN(p->lifetime_preferred_usec, USEC_PER_SEC),
631 FORMAT_TIMESPAN(p->lifetime_valid_usec, USEC_PER_SEC));
632 } else {
633 /* The prefix is new. Let's simply add it. */
634
635 sd_radv_prefix_ref(p);
636 LIST_APPEND(prefix, ra->prefixes, p);
637 ra->n_prefixes++;
638
639 log_radv(ra, "Added prefix %s", strna(addr_p));
640 }
641
642 if (ra->state == RADV_STATE_IDLE)
643 return 0;
644
645 if (ra->ra_sent == 0)
646 return 0;
647
648 /* If RAs have already been sent, send an RA immediately to announce the newly-added prefix */
649 r = radv_send(ra, NULL, ra->lifetime_usec);
650 if (r < 0)
651 log_radv_errno(ra, r, "Unable to send Router Advertisement for added prefix %s: %m",
652 strna(addr_p));
653 else
654 log_radv(ra, "Sent Router Advertisement for added/updated prefix %s.", strna(addr_p));
655
656 return 0;
657 }
658
659 void sd_radv_remove_prefix(
660 sd_radv *ra,
661 const struct in6_addr *prefix,
662 unsigned char prefixlen) {
663
664 sd_radv_prefix *cur;
665
666 if (!ra)
667 return;
668
669 if (!prefix)
670 return;
671
672 LIST_FOREACH(prefix, cur, ra->prefixes) {
673 if (prefixlen != cur->opt.prefixlen)
674 continue;
675
676 if (!in6_addr_equal(prefix, &cur->opt.in6_addr))
677 continue;
678
679 LIST_REMOVE(prefix, ra->prefixes, cur);
680 ra->n_prefixes--;
681 sd_radv_prefix_unref(cur);
682 return;
683 }
684 }
685
686 int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
687 _cleanup_free_ char *addr_p = NULL;
688 sd_radv_route_prefix *cur, *found = NULL;
689 int r;
690
691 assert_return(ra, -EINVAL);
692 assert_return(p, -EINVAL);
693
694 (void) in6_addr_prefix_to_string(&p->opt.in6_addr, p->opt.prefixlen, &addr_p);
695
696 LIST_FOREACH(prefix, cur, ra->route_prefixes) {
697
698 r = in_addr_prefix_intersect(AF_INET6,
699 (const union in_addr_union*) &cur->opt.in6_addr,
700 cur->opt.prefixlen,
701 (const union in_addr_union*) &p->opt.in6_addr,
702 p->opt.prefixlen);
703 if (r < 0)
704 return r;
705 if (r == 0)
706 continue;
707
708 if (cur->opt.prefixlen == p->opt.prefixlen) {
709 found = cur;
710 break;
711 }
712
713 _cleanup_free_ char *addr_cur = NULL;
714 (void) in6_addr_prefix_to_string(&cur->opt.in6_addr, cur->opt.prefixlen, &addr_cur);
715 return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
716 "IPv6 route prefix %s conflicts with %s, ignoring.",
717 strna(addr_p), strna(addr_cur));
718 }
719
720 if (found) {
721 /* p and cur may be equivalent. First increment the reference counter. */
722 sd_radv_route_prefix_ref(p);
723
724 /* Then, remove the old entry. */
725 LIST_REMOVE(prefix, ra->route_prefixes, found);
726 sd_radv_route_prefix_unref(found);
727
728 /* Finally, add the new entry. */
729 LIST_APPEND(prefix, ra->route_prefixes, p);
730
731 log_radv(ra, "Updated/replaced IPv6 route prefix %s (lifetime: %s)",
732 strna(addr_p),
733 FORMAT_TIMESPAN(p->lifetime_usec, USEC_PER_SEC));
734 } else {
735 /* The route prefix is new. Let's simply add it. */
736
737 sd_radv_route_prefix_ref(p);
738 LIST_APPEND(prefix, ra->route_prefixes, p);
739 ra->n_route_prefixes++;
740
741 log_radv(ra, "Added route prefix %s", strna(addr_p));
742 }
743
744 if (ra->state == RADV_STATE_IDLE)
745 return 0;
746
747 if (ra->ra_sent == 0)
748 return 0;
749
750 /* If RAs have already been sent, send an RA immediately to announce the newly-added route prefix */
751 r = radv_send(ra, NULL, ra->lifetime_usec);
752 if (r < 0)
753 log_radv_errno(ra, r, "Unable to send Router Advertisement for added route prefix %s: %m",
754 strna(addr_p));
755 else
756 log_radv(ra, "Sent Router Advertisement for added route prefix %s.", strna(addr_p));
757
758 return 0;
759 }
760
761 int sd_radv_set_rdnss(sd_radv *ra, uint32_t lifetime,
762 const struct in6_addr *dns, size_t n_dns) {
763 _cleanup_free_ struct sd_radv_opt_dns *opt_rdnss = NULL;
764 size_t len;
765
766 assert_return(ra, -EINVAL);
767 assert_return(n_dns < 128, -EINVAL);
768
769 if (!dns || n_dns == 0) {
770 ra->rdnss = mfree(ra->rdnss);
771 ra->n_rdnss = 0;
772
773 return 0;
774 }
775
776 len = sizeof(struct sd_radv_opt_dns) + sizeof(struct in6_addr) * n_dns;
777
778 opt_rdnss = malloc0(len);
779 if (!opt_rdnss)
780 return -ENOMEM;
781
782 opt_rdnss->type = RADV_OPT_RDNSS;
783 opt_rdnss->length = len / 8;
784 opt_rdnss->lifetime = htobe32(lifetime);
785
786 memcpy(opt_rdnss + 1, dns, n_dns * sizeof(struct in6_addr));
787
788 free_and_replace(ra->rdnss, opt_rdnss);
789
790 ra->n_rdnss = n_dns;
791
792 return 0;
793 }
794
795 int sd_radv_set_dnssl(sd_radv *ra, uint32_t lifetime,
796 char **search_list) {
797 _cleanup_free_ struct sd_radv_opt_dns *opt_dnssl = NULL;
798 size_t len = 0;
799 char **s;
800 uint8_t *p;
801
802 assert_return(ra, -EINVAL);
803
804 if (strv_isempty(search_list)) {
805 ra->dnssl = mfree(ra->dnssl);
806 return 0;
807 }
808
809 STRV_FOREACH(s, search_list)
810 len += strlen(*s) + 2;
811
812 len = (sizeof(struct sd_radv_opt_dns) + len + 7) & ~0x7;
813
814 opt_dnssl = malloc0(len);
815 if (!opt_dnssl)
816 return -ENOMEM;
817
818 opt_dnssl->type = RADV_OPT_DNSSL;
819 opt_dnssl->length = len / 8;
820 opt_dnssl->lifetime = htobe32(lifetime);
821
822 p = (uint8_t *)(opt_dnssl + 1);
823 len -= sizeof(struct sd_radv_opt_dns);
824
825 STRV_FOREACH(s, search_list) {
826 int r;
827
828 r = dns_name_to_wire_format(*s, p, len, false);
829 if (r < 0)
830 return r;
831
832 if (len < (size_t)r)
833 return -ENOBUFS;
834
835 p += r;
836 len -= r;
837 }
838
839 free_and_replace(ra->dnssl, opt_dnssl);
840
841 return 0;
842 }
843
844 int sd_radv_prefix_new(sd_radv_prefix **ret) {
845 sd_radv_prefix *p;
846
847 assert_return(ret, -EINVAL);
848
849 p = new(sd_radv_prefix, 1);
850 if (!p)
851 return -ENOMEM;
852
853 *p = (sd_radv_prefix) {
854 .n_ref = 1,
855
856 .opt.type = ND_OPT_PREFIX_INFORMATION,
857 .opt.length = (sizeof(p->opt) - 1)/8 + 1,
858 .opt.prefixlen = 64,
859
860 /* RFC 4861, Section 6.2.1 */
861 .opt.flags = ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO,
862
863 .lifetime_valid_usec = RADV_DEFAULT_VALID_LIFETIME_USEC,
864 .lifetime_preferred_usec = RADV_DEFAULT_PREFERRED_LIFETIME_USEC,
865 .valid_until = USEC_INFINITY,
866 .preferred_until = USEC_INFINITY,
867 };
868
869 *ret = p;
870 return 0;
871 }
872
873 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_prefix, sd_radv_prefix, mfree);
874
875 int sd_radv_prefix_set_prefix(sd_radv_prefix *p, const struct in6_addr *in6_addr,
876 unsigned char prefixlen) {
877 assert_return(p, -EINVAL);
878 assert_return(in6_addr, -EINVAL);
879
880 if (prefixlen < 3 || prefixlen > 128)
881 return -EINVAL;
882
883 if (prefixlen > 64)
884 /* unusual but allowed, log it */
885 log_radv(NULL, "Unusual prefix length %d greater than 64", prefixlen);
886
887 p->opt.in6_addr = *in6_addr;
888 p->opt.prefixlen = prefixlen;
889
890 return 0;
891 }
892
893 int sd_radv_prefix_get_prefix(sd_radv_prefix *p, struct in6_addr *ret_in6_addr,
894 unsigned char *ret_prefixlen) {
895 assert_return(p, -EINVAL);
896 assert_return(ret_in6_addr, -EINVAL);
897 assert_return(ret_prefixlen, -EINVAL);
898
899 *ret_in6_addr = p->opt.in6_addr;
900 *ret_prefixlen = p->opt.prefixlen;
901
902 return 0;
903 }
904
905 int sd_radv_prefix_set_onlink(sd_radv_prefix *p, int onlink) {
906 assert_return(p, -EINVAL);
907
908 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_ONLINK, onlink);
909
910 return 0;
911 }
912
913 int sd_radv_prefix_set_address_autoconfiguration(sd_radv_prefix *p,
914 int address_autoconfiguration) {
915 assert_return(p, -EINVAL);
916
917 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_AUTO, address_autoconfiguration);
918
919 return 0;
920 }
921
922 int sd_radv_prefix_set_valid_lifetime(sd_radv_prefix *p, uint64_t lifetime_usec, uint64_t valid_until) {
923 assert_return(p, -EINVAL);
924
925 p->lifetime_valid_usec = lifetime_usec;
926 p->valid_until = valid_until;
927
928 return 0;
929 }
930
931 int sd_radv_prefix_set_preferred_lifetime(sd_radv_prefix *p, uint64_t lifetime_usec, uint64_t valid_until) {
932 assert_return(p, -EINVAL);
933
934 p->lifetime_preferred_usec = lifetime_usec;
935 p->preferred_until = valid_until;
936
937 return 0;
938 }
939
940 int sd_radv_route_prefix_new(sd_radv_route_prefix **ret) {
941 sd_radv_route_prefix *p;
942
943 assert_return(ret, -EINVAL);
944
945 p = new(sd_radv_route_prefix, 1);
946 if (!p)
947 return -ENOMEM;
948
949 *p = (sd_radv_route_prefix) {
950 .n_ref = 1,
951
952 .opt.type = RADV_OPT_ROUTE_INFORMATION,
953 .opt.length = DIV_ROUND_UP(sizeof(p->opt), 8),
954 .opt.prefixlen = 64,
955
956 .lifetime_usec = RADV_DEFAULT_VALID_LIFETIME_USEC,
957 .valid_until = USEC_INFINITY,
958 };
959
960 *ret = p;
961 return 0;
962 }
963
964 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_route_prefix, sd_radv_route_prefix, mfree);
965
966 int sd_radv_route_prefix_set_prefix(sd_radv_route_prefix *p, const struct in6_addr *in6_addr,
967 unsigned char prefixlen) {
968 assert_return(p, -EINVAL);
969 assert_return(in6_addr, -EINVAL);
970
971 if (prefixlen > 128)
972 return -EINVAL;
973
974 if (prefixlen > 64)
975 /* unusual but allowed, log it */
976 log_radv(NULL, "Unusual prefix length %u greater than 64", prefixlen);
977
978 p->opt.in6_addr = *in6_addr;
979 p->opt.prefixlen = prefixlen;
980
981 return 0;
982 }
983
984 int sd_radv_route_prefix_set_lifetime(sd_radv_route_prefix *p, uint64_t lifetime_usec, uint64_t valid_until) {
985 assert_return(p, -EINVAL);
986
987 p->lifetime_usec = lifetime_usec;
988 p->valid_until = valid_until;
989
990 return 0;
991 }