2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
24 #include <arpa/inet.h>
25 #include <net/route.h>
27 #include <libubox/utils.h>
33 static void forward_router_solicitation(const struct interface
*iface
);
34 static void forward_router_advertisement(const struct interface
*iface
, uint8_t *data
, size_t len
);
36 static void handle_icmpv6(void *addr
, void *data
, size_t len
,
37 struct interface
*iface
, void *dest
);
38 static void trigger_router_advert(struct uloop_timeout
*event
);
39 static void router_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
);
41 static struct netevent_handler router_netevent_handler
= { .cb
= router_netevent_cb
, };
43 static FILE *fp_route
= NULL
;
46 #define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX)
52 if (!(fp_route
= fopen("/proc/net/ipv6_route", "r"))) {
53 syslog(LOG_ERR
, "fopen(/proc/net/ipv6_route): %m");
58 if (netlink_add_netevent_handler(&router_netevent_handler
) < 0) {
59 syslog(LOG_ERR
, "Failed to add netevent handler");
64 if (ret
< 0 && fp_route
) {
73 int router_setup_interface(struct interface
*iface
, bool enable
)
77 enable
= enable
&& (iface
->ra
!= MODE_DISABLED
);
85 if (!enable
&& iface
->router_event
.uloop
.fd
>= 0) {
87 uloop_timeout_cancel(&iface
->timer_rs
);
88 iface
->timer_rs
.cb
= NULL
;
90 trigger_router_advert(&iface
->timer_rs
);
93 uloop_fd_delete(&iface
->router_event
.uloop
);
94 close(iface
->router_event
.uloop
.fd
);
95 iface
->router_event
.uloop
.fd
= -1;
97 struct icmp6_filter filt
;
98 struct ipv6_mreq mreq
;
101 if (iface
->router_event
.uloop
.fd
< 0) {
102 /* Open ICMPv6 socket */
103 iface
->router_event
.uloop
.fd
= socket(AF_INET6
, SOCK_RAW
| SOCK_CLOEXEC
,
105 if (iface
->router_event
.uloop
.fd
< 0) {
106 syslog(LOG_ERR
, "socket(AF_INET6): %m");
111 if (setsockopt(iface
->router_event
.uloop
.fd
, SOL_SOCKET
, SO_BINDTODEVICE
,
112 iface
->ifname
, strlen(iface
->ifname
)) < 0) {
113 syslog(LOG_ERR
, "setsockopt(SO_BINDTODEVICE): %m");
118 /* Let the kernel compute our checksums */
119 if (setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_RAW
, IPV6_CHECKSUM
,
120 &val
, sizeof(val
)) < 0) {
121 syslog(LOG_ERR
, "setsockopt(IPV6_CHECKSUM): %m");
126 /* This is required by RFC 4861 */
128 if (setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
129 &val
, sizeof(val
)) < 0) {
130 syslog(LOG_ERR
, "setsockopt(IPV6_MULTICAST_HOPS): %m");
135 if (setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
136 &val
, sizeof(val
)) < 0) {
137 syslog(LOG_ERR
, "setsockopt(IPV6_UNICAST_HOPS): %m");
142 /* We need to know the source interface */
144 if (setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
,
145 &val
, sizeof(val
)) < 0) {
146 syslog(LOG_ERR
, "setsockopt(IPV6_RECVPKTINFO): %m");
151 if (setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
,
152 &val
, sizeof(val
)) < 0) {
153 syslog(LOG_ERR
, "setsockopt(IPV6_RECVHOPLIMIT): %m");
158 /* Don't loop back */
160 if (setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
161 &val
, sizeof(val
)) < 0) {
162 syslog(LOG_ERR
, "setsockopt(IPV6_MULTICAST_LOOP): %m");
167 /* Filter ICMPv6 package types */
168 ICMP6_FILTER_SETBLOCKALL(&filt
);
169 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT
, &filt
);
170 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT
, &filt
);
171 if (setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_ICMPV6
, ICMP6_FILTER
,
172 &filt
, sizeof(filt
)) < 0) {
173 syslog(LOG_ERR
, "setsockopt(ICMP6_FILTER): %m");
178 iface
->router_event
.handle_dgram
= handle_icmpv6
;
180 odhcpd_register(&iface
->router_event
);
182 uloop_timeout_cancel(&iface
->timer_rs
);
183 iface
->timer_rs
.cb
= NULL
;
185 memset(&mreq
, 0, sizeof(mreq
));
186 mreq
.ipv6mr_interface
= iface
->ifindex
;
187 inet_pton(AF_INET6
, ALL_IPV6_NODES
, &mreq
.ipv6mr_multiaddr
);
188 setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_DROP_MEMBERSHIP
,
189 &mreq
, sizeof(mreq
));
191 inet_pton(AF_INET6
, ALL_IPV6_ROUTERS
, &mreq
.ipv6mr_multiaddr
);
192 setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_IPV6
, IPV6_DROP_MEMBERSHIP
,
193 &mreq
, sizeof(mreq
));
196 memset(&mreq
, 0, sizeof(mreq
));
197 mreq
.ipv6mr_interface
= iface
->ifindex
;
198 inet_pton(AF_INET6
, ALL_IPV6_ROUTERS
, &mreq
.ipv6mr_multiaddr
);
200 if (iface
->ra
== MODE_RELAY
&& iface
->master
) {
201 inet_pton(AF_INET6
, ALL_IPV6_NODES
, &mreq
.ipv6mr_multiaddr
);
202 forward_router_solicitation(iface
);
203 } else if (iface
->ra
== MODE_SERVER
) {
204 iface
->timer_rs
.cb
= trigger_router_advert
;
205 uloop_timeout_set(&iface
->timer_rs
, 1000);
208 if (setsockopt(iface
->router_event
.uloop
.fd
, IPPROTO_IPV6
,
209 IPV6_ADD_MEMBERSHIP
, &mreq
, sizeof(mreq
)) < 0) {
211 syslog(LOG_ERR
, "setsockopt(IPV6_ADD_MEMBERSHIP): %m");
216 if (ret
< 0 && iface
->router_event
.uloop
.fd
>= 0) {
217 if (iface
->router_event
.uloop
.registered
)
218 uloop_fd_delete(&iface
->router_event
.uloop
);
220 close(iface
->router_event
.uloop
.fd
);
221 iface
->router_event
.uloop
.fd
= -1;
228 static void router_netevent_cb(unsigned long event
, struct netevent_handler_info
*info
)
230 struct interface
*iface
;
233 case NETEV_IFINDEX_CHANGE
:
235 if (iface
&& iface
->router_event
.uloop
.fd
>= 0) {
236 if (iface
->router_event
.uloop
.registered
)
237 uloop_fd_delete(&iface
->router_event
.uloop
);
239 close(iface
->router_event
.uloop
.fd
);
240 iface
->router_event
.uloop
.fd
= -1;
243 case NETEV_ROUTE6_ADD
:
244 case NETEV_ROUTE6_DEL
:
245 if (info
->rt
.dst_len
)
248 avl_for_each_element(&interfaces
, iface
, avl
) {
249 if (iface
->ra
== MODE_SERVER
&& !iface
->master
)
250 uloop_timeout_set(&iface
->timer_rs
, 1000);
253 case NETEV_ADDR6LIST_CHANGE
:
255 if (iface
&& iface
->ra
== MODE_SERVER
&& !iface
->master
)
256 uloop_timeout_set(&iface
->timer_rs
, 1000);
264 static bool router_icmpv6_valid(struct sockaddr_in6
*source
, uint8_t *data
, size_t len
)
266 struct icmp6_hdr
*hdr
= (struct icmp6_hdr
*)data
;
267 struct icmpv6_opt
*opt
, *end
= (struct icmpv6_opt
*)&data
[len
];
269 /* Hoplimit is already checked in odhcpd_receive_packets */
270 if (len
< sizeof(*hdr
) || hdr
->icmp6_code
)
273 switch (hdr
->icmp6_type
) {
274 case ND_ROUTER_ADVERT
:
275 if (!IN6_IS_ADDR_LINKLOCAL(&source
->sin6_addr
))
278 opt
= (struct icmpv6_opt
*)((struct nd_router_advert
*)data
+ 1);
281 case ND_ROUTER_SOLICIT
:
282 opt
= (struct icmpv6_opt
*)((struct nd_router_solicit
*)data
+ 1);
289 icmpv6_for_each_option(opt
, opt
, end
)
290 if (opt
->type
== ND_OPT_SOURCE_LINKADDR
&&
291 IN6_IS_ADDR_UNSPECIFIED(&source
->sin6_addr
) &&
292 hdr
->icmp6_type
== ND_ROUTER_SOLICIT
)
295 /* Check all options parsed successfully */
300 /* Detect whether a default route exists, also find the source prefixes */
301 static bool parse_routes(struct odhcpd_ipaddr
*n
, ssize_t len
)
303 struct odhcpd_ipaddr p
= { .addr
.in6
= IN6ADDR_ANY_INIT
, .prefix
= 0,
304 .dprefix
= 0, .preferred_lt
= 0, .valid_lt
= 0};
305 bool found_default
= false;
306 char line
[512], ifname
[16];
310 while (fgets(line
, sizeof(line
), fp_route
)) {
312 if (sscanf(line
, "00000000000000000000000000000000 00 "
313 "%*s %*s %*s %*s %*s %*s %*s %15s", ifname
) &&
314 strcmp(ifname
, "lo")) {
315 found_default
= true;
316 } else if (sscanf(line
, "%8" SCNx32
"%8" SCNx32
"%*8" SCNx32
"%*8" SCNx32
" %hhx %*s "
317 "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32
" lo",
318 &p
.addr
.in6
.s6_addr32
[0], &p
.addr
.in6
.s6_addr32
[1], &p
.prefix
, &rflags
) &&
319 p
.prefix
> 0 && (rflags
& RTF_NONEXTHOP
) && (rflags
& RTF_REJECT
)) {
320 // Find source prefixes by scanning through unreachable-routes
321 p
.addr
.in6
.s6_addr32
[0] = htonl(p
.addr
.in6
.s6_addr32
[0]);
322 p
.addr
.in6
.s6_addr32
[1] = htonl(p
.addr
.in6
.s6_addr32
[1]);
324 for (ssize_t i
= 0; i
< len
; ++i
) {
325 if (n
[i
].prefix
<= 64 && n
[i
].prefix
>= p
.prefix
&&
326 !odhcpd_bmemcmp(&p
.addr
.in6
, &n
[i
].addr
.in6
, p
.prefix
)) {
327 n
[i
].dprefix
= p
.prefix
;
334 return found_default
;
337 static int calc_adv_interval(struct interface
*iface
, uint32_t minvalid
,
340 uint32_t minival
= iface
->ra_mininterval
;
343 *maxival
= iface
->ra_maxinterval
;
345 if (*maxival
> minvalid
/3)
346 *maxival
= minvalid
/3;
348 if (*maxival
> MaxRtrAdvInterval
)
349 *maxival
= MaxRtrAdvInterval
;
350 else if (*maxival
< 4)
353 if (minival
< MinRtrAdvInterval
)
354 minival
= MinRtrAdvInterval
;
355 else if (minival
> (*maxival
* 3)/4)
356 minival
= (*maxival
>= 9 ? *maxival
/3 : *maxival
);
358 odhcpd_urandom(&msecs
, sizeof(msecs
));
359 msecs
= (labs(msecs
) % ((*maxival
!= minival
) ? (*maxival
- minival
)*1000 : 500)) +
362 /* RFC 2461 6.2.4 For the first MAX_INITIAL_RTR_ADVERTISEMENTS advertisements */
363 /* if the timer is bigger than MAX_INITIAL_RTR_ADVERT_INTERVAL it should be */
364 /* set to MAX_INITIAL_RTR_ADVERT_INTERVAL */
365 /* Off by one as an initial interval timer has already expired */
366 if ((iface
->ra_sent
+ 1) < MaxInitialRtAdvs
&& msecs
> MaxInitialRtrAdvInterval
*1000)
367 msecs
= MaxInitialRtrAdvInterval
*1000;
372 static uint32_t calc_ra_lifetime(struct interface
*iface
, uint32_t maxival
)
374 uint32_t lifetime
= 3*maxival
;
376 if (iface
->ra_lifetime
>= 0) {
377 lifetime
= iface
->ra_lifetime
;
378 if (lifetime
> 0 && lifetime
< maxival
)
380 else if (lifetime
> 9000)
399 struct nd_router_advert h
;
400 struct icmpv6_opt lladdr
;
401 struct nd_opt_mtu mtu
;
404 struct nd_opt_dns_server
{
410 struct in6_addr addr
[];
413 struct nd_opt_search_list
{
422 struct nd_opt_route_info
{
431 struct nd_opt_pref64_info
{
434 uint16_t lifetime_plc
;
438 /* Router Advert server mode */
439 static int send_router_advert(struct interface
*iface
, const struct in6_addr
*from
)
441 time_t now
= odhcpd_time();
442 struct odhcpd_ipaddr
*addrs
= NULL
;
444 struct nd_opt_prefix_info
*pfxs
= NULL
;
445 struct nd_opt_dns_server
*dns
= NULL
;
446 struct nd_opt_search_list
*search
= NULL
;
447 struct nd_opt_route_info
*routes
= NULL
;
448 struct nd_opt_pref64_info
*pref64
= NULL
;
449 struct nd_opt_adv_interval adv_interval
;
450 struct iovec iov
[IOV_RA_TOTAL
];
451 struct sockaddr_in6 dest
;
452 size_t dns_sz
= 0, search_sz
= 0, pref64_sz
= 0;
453 size_t pfxs_cnt
= 0, routes_cnt
= 0;
454 ssize_t valid_addr_cnt
= 0, invalid_addr_cnt
= 0;
455 uint32_t minvalid
= UINT32_MAX
, maxival
, lifetime
;
456 int msecs
, mtu
= iface
->ra_mtu
, hlim
= iface
->ra_hoplimit
;
457 bool default_route
= false;
458 bool valid_prefix
= false;
459 char buf
[INET6_ADDRSTRLEN
];
461 memset(&adv
, 0, sizeof(adv
));
462 adv
.h
.nd_ra_type
= ND_ROUTER_ADVERT
;
465 hlim
= odhcpd_get_interface_config(iface
->ifname
, "hop_limit");
468 adv
.h
.nd_ra_curhoplimit
= hlim
;
470 adv
.h
.nd_ra_flags_reserved
= iface
->ra_flags
;
472 if (iface
->route_preference
< 0)
473 adv
.h
.nd_ra_flags_reserved
|= ND_RA_PREF_LOW
;
474 else if (iface
->route_preference
> 0)
475 adv
.h
.nd_ra_flags_reserved
|= ND_RA_PREF_HIGH
;
477 adv
.h
.nd_ra_reachable
= htonl(iface
->ra_reachabletime
);
478 adv
.h
.nd_ra_retransmit
= htonl(iface
->ra_retranstime
);
480 adv
.lladdr
.type
= ND_OPT_SOURCE_LINKADDR
;
482 odhcpd_get_mac(iface
, adv
.lladdr
.data
);
484 adv
.mtu
.nd_opt_mtu_type
= ND_OPT_MTU
;
485 adv
.mtu
.nd_opt_mtu_len
= 1;
488 mtu
= odhcpd_get_interface_config(iface
->ifname
, "mtu");
493 adv
.mtu
.nd_opt_mtu_mtu
= htonl(mtu
);
495 iov
[IOV_RA_ADV
].iov_base
= (char *)&adv
;
496 iov
[IOV_RA_ADV
].iov_len
= sizeof(adv
);
498 valid_addr_cnt
= (iface
->timer_rs
.cb
/* if not shutdown */ ? iface
->addr6_len
: 0);
499 invalid_addr_cnt
= iface
->invalid_addr6_len
;
502 if (iface
->default_router
) {
503 default_route
= true;
505 if (iface
->default_router
> 1)
509 if (valid_addr_cnt
+ invalid_addr_cnt
) {
510 addrs
= alloca(sizeof(*addrs
) * (valid_addr_cnt
+ invalid_addr_cnt
));
512 if (valid_addr_cnt
) {
513 memcpy(addrs
, iface
->addr6
, sizeof(*addrs
) * valid_addr_cnt
);
515 /* Check default route */
516 if (!default_route
&& parse_routes(addrs
, valid_addr_cnt
))
517 default_route
= true;
520 if (invalid_addr_cnt
) {
523 memcpy(&addrs
[valid_addr_cnt
], iface
->invalid_addr6
, sizeof(*addrs
) * invalid_addr_cnt
);
525 /* Remove invalid prefixes that were advertised 3 times */
526 while (i
< iface
->invalid_addr6_len
) {
527 if (++iface
->invalid_addr6
[i
].invalid_advertisements
>= 3) {
528 if (i
+ 1 < iface
->invalid_addr6_len
)
529 memmove(&iface
->invalid_addr6
[i
], &iface
->invalid_addr6
[i
+ 1], sizeof(*addrs
) * (iface
->invalid_addr6_len
- i
- 1));
531 iface
->invalid_addr6_len
--;
533 if (iface
->invalid_addr6_len
) {
534 struct odhcpd_ipaddr
*new_invalid_addr6
= realloc(iface
->invalid_addr6
, sizeof(*addrs
) * iface
->invalid_addr6_len
);
536 if (new_invalid_addr6
)
537 iface
->invalid_addr6
= new_invalid_addr6
;
539 free(iface
->invalid_addr6
);
540 iface
->invalid_addr6
= NULL
;
548 /* Construct Prefix Information options */
549 for (ssize_t i
= 0; i
< valid_addr_cnt
+ invalid_addr_cnt
; ++i
) {
550 struct odhcpd_ipaddr
*addr
= &addrs
[i
];
551 struct nd_opt_prefix_info
*p
= NULL
;
552 uint32_t preferred_lt
= 0;
553 uint32_t valid_lt
= 0;
555 if (addr
->prefix
> 96 || (i
< valid_addr_cnt
&& addr
->valid_lt
<= (uint32_t)now
)) {
556 syslog(LOG_INFO
, "Address %s (prefix %d, valid-lifetime %u) not suitable as RA prefix on %s",
557 inet_ntop(AF_INET6
, &addr
->addr
.in6
, buf
, sizeof(buf
)), addr
->prefix
,
558 addr
->valid_lt
, iface
->name
);
562 if (ADDR_MATCH_PIO_FILTER(addr
, iface
)) {
563 syslog(LOG_INFO
, "Address %s filtered out as RA prefix on %s",
564 inet_ntop(AF_INET6
, &addr
->addr
.in6
, buf
, sizeof(buf
)),
566 continue; /* PIO filtered out of this RA */
569 for (size_t i
= 0; i
< pfxs_cnt
; ++i
) {
570 if (addr
->prefix
== pfxs
[i
].nd_opt_pi_prefix_len
&&
571 !odhcpd_bmemcmp(&pfxs
[i
].nd_opt_pi_prefix
,
572 &addr
->addr
.in6
, addr
->prefix
))
577 struct nd_opt_prefix_info
*tmp
;
579 tmp
= realloc(pfxs
, sizeof(*pfxs
) * (pfxs_cnt
+ 1));
581 syslog(LOG_ERR
, "Realloc failed for RA prefix option on %s", iface
->name
);
586 p
= &pfxs
[pfxs_cnt
++];
587 memset(p
, 0, sizeof(*p
));
590 if (addr
->preferred_lt
> (uint32_t)now
) {
591 preferred_lt
= TIME_LEFT(addr
->preferred_lt
, now
);
593 if (preferred_lt
> iface
->preferred_lifetime
) {
594 /* set to possibly user mandated preferred_lt */
595 preferred_lt
= iface
->preferred_lifetime
;
599 if (addr
->valid_lt
> (uint32_t)now
) {
600 valid_lt
= TIME_LEFT(addr
->valid_lt
, now
);
602 if (iface
->ra_useleasetime
&& valid_lt
> iface
->dhcp_leasetime
)
603 valid_lt
= iface
->dhcp_leasetime
;
606 if (preferred_lt
> valid_lt
) {
609 * This value [AdvPreferredLifetime] MUST NOT be larger than
612 preferred_lt
= valid_lt
;
615 if (minvalid
> valid_lt
)
618 if ((!IN6_IS_ADDR_ULA(&addr
->addr
.in6
) || iface
->default_router
) && valid_lt
)
621 odhcpd_bmemcpy(&p
->nd_opt_pi_prefix
, &addr
->addr
.in6
,
622 (iface
->ra_advrouter
) ? 128 : addr
->prefix
);
623 p
->nd_opt_pi_type
= ND_OPT_PREFIX_INFORMATION
;
624 p
->nd_opt_pi_len
= 4;
625 p
->nd_opt_pi_prefix_len
= (addr
->prefix
< 64) ? 64 : addr
->prefix
;
626 p
->nd_opt_pi_flags_reserved
= 0;
627 if (!iface
->ra_not_onlink
)
628 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_ONLINK
;
629 if (iface
->ra_slaac
&& addr
->prefix
<= 64)
630 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_AUTO
;
631 if (iface
->ra_advrouter
)
632 p
->nd_opt_pi_flags_reserved
|= ND_OPT_PI_FLAG_RADDR
;
633 p
->nd_opt_pi_preferred_time
= htonl(preferred_lt
);
634 p
->nd_opt_pi_valid_time
= htonl(valid_lt
);
637 iov
[IOV_RA_PFXS
].iov_base
= (char *)pfxs
;
638 iov
[IOV_RA_PFXS
].iov_len
= pfxs_cnt
* sizeof(*pfxs
);
640 /* Calculate periodic transmit */
641 msecs
= calc_adv_interval(iface
, minvalid
, &maxival
);
642 lifetime
= calc_ra_lifetime(iface
, maxival
);
644 if (!iface
->have_link_local
) {
645 syslog(LOG_NOTICE
, "Skip sending a RA on %s as no link local address is available", iface
->name
);
649 if (default_route
&& valid_prefix
) {
650 adv
.h
.nd_ra_router_lifetime
= htons(lifetime
< UINT16_MAX
? lifetime
: UINT16_MAX
);
652 adv
.h
.nd_ra_router_lifetime
= 0;
655 syslog(LOG_WARNING
, "A default route is present but there is no public prefix "
656 "on %s thus we don't announce a default route by overriding ra_lifetime!", iface
->name
);
658 syslog(LOG_WARNING
, "No default route present, overriding ra_lifetime!");
662 syslog(LOG_DEBUG
, "Using a RA lifetime of %d seconds on %s", ntohs(adv
.h
.nd_ra_router_lifetime
), iface
->name
);
666 struct in6_addr dns_pref
, *dns_addr
= NULL
;
667 size_t dns_cnt
= 0, search_len
= iface
->search_len
;
668 uint8_t *search_domain
= iface
->search
;
669 uint8_t search_buf
[256];
671 /* DNS Recursive DNS */
672 if (iface
->dns_cnt
> 0) {
673 dns_addr
= iface
->dns
;
674 dns_cnt
= iface
->dns_cnt
;
675 } else if (!odhcpd_get_interface_dns_addr(iface
, &dns_pref
)) {
676 dns_addr
= &dns_pref
;
681 dns_sz
= sizeof(*dns
) + sizeof(struct in6_addr
)*dns_cnt
;
683 dns
= alloca(dns_sz
);
684 memset(dns
, 0, dns_sz
);
685 dns
->type
= ND_OPT_RECURSIVE_DNS
;
686 dns
->len
= 1 + (2 * dns_cnt
);
687 dns
->lifetime
= htonl(lifetime
);
688 memcpy(dns
->addr
, dns_addr
, sizeof(struct in6_addr
)*dns_cnt
);
691 /* DNS Search options */
692 if (!search_domain
&& !res_init() && _res
.dnsrch
[0] && _res
.dnsrch
[0][0]) {
693 int len
= dn_comp(_res
.dnsrch
[0], search_buf
,
694 sizeof(search_buf
), NULL
, NULL
);
696 search_domain
= search_buf
;
701 if (search_len
> 0) {
702 size_t search_padded
= ((search_len
+ 7) & (~7)) + 8;
704 search_sz
= sizeof(*search
) + search_padded
;
706 search
= alloca(search_sz
);
707 memset(search
, 0, search_sz
);
708 search
->type
= ND_OPT_DNS_SEARCH
;
709 search
->len
= search_len
? ((sizeof(*search
) + search_padded
) / 8) : 0;
710 search
->lifetime
= htonl(lifetime
);
711 memcpy(search
->name
, search_domain
, search_len
);
712 memset(&search
->name
[search_len
], 0, search_padded
- search_len
);
716 iov
[IOV_RA_DNS
].iov_base
= (char *)dns
;
717 iov
[IOV_RA_DNS
].iov_len
= dns_sz
;
718 iov
[IOV_RA_SEARCH
].iov_base
= (char *)search
;
719 iov
[IOV_RA_SEARCH
].iov_len
= search_sz
;
721 if (iface
->pref64_length
) {
722 /* RFC 8781 § 4.1 rounding up lifetime to multiply of 8 */
723 uint16_t pref64_lifetime
= lifetime
< (UINT16_MAX
- 7) ? lifetime
+ 7 : UINT16_MAX
;
724 uint8_t prefix_length_code
;
725 uint32_t mask_a1
, mask_a2
;
727 switch (iface
->pref64_length
) {
729 prefix_length_code
= 0;
730 mask_a1
= 0xffffffff;
731 mask_a2
= 0xffffffff;
734 prefix_length_code
= 1;
735 mask_a1
= 0xffffffff;
736 mask_a2
= 0x00000000;
739 prefix_length_code
= 2;
740 mask_a1
= 0xffffff00;
741 mask_a2
= 0x00000000;
744 prefix_length_code
= 3;
745 mask_a1
= 0xffff0000;
746 mask_a2
= 0x00000000;
749 prefix_length_code
= 4;
750 mask_a1
= 0xff000000;
751 mask_a2
= 0x00000000;
754 prefix_length_code
= 5;
755 mask_a1
= 0x00000000;
756 mask_a2
= 0x00000000;
759 syslog(LOG_WARNING
, "Invalid PREF64 prefix size (%d), "
760 "ignoring ra_pref64 option!", iface
->pref64_length
);
765 pref64_sz
= sizeof(*pref64
);
766 pref64
= alloca(pref64_sz
);
767 memset(pref64
, 0, pref64_sz
);
768 pref64
->type
= ND_OPT_PREF64
;
770 pref64
->lifetime_plc
= htons((0xfff8 & pref64_lifetime
) |
771 (0x7 & prefix_length_code
));
772 pref64
->addr
[0] = iface
->pref64_addr
.s6_addr32
[0];
773 pref64
->addr
[1] = iface
->pref64_addr
.s6_addr32
[1] & htonl(mask_a1
);
774 pref64
->addr
[2] = iface
->pref64_addr
.s6_addr32
[2] & htonl(mask_a2
);
777 iov
[IOV_RA_PREF64
].iov_base
= (char *)pref64
;
778 iov
[IOV_RA_PREF64
].iov_len
= pref64_sz
;
782 * L-3: An IPv6 CE router MUST advertise itself as a router for the
783 * delegated prefix(es) (and ULA prefix if configured to provide
784 * ULA addressing) using the "Route Information Option" specified
785 * in Section 2.3 of [RFC4191]. This advertisement is
786 * independent of having or not having IPv6 connectivity on the
790 for (ssize_t i
= 0; i
< valid_addr_cnt
; ++i
) {
791 struct odhcpd_ipaddr
*addr
= &addrs
[i
];
792 struct nd_opt_route_info
*tmp
;
795 if (addr
->dprefix
>= 64 || addr
->dprefix
== 0 || addr
->valid_lt
<= (uint32_t)now
) {
796 syslog(LOG_INFO
, "Address %s (dprefix %d, valid-lifetime %u) not suitable as RA route on %s",
797 inet_ntop(AF_INET6
, &addr
->addr
.in6
, buf
, sizeof(buf
)),
798 addr
->dprefix
, addr
->valid_lt
, iface
->name
);
800 continue; /* Address not suitable */
803 if (ADDR_MATCH_PIO_FILTER(addr
, iface
)) {
804 syslog(LOG_INFO
, "Address %s filtered out as RA route on %s",
805 inet_ntop(AF_INET6
, &addr
->addr
.in6
, buf
, sizeof(buf
)),
807 continue; /* PIO filtered out of this RA */
810 if (addr
->dprefix
> 32) {
811 addr
->addr
.in6
.s6_addr32
[1] &= htonl(~((1U << (64 - addr
->dprefix
)) - 1));
812 } else if (addr
->dprefix
<= 32) {
813 addr
->addr
.in6
.s6_addr32
[0] &= htonl(~((1U << (32 - addr
->dprefix
)) - 1));
814 addr
->addr
.in6
.s6_addr32
[1] = 0;
817 tmp
= realloc(routes
, sizeof(*routes
) * (routes_cnt
+ 1));
819 syslog(LOG_ERR
, "Realloc failed for RA route option on %s", iface
->name
);
825 memset(&routes
[routes_cnt
], 0, sizeof(*routes
));
826 routes
[routes_cnt
].type
= ND_OPT_ROUTE_INFO
;
827 routes
[routes_cnt
].len
= sizeof(*routes
) / 8;
828 routes
[routes_cnt
].prefix
= addr
->dprefix
;
829 routes
[routes_cnt
].flags
= 0;
830 if (iface
->route_preference
< 0)
831 routes
[routes_cnt
].flags
|= ND_RA_PREF_LOW
;
832 else if (iface
->route_preference
> 0)
833 routes
[routes_cnt
].flags
|= ND_RA_PREF_HIGH
;
835 valid_lt
= TIME_LEFT(addr
->valid_lt
, now
);
836 routes
[routes_cnt
].lifetime
= htonl(valid_lt
< lifetime
? valid_lt
: lifetime
);
837 routes
[routes_cnt
].addr
[0] = addr
->addr
.in6
.s6_addr32
[0];
838 routes
[routes_cnt
].addr
[1] = addr
->addr
.in6
.s6_addr32
[1];
839 routes
[routes_cnt
].addr
[2] = 0;
840 routes
[routes_cnt
].addr
[3] = 0;
845 iov
[IOV_RA_ROUTES
].iov_base
= (char *)routes
;
846 iov
[IOV_RA_ROUTES
].iov_len
= routes_cnt
* sizeof(*routes
);
848 memset(&adv_interval
, 0, sizeof(adv_interval
));
849 adv_interval
.nd_opt_adv_interval_type
= ND_OPT_RTR_ADV_INTERVAL
;
850 adv_interval
.nd_opt_adv_interval_len
= 1;
851 adv_interval
.nd_opt_adv_interval_ival
= htonl(maxival
*1000);
853 iov
[IOV_RA_ADV_INTERVAL
].iov_base
= (char *)&adv_interval
;
854 iov
[IOV_RA_ADV_INTERVAL
].iov_len
= adv_interval
.nd_opt_adv_interval_len
* 8;
856 memset(&dest
, 0, sizeof(dest
));
857 dest
.sin6_family
= AF_INET6
;
859 if (from
&& !IN6_IS_ADDR_UNSPECIFIED(from
))
860 dest
.sin6_addr
= *from
;
862 inet_pton(AF_INET6
, ALL_IPV6_NODES
, &dest
.sin6_addr
);
864 syslog(LOG_NOTICE
, "Sending a RA on %s", iface
->name
);
866 if (odhcpd_send(iface
->router_event
.uloop
.fd
, &dest
, iov
, ARRAY_SIZE(iov
), iface
) > 0)
877 static void trigger_router_advert(struct uloop_timeout
*event
)
879 struct interface
*iface
= container_of(event
, struct interface
, timer_rs
);
880 int msecs
= send_router_advert(iface
, NULL
);
882 /* Rearm timer if not shut down */
884 uloop_timeout_set(event
, msecs
);
888 /* Event handler for incoming ICMPv6 packets */
889 static void handle_icmpv6(void *addr
, void *data
, size_t len
,
890 struct interface
*iface
, _unused
void *dest
)
892 struct icmp6_hdr
*hdr
= data
;
893 struct sockaddr_in6
*from
= addr
;
895 if (!router_icmpv6_valid(addr
, data
, len
))
898 if ((iface
->ra
== MODE_SERVER
&& !iface
->master
)) { /* Server mode */
899 if (hdr
->icmp6_type
== ND_ROUTER_SOLICIT
)
900 send_router_advert(iface
, &from
->sin6_addr
);
901 } else if (iface
->ra
== MODE_RELAY
) { /* Relay mode */
902 if (hdr
->icmp6_type
== ND_ROUTER_SOLICIT
&& !iface
->master
) {
905 avl_for_each_element(&interfaces
, c
, avl
) {
906 if (!c
->master
|| c
->ra
!= MODE_RELAY
)
909 forward_router_solicitation(c
);
911 } else if (hdr
->icmp6_type
== ND_ROUTER_ADVERT
&& iface
->master
)
912 forward_router_advertisement(iface
, data
, len
);
917 /* Forward router solicitation */
918 static void forward_router_solicitation(const struct interface
*iface
)
920 struct icmp6_hdr rs
= {ND_ROUTER_SOLICIT
, 0, 0, {{0}}};
921 struct iovec iov
= {&rs
, sizeof(rs
)};
922 struct sockaddr_in6 all_routers
;
927 memset(&all_routers
, 0, sizeof(all_routers
));
928 all_routers
.sin6_family
= AF_INET6
;
929 inet_pton(AF_INET6
, ALL_IPV6_ROUTERS
, &all_routers
.sin6_addr
);
930 all_routers
.sin6_scope_id
= iface
->ifindex
;
932 syslog(LOG_NOTICE
, "Sending RS to %s", iface
->name
);
933 odhcpd_send(iface
->router_event
.uloop
.fd
, &all_routers
, &iov
, 1, iface
);
937 /* Handler for incoming router solicitations on slave interfaces */
938 static void forward_router_advertisement(const struct interface
*iface
, uint8_t *data
, size_t len
)
940 struct nd_router_advert
*adv
= (struct nd_router_advert
*)data
;
941 struct sockaddr_in6 all_nodes
;
942 struct icmpv6_opt
*opt
;
944 struct iovec iov
= { .iov_base
= data
, .iov_len
= len
};
945 /* Rewrite options */
946 uint8_t *end
= data
+ len
;
947 uint8_t *mac_ptr
= NULL
;
948 struct in6_addr
*dns_ptr
= NULL
;
949 size_t dns_count
= 0;
951 icmpv6_for_each_option(opt
, &adv
[1], end
) {
952 if (opt
->type
== ND_OPT_SOURCE_LINKADDR
) {
953 /* Store address of source MAC-address */
955 } else if (opt
->type
== ND_OPT_RECURSIVE_DNS
&& opt
->len
> 1) {
956 /* Check if we have to rewrite DNS */
957 dns_ptr
= (struct in6_addr
*)&opt
->data
[6];
958 dns_count
= (opt
->len
- 1) / 2;
962 syslog(LOG_NOTICE
, "Got a RA on %s", iface
->name
);
964 /* Indicate a proxy, however we don't follow the rest of RFC 4389 yet */
965 adv
->nd_ra_flags_reserved
|= ND_RA_FLAG_PROXY
;
967 /* Forward advertisement to all slave interfaces */
968 memset(&all_nodes
, 0, sizeof(all_nodes
));
969 all_nodes
.sin6_family
= AF_INET6
;
970 inet_pton(AF_INET6
, ALL_IPV6_NODES
, &all_nodes
.sin6_addr
);
972 avl_for_each_element(&interfaces
, c
, avl
) {
973 if (c
->ra
!= MODE_RELAY
|| c
->master
)
976 /* Fixup source hardware address option */
978 odhcpd_get_mac(c
, mac_ptr
);
980 /* If we have to rewrite DNS entries */
981 if (c
->always_rewrite_dns
&& dns_ptr
&& dns_count
> 0) {
982 const struct in6_addr
*rewrite
= c
->dns
;
983 struct in6_addr addr
;
984 size_t rewrite_cnt
= c
->dns_cnt
;
986 if (rewrite_cnt
== 0) {
987 if (odhcpd_get_interface_dns_addr(c
, &addr
))
988 continue; /* Unable to comply */
994 /* Copy over any other addresses */
995 for (size_t i
= 0; i
< dns_count
; ++i
) {
996 size_t j
= (i
< rewrite_cnt
) ? i
: rewrite_cnt
- 1;
997 dns_ptr
[i
] = rewrite
[j
];
1001 syslog(LOG_NOTICE
, "Forward a RA on %s", c
->name
);
1003 odhcpd_send(c
->router_event
.uloop
.fd
, &all_nodes
, &iov
, 1, c
);