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