2 /* $KAME: rtadvd.c,v 1.50 2001/02/04 06:15:15 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>
66 struct msghdr rcvmhdr;
67 static u_char *rcvcmsgbuf;
68 static size_t rcvcmsgbuflen;
69 static u_char *sndcmsgbuf = NULL;
70 static size_t sndcmsgbuflen;
73 struct msghdr sndmhdr;
74 struct iovec rcviov[2];
75 struct iovec sndiov[2];
76 struct sockaddr_in6 from;
77 struct sockaddr_in6 sin6_allnodes = {sizeof(sin6_allnodes), AF_INET6};
78 struct in6_addr in6a_site_allrouters;
79 static char *dumpfilename = "/var/run/rtadvd.dump"; /* XXX: should be configurable */
80 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[7];
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 __P((int, char *[]));
128 static void set_die __P((int));
129 static void die __P((void));
130 static void sock_open __P((void));
131 static void rtsock_open __P((void));
132 static void rtadvd_input __P((void));
133 static void rs_input __P((int, struct nd_router_solicit *,
134 struct in6_pktinfo *, struct sockaddr_in6 *));
135 static void ra_input __P((int, struct nd_router_advert *,
136 struct in6_pktinfo *, struct sockaddr_in6 *));
137 static int prefix_check __P((struct nd_opt_prefix_info *, struct rainfo *,
138 struct sockaddr_in6 *));
139 static int nd6_options __P((struct nd_opt_hdr *, int,
140 union nd_opts *, u_int32_t));
141 static void free_ndopts __P((union nd_opts *));
142 static void ra_output __P((struct rainfo *));
143 static void rtmsg_input __P((void));
144 static void rtadvd_set_dump_file __P((void));
153 struct timeval *timeout;
159 openlog("rtadvd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
161 /* get command line options and arguments */
163 #define OPTIONS "c:dDfM:mRs"
165 #define OPTIONS "c:dDfM:Rs"
167 while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
191 fprintf(stderr, "rtadvd: "
192 "the -R option is currently ignored.\n");
206 "usage: rtadvd [-dDfMmRs] [-c conffile] "
208 "usage: rtadvd [-dDfMRs] [-c conffile] "
216 (void)setlogmask(LOG_UPTO(LOG_ERR));
218 (void)setlogmask(LOG_UPTO(LOG_INFO));
220 /* timer initialization */
223 /* random value initialization */
224 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");
241 /* record the current PID */
243 if ((pidfp = fopen(pidfilename, "w")) == NULL) {
245 "<%s> failed to open a log file(%s), run anyway.",
246 __FUNCTION__, pidfilename);
248 fprintf(pidfp, "%d\n", pid);
253 FD_SET(sock, &fdset);
257 FD_SET(rtsock, &fdset);
263 signal(SIGTERM, (void *)set_die);
264 signal(SIGUSR1, (void *)rtadvd_set_dump_file);
267 struct fd_set select_fd = fdset; /* reinitialize */
269 if (do_dump) { /* SIGUSR1 */
271 rtadvd_dump_file(dumpfilename);
279 /* timer expiration check and reset the timer */
280 timeout = rtadvd_check_timer();
282 if (timeout != NULL) {
284 "<%s> set timer to %ld:%ld. waiting for "
285 "inputs or timeout", __FUNCTION__,
286 (long int)timeout->tv_sec,
287 (long int)timeout->tv_usec);
290 "<%s> there's no timer. waiting for inputs",
294 if ((i = select(maxfd + 1, &select_fd,
295 NULL, NULL, timeout)) < 0) {
296 /* EINTR would occur upon SIGUSR1 for status dump */
298 syslog(LOG_ERR, "<%s> select: %s",
299 __FUNCTION__, strerror(errno));
302 if (i == 0) /* timeout */
304 if (rtsock != -1 && FD_ISSET(rtsock, &select_fd))
306 if (FD_ISSET(sock, &select_fd))
309 exit(0); /* NOTREACHED */
313 rtadvd_set_dump_file()
330 const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
333 syslog(LOG_DEBUG, "<%s> cease to be an advertising router\n",
337 for (ra = ralist; ra; ra = ra->next) {
341 for (i = 0; i < retrans; i++) {
342 for (ra = ralist; ra; ra = ra->next)
344 sleep(MIN_DELAY_BETWEEN_RAS);
353 int n, type, ifindex = 0, plen;
355 char msg[2048], *next, *lim;
356 u_char ifname[IF_NAMESIZE];
357 struct prefix *prefix;
359 struct in6_addr *addr;
360 char addrbuf[INET6_ADDRSTRLEN];
362 n = read(rtsock, msg, sizeof(msg));
364 syslog(LOG_DEBUG, "<%s> received a routing message "
365 "(type = %d, len = %d)", __FUNCTION__, rtmsg_type(msg), n);
367 if (n > rtmsg_len(msg)) {
369 * This usually won't happen for messages received on
374 "<%s> received data length is larger than "
375 "1st routing message len. multiple messages? "
376 "read %d bytes, but 1st msg len = %d",
377 __FUNCTION__, n, rtmsg_len(msg));
385 for (next = msg; next < lim; next += len) {
388 next = get_next_msg(next, lim, 0, &len,
389 RTADV_TYPE2BITMASK(RTM_ADD) |
390 RTADV_TYPE2BITMASK(RTM_DELETE) |
391 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
392 RTADV_TYPE2BITMASK(RTM_DELADDR) |
393 RTADV_TYPE2BITMASK(RTM_IFINFO));
396 type = rtmsg_type(next);
400 ifindex = get_rtm_ifindex(next);
404 ifindex = get_ifam_ifindex(next);
407 ifindex = get_ifm_ifindex(next);
410 /* should not reach here */
413 "<%s:%d> unknown rtmsg %d on %s",
414 __FUNCTION__, __LINE__, type,
415 if_indextoname(ifindex, ifname));
420 if ((rai = if_indextorainfo(ifindex)) == NULL) {
423 "<%s> route changed on "
424 "non advertising interface(%s)",
426 if_indextoname(ifindex, ifname));
430 oldifflags = iflist[ifindex]->ifm_flags;
434 /* init ifflags because it may have changed */
435 iflist[ifindex]->ifm_flags =
436 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
439 break; /* we aren't interested in prefixes */
441 addr = get_addr(msg);
442 plen = get_prefixlen(msg);
443 /* sanity check for plen */
444 /* as RFC2373, prefixlen is at least 4 */
445 if (plen < 4 || plen > 127) {
446 syslog(LOG_INFO, "<%s> new interface route's"
447 "plen %d is invalid for a prefix",
451 prefix = find_prefix(rai, addr, plen);
455 * If the prefix has been invalidated,
456 * make it available again.
458 update_prefix(prefix);
459 } else if (dflag > 1) {
461 "<%s> new prefix(%s/%d) "
463 "but it was already in list",
465 inet_ntop(AF_INET6, addr,
466 (char *)addrbuf, INET6_ADDRSTRLEN),
471 make_prefix(rai, ifindex, addr, plen);
474 /* init ifflags because it may have changed */
475 iflist[ifindex]->ifm_flags =
476 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
481 addr = get_addr(msg);
482 plen = get_prefixlen(msg);
483 /* sanity check for plen */
484 /* as RFC2373, prefixlen is at least 4 */
485 if (plen < 4 || plen > 127) {
487 "<%s> deleted interface route's "
488 "plen %d is invalid for a prefix",
492 prefix = find_prefix(rai, addr, plen);
493 if (prefix == NULL) {
496 "<%s> prefix(%s/%d) was "
498 "but it was not in list",
500 inet_ntop(AF_INET6, addr,
501 (char *)addrbuf, INET6_ADDRSTRLEN),
506 invalidate_prefix(prefix);
510 /* init ifflags because it may have changed */
511 iflist[ifindex]->ifm_flags =
512 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
515 iflist[ifindex]->ifm_flags = get_ifm_flags(next);
518 /* should not reach here */
521 "<%s:%d> unknown rtmsg %d on %s",
522 __FUNCTION__, __LINE__, type,
523 if_indextoname(ifindex, ifname));
528 /* check if an interface flag is changed */
529 if ((oldifflags & IFF_UP) != 0 && /* UP to DOWN */
530 (iflist[ifindex]->ifm_flags & IFF_UP) == 0) {
532 "<%s> interface %s becomes down. stop timer.",
533 __FUNCTION__, rai->ifname);
534 rtadvd_remove_timer(&rai->timer);
535 } else if ((oldifflags & IFF_UP) == 0 && /* DOWN to UP */
536 (iflist[ifindex]->ifm_flags & IFF_UP) != 0) {
538 "<%s> interface %s becomes up. restart timer.",
539 __FUNCTION__, rai->ifname);
541 rai->initcounter = 0; /* reset the counter */
542 rai->waiting = 0; /* XXX */
543 rai->timer = rtadvd_add_timer(ra_timeout,
544 ra_timer_update, rai, rai);
545 ra_timer_update((void *)rai, &rai->timer->tm);
546 rtadvd_set_timer(&rai->timer->tm, rai->timer);
561 struct icmp6_hdr *icp;
564 struct in6_pktinfo *pi = NULL;
565 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
566 struct in6_addr dst = in6addr_any;
569 * Get message. We reset msg_controllen since the field could
570 * be modified if we had received a message before setting
573 rcvmhdr.msg_controllen = rcvcmsgbuflen;
574 if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
577 /* extract optional information via Advanced API */
578 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
580 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
581 if (cm->cmsg_level == IPPROTO_IPV6 &&
582 cm->cmsg_type == IPV6_PKTINFO &&
583 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
584 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
585 ifindex = pi->ipi6_ifindex;
588 if (cm->cmsg_level == IPPROTO_IPV6 &&
589 cm->cmsg_type == IPV6_HOPLIMIT &&
590 cm->cmsg_len == CMSG_LEN(sizeof(int)))
591 hlimp = (int *)CMSG_DATA(cm);
595 "<%s> failed to get receiving interface",
601 "<%s> failed to get receiving hop limit",
607 * If we happen to receive data on an interface which is now down,
608 * just discard the data.
610 if ((iflist[pi->ipi6_ifindex]->ifm_flags & IFF_UP) == 0) {
612 "<%s> received data on a disabled interface (%s)",
614 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
619 if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
621 "<%s> packet size(%d) is too short",
626 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
627 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
629 if (i < sizeof(struct icmp6_hdr)) {
631 "<%s> packet size(%d) is too short",
636 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
639 switch (icp->icmp6_type) {
640 case ND_ROUTER_SOLICIT:
642 * Message verification - RFC-2461 6.1.1
643 * XXX: these checks must be done in the kernel as well,
644 * but we can't completely rely on them.
648 "<%s> RS with invalid hop limit(%d) "
649 "received from %s on %s",
650 __FUNCTION__, *hlimp,
651 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
653 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
656 if (icp->icmp6_code) {
658 "<%s> RS with invalid ICMP6 code(%d) "
659 "received from %s on %s",
660 __FUNCTION__, icp->icmp6_code,
661 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
663 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
666 if (i < sizeof(struct nd_router_solicit)) {
668 "<%s> RS from %s on %s does not have enough "
671 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
673 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
676 rs_input(i, (struct nd_router_solicit *)icp, pi, &from);
678 case ND_ROUTER_ADVERT:
680 * Message verification - RFC-2461 6.1.2
681 * XXX: there's a same dilemma as above...
685 "<%s> RA with invalid hop limit(%d) "
686 "received from %s on %s",
687 __FUNCTION__, *hlimp,
688 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
690 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
693 if (icp->icmp6_code) {
695 "<%s> RA with invalid ICMP6 code(%d) "
696 "received from %s on %s",
697 __FUNCTION__, icp->icmp6_code,
698 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
700 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
703 if (i < sizeof(struct nd_router_advert)) {
705 "<%s> RA from %s on %s does not have enough "
708 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
710 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
713 ra_input(i, (struct nd_router_advert *)icp, pi, &from);
715 case ICMP6_ROUTER_RENUMBERING:
716 if (accept_rr == 0) {
717 syslog(LOG_ERR, "<%s> received a router renumbering "
718 "message, but not allowed to be accepted",
722 rr_input(i, (struct icmp6_router_renum *)icp, pi, &from,
727 * Note that this case is POSSIBLE, especially just
728 * after invocation of the daemon. This is because we
729 * could receive message after opening the socket and
730 * before setting ICMP6 type filter(see sock_open()).
732 syslog(LOG_ERR, "<%s> invalid icmp type(%d)",
733 __FUNCTION__, icp->icmp6_type);
741 rs_input(int len, struct nd_router_solicit *rs,
742 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
744 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
745 union nd_opts ndopts;
749 "<%s> RS received from %s on %s",
751 inet_ntop(AF_INET6, &from->sin6_addr,
752 ntopbuf, INET6_ADDRSTRLEN),
753 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
755 /* ND option check */
756 memset(&ndopts, 0, sizeof(ndopts));
757 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
758 len - sizeof(struct nd_router_solicit),
759 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
761 "<%s> ND option check failed for an RS from %s on %s",
763 inet_ntop(AF_INET6, &from->sin6_addr,
764 ntopbuf, INET6_ADDRSTRLEN),
765 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
770 * If the IP source address is the unspecified address, there
771 * must be no source link-layer address option in the message.
774 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
775 ndopts.nd_opts_src_lladdr) {
777 "<%s> RS from unspecified src on %s has a link-layer"
780 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
786 if (pi->ipi6_ifindex == ra->ifindex)
792 "<%s> RS received on non advertising interface(%s)",
794 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
798 ra->rsinput++; /* increment statistics */
801 * Decide whether to send RA according to the rate-limit
805 long delay; /* must not be greater than 1000000 */
806 struct timeval interval, now, min_delay, tm_tmp, *rest;
807 struct soliciter *sol;
810 * record sockaddr waiting for RA, if possible
812 sol = (struct soliciter *)malloc(sizeof(*sol));
815 /*XXX RFC2553 need clarification on flowinfo */
816 sol->addr.sin6_flowinfo = 0;
817 sol->next = ra->soliciter;
818 ra->soliciter = sol->next;
822 * If there is already a waiting RS packet, don't
829 * Compute a random delay. If the computed value
830 * corresponds to a time later than the time the next
831 * multicast RA is scheduled to be sent, ignore the random
832 * delay and send the advertisement at the
833 * already-scheduled time. RFC-2461 6.2.6
835 delay = random() % MAX_RA_DELAY_TIME;
837 interval.tv_usec = delay;
838 rest = rtadvd_timer_rest(ra->timer);
839 if (TIMEVAL_LT(*rest, interval)) {
841 "<%s> random delay is larger than "
842 "the rest of normal timer",
848 * If we sent a multicast Router Advertisement within
849 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
850 * the advertisement to be sent at a time corresponding to
851 * MIN_DELAY_BETWEEN_RAS plus the random value after the
852 * previous advertisement was sent.
854 gettimeofday(&now, NULL);
855 TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp);
856 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
857 min_delay.tv_usec = 0;
858 if (TIMEVAL_LT(tm_tmp, min_delay)) {
859 TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
860 TIMEVAL_ADD(&min_delay, &interval, &interval);
862 rtadvd_set_timer(&interval, ra->timer);
867 free_ndopts(&ndopts);
872 ra_input(int len, struct nd_router_advert *ra,
873 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
876 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
877 union nd_opts ndopts;
878 char *on_off[] = {"OFF", "ON"};
879 u_int32_t reachabletime, retranstimer, mtu;
880 int inconsistent = 0;
883 "<%s> RA received from %s on %s",
885 inet_ntop(AF_INET6, &from->sin6_addr,
886 ntopbuf, INET6_ADDRSTRLEN),
887 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
889 /* ND option check */
890 memset(&ndopts, 0, sizeof(ndopts));
891 if (nd6_options((struct nd_opt_hdr *)(ra + 1),
892 len - sizeof(struct nd_router_advert),
893 &ndopts, NDOPT_FLAG_SRCLINKADDR |
894 NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
896 "<%s> ND option check failed for an RA from %s on %s",
898 inet_ntop(AF_INET6, &from->sin6_addr,
899 ntopbuf, INET6_ADDRSTRLEN),
900 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
905 * RA consistency check according to RFC-2461 6.2.7
907 if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == 0) {
909 "<%s> received RA from %s on non-advertising"
912 inet_ntop(AF_INET6, &from->sin6_addr,
913 ntopbuf, INET6_ADDRSTRLEN),
914 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
917 rai->rainput++; /* increment statistics */
919 /* Cur Hop Limit value */
920 if (ra->nd_ra_curhoplimit && rai->hoplimit &&
921 ra->nd_ra_curhoplimit != rai->hoplimit) {
923 "<%s> CurHopLimit inconsistent on %s:"
924 " %d from %s, %d from us",
927 ra->nd_ra_curhoplimit,
928 inet_ntop(AF_INET6, &from->sin6_addr,
929 ntopbuf, INET6_ADDRSTRLEN),
934 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
937 "<%s> M flag inconsistent on %s:"
938 " %s from %s, %s from us",
941 on_off[!rai->managedflg],
942 inet_ntop(AF_INET6, &from->sin6_addr,
943 ntopbuf, INET6_ADDRSTRLEN),
944 on_off[rai->managedflg]);
948 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
951 "<%s> O flag inconsistent on %s:"
952 " %s from %s, %s from us",
955 on_off[!rai->otherflg],
956 inet_ntop(AF_INET6, &from->sin6_addr,
957 ntopbuf, INET6_ADDRSTRLEN),
958 on_off[rai->otherflg]);
962 reachabletime = ntohl(ra->nd_ra_reachable);
963 if (reachabletime && rai->reachabletime &&
964 reachabletime != rai->reachabletime) {
966 "<%s> ReachableTime inconsistent on %s:"
967 " %d from %s, %d from us",
971 inet_ntop(AF_INET6, &from->sin6_addr,
972 ntopbuf, INET6_ADDRSTRLEN),
977 retranstimer = ntohl(ra->nd_ra_retransmit);
978 if (retranstimer && rai->retranstimer &&
979 retranstimer != rai->retranstimer) {
981 "<%s> RetranceTimer inconsistent on %s:"
982 " %d from %s, %d from us",
986 inet_ntop(AF_INET6, &from->sin6_addr,
987 ntopbuf, INET6_ADDRSTRLEN),
991 /* Values in the MTU options */
992 if (ndopts.nd_opts_mtu) {
993 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
994 if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
996 "<%s> MTU option value inconsistent on %s:"
997 " %d from %s, %d from us",
1000 inet_ntop(AF_INET6, &from->sin6_addr,
1001 ntopbuf, INET6_ADDRSTRLEN),
1006 /* Preferred and Valid Lifetimes for prefixes */
1008 struct nd_optlist *optp = ndopts.nd_opts_list;
1010 if (ndopts.nd_opts_pi) {
1011 if (prefix_check(ndopts.nd_opts_pi, rai, from))
1015 if (prefix_check((struct nd_opt_prefix_info *)optp->opt,
1023 rai->rainconsistent++;
1026 free_ndopts(&ndopts);
1030 /* return a non-zero value if the received prefix is inconsitent with ours */
1032 prefix_check(struct nd_opt_prefix_info *pinfo,
1033 struct rainfo *rai, struct sockaddr_in6 *from)
1035 u_int32_t preferred_time, valid_time;
1037 int inconsistent = 0;
1038 u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
1041 #if 0 /* impossible */
1042 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1047 * log if the adveritsed prefix has link-local scope(sanity check?)
1049 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
1051 "<%s> link-local prefix %s/%d is advertised "
1054 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1055 prefixbuf, INET6_ADDRSTRLEN),
1056 pinfo->nd_opt_pi_prefix_len,
1057 inet_ntop(AF_INET6, &from->sin6_addr,
1058 ntopbuf, INET6_ADDRSTRLEN),
1062 if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1063 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1065 "<%s> prefix %s/%d from %s on %s is not in our list",
1067 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1068 prefixbuf, INET6_ADDRSTRLEN),
1069 pinfo->nd_opt_pi_prefix_len,
1070 inet_ntop(AF_INET6, &from->sin6_addr,
1071 ntopbuf, INET6_ADDRSTRLEN),
1076 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1077 if (pp->pltimeexpire) {
1079 * The lifetime is decremented in real time, so we should
1080 * compare the expiration time.
1081 * (RFC 2461 Section 6.2.7.)
1082 * XXX: can we really expect that all routers on the link
1083 * have synchronized clocks?
1085 gettimeofday(&now, NULL);
1086 preferred_time += now.tv_sec;
1088 if (rai->clockskew &&
1089 abs(preferred_time - pp->pltimeexpire) > rai->clockskew) {
1091 "<%s> prefeerred lifetime for %s/%d"
1092 " (decr. in real time) inconsistent on %s:"
1093 " %d from %s, %ld from us",
1095 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1096 prefixbuf, INET6_ADDRSTRLEN),
1097 pinfo->nd_opt_pi_prefix_len,
1098 rai->ifname, preferred_time,
1099 inet_ntop(AF_INET6, &from->sin6_addr,
1100 ntopbuf, INET6_ADDRSTRLEN),
1104 } else if (preferred_time != pp->preflifetime) {
1106 "<%s> prefeerred lifetime for %s/%d"
1107 " inconsistent on %s:"
1108 " %d from %s, %d from us",
1110 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1111 prefixbuf, INET6_ADDRSTRLEN),
1112 pinfo->nd_opt_pi_prefix_len,
1113 rai->ifname, preferred_time,
1114 inet_ntop(AF_INET6, &from->sin6_addr,
1115 ntopbuf, INET6_ADDRSTRLEN),
1119 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1120 if (pp->vltimeexpire) {
1121 gettimeofday(&now, NULL);
1122 valid_time += now.tv_sec;
1124 if (rai->clockskew &&
1125 abs(valid_time - pp->vltimeexpire) > rai->clockskew) {
1127 "<%s> valid lifetime for %s/%d"
1128 " (decr. in real time) inconsistent on %s:"
1129 " %d from %s, %ld from us",
1131 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1132 prefixbuf, INET6_ADDRSTRLEN),
1133 pinfo->nd_opt_pi_prefix_len,
1134 rai->ifname, preferred_time,
1135 inet_ntop(AF_INET6, &from->sin6_addr,
1136 ntopbuf, INET6_ADDRSTRLEN),
1140 } else if (valid_time != pp->validlifetime) {
1142 "<%s> valid lifetime for %s/%d"
1143 " inconsistent on %s:"
1144 " %d from %s, %d from us",
1146 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1147 prefixbuf, INET6_ADDRSTRLEN),
1148 pinfo->nd_opt_pi_prefix_len,
1149 rai->ifname, valid_time,
1150 inet_ntop(AF_INET6, &from->sin6_addr,
1151 ntopbuf, INET6_ADDRSTRLEN),
1156 return(inconsistent);
1160 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1163 int bytelen, bitlen;
1165 for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
1166 if (plen != pp->prefixlen)
1170 if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
1172 if (prefix->s6_addr[bytelen] >> (8 - bitlen) ==
1173 pp->prefix.s6_addr[bytelen] >> (8 - bitlen))
1180 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1182 prefix_match(struct in6_addr *p0, int plen0,
1183 struct in6_addr *p1, int plen1)
1185 int bytelen, bitlen;
1189 bytelen = plen1 / 8;
1191 if (memcmp((void *)p0, (void *)p1, bytelen))
1193 if (p0->s6_addr[bytelen] >> (8 - bitlen) ==
1194 p1->s6_addr[bytelen] >> (8 - bitlen))
1201 nd6_options(struct nd_opt_hdr *hdr, int limit,
1202 union nd_opts *ndopts, u_int32_t optflags)
1206 for (; limit > 0; limit -= optlen) {
1207 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1208 optlen = hdr->nd_opt_len << 3;
1209 if (hdr->nd_opt_len == 0) {
1211 "<%s> bad ND option length(0) (type = %d)",
1212 __FUNCTION__, hdr->nd_opt_type);
1216 if (hdr->nd_opt_type > ND_OPT_MTU) {
1218 "<%s> unknown ND option(type %d)",
1219 __FUNCTION__, hdr->nd_opt_type);
1223 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1225 "<%s> unexpected ND option(type %d)",
1226 __FUNCTION__, hdr->nd_opt_type);
1230 switch (hdr->nd_opt_type) {
1231 case ND_OPT_SOURCE_LINKADDR:
1232 case ND_OPT_TARGET_LINKADDR:
1233 case ND_OPT_REDIRECTED_HEADER:
1235 if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
1237 "<%s> duplicated ND option (type = %d)",
1238 __FUNCTION__, hdr->nd_opt_type);
1240 ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
1242 case ND_OPT_PREFIX_INFORMATION:
1244 struct nd_optlist *pfxlist;
1246 if (ndopts->nd_opts_pi == 0) {
1247 ndopts->nd_opts_pi =
1248 (struct nd_opt_prefix_info *)hdr;
1251 if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
1252 syslog(LOG_ERR, "<%s> can't allocate memory",
1256 pfxlist->next = ndopts->nd_opts_list;
1258 ndopts->nd_opts_list = pfxlist;
1262 default: /* impossible */
1270 free_ndopts(ndopts);
1276 free_ndopts(union nd_opts *ndopts)
1278 struct nd_optlist *opt = ndopts->nd_opts_list, *next;
1290 struct icmp6_filter filt;
1291 struct ipv6_mreq mreq;
1292 struct rainfo *ra = ralist;
1294 /* XXX: should be max MTU attached to the node */
1295 static u_char answer[1500];
1297 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1298 CMSG_SPACE(sizeof(int));
1299 rcvcmsgbuf = (u_char *)malloc(rcvcmsgbuflen);
1300 if (rcvcmsgbuf == NULL) {
1301 syslog(LOG_ERR, "<%s> not enough core", __FUNCTION__);
1305 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1306 CMSG_SPACE(sizeof(int));
1307 sndcmsgbuf = (u_char *)malloc(sndcmsgbuflen);
1308 if (sndcmsgbuf == NULL) {
1309 syslog(LOG_ERR, "<%s> not enough core", __FUNCTION__);
1313 if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1314 syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
1319 /* specify to tell receiving interface */
1321 #ifdef IPV6_RECVPKTINFO
1322 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1324 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s",
1325 __FUNCTION__, strerror(errno));
1328 #else /* old adv. API */
1329 if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
1331 syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
1332 __FUNCTION__, strerror(errno));
1338 /* specify to tell value of hoplimit field of received IP6 hdr */
1339 #ifdef IPV6_RECVHOPLIMIT
1340 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1342 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s",
1343 __FUNCTION__, strerror(errno));
1346 #else /* old adv. API */
1347 if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
1349 syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
1350 __FUNCTION__, strerror(errno));
1355 ICMP6_FILTER_SETBLOCKALL(&filt);
1356 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1357 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1359 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1360 if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1361 sizeof(filt)) < 0) {
1362 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1363 __FUNCTION__, strerror(errno));
1368 * join all routers multicast address on each advertising interface.
1370 if (inet_pton(AF_INET6, ALLROUTERS_LINK,
1371 &mreq.ipv6mr_multiaddr.s6_addr)
1373 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1378 mreq.ipv6mr_interface = ra->ifindex;
1379 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
1380 sizeof(mreq)) < 0) {
1381 syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
1382 __FUNCTION__, ra->ifname, strerror(errno));
1389 * When attending router renumbering, join all-routers site-local
1393 if (inet_pton(AF_INET6, ALLROUTERS_SITE,
1394 &in6a_site_allrouters) != 1) {
1395 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1399 mreq.ipv6mr_multiaddr = in6a_site_allrouters;
1401 if ((mreq.ipv6mr_interface = if_nametoindex(mcastif))
1404 "<%s> invalid interface: %s",
1405 __FUNCTION__, mcastif);
1409 mreq.ipv6mr_interface = ralist->ifindex;
1410 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1411 &mreq, sizeof(mreq)) < 0) {
1413 "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
1415 mcastif ? mcastif : ralist->ifname,
1421 /* initialize msghdr for receiving packets */
1422 rcviov[0].iov_base = (caddr_t)answer;
1423 rcviov[0].iov_len = sizeof(answer);
1424 rcvmhdr.msg_name = (caddr_t)&from;
1425 rcvmhdr.msg_namelen = sizeof(from);
1426 rcvmhdr.msg_iov = rcviov;
1427 rcvmhdr.msg_iovlen = 1;
1428 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1429 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1431 /* initialize msghdr for sending packets */
1432 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1433 sndmhdr.msg_iov = sndiov;
1434 sndmhdr.msg_iovlen = 1;
1435 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1436 sndmhdr.msg_controllen = sndcmsgbuflen;
1441 /* open a routing socket to watch the routing table */
1445 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1447 "<%s> socket: %s", __FUNCTION__, strerror(errno));
1453 if_indextorainfo(int index)
1455 struct rainfo *rai = ralist;
1457 for (rai = ralist; rai; rai = rai->next) {
1458 if (rai->ifindex == index)
1462 return(NULL); /* search failed */
1467 struct rainfo *rainfo;
1471 struct in6_pktinfo *pi;
1472 struct soliciter *sol, *nextsol;
1474 if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) {
1475 syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
1476 __FUNCTION__, rainfo->ifname);
1480 make_packet(rainfo); /* XXX: inefficient */
1482 sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
1483 sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
1484 sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
1486 cm = CMSG_FIRSTHDR(&sndmhdr);
1487 /* specify the outgoing interface */
1488 cm->cmsg_level = IPPROTO_IPV6;
1489 cm->cmsg_type = IPV6_PKTINFO;
1490 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1491 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1492 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1493 pi->ipi6_ifindex = rainfo->ifindex;
1495 /* specify the hop limit of the packet */
1499 cm = CMSG_NXTHDR(&sndmhdr, cm);
1500 cm->cmsg_level = IPPROTO_IPV6;
1501 cm->cmsg_type = IPV6_HOPLIMIT;
1502 cm->cmsg_len = CMSG_LEN(sizeof(int));
1503 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1507 "<%s> send RA on %s, # of waitings = %d",
1508 __FUNCTION__, rainfo->ifname, rainfo->waiting);
1510 i = sendmsg(sock, &sndmhdr, 0);
1512 if (i < 0 || i != rainfo->ra_datalen) {
1514 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1515 __FUNCTION__, rainfo->ifname,
1521 * unicast advertisements
1522 * XXX commented out. reason: though spec does not forbit it, unicast
1523 * advert does not really help
1525 for (sol = rainfo->soliciter; sol; sol = nextsol) {
1526 nextsol = sol->next;
1529 sndmhdr.msg_name = (caddr_t)&sol->addr;
1530 i = sendmsg(sock, &sndmhdr, 0);
1531 if (i < 0 || i != rainfo->ra_datalen) {
1534 "<%s> unicast sendmsg on %s: %s",
1535 __FUNCTION__, rainfo->ifname,
1544 rainfo->soliciter = NULL;
1546 /* update counter */
1547 if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
1548 rainfo->initcounter++;
1551 /* update timestamp */
1552 gettimeofday(&rainfo->lastsent, NULL);
1554 /* reset waiting conter */
1555 rainfo->waiting = 0;
1558 /* process RA timer */
1559 struct rtadvd_timer *
1560 ra_timeout(void *data)
1562 struct rainfo *rai = (struct rainfo *)data;
1565 /* if necessary, reconstruct the packet. */
1569 "<%s> RA timer on %s is expired",
1570 __FUNCTION__, rai->ifname);
1577 /* update RA timer */
1579 ra_timer_update(void *data, struct timeval *tm)
1581 struct rainfo *rai = (struct rainfo *)data;
1585 * Whenever a multicast advertisement is sent from an interface,
1586 * the timer is reset to a uniformly-distributed random value
1587 * between the interface's configured MinRtrAdvInterval and
1588 * MaxRtrAdvInterval (RFC2461 6.2.4).
1590 interval = rai->mininterval;
1591 interval += random() % (rai->maxinterval - rai->mininterval);
1594 * For the first few advertisements (up to
1595 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1596 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1597 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1600 if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
1601 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1602 interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1604 tm->tv_sec = interval;
1608 "<%s> RA timer on %s is set to %ld:%ld",
1609 __FUNCTION__, rai->ifname,
1610 (long int)tm->tv_sec, (long int)tm->tv_usec);