1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2017 Intel Corporation. All rights reserved.
6 #include <netinet/icmp6.h>
7 #include <netinet/in.h>
12 #include "alloc-util.h"
13 #include "dns-domain.h"
14 #include "ether-addr-util.h"
15 #include "event-util.h"
17 #include "icmp6-util.h"
18 #include "in-addr-util.h"
21 #include "memory-util.h"
22 #include "radv-internal.h"
23 #include "random-util.h"
24 #include "socket-util.h"
25 #include "string-util.h"
28 _public_
int sd_radv_new(sd_radv
**ret
) {
29 _cleanup_(sd_radv_unrefp
) sd_radv
*ra
= NULL
;
31 assert_return(ret
, -EINVAL
);
47 _public_
int sd_radv_attach_event(sd_radv
*ra
, sd_event
*event
, int64_t priority
) {
50 assert_return(ra
, -EINVAL
);
51 assert_return(!ra
->event
, -EBUSY
);
54 ra
->event
= sd_event_ref(event
);
56 r
= sd_event_default(&ra
->event
);
61 ra
->event_priority
= priority
;
66 _public_
int sd_radv_detach_event(sd_radv
*ra
) {
68 assert_return(ra
, -EINVAL
);
70 ra
->event
= sd_event_unref(ra
->event
);
74 _public_ sd_event
*sd_radv_get_event(sd_radv
*ra
) {
75 assert_return(ra
, NULL
);
80 static void radv_reset(sd_radv
*ra
) {
83 (void) event_source_disable(ra
->timeout_event_source
);
85 ra
->recv_event_source
=
86 sd_event_source_unref(ra
->recv_event_source
);
91 static sd_radv
*radv_free(sd_radv
*ra
) {
95 while (ra
->prefixes
) {
96 sd_radv_prefix
*p
= ra
->prefixes
;
98 LIST_REMOVE(prefix
, ra
->prefixes
, p
);
99 sd_radv_prefix_unref(p
);
102 while (ra
->route_prefixes
) {
103 sd_radv_route_prefix
*p
= ra
->route_prefixes
;
105 LIST_REMOVE(prefix
, ra
->route_prefixes
, p
);
106 sd_radv_route_prefix_unref(p
);
112 ra
->timeout_event_source
= sd_event_source_unref(ra
->timeout_event_source
);
116 sd_radv_detach_event(ra
);
118 ra
->fd
= safe_close(ra
->fd
);
123 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv
, sd_radv
, radv_free
);
125 static int radv_send(sd_radv
*ra
, const struct in6_addr
*dst
, uint32_t router_lifetime
) {
126 sd_radv_route_prefix
*rt
;
128 struct sockaddr_in6 dst_addr
= {
129 .sin6_family
= AF_INET6
,
130 .sin6_addr
= IN6ADDR_ALL_NODES_MULTICAST_INIT
,
132 struct nd_router_advert adv
= {};
134 struct nd_opt_hdr opthdr
;
135 struct ether_addr slladdr
;
136 } _packed_ opt_mac
= {
138 .nd_opt_type
= ND_OPT_SOURCE_LINKADDR
,
139 .nd_opt_len
= (sizeof(struct nd_opt_hdr
) +
140 sizeof(struct ether_addr
) - 1) /8 + 1,
143 struct nd_opt_mtu opt_mtu
= {
144 .nd_opt_mtu_type
= ND_OPT_MTU
,
147 /* Reserve iov space for RA header, linkaddr, MTU, N prefixes, N routes, RDNSS
149 struct iovec iov
[5 + ra
->n_prefixes
+ ra
->n_route_prefixes
];
150 struct msghdr msg
= {
151 .msg_name
= &dst_addr
,
152 .msg_namelen
= sizeof(dst_addr
),
160 r
= sd_event_now(ra
->event
, clock_boottime_or_monotonic(), &time_now
);
164 if (dst
&& !IN6_IS_ADDR_UNSPECIFIED(dst
))
165 dst_addr
.sin6_addr
= *dst
;
167 adv
.nd_ra_type
= ND_ROUTER_ADVERT
;
168 adv
.nd_ra_curhoplimit
= ra
->hop_limit
;
169 adv
.nd_ra_flags_reserved
= ra
->flags
;
170 adv
.nd_ra_router_lifetime
= htobe16(router_lifetime
);
171 iov
[msg
.msg_iovlen
++] = IOVEC_MAKE(&adv
, sizeof(adv
));
173 /* MAC address is optional, either because the link does not use L2
174 addresses or load sharing is desired. See RFC 4861, Section 4.2 */
175 if (!ether_addr_is_null(&ra
->mac_addr
)) {
176 opt_mac
.slladdr
= ra
->mac_addr
;
177 iov
[msg
.msg_iovlen
++] = IOVEC_MAKE(&opt_mac
, sizeof(opt_mac
));
181 opt_mtu
.nd_opt_mtu_mtu
= htobe32(ra
->mtu
);
182 iov
[msg
.msg_iovlen
++] = IOVEC_MAKE(&opt_mtu
, sizeof(opt_mtu
));
185 LIST_FOREACH(prefix
, p
, ra
->prefixes
) {
186 if (p
->valid_until
) {
188 if (time_now
> p
->valid_until
)
189 p
->opt
.valid_lifetime
= 0;
191 p
->opt
.valid_lifetime
= htobe32((p
->valid_until
- time_now
) / USEC_PER_SEC
);
193 if (time_now
> p
->preferred_until
)
194 p
->opt
.preferred_lifetime
= 0;
196 p
->opt
.preferred_lifetime
= htobe32((p
->preferred_until
- time_now
) / USEC_PER_SEC
);
198 iov
[msg
.msg_iovlen
++] = IOVEC_MAKE(&p
->opt
, sizeof(p
->opt
));
201 LIST_FOREACH(prefix
, rt
, ra
->route_prefixes
)
202 iov
[msg
.msg_iovlen
++] = IOVEC_MAKE(&rt
->opt
, sizeof(rt
->opt
));
205 iov
[msg
.msg_iovlen
++] = IOVEC_MAKE(ra
->rdnss
, ra
->rdnss
->length
* 8);
208 iov
[msg
.msg_iovlen
++] = IOVEC_MAKE(ra
->dnssl
, ra
->dnssl
->length
* 8);
210 if (sendmsg(ra
->fd
, &msg
, 0) < 0)
216 static int radv_recv(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
217 sd_radv
*ra
= userdata
;
218 _cleanup_free_
char *addr
= NULL
;
220 triple_timestamp timestamp
;
223 _cleanup_free_
char *buf
= NULL
;
229 buflen
= next_datagram_size_fd(fd
);
233 buf
= new0(char, buflen
);
237 r
= icmp6_receive(fd
, buf
, buflen
, &src
, ×tamp
);
241 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &src
, &addr
);
242 log_radv("Received RS from non-link-local address %s. Ignoring", addr
);
246 log_radv("Received RS with invalid hop limit. Ignoring.");
250 log_radv("Received invalid source address from ICMPv6 socket. Ignoring.");
253 case -EAGAIN
: /* ignore spurious wakeups */
257 log_radv_errno(r
, "Unexpected error receiving from ICMPv6 socket: %m");
264 if ((size_t) buflen
< sizeof(struct nd_router_solicit
)) {
265 log_radv("Too short packet received");
269 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &src
, &addr
);
271 r
= radv_send(ra
, &src
, ra
->lifetime
);
273 log_radv_errno(r
, "Unable to send solicited Router Advertisement to %s: %m", strnull(addr
));
275 log_radv("Sent solicited Router Advertisement to %s", strnull(addr
));
280 static usec_t
radv_compute_timeout(usec_t min
, usec_t max
) {
281 assert_return(min
<= max
, SD_RADV_DEFAULT_MIN_TIMEOUT_USEC
);
283 /* RFC 4861: min must be no less than 3s, max must be no less than 4s */
284 min
= MAX(min
, 3*USEC_PER_SEC
);
285 max
= MAX(max
, 4*USEC_PER_SEC
);
287 return min
+ (random_u32() % (max
- min
));
290 static int radv_timeout(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
292 sd_radv
*ra
= userdata
;
293 usec_t min_timeout
= SD_RADV_DEFAULT_MIN_TIMEOUT_USEC
;
294 usec_t max_timeout
= SD_RADV_DEFAULT_MAX_TIMEOUT_USEC
;
295 usec_t time_now
, timeout
;
296 char time_string
[FORMAT_TIMESPAN_MAX
];
302 r
= sd_event_now(ra
->event
, clock_boottime_or_monotonic(), &time_now
);
306 r
= radv_send(ra
, NULL
, ra
->lifetime
);
308 log_radv_errno(r
, "Unable to send Router Advertisement: %m");
310 /* RFC 4861, Section 6.2.4, sending initial Router Advertisements */
311 if (ra
->ra_sent
< SD_RADV_MAX_INITIAL_RTR_ADVERTISEMENTS
) {
312 max_timeout
= SD_RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC
;
313 min_timeout
= SD_RADV_MAX_INITIAL_RTR_ADVERT_INTERVAL_USEC
/ 3;
316 /* RFC 4861, Section 6.2.1, lifetime must be at least MaxRtrAdvInterval,
317 so lower the interval here */
318 if (ra
->lifetime
> 0 && (ra
->lifetime
* USEC_PER_SEC
) < max_timeout
) {
319 max_timeout
= ra
->lifetime
* USEC_PER_SEC
;
320 min_timeout
= max_timeout
/ 3;
323 timeout
= radv_compute_timeout(min_timeout
, max_timeout
);
325 log_radv("Next Router Advertisement in %s",
326 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
,
327 timeout
, USEC_PER_SEC
));
329 r
= event_reset_time(ra
->event
, &ra
->timeout_event_source
,
330 clock_boottime_or_monotonic(),
331 time_now
+ timeout
, MSEC_PER_SEC
,
333 ra
->event_priority
, "radv-timeout", true);
347 _public_
int sd_radv_stop(sd_radv
*ra
) {
350 assert_return(ra
, -EINVAL
);
352 if (ra
->state
== SD_RADV_STATE_IDLE
)
355 log_radv("Stopping IPv6 Router Advertisement daemon");
357 /* RFC 4861, Section 6.2.5, send at least one Router Advertisement
358 with zero lifetime */
359 r
= radv_send(ra
, NULL
, 0);
361 log_radv_errno(r
, "Unable to send last Router Advertisement with router lifetime set to zero: %m");
364 ra
->fd
= safe_close(ra
->fd
);
365 ra
->state
= SD_RADV_STATE_IDLE
;
370 _public_
int sd_radv_start(sd_radv
*ra
) {
373 assert_return(ra
, -EINVAL
);
374 assert_return(ra
->event
, -EINVAL
);
375 assert_return(ra
->ifindex
> 0, -EINVAL
);
377 if (ra
->state
!= SD_RADV_STATE_IDLE
)
380 r
= event_reset_time(ra
->event
, &ra
->timeout_event_source
,
381 clock_boottime_or_monotonic(),
384 ra
->event_priority
, "radv-timeout", true);
388 r
= icmp6_bind_router_advertisement(ra
->ifindex
);
394 r
= sd_event_add_io(ra
->event
, &ra
->recv_event_source
, ra
->fd
, EPOLLIN
, radv_recv
, ra
);
398 r
= sd_event_source_set_priority(ra
->recv_event_source
, ra
->event_priority
);
402 (void) sd_event_source_set_description(ra
->recv_event_source
, "radv-receive-message");
404 ra
->state
= SD_RADV_STATE_ADVERTISING
;
406 log_radv("Started IPv6 Router Advertisement daemon");
416 _public_
int sd_radv_set_ifindex(sd_radv
*ra
, int ifindex
) {
417 assert_return(ra
, -EINVAL
);
418 assert_return(ifindex
> 0, -EINVAL
);
420 if (ra
->state
!= SD_RADV_STATE_IDLE
)
423 ra
->ifindex
= ifindex
;
428 _public_
int sd_radv_set_mac(sd_radv
*ra
, const struct ether_addr
*mac_addr
) {
429 assert_return(ra
, -EINVAL
);
431 if (ra
->state
!= SD_RADV_STATE_IDLE
)
435 ra
->mac_addr
= *mac_addr
;
442 _public_
int sd_radv_set_mtu(sd_radv
*ra
, uint32_t mtu
) {
443 assert_return(ra
, -EINVAL
);
444 assert_return(mtu
>= 1280, -EINVAL
);
451 _public_
int sd_radv_set_hop_limit(sd_radv
*ra
, uint8_t hop_limit
) {
452 assert_return(ra
, -EINVAL
);
454 if (ra
->state
!= SD_RADV_STATE_IDLE
)
457 ra
->hop_limit
= hop_limit
;
462 _public_
int sd_radv_set_router_lifetime(sd_radv
*ra
, uint32_t router_lifetime
) {
463 assert_return(ra
, -EINVAL
);
465 if (ra
->state
!= SD_RADV_STATE_IDLE
)
468 /* RFC 4191, Section 2.2, "...If the Router Lifetime is zero, the
469 preference value MUST be set to (00) by the sender..." */
470 if (router_lifetime
== 0 &&
471 (ra
->flags
& (0x3 << 3)) != (SD_NDISC_PREFERENCE_MEDIUM
<< 3))
474 ra
->lifetime
= router_lifetime
;
479 _public_
int sd_radv_set_managed_information(sd_radv
*ra
, int managed
) {
480 assert_return(ra
, -EINVAL
);
482 if (ra
->state
!= SD_RADV_STATE_IDLE
)
485 SET_FLAG(ra
->flags
, ND_RA_FLAG_MANAGED
, managed
);
490 _public_
int sd_radv_set_other_information(sd_radv
*ra
, int other
) {
491 assert_return(ra
, -EINVAL
);
493 if (ra
->state
!= SD_RADV_STATE_IDLE
)
496 SET_FLAG(ra
->flags
, ND_RA_FLAG_OTHER
, other
);
501 _public_
int sd_radv_set_preference(sd_radv
*ra
, unsigned preference
) {
504 assert_return(ra
, -EINVAL
);
505 assert_return(IN_SET(preference
,
506 SD_NDISC_PREFERENCE_LOW
,
507 SD_NDISC_PREFERENCE_MEDIUM
,
508 SD_NDISC_PREFERENCE_HIGH
), -EINVAL
);
510 ra
->flags
= (ra
->flags
& ~(0x3 << 3)) | (preference
<< 3);
515 _public_
int sd_radv_add_prefix(sd_radv
*ra
, sd_radv_prefix
*p
, int dynamic
) {
518 _cleanup_free_
char *addr_p
= NULL
;
519 char time_string_preferred
[FORMAT_TIMESPAN_MAX
];
520 char time_string_valid
[FORMAT_TIMESPAN_MAX
];
521 usec_t time_now
, valid
, preferred
, valid_until
, preferred_until
;
523 assert_return(ra
, -EINVAL
);
528 /* Refuse prefixes that don't have a prefix set */
529 if (IN6_IS_ADDR_UNSPECIFIED(&p
->opt
.in6_addr
))
532 LIST_FOREACH(prefix
, cur
, ra
->prefixes
) {
534 r
= in_addr_prefix_intersect(AF_INET6
,
535 (union in_addr_union
*) &cur
->opt
.in6_addr
,
537 (union in_addr_union
*) &p
->opt
.in6_addr
,
540 _cleanup_free_
char *addr_cur
= NULL
;
542 (void) in_addr_to_string(AF_INET6
,
543 (union in_addr_union
*) &p
->opt
.in6_addr
,
546 if (dynamic
&& cur
->opt
.prefixlen
== p
->opt
.prefixlen
)
549 (void) in_addr_to_string(AF_INET6
,
550 (union in_addr_union
*) &cur
->opt
.in6_addr
,
552 log_radv("IPv6 prefix %s/%u already configured, ignoring %s/%u",
553 addr_cur
, cur
->opt
.prefixlen
,
554 addr_p
, p
->opt
.prefixlen
);
560 p
= sd_radv_prefix_ref(p
);
562 LIST_APPEND(prefix
, ra
->prefixes
, p
);
566 (void) in_addr_to_string(AF_INET6
, (union in_addr_union
*) &p
->opt
.in6_addr
, &addr_p
);
569 log_radv("Added prefix %s/%d", addr_p
, p
->opt
.prefixlen
);
576 r
= sd_event_now(ra
->event
, clock_boottime_or_monotonic(), &time_now
);
580 valid
= be32toh(p
->opt
.valid_lifetime
) * USEC_PER_SEC
;
581 valid_until
= usec_add(valid
, time_now
);
582 if (valid_until
== USEC_INFINITY
)
585 preferred
= be32toh(p
->opt
.preferred_lifetime
) * USEC_PER_SEC
;
586 preferred_until
= usec_add(preferred
, time_now
);
587 if (preferred_until
== USEC_INFINITY
)
590 cur
->valid_until
= valid_until
;
591 cur
->preferred_until
= preferred_until
;
593 log_radv("Updated prefix %s/%u preferred %s valid %s",
594 addr_p
, p
->opt
.prefixlen
,
595 format_timespan(time_string_preferred
, FORMAT_TIMESPAN_MAX
,
596 preferred
, USEC_PER_SEC
),
597 format_timespan(time_string_valid
, FORMAT_TIMESPAN_MAX
,
598 valid
, USEC_PER_SEC
));
603 _public_ sd_radv_prefix
*sd_radv_remove_prefix(sd_radv
*ra
,
604 const struct in6_addr
*prefix
,
605 unsigned char prefixlen
) {
606 sd_radv_prefix
*cur
, *next
;
608 assert_return(ra
, NULL
);
609 assert_return(prefix
, NULL
);
611 LIST_FOREACH_SAFE(prefix
, cur
, next
, ra
->prefixes
) {
612 if (prefixlen
!= cur
->opt
.prefixlen
)
615 if (!in_addr_equal(AF_INET6
,
616 (union in_addr_union
*)prefix
,
617 (union in_addr_union
*)&cur
->opt
.in6_addr
))
620 LIST_REMOVE(prefix
, ra
->prefixes
, cur
);
622 sd_radv_prefix_unref(cur
);
630 _public_
int sd_radv_add_route_prefix(sd_radv
*ra
, sd_radv_route_prefix
*p
, int dynamic
) {
631 char time_string_valid
[FORMAT_TIMESPAN_MAX
];
632 usec_t time_now
, valid
, valid_until
;
633 _cleanup_free_
char *pretty
= NULL
;
634 sd_radv_route_prefix
*cur
;
637 assert_return(ra
, -EINVAL
);
642 (void) in_addr_to_string(AF_INET6
,
643 (union in_addr_union
*) &p
->opt
.in6_addr
,
646 LIST_FOREACH(prefix
, cur
, ra
->route_prefixes
) {
647 _cleanup_free_
char *addr
= NULL
;
649 r
= in_addr_prefix_intersect(AF_INET6
,
650 (union in_addr_union
*) &cur
->opt
.in6_addr
,
652 (union in_addr_union
*) &p
->opt
.in6_addr
,
659 if (dynamic
&& cur
->opt
.prefixlen
== p
->opt
.prefixlen
)
662 (void) in_addr_to_string(AF_INET6
,
663 (union in_addr_union
*) &cur
->opt
.in6_addr
,
665 log_radv("IPv6 route prefix %s/%u already configured, ignoring %s/%u",
666 strempty(addr
), cur
->opt
.prefixlen
,
667 strempty(pretty
), p
->opt
.prefixlen
);
672 p
= sd_radv_route_prefix_ref(p
);
674 LIST_APPEND(prefix
, ra
->route_prefixes
, p
);
675 ra
->n_route_prefixes
++;
679 log_radv("Added prefix %s/%u", strempty(pretty
), p
->opt
.prefixlen
);
684 r
= sd_event_now(ra
->event
, clock_boottime_or_monotonic(), &time_now
);
688 valid
= be32toh(p
->opt
.lifetime
) * USEC_PER_SEC
;
689 valid_until
= usec_add(valid
, time_now
);
690 if (valid_until
== USEC_INFINITY
)
693 log_radv("Updated route prefix %s/%u valid %s",
694 strempty(pretty
), p
->opt
.prefixlen
,
695 format_timespan(time_string_valid
, FORMAT_TIMESPAN_MAX
, valid
, USEC_PER_SEC
));
700 _public_
int sd_radv_set_rdnss(sd_radv
*ra
, uint32_t lifetime
,
701 const struct in6_addr
*dns
, size_t n_dns
) {
702 _cleanup_free_
struct sd_radv_opt_dns
*opt_rdnss
= NULL
;
705 assert_return(ra
, -EINVAL
);
706 assert_return(n_dns
< 128, -EINVAL
);
708 if (!dns
|| n_dns
== 0) {
709 ra
->rdnss
= mfree(ra
->rdnss
);
715 len
= sizeof(struct sd_radv_opt_dns
) + sizeof(struct in6_addr
) * n_dns
;
717 opt_rdnss
= malloc0(len
);
721 opt_rdnss
->type
= SD_RADV_OPT_RDNSS
;
722 opt_rdnss
->length
= len
/ 8;
723 opt_rdnss
->lifetime
= htobe32(lifetime
);
725 memcpy(opt_rdnss
+ 1, dns
, n_dns
* sizeof(struct in6_addr
));
727 free_and_replace(ra
->rdnss
, opt_rdnss
);
734 _public_
int sd_radv_set_dnssl(sd_radv
*ra
, uint32_t lifetime
,
735 char **search_list
) {
736 _cleanup_free_
struct sd_radv_opt_dns
*opt_dnssl
= NULL
;
741 assert_return(ra
, -EINVAL
);
743 if (strv_isempty(search_list
)) {
744 ra
->dnssl
= mfree(ra
->dnssl
);
748 STRV_FOREACH(s
, search_list
)
749 len
+= strlen(*s
) + 2;
751 len
= (sizeof(struct sd_radv_opt_dns
) + len
+ 7) & ~0x7;
753 opt_dnssl
= malloc0(len
);
757 opt_dnssl
->type
= SD_RADV_OPT_DNSSL
;
758 opt_dnssl
->length
= len
/ 8;
759 opt_dnssl
->lifetime
= htobe32(lifetime
);
761 p
= (uint8_t *)(opt_dnssl
+ 1);
762 len
-= sizeof(struct sd_radv_opt_dns
);
764 STRV_FOREACH(s
, search_list
) {
767 r
= dns_name_to_wire_format(*s
, p
, len
, false);
778 free_and_replace(ra
->dnssl
, opt_dnssl
);
783 _public_
int sd_radv_prefix_new(sd_radv_prefix
**ret
) {
786 assert_return(ret
, -EINVAL
);
788 p
= new(sd_radv_prefix
, 1);
792 *p
= (sd_radv_prefix
) {
795 .opt
.type
= ND_OPT_PREFIX_INFORMATION
,
796 .opt
.length
= (sizeof(p
->opt
) - 1)/8 + 1,
799 /* RFC 4861, Section 6.2.1 */
800 .opt
.flags
= ND_OPT_PI_FLAG_ONLINK
|ND_OPT_PI_FLAG_AUTO
,
802 .opt
.preferred_lifetime
= htobe32(604800),
803 .opt
.valid_lifetime
= htobe32(2592000),
810 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_prefix
, sd_radv_prefix
, mfree
);
812 _public_
int sd_radv_prefix_set_prefix(sd_radv_prefix
*p
, const struct in6_addr
*in6_addr
,
813 unsigned char prefixlen
) {
814 assert_return(p
, -EINVAL
);
815 assert_return(in6_addr
, -EINVAL
);
817 if (prefixlen
< 3 || prefixlen
> 128)
821 /* unusual but allowed, log it */
822 log_radv("Unusual prefix length %d greater than 64", prefixlen
);
824 p
->opt
.in6_addr
= *in6_addr
;
825 p
->opt
.prefixlen
= prefixlen
;
830 _public_
int sd_radv_prefix_get_prefix(sd_radv_prefix
*p
, struct in6_addr
*ret_in6_addr
,
831 unsigned char *ret_prefixlen
) {
832 assert_return(p
, -EINVAL
);
833 assert_return(ret_in6_addr
, -EINVAL
);
834 assert_return(ret_prefixlen
, -EINVAL
);
836 *ret_in6_addr
= p
->opt
.in6_addr
;
837 *ret_prefixlen
= p
->opt
.prefixlen
;
842 _public_
int sd_radv_prefix_set_onlink(sd_radv_prefix
*p
, int onlink
) {
843 assert_return(p
, -EINVAL
);
845 SET_FLAG(p
->opt
.flags
, ND_OPT_PI_FLAG_ONLINK
, onlink
);
850 _public_
int sd_radv_prefix_set_address_autoconfiguration(sd_radv_prefix
*p
,
851 int address_autoconfiguration
) {
852 assert_return(p
, -EINVAL
);
854 SET_FLAG(p
->opt
.flags
, ND_OPT_PI_FLAG_AUTO
, address_autoconfiguration
);
859 _public_
int sd_radv_prefix_set_valid_lifetime(sd_radv_prefix
*p
,
860 uint32_t valid_lifetime
) {
861 assert_return(p
, -EINVAL
);
863 p
->opt
.valid_lifetime
= htobe32(valid_lifetime
);
868 _public_
int sd_radv_prefix_set_preferred_lifetime(sd_radv_prefix
*p
,
869 uint32_t preferred_lifetime
) {
870 assert_return(p
, -EINVAL
);
872 p
->opt
.preferred_lifetime
= htobe32(preferred_lifetime
);
877 _public_
int sd_radv_route_prefix_new(sd_radv_route_prefix
**ret
) {
878 sd_radv_route_prefix
*p
;
880 assert_return(ret
, -EINVAL
);
882 p
= new(sd_radv_route_prefix
, 1);
886 *p
= (sd_radv_route_prefix
) {
889 .opt
.type
= SD_RADV_OPT_ROUTE_INFORMATION
,
890 .opt
.length
= DIV_ROUND_UP(sizeof(p
->opt
), 8),
893 .opt
.lifetime
= htobe32(604800),
900 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_radv_route_prefix
, sd_radv_route_prefix
, mfree
);
902 _public_
int sd_radv_prefix_set_route_prefix(sd_radv_route_prefix
*p
, const struct in6_addr
*in6_addr
,
903 unsigned char prefixlen
) {
904 assert_return(p
, -EINVAL
);
905 assert_return(in6_addr
, -EINVAL
);
911 /* unusual but allowed, log it */
912 log_radv("Unusual prefix length %u greater than 64", prefixlen
);
914 p
->opt
.in6_addr
= *in6_addr
;
915 p
->opt
.prefixlen
= prefixlen
;
920 _public_
int sd_radv_route_prefix_set_lifetime(sd_radv_route_prefix
*p
, uint32_t valid_lifetime
) {
921 assert_return(p
, -EINVAL
);
923 p
->opt
.lifetime
= htobe32(valid_lifetime
);