1 /* $KAME: rtadvd.c,v 1.50 2001/02/04 06:15:15 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/usr.sbin/rtadvd/rtadvd.c,v 1.3.2.4 2003/04/05 10:31:58 ume Exp $
34 #include <sys/param.h>
35 #include <sys/socket.h>
38 #include <sys/queue.h>
41 #include <net/route.h>
42 #include <net/if_dl.h>
43 #include <netinet/in.h>
44 #include <netinet/ip6.h>
45 #include <netinet6/ip6_var.h>
46 #include <netinet/icmp6.h>
48 #include <arpa/inet.h>
67 struct msghdr rcvmhdr;
68 static u_char *rcvcmsgbuf;
69 static size_t rcvcmsgbuflen;
70 static u_char *sndcmsgbuf = NULL;
71 static size_t sndcmsgbuflen;
74 struct msghdr sndmhdr;
75 struct iovec rcviov[2];
76 struct iovec sndiov[2];
77 struct sockaddr_in6 from;
78 struct sockaddr_in6 sin6_allnodes = {sizeof(sin6_allnodes), AF_INET6};
79 struct in6_addr in6a_site_allrouters;
80 static char *dumpfilename = "/var/run/rtadvd.dump"; /* XXX: should be configurable */
81 static char *pidfilename = "/var/run/rtadvd.pid"; /* should be configurable */
89 int dflag = 0, sflag = 0;
91 u_char *conffile = NULL;
93 struct rainfo *ralist = NULL;
95 struct nd_optlist *next;
96 struct nd_opt_hdr *opt;
99 struct nd_opt_hdr *nd_opt_array[7];
101 struct nd_opt_hdr *zero;
102 struct nd_opt_hdr *src_lladdr;
103 struct nd_opt_hdr *tgt_lladdr;
104 struct nd_opt_prefix_info *pi;
105 struct nd_opt_rd_hdr *rh;
106 struct nd_opt_mtu *mtu;
107 struct nd_optlist *list;
110 #define nd_opts_src_lladdr nd_opt_each.src_lladdr
111 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr
112 #define nd_opts_pi nd_opt_each.pi
113 #define nd_opts_rh nd_opt_each.rh
114 #define nd_opts_mtu nd_opt_each.mtu
115 #define nd_opts_list nd_opt_each.list
117 #define NDOPT_FLAG_SRCLINKADDR 0x1
118 #define NDOPT_FLAG_TGTLINKADDR 0x2
119 #define NDOPT_FLAG_PREFIXINFO 0x4
120 #define NDOPT_FLAG_RDHDR 0x8
121 #define NDOPT_FLAG_MTU 0x10
123 u_int32_t ndopt_flags[] = {
124 0, NDOPT_FLAG_SRCLINKADDR, NDOPT_FLAG_TGTLINKADDR,
125 NDOPT_FLAG_PREFIXINFO, NDOPT_FLAG_RDHDR, NDOPT_FLAG_MTU
128 int main(int, char *[]);
129 static void set_die(int);
130 static void die(void);
131 static void sock_open(void);
132 static void rtsock_open(void);
133 static void rtadvd_input(void);
134 static void rs_input(int, struct nd_router_solicit *,
135 struct in6_pktinfo *, struct sockaddr_in6 *);
136 static void ra_input(int, struct nd_router_advert *,
137 struct in6_pktinfo *, struct sockaddr_in6 *);
138 static int prefix_check(struct nd_opt_prefix_info *, struct rainfo *,
139 struct sockaddr_in6 *);
140 static int nd6_options(struct nd_opt_hdr *, int,
141 union nd_opts *, u_int32_t);
142 static void free_ndopts(union nd_opts *);
143 static void ra_output(struct rainfo *);
144 static void rtmsg_input(void);
145 static void rtadvd_set_dump_file(void);
148 main(int argc, char *argv[])
152 struct timeval *timeout;
158 openlog("rtadvd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
160 /* get command line options and arguments */
162 #define OPTIONS "c:dDfM:mRs"
164 #define OPTIONS "c:dDfM:Rs"
166 while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
190 fprintf(stderr, "rtadvd: "
191 "the -R option is currently ignored.\n");
205 "usage: rtadvd [-dDfMmRs] [-c conffile] "
207 "usage: rtadvd [-dDfMRs] [-c conffile] "
215 setlogmask(LOG_UPTO(LOG_ERR));
217 setlogmask(LOG_UPTO(LOG_INFO));
219 /* timer initialization */
222 /* random value initialization */
223 srandom((u_long)time(NULL));
225 /* get iflist block from kernel */
231 if (inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr) != 1) {
232 fprintf(stderr, "fatal: inet_pton failed\n");
240 /* record the current PID */
242 if ((pidfp = fopen(pidfilename, "w")) == NULL) {
244 "<%s> failed to open a log file(%s), run anyway.",
245 __func__, pidfilename);
247 fprintf(pidfp, "%d\n", pid);
252 FD_SET(sock, &fdset);
256 FD_SET(rtsock, &fdset);
262 signal(SIGTERM, (void *)set_die);
263 signal(SIGUSR1, (void *)rtadvd_set_dump_file);
266 struct fd_set select_fd = fdset; /* reinitialize */
268 if (do_dump) { /* SIGUSR1 */
270 rtadvd_dump_file(dumpfilename);
278 /* timer expiration check and reset the timer */
279 timeout = rtadvd_check_timer();
281 if (timeout != NULL) {
283 "<%s> set timer to %ld:%ld. waiting for "
284 "inputs or timeout", __func__,
285 (long int)timeout->tv_sec,
286 (long int)timeout->tv_usec);
289 "<%s> there's no timer. waiting for inputs",
293 if ((i = select(maxfd + 1, &select_fd,
294 NULL, NULL, timeout)) < 0) {
295 /* EINTR would occur upon SIGUSR1 for status dump */
297 syslog(LOG_ERR, "<%s> select: %s",
298 __func__, strerror(errno));
301 if (i == 0) /* timeout */
303 if (rtsock != -1 && FD_ISSET(rtsock, &select_fd))
305 if (FD_ISSET(sock, &select_fd))
308 exit(0); /* NOTREACHED */
312 rtadvd_set_dump_file(void)
328 const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
331 syslog(LOG_DEBUG, "<%s> cease to be an advertising router\n",
335 for (ra = ralist; ra; ra = ra->next) {
339 for (i = 0; i < retrans; i++) {
340 for (ra = ralist; ra; ra = ra->next)
342 sleep(MIN_DELAY_BETWEEN_RAS);
351 int n, type, ifindex = 0, plen;
353 char msg[2048], *next, *lim;
354 u_char ifname[IF_NAMESIZE];
355 struct prefix *prefix;
357 struct in6_addr *addr;
358 char addrbuf[INET6_ADDRSTRLEN];
360 n = read(rtsock, msg, sizeof(msg));
362 syslog(LOG_DEBUG, "<%s> received a routing message "
363 "(type = %d, len = %d)", __func__, rtmsg_type(msg), n);
365 if (n > rtmsg_len(msg)) {
367 * This usually won't happen for messages received on
372 "<%s> received data length is larger than "
373 "1st routing message len. multiple messages? "
374 "read %d bytes, but 1st msg len = %d",
375 __func__, n, rtmsg_len(msg));
383 for (next = msg; next < lim; next += len) {
386 next = get_next_msg(next, lim, 0, &len,
387 RTADV_TYPE2BITMASK(RTM_ADD) |
388 RTADV_TYPE2BITMASK(RTM_DELETE) |
389 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
390 RTADV_TYPE2BITMASK(RTM_DELADDR) |
391 RTADV_TYPE2BITMASK(RTM_IFINFO));
394 type = rtmsg_type(next);
398 ifindex = get_rtm_ifindex(next);
402 ifindex = get_ifam_ifindex(next);
405 ifindex = get_ifm_ifindex(next);
408 /* should not reach here */
411 "<%s:%d> unknown rtmsg %d on %s",
412 __func__, __LINE__, type,
413 if_indextoname(ifindex, ifname));
418 if ((rai = if_indextorainfo(ifindex)) == NULL) {
421 "<%s> route changed on "
422 "non advertising interface(%s)",
424 if_indextoname(ifindex, ifname));
428 oldifflags = iflist[ifindex]->ifm_flags;
432 /* init ifflags because it may have changed */
433 iflist[ifindex]->ifm_flags =
434 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
437 break; /* we aren't interested in prefixes */
439 addr = get_addr(msg);
440 plen = get_prefixlen(msg);
441 /* sanity check for plen */
442 /* as RFC2373, prefixlen is at least 4 */
443 if (plen < 4 || plen > 127) {
444 syslog(LOG_INFO, "<%s> new interface route's"
445 "plen %d is invalid for a prefix",
449 prefix = find_prefix(rai, addr, plen);
453 * If the prefix has been invalidated,
454 * make it available again.
456 update_prefix(prefix);
457 } else if (dflag > 1) {
459 "<%s> new prefix(%s/%d) "
461 "but it was already in list",
463 inet_ntop(AF_INET6, addr,
464 (char *)addrbuf, INET6_ADDRSTRLEN),
469 make_prefix(rai, ifindex, addr, plen);
472 /* init ifflags because it may have changed */
473 iflist[ifindex]->ifm_flags =
474 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
479 addr = get_addr(msg);
480 plen = get_prefixlen(msg);
481 /* sanity check for plen */
482 /* as RFC2373, prefixlen is at least 4 */
483 if (plen < 4 || plen > 127) {
485 "<%s> deleted interface route's "
486 "plen %d is invalid for a prefix",
490 prefix = find_prefix(rai, addr, plen);
491 if (prefix == NULL) {
494 "<%s> prefix(%s/%d) was "
496 "but it was not in list",
498 inet_ntop(AF_INET6, addr,
499 (char *)addrbuf, INET6_ADDRSTRLEN),
504 invalidate_prefix(prefix);
508 /* init ifflags because it may have changed */
509 iflist[ifindex]->ifm_flags =
510 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
513 iflist[ifindex]->ifm_flags = get_ifm_flags(next);
516 /* should not reach here */
519 "<%s:%d> unknown rtmsg %d on %s",
520 __func__, __LINE__, type,
521 if_indextoname(ifindex, ifname));
526 /* check if an interface flag is changed */
527 if ((oldifflags & IFF_UP) != 0 && /* UP to DOWN */
528 (iflist[ifindex]->ifm_flags & IFF_UP) == 0) {
530 "<%s> interface %s becomes down. stop timer.",
531 __func__, rai->ifname);
532 rtadvd_remove_timer(&rai->timer);
533 } else if ((oldifflags & IFF_UP) == 0 && /* DOWN to UP */
534 (iflist[ifindex]->ifm_flags & IFF_UP) != 0) {
536 "<%s> interface %s becomes up. restart timer.",
537 __func__, rai->ifname);
539 rai->initcounter = 0; /* reset the counter */
540 rai->waiting = 0; /* XXX */
541 rai->timer = rtadvd_add_timer(ra_timeout,
542 ra_timer_update, rai, rai);
543 ra_timer_update((void *)rai, &rai->timer->tm);
544 rtadvd_set_timer(&rai->timer->tm, rai->timer);
559 struct icmp6_hdr *icp;
562 struct in6_pktinfo *pi = NULL;
563 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
564 struct in6_addr dst = in6addr_any;
567 * Get message. We reset msg_controllen since the field could
568 * be modified if we had received a message before setting
571 rcvmhdr.msg_controllen = rcvcmsgbuflen;
572 if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
575 /* extract optional information via Advanced API */
576 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
578 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
579 if (cm->cmsg_level == IPPROTO_IPV6 &&
580 cm->cmsg_type == IPV6_PKTINFO &&
581 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
582 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
583 ifindex = pi->ipi6_ifindex;
586 if (cm->cmsg_level == IPPROTO_IPV6 &&
587 cm->cmsg_type == IPV6_HOPLIMIT &&
588 cm->cmsg_len == CMSG_LEN(sizeof(int)))
589 hlimp = (int *)CMSG_DATA(cm);
593 "<%s> failed to get receiving interface",
599 "<%s> failed to get receiving hop limit",
605 * If we happen to receive data on an interface which is now down,
606 * just discard the data.
608 if ((iflist[pi->ipi6_ifindex]->ifm_flags & IFF_UP) == 0) {
610 "<%s> received data on a disabled interface (%s)",
612 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
617 if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
619 "<%s> packet size(%d) is too short",
624 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
625 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
627 if (i < sizeof(struct icmp6_hdr)) {
629 "<%s> packet size(%d) is too short",
634 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
637 switch (icp->icmp6_type) {
638 case ND_ROUTER_SOLICIT:
640 * Message verification - RFC-2461 6.1.1
641 * XXX: these checks must be done in the kernel as well,
642 * but we can't completely rely on them.
646 "<%s> RS with invalid hop limit(%d) "
647 "received from %s on %s",
649 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
651 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
654 if (icp->icmp6_code) {
656 "<%s> RS with invalid ICMP6 code(%d) "
657 "received from %s on %s",
658 __func__, icp->icmp6_code,
659 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
661 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
664 if (i < sizeof(struct nd_router_solicit)) {
666 "<%s> RS from %s on %s does not have enough "
669 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
671 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
674 rs_input(i, (struct nd_router_solicit *)icp, pi, &from);
676 case ND_ROUTER_ADVERT:
678 * Message verification - RFC-2461 6.1.2
679 * XXX: there's a same dilemma as above...
683 "<%s> RA with invalid hop limit(%d) "
684 "received from %s on %s",
686 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
688 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
691 if (icp->icmp6_code) {
693 "<%s> RA with invalid ICMP6 code(%d) "
694 "received from %s on %s",
695 __func__, icp->icmp6_code,
696 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
698 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
701 if (i < sizeof(struct nd_router_advert)) {
703 "<%s> RA from %s on %s does not have enough "
706 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
708 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
711 ra_input(i, (struct nd_router_advert *)icp, pi, &from);
713 case ICMP6_ROUTER_RENUMBERING:
714 if (accept_rr == 0) {
715 syslog(LOG_ERR, "<%s> received a router renumbering "
716 "message, but not allowed to be accepted",
720 rr_input(i, (struct icmp6_router_renum *)icp, pi, &from,
725 * Note that this case is POSSIBLE, especially just
726 * after invocation of the daemon. This is because we
727 * could receive message after opening the socket and
728 * before setting ICMP6 type filter(see sock_open()).
730 syslog(LOG_ERR, "<%s> invalid icmp type(%d)",
731 __func__, icp->icmp6_type);
739 rs_input(int len, struct nd_router_solicit *rs,
740 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
742 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
743 union nd_opts ndopts;
747 "<%s> RS received from %s on %s",
749 inet_ntop(AF_INET6, &from->sin6_addr,
750 ntopbuf, INET6_ADDRSTRLEN),
751 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
753 /* ND option check */
754 memset(&ndopts, 0, sizeof(ndopts));
755 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
756 len - sizeof(struct nd_router_solicit),
757 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
759 "<%s> ND option check failed for an RS from %s on %s",
761 inet_ntop(AF_INET6, &from->sin6_addr,
762 ntopbuf, INET6_ADDRSTRLEN),
763 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
768 * If the IP source address is the unspecified address, there
769 * must be no source link-layer address option in the message.
772 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
773 ndopts.nd_opts_src_lladdr) {
775 "<%s> RS from unspecified src on %s has a link-layer"
778 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
784 if (pi->ipi6_ifindex == ra->ifindex)
790 "<%s> RS received on non advertising interface(%s)",
792 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
796 ra->rsinput++; /* increment statistics */
799 * Decide whether to send RA according to the rate-limit
803 long delay; /* must not be greater than 1000000 */
804 struct timeval interval, now, min_delay, tm_tmp, *rest;
805 struct soliciter *sol;
808 * record sockaddr waiting for RA, if possible
810 sol = (struct soliciter *)malloc(sizeof(*sol));
813 /*XXX RFC2553 need clarification on flowinfo */
814 sol->addr.sin6_flowinfo = 0;
815 sol->next = ra->soliciter;
816 ra->soliciter = sol->next;
820 * If there is already a waiting RS packet, don't
827 * Compute a random delay. If the computed value
828 * corresponds to a time later than the time the next
829 * multicast RA is scheduled to be sent, ignore the random
830 * delay and send the advertisement at the
831 * already-scheduled time. RFC-2461 6.2.6
833 delay = random() % MAX_RA_DELAY_TIME;
835 interval.tv_usec = delay;
836 rest = rtadvd_timer_rest(ra->timer);
837 if (TIMEVAL_LT(*rest, interval)) {
839 "<%s> random delay is larger than "
840 "the rest of normal timer",
846 * If we sent a multicast Router Advertisement within
847 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
848 * the advertisement to be sent at a time corresponding to
849 * MIN_DELAY_BETWEEN_RAS plus the random value after the
850 * previous advertisement was sent.
852 gettimeofday(&now, NULL);
853 TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp);
854 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
855 min_delay.tv_usec = 0;
856 if (TIMEVAL_LT(tm_tmp, min_delay)) {
857 TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
858 TIMEVAL_ADD(&min_delay, &interval, &interval);
860 rtadvd_set_timer(&interval, ra->timer);
865 free_ndopts(&ndopts);
870 ra_input(int len, struct nd_router_advert *ra,
871 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
874 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
875 union nd_opts ndopts;
876 char *on_off[] = {"OFF", "ON"};
877 u_int32_t reachabletime, retranstimer, mtu;
878 int inconsistent = 0;
881 "<%s> RA received from %s on %s",
883 inet_ntop(AF_INET6, &from->sin6_addr,
884 ntopbuf, INET6_ADDRSTRLEN),
885 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
887 /* ND option check */
888 memset(&ndopts, 0, sizeof(ndopts));
889 if (nd6_options((struct nd_opt_hdr *)(ra + 1),
890 len - sizeof(struct nd_router_advert),
891 &ndopts, NDOPT_FLAG_SRCLINKADDR |
892 NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
894 "<%s> ND option check failed for an RA from %s on %s",
896 inet_ntop(AF_INET6, &from->sin6_addr,
897 ntopbuf, INET6_ADDRSTRLEN),
898 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
903 * RA consistency check according to RFC-2461 6.2.7
905 if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == 0) {
907 "<%s> received RA from %s on non-advertising"
910 inet_ntop(AF_INET6, &from->sin6_addr,
911 ntopbuf, INET6_ADDRSTRLEN),
912 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
915 rai->rainput++; /* increment statistics */
917 /* Cur Hop Limit value */
918 if (ra->nd_ra_curhoplimit && rai->hoplimit &&
919 ra->nd_ra_curhoplimit != rai->hoplimit) {
921 "<%s> CurHopLimit inconsistent on %s:"
922 " %d from %s, %d from us",
925 ra->nd_ra_curhoplimit,
926 inet_ntop(AF_INET6, &from->sin6_addr,
927 ntopbuf, INET6_ADDRSTRLEN),
932 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
935 "<%s> M flag inconsistent on %s:"
936 " %s from %s, %s from us",
939 on_off[!rai->managedflg],
940 inet_ntop(AF_INET6, &from->sin6_addr,
941 ntopbuf, INET6_ADDRSTRLEN),
942 on_off[rai->managedflg]);
946 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
949 "<%s> O flag inconsistent on %s:"
950 " %s from %s, %s from us",
953 on_off[!rai->otherflg],
954 inet_ntop(AF_INET6, &from->sin6_addr,
955 ntopbuf, INET6_ADDRSTRLEN),
956 on_off[rai->otherflg]);
960 reachabletime = ntohl(ra->nd_ra_reachable);
961 if (reachabletime && rai->reachabletime &&
962 reachabletime != rai->reachabletime) {
964 "<%s> ReachableTime inconsistent on %s:"
965 " %d from %s, %d from us",
969 inet_ntop(AF_INET6, &from->sin6_addr,
970 ntopbuf, INET6_ADDRSTRLEN),
975 retranstimer = ntohl(ra->nd_ra_retransmit);
976 if (retranstimer && rai->retranstimer &&
977 retranstimer != rai->retranstimer) {
979 "<%s> RetranceTimer inconsistent on %s:"
980 " %d from %s, %d from us",
984 inet_ntop(AF_INET6, &from->sin6_addr,
985 ntopbuf, INET6_ADDRSTRLEN),
989 /* Values in the MTU options */
990 if (ndopts.nd_opts_mtu) {
991 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
992 if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
994 "<%s> MTU option value inconsistent on %s:"
995 " %d from %s, %d from us",
998 inet_ntop(AF_INET6, &from->sin6_addr,
999 ntopbuf, INET6_ADDRSTRLEN),
1004 /* Preferred and Valid Lifetimes for prefixes */
1006 struct nd_optlist *optp = ndopts.nd_opts_list;
1008 if (ndopts.nd_opts_pi) {
1009 if (prefix_check(ndopts.nd_opts_pi, rai, from))
1013 if (prefix_check((struct nd_opt_prefix_info *)optp->opt,
1021 rai->rainconsistent++;
1024 free_ndopts(&ndopts);
1028 /* return a non-zero value if the received prefix is inconsitent with ours */
1030 prefix_check(struct nd_opt_prefix_info *pinfo,
1031 struct rainfo *rai, struct sockaddr_in6 *from)
1033 u_int32_t preferred_time, valid_time;
1035 int inconsistent = 0;
1036 u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
1039 #if 0 /* impossible */
1040 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1045 * log if the adveritsed prefix has link-local scope(sanity check?)
1047 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
1049 "<%s> link-local prefix %s/%d is advertised "
1052 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1053 prefixbuf, INET6_ADDRSTRLEN),
1054 pinfo->nd_opt_pi_prefix_len,
1055 inet_ntop(AF_INET6, &from->sin6_addr,
1056 ntopbuf, INET6_ADDRSTRLEN),
1060 if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1061 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1063 "<%s> prefix %s/%d from %s on %s is not in our list",
1065 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1066 prefixbuf, INET6_ADDRSTRLEN),
1067 pinfo->nd_opt_pi_prefix_len,
1068 inet_ntop(AF_INET6, &from->sin6_addr,
1069 ntopbuf, INET6_ADDRSTRLEN),
1074 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1075 if (pp->pltimeexpire) {
1077 * The lifetime is decremented in real time, so we should
1078 * compare the expiration time.
1079 * (RFC 2461 Section 6.2.7.)
1080 * XXX: can we really expect that all routers on the link
1081 * have synchronized clocks?
1083 gettimeofday(&now, NULL);
1084 preferred_time += now.tv_sec;
1086 if (rai->clockskew &&
1087 abs(preferred_time - pp->pltimeexpire) > rai->clockskew) {
1089 "<%s> prefeerred lifetime for %s/%d"
1090 " (decr. in real time) inconsistent on %s:"
1091 " %d from %s, %ld from us",
1093 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1094 prefixbuf, INET6_ADDRSTRLEN),
1095 pinfo->nd_opt_pi_prefix_len,
1096 rai->ifname, preferred_time,
1097 inet_ntop(AF_INET6, &from->sin6_addr,
1098 ntopbuf, INET6_ADDRSTRLEN),
1102 } else if (preferred_time != pp->preflifetime) {
1104 "<%s> prefeerred lifetime for %s/%d"
1105 " inconsistent on %s:"
1106 " %d from %s, %d from us",
1108 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1109 prefixbuf, INET6_ADDRSTRLEN),
1110 pinfo->nd_opt_pi_prefix_len,
1111 rai->ifname, preferred_time,
1112 inet_ntop(AF_INET6, &from->sin6_addr,
1113 ntopbuf, INET6_ADDRSTRLEN),
1117 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1118 if (pp->vltimeexpire) {
1119 gettimeofday(&now, NULL);
1120 valid_time += now.tv_sec;
1122 if (rai->clockskew &&
1123 abs(valid_time - pp->vltimeexpire) > rai->clockskew) {
1125 "<%s> valid lifetime for %s/%d"
1126 " (decr. in real time) inconsistent on %s:"
1127 " %d from %s, %ld from us",
1129 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1130 prefixbuf, INET6_ADDRSTRLEN),
1131 pinfo->nd_opt_pi_prefix_len,
1132 rai->ifname, preferred_time,
1133 inet_ntop(AF_INET6, &from->sin6_addr,
1134 ntopbuf, INET6_ADDRSTRLEN),
1138 } else if (valid_time != pp->validlifetime) {
1140 "<%s> valid lifetime for %s/%d"
1141 " inconsistent on %s:"
1142 " %d from %s, %d from us",
1144 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1145 prefixbuf, INET6_ADDRSTRLEN),
1146 pinfo->nd_opt_pi_prefix_len,
1147 rai->ifname, valid_time,
1148 inet_ntop(AF_INET6, &from->sin6_addr,
1149 ntopbuf, INET6_ADDRSTRLEN),
1154 return(inconsistent);
1158 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1161 int bytelen, bitlen;
1163 for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
1164 if (plen != pp->prefixlen)
1168 if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
1170 if (prefix->s6_addr[bytelen] >> (8 - bitlen) ==
1171 pp->prefix.s6_addr[bytelen] >> (8 - bitlen))
1178 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1180 prefix_match(struct in6_addr *p0, int plen0,
1181 struct in6_addr *p1, int plen1)
1183 int bytelen, bitlen;
1187 bytelen = plen1 / 8;
1189 if (memcmp((void *)p0, (void *)p1, bytelen))
1191 if (p0->s6_addr[bytelen] >> (8 - bitlen) ==
1192 p1->s6_addr[bytelen] >> (8 - bitlen))
1199 nd6_options(struct nd_opt_hdr *hdr, int limit,
1200 union nd_opts *ndopts, u_int32_t optflags)
1204 for (; limit > 0; limit -= optlen) {
1205 if (limit < sizeof(struct nd_opt_hdr)) {
1206 syslog(LOG_INFO, "<%s> short option header", __func__);
1210 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1211 if (hdr->nd_opt_len == 0) {
1213 "<%s> bad ND option length(0) (type = %d)",
1214 __func__, hdr->nd_opt_type);
1217 optlen = hdr->nd_opt_len << 3;
1218 if (optlen > limit) {
1219 syslog(LOG_INFO, "<%s> short option", __func__);
1223 if (hdr->nd_opt_type > ND_OPT_MTU) {
1225 "<%s> unknown ND option(type %d)",
1226 __func__, hdr->nd_opt_type);
1230 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1232 "<%s> unexpected ND option(type %d)",
1233 __func__, hdr->nd_opt_type);
1238 * Option length check. Do it here for all fixed-length
1241 if ((hdr->nd_opt_type == ND_OPT_MTU &&
1242 (optlen != sizeof(struct nd_opt_mtu))) ||
1243 ((hdr->nd_opt_type == ND_OPT_PREFIX_INFORMATION &&
1244 optlen != sizeof(struct nd_opt_prefix_info)))) {
1245 syslog(LOG_INFO, "<%s> invalid option length",
1250 switch (hdr->nd_opt_type) {
1251 case ND_OPT_SOURCE_LINKADDR:
1252 case ND_OPT_TARGET_LINKADDR:
1253 case ND_OPT_REDIRECTED_HEADER:
1254 break; /* we don't care about these options */
1256 if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
1258 "<%s> duplicated ND option (type = %d)",
1259 __func__, hdr->nd_opt_type);
1261 ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
1263 case ND_OPT_PREFIX_INFORMATION:
1265 struct nd_optlist *pfxlist;
1267 if (ndopts->nd_opts_pi == 0) {
1268 ndopts->nd_opts_pi =
1269 (struct nd_opt_prefix_info *)hdr;
1272 if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
1273 syslog(LOG_ERR, "<%s> can't allocate memory",
1277 pfxlist->next = ndopts->nd_opts_list;
1279 ndopts->nd_opts_list = pfxlist;
1283 default: /* impossible */
1291 free_ndopts(ndopts);
1297 free_ndopts(union nd_opts *ndopts)
1299 struct nd_optlist *opt = ndopts->nd_opts_list, *next;
1311 struct icmp6_filter filt;
1312 struct ipv6_mreq mreq;
1313 struct rainfo *ra = ralist;
1315 /* XXX: should be max MTU attached to the node */
1316 static u_char answer[1500];
1318 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1319 CMSG_SPACE(sizeof(int));
1320 rcvcmsgbuf = (u_char *)malloc(rcvcmsgbuflen);
1321 if (rcvcmsgbuf == NULL) {
1322 syslog(LOG_ERR, "<%s> not enough core", __func__);
1326 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1327 CMSG_SPACE(sizeof(int));
1328 sndcmsgbuf = (u_char *)malloc(sndcmsgbuflen);
1329 if (sndcmsgbuf == NULL) {
1330 syslog(LOG_ERR, "<%s> not enough core", __func__);
1334 if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1335 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1340 /* specify to tell receiving interface */
1342 #ifdef IPV6_RECVPKTINFO
1343 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1345 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s",
1346 __func__, strerror(errno));
1349 #else /* old adv. API */
1350 if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
1352 syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
1353 __func__, strerror(errno));
1359 /* specify to tell value of hoplimit field of received IP6 hdr */
1360 #ifdef IPV6_RECVHOPLIMIT
1361 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1363 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s",
1364 __func__, strerror(errno));
1367 #else /* old adv. API */
1368 if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
1370 syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
1371 __func__, strerror(errno));
1376 ICMP6_FILTER_SETBLOCKALL(&filt);
1377 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1378 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1380 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1381 if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1382 sizeof(filt)) < 0) {
1383 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1384 __func__, strerror(errno));
1389 * join all routers multicast address on each advertising interface.
1391 if (inet_pton(AF_INET6, ALLROUTERS_LINK,
1392 &mreq.ipv6mr_multiaddr.s6_addr)
1394 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1399 mreq.ipv6mr_interface = ra->ifindex;
1400 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
1401 sizeof(mreq)) < 0) {
1402 syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
1403 __func__, ra->ifname, strerror(errno));
1410 * When attending router renumbering, join all-routers site-local
1414 if (inet_pton(AF_INET6, ALLROUTERS_SITE,
1415 &in6a_site_allrouters) != 1) {
1416 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1420 mreq.ipv6mr_multiaddr = in6a_site_allrouters;
1422 if ((mreq.ipv6mr_interface = if_nametoindex(mcastif))
1425 "<%s> invalid interface: %s",
1430 mreq.ipv6mr_interface = ralist->ifindex;
1431 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1432 &mreq, sizeof(mreq)) < 0) {
1434 "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
1436 mcastif ? mcastif : ralist->ifname,
1442 /* initialize msghdr for receiving packets */
1443 rcviov[0].iov_base = (caddr_t)answer;
1444 rcviov[0].iov_len = sizeof(answer);
1445 rcvmhdr.msg_name = (caddr_t)&from;
1446 rcvmhdr.msg_namelen = sizeof(from);
1447 rcvmhdr.msg_iov = rcviov;
1448 rcvmhdr.msg_iovlen = 1;
1449 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1450 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1452 /* initialize msghdr for sending packets */
1453 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1454 sndmhdr.msg_iov = sndiov;
1455 sndmhdr.msg_iovlen = 1;
1456 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1457 sndmhdr.msg_controllen = sndcmsgbuflen;
1462 /* open a routing socket to watch the routing table */
1466 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1468 "<%s> socket: %s", __func__, strerror(errno));
1474 if_indextorainfo(int index)
1476 struct rainfo *rai = ralist;
1478 for (rai = ralist; rai; rai = rai->next) {
1479 if (rai->ifindex == index)
1483 return(NULL); /* search failed */
1487 ra_output(struct rainfo *rainfo)
1491 struct in6_pktinfo *pi;
1492 struct soliciter *sol, *nextsol;
1494 if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) {
1495 syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
1496 __func__, rainfo->ifname);
1500 make_packet(rainfo); /* XXX: inefficient */
1502 sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
1503 sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
1504 sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
1506 cm = CMSG_FIRSTHDR(&sndmhdr);
1507 /* specify the outgoing interface */
1508 cm->cmsg_level = IPPROTO_IPV6;
1509 cm->cmsg_type = IPV6_PKTINFO;
1510 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1511 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1512 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1513 pi->ipi6_ifindex = rainfo->ifindex;
1515 /* specify the hop limit of the packet */
1519 cm = CMSG_NXTHDR(&sndmhdr, cm);
1520 cm->cmsg_level = IPPROTO_IPV6;
1521 cm->cmsg_type = IPV6_HOPLIMIT;
1522 cm->cmsg_len = CMSG_LEN(sizeof(int));
1523 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1527 "<%s> send RA on %s, # of waitings = %d",
1528 __func__, rainfo->ifname, rainfo->waiting);
1530 i = sendmsg(sock, &sndmhdr, 0);
1532 if (i < 0 || i != rainfo->ra_datalen) {
1534 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1535 __func__, rainfo->ifname,
1541 * unicast advertisements
1542 * XXX commented out. reason: though spec does not forbit it, unicast
1543 * advert does not really help
1545 for (sol = rainfo->soliciter; sol; sol = nextsol) {
1546 nextsol = sol->next;
1549 sndmhdr.msg_name = (caddr_t)&sol->addr;
1550 i = sendmsg(sock, &sndmhdr, 0);
1551 if (i < 0 || i != rainfo->ra_datalen) {
1554 "<%s> unicast sendmsg on %s: %s",
1555 __func__, rainfo->ifname,
1564 rainfo->soliciter = NULL;
1566 /* update counter */
1567 if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
1568 rainfo->initcounter++;
1571 /* update timestamp */
1572 gettimeofday(&rainfo->lastsent, NULL);
1574 /* reset waiting conter */
1575 rainfo->waiting = 0;
1578 /* process RA timer */
1579 struct rtadvd_timer *
1580 ra_timeout(void *data)
1582 struct rainfo *rai = (struct rainfo *)data;
1585 /* if necessary, reconstruct the packet. */
1589 "<%s> RA timer on %s is expired",
1590 __func__, rai->ifname);
1597 /* update RA timer */
1599 ra_timer_update(void *data, struct timeval *tm)
1601 struct rainfo *rai = (struct rainfo *)data;
1605 * Whenever a multicast advertisement is sent from an interface,
1606 * the timer is reset to a uniformly-distributed random value
1607 * between the interface's configured MinRtrAdvInterval and
1608 * MaxRtrAdvInterval (RFC2461 6.2.4).
1610 interval = rai->mininterval;
1611 interval += random() % (rai->maxinterval - rai->mininterval);
1614 * For the first few advertisements (up to
1615 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1616 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1617 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1620 if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
1621 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1622 interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1624 tm->tv_sec = interval;
1628 "<%s> RA timer on %s is set to %ld:%ld",
1629 __func__, rai->ifname,
1630 (long int)tm->tv_sec, (long int)tm->tv_usec);