2 /* $KAME: rtadvd.c,v 1.82 2003/08/05 12:34:23 itojun Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
40 #include <sys/queue.h>
42 #include <sys/sysctl.h>
45 #include <net/if_types.h>
46 #include <net/if_media.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/ip6.h>
51 #include <netinet6/ip6_var.h>
52 #include <netinet/icmp6.h>
54 #include <arpa/inet.h>
56 #include <netinet/in_var.h>
57 #include <netinet6/nd6.h>
73 #include "pathnames.h"
78 #include "timer_subr.h"
82 #include "control_server.h"
84 #define RTADV_TYPE2BITMASK(type) (0x1 << type)
86 struct msghdr rcvmhdr;
87 static char *rcvcmsgbuf;
88 static size_t rcvcmsgbuflen;
89 static char *sndcmsgbuf = NULL;
90 static size_t sndcmsgbuflen;
91 struct msghdr sndmhdr;
92 struct iovec rcviov[2];
93 struct iovec sndiov[2];
94 struct sockaddr_in6 rcvfrom;
95 static const char *pidfilename = _PATH_RTADVDPID;
96 const char *conffile = _PATH_RTADVDCONF;
97 static struct pidfh *pfh;
98 static int dflag, sflag;
99 static int wait_shutdown;
101 #define PFD_RAWSOCK 0
106 struct railist_head_t railist =
107 TAILQ_HEAD_INITIALIZER(railist);
108 struct ifilist_head_t ifilist =
109 TAILQ_HEAD_INITIALIZER(ifilist);
112 TAILQ_ENTRY(nd_optlist) nol_next;
113 struct nd_opt_hdr *nol_opt;
116 struct nd_opt_hdr *opt_array[9];
118 struct nd_opt_hdr *zero;
119 struct nd_opt_hdr *src_lladdr;
120 struct nd_opt_hdr *tgt_lladdr;
121 struct nd_opt_prefix_info *pi;
122 struct nd_opt_rd_hdr *rh;
123 struct nd_opt_mtu *mtu;
124 TAILQ_HEAD(, nd_optlist) opt_list;
127 #define opt_src_lladdr nd_opt_each.src_lladdr
128 #define opt_tgt_lladdr nd_opt_each.tgt_lladdr
129 #define opt_pi nd_opt_each.pi
130 #define opt_rh nd_opt_each.rh
131 #define opt_mtu nd_opt_each.mtu
132 #define opt_list nd_opt_each.opt_list
134 #define NDOPT_FLAG_SRCLINKADDR (1 << 0)
135 #define NDOPT_FLAG_TGTLINKADDR (1 << 1)
136 #define NDOPT_FLAG_PREFIXINFO (1 << 2)
137 #define NDOPT_FLAG_RDHDR (1 << 3)
138 #define NDOPT_FLAG_MTU (1 << 4)
139 #define NDOPT_FLAG_RDNSS (1 << 5)
140 #define NDOPT_FLAG_DNSSL (1 << 6)
142 static uint32_t ndopt_flags[] = {
143 [ND_OPT_SOURCE_LINKADDR] = NDOPT_FLAG_SRCLINKADDR,
144 [ND_OPT_TARGET_LINKADDR] = NDOPT_FLAG_TGTLINKADDR,
145 [ND_OPT_PREFIX_INFORMATION] = NDOPT_FLAG_PREFIXINFO,
146 [ND_OPT_REDIRECTED_HEADER] = NDOPT_FLAG_RDHDR,
147 [ND_OPT_MTU] = NDOPT_FLAG_MTU,
148 [ND_OPT_RDNSS] = NDOPT_FLAG_RDNSS,
149 [ND_OPT_DNSSL] = NDOPT_FLAG_DNSSL,
152 static void rtadvd_shutdown(void);
153 static void sock_open(struct sockinfo *);
154 static void rtsock_open(struct sockinfo *);
155 static void rtadvd_input(struct sockinfo *);
156 static void rs_input(int, struct nd_router_solicit *,
157 struct in6_pktinfo *, struct sockaddr_in6 *);
158 static void ra_input(int, struct nd_router_advert *,
159 struct in6_pktinfo *, struct sockaddr_in6 *);
160 static int prefix_check(struct nd_opt_prefix_info *, struct rainfo *,
161 struct sockaddr_in6 *);
162 static int nd6_options(struct nd_opt_hdr *, int,
163 union nd_opt *, uint32_t);
164 static void free_ndopts(union nd_opt *);
165 static void rtmsg_input(struct sockinfo *);
166 static void set_short_delay(struct ifinfo *);
167 static int check_accept_rtadv(int);
173 fprintf(stderr, "usage: rtadvd [-dDfRs] "
174 "[-c configfile] [-C ctlsock] [-M ifname] [-p pidfile]\n");
179 main(int argc, char *argv[])
181 struct pollfd set[PFD_MAX];
182 struct timespec *timeout;
184 int fflag = 0, logopt;
188 /* get command line options and arguments */
189 while ((ch = getopt(argc, argv, "c:C:dDfhM:p:Rs")) != -1) {
195 ctrlsock.si_name = optarg;
210 fprintf(stderr, "rtadvd: "
211 "the -R option is currently ignored.\n");
219 pidfilename = optarg;
228 logopt = LOG_NDELAY | LOG_PID;
230 logopt |= LOG_PERROR;
231 openlog("rtadvd", logopt, LOG_DAEMON);
235 (void)setlogmask(LOG_UPTO(LOG_DEBUG));
237 (void)setlogmask(LOG_UPTO(LOG_INFO));
239 (void)setlogmask(LOG_UPTO(LOG_NOTICE));
241 (void)setlogmask(LOG_UPTO(LOG_ERR));
243 /* timer initialization */
246 pfh = pidfile_open(pidfilename, 0600, &otherpid);
249 errx(1, "%s already running, pid: %d",
250 getprogname(), otherpid);
252 "failed to open the pid file %s, run anyway.",
260 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
261 for (i = 0; i < argc; i++)
262 update_persist_ifinfo(&ifilist, argv[i]);
264 csock_open(&ctrlsock, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
265 if (ctrlsock.si_fd == -1) {
266 syslog(LOG_ERR, "cannot open control socket: %s",
271 /* record the current PID */
275 set[PFD_RAWSOCK].fd = sock.si_fd;
276 set[PFD_RAWSOCK].events = POLLIN;
278 rtsock_open(&rtsock);
279 set[PFD_RTSOCK].fd = rtsock.si_fd;
280 set[PFD_RTSOCK].events = POLLIN;
282 set[PFD_RTSOCK].fd = -1;
283 set[PFD_CSOCK].fd = ctrlsock.si_fd;
284 set[PFD_CSOCK].events = POLLIN;
285 signal(SIGTERM, set_do_shutdown);
286 signal(SIGINT, set_do_shutdown);
287 signal(SIGHUP, set_do_reload);
289 error = csock_listen(&ctrlsock);
291 syslog(LOG_ERR, "cannot listen control socket: %s",
296 /* load configuration file */
300 if (is_do_shutdown())
303 if (is_do_reload()) {
304 loadconfig_ifname(reload_ifname());
305 if (reload_ifname() == NULL)
307 "configuration file reloaded.");
310 "configuration file for %s reloaded.",
315 /* timeout handler update for active interfaces */
316 rtadvd_update_timeout_handler();
318 /* timer expiration check and reset the timer */
319 timeout = rtadvd_check_timer();
321 if (timeout != NULL) {
323 "<%s> set timer to %ld:%ld. waiting for "
324 "inputs or timeout", __func__,
325 (long int)timeout->tv_sec,
326 (long int)timeout->tv_nsec / 1000);
329 "<%s> there's no timer. waiting for inputs",
332 if ((i = poll(set, sizeof(set)/sizeof(set[0]),
333 timeout ? (timeout->tv_sec * 1000 +
334 timeout->tv_nsec / 1000 / 1000) : INFTIM)) < 0) {
336 /* EINTR would occur if a signal was delivered */
338 syslog(LOG_ERR, "poll() failed: %s",
342 if (i == 0) /* timeout */
344 if (rtsock.si_fd != -1 && set[PFD_RTSOCK].revents & POLLIN)
345 rtmsg_input(&rtsock);
347 if (set[PFD_RAWSOCK].revents & POLLIN)
350 if (set[PFD_CSOCK].revents & POLLIN) {
353 fd = csock_accept(&ctrlsock);
356 "cannot accept() control socket: %s",
359 cm_handler_server(fd);
364 exit(0); /* NOTREACHED */
368 rtadvd_shutdown(void)
377 "waiting expiration of the all RA timers.");
379 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
381 * Ignore !IFF_UP interfaces in waiting for shutdown.
383 if (!(ifi->ifi_flags & IFF_UP) &&
384 ifi->ifi_ra_timer != NULL) {
385 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
386 rtadvd_remove_timer(ifi->ifi_ra_timer);
387 ifi->ifi_ra_timer = NULL;
388 syslog(LOG_DEBUG, "<%s> %s(idx=%d) is down. "
389 "Timer removed and marked as UNCONFIGURED.",
390 __func__, ifi->ifi_ifname,
394 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
395 if (ifi->ifi_ra_timer != NULL)
399 syslog(LOG_NOTICE, "gracefully terminated.");
407 syslog(LOG_DEBUG, "<%s> cease to be an advertising router",
412 TAILQ_FOREACH(rai, &railist, rai_next) {
413 rai->rai_lifetime = 0;
414 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next)
416 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next)
419 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
420 if (!ifi->ifi_persist)
422 if (ifi->ifi_state == IFI_STATE_UNCONFIGURED)
424 if (ifi->ifi_ra_timer == NULL)
426 if (ifi->ifi_ra_lastsent.tv_sec == 0 &&
427 ifi->ifi_ra_lastsent.tv_nsec == 0 &&
428 ifi->ifi_ra_timer != NULL) {
430 * When RA configured but never sent,
431 * ignore the IF immediately.
433 rtadvd_remove_timer(ifi->ifi_ra_timer);
434 ifi->ifi_ra_timer = NULL;
435 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
439 ifi->ifi_state = IFI_STATE_TRANSITIVE;
441 /* Mark as the shut-down state. */
442 ifi->ifi_rainfo_trans = ifi->ifi_rainfo;
443 ifi->ifi_rainfo = NULL;
445 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
446 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
448 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
449 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
452 syslog(LOG_NOTICE, "final RA transmission started.");
455 csock_close(&ctrlsock);
459 rtmsg_input(struct sockinfo *s)
461 int n, type, ifindex = 0, plen;
463 char msg[2048], *next, *lim;
464 char ifname[IFNAMSIZ];
465 struct if_announcemsghdr *ifan;
466 struct rt_msghdr *rtm;
469 struct in6_addr *addr;
471 char addrbuf[INET6_ADDRSTRLEN];
472 int prefixchange = 0;
475 syslog(LOG_ERR, "<%s> internal error", __func__);
478 n = read(s->si_fd, msg, sizeof(msg));
479 rtm = (struct rt_msghdr *)msg;
480 syslog(LOG_DEBUG, "<%s> received a routing message "
481 "(type = %d, len = %d)", __func__, rtm->rtm_type, n);
483 if (n > rtm->rtm_msglen) {
485 * This usually won't happen for messages received on
489 "<%s> received data length is larger than "
490 "1st routing message len. multiple messages? "
491 "read %d bytes, but 1st msg len = %d",
492 __func__, n, rtm->rtm_msglen);
500 for (next = msg; next < lim; next += len) {
503 next = get_next_msg(next, lim, 0, &len,
504 RTADV_TYPE2BITMASK(RTM_ADD) |
505 RTADV_TYPE2BITMASK(RTM_DELETE) |
506 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
507 RTADV_TYPE2BITMASK(RTM_DELADDR) |
508 RTADV_TYPE2BITMASK(RTM_IFINFO) |
509 RTADV_TYPE2BITMASK(RTM_IFANNOUNCE));
512 type = ((struct rt_msghdr *)next)->rtm_type;
516 ifindex = get_rtm_ifindex(next);
520 ifindex = (int)((struct ifa_msghdr *)next)->ifam_index;
523 ifindex = (int)((struct if_msghdr *)next)->ifm_index;
526 ifan = (struct if_announcemsghdr *)next;
527 switch (ifan->ifan_what) {
533 "<%s:%d> unknown ifan msg (ifan_what=%d)",
534 __func__, __LINE__, ifan->ifan_what);
538 syslog(LOG_DEBUG, "<%s>: if_announcemsg (idx=%d:%d)",
539 __func__, ifan->ifan_index, ifan->ifan_what);
540 switch (ifan->ifan_what) {
543 "interface added (idx=%d)",
545 update_ifinfo(&ifilist, ifan->ifan_index);
546 loadconfig_index(ifan->ifan_index);
550 "interface removed (idx=%d)",
552 rm_ifinfo_index(ifan->ifan_index);
554 /* Clear ifi_ifindex */
555 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
557 == ifan->ifan_index) {
558 ifi->ifi_ifindex = 0;
562 update_ifinfo(&ifilist, ifan->ifan_index);
567 /* should not reach here */
569 "<%s:%d> unknown rtmsg %d on %s",
570 __func__, __LINE__, type,
571 if_indextoname(ifindex, ifname));
574 ifi = if_indextoifinfo(ifindex);
577 "<%s> ifinfo not found for idx=%d. Why?",
581 rai = ifi->ifi_rainfo;
584 "<%s> route changed on "
585 "non advertising interface(%s)",
586 __func__, ifi->ifi_ifname);
590 oldifflags = ifi->ifi_flags;
591 /* init ifflags because it may have changed */
592 update_ifinfo(&ifilist, ifindex);
597 break; /* we aren't interested in prefixes */
599 addr = get_addr(msg);
600 plen = get_prefixlen(msg);
601 /* sanity check for plen */
602 /* as RFC2373, prefixlen is at least 4 */
603 if (plen < 4 || plen > 127) {
604 syslog(LOG_INFO, "<%s> new interface route's"
605 "plen %d is invalid for a prefix",
609 pfx = find_prefix(rai, addr, plen);
611 if (pfx->pfx_timer) {
613 * If the prefix has been invalidated,
614 * make it available again.
620 "<%s> new prefix(%s/%d) "
622 "but it was already in list",
624 inet_ntop(AF_INET6, addr,
627 plen, ifi->ifi_ifname);
630 make_prefix(rai, ifindex, addr, plen);
637 addr = get_addr(msg);
638 plen = get_prefixlen(msg);
639 /* sanity check for plen */
640 /* as RFC2373, prefixlen is at least 4 */
641 if (plen < 4 || plen > 127) {
643 "<%s> deleted interface route's "
644 "plen %d is invalid for a prefix",
648 pfx = find_prefix(rai, addr, plen);
651 "<%s> prefix(%s/%d) was deleted on %s, "
652 "but it was not in list",
653 __func__, inet_ntop(AF_INET6, addr,
654 (char *)addrbuf, sizeof(addrbuf)),
655 plen, ifi->ifi_ifname);
658 invalidate_prefix(pfx);
666 /* should not reach here */
668 "<%s:%d> unknown rtmsg %d on %s",
669 __func__, __LINE__, type,
670 if_indextoname(ifindex, ifname));
674 /* check if an interface flag is changed */
675 if ((oldifflags & IFF_UP) && /* UP to DOWN */
676 !(ifi->ifi_flags & IFF_UP)) {
678 "<interface %s becomes down. stop timer.",
680 rtadvd_remove_timer(ifi->ifi_ra_timer);
681 ifi->ifi_ra_timer = NULL;
682 } else if (!(oldifflags & IFF_UP) && /* DOWN to UP */
683 (ifi->ifi_flags & IFF_UP)) {
685 "interface %s becomes up. restart timer.",
688 ifi->ifi_state = IFI_STATE_TRANSITIVE;
689 ifi->ifi_burstcount =
690 MAX_INITIAL_RTR_ADVERTISEMENTS;
691 ifi->ifi_burstinterval =
692 MAX_INITIAL_RTR_ADVERT_INTERVAL;
694 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
695 ra_timer_update, ifi, ifi);
696 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
697 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
699 } else if (prefixchange &&
700 (ifi->ifi_flags & IFF_UP)) {
702 * An advertised prefix has been added or invalidated.
703 * Will notice the change in a short delay.
705 set_short_delay(ifi);
713 rtadvd_input(struct sockinfo *s)
720 struct icmp6_hdr *icp;
723 struct in6_pktinfo *pi = NULL;
724 char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
725 struct in6_addr dst = in6addr_any;
728 syslog(LOG_DEBUG, "<%s> enter", __func__);
731 syslog(LOG_ERR, "<%s> internal error", __func__);
735 * Get message. We reset msg_controllen since the field could
736 * be modified if we had received a message before setting
739 rcvmhdr.msg_controllen = rcvcmsgbuflen;
740 if ((i = recvmsg(s->si_fd, &rcvmhdr, 0)) < 0)
743 /* extract optional information via Advanced API */
744 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
746 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
747 if (cm->cmsg_level == IPPROTO_IPV6 &&
748 cm->cmsg_type == IPV6_PKTINFO &&
749 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
750 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
751 ifindex = pi->ipi6_ifindex;
754 if (cm->cmsg_level == IPPROTO_IPV6 &&
755 cm->cmsg_type == IPV6_HOPLIMIT &&
756 cm->cmsg_len == CMSG_LEN(sizeof(int)))
757 hlimp = (int *)CMSG_DATA(cm);
760 syslog(LOG_ERR, "failed to get receiving interface");
764 syslog(LOG_ERR, "failed to get receiving hop limit");
769 * If we happen to receive data on an interface which is now gone
770 * or down, just discard the data.
772 ifi = if_indextoifinfo(pi->ipi6_ifindex);
773 if (ifi == NULL || !(ifi->ifi_flags & IFF_UP)) {
775 "<%s> received data on a disabled interface (%s)",
777 (ifi == NULL) ? "[gone]" : ifi->ifi_ifname);
782 if ((size_t)i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
784 "packet size(%d) is too short", i);
788 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
789 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
791 if ((size_t)i < sizeof(struct icmp6_hdr)) {
792 syslog(LOG_ERR, "packet size(%zd) is too short", i);
796 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
799 switch (icp->icmp6_type) {
800 case ND_ROUTER_SOLICIT:
802 * Message verification - RFC 4861 6.1.1
803 * XXX: these checks must be done in the kernel as well,
804 * but we can't completely rely on them.
808 "RS with invalid hop limit(%d) "
809 "received from %s on %s",
811 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
813 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
816 if (icp->icmp6_code) {
818 "RS with invalid ICMP6 code(%d) "
819 "received from %s on %s",
821 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
823 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
826 if ((size_t)i < sizeof(struct nd_router_solicit)) {
828 "RS from %s on %s does not have enough "
829 "length (len = %zd)",
830 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
832 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
835 rs_input(i, (struct nd_router_solicit *)icp, pi, &rcvfrom);
837 case ND_ROUTER_ADVERT:
839 * Message verification - RFC 4861 6.1.2
840 * XXX: there's the same dilemma as above...
842 if (!IN6_IS_ADDR_LINKLOCAL(&rcvfrom.sin6_addr)) {
844 "RA with non-linklocal source address "
845 "received from %s on %s",
846 inet_ntop(AF_INET6, &rcvfrom.sin6_addr,
847 ntopbuf, sizeof(ntopbuf)),
848 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
853 "RA with invalid hop limit(%d) "
854 "received from %s on %s",
856 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
858 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
861 if (icp->icmp6_code) {
863 "RA with invalid ICMP6 code(%d) "
864 "received from %s on %s",
866 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
868 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
871 if ((size_t)i < sizeof(struct nd_router_advert)) {
873 "RA from %s on %s does not have enough "
874 "length (len = %zd)",
875 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
877 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
880 ra_input(i, (struct nd_router_advert *)icp, pi, &rcvfrom);
882 case ICMP6_ROUTER_RENUMBERING:
883 if (mcastif == NULL) {
884 syslog(LOG_ERR, "received a router renumbering "
885 "message, but not allowed to be accepted");
888 rr_input(i, (struct icmp6_router_renum *)icp, pi, &rcvfrom,
893 * Note that this case is POSSIBLE, especially just
894 * after invocation of the daemon. This is because we
895 * could receive message after opening the socket and
896 * before setting ICMP6 type filter(see sock_open()).
898 syslog(LOG_ERR, "invalid icmp type(%d)", icp->icmp6_type);
906 rs_input(int len, struct nd_router_solicit *rs,
907 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
909 char ntopbuf[INET6_ADDRSTRLEN];
910 char ifnamebuf[IFNAMSIZ];
914 struct soliciter *sol;
917 "<%s> RS received from %s on %s",
919 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
920 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
922 /* ND option check */
923 memset(&ndopts, 0, sizeof(ndopts));
924 TAILQ_INIT(&ndopts.opt_list);
925 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
926 len - sizeof(struct nd_router_solicit),
927 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
929 "<%s> ND option check failed for an RS from %s on %s",
931 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
933 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
938 * If the IP source address is the unspecified address, there
939 * must be no source link-layer address option in the message.
942 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
943 ndopts.opt_src_lladdr) {
945 "<%s> RS from unspecified src on %s has a link-layer"
947 __func__, if_indextoname(pi->ipi6_ifindex, ifnamebuf));
951 ifi = if_indextoifinfo(pi->ipi6_ifindex);
954 "<%s> if (idx=%d) not found. Why?",
955 __func__, pi->ipi6_ifindex);
958 rai = ifi->ifi_rainfo;
961 "<%s> RS received on non advertising interface(%s)",
963 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
967 rai->rai_ifinfo->ifi_rsinput++;
970 * Decide whether to send RA according to the rate-limit
974 /* record sockaddr waiting for RA, if possible */
975 sol = (struct soliciter *)malloc(sizeof(*sol));
977 sol->sol_addr = *from;
978 /* XXX RFC 2553 need clarification on flowinfo */
979 sol->sol_addr.sin6_flowinfo = 0;
980 TAILQ_INSERT_TAIL(&rai->rai_soliciter, sol, sol_next);
984 * If there is already a waiting RS packet, don't
987 if (ifi->ifi_rs_waitcount++)
990 set_short_delay(ifi);
993 free_ndopts(&ndopts);
998 set_short_delay(struct ifinfo *ifi)
1000 long delay; /* must not be greater than 1000000 */
1001 struct timespec interval, now, min_delay, tm_tmp, *rest;
1003 if (ifi->ifi_ra_timer == NULL)
1006 * Compute a random delay. If the computed value
1007 * corresponds to a time later than the time the next
1008 * multicast RA is scheduled to be sent, ignore the random
1009 * delay and send the advertisement at the
1010 * already-scheduled time. RFC 4861 6.2.6
1012 delay = arc4random_uniform(MAX_RA_DELAY_TIME);
1013 interval.tv_sec = 0;
1014 interval.tv_nsec = delay * 1000;
1015 rest = rtadvd_timer_rest(ifi->ifi_ra_timer);
1016 if (TS_CMP(rest, &interval, <)) {
1017 syslog(LOG_DEBUG, "<%s> random delay is larger than "
1018 "the rest of the current timer", __func__);
1023 * If we sent a multicast Router Advertisement within
1024 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
1025 * the advertisement to be sent at a time corresponding to
1026 * MIN_DELAY_BETWEEN_RAS plus the random value after the
1027 * previous advertisement was sent.
1029 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1030 TS_SUB(&now, &ifi->ifi_ra_lastsent, &tm_tmp);
1031 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
1032 min_delay.tv_nsec = 0;
1033 if (TS_CMP(&tm_tmp, &min_delay, <)) {
1034 TS_SUB(&min_delay, &tm_tmp, &min_delay);
1035 TS_ADD(&min_delay, &interval, &interval);
1037 rtadvd_set_timer(&interval, ifi->ifi_ra_timer);
1041 check_accept_rtadv(int idx)
1045 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1046 if (ifi->ifi_ifindex == idx)
1051 "<%s> if (idx=%d) not found. Why?",
1055 #if (__FreeBSD_version < 900000)
1057 * RA_RECV: !ip6.forwarding && ip6.accept_rtadv
1058 * RA_SEND: ip6.forwarding
1060 return ((getinet6sysctl(IPV6CTL_FORWARDING) == 0) &&
1061 (getinet6sysctl(IPV6CTL_ACCEPT_RTADV) == 1));
1064 * RA_RECV: ND6_IFF_ACCEPT_RTADV
1065 * RA_SEND: ip6.forwarding
1067 if (update_ifinfo_nd_flags(ifi) != 0) {
1068 syslog(LOG_ERR, "cannot get nd6 flags (idx=%d)", idx);
1072 return (ifi->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV);
1077 ra_input(int len, struct nd_router_advert *nra,
1078 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
1082 char ntopbuf[INET6_ADDRSTRLEN];
1083 char ifnamebuf[IFNAMSIZ];
1084 union nd_opt ndopts;
1085 const char *on_off[] = {"OFF", "ON"};
1086 uint32_t reachabletime, retranstimer, mtu;
1087 int inconsistent = 0;
1090 syslog(LOG_DEBUG, "<%s> RA received from %s on %s", __func__,
1091 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
1092 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
1094 /* ND option check */
1095 memset(&ndopts, 0, sizeof(ndopts));
1096 TAILQ_INIT(&ndopts.opt_list);
1097 error = nd6_options((struct nd_opt_hdr *)(nra + 1),
1098 len - sizeof(struct nd_router_advert), &ndopts,
1099 NDOPT_FLAG_SRCLINKADDR | NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU |
1100 NDOPT_FLAG_RDNSS | NDOPT_FLAG_DNSSL);
1103 "<%s> ND option check failed for an RA from %s on %s",
1105 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1106 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1112 * RA consistency check according to RFC 4861 6.2.7
1114 ifi = if_indextoifinfo(pi->ipi6_ifindex);
1115 if (ifi->ifi_rainfo == NULL) {
1117 "<%s> received RA from %s on non-advertising"
1120 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1121 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1125 rai = ifi->ifi_rainfo;
1127 syslog(LOG_DEBUG, "<%s> ifi->ifi_rainput = %" PRIu64, __func__,
1130 /* Cur Hop Limit value */
1131 if (nra->nd_ra_curhoplimit && rai->rai_hoplimit &&
1132 nra->nd_ra_curhoplimit != rai->rai_hoplimit) {
1134 "CurHopLimit inconsistent on %s:"
1135 " %d from %s, %d from us",
1136 ifi->ifi_ifname, nra->nd_ra_curhoplimit,
1137 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1138 sizeof(ntopbuf)), rai->rai_hoplimit);
1142 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
1143 rai->rai_managedflg) {
1145 "M flag inconsistent on %s:"
1146 " %s from %s, %s from us",
1147 ifi->ifi_ifname, on_off[!rai->rai_managedflg],
1148 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1149 sizeof(ntopbuf)), on_off[rai->rai_managedflg]);
1153 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
1154 rai->rai_otherflg) {
1156 "O flag inconsistent on %s:"
1157 " %s from %s, %s from us",
1158 ifi->ifi_ifname, on_off[!rai->rai_otherflg],
1159 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1160 sizeof(ntopbuf)), on_off[rai->rai_otherflg]);
1163 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
1164 /* S "IPv6-Only" (Six, Silence-IPv4) flag */
1165 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_IPV6_ONLY) !=
1166 rai->rai_ipv6onlyflg) {
1168 "S flag inconsistent on %s:"
1169 " %s from %s, %s from us",
1170 ifi->ifi_ifname, on_off[!rai->rai_ipv6onlyflg],
1171 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1172 sizeof(ntopbuf)), on_off[rai->rai_ipv6onlyflg]);
1176 /* Reachable Time */
1177 reachabletime = ntohl(nra->nd_ra_reachable);
1178 if (reachabletime && rai->rai_reachabletime &&
1179 reachabletime != rai->rai_reachabletime) {
1181 "ReachableTime inconsistent on %s:"
1182 " %d from %s, %d from us",
1183 ifi->ifi_ifname, reachabletime,
1184 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1185 sizeof(ntopbuf)), rai->rai_reachabletime);
1189 retranstimer = ntohl(nra->nd_ra_retransmit);
1190 if (retranstimer && rai->rai_retranstimer &&
1191 retranstimer != rai->rai_retranstimer) {
1193 "RetranceTimer inconsistent on %s:"
1194 " %d from %s, %d from us",
1195 ifi->ifi_ifname, retranstimer,
1196 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1197 sizeof(ntopbuf)), rai->rai_retranstimer);
1200 /* Values in the MTU options */
1201 if (ndopts.opt_mtu) {
1202 mtu = ntohl(ndopts.opt_mtu->nd_opt_mtu_mtu);
1203 if (mtu && rai->rai_linkmtu && mtu != rai->rai_linkmtu) {
1205 "MTU option value inconsistent on %s:"
1206 " %d from %s, %d from us",
1207 ifi->ifi_ifname, mtu,
1208 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1209 sizeof(ntopbuf)), rai->rai_linkmtu);
1213 /* Preferred and Valid Lifetimes for prefixes */
1215 struct nd_optlist *nol;
1218 if (prefix_check(ndopts.opt_pi, rai, from))
1221 TAILQ_FOREACH(nol, &ndopts.opt_list, nol_next)
1222 if (prefix_check((struct nd_opt_prefix_info *)nol->nol_opt,
1228 ifi->ifi_rainconsistent++;
1231 free_ndopts(&ndopts);
1236 udiff(uint32_t u, uint32_t v)
1238 return (u >= v ? u - v : v - u);
1241 /* return a non-zero value if the received prefix is inconsistent with ours */
1243 prefix_check(struct nd_opt_prefix_info *pinfo,
1244 struct rainfo *rai, struct sockaddr_in6 *from)
1247 uint32_t preferred_time, valid_time;
1249 int inconsistent = 0;
1250 char ntopbuf[INET6_ADDRSTRLEN];
1251 char prefixbuf[INET6_ADDRSTRLEN];
1252 struct timespec now;
1254 #if 0 /* impossible */
1255 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1258 ifi = rai->rai_ifinfo;
1260 * log if the adveritsed prefix has link-local scope(sanity check?)
1262 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix))
1264 "<%s> link-local prefix %s/%d is advertised "
1267 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1269 pinfo->nd_opt_pi_prefix_len,
1270 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1271 sizeof(ntopbuf)), ifi->ifi_ifname);
1273 if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1274 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1276 "<%s> prefix %s/%d from %s on %s is not in our list",
1278 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1280 pinfo->nd_opt_pi_prefix_len,
1281 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1282 sizeof(ntopbuf)), ifi->ifi_ifname);
1286 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1287 if (pfx->pfx_pltimeexpire) {
1289 * The lifetime is decremented in real time, so we should
1290 * compare the expiration time.
1291 * (RFC 2461 Section 6.2.7.)
1292 * XXX: can we really expect that all routers on the link
1293 * have synchronized clocks?
1295 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1296 preferred_time += now.tv_sec;
1298 if (!pfx->pfx_timer && rai->rai_clockskew &&
1299 udiff(preferred_time, pfx->pfx_pltimeexpire) > rai->rai_clockskew) {
1301 "<%s> preferred lifetime for %s/%d"
1302 " (decr. in real time) inconsistent on %s:"
1303 " %" PRIu32 " from %s, %" PRIu32 " from us",
1305 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1307 pinfo->nd_opt_pi_prefix_len,
1308 ifi->ifi_ifname, preferred_time,
1309 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1310 sizeof(ntopbuf)), pfx->pfx_pltimeexpire);
1313 } else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime)
1315 "<%s> preferred lifetime for %s/%d"
1316 " inconsistent on %s:"
1317 " %d from %s, %d from us",
1319 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1321 pinfo->nd_opt_pi_prefix_len,
1322 ifi->ifi_ifname, preferred_time,
1323 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1324 sizeof(ntopbuf)), pfx->pfx_preflifetime);
1326 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1327 if (pfx->pfx_vltimeexpire) {
1328 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1329 valid_time += now.tv_sec;
1331 if (!pfx->pfx_timer && rai->rai_clockskew &&
1332 udiff(valid_time, pfx->pfx_vltimeexpire) > rai->rai_clockskew) {
1334 "<%s> valid lifetime for %s/%d"
1335 " (decr. in real time) inconsistent on %s:"
1336 " %d from %s, %" PRIu32 " from us",
1338 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1340 pinfo->nd_opt_pi_prefix_len,
1341 ifi->ifi_ifname, preferred_time,
1342 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1343 sizeof(ntopbuf)), pfx->pfx_vltimeexpire);
1346 } else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) {
1348 "<%s> valid lifetime for %s/%d"
1349 " inconsistent on %s:"
1350 " %d from %s, %d from us",
1352 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1354 pinfo->nd_opt_pi_prefix_len,
1355 ifi->ifi_ifname, valid_time,
1356 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1357 sizeof(ntopbuf)), pfx->pfx_validlifetime);
1361 return (inconsistent);
1365 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1368 int bytelen, bitlen;
1371 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1372 if (plen != pfx->pfx_prefixlen)
1377 bitmask = 0xff << (8 - bitlen);
1379 if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen))
1383 ((prefix->s6_addr[bytelen] & bitmask) ==
1384 (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) {
1392 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1394 prefix_match(struct in6_addr *p0, int plen0,
1395 struct in6_addr *p1, int plen1)
1397 int bytelen, bitlen;
1403 bytelen = plen1 / 8;
1405 bitmask = 0xff << (8 - bitlen);
1407 if (memcmp((void *)p0, (void *)p1, bytelen))
1411 ((p0->s6_addr[bytelen] & bitmask) ==
1412 (p1->s6_addr[bytelen] & bitmask))) {
1420 nd6_options(struct nd_opt_hdr *hdr, int limit,
1421 union nd_opt *ndopts, uint32_t optflags)
1425 for (; limit > 0; limit -= optlen) {
1426 if ((size_t)limit < sizeof(struct nd_opt_hdr)) {
1427 syslog(LOG_INFO, "<%s> short option header", __func__);
1431 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1432 if (hdr->nd_opt_len == 0) {
1434 "<%s> bad ND option length(0) (type = %d)",
1435 __func__, hdr->nd_opt_type);
1438 optlen = hdr->nd_opt_len << 3;
1439 if (optlen > limit) {
1440 syslog(LOG_INFO, "<%s> short option", __func__);
1444 if (hdr->nd_opt_type > ND_OPT_MTU &&
1445 hdr->nd_opt_type != ND_OPT_RDNSS &&
1446 hdr->nd_opt_type != ND_OPT_DNSSL) {
1447 syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1448 __func__, hdr->nd_opt_type);
1452 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1453 syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1454 __func__, hdr->nd_opt_type);
1459 * Option length check. Do it here for all fixed-length
1462 switch (hdr->nd_opt_type) {
1464 if (optlen == sizeof(struct nd_opt_mtu))
1469 (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0)
1474 (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0)
1477 case ND_OPT_PREFIX_INFORMATION:
1478 if (optlen == sizeof(struct nd_opt_prefix_info))
1481 syslog(LOG_INFO, "<%s> invalid option length",
1486 switch (hdr->nd_opt_type) {
1487 case ND_OPT_TARGET_LINKADDR:
1488 case ND_OPT_REDIRECTED_HEADER:
1491 break; /* we don't care about these options */
1492 case ND_OPT_SOURCE_LINKADDR:
1494 if (ndopts->opt_array[hdr->nd_opt_type]) {
1496 "<%s> duplicated ND option (type = %d)",
1497 __func__, hdr->nd_opt_type);
1499 ndopts->opt_array[hdr->nd_opt_type] = hdr;
1501 case ND_OPT_PREFIX_INFORMATION:
1503 struct nd_optlist *nol;
1505 if (ndopts->opt_pi == 0) {
1507 (struct nd_opt_prefix_info *)hdr;
1510 nol = malloc(sizeof(*nol));
1512 syslog(LOG_ERR, "<%s> can't allocate memory",
1517 TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next);
1521 default: /* impossible */
1529 free_ndopts(ndopts);
1535 free_ndopts(union nd_opt *ndopts)
1537 struct nd_optlist *nol;
1539 while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) {
1540 TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next);
1546 sock_open(struct sockinfo *s)
1548 struct icmp6_filter filt;
1550 /* XXX: should be max MTU attached to the node */
1551 static char answer[1500];
1553 syslog(LOG_DEBUG, "<%s> enter", __func__);
1556 syslog(LOG_ERR, "<%s> internal error", __func__);
1559 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1560 CMSG_SPACE(sizeof(int));
1561 rcvcmsgbuf = (char *)malloc(rcvcmsgbuflen);
1562 if (rcvcmsgbuf == NULL) {
1563 syslog(LOG_ERR, "<%s> not enough core", __func__);
1567 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1568 CMSG_SPACE(sizeof(int));
1569 sndcmsgbuf = (char *)malloc(sndcmsgbuflen);
1570 if (sndcmsgbuf == NULL) {
1571 syslog(LOG_ERR, "<%s> not enough core", __func__);
1575 if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1576 syslog(LOG_ERR, "<%s> socket: %s", __func__, strerror(errno));
1579 /* specify to tell receiving interface */
1581 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1583 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s", __func__,
1588 /* specify to tell value of hoplimit field of received IP6 hdr */
1589 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1591 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s", __func__,
1595 ICMP6_FILTER_SETBLOCKALL(&filt);
1596 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1597 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1598 if (mcastif != NULL)
1599 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1601 if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1602 sizeof(filt)) < 0) {
1603 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1604 __func__, strerror(errno));
1608 /* initialize msghdr for receiving packets */
1609 rcviov[0].iov_base = (caddr_t)answer;
1610 rcviov[0].iov_len = sizeof(answer);
1611 rcvmhdr.msg_name = (caddr_t)&rcvfrom;
1612 rcvmhdr.msg_namelen = sizeof(rcvfrom);
1613 rcvmhdr.msg_iov = rcviov;
1614 rcvmhdr.msg_iovlen = 1;
1615 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1616 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1618 /* initialize msghdr for sending packets */
1619 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1620 sndmhdr.msg_iov = sndiov;
1621 sndmhdr.msg_iovlen = 1;
1622 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1623 sndmhdr.msg_controllen = sndcmsgbuflen;
1628 /* open a routing socket to watch the routing table */
1630 rtsock_open(struct sockinfo *s)
1633 syslog(LOG_ERR, "<%s> internal error", __func__);
1636 if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1638 "<%s> socket: %s", __func__, strerror(errno));
1644 if_indextoifinfo(int idx)
1647 char *name, name0[IFNAMSIZ];
1649 /* Check if the interface has a valid name or not. */
1650 if (if_indextoname(idx, name0) == NULL)
1653 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1654 if (ifi->ifi_ifindex == idx)
1659 syslog(LOG_DEBUG, "<%s> ifi found (idx=%d)",
1662 syslog(LOG_DEBUG, "<%s> ifi not found (idx=%d)",
1665 return (NULL); /* search failed */
1669 ra_output(struct ifinfo *ifi)
1673 struct in6_pktinfo *pi;
1674 struct soliciter *sol;
1677 switch (ifi->ifi_state) {
1678 case IFI_STATE_CONFIGURED:
1679 rai = ifi->ifi_rainfo;
1681 case IFI_STATE_TRANSITIVE:
1682 rai = ifi->ifi_rainfo_trans;
1684 case IFI_STATE_UNCONFIGURED:
1685 syslog(LOG_DEBUG, "<%s> %s is unconfigured. "
1686 "Skip sending RAs.",
1687 __func__, ifi->ifi_ifname);
1693 syslog(LOG_DEBUG, "<%s> rainfo is NULL on %s."
1694 "Skip sending RAs.",
1695 __func__, ifi->ifi_ifname);
1698 if (!(ifi->ifi_flags & IFF_UP)) {
1699 syslog(LOG_DEBUG, "<%s> %s is not up. "
1700 "Skip sending RAs.",
1701 __func__, ifi->ifi_ifname);
1705 * Check lifetime, ACCEPT_RTADV flag, and ip6.forwarding.
1707 * (lifetime == 0) = output
1708 * (lifetime != 0 && (check_accept_rtadv()) = no output
1710 * Basically, hosts MUST NOT send Router Advertisement
1711 * messages at any time (RFC 4861, Section 6.2.3). However, it
1712 * would sometimes be useful to allow hosts to advertise some
1713 * parameters such as prefix information and link MTU. Thus,
1714 * we allow hosts to invoke rtadvd only when router lifetime
1715 * (on every advertising interface) is explicitly set
1716 * zero. (see also the above section)
1719 "<%s> check lifetime=%d, ACCEPT_RTADV=%d, ip6.forwarding=%d "
1722 check_accept_rtadv(ifi->ifi_ifindex),
1723 getinet6sysctl(IPV6CTL_FORWARDING),
1726 if (rai->rai_lifetime != 0) {
1727 if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) {
1729 "non-zero lifetime RA "
1730 "but net.inet6.ip6.forwarding=0. "
1734 if (check_accept_rtadv(ifi->ifi_ifindex)) {
1736 "non-zero lifetime RA "
1737 "on RA receiving interface %s."
1738 " Ignored.", ifi->ifi_ifname);
1743 make_packet(rai); /* XXX: inefficient */
1745 sndmhdr.msg_name = (caddr_t)&sin6_linklocal_allnodes;
1746 sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data;
1747 sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen;
1749 cm = CMSG_FIRSTHDR(&sndmhdr);
1750 /* specify the outgoing interface */
1751 cm->cmsg_level = IPPROTO_IPV6;
1752 cm->cmsg_type = IPV6_PKTINFO;
1753 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1754 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1755 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1756 pi->ipi6_ifindex = ifi->ifi_ifindex;
1758 /* specify the hop limit of the packet */
1762 cm = CMSG_NXTHDR(&sndmhdr, cm);
1763 cm->cmsg_level = IPPROTO_IPV6;
1764 cm->cmsg_type = IPV6_HOPLIMIT;
1765 cm->cmsg_len = CMSG_LEN(sizeof(int));
1766 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1770 "<%s> send RA on %s, # of RS waitings = %d",
1771 __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount);
1773 i = sendmsg(sock.si_fd, &sndmhdr, 0);
1775 if (i < 0 || (size_t)i != rai->rai_ra_datalen) {
1777 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1778 __func__, ifi->ifi_ifname,
1784 * unicast advertisements
1785 * XXX commented out. reason: though spec does not forbit it, unicast
1786 * advert does not really help
1788 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
1789 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
1793 /* update timestamp */
1794 clock_gettime(CLOCK_MONOTONIC_FAST, &ifi->ifi_ra_lastsent);
1796 /* update counter */
1797 ifi->ifi_rs_waitcount = 0;
1798 ifi->ifi_raoutput++;
1800 switch (ifi->ifi_state) {
1801 case IFI_STATE_CONFIGURED:
1802 if (ifi->ifi_burstcount > 0)
1803 ifi->ifi_burstcount--;
1805 case IFI_STATE_TRANSITIVE:
1806 ifi->ifi_burstcount--;
1807 if (ifi->ifi_burstcount == 0) {
1808 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1809 /* Initial burst finished. */
1810 if (ifi->ifi_rainfo_trans != NULL)
1811 ifi->ifi_rainfo_trans = NULL;
1814 /* Remove burst RA information */
1815 if (ifi->ifi_rainfo_trans != NULL) {
1816 rm_rainfo(ifi->ifi_rainfo_trans);
1817 ifi->ifi_rainfo_trans = NULL;
1820 if (ifi->ifi_rainfo != NULL) {
1822 * TRANSITIVE -> CONFIGURED
1824 * After initial burst or transition from
1825 * one configuration to another,
1826 * ifi_rainfo always points to the next RA
1829 ifi->ifi_state = IFI_STATE_CONFIGURED;
1831 "<%s> ifname=%s marked as "
1832 "CONFIGURED.", __func__,
1836 * TRANSITIVE -> UNCONFIGURED
1838 * If ifi_rainfo points to NULL, this
1839 * interface is shutting down.
1844 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
1846 "<%s> ifname=%s marked as "
1847 "UNCONFIGURED.", __func__,
1849 error = sock_mc_leave(&sock,
1859 /* process RA timer */
1860 struct rtadvd_timer *
1861 ra_timeout(void *arg)
1865 ifi = (struct ifinfo *)arg;
1866 syslog(LOG_DEBUG, "<%s> RA timer on %s is expired",
1867 __func__, ifi->ifi_ifname);
1871 return (ifi->ifi_ra_timer);
1874 /* update RA timer */
1876 ra_timer_update(void *arg, struct timespec *tm)
1882 ifi = (struct ifinfo *)arg;
1883 rai = ifi->ifi_rainfo;
1886 switch (ifi->ifi_state) {
1887 case IFI_STATE_UNCONFIGURED:
1890 case IFI_STATE_CONFIGURED:
1892 * Whenever a multicast advertisement is sent from
1893 * an interface, the timer is reset to a
1894 * uniformly-distributed random value between the
1895 * interface's configured MinRtrAdvInterval and
1896 * MaxRtrAdvInterval (RFC4861 6.2.4).
1898 interval = rai->rai_mininterval;
1899 interval += arc4random_uniform(rai->rai_maxinterval -
1900 rai->rai_mininterval);
1902 case IFI_STATE_TRANSITIVE:
1904 * For the first few advertisements (up to
1905 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen
1906 * interval is greater than
1907 * MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer SHOULD be
1908 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead. (RFC
1911 * In such cases, the router SHOULD transmit one or more
1912 * (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final
1913 * multicast Router Advertisements on the interface with a
1914 * Router Lifetime field of zero. (RFC 4861 6.2.5)
1916 interval = ifi->ifi_burstinterval;
1920 tm->tv_sec = interval;
1924 "<%s> RA timer on %s is set to %ld:%ld",
1925 __func__, ifi->ifi_ifname,
1926 (long int)tm->tv_sec, (long int)tm->tv_nsec / 1000);