]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/sd-radv.c
sd-radv: allow to send multiple routes or prefix64 that have intersection with others
[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 "iovec-util.h"
20 #include "macro.h"
21 #include "memory-util.h"
22 #include "ndisc-router-solicit-internal.h"
23 #include "network-common.h"
24 #include "radv-internal.h"
25 #include "random-util.h"
26 #include "socket-util.h"
27 #include "string-util.h"
28 #include "strv.h"
29 #include "unaligned.h"
30
31 int sd_radv_new(sd_radv **ret) {
32 _cleanup_(sd_radv_unrefp) sd_radv *ra = NULL;
33
34 assert_return(ret, -EINVAL);
35
36 ra = new(sd_radv, 1);
37 if (!ra)
38 return -ENOMEM;
39
40 *ra = (sd_radv) {
41 .n_ref = 1,
42 .fd = -EBADF,
43 .lifetime_usec = RADV_DEFAULT_ROUTER_LIFETIME_USEC,
44 };
45
46 *ret = TAKE_PTR(ra);
47
48 return 0;
49 }
50
51 int sd_radv_attach_event(sd_radv *ra, sd_event *event, int64_t priority) {
52 int r;
53
54 assert_return(ra, -EINVAL);
55 assert_return(!ra->event, -EBUSY);
56
57 if (event)
58 ra->event = sd_event_ref(event);
59 else {
60 r = sd_event_default(&ra->event);
61 if (r < 0)
62 return 0;
63 }
64
65 ra->event_priority = priority;
66
67 return 0;
68 }
69
70 int sd_radv_detach_event(sd_radv *ra) {
71
72 assert_return(ra, -EINVAL);
73
74 ra->event = sd_event_unref(ra->event);
75 return 0;
76 }
77
78 sd_event *sd_radv_get_event(sd_radv *ra) {
79 assert_return(ra, NULL);
80
81 return ra->event;
82 }
83
84 int sd_radv_is_running(sd_radv *ra) {
85 if (!ra)
86 return false;
87
88 return ra->state != RADV_STATE_IDLE;
89 }
90
91 static void radv_reset(sd_radv *ra) {
92 assert(ra);
93
94 (void) event_source_disable(ra->timeout_event_source);
95
96 ra->recv_event_source = sd_event_source_disable_unref(ra->recv_event_source);
97
98 ra->ra_sent = 0;
99 }
100
101 static sd_radv *radv_free(sd_radv *ra) {
102 if (!ra)
103 return NULL;
104
105 LIST_CLEAR(prefix, ra->prefixes, sd_radv_prefix_unref);
106 LIST_CLEAR(prefix, ra->route_prefixes, sd_radv_route_prefix_unref);
107 LIST_CLEAR(prefix, ra->pref64_prefixes, sd_radv_pref64_prefix_unref);
108
109 free(ra->rdnss);
110 free(ra->dnssl);
111
112 radv_reset(ra);
113
114 sd_event_source_unref(ra->timeout_event_source);
115 sd_radv_detach_event(ra);
116
117 ra->fd = safe_close(ra->fd);
118 free(ra->ifname);
119
120 return mfree(ra);
121 }
122
123 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv, sd_radv, radv_free);
124
125 static bool router_lifetime_is_valid(usec_t lifetime_usec) {
126 assert_cc(RADV_MAX_ROUTER_LIFETIME_USEC <= UINT16_MAX * USEC_PER_SEC);
127 return lifetime_usec == 0 ||
128 (lifetime_usec >= RADV_MIN_ROUTER_LIFETIME_USEC &&
129 lifetime_usec <= RADV_MAX_ROUTER_LIFETIME_USEC);
130 }
131
132 static int radv_send_router_on_stop(sd_radv *ra) {
133 static const struct nd_router_advert adv = {
134 .nd_ra_type = ND_ROUTER_ADVERT,
135 };
136
137 _cleanup_set_free_ Set *options = NULL;
138 usec_t time_now;
139 int r;
140
141 assert(ra);
142
143 r = sd_event_now(ra->event, CLOCK_BOOTTIME, &time_now);
144 if (r < 0)
145 return r;
146
147 if (!ether_addr_is_null(&ra->mac_addr)) {
148 r = ndisc_option_set_link_layer_address(&options, SD_NDISC_OPTION_SOURCE_LL_ADDRESS, &ra->mac_addr);
149 if (r < 0)
150 return r;
151 }
152
153 return ndisc_send(ra->fd, &IN6_ADDR_ALL_NODES_MULTICAST, &adv.nd_ra_hdr, options, time_now);
154 }
155
156 static int radv_send_router(sd_radv *ra, const struct in6_addr *dst) {
157 assert(ra);
158
159 struct sockaddr_in6 dst_addr = {
160 .sin6_family = AF_INET6,
161 .sin6_addr = IN6_ADDR_ALL_NODES_MULTICAST,
162 };
163 struct nd_router_advert adv = {
164 .nd_ra_type = ND_ROUTER_ADVERT,
165 .nd_ra_router_lifetime = usec_to_be16_sec(ra->lifetime_usec),
166 .nd_ra_reachable = usec_to_be32_msec(ra->reachable_usec),
167 .nd_ra_retransmit = usec_to_be32_msec(ra->retransmit_usec),
168 };
169 struct {
170 struct nd_opt_hdr opthdr;
171 struct ether_addr slladdr;
172 } _packed_ opt_mac = {
173 .opthdr = {
174 .nd_opt_type = ND_OPT_SOURCE_LINKADDR,
175 .nd_opt_len = DIV_ROUND_UP(sizeof(struct nd_opt_hdr) + sizeof(struct ether_addr), 8),
176 },
177 .slladdr = ra->mac_addr,
178 };
179 struct nd_opt_mtu opt_mtu = {
180 .nd_opt_mtu_type = ND_OPT_MTU,
181 .nd_opt_mtu_len = 1,
182 .nd_opt_mtu_mtu = htobe32(ra->mtu),
183 };
184 /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, N routes, N pref64 prefixes, RDNSS,
185 * DNSSL, and home agent. */
186 struct iovec iov[6 + ra->n_prefixes + ra->n_route_prefixes + ra->n_pref64_prefixes];
187 struct msghdr msg = {
188 .msg_name = &dst_addr,
189 .msg_namelen = sizeof(dst_addr),
190 .msg_iov = iov,
191 };
192 usec_t time_now;
193 int r;
194
195 r = sd_event_now(ra->event, CLOCK_BOOTTIME, &time_now);
196 if (r < 0)
197 return r;
198
199 if (dst && in6_addr_is_set(dst))
200 dst_addr.sin6_addr = *dst;
201
202 /* The nd_ra_curhoplimit and nd_ra_flags_reserved fields cannot specified with nd_ra_router_lifetime
203 * simultaneously in the structured initializer in the above. */
204 adv.nd_ra_curhoplimit = ra->hop_limit;
205 /* RFC 4191, Section 2.2,
206 * "...If the Router Lifetime is zero, the preference value MUST be set to (00) by the sender..." */
207 adv.nd_ra_flags_reserved = ra->flags | (ra->lifetime_usec > 0 ? (ra->preference << 3) : 0);
208 iov[msg.msg_iovlen++] = IOVEC_MAKE(&adv, sizeof(adv));
209
210 /* MAC address is optional, either because the link does not use L2 addresses or load sharing is
211 * desired. See RFC 4861, Section 4.2. */
212 if (!ether_addr_is_null(&ra->mac_addr))
213 iov[msg.msg_iovlen++] = IOVEC_MAKE(&opt_mac, sizeof(opt_mac));
214
215 if (ra->mtu > 0)
216 iov[msg.msg_iovlen++] = IOVEC_MAKE(&opt_mtu, sizeof(opt_mtu));
217
218 LIST_FOREACH(prefix, p, ra->prefixes) {
219 usec_t lifetime_valid_usec, lifetime_preferred_usec;
220
221 lifetime_valid_usec = MIN(usec_sub_unsigned(p->valid_until, time_now),
222 p->lifetime_valid_usec);
223
224 lifetime_preferred_usec = MIN3(usec_sub_unsigned(p->preferred_until, time_now),
225 p->lifetime_preferred_usec,
226 lifetime_valid_usec);
227
228 p->opt.lifetime_valid = usec_to_be32_sec(lifetime_valid_usec);
229 p->opt.lifetime_preferred = usec_to_be32_sec(lifetime_preferred_usec);
230
231 iov[msg.msg_iovlen++] = IOVEC_MAKE(&p->opt, sizeof(p->opt));
232 }
233
234 LIST_FOREACH(prefix, rt, ra->route_prefixes) {
235 rt->opt.lifetime = usec_to_be32_sec(MIN(usec_sub_unsigned(rt->valid_until, time_now),
236 rt->lifetime_usec));
237
238 iov[msg.msg_iovlen++] = IOVEC_MAKE(&rt->opt, sizeof(rt->opt));
239 }
240
241 LIST_FOREACH(prefix, p, ra->pref64_prefixes)
242 iov[msg.msg_iovlen++] = IOVEC_MAKE(&p->opt, sizeof(p->opt));
243
244 if (ra->rdnss)
245 iov[msg.msg_iovlen++] = IOVEC_MAKE(ra->rdnss, ra->rdnss->length * 8);
246
247 if (ra->dnssl)
248 iov[msg.msg_iovlen++] = IOVEC_MAKE(ra->dnssl, ra->dnssl->length * 8);
249
250 if (FLAGS_SET(ra->flags, ND_RA_FLAG_HOME_AGENT)) {
251 ra->home_agent.nd_opt_home_agent_info_type = ND_OPT_HOME_AGENT_INFO;
252 ra->home_agent.nd_opt_home_agent_info_len = 1;
253
254 /* 0 means to place the current Router Lifetime value */
255 if (ra->home_agent.nd_opt_home_agent_info_lifetime == 0)
256 ra->home_agent.nd_opt_home_agent_info_lifetime = adv.nd_ra_router_lifetime;
257
258 iov[msg.msg_iovlen++] = IOVEC_MAKE(&ra->home_agent, sizeof(ra->home_agent));
259 }
260
261 if (sendmsg(ra->fd, &msg, 0) < 0)
262 return -errno;
263
264 return 0;
265 }
266
267 static int radv_process_packet(sd_radv *ra, ICMP6Packet *packet) {
268 int r;
269
270 assert(ra);
271 assert(packet);
272
273 if (icmp6_packet_get_type(packet) != ND_ROUTER_SOLICIT)
274 return log_radv_errno(ra, SYNTHETIC_ERRNO(EBADMSG), "Received ICMP6 packet with unexpected type, ignoring.");
275
276 _cleanup_(sd_ndisc_router_solicit_unrefp) sd_ndisc_router_solicit *rs = NULL;
277 rs = ndisc_router_solicit_new(packet);
278 if (!rs)
279 return log_oom_debug();
280
281 r = ndisc_router_solicit_parse(ra, rs);
282 if (r < 0)
283 return r;
284
285 struct in6_addr src;
286 r = sd_ndisc_router_solicit_get_sender_address(rs, &src);
287 if (r == -ENODATA) /* null address is allowed */
288 return sd_radv_send(ra); /* When an unsolicited RA, we need to also update timer. */
289 if (r < 0)
290 return log_radv_errno(ra, r, "Failed to get sender address of RS, ignoring: %m");
291 if (in6_addr_equal(&src, &ra->ipv6ll))
292 /* This should be definitely caused by a misconfiguration. If we send RA to ourself, the
293 * kernel complains about that. Let's ignore the packet. */
294 return log_radv_errno(ra, SYNTHETIC_ERRNO(EADDRINUSE), "Received RS from the same interface, ignoring.");
295
296 r = radv_send_router(ra, &src);
297 if (r < 0)
298 return log_radv_errno(ra, r, "Unable to send solicited Router Advertisement to %s, ignoring: %m", IN6_ADDR_TO_STRING(&src));
299
300 log_radv(ra, "Sent solicited Router Advertisement to %s.", IN6_ADDR_TO_STRING(&src));
301 return 0;
302 }
303
304 static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
305 _cleanup_(icmp6_packet_unrefp) ICMP6Packet *packet = NULL;
306 sd_radv *ra = ASSERT_PTR(userdata);
307 int r;
308
309 assert(fd >= 0);
310
311 r = icmp6_packet_receive(fd, &packet);
312 if (r < 0) {
313 log_radv_errno(ra, r, "Failed to receive ICMPv6 packet, ignoring: %m");
314 return 0;
315 }
316
317 (void) radv_process_packet(ra, packet);
318 return 0;
319 }
320
321 static int radv_timeout(sd_event_source *s, uint64_t usec, void *userdata) {
322 sd_radv *ra = ASSERT_PTR(userdata);
323
324 if (sd_radv_send(ra) < 0)
325 (void) sd_radv_stop(ra);
326
327 return 0;
328 }
329
330 int sd_radv_send(sd_radv *ra) {
331 usec_t min_timeout, max_timeout, time_now, timeout;
332 int r;
333
334 assert_return(ra, -EINVAL);
335 assert_return(ra->event, -EINVAL);
336 assert_return(sd_radv_is_running(ra), -EINVAL);
337 assert(router_lifetime_is_valid(ra->lifetime_usec));
338
339 r = sd_event_now(ra->event, CLOCK_BOOTTIME, &time_now);
340 if (r < 0)
341 return r;
342
343 r = radv_send_router(ra, NULL);
344 if (r < 0)
345 return log_radv_errno(ra, r, "Unable to send Router Advertisement: %m");
346
347 ra->ra_sent++;
348
349 /* RFC 4861, Section 6.2.4, sending initial Router Advertisements */
350 if (ra->ra_sent <= RADV_MAX_INITIAL_RTR_ADVERTISEMENTS)
351 max_timeout = RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC;
352 else
353 max_timeout = RADV_DEFAULT_MAX_TIMEOUT_USEC;
354
355 /* RFC 4861, Section 6.2.1, lifetime must be at least MaxRtrAdvInterval,
356 * so lower the interval here */
357 if (ra->lifetime_usec > 0)
358 max_timeout = MIN(max_timeout, ra->lifetime_usec);
359
360 if (max_timeout >= 9 * USEC_PER_SEC)
361 min_timeout = max_timeout / 3;
362 else
363 min_timeout = max_timeout * 3 / 4;
364
365 /* RFC 4861, Section 6.2.1.
366 * MaxRtrAdvInterval MUST be no less than 4 seconds and no greater than 1800 seconds.
367 * MinRtrAdvInterval MUST be no less than 3 seconds and no greater than .75 * MaxRtrAdvInterval. */
368 assert(max_timeout >= RADV_MIN_MAX_TIMEOUT_USEC);
369 assert(max_timeout <= RADV_MAX_MAX_TIMEOUT_USEC);
370 assert(min_timeout >= RADV_MIN_MIN_TIMEOUT_USEC);
371 assert(min_timeout <= max_timeout * 3 / 4);
372
373 timeout = min_timeout + random_u64_range(max_timeout - min_timeout);
374 log_radv(ra, "Sent unsolicited Router Advertisement. Next advertisement will be in %s.",
375 FORMAT_TIMESPAN(timeout, USEC_PER_SEC));
376
377 return event_reset_time(
378 ra->event, &ra->timeout_event_source,
379 CLOCK_BOOTTIME,
380 usec_add(time_now, timeout), MSEC_PER_SEC,
381 radv_timeout, ra,
382 ra->event_priority, "radv-timeout", true);
383 }
384
385 int sd_radv_stop(sd_radv *ra) {
386 int r;
387
388 if (!sd_radv_is_running(ra))
389 return 0; /* Already stopped. */
390
391 log_radv(ra, "Stopping IPv6 Router Advertisement daemon");
392
393 /* RFC 4861, Section 6.2.5:
394 * the router SHOULD transmit one or more (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final
395 * multicast Router Advertisements on the interface with a Router Lifetime field of zero. */
396 r = radv_send_router_on_stop(ra);
397 if (r < 0)
398 log_radv_errno(ra, r, "Unable to send last Router Advertisement with router lifetime set to zero, ignoring: %m");
399
400 radv_reset(ra);
401 ra->fd = safe_close(ra->fd);
402 ra->state = RADV_STATE_IDLE;
403
404 return 0;
405 }
406
407 static int radv_setup_recv_event(sd_radv *ra) {
408 int r;
409
410 assert(ra);
411 assert(ra->event);
412 assert(ra->ifindex > 0);
413
414 _cleanup_close_ int fd = -EBADF;
415 fd = icmp6_bind(ra->ifindex, /* is_router = */ true);
416 if (fd < 0)
417 return fd;
418
419 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
420 r = sd_event_add_io(ra->event, &s, fd, EPOLLIN, radv_recv, ra);
421 if (r < 0)
422 return r;
423
424 r = sd_event_source_set_priority(s, ra->event_priority);
425 if (r < 0)
426 return r;
427
428 (void) sd_event_source_set_description(s, "radv-receive-message");
429
430 ra->fd = TAKE_FD(fd);
431 ra->recv_event_source = TAKE_PTR(s);
432 return 0;
433 }
434
435 int sd_radv_start(sd_radv *ra) {
436 int r;
437
438 assert_return(ra, -EINVAL);
439 assert_return(ra->event, -EINVAL);
440 assert_return(ra->ifindex > 0, -EINVAL);
441
442 if (sd_radv_is_running(ra))
443 return 0; /* Already started. */
444
445 r = radv_setup_recv_event(ra);
446 if (r < 0)
447 goto fail;
448
449 r = event_reset_time(ra->event, &ra->timeout_event_source,
450 CLOCK_BOOTTIME,
451 0, 0,
452 radv_timeout, ra,
453 ra->event_priority, "radv-timeout", true);
454 if (r < 0)
455 goto fail;
456
457 ra->state = RADV_STATE_ADVERTISING;
458
459 log_radv(ra, "Started IPv6 Router Advertisement daemon");
460
461 return 0;
462
463 fail:
464 radv_reset(ra);
465
466 return r;
467 }
468
469 int sd_radv_set_ifindex(sd_radv *ra, int ifindex) {
470 assert_return(ra, -EINVAL);
471 assert_return(!sd_radv_is_running(ra), -EBUSY);
472 assert_return(ifindex > 0, -EINVAL);
473
474 ra->ifindex = ifindex;
475 return 0;
476 }
477
478 int sd_radv_set_ifname(sd_radv *ra, const char *ifname) {
479 assert_return(ra, -EINVAL);
480 assert_return(ifname, -EINVAL);
481
482 if (!ifname_valid_full(ifname, IFNAME_VALID_ALTERNATIVE))
483 return -EINVAL;
484
485 return free_and_strdup(&ra->ifname, ifname);
486 }
487
488 int sd_radv_get_ifname(sd_radv *ra, const char **ret) {
489 int r;
490
491 assert_return(ra, -EINVAL);
492
493 r = get_ifname(ra->ifindex, &ra->ifname);
494 if (r < 0)
495 return r;
496
497 if (ret)
498 *ret = ra->ifname;
499
500 return 0;
501 }
502
503 int sd_radv_set_link_local_address(sd_radv *ra, const struct in6_addr *addr) {
504 assert_return(ra, -EINVAL);
505 assert_return(!addr || in6_addr_is_link_local(addr), -EINVAL);
506
507 if (addr)
508 ra->ipv6ll = *addr;
509 else
510 zero(ra->ipv6ll);
511
512 return 0;
513 }
514
515 int sd_radv_set_mac(sd_radv *ra, const struct ether_addr *mac_addr) {
516 assert_return(ra, -EINVAL);
517
518 if (mac_addr)
519 ra->mac_addr = *mac_addr;
520 else
521 zero(ra->mac_addr);
522
523 return 0;
524 }
525
526 int sd_radv_set_mtu(sd_radv *ra, uint32_t mtu) {
527 assert_return(ra, -EINVAL);
528 assert_return(mtu >= 1280, -EINVAL);
529
530 ra->mtu = mtu;
531
532 return 0;
533 }
534
535 int sd_radv_set_hop_limit(sd_radv *ra, uint8_t hop_limit) {
536 assert_return(ra, -EINVAL);
537
538 ra->hop_limit = hop_limit;
539 return 0;
540 }
541
542 int sd_radv_set_reachable_time(sd_radv *ra, uint64_t usec) {
543 assert_return(ra, -EINVAL);
544
545 ra->reachable_usec = usec;
546 return 0;
547 }
548
549 int sd_radv_set_retransmit(sd_radv *ra, uint64_t usec) {
550 assert_return(ra, -EINVAL);
551
552 ra->retransmit_usec = usec;
553 return 0;
554 }
555
556 int sd_radv_set_router_lifetime(sd_radv *ra, uint64_t usec) {
557 assert_return(ra, -EINVAL);
558
559 if (!router_lifetime_is_valid(usec))
560 return -EINVAL;
561
562 ra->lifetime_usec = usec;
563 return 0;
564 }
565
566 int sd_radv_set_managed_information(sd_radv *ra, int b) {
567 assert_return(ra, -EINVAL);
568
569 SET_FLAG(ra->flags, ND_RA_FLAG_MANAGED, b);
570 return 0;
571 }
572
573 int sd_radv_set_other_information(sd_radv *ra, int b) {
574 assert_return(ra, -EINVAL);
575
576 SET_FLAG(ra->flags, ND_RA_FLAG_OTHER, b);
577 return 0;
578 }
579
580 int sd_radv_set_preference(sd_radv *ra, uint8_t preference) {
581 assert_return(ra, -EINVAL);
582 assert_return(IN_SET(preference,
583 SD_NDISC_PREFERENCE_LOW,
584 SD_NDISC_PREFERENCE_MEDIUM,
585 SD_NDISC_PREFERENCE_HIGH), -EINVAL);
586
587 ra->preference = preference;
588 return 0;
589 }
590
591 int sd_radv_set_home_agent_information(sd_radv *ra, int home_agent) {
592 assert_return(ra, -EINVAL);
593
594 SET_FLAG(ra->flags, ND_RA_FLAG_HOME_AGENT, home_agent);
595 return 0;
596 }
597
598 int sd_radv_set_home_agent_preference(sd_radv *ra, uint16_t preference) {
599 assert_return(ra, -EINVAL);
600
601 ra->home_agent.nd_opt_home_agent_info_preference = htobe16(preference);
602 return 0;
603 }
604
605 int sd_radv_set_home_agent_lifetime(sd_radv *ra, uint64_t lifetime_usec) {
606 assert_return(ra, -EINVAL);
607
608 if (lifetime_usec > RADV_HOME_AGENT_MAX_LIFETIME_USEC)
609 return -EINVAL;
610
611 ra->home_agent.nd_opt_home_agent_info_lifetime = usec_to_be16_sec(lifetime_usec);
612 return 0;
613 }
614
615 int sd_radv_add_prefix(sd_radv *ra, sd_radv_prefix *p) {
616 sd_radv_prefix *found = NULL;
617 int r;
618
619 assert_return(ra, -EINVAL);
620 assert_return(p, -EINVAL);
621
622 /* Refuse prefixes that don't have a prefix set */
623 if (in6_addr_is_null(&p->opt.in6_addr))
624 return -ENOEXEC;
625
626 const char *addr_p = IN6_ADDR_PREFIX_TO_STRING(&p->opt.in6_addr, p->opt.prefixlen);
627
628 LIST_FOREACH(prefix, cur, ra->prefixes) {
629 r = in_addr_prefix_intersect(AF_INET6,
630 (const union in_addr_union*) &cur->opt.in6_addr,
631 cur->opt.prefixlen,
632 (const union in_addr_union*) &p->opt.in6_addr,
633 p->opt.prefixlen);
634 if (r < 0)
635 return r;
636 if (r == 0)
637 continue;
638
639 if (cur->opt.prefixlen == p->opt.prefixlen) {
640 found = cur;
641 break;
642 }
643
644 return log_radv_errno(ra, SYNTHETIC_ERRNO(EEXIST),
645 "IPv6 prefix %s conflicts with %s, ignoring.",
646 addr_p,
647 IN6_ADDR_PREFIX_TO_STRING(&cur->opt.in6_addr, cur->opt.prefixlen));
648 }
649
650 if (found) {
651 /* p and cur may be equivalent. First increment the reference counter. */
652 sd_radv_prefix_ref(p);
653
654 /* Then, remove the old entry. */
655 LIST_REMOVE(prefix, ra->prefixes, found);
656 sd_radv_prefix_unref(found);
657
658 /* Finally, add the new entry. */
659 LIST_APPEND(prefix, ra->prefixes, p);
660
661 log_radv(ra, "Updated/replaced IPv6 prefix %s (preferred: %s, valid: %s)",
662 addr_p,
663 FORMAT_TIMESPAN(p->lifetime_preferred_usec, USEC_PER_SEC),
664 FORMAT_TIMESPAN(p->lifetime_valid_usec, USEC_PER_SEC));
665 } else {
666 /* The prefix is new. Let's simply add it. */
667
668 sd_radv_prefix_ref(p);
669 LIST_APPEND(prefix, ra->prefixes, p);
670 ra->n_prefixes++;
671
672 log_radv(ra, "Added prefix %s", addr_p);
673 }
674
675 return 0;
676 }
677
678 void sd_radv_remove_prefix(
679 sd_radv *ra,
680 const struct in6_addr *prefix,
681 unsigned char prefixlen) {
682
683 if (!ra)
684 return;
685
686 if (!prefix)
687 return;
688
689 LIST_FOREACH(prefix, cur, ra->prefixes) {
690 if (prefixlen != cur->opt.prefixlen)
691 continue;
692
693 if (!in6_addr_equal(prefix, &cur->opt.in6_addr))
694 continue;
695
696 LIST_REMOVE(prefix, ra->prefixes, cur);
697 ra->n_prefixes--;
698 sd_radv_prefix_unref(cur);
699 return;
700 }
701 }
702
703 int sd_radv_add_route_prefix(sd_radv *ra, sd_radv_route_prefix *p) {
704 sd_radv_route_prefix *found = NULL;
705
706 assert_return(ra, -EINVAL);
707 assert_return(p, -EINVAL);
708
709 LIST_FOREACH(prefix, cur, ra->route_prefixes)
710 if (cur->opt.prefixlen == p->opt.prefixlen &&
711 in6_addr_equal(&cur->opt.in6_addr, &p->opt.in6_addr)) {
712 found = cur;
713 break;
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 IN6_ADDR_PREFIX_TO_STRING(&p->opt.in6_addr, p->opt.prefixlen),
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",
738 IN6_ADDR_PREFIX_TO_STRING(&p->opt.in6_addr, p->opt.prefixlen));
739 }
740
741 return 0;
742 }
743
744 int sd_radv_add_pref64_prefix(sd_radv *ra, sd_radv_pref64_prefix *p) {
745 sd_radv_pref64_prefix *found = NULL;
746
747 assert_return(ra, -EINVAL);
748 assert_return(p, -EINVAL);
749
750 LIST_FOREACH(prefix, cur, ra->pref64_prefixes)
751 if (cur->prefixlen == p->prefixlen &&
752 in6_addr_equal(&cur->in6_addr, &p->in6_addr)) {
753 found = cur;
754 break;
755 }
756
757 if (found) {
758 /* p and cur may be equivalent. First increment the reference counter. */
759 sd_radv_pref64_prefix_ref(p);
760
761 /* Then, remove the old entry. */
762 LIST_REMOVE(prefix, ra->pref64_prefixes, found);
763 sd_radv_pref64_prefix_unref(found);
764
765 /* Finally, add the new entry. */
766 LIST_APPEND(prefix, ra->pref64_prefixes, p);
767
768 log_radv(ra, "Updated/replaced IPv6 PREF64 prefix %s (lifetime: %s)",
769 IN6_ADDR_PREFIX_TO_STRING(&p->in6_addr, p->prefixlen),
770 FORMAT_TIMESPAN(p->lifetime_usec, USEC_PER_SEC));
771 } else {
772 /* The route prefix is new. Let's simply add it. */
773
774 sd_radv_pref64_prefix_ref(p);
775 LIST_APPEND(prefix, ra->pref64_prefixes, p);
776 ra->n_pref64_prefixes++;
777
778 log_radv(ra, "Added PREF64 prefix %s",
779 IN6_ADDR_PREFIX_TO_STRING(&p->in6_addr, p->prefixlen));
780 }
781
782 return 0;
783 }
784
785 int sd_radv_set_rdnss(
786 sd_radv *ra,
787 uint64_t lifetime_usec,
788 const struct in6_addr *dns,
789 size_t n_dns) {
790
791 _cleanup_free_ struct sd_radv_opt_dns *opt_rdnss = NULL;
792 size_t len;
793
794 assert_return(ra, -EINVAL);
795 assert_return(n_dns < 128, -EINVAL);
796
797 if (lifetime_usec > RADV_RDNSS_MAX_LIFETIME_USEC)
798 return -EINVAL;
799
800 if (!dns || n_dns == 0) {
801 ra->rdnss = mfree(ra->rdnss);
802 ra->n_rdnss = 0;
803
804 return 0;
805 }
806
807 len = sizeof(struct sd_radv_opt_dns) + sizeof(struct in6_addr) * n_dns;
808
809 opt_rdnss = malloc0(len);
810 if (!opt_rdnss)
811 return -ENOMEM;
812
813 opt_rdnss->type = RADV_OPT_RDNSS;
814 opt_rdnss->length = len / 8;
815 opt_rdnss->lifetime = usec_to_be32_sec(lifetime_usec);
816
817 memcpy(opt_rdnss + 1, dns, n_dns * sizeof(struct in6_addr));
818
819 free_and_replace(ra->rdnss, opt_rdnss);
820
821 ra->n_rdnss = n_dns;
822
823 return 0;
824 }
825
826 int sd_radv_set_dnssl(
827 sd_radv *ra,
828 uint64_t lifetime_usec,
829 char **search_list) {
830
831 _cleanup_free_ struct sd_radv_opt_dns *opt_dnssl = NULL;
832 size_t len = 0;
833 uint8_t *p;
834
835 assert_return(ra, -EINVAL);
836
837 if (lifetime_usec > RADV_DNSSL_MAX_LIFETIME_USEC)
838 return -EINVAL;
839
840 if (strv_isempty(search_list)) {
841 ra->dnssl = mfree(ra->dnssl);
842 return 0;
843 }
844
845 STRV_FOREACH(s, search_list)
846 len += strlen(*s) + 2;
847
848 len = (sizeof(struct sd_radv_opt_dns) + len + 7) & ~0x7;
849
850 opt_dnssl = malloc0(len);
851 if (!opt_dnssl)
852 return -ENOMEM;
853
854 opt_dnssl->type = RADV_OPT_DNSSL;
855 opt_dnssl->length = len / 8;
856 opt_dnssl->lifetime = usec_to_be32_sec(lifetime_usec);
857
858 p = (uint8_t *)(opt_dnssl + 1);
859 len -= sizeof(struct sd_radv_opt_dns);
860
861 STRV_FOREACH(s, search_list) {
862 int r;
863
864 r = dns_name_to_wire_format(*s, p, len, false);
865 if (r < 0)
866 return r;
867
868 if (len < (size_t)r)
869 return -ENOBUFS;
870
871 p += r;
872 len -= r;
873 }
874
875 free_and_replace(ra->dnssl, opt_dnssl);
876
877 return 0;
878 }
879
880 int sd_radv_prefix_new(sd_radv_prefix **ret) {
881 sd_radv_prefix *p;
882
883 assert_return(ret, -EINVAL);
884
885 p = new(sd_radv_prefix, 1);
886 if (!p)
887 return -ENOMEM;
888
889 *p = (sd_radv_prefix) {
890 .n_ref = 1,
891
892 .opt.type = ND_OPT_PREFIX_INFORMATION,
893 .opt.length = (sizeof(p->opt) - 1)/8 + 1,
894 .opt.prefixlen = 64,
895
896 /* RFC 4861, Section 6.2.1 */
897 .opt.flags = ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO,
898
899 .lifetime_valid_usec = RADV_DEFAULT_VALID_LIFETIME_USEC,
900 .lifetime_preferred_usec = RADV_DEFAULT_PREFERRED_LIFETIME_USEC,
901 .valid_until = USEC_INFINITY,
902 .preferred_until = USEC_INFINITY,
903 };
904
905 *ret = p;
906 return 0;
907 }
908
909 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_prefix, sd_radv_prefix, mfree);
910
911 int sd_radv_prefix_set_prefix(
912 sd_radv_prefix *p,
913 const struct in6_addr *in6_addr,
914 unsigned char prefixlen) {
915
916 assert_return(p, -EINVAL);
917 assert_return(in6_addr, -EINVAL);
918
919 if (prefixlen < 3 || prefixlen > 128)
920 return -EINVAL;
921
922 if (prefixlen > 64)
923 /* unusual but allowed, log it */
924 log_radv(NULL, "Unusual prefix length %d greater than 64", prefixlen);
925
926 p->opt.in6_addr = *in6_addr;
927 p->opt.prefixlen = prefixlen;
928
929 return 0;
930 }
931
932 int sd_radv_prefix_get_prefix(
933 sd_radv_prefix *p,
934 struct in6_addr *ret_in6_addr,
935 unsigned char *ret_prefixlen) {
936
937 assert_return(p, -EINVAL);
938 assert_return(ret_in6_addr, -EINVAL);
939 assert_return(ret_prefixlen, -EINVAL);
940
941 *ret_in6_addr = p->opt.in6_addr;
942 *ret_prefixlen = p->opt.prefixlen;
943
944 return 0;
945 }
946
947 int sd_radv_prefix_set_onlink(sd_radv_prefix *p, int onlink) {
948 assert_return(p, -EINVAL);
949
950 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_ONLINK, onlink);
951
952 return 0;
953 }
954
955 int sd_radv_prefix_set_address_autoconfiguration(sd_radv_prefix *p, int address_autoconfiguration) {
956 assert_return(p, -EINVAL);
957
958 SET_FLAG(p->opt.flags, ND_OPT_PI_FLAG_AUTO, address_autoconfiguration);
959
960 return 0;
961 }
962
963 int sd_radv_prefix_set_valid_lifetime(sd_radv_prefix *p, uint64_t lifetime_usec, uint64_t valid_until) {
964 assert_return(p, -EINVAL);
965
966 p->lifetime_valid_usec = lifetime_usec;
967 p->valid_until = valid_until;
968
969 return 0;
970 }
971
972 int sd_radv_prefix_set_preferred_lifetime(sd_radv_prefix *p, uint64_t lifetime_usec, uint64_t valid_until) {
973 assert_return(p, -EINVAL);
974
975 p->lifetime_preferred_usec = lifetime_usec;
976 p->preferred_until = valid_until;
977
978 return 0;
979 }
980
981 int sd_radv_route_prefix_new(sd_radv_route_prefix **ret) {
982 sd_radv_route_prefix *p;
983
984 assert_return(ret, -EINVAL);
985
986 p = new(sd_radv_route_prefix, 1);
987 if (!p)
988 return -ENOMEM;
989
990 *p = (sd_radv_route_prefix) {
991 .n_ref = 1,
992
993 .opt.type = RADV_OPT_ROUTE_INFORMATION,
994 .opt.length = DIV_ROUND_UP(sizeof(p->opt), 8),
995 .opt.prefixlen = 64,
996
997 .lifetime_usec = RADV_DEFAULT_VALID_LIFETIME_USEC,
998 .valid_until = USEC_INFINITY,
999 };
1000
1001 *ret = p;
1002 return 0;
1003 }
1004
1005 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_route_prefix, sd_radv_route_prefix, mfree);
1006
1007 int sd_radv_route_prefix_set_prefix(
1008 sd_radv_route_prefix *p,
1009 const struct in6_addr *in6_addr,
1010 unsigned char prefixlen) {
1011
1012 assert_return(p, -EINVAL);
1013 assert_return(in6_addr, -EINVAL);
1014
1015 if (prefixlen > 128)
1016 return -EINVAL;
1017
1018 if (prefixlen > 64)
1019 /* unusual but allowed, log it */
1020 log_radv(NULL, "Unusual prefix length %u greater than 64", prefixlen);
1021
1022 p->opt.in6_addr = *in6_addr;
1023 p->opt.prefixlen = prefixlen;
1024
1025 return 0;
1026 }
1027
1028 int sd_radv_route_prefix_set_lifetime(sd_radv_route_prefix *p, uint64_t lifetime_usec, uint64_t valid_until) {
1029 assert_return(p, -EINVAL);
1030
1031 p->lifetime_usec = lifetime_usec;
1032 p->valid_until = valid_until;
1033
1034 return 0;
1035 }
1036
1037 int sd_radv_pref64_prefix_new(sd_radv_pref64_prefix **ret) {
1038 sd_radv_pref64_prefix *p;
1039
1040 assert_return(ret, -EINVAL);
1041
1042 p = new(sd_radv_pref64_prefix, 1);
1043 if (!p)
1044 return -ENOMEM;
1045
1046 *p = (sd_radv_pref64_prefix) {
1047 .n_ref = 1,
1048
1049 .opt.type = RADV_OPT_PREF64,
1050 .opt.length = 2,
1051 };
1052
1053 *ret = p;
1054 return 0;
1055 }
1056
1057 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_pref64_prefix, sd_radv_pref64_prefix, mfree);
1058
1059 int sd_radv_pref64_prefix_set_prefix(
1060 sd_radv_pref64_prefix *p,
1061 const struct in6_addr *prefix,
1062 uint8_t prefixlen,
1063 uint64_t lifetime_usec) {
1064
1065 uint16_t pref64_lifetime;
1066 uint8_t prefixlen_code;
1067 int r;
1068
1069 assert_return(p, -EINVAL);
1070 assert_return(prefix, -EINVAL);
1071
1072 r = pref64_prefix_length_to_plc(prefixlen, &prefixlen_code);
1073 if (r < 0)
1074 return log_radv_errno(NULL, r,
1075 "Unsupported PREF64 prefix length %u. Valid lengths are 32, 40, 48, 56, 64 and 96", prefixlen);
1076
1077 if (lifetime_usec > PREF64_MAX_LIFETIME_USEC)
1078 return -EINVAL;
1079
1080 /* RFC 8781 - 4.1 rounding up lifetime to multiply of 8 */
1081 pref64_lifetime = DIV_ROUND_UP(lifetime_usec, 8 * USEC_PER_SEC) << 3;
1082 pref64_lifetime |= prefixlen_code;
1083
1084 unaligned_write_be16(&p->opt.lifetime_and_plc, pref64_lifetime);
1085 memcpy(&p->opt.prefix, prefix, sizeof(p->opt.prefix));
1086
1087 p->in6_addr = *prefix;
1088 p->prefixlen = prefixlen;
1089
1090 return 0;
1091 }