2 /* $KAME: rtadvd.c,v 1.82 2003/08/05 12:34:23 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
39 #include <sys/queue.h>
41 #include <sys/sysctl.h>
44 #include <net/if_types.h>
45 #include <net/if_media.h>
46 #include <net/if_dl.h>
47 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/ip6.h>
50 #include <netinet6/ip6_var.h>
51 #include <netinet/icmp6.h>
53 #include <arpa/inet.h>
55 #include <net/if_var.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 int dflag = 0, sflag = 0;
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 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);
170 main(int argc, char *argv[])
172 struct pollfd set[PFD_MAX];
173 struct timeval *timeout;
175 int fflag = 0, logopt;
179 /* get command line options and arguments */
180 while ((ch = getopt(argc, argv, "c:C:dDfM:p:Rs")) != -1) {
186 ctrlsock.si_name = optarg;
201 fprintf(stderr, "rtadvd: "
202 "the -R option is currently ignored.\n");
210 pidfilename = optarg;
218 "usage: rtadvd [-dDfRs] [-c conffile] "
219 "[-C ctrlsockname] [-M ifname] "
220 "[-p pidfile] interfaces...\n");
224 logopt = LOG_NDELAY | LOG_PID;
226 logopt |= LOG_PERROR;
227 openlog("rtadvd", logopt, LOG_DAEMON);
231 (void)setlogmask(LOG_UPTO(LOG_DEBUG));
233 (void)setlogmask(LOG_UPTO(LOG_INFO));
235 (void)setlogmask(LOG_UPTO(LOG_NOTICE));
237 (void)setlogmask(LOG_UPTO(LOG_ERR));
239 /* timer initialization */
242 #ifndef HAVE_ARC4RANDOM
243 /* random value initialization */
247 srandom((unsigned long)time(NULL));
250 pfh = pidfile_open(pidfilename, 0600, &otherpid);
253 errx(1, "%s already running, pid: %d",
254 getprogname(), otherpid);
256 "failed to open the pid file %s, run anyway.",
264 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
265 for (i = 0; i < argc; i++)
266 update_persist_ifinfo(&ifilist, argv[i]);
268 csock_open(&ctrlsock, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
269 if (ctrlsock.si_fd == -1) {
270 syslog(LOG_ERR, "cannot open control socket: %s",
275 /* record the current PID */
279 set[PFD_RAWSOCK].fd = sock.si_fd;
280 set[PFD_RAWSOCK].events = POLLIN;
282 rtsock_open(&rtsock);
283 set[PFD_RTSOCK].fd = rtsock.si_fd;
284 set[PFD_RTSOCK].events = POLLIN;
286 set[PFD_RTSOCK].fd = -1;
287 set[PFD_CSOCK].fd = ctrlsock.si_fd;
288 set[PFD_CSOCK].events = POLLIN;
289 signal(SIGTERM, set_do_shutdown);
290 signal(SIGINT, set_do_shutdown);
291 signal(SIGHUP, set_do_reload);
293 error = csock_listen(&ctrlsock);
295 syslog(LOG_ERR, "cannot listen control socket: %s",
300 /* load configuration file */
304 if (is_do_shutdown())
307 if (is_do_reload()) {
308 loadconfig_ifname(reload_ifname());
309 if (reload_ifname() == NULL)
311 "configuration file reloaded.");
314 "configuration file for %s reloaded.",
319 /* timeout handler update for active interfaces */
320 rtadvd_update_timeout_handler();
322 /* timer expiration check and reset the timer */
323 timeout = rtadvd_check_timer();
325 if (timeout != NULL) {
327 "<%s> set timer to %ld:%ld. waiting for "
328 "inputs or timeout", __func__,
329 (long int)timeout->tv_sec,
330 (long int)timeout->tv_usec);
333 "<%s> there's no timer. waiting for inputs",
336 if ((i = poll(set, sizeof(set)/sizeof(set[0]),
337 timeout ? (timeout->tv_sec * 1000 +
338 timeout->tv_usec / 1000) : INFTIM)) < 0) {
340 /* EINTR would occur if a signal was delivered */
342 syslog(LOG_ERR, "poll() failed: %s",
346 if (i == 0) /* timeout */
348 if (rtsock.si_fd != -1 && set[PFD_RTSOCK].revents & POLLIN)
349 rtmsg_input(&rtsock);
351 if (set[PFD_RAWSOCK].revents & POLLIN)
354 if (set[PFD_CSOCK].revents & POLLIN) {
357 fd = csock_accept(&ctrlsock);
360 "cannot accept() control socket: %s",
363 cm_handler_server(fd);
368 exit(0); /* NOTREACHED */
372 rtadvd_shutdown(void)
381 "waiting expiration of the all RA timers.");
383 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
385 * Ignore !IFF_UP interfaces in waiting for shutdown.
387 if (!(ifi->ifi_flags & IFF_UP) &&
388 ifi->ifi_ra_timer != NULL) {
389 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
390 rtadvd_remove_timer(ifi->ifi_ra_timer);
391 ifi->ifi_ra_timer = NULL;
392 syslog(LOG_DEBUG, "<%s> %s(idx=%d) is down. "
393 "Timer removed and marked as UNCONFIGURED.",
394 __func__, ifi->ifi_ifname,
398 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
399 if (ifi->ifi_ra_timer != NULL)
403 syslog(LOG_NOTICE, "gracefully terminated.");
411 syslog(LOG_DEBUG, "<%s> cease to be an advertising router",
416 TAILQ_FOREACH(rai, &railist, rai_next) {
417 rai->rai_lifetime = 0;
418 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next)
420 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next)
423 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
424 if (!ifi->ifi_persist)
426 if (ifi->ifi_state == IFI_STATE_UNCONFIGURED)
428 if (ifi->ifi_ra_timer == NULL)
430 if (ifi->ifi_ra_lastsent.tv_sec == 0 &&
431 ifi->ifi_ra_lastsent.tv_usec == 0 &&
432 ifi->ifi_ra_timer != NULL) {
434 * When RA configured but never sent,
435 * ignore the IF immediately.
437 rtadvd_remove_timer(ifi->ifi_ra_timer);
438 ifi->ifi_ra_timer = NULL;
439 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
443 ifi->ifi_state = IFI_STATE_TRANSITIVE;
445 /* Mark as the shut-down state. */
446 ifi->ifi_rainfo_trans = ifi->ifi_rainfo;
447 ifi->ifi_rainfo = NULL;
449 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
450 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
452 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
453 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
456 syslog(LOG_NOTICE, "final RA transmission started.");
459 csock_close(&ctrlsock);
463 rtmsg_input(struct sockinfo *s)
465 int n, type, ifindex = 0, plen;
467 char msg[2048], *next, *lim;
468 char ifname[IFNAMSIZ];
469 struct if_announcemsghdr *ifan;
470 struct rt_msghdr *rtm;
473 struct in6_addr *addr;
475 char addrbuf[INET6_ADDRSTRLEN];
476 int prefixchange = 0;
479 syslog(LOG_ERR, "<%s> internal error", __func__);
482 n = read(s->si_fd, msg, sizeof(msg));
483 rtm = (struct rt_msghdr *)msg;
484 syslog(LOG_DEBUG, "<%s> received a routing message "
485 "(type = %d, len = %d)", __func__, rtm->rtm_type, n);
487 if (n > rtm->rtm_msglen) {
489 * This usually won't happen for messages received on
493 "<%s> received data length is larger than "
494 "1st routing message len. multiple messages? "
495 "read %d bytes, but 1st msg len = %d",
496 __func__, n, rtm->rtm_msglen);
504 for (next = msg; next < lim; next += len) {
507 next = get_next_msg(next, lim, 0, &len,
508 RTADV_TYPE2BITMASK(RTM_ADD) |
509 RTADV_TYPE2BITMASK(RTM_DELETE) |
510 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
511 RTADV_TYPE2BITMASK(RTM_DELADDR) |
512 RTADV_TYPE2BITMASK(RTM_IFINFO) |
513 RTADV_TYPE2BITMASK(RTM_IFANNOUNCE));
516 type = ((struct rt_msghdr *)next)->rtm_type;
520 ifindex = get_rtm_ifindex(next);
524 ifindex = (int)((struct ifa_msghdr *)next)->ifam_index;
527 ifindex = (int)((struct if_msghdr *)next)->ifm_index;
530 ifan = (struct if_announcemsghdr *)next;
531 switch (ifan->ifan_what) {
537 "<%s:%d> unknown ifan msg (ifan_what=%d)",
538 __func__, __LINE__, ifan->ifan_what);
542 syslog(LOG_DEBUG, "<%s>: if_announcemsg (idx=%d:%d)",
543 __func__, ifan->ifan_index, ifan->ifan_what);
544 switch (ifan->ifan_what) {
547 "interface added (idx=%d)",
549 update_ifinfo(&ifilist, ifan->ifan_index);
550 loadconfig_index(ifan->ifan_index);
554 "interface removed (idx=%d)",
556 rm_ifinfo_index(ifan->ifan_index);
558 /* Clear ifi_ifindex */
559 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
561 == ifan->ifan_index) {
562 ifi->ifi_ifindex = 0;
566 update_ifinfo(&ifilist, ifan->ifan_index);
571 /* should not reach here */
573 "<%s:%d> unknown rtmsg %d on %s",
574 __func__, __LINE__, type,
575 if_indextoname(ifindex, ifname));
578 ifi = if_indextoifinfo(ifindex);
581 "<%s> ifinfo not found for idx=%d. Why?",
585 rai = ifi->ifi_rainfo;
588 "<%s> route changed on "
589 "non advertising interface(%s)",
590 __func__, ifi->ifi_ifname);
594 oldifflags = ifi->ifi_flags;
595 /* init ifflags because it may have changed */
596 update_ifinfo(&ifilist, ifindex);
601 break; /* we aren't interested in prefixes */
603 addr = get_addr(msg);
604 plen = get_prefixlen(msg);
605 /* sanity check for plen */
606 /* as RFC2373, prefixlen is at least 4 */
607 if (plen < 4 || plen > 127) {
608 syslog(LOG_INFO, "<%s> new interface route's"
609 "plen %d is invalid for a prefix",
613 pfx = find_prefix(rai, addr, plen);
615 if (pfx->pfx_timer) {
617 * If the prefix has been invalidated,
618 * make it available again.
624 "<%s> new prefix(%s/%d) "
626 "but it was already in list",
628 inet_ntop(AF_INET6, addr,
631 plen, ifi->ifi_ifname);
634 make_prefix(rai, ifindex, addr, plen);
641 addr = get_addr(msg);
642 plen = get_prefixlen(msg);
643 /* sanity check for plen */
644 /* as RFC2373, prefixlen is at least 4 */
645 if (plen < 4 || plen > 127) {
647 "<%s> deleted interface route's "
648 "plen %d is invalid for a prefix",
652 pfx = find_prefix(rai, addr, plen);
655 "<%s> prefix(%s/%d) was deleted on %s, "
656 "but it was not in list",
657 __func__, inet_ntop(AF_INET6, addr,
658 (char *)addrbuf, sizeof(addrbuf)),
659 plen, ifi->ifi_ifname);
662 invalidate_prefix(pfx);
670 /* should not reach here */
672 "<%s:%d> unknown rtmsg %d on %s",
673 __func__, __LINE__, type,
674 if_indextoname(ifindex, ifname));
678 /* check if an interface flag is changed */
679 if ((oldifflags & IFF_UP) && /* UP to DOWN */
680 !(ifi->ifi_flags & IFF_UP)) {
682 "<interface %s becomes down. stop timer.",
684 rtadvd_remove_timer(ifi->ifi_ra_timer);
685 ifi->ifi_ra_timer = NULL;
686 } else if (!(oldifflags & IFF_UP) && /* DOWN to UP */
687 (ifi->ifi_flags & IFF_UP)) {
689 "interface %s becomes up. restart timer.",
692 ifi->ifi_state = IFI_STATE_TRANSITIVE;
693 ifi->ifi_burstcount =
694 MAX_INITIAL_RTR_ADVERTISEMENTS;
695 ifi->ifi_burstinterval =
696 MAX_INITIAL_RTR_ADVERT_INTERVAL;
698 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
699 ra_timer_update, ifi, ifi);
700 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
701 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
703 } else if (prefixchange &&
704 (ifi->ifi_flags & IFF_UP)) {
706 * An advertised prefix has been added or invalidated.
707 * Will notice the change in a short delay.
709 set_short_delay(ifi);
717 rtadvd_input(struct sockinfo *s)
724 struct icmp6_hdr *icp;
727 struct in6_pktinfo *pi = NULL;
728 char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
729 struct in6_addr dst = in6addr_any;
732 syslog(LOG_DEBUG, "<%s> enter", __func__);
735 syslog(LOG_ERR, "<%s> internal error", __func__);
739 * Get message. We reset msg_controllen since the field could
740 * be modified if we had received a message before setting
743 rcvmhdr.msg_controllen = rcvcmsgbuflen;
744 if ((i = recvmsg(s->si_fd, &rcvmhdr, 0)) < 0)
747 /* extract optional information via Advanced API */
748 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
750 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
751 if (cm->cmsg_level == IPPROTO_IPV6 &&
752 cm->cmsg_type == IPV6_PKTINFO &&
753 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
754 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
755 ifindex = pi->ipi6_ifindex;
758 if (cm->cmsg_level == IPPROTO_IPV6 &&
759 cm->cmsg_type == IPV6_HOPLIMIT &&
760 cm->cmsg_len == CMSG_LEN(sizeof(int)))
761 hlimp = (int *)CMSG_DATA(cm);
764 syslog(LOG_ERR, "failed to get receiving interface");
768 syslog(LOG_ERR, "failed to get receiving hop limit");
773 * If we happen to receive data on an interface which is now gone
774 * or down, just discard the data.
776 ifi = if_indextoifinfo(pi->ipi6_ifindex);
777 if (ifi == NULL || !(ifi->ifi_flags & IFF_UP)) {
779 "<%s> received data on a disabled interface (%s)",
781 (ifi == NULL) ? "[gone]" : ifi->ifi_ifname);
786 if ((size_t)i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
788 "packet size(%d) is too short", i);
792 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
793 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
795 if ((size_t)i < sizeof(struct icmp6_hdr)) {
796 syslog(LOG_ERR, "packet size(%zd) is too short", i);
800 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
803 switch (icp->icmp6_type) {
804 case ND_ROUTER_SOLICIT:
806 * Message verification - RFC 4861 6.1.1
807 * XXX: these checks must be done in the kernel as well,
808 * but we can't completely rely on them.
812 "RS with invalid hop limit(%d) "
813 "received from %s on %s",
815 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
817 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
820 if (icp->icmp6_code) {
822 "RS with invalid ICMP6 code(%d) "
823 "received from %s on %s",
825 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
827 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
830 if ((size_t)i < sizeof(struct nd_router_solicit)) {
832 "RS from %s on %s does not have enough "
833 "length (len = %zd)",
834 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
836 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
839 rs_input(i, (struct nd_router_solicit *)icp, pi, &rcvfrom);
841 case ND_ROUTER_ADVERT:
843 * Message verification - RFC 4861 6.1.2
844 * XXX: there's the same dilemma as above...
846 if (!IN6_IS_ADDR_LINKLOCAL(&rcvfrom.sin6_addr)) {
848 "RA with non-linklocal source address "
849 "received from %s on %s",
850 inet_ntop(AF_INET6, &rcvfrom.sin6_addr,
851 ntopbuf, sizeof(ntopbuf)),
852 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
857 "RA with invalid hop limit(%d) "
858 "received from %s on %s",
860 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
862 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
865 if (icp->icmp6_code) {
867 "RA with invalid ICMP6 code(%d) "
868 "received from %s on %s",
870 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
872 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
875 if ((size_t)i < sizeof(struct nd_router_advert)) {
877 "RA from %s on %s does not have enough "
878 "length (len = %zd)",
879 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
881 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
884 ra_input(i, (struct nd_router_advert *)icp, pi, &rcvfrom);
886 case ICMP6_ROUTER_RENUMBERING:
887 if (mcastif == NULL) {
888 syslog(LOG_ERR, "received a router renumbering "
889 "message, but not allowed to be accepted");
892 rr_input(i, (struct icmp6_router_renum *)icp, pi, &rcvfrom,
897 * Note that this case is POSSIBLE, especially just
898 * after invocation of the daemon. This is because we
899 * could receive message after opening the socket and
900 * before setting ICMP6 type filter(see sock_open()).
902 syslog(LOG_ERR, "invalid icmp type(%d)", icp->icmp6_type);
910 rs_input(int len, struct nd_router_solicit *rs,
911 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
913 char ntopbuf[INET6_ADDRSTRLEN];
914 char ifnamebuf[IFNAMSIZ];
918 struct soliciter *sol;
921 "<%s> RS received from %s on %s",
923 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
924 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
926 /* ND option check */
927 memset(&ndopts, 0, sizeof(ndopts));
928 TAILQ_INIT(&ndopts.opt_list);
929 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
930 len - sizeof(struct nd_router_solicit),
931 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
933 "<%s> ND option check failed for an RS from %s on %s",
935 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
937 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
942 * If the IP source address is the unspecified address, there
943 * must be no source link-layer address option in the message.
946 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
947 ndopts.opt_src_lladdr) {
949 "<%s> RS from unspecified src on %s has a link-layer"
951 __func__, if_indextoname(pi->ipi6_ifindex, ifnamebuf));
955 ifi = if_indextoifinfo(pi->ipi6_ifindex);
958 "<%s> if (idx=%d) not found. Why?",
959 __func__, pi->ipi6_ifindex);
962 rai = ifi->ifi_rainfo;
965 "<%s> RS received on non advertising interface(%s)",
967 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
971 rai->rai_ifinfo->ifi_rsinput++;
974 * Decide whether to send RA according to the rate-limit
978 /* record sockaddr waiting for RA, if possible */
979 sol = (struct soliciter *)malloc(sizeof(*sol));
981 sol->sol_addr = *from;
982 /* XXX RFC 2553 need clarification on flowinfo */
983 sol->sol_addr.sin6_flowinfo = 0;
984 TAILQ_INSERT_TAIL(&rai->rai_soliciter, sol, sol_next);
988 * If there is already a waiting RS packet, don't
991 if (ifi->ifi_rs_waitcount++)
994 set_short_delay(ifi);
997 free_ndopts(&ndopts);
1002 set_short_delay(struct ifinfo *ifi)
1004 long delay; /* must not be greater than 1000000 */
1005 struct timeval interval, now, min_delay, tm_tmp, *rest;
1008 * Compute a random delay. If the computed value
1009 * corresponds to a time later than the time the next
1010 * multicast RA is scheduled to be sent, ignore the random
1011 * delay and send the advertisement at the
1012 * already-scheduled time. RFC 4861 6.2.6
1014 #ifdef HAVE_ARC4RANDOM
1015 delay = arc4random_uniform(MAX_RA_DELAY_TIME);
1017 delay = random() % MAX_RA_DELAY_TIME;
1019 interval.tv_sec = 0;
1020 interval.tv_usec = delay;
1021 rest = rtadvd_timer_rest(ifi->ifi_ra_timer);
1022 if (TIMEVAL_LT(rest, &interval)) {
1023 syslog(LOG_DEBUG, "<%s> random delay is larger than "
1024 "the rest of the current timer", __func__);
1029 * If we sent a multicast Router Advertisement within
1030 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
1031 * the advertisement to be sent at a time corresponding to
1032 * MIN_DELAY_BETWEEN_RAS plus the random value after the
1033 * previous advertisement was sent.
1035 gettimeofday(&now, NULL);
1036 TIMEVAL_SUB(&now, &ifi->ifi_ra_lastsent, &tm_tmp);
1037 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
1038 min_delay.tv_usec = 0;
1039 if (TIMEVAL_LT(&tm_tmp, &min_delay)) {
1040 TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
1041 TIMEVAL_ADD(&min_delay, &interval, &interval);
1043 rtadvd_set_timer(&interval, ifi->ifi_ra_timer);
1047 check_accept_rtadv(int idx)
1051 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1052 if (ifi->ifi_ifindex == idx)
1057 "<%s> if (idx=%d) not found. Why?",
1061 #if (__FreeBSD_version < 900000)
1063 * RA_RECV: !ip6.forwarding && ip6.accept_rtadv
1064 * RA_SEND: ip6.forwarding
1066 return ((getinet6sysctl(IPV6CTL_FORWARDING) == 0) &&
1067 (getinet6sysctl(IPV6CTL_ACCEPT_RTADV) == 1));
1070 * RA_RECV: ND6_IFF_ACCEPT_RTADV
1071 * RA_SEND: ip6.forwarding
1073 if (update_ifinfo_nd_flags(ifi) != 0) {
1074 syslog(LOG_ERR, "cannot get nd6 flags (idx=%d)", idx);
1078 return (ifi->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV);
1083 ra_input(int len, struct nd_router_advert *nra,
1084 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
1088 char ntopbuf[INET6_ADDRSTRLEN];
1089 char ifnamebuf[IFNAMSIZ];
1090 union nd_opt ndopts;
1091 const char *on_off[] = {"OFF", "ON"};
1092 uint32_t reachabletime, retranstimer, mtu;
1093 int inconsistent = 0;
1096 syslog(LOG_DEBUG, "<%s> RA received from %s on %s", __func__,
1097 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
1098 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
1100 /* ND option check */
1101 memset(&ndopts, 0, sizeof(ndopts));
1102 TAILQ_INIT(&ndopts.opt_list);
1103 error = nd6_options((struct nd_opt_hdr *)(nra + 1),
1104 len - sizeof(struct nd_router_advert), &ndopts,
1105 NDOPT_FLAG_SRCLINKADDR | NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU |
1106 NDOPT_FLAG_RDNSS | NDOPT_FLAG_DNSSL);
1109 "<%s> ND option check failed for an RA from %s on %s",
1111 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1112 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1118 * RA consistency check according to RFC 4861 6.2.7
1120 ifi = if_indextoifinfo(pi->ipi6_ifindex);
1121 if (ifi->ifi_rainfo == NULL) {
1123 "<%s> received RA from %s on non-advertising"
1126 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1127 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1131 rai = ifi->ifi_rainfo;
1133 syslog(LOG_DEBUG, "<%s> ifi->ifi_rainput = %" PRIu64, __func__,
1136 /* Cur Hop Limit value */
1137 if (nra->nd_ra_curhoplimit && rai->rai_hoplimit &&
1138 nra->nd_ra_curhoplimit != rai->rai_hoplimit) {
1140 "CurHopLimit inconsistent on %s:"
1141 " %d from %s, %d from us",
1142 ifi->ifi_ifname, nra->nd_ra_curhoplimit,
1143 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1144 sizeof(ntopbuf)), rai->rai_hoplimit);
1148 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
1149 rai->rai_managedflg) {
1151 "M flag inconsistent on %s:"
1152 " %s from %s, %s from us",
1153 ifi->ifi_ifname, on_off[!rai->rai_managedflg],
1154 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1155 sizeof(ntopbuf)), on_off[rai->rai_managedflg]);
1159 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
1160 rai->rai_otherflg) {
1162 "O flag inconsistent on %s:"
1163 " %s from %s, %s from us",
1164 ifi->ifi_ifname, on_off[!rai->rai_otherflg],
1165 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1166 sizeof(ntopbuf)), on_off[rai->rai_otherflg]);
1169 /* Reachable Time */
1170 reachabletime = ntohl(nra->nd_ra_reachable);
1171 if (reachabletime && rai->rai_reachabletime &&
1172 reachabletime != rai->rai_reachabletime) {
1174 "ReachableTime inconsistent on %s:"
1175 " %d from %s, %d from us",
1176 ifi->ifi_ifname, reachabletime,
1177 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1178 sizeof(ntopbuf)), rai->rai_reachabletime);
1182 retranstimer = ntohl(nra->nd_ra_retransmit);
1183 if (retranstimer && rai->rai_retranstimer &&
1184 retranstimer != rai->rai_retranstimer) {
1186 "RetranceTimer inconsistent on %s:"
1187 " %d from %s, %d from us",
1188 ifi->ifi_ifname, retranstimer,
1189 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1190 sizeof(ntopbuf)), rai->rai_retranstimer);
1193 /* Values in the MTU options */
1194 if (ndopts.opt_mtu) {
1195 mtu = ntohl(ndopts.opt_mtu->nd_opt_mtu_mtu);
1196 if (mtu && rai->rai_linkmtu && mtu != rai->rai_linkmtu) {
1198 "MTU option value inconsistent on %s:"
1199 " %d from %s, %d from us",
1200 ifi->ifi_ifname, mtu,
1201 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1202 sizeof(ntopbuf)), rai->rai_linkmtu);
1206 /* Preferred and Valid Lifetimes for prefixes */
1208 struct nd_optlist *nol;
1211 if (prefix_check(ndopts.opt_pi, rai, from))
1214 TAILQ_FOREACH(nol, &ndopts.opt_list, nol_next)
1215 if (prefix_check((struct nd_opt_prefix_info *)nol->nol_opt,
1221 ifi->ifi_rainconsistent++;
1224 free_ndopts(&ndopts);
1229 udiff(uint32_t u, uint32_t v)
1231 return (u >= v ? u - v : v - u);
1234 /* return a non-zero value if the received prefix is inconsitent with ours */
1236 prefix_check(struct nd_opt_prefix_info *pinfo,
1237 struct rainfo *rai, struct sockaddr_in6 *from)
1240 uint32_t preferred_time, valid_time;
1242 int inconsistent = 0;
1243 char ntopbuf[INET6_ADDRSTRLEN];
1244 char prefixbuf[INET6_ADDRSTRLEN];
1247 #if 0 /* impossible */
1248 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1251 ifi = rai->rai_ifinfo;
1253 * log if the adveritsed prefix has link-local scope(sanity check?)
1255 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix))
1257 "<%s> link-local prefix %s/%d is advertised "
1260 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1262 pinfo->nd_opt_pi_prefix_len,
1263 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1264 sizeof(ntopbuf)), ifi->ifi_ifname);
1266 if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1267 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1269 "<%s> prefix %s/%d from %s on %s is not in our list",
1271 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1273 pinfo->nd_opt_pi_prefix_len,
1274 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1275 sizeof(ntopbuf)), ifi->ifi_ifname);
1279 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1280 if (pfx->pfx_pltimeexpire) {
1282 * The lifetime is decremented in real time, so we should
1283 * compare the expiration time.
1284 * (RFC 2461 Section 6.2.7.)
1285 * XXX: can we really expect that all routers on the link
1286 * have synchronized clocks?
1288 gettimeofday(&now, NULL);
1289 preferred_time += now.tv_sec;
1291 if (!pfx->pfx_timer && rai->rai_clockskew &&
1292 udiff(preferred_time, pfx->pfx_pltimeexpire) > rai->rai_clockskew) {
1294 "<%s> preferred lifetime for %s/%d"
1295 " (decr. in real time) inconsistent on %s:"
1296 " %" PRIu32 " from %s, %" PRIu32 " from us",
1298 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1300 pinfo->nd_opt_pi_prefix_len,
1301 ifi->ifi_ifname, preferred_time,
1302 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1303 sizeof(ntopbuf)), pfx->pfx_pltimeexpire);
1306 } else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime)
1308 "<%s> preferred lifetime for %s/%d"
1309 " inconsistent on %s:"
1310 " %d from %s, %d from us",
1312 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1314 pinfo->nd_opt_pi_prefix_len,
1315 ifi->ifi_ifname, preferred_time,
1316 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1317 sizeof(ntopbuf)), pfx->pfx_preflifetime);
1319 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1320 if (pfx->pfx_vltimeexpire) {
1321 gettimeofday(&now, NULL);
1322 valid_time += now.tv_sec;
1324 if (!pfx->pfx_timer && rai->rai_clockskew &&
1325 udiff(valid_time, pfx->pfx_vltimeexpire) > rai->rai_clockskew) {
1327 "<%s> valid lifetime for %s/%d"
1328 " (decr. in real time) inconsistent on %s:"
1329 " %d from %s, %" PRIu32 " from us",
1331 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1333 pinfo->nd_opt_pi_prefix_len,
1334 ifi->ifi_ifname, preferred_time,
1335 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1336 sizeof(ntopbuf)), pfx->pfx_vltimeexpire);
1339 } else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) {
1341 "<%s> valid lifetime for %s/%d"
1342 " inconsistent on %s:"
1343 " %d from %s, %d from us",
1345 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1347 pinfo->nd_opt_pi_prefix_len,
1348 ifi->ifi_ifname, valid_time,
1349 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1350 sizeof(ntopbuf)), pfx->pfx_validlifetime);
1354 return (inconsistent);
1358 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1361 int bytelen, bitlen;
1364 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1365 if (plen != pfx->pfx_prefixlen)
1370 bitmask = 0xff << (8 - bitlen);
1372 if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen))
1376 ((prefix->s6_addr[bytelen] & bitmask) ==
1377 (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) {
1385 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1387 prefix_match(struct in6_addr *p0, int plen0,
1388 struct in6_addr *p1, int plen1)
1390 int bytelen, bitlen;
1396 bytelen = plen1 / 8;
1398 bitmask = 0xff << (8 - bitlen);
1400 if (memcmp((void *)p0, (void *)p1, bytelen))
1404 ((p0->s6_addr[bytelen] & bitmask) ==
1405 (p1->s6_addr[bytelen] & bitmask))) {
1413 nd6_options(struct nd_opt_hdr *hdr, int limit,
1414 union nd_opt *ndopts, uint32_t optflags)
1418 for (; limit > 0; limit -= optlen) {
1419 if ((size_t)limit < sizeof(struct nd_opt_hdr)) {
1420 syslog(LOG_INFO, "<%s> short option header", __func__);
1424 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1425 if (hdr->nd_opt_len == 0) {
1427 "<%s> bad ND option length(0) (type = %d)",
1428 __func__, hdr->nd_opt_type);
1431 optlen = hdr->nd_opt_len << 3;
1432 if (optlen > limit) {
1433 syslog(LOG_INFO, "<%s> short option", __func__);
1437 if (hdr->nd_opt_type > ND_OPT_MTU &&
1438 hdr->nd_opt_type != ND_OPT_RDNSS &&
1439 hdr->nd_opt_type != ND_OPT_DNSSL) {
1440 syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1441 __func__, hdr->nd_opt_type);
1445 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1446 syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1447 __func__, hdr->nd_opt_type);
1452 * Option length check. Do it here for all fixed-length
1455 switch (hdr->nd_opt_type) {
1457 if (optlen == sizeof(struct nd_opt_mtu))
1462 (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0)
1467 (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0)
1470 case ND_OPT_PREFIX_INFORMATION:
1471 if (optlen == sizeof(struct nd_opt_prefix_info))
1474 syslog(LOG_INFO, "<%s> invalid option length",
1479 switch (hdr->nd_opt_type) {
1480 case ND_OPT_TARGET_LINKADDR:
1481 case ND_OPT_REDIRECTED_HEADER:
1484 break; /* we don't care about these options */
1485 case ND_OPT_SOURCE_LINKADDR:
1487 if (ndopts->opt_array[hdr->nd_opt_type]) {
1489 "<%s> duplicated ND option (type = %d)",
1490 __func__, hdr->nd_opt_type);
1492 ndopts->opt_array[hdr->nd_opt_type] = hdr;
1494 case ND_OPT_PREFIX_INFORMATION:
1496 struct nd_optlist *nol;
1498 if (ndopts->opt_pi == 0) {
1500 (struct nd_opt_prefix_info *)hdr;
1503 nol = malloc(sizeof(*nol));
1505 syslog(LOG_ERR, "<%s> can't allocate memory",
1510 TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next);
1514 default: /* impossible */
1522 free_ndopts(ndopts);
1528 free_ndopts(union nd_opt *ndopts)
1530 struct nd_optlist *nol;
1532 while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) {
1533 TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next);
1539 sock_open(struct sockinfo *s)
1541 struct icmp6_filter filt;
1543 /* XXX: should be max MTU attached to the node */
1544 static char answer[1500];
1546 syslog(LOG_DEBUG, "<%s> enter", __func__);
1549 syslog(LOG_ERR, "<%s> internal error", __func__);
1552 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1553 CMSG_SPACE(sizeof(int));
1554 rcvcmsgbuf = (char *)malloc(rcvcmsgbuflen);
1555 if (rcvcmsgbuf == NULL) {
1556 syslog(LOG_ERR, "<%s> not enough core", __func__);
1560 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1561 CMSG_SPACE(sizeof(int));
1562 sndcmsgbuf = (char *)malloc(sndcmsgbuflen);
1563 if (sndcmsgbuf == NULL) {
1564 syslog(LOG_ERR, "<%s> not enough core", __func__);
1568 if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1569 syslog(LOG_ERR, "<%s> socket: %s", __func__, strerror(errno));
1572 /* specify to tell receiving interface */
1574 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1576 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s", __func__,
1581 /* specify to tell value of hoplimit field of received IP6 hdr */
1582 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1584 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s", __func__,
1588 ICMP6_FILTER_SETBLOCKALL(&filt);
1589 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1590 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1591 if (mcastif != NULL)
1592 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1594 if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1595 sizeof(filt)) < 0) {
1596 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1597 __func__, strerror(errno));
1601 /* initialize msghdr for receiving packets */
1602 rcviov[0].iov_base = (caddr_t)answer;
1603 rcviov[0].iov_len = sizeof(answer);
1604 rcvmhdr.msg_name = (caddr_t)&rcvfrom;
1605 rcvmhdr.msg_namelen = sizeof(rcvfrom);
1606 rcvmhdr.msg_iov = rcviov;
1607 rcvmhdr.msg_iovlen = 1;
1608 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1609 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1611 /* initialize msghdr for sending packets */
1612 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1613 sndmhdr.msg_iov = sndiov;
1614 sndmhdr.msg_iovlen = 1;
1615 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1616 sndmhdr.msg_controllen = sndcmsgbuflen;
1621 /* open a routing socket to watch the routing table */
1623 rtsock_open(struct sockinfo *s)
1626 syslog(LOG_ERR, "<%s> internal error", __func__);
1629 if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1631 "<%s> socket: %s", __func__, strerror(errno));
1637 if_indextoifinfo(int idx)
1641 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1642 if (ifi->ifi_ifindex == idx)
1647 syslog(LOG_DEBUG, "<%s> ifi found (idx=%d)",
1650 syslog(LOG_DEBUG, "<%s> ifi not found (idx=%d)",
1653 return (NULL); /* search failed */
1657 ra_output(struct ifinfo *ifi)
1661 struct in6_pktinfo *pi;
1662 struct soliciter *sol;
1665 switch (ifi->ifi_state) {
1666 case IFI_STATE_CONFIGURED:
1667 rai = ifi->ifi_rainfo;
1669 case IFI_STATE_TRANSITIVE:
1670 rai = ifi->ifi_rainfo_trans;
1672 case IFI_STATE_UNCONFIGURED:
1673 syslog(LOG_DEBUG, "<%s> %s is unconfigured. "
1674 "Skip sending RAs.",
1675 __func__, ifi->ifi_ifname);
1681 syslog(LOG_DEBUG, "<%s> rainfo is NULL on %s."
1682 "Skip sending RAs.",
1683 __func__, ifi->ifi_ifname);
1686 if (!(ifi->ifi_flags & IFF_UP)) {
1687 syslog(LOG_DEBUG, "<%s> %s is not up. "
1688 "Skip sending RAs.",
1689 __func__, ifi->ifi_ifname);
1693 * Check lifetime, ACCEPT_RTADV flag, and ip6.forwarding.
1695 * (lifetime == 0) = output
1696 * (lifetime != 0 && (check_accept_rtadv()) = no output
1698 * Basically, hosts MUST NOT send Router Advertisement
1699 * messages at any time (RFC 4861, Section 6.2.3). However, it
1700 * would sometimes be useful to allow hosts to advertise some
1701 * parameters such as prefix information and link MTU. Thus,
1702 * we allow hosts to invoke rtadvd only when router lifetime
1703 * (on every advertising interface) is explicitly set
1704 * zero. (see also the above section)
1707 "<%s> check lifetime=%d, ACCEPT_RTADV=%d, ip6.forwarding=%d "
1710 check_accept_rtadv(ifi->ifi_ifindex),
1711 getinet6sysctl(IPV6CTL_FORWARDING),
1714 if (rai->rai_lifetime != 0) {
1715 if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) {
1717 "non-zero lifetime RA "
1718 "but net.inet6.ip6.forwarding=0. "
1722 if (check_accept_rtadv(ifi->ifi_ifindex)) {
1724 "non-zero lifetime RA "
1725 "on RA receiving interface %s."
1726 " Ignored.", ifi->ifi_ifname);
1731 make_packet(rai); /* XXX: inefficient */
1733 sndmhdr.msg_name = (caddr_t)&sin6_linklocal_allnodes;
1734 sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data;
1735 sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen;
1737 cm = CMSG_FIRSTHDR(&sndmhdr);
1738 /* specify the outgoing interface */
1739 cm->cmsg_level = IPPROTO_IPV6;
1740 cm->cmsg_type = IPV6_PKTINFO;
1741 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1742 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1743 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1744 pi->ipi6_ifindex = ifi->ifi_ifindex;
1746 /* specify the hop limit of the packet */
1750 cm = CMSG_NXTHDR(&sndmhdr, cm);
1751 cm->cmsg_level = IPPROTO_IPV6;
1752 cm->cmsg_type = IPV6_HOPLIMIT;
1753 cm->cmsg_len = CMSG_LEN(sizeof(int));
1754 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1758 "<%s> send RA on %s, # of RS waitings = %d",
1759 __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount);
1761 i = sendmsg(sock.si_fd, &sndmhdr, 0);
1763 if (i < 0 || (size_t)i != rai->rai_ra_datalen) {
1765 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1766 __func__, ifi->ifi_ifname,
1772 * unicast advertisements
1773 * XXX commented out. reason: though spec does not forbit it, unicast
1774 * advert does not really help
1776 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
1777 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
1781 /* update timestamp */
1782 gettimeofday(&ifi->ifi_ra_lastsent, NULL);
1784 /* update counter */
1785 ifi->ifi_rs_waitcount = 0;
1786 ifi->ifi_raoutput++;
1788 switch (ifi->ifi_state) {
1789 case IFI_STATE_CONFIGURED:
1790 if (ifi->ifi_burstcount > 0)
1791 ifi->ifi_burstcount--;
1793 case IFI_STATE_TRANSITIVE:
1794 ifi->ifi_burstcount--;
1795 if (ifi->ifi_burstcount == 0) {
1796 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1797 /* Initial burst finished. */
1798 if (ifi->ifi_rainfo_trans != NULL)
1799 ifi->ifi_rainfo_trans = NULL;
1802 /* Remove burst RA information */
1803 if (ifi->ifi_rainfo_trans != NULL) {
1804 rm_rainfo(ifi->ifi_rainfo_trans);
1805 ifi->ifi_rainfo_trans = NULL;
1808 if (ifi->ifi_rainfo != NULL) {
1810 * TRANSITIVE -> CONFIGURED
1812 * After initial burst or transition from
1813 * one configuration to another,
1814 * ifi_rainfo always points to the next RA
1817 ifi->ifi_state = IFI_STATE_CONFIGURED;
1819 "<%s> ifname=%s marked as "
1820 "CONFIGURED.", __func__,
1824 * TRANSITIVE -> UNCONFIGURED
1826 * If ifi_rainfo points to NULL, this
1827 * interface is shutting down.
1832 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
1834 "<%s> ifname=%s marked as "
1835 "UNCONFIGURED.", __func__,
1837 error = sock_mc_leave(&sock,
1847 /* process RA timer */
1848 struct rtadvd_timer *
1849 ra_timeout(void *arg)
1853 ifi = (struct ifinfo *)arg;
1854 syslog(LOG_DEBUG, "<%s> RA timer on %s is expired",
1855 __func__, ifi->ifi_ifname);
1859 return (ifi->ifi_ra_timer);
1862 /* update RA timer */
1864 ra_timer_update(void *arg, struct timeval *tm)
1870 ifi = (struct ifinfo *)arg;
1871 rai = ifi->ifi_rainfo;
1874 switch (ifi->ifi_state) {
1875 case IFI_STATE_UNCONFIGURED:
1878 case IFI_STATE_CONFIGURED:
1880 * Whenever a multicast advertisement is sent from
1881 * an interface, the timer is reset to a
1882 * uniformly-distributed random value between the
1883 * interface's configured MinRtrAdvInterval and
1884 * MaxRtrAdvInterval (RFC4861 6.2.4).
1886 interval = rai->rai_mininterval;
1887 #ifdef HAVE_ARC4RANDOM
1888 interval += arc4random_uniform(rai->rai_maxinterval -
1889 rai->rai_mininterval);
1891 interval += random() % (rai->rai_maxinterval -
1892 rai->rai_mininterval);
1895 case IFI_STATE_TRANSITIVE:
1897 * For the first few advertisements (up to
1898 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen
1899 * interval is greater than
1900 * MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer SHOULD be
1901 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead. (RFC
1904 * In such cases, the router SHOULD transmit one or more
1905 * (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final
1906 * multicast Router Advertisements on the interface with a
1907 * Router Lifetime field of zero. (RFC 4861 6.2.5)
1909 interval = ifi->ifi_burstinterval;
1913 tm->tv_sec = interval;
1917 "<%s> RA timer on %s is set to %ld:%ld",
1918 __func__, ifi->ifi_ifname,
1919 (long int)tm->tv_sec, (long int)tm->tv_usec);