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>
38 #include <sys/queue.h>
40 #include <sys/sysctl.h>
43 #include <net/if_types.h>
44 #include <net/if_media.h>
45 #include <net/if_dl.h>
46 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/ip6.h>
49 #include <netinet6/ip6_var.h>
50 #include <netinet/icmp6.h>
52 #include <arpa/inet.h>
54 #include <net/if_var.h>
55 #include <netinet/in_var.h>
56 #include <netinet6/nd6.h>
72 #include "pathnames.h"
77 #include "timer_subr.h"
81 #include "control_server.h"
83 #define RTADV_TYPE2BITMASK(type) (0x1 << type)
85 struct msghdr rcvmhdr;
86 static char *rcvcmsgbuf;
87 static size_t rcvcmsgbuflen;
88 static char *sndcmsgbuf = NULL;
89 static size_t sndcmsgbuflen;
90 struct msghdr sndmhdr;
91 struct iovec rcviov[2];
92 struct iovec sndiov[2];
93 struct sockaddr_in6 rcvfrom;
94 static const char *pidfilename = _PATH_RTADVDPID;
95 const char *conffile = _PATH_RTADVDCONF;
96 static struct pidfh *pfh;
97 static int dflag, sflag;
98 static int wait_shutdown;
100 #define PFD_RAWSOCK 0
105 struct railist_head_t railist =
106 TAILQ_HEAD_INITIALIZER(railist);
107 struct ifilist_head_t ifilist =
108 TAILQ_HEAD_INITIALIZER(ifilist);
111 TAILQ_ENTRY(nd_optlist) nol_next;
112 struct nd_opt_hdr *nol_opt;
115 struct nd_opt_hdr *opt_array[9];
117 struct nd_opt_hdr *zero;
118 struct nd_opt_hdr *src_lladdr;
119 struct nd_opt_hdr *tgt_lladdr;
120 struct nd_opt_prefix_info *pi;
121 struct nd_opt_rd_hdr *rh;
122 struct nd_opt_mtu *mtu;
123 TAILQ_HEAD(, nd_optlist) opt_list;
126 #define opt_src_lladdr nd_opt_each.src_lladdr
127 #define opt_tgt_lladdr nd_opt_each.tgt_lladdr
128 #define opt_pi nd_opt_each.pi
129 #define opt_rh nd_opt_each.rh
130 #define opt_mtu nd_opt_each.mtu
131 #define opt_list nd_opt_each.opt_list
133 #define NDOPT_FLAG_SRCLINKADDR (1 << 0)
134 #define NDOPT_FLAG_TGTLINKADDR (1 << 1)
135 #define NDOPT_FLAG_PREFIXINFO (1 << 2)
136 #define NDOPT_FLAG_RDHDR (1 << 3)
137 #define NDOPT_FLAG_MTU (1 << 4)
138 #define NDOPT_FLAG_RDNSS (1 << 5)
139 #define NDOPT_FLAG_DNSSL (1 << 6)
141 static uint32_t ndopt_flags[] = {
142 [ND_OPT_SOURCE_LINKADDR] = NDOPT_FLAG_SRCLINKADDR,
143 [ND_OPT_TARGET_LINKADDR] = NDOPT_FLAG_TGTLINKADDR,
144 [ND_OPT_PREFIX_INFORMATION] = NDOPT_FLAG_PREFIXINFO,
145 [ND_OPT_REDIRECTED_HEADER] = NDOPT_FLAG_RDHDR,
146 [ND_OPT_MTU] = NDOPT_FLAG_MTU,
147 [ND_OPT_RDNSS] = NDOPT_FLAG_RDNSS,
148 [ND_OPT_DNSSL] = NDOPT_FLAG_DNSSL,
151 static void rtadvd_shutdown(void);
152 static void sock_open(struct sockinfo *);
153 static void rtsock_open(struct sockinfo *);
154 static void rtadvd_input(struct sockinfo *);
155 static void rs_input(int, struct nd_router_solicit *,
156 struct in6_pktinfo *, struct sockaddr_in6 *);
157 static void ra_input(int, struct nd_router_advert *,
158 struct in6_pktinfo *, struct sockaddr_in6 *);
159 static int prefix_check(struct nd_opt_prefix_info *, struct rainfo *,
160 struct sockaddr_in6 *);
161 static int nd6_options(struct nd_opt_hdr *, int,
162 union nd_opt *, uint32_t);
163 static void free_ndopts(union nd_opt *);
164 static void rtmsg_input(struct sockinfo *);
165 static void set_short_delay(struct ifinfo *);
166 static int check_accept_rtadv(int);
172 fprintf(stderr, "usage: rtadvd [-dDfRs] "
173 "[-c configfile] [-C ctlsock] [-M ifname] [-p pidfile]\n");
178 main(int argc, char *argv[])
180 struct pollfd set[PFD_MAX];
181 struct timespec *timeout;
183 int fflag = 0, logopt;
187 /* get command line options and arguments */
188 while ((ch = getopt(argc, argv, "c:C:dDfhM:p:Rs")) != -1) {
194 ctrlsock.si_name = optarg;
209 fprintf(stderr, "rtadvd: "
210 "the -R option is currently ignored.\n");
218 pidfilename = optarg;
227 logopt = LOG_NDELAY | LOG_PID;
229 logopt |= LOG_PERROR;
230 openlog("rtadvd", logopt, LOG_DAEMON);
234 (void)setlogmask(LOG_UPTO(LOG_DEBUG));
236 (void)setlogmask(LOG_UPTO(LOG_INFO));
238 (void)setlogmask(LOG_UPTO(LOG_NOTICE));
240 (void)setlogmask(LOG_UPTO(LOG_ERR));
242 /* timer initialization */
245 #ifndef HAVE_ARC4RANDOM
246 /* random value initialization */
250 srandom((unsigned long)time(NULL));
253 pfh = pidfile_open(pidfilename, 0600, &otherpid);
256 errx(1, "%s already running, pid: %d",
257 getprogname(), otherpid);
259 "failed to open the pid file %s, run anyway.",
267 update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
268 for (i = 0; i < argc; i++)
269 update_persist_ifinfo(&ifilist, argv[i]);
271 csock_open(&ctrlsock, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
272 if (ctrlsock.si_fd == -1) {
273 syslog(LOG_ERR, "cannot open control socket: %s",
278 /* record the current PID */
282 set[PFD_RAWSOCK].fd = sock.si_fd;
283 set[PFD_RAWSOCK].events = POLLIN;
285 rtsock_open(&rtsock);
286 set[PFD_RTSOCK].fd = rtsock.si_fd;
287 set[PFD_RTSOCK].events = POLLIN;
289 set[PFD_RTSOCK].fd = -1;
290 set[PFD_CSOCK].fd = ctrlsock.si_fd;
291 set[PFD_CSOCK].events = POLLIN;
292 signal(SIGTERM, set_do_shutdown);
293 signal(SIGINT, set_do_shutdown);
294 signal(SIGHUP, set_do_reload);
296 error = csock_listen(&ctrlsock);
298 syslog(LOG_ERR, "cannot listen control socket: %s",
303 /* load configuration file */
307 if (is_do_shutdown())
310 if (is_do_reload()) {
311 loadconfig_ifname(reload_ifname());
312 if (reload_ifname() == NULL)
314 "configuration file reloaded.");
317 "configuration file for %s reloaded.",
322 /* timeout handler update for active interfaces */
323 rtadvd_update_timeout_handler();
325 /* timer expiration check and reset the timer */
326 timeout = rtadvd_check_timer();
328 if (timeout != NULL) {
330 "<%s> set timer to %ld:%ld. waiting for "
331 "inputs or timeout", __func__,
332 (long int)timeout->tv_sec,
333 (long int)timeout->tv_nsec / 1000);
336 "<%s> there's no timer. waiting for inputs",
339 if ((i = poll(set, sizeof(set)/sizeof(set[0]),
340 timeout ? (timeout->tv_sec * 1000 +
341 timeout->tv_nsec / 1000 / 1000) : INFTIM)) < 0) {
343 /* EINTR would occur if a signal was delivered */
345 syslog(LOG_ERR, "poll() failed: %s",
349 if (i == 0) /* timeout */
351 if (rtsock.si_fd != -1 && set[PFD_RTSOCK].revents & POLLIN)
352 rtmsg_input(&rtsock);
354 if (set[PFD_RAWSOCK].revents & POLLIN)
357 if (set[PFD_CSOCK].revents & POLLIN) {
360 fd = csock_accept(&ctrlsock);
363 "cannot accept() control socket: %s",
366 cm_handler_server(fd);
371 exit(0); /* NOTREACHED */
375 rtadvd_shutdown(void)
384 "waiting expiration of the all RA timers.");
386 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
388 * Ignore !IFF_UP interfaces in waiting for shutdown.
390 if (!(ifi->ifi_flags & IFF_UP) &&
391 ifi->ifi_ra_timer != NULL) {
392 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
393 rtadvd_remove_timer(ifi->ifi_ra_timer);
394 ifi->ifi_ra_timer = NULL;
395 syslog(LOG_DEBUG, "<%s> %s(idx=%d) is down. "
396 "Timer removed and marked as UNCONFIGURED.",
397 __func__, ifi->ifi_ifname,
401 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
402 if (ifi->ifi_ra_timer != NULL)
406 syslog(LOG_NOTICE, "gracefully terminated.");
414 syslog(LOG_DEBUG, "<%s> cease to be an advertising router",
419 TAILQ_FOREACH(rai, &railist, rai_next) {
420 rai->rai_lifetime = 0;
421 TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next)
423 TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next)
426 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
427 if (!ifi->ifi_persist)
429 if (ifi->ifi_state == IFI_STATE_UNCONFIGURED)
431 if (ifi->ifi_ra_timer == NULL)
433 if (ifi->ifi_ra_lastsent.tv_sec == 0 &&
434 ifi->ifi_ra_lastsent.tv_nsec == 0 &&
435 ifi->ifi_ra_timer != NULL) {
437 * When RA configured but never sent,
438 * ignore the IF immediately.
440 rtadvd_remove_timer(ifi->ifi_ra_timer);
441 ifi->ifi_ra_timer = NULL;
442 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
446 ifi->ifi_state = IFI_STATE_TRANSITIVE;
448 /* Mark as the shut-down state. */
449 ifi->ifi_rainfo_trans = ifi->ifi_rainfo;
450 ifi->ifi_rainfo = NULL;
452 ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
453 ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
455 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
456 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
459 syslog(LOG_NOTICE, "final RA transmission started.");
462 csock_close(&ctrlsock);
466 rtmsg_input(struct sockinfo *s)
468 int n, type, ifindex = 0, plen;
470 char msg[2048], *next, *lim;
471 char ifname[IFNAMSIZ];
472 struct if_announcemsghdr *ifan;
473 struct rt_msghdr *rtm;
476 struct in6_addr *addr;
478 char addrbuf[INET6_ADDRSTRLEN];
479 int prefixchange = 0;
482 syslog(LOG_ERR, "<%s> internal error", __func__);
485 n = read(s->si_fd, msg, sizeof(msg));
486 rtm = (struct rt_msghdr *)msg;
487 syslog(LOG_DEBUG, "<%s> received a routing message "
488 "(type = %d, len = %d)", __func__, rtm->rtm_type, n);
490 if (n > rtm->rtm_msglen) {
492 * This usually won't happen for messages received on
496 "<%s> received data length is larger than "
497 "1st routing message len. multiple messages? "
498 "read %d bytes, but 1st msg len = %d",
499 __func__, n, rtm->rtm_msglen);
507 for (next = msg; next < lim; next += len) {
510 next = get_next_msg(next, lim, 0, &len,
511 RTADV_TYPE2BITMASK(RTM_ADD) |
512 RTADV_TYPE2BITMASK(RTM_DELETE) |
513 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
514 RTADV_TYPE2BITMASK(RTM_DELADDR) |
515 RTADV_TYPE2BITMASK(RTM_IFINFO) |
516 RTADV_TYPE2BITMASK(RTM_IFANNOUNCE));
519 type = ((struct rt_msghdr *)next)->rtm_type;
523 ifindex = get_rtm_ifindex(next);
527 ifindex = (int)((struct ifa_msghdr *)next)->ifam_index;
530 ifindex = (int)((struct if_msghdr *)next)->ifm_index;
533 ifan = (struct if_announcemsghdr *)next;
534 switch (ifan->ifan_what) {
540 "<%s:%d> unknown ifan msg (ifan_what=%d)",
541 __func__, __LINE__, ifan->ifan_what);
545 syslog(LOG_DEBUG, "<%s>: if_announcemsg (idx=%d:%d)",
546 __func__, ifan->ifan_index, ifan->ifan_what);
547 switch (ifan->ifan_what) {
550 "interface added (idx=%d)",
552 update_ifinfo(&ifilist, ifan->ifan_index);
553 loadconfig_index(ifan->ifan_index);
557 "interface removed (idx=%d)",
559 rm_ifinfo_index(ifan->ifan_index);
561 /* Clear ifi_ifindex */
562 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
564 == ifan->ifan_index) {
565 ifi->ifi_ifindex = 0;
569 update_ifinfo(&ifilist, ifan->ifan_index);
574 /* should not reach here */
576 "<%s:%d> unknown rtmsg %d on %s",
577 __func__, __LINE__, type,
578 if_indextoname(ifindex, ifname));
581 ifi = if_indextoifinfo(ifindex);
584 "<%s> ifinfo not found for idx=%d. Why?",
588 rai = ifi->ifi_rainfo;
591 "<%s> route changed on "
592 "non advertising interface(%s)",
593 __func__, ifi->ifi_ifname);
597 oldifflags = ifi->ifi_flags;
598 /* init ifflags because it may have changed */
599 update_ifinfo(&ifilist, ifindex);
604 break; /* we aren't interested in prefixes */
606 addr = get_addr(msg);
607 plen = get_prefixlen(msg);
608 /* sanity check for plen */
609 /* as RFC2373, prefixlen is at least 4 */
610 if (plen < 4 || plen > 127) {
611 syslog(LOG_INFO, "<%s> new interface route's"
612 "plen %d is invalid for a prefix",
616 pfx = find_prefix(rai, addr, plen);
618 if (pfx->pfx_timer) {
620 * If the prefix has been invalidated,
621 * make it available again.
627 "<%s> new prefix(%s/%d) "
629 "but it was already in list",
631 inet_ntop(AF_INET6, addr,
634 plen, ifi->ifi_ifname);
637 make_prefix(rai, ifindex, addr, plen);
644 addr = get_addr(msg);
645 plen = get_prefixlen(msg);
646 /* sanity check for plen */
647 /* as RFC2373, prefixlen is at least 4 */
648 if (plen < 4 || plen > 127) {
650 "<%s> deleted interface route's "
651 "plen %d is invalid for a prefix",
655 pfx = find_prefix(rai, addr, plen);
658 "<%s> prefix(%s/%d) was deleted on %s, "
659 "but it was not in list",
660 __func__, inet_ntop(AF_INET6, addr,
661 (char *)addrbuf, sizeof(addrbuf)),
662 plen, ifi->ifi_ifname);
665 invalidate_prefix(pfx);
673 /* should not reach here */
675 "<%s:%d> unknown rtmsg %d on %s",
676 __func__, __LINE__, type,
677 if_indextoname(ifindex, ifname));
681 /* check if an interface flag is changed */
682 if ((oldifflags & IFF_UP) && /* UP to DOWN */
683 !(ifi->ifi_flags & IFF_UP)) {
685 "<interface %s becomes down. stop timer.",
687 rtadvd_remove_timer(ifi->ifi_ra_timer);
688 ifi->ifi_ra_timer = NULL;
689 } else if (!(oldifflags & IFF_UP) && /* DOWN to UP */
690 (ifi->ifi_flags & IFF_UP)) {
692 "interface %s becomes up. restart timer.",
695 ifi->ifi_state = IFI_STATE_TRANSITIVE;
696 ifi->ifi_burstcount =
697 MAX_INITIAL_RTR_ADVERTISEMENTS;
698 ifi->ifi_burstinterval =
699 MAX_INITIAL_RTR_ADVERT_INTERVAL;
701 ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
702 ra_timer_update, ifi, ifi);
703 ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
704 rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
706 } else if (prefixchange &&
707 (ifi->ifi_flags & IFF_UP)) {
709 * An advertised prefix has been added or invalidated.
710 * Will notice the change in a short delay.
712 set_short_delay(ifi);
720 rtadvd_input(struct sockinfo *s)
727 struct icmp6_hdr *icp;
730 struct in6_pktinfo *pi = NULL;
731 char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
732 struct in6_addr dst = in6addr_any;
735 syslog(LOG_DEBUG, "<%s> enter", __func__);
738 syslog(LOG_ERR, "<%s> internal error", __func__);
742 * Get message. We reset msg_controllen since the field could
743 * be modified if we had received a message before setting
746 rcvmhdr.msg_controllen = rcvcmsgbuflen;
747 if ((i = recvmsg(s->si_fd, &rcvmhdr, 0)) < 0)
750 /* extract optional information via Advanced API */
751 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
753 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
754 if (cm->cmsg_level == IPPROTO_IPV6 &&
755 cm->cmsg_type == IPV6_PKTINFO &&
756 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
757 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
758 ifindex = pi->ipi6_ifindex;
761 if (cm->cmsg_level == IPPROTO_IPV6 &&
762 cm->cmsg_type == IPV6_HOPLIMIT &&
763 cm->cmsg_len == CMSG_LEN(sizeof(int)))
764 hlimp = (int *)CMSG_DATA(cm);
767 syslog(LOG_ERR, "failed to get receiving interface");
771 syslog(LOG_ERR, "failed to get receiving hop limit");
776 * If we happen to receive data on an interface which is now gone
777 * or down, just discard the data.
779 ifi = if_indextoifinfo(pi->ipi6_ifindex);
780 if (ifi == NULL || !(ifi->ifi_flags & IFF_UP)) {
782 "<%s> received data on a disabled interface (%s)",
784 (ifi == NULL) ? "[gone]" : ifi->ifi_ifname);
789 if ((size_t)i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
791 "packet size(%d) is too short", i);
795 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
796 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
798 if ((size_t)i < sizeof(struct icmp6_hdr)) {
799 syslog(LOG_ERR, "packet size(%zd) is too short", i);
803 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
806 switch (icp->icmp6_type) {
807 case ND_ROUTER_SOLICIT:
809 * Message verification - RFC 4861 6.1.1
810 * XXX: these checks must be done in the kernel as well,
811 * but we can't completely rely on them.
815 "RS with invalid hop limit(%d) "
816 "received from %s on %s",
818 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
820 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
823 if (icp->icmp6_code) {
825 "RS with invalid ICMP6 code(%d) "
826 "received from %s on %s",
828 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
830 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
833 if ((size_t)i < sizeof(struct nd_router_solicit)) {
835 "RS from %s on %s does not have enough "
836 "length (len = %zd)",
837 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
839 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
842 rs_input(i, (struct nd_router_solicit *)icp, pi, &rcvfrom);
844 case ND_ROUTER_ADVERT:
846 * Message verification - RFC 4861 6.1.2
847 * XXX: there's the same dilemma as above...
849 if (!IN6_IS_ADDR_LINKLOCAL(&rcvfrom.sin6_addr)) {
851 "RA with non-linklocal source address "
852 "received from %s on %s",
853 inet_ntop(AF_INET6, &rcvfrom.sin6_addr,
854 ntopbuf, sizeof(ntopbuf)),
855 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
860 "RA with invalid hop limit(%d) "
861 "received from %s on %s",
863 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
865 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
868 if (icp->icmp6_code) {
870 "RA with invalid ICMP6 code(%d) "
871 "received from %s on %s",
873 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
875 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
878 if ((size_t)i < sizeof(struct nd_router_advert)) {
880 "RA from %s on %s does not have enough "
881 "length (len = %zd)",
882 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
884 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
887 ra_input(i, (struct nd_router_advert *)icp, pi, &rcvfrom);
889 case ICMP6_ROUTER_RENUMBERING:
890 if (mcastif == NULL) {
891 syslog(LOG_ERR, "received a router renumbering "
892 "message, but not allowed to be accepted");
895 rr_input(i, (struct icmp6_router_renum *)icp, pi, &rcvfrom,
900 * Note that this case is POSSIBLE, especially just
901 * after invocation of the daemon. This is because we
902 * could receive message after opening the socket and
903 * before setting ICMP6 type filter(see sock_open()).
905 syslog(LOG_ERR, "invalid icmp type(%d)", icp->icmp6_type);
913 rs_input(int len, struct nd_router_solicit *rs,
914 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
916 char ntopbuf[INET6_ADDRSTRLEN];
917 char ifnamebuf[IFNAMSIZ];
921 struct soliciter *sol;
924 "<%s> RS received from %s on %s",
926 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
927 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
929 /* ND option check */
930 memset(&ndopts, 0, sizeof(ndopts));
931 TAILQ_INIT(&ndopts.opt_list);
932 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
933 len - sizeof(struct nd_router_solicit),
934 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
936 "<%s> ND option check failed for an RS from %s on %s",
938 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
940 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
945 * If the IP source address is the unspecified address, there
946 * must be no source link-layer address option in the message.
949 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
950 ndopts.opt_src_lladdr) {
952 "<%s> RS from unspecified src on %s has a link-layer"
954 __func__, if_indextoname(pi->ipi6_ifindex, ifnamebuf));
958 ifi = if_indextoifinfo(pi->ipi6_ifindex);
961 "<%s> if (idx=%d) not found. Why?",
962 __func__, pi->ipi6_ifindex);
965 rai = ifi->ifi_rainfo;
968 "<%s> RS received on non advertising interface(%s)",
970 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
974 rai->rai_ifinfo->ifi_rsinput++;
977 * Decide whether to send RA according to the rate-limit
981 /* record sockaddr waiting for RA, if possible */
982 sol = (struct soliciter *)malloc(sizeof(*sol));
984 sol->sol_addr = *from;
985 /* XXX RFC 2553 need clarification on flowinfo */
986 sol->sol_addr.sin6_flowinfo = 0;
987 TAILQ_INSERT_TAIL(&rai->rai_soliciter, sol, sol_next);
991 * If there is already a waiting RS packet, don't
994 if (ifi->ifi_rs_waitcount++)
997 set_short_delay(ifi);
1000 free_ndopts(&ndopts);
1005 set_short_delay(struct ifinfo *ifi)
1007 long delay; /* must not be greater than 1000000 */
1008 struct timespec interval, now, min_delay, tm_tmp, *rest;
1010 if (ifi->ifi_ra_timer == NULL)
1013 * Compute a random delay. If the computed value
1014 * corresponds to a time later than the time the next
1015 * multicast RA is scheduled to be sent, ignore the random
1016 * delay and send the advertisement at the
1017 * already-scheduled time. RFC 4861 6.2.6
1019 #ifdef HAVE_ARC4RANDOM
1020 delay = arc4random_uniform(MAX_RA_DELAY_TIME);
1022 delay = random() % MAX_RA_DELAY_TIME;
1024 interval.tv_sec = 0;
1025 interval.tv_nsec = delay * 1000;
1026 rest = rtadvd_timer_rest(ifi->ifi_ra_timer);
1027 if (TS_CMP(rest, &interval, <)) {
1028 syslog(LOG_DEBUG, "<%s> random delay is larger than "
1029 "the rest of the current timer", __func__);
1034 * If we sent a multicast Router Advertisement within
1035 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
1036 * the advertisement to be sent at a time corresponding to
1037 * MIN_DELAY_BETWEEN_RAS plus the random value after the
1038 * previous advertisement was sent.
1040 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1041 TS_SUB(&now, &ifi->ifi_ra_lastsent, &tm_tmp);
1042 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
1043 min_delay.tv_nsec = 0;
1044 if (TS_CMP(&tm_tmp, &min_delay, <)) {
1045 TS_SUB(&min_delay, &tm_tmp, &min_delay);
1046 TS_ADD(&min_delay, &interval, &interval);
1048 rtadvd_set_timer(&interval, ifi->ifi_ra_timer);
1052 check_accept_rtadv(int idx)
1056 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1057 if (ifi->ifi_ifindex == idx)
1062 "<%s> if (idx=%d) not found. Why?",
1066 #if (__FreeBSD_version < 900000)
1068 * RA_RECV: !ip6.forwarding && ip6.accept_rtadv
1069 * RA_SEND: ip6.forwarding
1071 return ((getinet6sysctl(IPV6CTL_FORWARDING) == 0) &&
1072 (getinet6sysctl(IPV6CTL_ACCEPT_RTADV) == 1));
1075 * RA_RECV: ND6_IFF_ACCEPT_RTADV
1076 * RA_SEND: ip6.forwarding
1078 if (update_ifinfo_nd_flags(ifi) != 0) {
1079 syslog(LOG_ERR, "cannot get nd6 flags (idx=%d)", idx);
1083 return (ifi->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV);
1088 ra_input(int len, struct nd_router_advert *nra,
1089 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
1093 char ntopbuf[INET6_ADDRSTRLEN];
1094 char ifnamebuf[IFNAMSIZ];
1095 union nd_opt ndopts;
1096 const char *on_off[] = {"OFF", "ON"};
1097 uint32_t reachabletime, retranstimer, mtu;
1098 int inconsistent = 0;
1101 syslog(LOG_DEBUG, "<%s> RA received from %s on %s", __func__,
1102 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
1103 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
1105 /* ND option check */
1106 memset(&ndopts, 0, sizeof(ndopts));
1107 TAILQ_INIT(&ndopts.opt_list);
1108 error = nd6_options((struct nd_opt_hdr *)(nra + 1),
1109 len - sizeof(struct nd_router_advert), &ndopts,
1110 NDOPT_FLAG_SRCLINKADDR | NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU |
1111 NDOPT_FLAG_RDNSS | NDOPT_FLAG_DNSSL);
1114 "<%s> ND option check failed for an RA from %s on %s",
1116 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1117 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1123 * RA consistency check according to RFC 4861 6.2.7
1125 ifi = if_indextoifinfo(pi->ipi6_ifindex);
1126 if (ifi->ifi_rainfo == NULL) {
1128 "<%s> received RA from %s on non-advertising"
1131 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1132 sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1136 rai = ifi->ifi_rainfo;
1138 syslog(LOG_DEBUG, "<%s> ifi->ifi_rainput = %" PRIu64, __func__,
1141 /* Cur Hop Limit value */
1142 if (nra->nd_ra_curhoplimit && rai->rai_hoplimit &&
1143 nra->nd_ra_curhoplimit != rai->rai_hoplimit) {
1145 "CurHopLimit inconsistent on %s:"
1146 " %d from %s, %d from us",
1147 ifi->ifi_ifname, nra->nd_ra_curhoplimit,
1148 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1149 sizeof(ntopbuf)), rai->rai_hoplimit);
1153 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
1154 rai->rai_managedflg) {
1156 "M flag inconsistent on %s:"
1157 " %s from %s, %s from us",
1158 ifi->ifi_ifname, on_off[!rai->rai_managedflg],
1159 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1160 sizeof(ntopbuf)), on_off[rai->rai_managedflg]);
1164 if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
1165 rai->rai_otherflg) {
1167 "O flag inconsistent on %s:"
1168 " %s from %s, %s from us",
1169 ifi->ifi_ifname, on_off[!rai->rai_otherflg],
1170 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1171 sizeof(ntopbuf)), on_off[rai->rai_otherflg]);
1174 /* Reachable Time */
1175 reachabletime = ntohl(nra->nd_ra_reachable);
1176 if (reachabletime && rai->rai_reachabletime &&
1177 reachabletime != rai->rai_reachabletime) {
1179 "ReachableTime inconsistent on %s:"
1180 " %d from %s, %d from us",
1181 ifi->ifi_ifname, reachabletime,
1182 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1183 sizeof(ntopbuf)), rai->rai_reachabletime);
1187 retranstimer = ntohl(nra->nd_ra_retransmit);
1188 if (retranstimer && rai->rai_retranstimer &&
1189 retranstimer != rai->rai_retranstimer) {
1191 "RetranceTimer inconsistent on %s:"
1192 " %d from %s, %d from us",
1193 ifi->ifi_ifname, retranstimer,
1194 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1195 sizeof(ntopbuf)), rai->rai_retranstimer);
1198 /* Values in the MTU options */
1199 if (ndopts.opt_mtu) {
1200 mtu = ntohl(ndopts.opt_mtu->nd_opt_mtu_mtu);
1201 if (mtu && rai->rai_linkmtu && mtu != rai->rai_linkmtu) {
1203 "MTU option value inconsistent on %s:"
1204 " %d from %s, %d from us",
1205 ifi->ifi_ifname, mtu,
1206 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1207 sizeof(ntopbuf)), rai->rai_linkmtu);
1211 /* Preferred and Valid Lifetimes for prefixes */
1213 struct nd_optlist *nol;
1216 if (prefix_check(ndopts.opt_pi, rai, from))
1219 TAILQ_FOREACH(nol, &ndopts.opt_list, nol_next)
1220 if (prefix_check((struct nd_opt_prefix_info *)nol->nol_opt,
1226 ifi->ifi_rainconsistent++;
1229 free_ndopts(&ndopts);
1233 /* return a non-zero value if the received prefix is inconsitent with ours */
1235 prefix_check(struct nd_opt_prefix_info *pinfo,
1236 struct rainfo *rai, struct sockaddr_in6 *from)
1239 uint32_t preferred_time, valid_time;
1241 int inconsistent = 0;
1242 char ntopbuf[INET6_ADDRSTRLEN];
1243 char prefixbuf[INET6_ADDRSTRLEN];
1244 struct timespec now;
1246 #if 0 /* impossible */
1247 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1250 ifi = rai->rai_ifinfo;
1252 * log if the adveritsed prefix has link-local scope(sanity check?)
1254 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix))
1256 "<%s> link-local prefix %s/%d is advertised "
1259 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1261 pinfo->nd_opt_pi_prefix_len,
1262 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1263 sizeof(ntopbuf)), ifi->ifi_ifname);
1265 if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1266 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1268 "<%s> prefix %s/%d from %s on %s is not in our list",
1270 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1272 pinfo->nd_opt_pi_prefix_len,
1273 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1274 sizeof(ntopbuf)), ifi->ifi_ifname);
1278 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1279 if (pfx->pfx_pltimeexpire) {
1281 * The lifetime is decremented in real time, so we should
1282 * compare the expiration time.
1283 * (RFC 2461 Section 6.2.7.)
1284 * XXX: can we really expect that all routers on the link
1285 * have synchronized clocks?
1287 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1288 preferred_time += now.tv_sec;
1290 if (!pfx->pfx_timer && rai->rai_clockskew &&
1291 abs(preferred_time - pfx->pfx_pltimeexpire) > rai->rai_clockskew) {
1293 "<%s> preferred lifetime for %s/%d"
1294 " (decr. in real time) inconsistent on %s:"
1295 " %" PRIu32 " from %s, %" PRIu32 " from us",
1297 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1299 pinfo->nd_opt_pi_prefix_len,
1300 ifi->ifi_ifname, preferred_time,
1301 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1302 sizeof(ntopbuf)), pfx->pfx_pltimeexpire);
1305 } else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime)
1307 "<%s> preferred lifetime for %s/%d"
1308 " inconsistent on %s:"
1309 " %d from %s, %d from us",
1311 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1313 pinfo->nd_opt_pi_prefix_len,
1314 ifi->ifi_ifname, preferred_time,
1315 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1316 sizeof(ntopbuf)), pfx->pfx_preflifetime);
1318 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1319 if (pfx->pfx_vltimeexpire) {
1320 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1321 valid_time += now.tv_sec;
1323 if (!pfx->pfx_timer && rai->rai_clockskew &&
1324 abs(valid_time - pfx->pfx_vltimeexpire) > rai->rai_clockskew) {
1326 "<%s> valid lifetime for %s/%d"
1327 " (decr. in real time) inconsistent on %s:"
1328 " %d from %s, %" PRIu32 " from us",
1330 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1332 pinfo->nd_opt_pi_prefix_len,
1333 ifi->ifi_ifname, preferred_time,
1334 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1335 sizeof(ntopbuf)), pfx->pfx_vltimeexpire);
1338 } else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) {
1340 "<%s> valid lifetime for %s/%d"
1341 " inconsistent on %s:"
1342 " %d from %s, %d from us",
1344 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1346 pinfo->nd_opt_pi_prefix_len,
1347 ifi->ifi_ifname, valid_time,
1348 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1349 sizeof(ntopbuf)), pfx->pfx_validlifetime);
1353 return (inconsistent);
1357 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1360 int bytelen, bitlen;
1363 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1364 if (plen != pfx->pfx_prefixlen)
1369 bitmask = 0xff << (8 - bitlen);
1371 if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen))
1375 ((prefix->s6_addr[bytelen] & bitmask) ==
1376 (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) {
1384 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1386 prefix_match(struct in6_addr *p0, int plen0,
1387 struct in6_addr *p1, int plen1)
1389 int bytelen, bitlen;
1395 bytelen = plen1 / 8;
1397 bitmask = 0xff << (8 - bitlen);
1399 if (memcmp((void *)p0, (void *)p1, bytelen))
1403 ((p0->s6_addr[bytelen] & bitmask) ==
1404 (p1->s6_addr[bytelen] & bitmask))) {
1412 nd6_options(struct nd_opt_hdr *hdr, int limit,
1413 union nd_opt *ndopts, uint32_t optflags)
1417 for (; limit > 0; limit -= optlen) {
1418 if ((size_t)limit < sizeof(struct nd_opt_hdr)) {
1419 syslog(LOG_INFO, "<%s> short option header", __func__);
1423 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1424 if (hdr->nd_opt_len == 0) {
1426 "<%s> bad ND option length(0) (type = %d)",
1427 __func__, hdr->nd_opt_type);
1430 optlen = hdr->nd_opt_len << 3;
1431 if (optlen > limit) {
1432 syslog(LOG_INFO, "<%s> short option", __func__);
1436 if (hdr->nd_opt_type > ND_OPT_MTU &&
1437 hdr->nd_opt_type != ND_OPT_RDNSS &&
1438 hdr->nd_opt_type != ND_OPT_DNSSL) {
1439 syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1440 __func__, hdr->nd_opt_type);
1444 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1445 syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1446 __func__, hdr->nd_opt_type);
1451 * Option length check. Do it here for all fixed-length
1454 switch (hdr->nd_opt_type) {
1456 if (optlen == sizeof(struct nd_opt_mtu))
1461 (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0)
1466 (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0)
1469 case ND_OPT_PREFIX_INFORMATION:
1470 if (optlen == sizeof(struct nd_opt_prefix_info))
1473 syslog(LOG_INFO, "<%s> invalid option length",
1478 switch (hdr->nd_opt_type) {
1479 case ND_OPT_TARGET_LINKADDR:
1480 case ND_OPT_REDIRECTED_HEADER:
1483 break; /* we don't care about these options */
1484 case ND_OPT_SOURCE_LINKADDR:
1486 if (ndopts->opt_array[hdr->nd_opt_type]) {
1488 "<%s> duplicated ND option (type = %d)",
1489 __func__, hdr->nd_opt_type);
1491 ndopts->opt_array[hdr->nd_opt_type] = hdr;
1493 case ND_OPT_PREFIX_INFORMATION:
1495 struct nd_optlist *nol;
1497 if (ndopts->opt_pi == 0) {
1499 (struct nd_opt_prefix_info *)hdr;
1502 nol = malloc(sizeof(*nol));
1504 syslog(LOG_ERR, "<%s> can't allocate memory",
1509 TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next);
1513 default: /* impossible */
1521 free_ndopts(ndopts);
1527 free_ndopts(union nd_opt *ndopts)
1529 struct nd_optlist *nol;
1531 while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) {
1532 TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next);
1538 sock_open(struct sockinfo *s)
1540 struct icmp6_filter filt;
1542 /* XXX: should be max MTU attached to the node */
1543 static char answer[1500];
1545 syslog(LOG_DEBUG, "<%s> enter", __func__);
1548 syslog(LOG_ERR, "<%s> internal error", __func__);
1551 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1552 CMSG_SPACE(sizeof(int));
1553 rcvcmsgbuf = (char *)malloc(rcvcmsgbuflen);
1554 if (rcvcmsgbuf == NULL) {
1555 syslog(LOG_ERR, "<%s> not enough core", __func__);
1559 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1560 CMSG_SPACE(sizeof(int));
1561 sndcmsgbuf = (char *)malloc(sndcmsgbuflen);
1562 if (sndcmsgbuf == NULL) {
1563 syslog(LOG_ERR, "<%s> not enough core", __func__);
1567 if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1568 syslog(LOG_ERR, "<%s> socket: %s", __func__, strerror(errno));
1571 /* specify to tell receiving interface */
1573 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1575 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s", __func__,
1580 /* specify to tell value of hoplimit field of received IP6 hdr */
1581 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1583 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s", __func__,
1587 ICMP6_FILTER_SETBLOCKALL(&filt);
1588 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1589 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1590 if (mcastif != NULL)
1591 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1593 if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1594 sizeof(filt)) < 0) {
1595 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1596 __func__, strerror(errno));
1600 /* initialize msghdr for receiving packets */
1601 rcviov[0].iov_base = (caddr_t)answer;
1602 rcviov[0].iov_len = sizeof(answer);
1603 rcvmhdr.msg_name = (caddr_t)&rcvfrom;
1604 rcvmhdr.msg_namelen = sizeof(rcvfrom);
1605 rcvmhdr.msg_iov = rcviov;
1606 rcvmhdr.msg_iovlen = 1;
1607 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1608 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1610 /* initialize msghdr for sending packets */
1611 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1612 sndmhdr.msg_iov = sndiov;
1613 sndmhdr.msg_iovlen = 1;
1614 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1615 sndmhdr.msg_controllen = sndcmsgbuflen;
1620 /* open a routing socket to watch the routing table */
1622 rtsock_open(struct sockinfo *s)
1625 syslog(LOG_ERR, "<%s> internal error", __func__);
1628 if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1630 "<%s> socket: %s", __func__, strerror(errno));
1636 if_indextoifinfo(int idx)
1639 char *name, name0[IFNAMSIZ];
1641 /* Check if the interface has a valid name or not. */
1642 if (if_indextoname(idx, name0) == NULL)
1645 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1646 if (ifi->ifi_ifindex == idx)
1651 syslog(LOG_DEBUG, "<%s> ifi found (idx=%d)",
1654 syslog(LOG_DEBUG, "<%s> ifi not found (idx=%d)",
1657 return (NULL); /* search failed */
1661 ra_output(struct ifinfo *ifi)
1665 struct in6_pktinfo *pi;
1666 struct soliciter *sol;
1669 switch (ifi->ifi_state) {
1670 case IFI_STATE_CONFIGURED:
1671 rai = ifi->ifi_rainfo;
1673 case IFI_STATE_TRANSITIVE:
1674 rai = ifi->ifi_rainfo_trans;
1676 case IFI_STATE_UNCONFIGURED:
1677 syslog(LOG_DEBUG, "<%s> %s is unconfigured. "
1678 "Skip sending RAs.",
1679 __func__, ifi->ifi_ifname);
1685 syslog(LOG_DEBUG, "<%s> rainfo is NULL on %s."
1686 "Skip sending RAs.",
1687 __func__, ifi->ifi_ifname);
1690 if (!(ifi->ifi_flags & IFF_UP)) {
1691 syslog(LOG_DEBUG, "<%s> %s is not up. "
1692 "Skip sending RAs.",
1693 __func__, ifi->ifi_ifname);
1697 * Check lifetime, ACCEPT_RTADV flag, and ip6.forwarding.
1699 * (lifetime == 0) = output
1700 * (lifetime != 0 && (check_accept_rtadv()) = no output
1702 * Basically, hosts MUST NOT send Router Advertisement
1703 * messages at any time (RFC 4861, Section 6.2.3). However, it
1704 * would sometimes be useful to allow hosts to advertise some
1705 * parameters such as prefix information and link MTU. Thus,
1706 * we allow hosts to invoke rtadvd only when router lifetime
1707 * (on every advertising interface) is explicitly set
1708 * zero. (see also the above section)
1711 "<%s> check lifetime=%d, ACCEPT_RTADV=%d, ip6.forwarding=%d "
1714 check_accept_rtadv(ifi->ifi_ifindex),
1715 getinet6sysctl(IPV6CTL_FORWARDING),
1718 if (rai->rai_lifetime != 0) {
1719 if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) {
1721 "non-zero lifetime RA "
1722 "but net.inet6.ip6.forwarding=0. "
1726 if (check_accept_rtadv(ifi->ifi_ifindex)) {
1728 "non-zero lifetime RA "
1729 "on RA receiving interface %s."
1730 " Ignored.", ifi->ifi_ifname);
1735 make_packet(rai); /* XXX: inefficient */
1737 sndmhdr.msg_name = (caddr_t)&sin6_linklocal_allnodes;
1738 sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data;
1739 sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen;
1741 cm = CMSG_FIRSTHDR(&sndmhdr);
1742 /* specify the outgoing interface */
1743 cm->cmsg_level = IPPROTO_IPV6;
1744 cm->cmsg_type = IPV6_PKTINFO;
1745 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1746 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1747 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1748 pi->ipi6_ifindex = ifi->ifi_ifindex;
1750 /* specify the hop limit of the packet */
1754 cm = CMSG_NXTHDR(&sndmhdr, cm);
1755 cm->cmsg_level = IPPROTO_IPV6;
1756 cm->cmsg_type = IPV6_HOPLIMIT;
1757 cm->cmsg_len = CMSG_LEN(sizeof(int));
1758 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1762 "<%s> send RA on %s, # of RS waitings = %d",
1763 __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount);
1765 i = sendmsg(sock.si_fd, &sndmhdr, 0);
1767 if (i < 0 || (size_t)i != rai->rai_ra_datalen) {
1769 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1770 __func__, ifi->ifi_ifname,
1776 * unicast advertisements
1777 * XXX commented out. reason: though spec does not forbit it, unicast
1778 * advert does not really help
1780 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
1781 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
1785 /* update timestamp */
1786 clock_gettime(CLOCK_MONOTONIC_FAST, &ifi->ifi_ra_lastsent);
1788 /* update counter */
1789 ifi->ifi_rs_waitcount = 0;
1790 ifi->ifi_raoutput++;
1792 switch (ifi->ifi_state) {
1793 case IFI_STATE_CONFIGURED:
1794 if (ifi->ifi_burstcount > 0)
1795 ifi->ifi_burstcount--;
1797 case IFI_STATE_TRANSITIVE:
1798 ifi->ifi_burstcount--;
1799 if (ifi->ifi_burstcount == 0) {
1800 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1801 /* Initial burst finished. */
1802 if (ifi->ifi_rainfo_trans != NULL)
1803 ifi->ifi_rainfo_trans = NULL;
1806 /* Remove burst RA information */
1807 if (ifi->ifi_rainfo_trans != NULL) {
1808 rm_rainfo(ifi->ifi_rainfo_trans);
1809 ifi->ifi_rainfo_trans = NULL;
1812 if (ifi->ifi_rainfo != NULL) {
1814 * TRANSITIVE -> CONFIGURED
1816 * After initial burst or transition from
1817 * one configuration to another,
1818 * ifi_rainfo always points to the next RA
1821 ifi->ifi_state = IFI_STATE_CONFIGURED;
1823 "<%s> ifname=%s marked as "
1824 "CONFIGURED.", __func__,
1828 * TRANSITIVE -> UNCONFIGURED
1830 * If ifi_rainfo points to NULL, this
1831 * interface is shutting down.
1836 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
1838 "<%s> ifname=%s marked as "
1839 "UNCONFIGURED.", __func__,
1841 error = sock_mc_leave(&sock,
1851 /* process RA timer */
1852 struct rtadvd_timer *
1853 ra_timeout(void *arg)
1857 ifi = (struct ifinfo *)arg;
1858 syslog(LOG_DEBUG, "<%s> RA timer on %s is expired",
1859 __func__, ifi->ifi_ifname);
1863 return (ifi->ifi_ra_timer);
1866 /* update RA timer */
1868 ra_timer_update(void *arg, struct timespec *tm)
1874 ifi = (struct ifinfo *)arg;
1875 rai = ifi->ifi_rainfo;
1878 switch (ifi->ifi_state) {
1879 case IFI_STATE_UNCONFIGURED:
1882 case IFI_STATE_CONFIGURED:
1884 * Whenever a multicast advertisement is sent from
1885 * an interface, the timer is reset to a
1886 * uniformly-distributed random value between the
1887 * interface's configured MinRtrAdvInterval and
1888 * MaxRtrAdvInterval (RFC4861 6.2.4).
1890 interval = rai->rai_mininterval;
1891 #ifdef HAVE_ARC4RANDOM
1892 interval += arc4random_uniform(rai->rai_maxinterval -
1893 rai->rai_mininterval);
1895 interval += random() % (rai->rai_maxinterval -
1896 rai->rai_mininterval);
1899 case IFI_STATE_TRANSITIVE:
1901 * For the first few advertisements (up to
1902 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen
1903 * interval is greater than
1904 * MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer SHOULD be
1905 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead. (RFC
1908 * In such cases, the router SHOULD transmit one or more
1909 * (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final
1910 * multicast Router Advertisements on the interface with a
1911 * Router Lifetime field of zero. (RFC 4861 6.2.5)
1913 interval = ifi->ifi_burstinterval;
1917 tm->tv_sec = interval;
1921 "<%s> RA timer on %s is set to %ld:%ld",
1922 __func__, ifi->ifi_ifname,
1923 (long int)tm->tv_sec, (long int)tm->tv_nsec / 1000);