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);
1234 udiff(uint32_t u, uint32_t v)
1236 return (u >= v ? u - v : v - u);
1239 /* return a non-zero value if the received prefix is inconsitent with ours */
1241 prefix_check(struct nd_opt_prefix_info *pinfo,
1242 struct rainfo *rai, struct sockaddr_in6 *from)
1245 uint32_t preferred_time, valid_time;
1247 int inconsistent = 0;
1248 char ntopbuf[INET6_ADDRSTRLEN];
1249 char prefixbuf[INET6_ADDRSTRLEN];
1250 struct timespec now;
1252 #if 0 /* impossible */
1253 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1256 ifi = rai->rai_ifinfo;
1258 * log if the adveritsed prefix has link-local scope(sanity check?)
1260 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix))
1262 "<%s> link-local prefix %s/%d is advertised "
1265 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1267 pinfo->nd_opt_pi_prefix_len,
1268 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1269 sizeof(ntopbuf)), ifi->ifi_ifname);
1271 if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1272 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1274 "<%s> prefix %s/%d from %s on %s is not in our list",
1276 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1278 pinfo->nd_opt_pi_prefix_len,
1279 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1280 sizeof(ntopbuf)), ifi->ifi_ifname);
1284 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1285 if (pfx->pfx_pltimeexpire) {
1287 * The lifetime is decremented in real time, so we should
1288 * compare the expiration time.
1289 * (RFC 2461 Section 6.2.7.)
1290 * XXX: can we really expect that all routers on the link
1291 * have synchronized clocks?
1293 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1294 preferred_time += now.tv_sec;
1296 if (!pfx->pfx_timer && rai->rai_clockskew &&
1297 udiff(preferred_time, pfx->pfx_pltimeexpire) > rai->rai_clockskew) {
1299 "<%s> preferred lifetime for %s/%d"
1300 " (decr. in real time) inconsistent on %s:"
1301 " %" PRIu32 " from %s, %" PRIu32 " from us",
1303 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1305 pinfo->nd_opt_pi_prefix_len,
1306 ifi->ifi_ifname, preferred_time,
1307 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1308 sizeof(ntopbuf)), pfx->pfx_pltimeexpire);
1311 } else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime)
1313 "<%s> preferred lifetime for %s/%d"
1314 " inconsistent on %s:"
1315 " %d from %s, %d from us",
1317 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1319 pinfo->nd_opt_pi_prefix_len,
1320 ifi->ifi_ifname, preferred_time,
1321 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1322 sizeof(ntopbuf)), pfx->pfx_preflifetime);
1324 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1325 if (pfx->pfx_vltimeexpire) {
1326 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1327 valid_time += now.tv_sec;
1329 if (!pfx->pfx_timer && rai->rai_clockskew &&
1330 udiff(valid_time, pfx->pfx_vltimeexpire) > rai->rai_clockskew) {
1332 "<%s> valid lifetime for %s/%d"
1333 " (decr. in real time) inconsistent on %s:"
1334 " %d from %s, %" PRIu32 " from us",
1336 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1338 pinfo->nd_opt_pi_prefix_len,
1339 ifi->ifi_ifname, preferred_time,
1340 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1341 sizeof(ntopbuf)), pfx->pfx_vltimeexpire);
1344 } else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) {
1346 "<%s> valid lifetime for %s/%d"
1347 " inconsistent on %s:"
1348 " %d from %s, %d from us",
1350 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1352 pinfo->nd_opt_pi_prefix_len,
1353 ifi->ifi_ifname, valid_time,
1354 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1355 sizeof(ntopbuf)), pfx->pfx_validlifetime);
1359 return (inconsistent);
1363 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1366 int bytelen, bitlen;
1369 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1370 if (plen != pfx->pfx_prefixlen)
1375 bitmask = 0xff << (8 - bitlen);
1377 if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen))
1381 ((prefix->s6_addr[bytelen] & bitmask) ==
1382 (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) {
1390 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1392 prefix_match(struct in6_addr *p0, int plen0,
1393 struct in6_addr *p1, int plen1)
1395 int bytelen, bitlen;
1401 bytelen = plen1 / 8;
1403 bitmask = 0xff << (8 - bitlen);
1405 if (memcmp((void *)p0, (void *)p1, bytelen))
1409 ((p0->s6_addr[bytelen] & bitmask) ==
1410 (p1->s6_addr[bytelen] & bitmask))) {
1418 nd6_options(struct nd_opt_hdr *hdr, int limit,
1419 union nd_opt *ndopts, uint32_t optflags)
1423 for (; limit > 0; limit -= optlen) {
1424 if ((size_t)limit < sizeof(struct nd_opt_hdr)) {
1425 syslog(LOG_INFO, "<%s> short option header", __func__);
1429 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1430 if (hdr->nd_opt_len == 0) {
1432 "<%s> bad ND option length(0) (type = %d)",
1433 __func__, hdr->nd_opt_type);
1436 optlen = hdr->nd_opt_len << 3;
1437 if (optlen > limit) {
1438 syslog(LOG_INFO, "<%s> short option", __func__);
1442 if (hdr->nd_opt_type > ND_OPT_MTU &&
1443 hdr->nd_opt_type != ND_OPT_RDNSS &&
1444 hdr->nd_opt_type != ND_OPT_DNSSL) {
1445 syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1446 __func__, hdr->nd_opt_type);
1450 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1451 syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1452 __func__, hdr->nd_opt_type);
1457 * Option length check. Do it here for all fixed-length
1460 switch (hdr->nd_opt_type) {
1462 if (optlen == sizeof(struct nd_opt_mtu))
1467 (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0)
1472 (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0)
1475 case ND_OPT_PREFIX_INFORMATION:
1476 if (optlen == sizeof(struct nd_opt_prefix_info))
1479 syslog(LOG_INFO, "<%s> invalid option length",
1484 switch (hdr->nd_opt_type) {
1485 case ND_OPT_TARGET_LINKADDR:
1486 case ND_OPT_REDIRECTED_HEADER:
1489 break; /* we don't care about these options */
1490 case ND_OPT_SOURCE_LINKADDR:
1492 if (ndopts->opt_array[hdr->nd_opt_type]) {
1494 "<%s> duplicated ND option (type = %d)",
1495 __func__, hdr->nd_opt_type);
1497 ndopts->opt_array[hdr->nd_opt_type] = hdr;
1499 case ND_OPT_PREFIX_INFORMATION:
1501 struct nd_optlist *nol;
1503 if (ndopts->opt_pi == 0) {
1505 (struct nd_opt_prefix_info *)hdr;
1508 nol = malloc(sizeof(*nol));
1510 syslog(LOG_ERR, "<%s> can't allocate memory",
1515 TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next);
1519 default: /* impossible */
1527 free_ndopts(ndopts);
1533 free_ndopts(union nd_opt *ndopts)
1535 struct nd_optlist *nol;
1537 while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) {
1538 TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next);
1544 sock_open(struct sockinfo *s)
1546 struct icmp6_filter filt;
1548 /* XXX: should be max MTU attached to the node */
1549 static char answer[1500];
1551 syslog(LOG_DEBUG, "<%s> enter", __func__);
1554 syslog(LOG_ERR, "<%s> internal error", __func__);
1557 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1558 CMSG_SPACE(sizeof(int));
1559 rcvcmsgbuf = (char *)malloc(rcvcmsgbuflen);
1560 if (rcvcmsgbuf == NULL) {
1561 syslog(LOG_ERR, "<%s> not enough core", __func__);
1565 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1566 CMSG_SPACE(sizeof(int));
1567 sndcmsgbuf = (char *)malloc(sndcmsgbuflen);
1568 if (sndcmsgbuf == NULL) {
1569 syslog(LOG_ERR, "<%s> not enough core", __func__);
1573 if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1574 syslog(LOG_ERR, "<%s> socket: %s", __func__, strerror(errno));
1577 /* specify to tell receiving interface */
1579 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1581 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s", __func__,
1586 /* specify to tell value of hoplimit field of received IP6 hdr */
1587 if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1589 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s", __func__,
1593 ICMP6_FILTER_SETBLOCKALL(&filt);
1594 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1595 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1596 if (mcastif != NULL)
1597 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1599 if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1600 sizeof(filt)) < 0) {
1601 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1602 __func__, strerror(errno));
1606 /* initialize msghdr for receiving packets */
1607 rcviov[0].iov_base = (caddr_t)answer;
1608 rcviov[0].iov_len = sizeof(answer);
1609 rcvmhdr.msg_name = (caddr_t)&rcvfrom;
1610 rcvmhdr.msg_namelen = sizeof(rcvfrom);
1611 rcvmhdr.msg_iov = rcviov;
1612 rcvmhdr.msg_iovlen = 1;
1613 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1614 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1616 /* initialize msghdr for sending packets */
1617 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1618 sndmhdr.msg_iov = sndiov;
1619 sndmhdr.msg_iovlen = 1;
1620 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1621 sndmhdr.msg_controllen = sndcmsgbuflen;
1626 /* open a routing socket to watch the routing table */
1628 rtsock_open(struct sockinfo *s)
1631 syslog(LOG_ERR, "<%s> internal error", __func__);
1634 if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1636 "<%s> socket: %s", __func__, strerror(errno));
1642 if_indextoifinfo(int idx)
1645 char *name, name0[IFNAMSIZ];
1647 /* Check if the interface has a valid name or not. */
1648 if (if_indextoname(idx, name0) == NULL)
1651 TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1652 if (ifi->ifi_ifindex == idx)
1657 syslog(LOG_DEBUG, "<%s> ifi found (idx=%d)",
1660 syslog(LOG_DEBUG, "<%s> ifi not found (idx=%d)",
1663 return (NULL); /* search failed */
1667 ra_output(struct ifinfo *ifi)
1671 struct in6_pktinfo *pi;
1672 struct soliciter *sol;
1675 switch (ifi->ifi_state) {
1676 case IFI_STATE_CONFIGURED:
1677 rai = ifi->ifi_rainfo;
1679 case IFI_STATE_TRANSITIVE:
1680 rai = ifi->ifi_rainfo_trans;
1682 case IFI_STATE_UNCONFIGURED:
1683 syslog(LOG_DEBUG, "<%s> %s is unconfigured. "
1684 "Skip sending RAs.",
1685 __func__, ifi->ifi_ifname);
1691 syslog(LOG_DEBUG, "<%s> rainfo is NULL on %s."
1692 "Skip sending RAs.",
1693 __func__, ifi->ifi_ifname);
1696 if (!(ifi->ifi_flags & IFF_UP)) {
1697 syslog(LOG_DEBUG, "<%s> %s is not up. "
1698 "Skip sending RAs.",
1699 __func__, ifi->ifi_ifname);
1703 * Check lifetime, ACCEPT_RTADV flag, and ip6.forwarding.
1705 * (lifetime == 0) = output
1706 * (lifetime != 0 && (check_accept_rtadv()) = no output
1708 * Basically, hosts MUST NOT send Router Advertisement
1709 * messages at any time (RFC 4861, Section 6.2.3). However, it
1710 * would sometimes be useful to allow hosts to advertise some
1711 * parameters such as prefix information and link MTU. Thus,
1712 * we allow hosts to invoke rtadvd only when router lifetime
1713 * (on every advertising interface) is explicitly set
1714 * zero. (see also the above section)
1717 "<%s> check lifetime=%d, ACCEPT_RTADV=%d, ip6.forwarding=%d "
1720 check_accept_rtadv(ifi->ifi_ifindex),
1721 getinet6sysctl(IPV6CTL_FORWARDING),
1724 if (rai->rai_lifetime != 0) {
1725 if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) {
1727 "non-zero lifetime RA "
1728 "but net.inet6.ip6.forwarding=0. "
1732 if (check_accept_rtadv(ifi->ifi_ifindex)) {
1734 "non-zero lifetime RA "
1735 "on RA receiving interface %s."
1736 " Ignored.", ifi->ifi_ifname);
1741 make_packet(rai); /* XXX: inefficient */
1743 sndmhdr.msg_name = (caddr_t)&sin6_linklocal_allnodes;
1744 sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data;
1745 sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen;
1747 cm = CMSG_FIRSTHDR(&sndmhdr);
1748 /* specify the outgoing interface */
1749 cm->cmsg_level = IPPROTO_IPV6;
1750 cm->cmsg_type = IPV6_PKTINFO;
1751 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1752 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1753 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1754 pi->ipi6_ifindex = ifi->ifi_ifindex;
1756 /* specify the hop limit of the packet */
1760 cm = CMSG_NXTHDR(&sndmhdr, cm);
1761 cm->cmsg_level = IPPROTO_IPV6;
1762 cm->cmsg_type = IPV6_HOPLIMIT;
1763 cm->cmsg_len = CMSG_LEN(sizeof(int));
1764 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1768 "<%s> send RA on %s, # of RS waitings = %d",
1769 __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount);
1771 i = sendmsg(sock.si_fd, &sndmhdr, 0);
1773 if (i < 0 || (size_t)i != rai->rai_ra_datalen) {
1775 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1776 __func__, ifi->ifi_ifname,
1782 * unicast advertisements
1783 * XXX commented out. reason: though spec does not forbit it, unicast
1784 * advert does not really help
1786 while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
1787 TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
1791 /* update timestamp */
1792 clock_gettime(CLOCK_MONOTONIC_FAST, &ifi->ifi_ra_lastsent);
1794 /* update counter */
1795 ifi->ifi_rs_waitcount = 0;
1796 ifi->ifi_raoutput++;
1798 switch (ifi->ifi_state) {
1799 case IFI_STATE_CONFIGURED:
1800 if (ifi->ifi_burstcount > 0)
1801 ifi->ifi_burstcount--;
1803 case IFI_STATE_TRANSITIVE:
1804 ifi->ifi_burstcount--;
1805 if (ifi->ifi_burstcount == 0) {
1806 if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1807 /* Initial burst finished. */
1808 if (ifi->ifi_rainfo_trans != NULL)
1809 ifi->ifi_rainfo_trans = NULL;
1812 /* Remove burst RA information */
1813 if (ifi->ifi_rainfo_trans != NULL) {
1814 rm_rainfo(ifi->ifi_rainfo_trans);
1815 ifi->ifi_rainfo_trans = NULL;
1818 if (ifi->ifi_rainfo != NULL) {
1820 * TRANSITIVE -> CONFIGURED
1822 * After initial burst or transition from
1823 * one configuration to another,
1824 * ifi_rainfo always points to the next RA
1827 ifi->ifi_state = IFI_STATE_CONFIGURED;
1829 "<%s> ifname=%s marked as "
1830 "CONFIGURED.", __func__,
1834 * TRANSITIVE -> UNCONFIGURED
1836 * If ifi_rainfo points to NULL, this
1837 * interface is shutting down.
1842 ifi->ifi_state = IFI_STATE_UNCONFIGURED;
1844 "<%s> ifname=%s marked as "
1845 "UNCONFIGURED.", __func__,
1847 error = sock_mc_leave(&sock,
1857 /* process RA timer */
1858 struct rtadvd_timer *
1859 ra_timeout(void *arg)
1863 ifi = (struct ifinfo *)arg;
1864 syslog(LOG_DEBUG, "<%s> RA timer on %s is expired",
1865 __func__, ifi->ifi_ifname);
1869 return (ifi->ifi_ra_timer);
1872 /* update RA timer */
1874 ra_timer_update(void *arg, struct timespec *tm)
1880 ifi = (struct ifinfo *)arg;
1881 rai = ifi->ifi_rainfo;
1884 switch (ifi->ifi_state) {
1885 case IFI_STATE_UNCONFIGURED:
1888 case IFI_STATE_CONFIGURED:
1890 * Whenever a multicast advertisement is sent from
1891 * an interface, the timer is reset to a
1892 * uniformly-distributed random value between the
1893 * interface's configured MinRtrAdvInterval and
1894 * MaxRtrAdvInterval (RFC4861 6.2.4).
1896 interval = rai->rai_mininterval;
1897 #ifdef HAVE_ARC4RANDOM
1898 interval += arc4random_uniform(rai->rai_maxinterval -
1899 rai->rai_mininterval);
1901 interval += random() % (rai->rai_maxinterval -
1902 rai->rai_mininterval);
1905 case IFI_STATE_TRANSITIVE:
1907 * For the first few advertisements (up to
1908 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen
1909 * interval is greater than
1910 * MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer SHOULD be
1911 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead. (RFC
1914 * In such cases, the router SHOULD transmit one or more
1915 * (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final
1916 * multicast Router Advertisements on the interface with a
1917 * Router Lifetime field of zero. (RFC 4861 6.2.5)
1919 interval = ifi->ifi_burstinterval;
1923 tm->tv_sec = interval;
1927 "<%s> RA timer on %s is set to %ld:%ld",
1928 __func__, ifi->ifi_ifname,
1929 (long int)tm->tv_sec, (long int)tm->tv_nsec / 1000);