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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/socket.h>
37 #include <sys/queue.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/ip6.h>
44 #include <netinet6/ip6_var.h>
45 #include <netinet/icmp6.h>
47 #include <arpa/inet.h>
69 struct msghdr rcvmhdr;
70 static u_char *rcvcmsgbuf;
71 static size_t rcvcmsgbuflen;
72 static u_char *sndcmsgbuf = NULL;
73 static size_t sndcmsgbuflen;
74 volatile sig_atomic_t do_dump;
75 volatile sig_atomic_t do_die;
76 struct msghdr sndmhdr;
77 struct iovec rcviov[2];
78 struct iovec sndiov[2];
79 struct sockaddr_in6 rcvfrom;
80 struct sockaddr_in6 sin6_allnodes = {sizeof(sin6_allnodes), AF_INET6};
81 struct in6_addr in6a_site_allrouters;
82 static char *dumpfilename = "/var/run/rtadvd.dump"; /* XXX: should be configurable */
83 static char *pidfilename = "/var/run/rtadvd.pid"; /* should be configurable */
88 int dflag = 0, sflag = 0;
90 u_char *conffile = NULL;
92 struct rainfo *ralist = NULL;
94 struct nd_optlist *next;
95 struct nd_opt_hdr *opt;
98 struct nd_opt_hdr *nd_opt_array[9];
100 struct nd_opt_hdr *zero;
101 struct nd_opt_hdr *src_lladdr;
102 struct nd_opt_hdr *tgt_lladdr;
103 struct nd_opt_prefix_info *pi;
104 struct nd_opt_rd_hdr *rh;
105 struct nd_opt_mtu *mtu;
106 struct nd_optlist *list;
109 #define nd_opts_src_lladdr nd_opt_each.src_lladdr
110 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr
111 #define nd_opts_pi nd_opt_each.pi
112 #define nd_opts_rh nd_opt_each.rh
113 #define nd_opts_mtu nd_opt_each.mtu
114 #define nd_opts_list nd_opt_each.list
116 #define NDOPT_FLAG_SRCLINKADDR 0x1
117 #define NDOPT_FLAG_TGTLINKADDR 0x2
118 #define NDOPT_FLAG_PREFIXINFO 0x4
119 #define NDOPT_FLAG_RDHDR 0x8
120 #define NDOPT_FLAG_MTU 0x10
122 u_int32_t ndopt_flags[] = {
123 0, NDOPT_FLAG_SRCLINKADDR, NDOPT_FLAG_TGTLINKADDR,
124 NDOPT_FLAG_PREFIXINFO, NDOPT_FLAG_RDHDR, NDOPT_FLAG_MTU,
127 int main(int, char *[]);
128 static void set_die(int);
129 static void die(void);
130 static void sock_open(void);
131 static void rtsock_open(void);
132 static void rtadvd_input(void);
133 static void rs_input(int, struct nd_router_solicit *,
134 struct in6_pktinfo *, struct sockaddr_in6 *);
135 static void ra_input(int, struct nd_router_advert *,
136 struct in6_pktinfo *, struct sockaddr_in6 *);
137 static int prefix_check(struct nd_opt_prefix_info *, struct rainfo *,
138 struct sockaddr_in6 *);
139 static int nd6_options(struct nd_opt_hdr *, int,
140 union nd_opts *, u_int32_t);
141 static void free_ndopts(union nd_opts *);
142 static void ra_output(struct rainfo *);
143 static void rtmsg_input(void);
144 static void rtadvd_set_dump_file(int);
145 static void set_short_delay(struct rainfo *);
153 struct pollfd set[2];
155 fd_set *fdsetp, *selectfdp;
159 struct timeval *timeout;
161 int fflag = 0, logopt;
165 /* get command line options and arguments */
166 while ((ch = getopt(argc, argv, "c:dDfM:Rs")) != -1) {
184 fprintf(stderr, "rtadvd: "
185 "the -R option is currently ignored.\n");
198 "usage: rtadvd [-dDfMRs] [-c conffile] "
203 logopt = LOG_NDELAY | LOG_PID;
205 logopt |= LOG_PERROR;
206 openlog("rtadvd", logopt, LOG_DAEMON);
210 (void)setlogmask(LOG_UPTO(LOG_ERR));
212 (void)setlogmask(LOG_UPTO(LOG_INFO));
214 /* timer initialization */
217 #ifndef HAVE_ARC4RANDOM
218 /* random value initialization */
222 srandom((u_long)time(NULL));
226 /* get iflist block from kernel */
232 if (inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr) != 1) {
233 fprintf(stderr, "fatal: inet_pton failed\n");
242 /* record the current PID */
244 if ((pidfp = fopen(pidfilename, "w")) == NULL) {
246 "<%s> failed to open the pid log file, run anyway.",
249 fprintf(pidfp, "%d\n", pid);
255 set[0].events = POLLIN;
259 set[1].events = POLLIN;
271 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
272 if ((fdsetp = malloc(fdmasks)) == NULL) {
276 if ((selectfdp = malloc(fdmasks)) == NULL) {
280 memset(fdsetp, 0, fdmasks);
281 FD_SET(sock, fdsetp);
283 FD_SET(rtsock, fdsetp);
286 signal(SIGTERM, set_die);
287 signal(SIGUSR1, rtadvd_set_dump_file);
291 memcpy(selectfdp, fdsetp, fdmasks); /* reinitialize */
294 if (do_dump) { /* SIGUSR1 */
296 rtadvd_dump_file(dumpfilename);
304 /* timer expiration check and reset the timer */
305 timeout = rtadvd_check_timer();
307 if (timeout != NULL) {
309 "<%s> set timer to %ld:%ld. waiting for "
310 "inputs or timeout", __func__,
311 (long int)timeout->tv_sec,
312 (long int)timeout->tv_usec);
315 "<%s> there's no timer. waiting for inputs",
320 if ((i = poll(set, 2, timeout ? (timeout->tv_sec * 1000 +
321 timeout->tv_usec / 1000) : INFTIM)) < 0)
323 if ((i = select(maxfd + 1, selectfdp, NULL, NULL,
327 /* EINTR would occur upon SIGUSR1 for status dump */
329 syslog(LOG_ERR, "<%s> select: %s",
330 __func__, strerror(errno));
333 if (i == 0) /* timeout */
336 if (rtsock != -1 && set[1].revents & POLLIN)
338 if (rtsock != -1 && FD_ISSET(rtsock, selectfdp))
342 if (set[0].revents & POLLIN)
344 if (FD_ISSET(sock, selectfdp))
348 exit(0); /* NOTREACHED */
352 rtadvd_set_dump_file(sig)
370 const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
373 syslog(LOG_DEBUG, "<%s> cease to be an advertising router\n",
377 for (ra = ralist; ra; ra = ra->next) {
381 for (i = 0; i < retrans; i++) {
382 for (ra = ralist; ra; ra = ra->next)
384 sleep(MIN_DELAY_BETWEEN_RAS);
393 int n, type, ifindex = 0, plen;
395 char msg[2048], *next, *lim;
396 u_char ifname[IF_NAMESIZE];
397 struct prefix *prefix;
399 struct in6_addr *addr;
400 char addrbuf[INET6_ADDRSTRLEN];
401 int prefixchange = 0;
403 n = read(rtsock, msg, sizeof(msg));
405 syslog(LOG_DEBUG, "<%s> received a routing message "
406 "(type = %d, len = %d)", __func__, rtmsg_type(msg), n);
408 if (n > rtmsg_len(msg)) {
410 * This usually won't happen for messages received on
415 "<%s> received data length is larger than "
416 "1st routing message len. multiple messages? "
417 "read %d bytes, but 1st msg len = %d",
418 __func__, n, rtmsg_len(msg));
426 for (next = msg; next < lim; next += len) {
429 next = get_next_msg(next, lim, 0, &len,
430 RTADV_TYPE2BITMASK(RTM_ADD) |
431 RTADV_TYPE2BITMASK(RTM_DELETE) |
432 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
433 RTADV_TYPE2BITMASK(RTM_DELADDR) |
434 RTADV_TYPE2BITMASK(RTM_IFINFO));
437 type = rtmsg_type(next);
441 ifindex = get_rtm_ifindex(next);
445 ifindex = get_ifam_ifindex(next);
448 ifindex = get_ifm_ifindex(next);
451 /* should not reach here */
454 "<%s:%d> unknown rtmsg %d on %s",
455 __func__, __LINE__, type,
456 if_indextoname(ifindex, ifname));
461 if ((rai = if_indextorainfo(ifindex)) == NULL) {
464 "<%s> route changed on "
465 "non advertising interface(%s)",
467 if_indextoname(ifindex, ifname));
471 oldifflags = iflist[ifindex]->ifm_flags;
475 /* init ifflags because it may have changed */
476 iflist[ifindex]->ifm_flags =
477 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
480 break; /* we aren't interested in prefixes */
482 addr = get_addr(msg);
483 plen = get_prefixlen(msg);
484 /* sanity check for plen */
485 /* as RFC2373, prefixlen is at least 4 */
486 if (plen < 4 || plen > 127) {
487 syslog(LOG_INFO, "<%s> new interface route's"
488 "plen %d is invalid for a prefix",
492 prefix = find_prefix(rai, addr, plen);
496 * If the prefix has been invalidated,
497 * make it available again.
499 update_prefix(prefix);
501 } else if (dflag > 1) {
503 "<%s> new prefix(%s/%d) "
505 "but it was already in list",
507 inet_ntop(AF_INET6, addr,
508 (char *)addrbuf, INET6_ADDRSTRLEN),
513 make_prefix(rai, ifindex, addr, plen);
517 /* init ifflags because it may have changed */
518 iflist[ifindex]->ifm_flags =
519 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
524 addr = get_addr(msg);
525 plen = get_prefixlen(msg);
526 /* sanity check for plen */
527 /* as RFC2373, prefixlen is at least 4 */
528 if (plen < 4 || plen > 127) {
530 "<%s> deleted interface route's "
531 "plen %d is invalid for a prefix",
535 prefix = find_prefix(rai, addr, plen);
536 if (prefix == NULL) {
539 "<%s> prefix(%s/%d) was "
541 "but it was not in list",
543 inet_ntop(AF_INET6, addr,
544 (char *)addrbuf, INET6_ADDRSTRLEN),
549 invalidate_prefix(prefix);
554 /* init ifflags because it may have changed */
555 iflist[ifindex]->ifm_flags =
556 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
559 iflist[ifindex]->ifm_flags = get_ifm_flags(next);
562 /* should not reach here */
565 "<%s:%d> unknown rtmsg %d on %s",
566 __func__, __LINE__, type,
567 if_indextoname(ifindex, ifname));
572 /* check if an interface flag is changed */
573 if ((oldifflags & IFF_UP) && /* UP to DOWN */
574 !(iflist[ifindex]->ifm_flags & IFF_UP)) {
576 "<%s> interface %s becomes down. stop timer.",
577 __func__, rai->ifname);
578 rtadvd_remove_timer(&rai->timer);
579 } else if (!(oldifflags & IFF_UP) && /* DOWN to UP */
580 (iflist[ifindex]->ifm_flags & IFF_UP)) {
582 "<%s> interface %s becomes up. restart timer.",
583 __func__, rai->ifname);
585 rai->initcounter = 0; /* reset the counter */
586 rai->waiting = 0; /* XXX */
587 rai->timer = rtadvd_add_timer(ra_timeout,
588 ra_timer_update, rai, rai);
589 ra_timer_update((void *)rai, &rai->timer->tm);
590 rtadvd_set_timer(&rai->timer->tm, rai->timer);
591 } else if (prefixchange &&
592 (iflist[ifindex]->ifm_flags & IFF_UP)) {
594 * An advertised prefix has been added or invalidated.
595 * Will notice the change in a short delay.
597 rai->initcounter = 0;
598 set_short_delay(rai);
613 struct icmp6_hdr *icp;
616 struct in6_pktinfo *pi = NULL;
617 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
618 struct in6_addr dst = in6addr_any;
621 * Get message. We reset msg_controllen since the field could
622 * be modified if we had received a message before setting
625 rcvmhdr.msg_controllen = rcvcmsgbuflen;
626 if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
629 /* extract optional information via Advanced API */
630 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
632 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
633 if (cm->cmsg_level == IPPROTO_IPV6 &&
634 cm->cmsg_type == IPV6_PKTINFO &&
635 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
636 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
637 ifindex = pi->ipi6_ifindex;
640 if (cm->cmsg_level == IPPROTO_IPV6 &&
641 cm->cmsg_type == IPV6_HOPLIMIT &&
642 cm->cmsg_len == CMSG_LEN(sizeof(int)))
643 hlimp = (int *)CMSG_DATA(cm);
647 "<%s> failed to get receiving interface",
653 "<%s> failed to get receiving hop limit",
659 * If we happen to receive data on an interface which is now down,
660 * just discard the data.
662 if ((iflist[pi->ipi6_ifindex]->ifm_flags & IFF_UP) == 0) {
664 "<%s> received data on a disabled interface (%s)",
666 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
671 if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
673 "<%s> packet size(%d) is too short",
678 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
679 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
681 if (i < sizeof(struct icmp6_hdr)) {
683 "<%s> packet size(%d) is too short",
688 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
691 switch (icp->icmp6_type) {
692 case ND_ROUTER_SOLICIT:
694 * Message verification - RFC-2461 6.1.1
695 * XXX: these checks must be done in the kernel as well,
696 * but we can't completely rely on them.
700 "<%s> RS with invalid hop limit(%d) "
701 "received from %s on %s",
703 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
705 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
708 if (icp->icmp6_code) {
710 "<%s> RS with invalid ICMP6 code(%d) "
711 "received from %s on %s",
712 __func__, icp->icmp6_code,
713 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
715 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
718 if (i < sizeof(struct nd_router_solicit)) {
720 "<%s> RS from %s on %s does not have enough "
723 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
725 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
728 rs_input(i, (struct nd_router_solicit *)icp, pi, &rcvfrom);
730 case ND_ROUTER_ADVERT:
732 * Message verification - RFC-2461 6.1.2
733 * XXX: there's a same dilemma as above...
737 "<%s> RA with invalid hop limit(%d) "
738 "received from %s on %s",
740 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
742 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
745 if (icp->icmp6_code) {
747 "<%s> RA with invalid ICMP6 code(%d) "
748 "received from %s on %s",
749 __func__, icp->icmp6_code,
750 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
752 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
755 if (i < sizeof(struct nd_router_advert)) {
757 "<%s> RA from %s on %s does not have enough "
760 inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
762 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
765 ra_input(i, (struct nd_router_advert *)icp, pi, &rcvfrom);
767 case ICMP6_ROUTER_RENUMBERING:
768 if (accept_rr == 0) {
769 syslog(LOG_ERR, "<%s> received a router renumbering "
770 "message, but not allowed to be accepted",
774 rr_input(i, (struct icmp6_router_renum *)icp, pi, &rcvfrom,
779 * Note that this case is POSSIBLE, especially just
780 * after invocation of the daemon. This is because we
781 * could receive message after opening the socket and
782 * before setting ICMP6 type filter(see sock_open()).
784 syslog(LOG_ERR, "<%s> invalid icmp type(%d)",
785 __func__, icp->icmp6_type);
793 rs_input(int len, struct nd_router_solicit *rs,
794 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
796 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
797 union nd_opts ndopts;
799 struct soliciter *sol;
802 "<%s> RS received from %s on %s",
804 inet_ntop(AF_INET6, &from->sin6_addr,
805 ntopbuf, INET6_ADDRSTRLEN),
806 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
808 /* ND option check */
809 memset(&ndopts, 0, sizeof(ndopts));
810 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
811 len - sizeof(struct nd_router_solicit),
812 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
814 "<%s> ND option check failed for an RS from %s on %s",
816 inet_ntop(AF_INET6, &from->sin6_addr,
817 ntopbuf, INET6_ADDRSTRLEN),
818 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
823 * If the IP source address is the unspecified address, there
824 * must be no source link-layer address option in the message.
827 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
828 ndopts.nd_opts_src_lladdr) {
830 "<%s> RS from unspecified src on %s has a link-layer"
833 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
839 if (pi->ipi6_ifindex == ra->ifindex)
845 "<%s> RS received on non advertising interface(%s)",
847 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
851 ra->rsinput++; /* increment statistics */
854 * Decide whether to send RA according to the rate-limit
858 /* record sockaddr waiting for RA, if possible */
859 sol = (struct soliciter *)malloc(sizeof(*sol));
862 /* XXX RFC2553 need clarification on flowinfo */
863 sol->addr.sin6_flowinfo = 0;
864 sol->next = ra->soliciter;
869 * If there is already a waiting RS packet, don't
878 free_ndopts(&ndopts);
886 long delay; /* must not be greater than 1000000 */
887 struct timeval interval, now, min_delay, tm_tmp, *rest;
890 * Compute a random delay. If the computed value
891 * corresponds to a time later than the time the next
892 * multicast RA is scheduled to be sent, ignore the random
893 * delay and send the advertisement at the
894 * already-scheduled time. RFC-2461 6.2.6
896 #ifdef HAVE_ARC4RANDOM
897 delay = arc4random_uniform(MAX_RA_DELAY_TIME);
899 delay = random() % MAX_RA_DELAY_TIME;
902 interval.tv_usec = delay;
903 rest = rtadvd_timer_rest(rai->timer);
904 if (TIMEVAL_LT(*rest, interval)) {
905 syslog(LOG_DEBUG, "<%s> random delay is larger than "
906 "the rest of the current timer", __func__);
911 * If we sent a multicast Router Advertisement within
912 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
913 * the advertisement to be sent at a time corresponding to
914 * MIN_DELAY_BETWEEN_RAS plus the random value after the
915 * previous advertisement was sent.
917 gettimeofday(&now, NULL);
918 TIMEVAL_SUB(&now, &rai->lastsent, &tm_tmp);
919 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
920 min_delay.tv_usec = 0;
921 if (TIMEVAL_LT(tm_tmp, min_delay)) {
922 TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
923 TIMEVAL_ADD(&min_delay, &interval, &interval);
925 rtadvd_set_timer(&interval, rai->timer);
929 ra_input(int len, struct nd_router_advert *ra,
930 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
933 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
934 union nd_opts ndopts;
935 char *on_off[] = {"OFF", "ON"};
936 u_int32_t reachabletime, retranstimer, mtu;
937 int inconsistent = 0;
940 "<%s> RA received from %s on %s",
942 inet_ntop(AF_INET6, &from->sin6_addr,
943 ntopbuf, INET6_ADDRSTRLEN),
944 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
946 /* ND option check */
947 memset(&ndopts, 0, sizeof(ndopts));
948 if (nd6_options((struct nd_opt_hdr *)(ra + 1),
949 len - sizeof(struct nd_router_advert),
950 &ndopts, NDOPT_FLAG_SRCLINKADDR |
951 NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
953 "<%s> ND option check failed for an RA from %s on %s",
955 inet_ntop(AF_INET6, &from->sin6_addr,
956 ntopbuf, INET6_ADDRSTRLEN),
957 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
962 * RA consistency check according to RFC-2461 6.2.7
964 if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == 0) {
966 "<%s> received RA from %s on non-advertising"
969 inet_ntop(AF_INET6, &from->sin6_addr,
970 ntopbuf, INET6_ADDRSTRLEN),
971 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
974 rai->rainput++; /* increment statistics */
976 /* Cur Hop Limit value */
977 if (ra->nd_ra_curhoplimit && rai->hoplimit &&
978 ra->nd_ra_curhoplimit != rai->hoplimit) {
980 "<%s> CurHopLimit inconsistent on %s:"
981 " %d from %s, %d from us",
984 ra->nd_ra_curhoplimit,
985 inet_ntop(AF_INET6, &from->sin6_addr,
986 ntopbuf, INET6_ADDRSTRLEN),
991 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
994 "<%s> M flag inconsistent on %s:"
995 " %s from %s, %s from us",
998 on_off[!rai->managedflg],
999 inet_ntop(AF_INET6, &from->sin6_addr,
1000 ntopbuf, INET6_ADDRSTRLEN),
1001 on_off[rai->managedflg]);
1005 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
1008 "<%s> O flag inconsistent on %s:"
1009 " %s from %s, %s from us",
1012 on_off[!rai->otherflg],
1013 inet_ntop(AF_INET6, &from->sin6_addr,
1014 ntopbuf, INET6_ADDRSTRLEN),
1015 on_off[rai->otherflg]);
1018 /* Reachable Time */
1019 reachabletime = ntohl(ra->nd_ra_reachable);
1020 if (reachabletime && rai->reachabletime &&
1021 reachabletime != rai->reachabletime) {
1023 "<%s> ReachableTime inconsistent on %s:"
1024 " %d from %s, %d from us",
1028 inet_ntop(AF_INET6, &from->sin6_addr,
1029 ntopbuf, INET6_ADDRSTRLEN),
1030 rai->reachabletime);
1034 retranstimer = ntohl(ra->nd_ra_retransmit);
1035 if (retranstimer && rai->retranstimer &&
1036 retranstimer != rai->retranstimer) {
1038 "<%s> RetranceTimer inconsistent on %s:"
1039 " %d from %s, %d from us",
1043 inet_ntop(AF_INET6, &from->sin6_addr,
1044 ntopbuf, INET6_ADDRSTRLEN),
1048 /* Values in the MTU options */
1049 if (ndopts.nd_opts_mtu) {
1050 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
1051 if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
1053 "<%s> MTU option value inconsistent on %s:"
1054 " %d from %s, %d from us",
1057 inet_ntop(AF_INET6, &from->sin6_addr,
1058 ntopbuf, INET6_ADDRSTRLEN),
1063 /* Preferred and Valid Lifetimes for prefixes */
1065 struct nd_optlist *optp = ndopts.nd_opts_list;
1067 if (ndopts.nd_opts_pi) {
1068 if (prefix_check(ndopts.nd_opts_pi, rai, from))
1072 if (prefix_check((struct nd_opt_prefix_info *)optp->opt,
1080 rai->rainconsistent++;
1083 free_ndopts(&ndopts);
1087 /* return a non-zero value if the received prefix is inconsitent with ours */
1089 prefix_check(struct nd_opt_prefix_info *pinfo,
1090 struct rainfo *rai, struct sockaddr_in6 *from)
1092 u_int32_t preferred_time, valid_time;
1094 int inconsistent = 0;
1095 u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
1098 #if 0 /* impossible */
1099 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1104 * log if the adveritsed prefix has link-local scope(sanity check?)
1106 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
1108 "<%s> link-local prefix %s/%d is advertised "
1111 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1112 prefixbuf, INET6_ADDRSTRLEN),
1113 pinfo->nd_opt_pi_prefix_len,
1114 inet_ntop(AF_INET6, &from->sin6_addr,
1115 ntopbuf, INET6_ADDRSTRLEN),
1119 if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1120 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1122 "<%s> prefix %s/%d from %s on %s is not in our list",
1124 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1125 prefixbuf, INET6_ADDRSTRLEN),
1126 pinfo->nd_opt_pi_prefix_len,
1127 inet_ntop(AF_INET6, &from->sin6_addr,
1128 ntopbuf, INET6_ADDRSTRLEN),
1133 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1134 if (pp->pltimeexpire) {
1136 * The lifetime is decremented in real time, so we should
1137 * compare the expiration time.
1138 * (RFC 2461 Section 6.2.7.)
1139 * XXX: can we really expect that all routers on the link
1140 * have synchronized clocks?
1142 gettimeofday(&now, NULL);
1143 preferred_time += now.tv_sec;
1145 if (!pp->timer && rai->clockskew &&
1146 abs(preferred_time - pp->pltimeexpire) > rai->clockskew) {
1148 "<%s> preferred lifetime for %s/%d"
1149 " (decr. in real time) inconsistent on %s:"
1150 " %d from %s, %ld from us",
1152 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1153 prefixbuf, INET6_ADDRSTRLEN),
1154 pinfo->nd_opt_pi_prefix_len,
1155 rai->ifname, preferred_time,
1156 inet_ntop(AF_INET6, &from->sin6_addr,
1157 ntopbuf, INET6_ADDRSTRLEN),
1161 } else if (!pp->timer && preferred_time != pp->preflifetime) {
1163 "<%s> preferred lifetime for %s/%d"
1164 " inconsistent on %s:"
1165 " %d from %s, %d from us",
1167 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1168 prefixbuf, INET6_ADDRSTRLEN),
1169 pinfo->nd_opt_pi_prefix_len,
1170 rai->ifname, preferred_time,
1171 inet_ntop(AF_INET6, &from->sin6_addr,
1172 ntopbuf, INET6_ADDRSTRLEN),
1176 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1177 if (pp->vltimeexpire) {
1178 gettimeofday(&now, NULL);
1179 valid_time += now.tv_sec;
1181 if (!pp->timer && rai->clockskew &&
1182 abs(valid_time - pp->vltimeexpire) > rai->clockskew) {
1184 "<%s> valid lifetime for %s/%d"
1185 " (decr. in real time) inconsistent on %s:"
1186 " %d from %s, %ld from us",
1188 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1189 prefixbuf, INET6_ADDRSTRLEN),
1190 pinfo->nd_opt_pi_prefix_len,
1191 rai->ifname, preferred_time,
1192 inet_ntop(AF_INET6, &from->sin6_addr,
1193 ntopbuf, INET6_ADDRSTRLEN),
1197 } else if (!pp->timer && valid_time != pp->validlifetime) {
1199 "<%s> valid lifetime for %s/%d"
1200 " inconsistent on %s:"
1201 " %d from %s, %d from us",
1203 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1204 prefixbuf, INET6_ADDRSTRLEN),
1205 pinfo->nd_opt_pi_prefix_len,
1206 rai->ifname, valid_time,
1207 inet_ntop(AF_INET6, &from->sin6_addr,
1208 ntopbuf, INET6_ADDRSTRLEN),
1213 return(inconsistent);
1217 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1220 int bytelen, bitlen;
1223 for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
1224 if (plen != pp->prefixlen)
1228 bitmask = 0xff << (8 - bitlen);
1229 if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
1232 ((prefix->s6_addr[bytelen] & bitmask) ==
1233 (pp->prefix.s6_addr[bytelen] & bitmask))) {
1241 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1243 prefix_match(struct in6_addr *p0, int plen0,
1244 struct in6_addr *p1, int plen1)
1246 int bytelen, bitlen;
1251 bytelen = plen1 / 8;
1253 bitmask = 0xff << (8 - bitlen);
1254 if (memcmp((void *)p0, (void *)p1, bytelen))
1257 ((p0->s6_addr[bytelen] & bitmask) ==
1258 (p1->s6_addr[bytelen] & bitmask))) {
1266 nd6_options(struct nd_opt_hdr *hdr, int limit,
1267 union nd_opts *ndopts, u_int32_t optflags)
1271 for (; limit > 0; limit -= optlen) {
1272 if (limit < sizeof(struct nd_opt_hdr)) {
1273 syslog(LOG_INFO, "<%s> short option header", __func__);
1277 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1278 if (hdr->nd_opt_len == 0) {
1280 "<%s> bad ND option length(0) (type = %d)",
1281 __func__, hdr->nd_opt_type);
1284 optlen = hdr->nd_opt_len << 3;
1285 if (optlen > limit) {
1286 syslog(LOG_INFO, "<%s> short option", __func__);
1290 if (hdr->nd_opt_type > ND_OPT_MTU) {
1291 syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1292 __func__, hdr->nd_opt_type);
1296 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1297 syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1298 __func__, hdr->nd_opt_type);
1303 * Option length check. Do it here for all fixed-length
1306 if ((hdr->nd_opt_type == ND_OPT_MTU &&
1307 (optlen != sizeof(struct nd_opt_mtu))) ||
1308 ((hdr->nd_opt_type == ND_OPT_PREFIX_INFORMATION &&
1309 optlen != sizeof(struct nd_opt_prefix_info)))) {
1310 syslog(LOG_INFO, "<%s> invalid option length",
1315 switch (hdr->nd_opt_type) {
1316 case ND_OPT_TARGET_LINKADDR:
1317 case ND_OPT_REDIRECTED_HEADER:
1318 break; /* we don't care about these options */
1319 case ND_OPT_SOURCE_LINKADDR:
1321 if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
1323 "<%s> duplicated ND option (type = %d)",
1324 __func__, hdr->nd_opt_type);
1326 ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
1328 case ND_OPT_PREFIX_INFORMATION:
1330 struct nd_optlist *pfxlist;
1332 if (ndopts->nd_opts_pi == 0) {
1333 ndopts->nd_opts_pi =
1334 (struct nd_opt_prefix_info *)hdr;
1337 if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
1338 syslog(LOG_ERR, "<%s> can't allocate memory",
1342 pfxlist->next = ndopts->nd_opts_list;
1344 ndopts->nd_opts_list = pfxlist;
1348 default: /* impossible */
1356 free_ndopts(ndopts);
1362 free_ndopts(union nd_opts *ndopts)
1364 struct nd_optlist *opt = ndopts->nd_opts_list, *next;
1376 struct icmp6_filter filt;
1377 struct ipv6_mreq mreq;
1378 struct rainfo *ra = ralist;
1380 /* XXX: should be max MTU attached to the node */
1381 static u_char answer[1500];
1383 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1384 CMSG_SPACE(sizeof(int));
1385 rcvcmsgbuf = (u_char *)malloc(rcvcmsgbuflen);
1386 if (rcvcmsgbuf == NULL) {
1387 syslog(LOG_ERR, "<%s> not enough core", __func__);
1391 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1392 CMSG_SPACE(sizeof(int));
1393 sndcmsgbuf = (u_char *)malloc(sndcmsgbuflen);
1394 if (sndcmsgbuf == NULL) {
1395 syslog(LOG_ERR, "<%s> not enough core", __func__);
1399 if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1400 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1405 /* specify to tell receiving interface */
1407 #ifdef IPV6_RECVPKTINFO
1408 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1410 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s",
1411 __func__, strerror(errno));
1414 #else /* old adv. API */
1415 if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
1417 syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
1418 __func__, strerror(errno));
1424 /* specify to tell value of hoplimit field of received IP6 hdr */
1425 #ifdef IPV6_RECVHOPLIMIT
1426 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1428 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s",
1429 __func__, strerror(errno));
1432 #else /* old adv. API */
1433 if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
1435 syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
1436 __func__, strerror(errno));
1441 ICMP6_FILTER_SETBLOCKALL(&filt);
1442 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1443 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1445 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1446 if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1447 sizeof(filt)) < 0) {
1448 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1449 __func__, strerror(errno));
1454 * join all routers multicast address on each advertising interface.
1456 if (inet_pton(AF_INET6, ALLROUTERS_LINK,
1457 &mreq.ipv6mr_multiaddr.s6_addr)
1459 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1464 mreq.ipv6mr_interface = ra->ifindex;
1465 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
1466 sizeof(mreq)) < 0) {
1467 syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
1468 __func__, ra->ifname, strerror(errno));
1475 * When attending router renumbering, join all-routers site-local
1479 if (inet_pton(AF_INET6, ALLROUTERS_SITE,
1480 &in6a_site_allrouters) != 1) {
1481 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1485 mreq.ipv6mr_multiaddr = in6a_site_allrouters;
1487 if ((mreq.ipv6mr_interface = if_nametoindex(mcastif))
1490 "<%s> invalid interface: %s",
1495 mreq.ipv6mr_interface = ralist->ifindex;
1496 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1497 &mreq, sizeof(mreq)) < 0) {
1499 "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
1501 mcastif ? mcastif : ralist->ifname,
1507 /* initialize msghdr for receiving packets */
1508 rcviov[0].iov_base = (caddr_t)answer;
1509 rcviov[0].iov_len = sizeof(answer);
1510 rcvmhdr.msg_name = (caddr_t)&rcvfrom;
1511 rcvmhdr.msg_namelen = sizeof(rcvfrom);
1512 rcvmhdr.msg_iov = rcviov;
1513 rcvmhdr.msg_iovlen = 1;
1514 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1515 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1517 /* initialize msghdr for sending packets */
1518 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1519 sndmhdr.msg_iov = sndiov;
1520 sndmhdr.msg_iovlen = 1;
1521 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1522 sndmhdr.msg_controllen = sndcmsgbuflen;
1527 /* open a routing socket to watch the routing table */
1531 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1533 "<%s> socket: %s", __func__, strerror(errno));
1539 if_indextorainfo(int idx)
1541 struct rainfo *rai = ralist;
1543 for (rai = ralist; rai; rai = rai->next) {
1544 if (rai->ifindex == idx)
1548 return(NULL); /* search failed */
1553 struct rainfo *rainfo;
1557 struct in6_pktinfo *pi;
1558 struct soliciter *sol, *nextsol;
1560 if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) {
1561 syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
1562 __func__, rainfo->ifname);
1566 make_packet(rainfo); /* XXX: inefficient */
1568 sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
1569 sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
1570 sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
1572 cm = CMSG_FIRSTHDR(&sndmhdr);
1573 /* specify the outgoing interface */
1574 cm->cmsg_level = IPPROTO_IPV6;
1575 cm->cmsg_type = IPV6_PKTINFO;
1576 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1577 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1578 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1579 pi->ipi6_ifindex = rainfo->ifindex;
1581 /* specify the hop limit of the packet */
1585 cm = CMSG_NXTHDR(&sndmhdr, cm);
1586 cm->cmsg_level = IPPROTO_IPV6;
1587 cm->cmsg_type = IPV6_HOPLIMIT;
1588 cm->cmsg_len = CMSG_LEN(sizeof(int));
1589 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1593 "<%s> send RA on %s, # of waitings = %d",
1594 __func__, rainfo->ifname, rainfo->waiting);
1596 i = sendmsg(sock, &sndmhdr, 0);
1598 if (i < 0 || i != rainfo->ra_datalen) {
1600 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1601 __func__, rainfo->ifname,
1605 /* update counter */
1606 if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
1607 rainfo->initcounter++;
1611 * unicast advertisements
1612 * XXX commented out. reason: though spec does not forbit it, unicast
1613 * advert does not really help
1615 for (sol = rainfo->soliciter; sol; sol = nextsol) {
1616 nextsol = sol->next;
1621 rainfo->soliciter = NULL;
1623 /* update timestamp */
1624 gettimeofday(&rainfo->lastsent, NULL);
1626 /* reset waiting conter */
1627 rainfo->waiting = 0;
1630 /* process RA timer */
1631 struct rtadvd_timer *
1632 ra_timeout(void *data)
1634 struct rainfo *rai = (struct rainfo *)data;
1637 /* if necessary, reconstruct the packet. */
1641 "<%s> RA timer on %s is expired",
1642 __func__, rai->ifname);
1649 /* update RA timer */
1651 ra_timer_update(void *data, struct timeval *tm)
1653 struct rainfo *rai = (struct rainfo *)data;
1657 * Whenever a multicast advertisement is sent from an interface,
1658 * the timer is reset to a uniformly-distributed random value
1659 * between the interface's configured MinRtrAdvInterval and
1660 * MaxRtrAdvInterval (RFC2461 6.2.4).
1662 interval = rai->mininterval;
1663 #ifdef HAVE_ARC4RANDOM
1664 interval += arc4random_uniform(rai->maxinterval - rai->mininterval);
1666 interval += random() % (rai->maxinterval - rai->mininterval);
1670 * For the first few advertisements (up to
1671 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1672 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1673 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1676 if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
1677 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1678 interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1680 tm->tv_sec = interval;
1684 "<%s> RA timer on %s is set to %ld:%ld",
1685 __func__, rai->ifname,
1686 (long int)tm->tv_sec, (long int)tm->tv_usec);