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>
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(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(void);
147 main(int argc, char *argv[])
151 struct timeval *timeout;
157 openlog("rtadvd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
159 /* get command line options and arguments */
161 #define OPTIONS "c:dDfM:mRs"
163 #define OPTIONS "c:dDfM:Rs"
165 while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
189 fprintf(stderr, "rtadvd: "
190 "the -R option is currently ignored.\n");
204 "usage: rtadvd [-dDfMmRs] [-c conffile] "
206 "usage: rtadvd [-dDfMRs] [-c conffile] "
214 setlogmask(LOG_UPTO(LOG_ERR));
216 setlogmask(LOG_UPTO(LOG_INFO));
218 /* timer initialization */
221 /* random value initialization */
222 srandom((u_long)time(NULL));
224 /* get iflist block from kernel */
230 if (inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr) != 1) {
231 fprintf(stderr, "fatal: inet_pton failed\n");
239 /* record the current PID */
241 if ((pidfp = fopen(pidfilename, "w")) == NULL) {
243 "<%s> failed to open a log file(%s), run anyway.",
244 __func__, pidfilename);
246 fprintf(pidfp, "%d\n", pid);
251 FD_SET(sock, &fdset);
255 FD_SET(rtsock, &fdset);
261 signal(SIGTERM, (void *)set_die);
262 signal(SIGUSR1, (void *)rtadvd_set_dump_file);
265 struct fd_set select_fd = fdset; /* reinitialize */
267 if (do_dump) { /* SIGUSR1 */
269 rtadvd_dump_file(dumpfilename);
277 /* timer expiration check and reset the timer */
278 timeout = rtadvd_check_timer();
280 if (timeout != NULL) {
282 "<%s> set timer to %ld:%ld. waiting for "
283 "inputs or timeout", __func__,
284 (long int)timeout->tv_sec,
285 (long int)timeout->tv_usec);
288 "<%s> there's no timer. waiting for inputs",
292 if ((i = select(maxfd + 1, &select_fd,
293 NULL, NULL, timeout)) < 0) {
294 /* EINTR would occur upon SIGUSR1 for status dump */
296 syslog(LOG_ERR, "<%s> select: %s",
297 __func__, strerror(errno));
300 if (i == 0) /* timeout */
302 if (rtsock != -1 && FD_ISSET(rtsock, &select_fd))
304 if (FD_ISSET(sock, &select_fd))
307 exit(0); /* NOTREACHED */
311 rtadvd_set_dump_file(void)
327 const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
330 syslog(LOG_DEBUG, "<%s> cease to be an advertising router\n",
334 for (ra = ralist; ra; ra = ra->next) {
338 for (i = 0; i < retrans; i++) {
339 for (ra = ralist; ra; ra = ra->next)
341 sleep(MIN_DELAY_BETWEEN_RAS);
350 int n, type, ifindex = 0, plen;
352 char msg[2048], *next, *lim;
353 u_char ifname[IF_NAMESIZE];
354 struct prefix *prefix;
356 struct in6_addr *addr;
357 char addrbuf[INET6_ADDRSTRLEN];
359 n = read(rtsock, msg, sizeof(msg));
361 syslog(LOG_DEBUG, "<%s> received a routing message "
362 "(type = %d, len = %d)", __func__, rtmsg_type(msg), n);
364 if (n > rtmsg_len(msg)) {
366 * This usually won't happen for messages received on
371 "<%s> received data length is larger than "
372 "1st routing message len. multiple messages? "
373 "read %d bytes, but 1st msg len = %d",
374 __func__, n, rtmsg_len(msg));
382 for (next = msg; next < lim; next += len) {
385 next = get_next_msg(next, lim, 0, &len,
386 RTADV_TYPE2BITMASK(RTM_ADD) |
387 RTADV_TYPE2BITMASK(RTM_DELETE) |
388 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
389 RTADV_TYPE2BITMASK(RTM_DELADDR) |
390 RTADV_TYPE2BITMASK(RTM_IFINFO));
393 type = rtmsg_type(next);
397 ifindex = get_rtm_ifindex(next);
401 ifindex = get_ifam_ifindex(next);
404 ifindex = get_ifm_ifindex(next);
407 /* should not reach here */
410 "<%s:%d> unknown rtmsg %d on %s",
411 __func__, __LINE__, type,
412 if_indextoname(ifindex, ifname));
417 if ((rai = if_indextorainfo(ifindex)) == NULL) {
420 "<%s> route changed on "
421 "non advertising interface(%s)",
423 if_indextoname(ifindex, ifname));
427 oldifflags = iflist[ifindex]->ifm_flags;
431 /* init ifflags because it may have changed */
432 iflist[ifindex]->ifm_flags =
433 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
436 break; /* we aren't interested in prefixes */
438 addr = get_addr(msg);
439 plen = get_prefixlen(msg);
440 /* sanity check for plen */
441 /* as RFC2373, prefixlen is at least 4 */
442 if (plen < 4 || plen > 127) {
443 syslog(LOG_INFO, "<%s> new interface route's"
444 "plen %d is invalid for a prefix",
448 prefix = find_prefix(rai, addr, plen);
452 * If the prefix has been invalidated,
453 * make it available again.
455 update_prefix(prefix);
456 } else if (dflag > 1) {
458 "<%s> new prefix(%s/%d) "
460 "but it was already in list",
462 inet_ntop(AF_INET6, addr,
463 (char *)addrbuf, INET6_ADDRSTRLEN),
468 make_prefix(rai, ifindex, addr, plen);
471 /* init ifflags because it may have changed */
472 iflist[ifindex]->ifm_flags =
473 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
478 addr = get_addr(msg);
479 plen = get_prefixlen(msg);
480 /* sanity check for plen */
481 /* as RFC2373, prefixlen is at least 4 */
482 if (plen < 4 || plen > 127) {
484 "<%s> deleted interface route's "
485 "plen %d is invalid for a prefix",
489 prefix = find_prefix(rai, addr, plen);
490 if (prefix == NULL) {
493 "<%s> prefix(%s/%d) was "
495 "but it was not in list",
497 inet_ntop(AF_INET6, addr,
498 (char *)addrbuf, INET6_ADDRSTRLEN),
503 invalidate_prefix(prefix);
507 /* init ifflags because it may have changed */
508 iflist[ifindex]->ifm_flags =
509 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
512 iflist[ifindex]->ifm_flags = get_ifm_flags(next);
515 /* should not reach here */
518 "<%s:%d> unknown rtmsg %d on %s",
519 __func__, __LINE__, type,
520 if_indextoname(ifindex, ifname));
525 /* check if an interface flag is changed */
526 if ((oldifflags & IFF_UP) != 0 && /* UP to DOWN */
527 (iflist[ifindex]->ifm_flags & IFF_UP) == 0) {
529 "<%s> interface %s becomes down. stop timer.",
530 __func__, rai->ifname);
531 rtadvd_remove_timer(&rai->timer);
532 } else if ((oldifflags & IFF_UP) == 0 && /* DOWN to UP */
533 (iflist[ifindex]->ifm_flags & IFF_UP) != 0) {
535 "<%s> interface %s becomes up. restart timer.",
536 __func__, rai->ifname);
538 rai->initcounter = 0; /* reset the counter */
539 rai->waiting = 0; /* XXX */
540 rai->timer = rtadvd_add_timer(ra_timeout,
541 ra_timer_update, rai, rai);
542 ra_timer_update(rai, &rai->timer->tm);
543 rtadvd_set_timer(&rai->timer->tm, rai->timer);
558 struct icmp6_hdr *icp;
561 struct in6_pktinfo *pi = NULL;
562 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
563 struct in6_addr dst = in6addr_any;
566 * Get message. We reset msg_controllen since the field could
567 * be modified if we had received a message before setting
570 rcvmhdr.msg_controllen = rcvcmsgbuflen;
571 if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
574 /* extract optional information via Advanced API */
575 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
577 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
578 if (cm->cmsg_level == IPPROTO_IPV6 &&
579 cm->cmsg_type == IPV6_PKTINFO &&
580 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
581 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
582 ifindex = pi->ipi6_ifindex;
585 if (cm->cmsg_level == IPPROTO_IPV6 &&
586 cm->cmsg_type == IPV6_HOPLIMIT &&
587 cm->cmsg_len == CMSG_LEN(sizeof(int)))
588 hlimp = (int *)CMSG_DATA(cm);
592 "<%s> failed to get receiving interface",
598 "<%s> failed to get receiving hop limit",
604 * If we happen to receive data on an interface which is now down,
605 * just discard the data.
607 if ((iflist[pi->ipi6_ifindex]->ifm_flags & IFF_UP) == 0) {
609 "<%s> received data on a disabled interface (%s)",
611 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
616 if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
618 "<%s> packet size(%d) is too short",
623 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
624 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
626 if (i < sizeof(struct icmp6_hdr)) {
628 "<%s> packet size(%d) is too short",
633 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
636 switch (icp->icmp6_type) {
637 case ND_ROUTER_SOLICIT:
639 * Message verification - RFC-2461 6.1.1
640 * XXX: these checks must be done in the kernel as well,
641 * but we can't completely rely on them.
645 "<%s> RS with invalid hop limit(%d) "
646 "received from %s on %s",
648 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
650 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
653 if (icp->icmp6_code) {
655 "<%s> RS with invalid ICMP6 code(%d) "
656 "received from %s on %s",
657 __func__, icp->icmp6_code,
658 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
660 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
663 if (i < sizeof(struct nd_router_solicit)) {
665 "<%s> RS from %s on %s does not have enough "
668 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
670 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
673 rs_input(i, (struct nd_router_solicit *)icp, pi, &from);
675 case ND_ROUTER_ADVERT:
677 * Message verification - RFC-2461 6.1.2
678 * XXX: there's a same dilemma as above...
682 "<%s> RA with invalid hop limit(%d) "
683 "received from %s on %s",
685 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
687 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
690 if (icp->icmp6_code) {
692 "<%s> RA with invalid ICMP6 code(%d) "
693 "received from %s on %s",
694 __func__, icp->icmp6_code,
695 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
697 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
700 if (i < sizeof(struct nd_router_advert)) {
702 "<%s> RA from %s on %s does not have enough "
705 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
707 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
710 ra_input(i, (struct nd_router_advert *)icp, pi, &from);
712 case ICMP6_ROUTER_RENUMBERING:
713 if (accept_rr == 0) {
714 syslog(LOG_ERR, "<%s> received a router renumbering "
715 "message, but not allowed to be accepted",
719 rr_input(i, (struct icmp6_router_renum *)icp, pi, &from,
724 * Note that this case is POSSIBLE, especially just
725 * after invocation of the daemon. This is because we
726 * could receive message after opening the socket and
727 * before setting ICMP6 type filter(see sock_open()).
729 syslog(LOG_ERR, "<%s> invalid icmp type(%d)",
730 __func__, icp->icmp6_type);
738 rs_input(int len, struct nd_router_solicit *rs,
739 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
741 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
742 union nd_opts ndopts;
746 "<%s> RS received from %s on %s",
748 inet_ntop(AF_INET6, &from->sin6_addr,
749 ntopbuf, INET6_ADDRSTRLEN),
750 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
752 /* ND option check */
753 memset(&ndopts, 0, sizeof(ndopts));
754 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
755 len - sizeof(struct nd_router_solicit),
756 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
758 "<%s> ND option check failed for an RS from %s on %s",
760 inet_ntop(AF_INET6, &from->sin6_addr,
761 ntopbuf, INET6_ADDRSTRLEN),
762 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
767 * If the IP source address is the unspecified address, there
768 * must be no source link-layer address option in the message.
771 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
772 ndopts.nd_opts_src_lladdr) {
774 "<%s> RS from unspecified src on %s has a link-layer"
777 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
783 if (pi->ipi6_ifindex == ra->ifindex)
789 "<%s> RS received on non advertising interface(%s)",
791 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
795 ra->rsinput++; /* increment statistics */
798 * Decide whether to send RA according to the rate-limit
802 long delay; /* must not be greater than 1000000 */
803 struct timeval interval, now, min_delay, tm_tmp, *rest;
804 struct soliciter *sol;
807 * record sockaddr waiting for RA, if possible
809 sol = (struct soliciter *)malloc(sizeof(*sol));
812 /*XXX RFC2553 need clarification on flowinfo */
813 sol->addr.sin6_flowinfo = 0;
814 sol->next = ra->soliciter;
815 ra->soliciter = sol->next;
819 * If there is already a waiting RS packet, don't
826 * Compute a random delay. If the computed value
827 * corresponds to a time later than the time the next
828 * multicast RA is scheduled to be sent, ignore the random
829 * delay and send the advertisement at the
830 * already-scheduled time. RFC-2461 6.2.6
832 delay = random() % MAX_RA_DELAY_TIME;
834 interval.tv_usec = delay;
835 rest = rtadvd_timer_rest(ra->timer);
836 if (TIMEVAL_LT(*rest, interval)) {
838 "<%s> random delay is larger than "
839 "the rest of normal timer",
845 * If we sent a multicast Router Advertisement within
846 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
847 * the advertisement to be sent at a time corresponding to
848 * MIN_DELAY_BETWEEN_RAS plus the random value after the
849 * previous advertisement was sent.
851 gettimeofday(&now, NULL);
852 TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp);
853 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
854 min_delay.tv_usec = 0;
855 if (TIMEVAL_LT(tm_tmp, min_delay)) {
856 TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
857 TIMEVAL_ADD(&min_delay, &interval, &interval);
859 rtadvd_set_timer(&interval, ra->timer);
864 free_ndopts(&ndopts);
869 ra_input(int len, struct nd_router_advert *ra,
870 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
873 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
874 union nd_opts ndopts;
875 char *on_off[] = {"OFF", "ON"};
876 u_int32_t reachabletime, retranstimer, mtu;
877 int inconsistent = 0;
880 "<%s> RA received from %s on %s",
882 inet_ntop(AF_INET6, &from->sin6_addr,
883 ntopbuf, INET6_ADDRSTRLEN),
884 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
886 /* ND option check */
887 memset(&ndopts, 0, sizeof(ndopts));
888 if (nd6_options((struct nd_opt_hdr *)(ra + 1),
889 len - sizeof(struct nd_router_advert),
890 &ndopts, NDOPT_FLAG_SRCLINKADDR |
891 NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
893 "<%s> ND option check failed for an RA from %s on %s",
895 inet_ntop(AF_INET6, &from->sin6_addr,
896 ntopbuf, INET6_ADDRSTRLEN),
897 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
902 * RA consistency check according to RFC-2461 6.2.7
904 if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == NULL) {
906 "<%s> received RA from %s on non-advertising"
909 inet_ntop(AF_INET6, &from->sin6_addr,
910 ntopbuf, INET6_ADDRSTRLEN),
911 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
914 rai->rainput++; /* increment statistics */
916 /* Cur Hop Limit value */
917 if (ra->nd_ra_curhoplimit && rai->hoplimit &&
918 ra->nd_ra_curhoplimit != rai->hoplimit) {
920 "<%s> CurHopLimit inconsistent on %s:"
921 " %d from %s, %d from us",
924 ra->nd_ra_curhoplimit,
925 inet_ntop(AF_INET6, &from->sin6_addr,
926 ntopbuf, INET6_ADDRSTRLEN),
931 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
934 "<%s> M flag inconsistent on %s:"
935 " %s from %s, %s from us",
938 on_off[!rai->managedflg],
939 inet_ntop(AF_INET6, &from->sin6_addr,
940 ntopbuf, INET6_ADDRSTRLEN),
941 on_off[rai->managedflg]);
945 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
948 "<%s> O flag inconsistent on %s:"
949 " %s from %s, %s from us",
952 on_off[!rai->otherflg],
953 inet_ntop(AF_INET6, &from->sin6_addr,
954 ntopbuf, INET6_ADDRSTRLEN),
955 on_off[rai->otherflg]);
959 reachabletime = ntohl(ra->nd_ra_reachable);
960 if (reachabletime && rai->reachabletime &&
961 reachabletime != rai->reachabletime) {
963 "<%s> ReachableTime inconsistent on %s:"
964 " %d from %s, %d from us",
968 inet_ntop(AF_INET6, &from->sin6_addr,
969 ntopbuf, INET6_ADDRSTRLEN),
974 retranstimer = ntohl(ra->nd_ra_retransmit);
975 if (retranstimer && rai->retranstimer &&
976 retranstimer != rai->retranstimer) {
978 "<%s> RetranceTimer inconsistent on %s:"
979 " %d from %s, %d from us",
983 inet_ntop(AF_INET6, &from->sin6_addr,
984 ntopbuf, INET6_ADDRSTRLEN),
988 /* Values in the MTU options */
989 if (ndopts.nd_opts_mtu) {
990 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
991 if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
993 "<%s> MTU option value inconsistent on %s:"
994 " %d from %s, %d from us",
997 inet_ntop(AF_INET6, &from->sin6_addr,
998 ntopbuf, INET6_ADDRSTRLEN),
1003 /* Preferred and Valid Lifetimes for prefixes */
1005 struct nd_optlist *optp = ndopts.nd_opts_list;
1007 if (ndopts.nd_opts_pi) {
1008 if (prefix_check(ndopts.nd_opts_pi, rai, from))
1012 if (prefix_check((struct nd_opt_prefix_info *)optp->opt,
1020 rai->rainconsistent++;
1023 free_ndopts(&ndopts);
1027 /* return a non-zero value if the received prefix is inconsitent with ours */
1029 prefix_check(struct nd_opt_prefix_info *pinfo,
1030 struct rainfo *rai, struct sockaddr_in6 *from)
1032 u_int32_t preferred_time, valid_time;
1034 int inconsistent = 0;
1035 u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
1038 #if 0 /* impossible */
1039 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1044 * log if the adveritsed prefix has link-local scope(sanity check?)
1046 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
1048 "<%s> link-local prefix %s/%d is advertised "
1051 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1052 prefixbuf, INET6_ADDRSTRLEN),
1053 pinfo->nd_opt_pi_prefix_len,
1054 inet_ntop(AF_INET6, &from->sin6_addr,
1055 ntopbuf, INET6_ADDRSTRLEN),
1059 if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1060 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1062 "<%s> prefix %s/%d from %s on %s is not in our list",
1064 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1065 prefixbuf, INET6_ADDRSTRLEN),
1066 pinfo->nd_opt_pi_prefix_len,
1067 inet_ntop(AF_INET6, &from->sin6_addr,
1068 ntopbuf, INET6_ADDRSTRLEN),
1073 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1074 if (pp->pltimeexpire) {
1076 * The lifetime is decremented in real time, so we should
1077 * compare the expiration time.
1078 * (RFC 2461 Section 6.2.7.)
1079 * XXX: can we really expect that all routers on the link
1080 * have synchronized clocks?
1082 gettimeofday(&now, NULL);
1083 preferred_time += now.tv_sec;
1085 if (rai->clockskew &&
1086 abs(preferred_time - pp->pltimeexpire) > rai->clockskew) {
1088 "<%s> prefeerred lifetime for %s/%d"
1089 " (decr. in real time) inconsistent on %s:"
1090 " %d from %s, %ld from us",
1092 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1093 prefixbuf, INET6_ADDRSTRLEN),
1094 pinfo->nd_opt_pi_prefix_len,
1095 rai->ifname, preferred_time,
1096 inet_ntop(AF_INET6, &from->sin6_addr,
1097 ntopbuf, INET6_ADDRSTRLEN),
1101 } else if (preferred_time != pp->preflifetime) {
1103 "<%s> prefeerred lifetime for %s/%d"
1104 " inconsistent on %s:"
1105 " %d from %s, %d from us",
1107 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1108 prefixbuf, INET6_ADDRSTRLEN),
1109 pinfo->nd_opt_pi_prefix_len,
1110 rai->ifname, preferred_time,
1111 inet_ntop(AF_INET6, &from->sin6_addr,
1112 ntopbuf, INET6_ADDRSTRLEN),
1116 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1117 if (pp->vltimeexpire) {
1118 gettimeofday(&now, NULL);
1119 valid_time += now.tv_sec;
1121 if (rai->clockskew &&
1122 abs(valid_time - pp->vltimeexpire) > rai->clockskew) {
1124 "<%s> valid lifetime for %s/%d"
1125 " (decr. in real time) inconsistent on %s:"
1126 " %d from %s, %ld from us",
1128 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1129 prefixbuf, INET6_ADDRSTRLEN),
1130 pinfo->nd_opt_pi_prefix_len,
1131 rai->ifname, preferred_time,
1132 inet_ntop(AF_INET6, &from->sin6_addr,
1133 ntopbuf, INET6_ADDRSTRLEN),
1137 } else if (valid_time != pp->validlifetime) {
1139 "<%s> valid lifetime for %s/%d"
1140 " inconsistent on %s:"
1141 " %d from %s, %d from us",
1143 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1144 prefixbuf, INET6_ADDRSTRLEN),
1145 pinfo->nd_opt_pi_prefix_len,
1146 rai->ifname, valid_time,
1147 inet_ntop(AF_INET6, &from->sin6_addr,
1148 ntopbuf, INET6_ADDRSTRLEN),
1153 return(inconsistent);
1157 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1160 int bytelen, bitlen;
1162 for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
1163 if (plen != pp->prefixlen)
1167 if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
1169 if (prefix->s6_addr[bytelen] >> (8 - bitlen) ==
1170 pp->prefix.s6_addr[bytelen] >> (8 - bitlen))
1177 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1179 prefix_match(struct in6_addr *p0, int plen0,
1180 struct in6_addr *p1, int plen1)
1182 int bytelen, bitlen;
1186 bytelen = plen1 / 8;
1188 if (memcmp((void *)p0, (void *)p1, bytelen))
1190 if (p0->s6_addr[bytelen] >> (8 - bitlen) ==
1191 p1->s6_addr[bytelen] >> (8 - bitlen))
1198 nd6_options(struct nd_opt_hdr *hdr, int limit,
1199 union nd_opts *ndopts, u_int32_t optflags)
1203 for (; limit > 0; limit -= optlen) {
1204 if (limit < sizeof(struct nd_opt_hdr)) {
1205 syslog(LOG_INFO, "<%s> short option header", __func__);
1209 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1210 if (hdr->nd_opt_len == 0) {
1212 "<%s> bad ND option length(0) (type = %d)",
1213 __func__, hdr->nd_opt_type);
1216 optlen = hdr->nd_opt_len << 3;
1217 if (optlen > limit) {
1218 syslog(LOG_INFO, "<%s> short option", __func__);
1222 if (hdr->nd_opt_type > ND_OPT_MTU) {
1224 "<%s> unknown ND option(type %d)",
1225 __func__, hdr->nd_opt_type);
1229 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1231 "<%s> unexpected ND option(type %d)",
1232 __func__, hdr->nd_opt_type);
1237 * Option length check. Do it here for all fixed-length
1240 if ((hdr->nd_opt_type == ND_OPT_MTU &&
1241 (optlen != sizeof(struct nd_opt_mtu))) ||
1242 ((hdr->nd_opt_type == ND_OPT_PREFIX_INFORMATION &&
1243 optlen != sizeof(struct nd_opt_prefix_info)))) {
1244 syslog(LOG_INFO, "<%s> invalid option length",
1249 switch (hdr->nd_opt_type) {
1250 case ND_OPT_SOURCE_LINKADDR:
1251 case ND_OPT_TARGET_LINKADDR:
1252 case ND_OPT_REDIRECTED_HEADER:
1253 break; /* we don't care about these options */
1255 if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
1257 "<%s> duplicated ND option (type = %d)",
1258 __func__, hdr->nd_opt_type);
1260 ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
1262 case ND_OPT_PREFIX_INFORMATION:
1264 struct nd_optlist *pfxlist;
1266 if (ndopts->nd_opts_pi == 0) {
1267 ndopts->nd_opts_pi =
1268 (struct nd_opt_prefix_info *)hdr;
1271 if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
1272 syslog(LOG_ERR, "<%s> can't allocate memory",
1276 pfxlist->next = ndopts->nd_opts_list;
1278 ndopts->nd_opts_list = pfxlist;
1282 default: /* impossible */
1290 free_ndopts(ndopts);
1296 free_ndopts(union nd_opts *ndopts)
1298 struct nd_optlist *opt = ndopts->nd_opts_list, *next;
1310 struct icmp6_filter filt;
1311 struct ipv6_mreq mreq;
1312 struct rainfo *ra = ralist;
1314 /* XXX: should be max MTU attached to the node */
1315 static u_char answer[1500];
1317 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1318 CMSG_SPACE(sizeof(int));
1319 rcvcmsgbuf = (u_char *)malloc(rcvcmsgbuflen);
1320 if (rcvcmsgbuf == NULL) {
1321 syslog(LOG_ERR, "<%s> not enough core", __func__);
1325 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1326 CMSG_SPACE(sizeof(int));
1327 sndcmsgbuf = (u_char *)malloc(sndcmsgbuflen);
1328 if (sndcmsgbuf == NULL) {
1329 syslog(LOG_ERR, "<%s> not enough core", __func__);
1333 if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1334 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1339 /* specify to tell receiving interface */
1341 #ifdef IPV6_RECVPKTINFO
1342 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1344 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s",
1345 __func__, strerror(errno));
1348 #else /* old adv. API */
1349 if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
1351 syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
1352 __func__, strerror(errno));
1358 /* specify to tell value of hoplimit field of received IP6 hdr */
1359 #ifdef IPV6_RECVHOPLIMIT
1360 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1362 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s",
1363 __func__, strerror(errno));
1366 #else /* old adv. API */
1367 if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
1369 syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
1370 __func__, strerror(errno));
1375 ICMP6_FILTER_SETBLOCKALL(&filt);
1376 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1377 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1379 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1380 if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1381 sizeof(filt)) < 0) {
1382 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1383 __func__, strerror(errno));
1388 * join all routers multicast address on each advertising interface.
1390 if (inet_pton(AF_INET6, ALLROUTERS_LINK,
1391 &mreq.ipv6mr_multiaddr.s6_addr)
1393 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1398 mreq.ipv6mr_interface = ra->ifindex;
1399 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
1400 sizeof(mreq)) < 0) {
1401 syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
1402 __func__, ra->ifname, strerror(errno));
1409 * When attending router renumbering, join all-routers site-local
1413 if (inet_pton(AF_INET6, ALLROUTERS_SITE,
1414 &in6a_site_allrouters) != 1) {
1415 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1419 mreq.ipv6mr_multiaddr = in6a_site_allrouters;
1421 if ((mreq.ipv6mr_interface = if_nametoindex(mcastif))
1424 "<%s> invalid interface: %s",
1429 mreq.ipv6mr_interface = ralist->ifindex;
1430 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1431 &mreq, sizeof(mreq)) < 0) {
1433 "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
1435 mcastif ? mcastif : ralist->ifname,
1441 /* initialize msghdr for receiving packets */
1442 rcviov[0].iov_base = (caddr_t)answer;
1443 rcviov[0].iov_len = sizeof(answer);
1444 rcvmhdr.msg_name = (caddr_t)&from;
1445 rcvmhdr.msg_namelen = sizeof(from);
1446 rcvmhdr.msg_iov = rcviov;
1447 rcvmhdr.msg_iovlen = 1;
1448 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1449 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1451 /* initialize msghdr for sending packets */
1452 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1453 sndmhdr.msg_iov = sndiov;
1454 sndmhdr.msg_iovlen = 1;
1455 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1456 sndmhdr.msg_controllen = sndcmsgbuflen;
1461 /* open a routing socket to watch the routing table */
1465 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1467 "<%s> socket: %s", __func__, strerror(errno));
1473 if_indextorainfo(int index)
1475 struct rainfo *rai = ralist;
1477 for (rai = ralist; rai; rai = rai->next) {
1478 if (rai->ifindex == index)
1482 return(NULL); /* search failed */
1486 ra_output(struct rainfo *rainfo)
1490 struct in6_pktinfo *pi;
1491 struct soliciter *sol, *nextsol;
1493 if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) {
1494 syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
1495 __func__, rainfo->ifname);
1499 make_packet(rainfo); /* XXX: inefficient */
1501 sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
1502 sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
1503 sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
1505 cm = CMSG_FIRSTHDR(&sndmhdr);
1506 /* specify the outgoing interface */
1507 cm->cmsg_level = IPPROTO_IPV6;
1508 cm->cmsg_type = IPV6_PKTINFO;
1509 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1510 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1511 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1512 pi->ipi6_ifindex = rainfo->ifindex;
1514 /* specify the hop limit of the packet */
1518 cm = CMSG_NXTHDR(&sndmhdr, cm);
1519 cm->cmsg_level = IPPROTO_IPV6;
1520 cm->cmsg_type = IPV6_HOPLIMIT;
1521 cm->cmsg_len = CMSG_LEN(sizeof(int));
1522 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1526 "<%s> send RA on %s, # of waitings = %d",
1527 __func__, rainfo->ifname, rainfo->waiting);
1529 i = sendmsg(sock, &sndmhdr, 0);
1531 if (i < 0 || i != rainfo->ra_datalen) {
1533 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1534 __func__, rainfo->ifname,
1540 * unicast advertisements
1541 * XXX commented out. reason: though spec does not forbit it, unicast
1542 * advert does not really help
1544 for (sol = rainfo->soliciter; sol; sol = nextsol) {
1545 nextsol = sol->next;
1548 sndmhdr.msg_name = (caddr_t)&sol->addr;
1549 i = sendmsg(sock, &sndmhdr, 0);
1550 if (i < 0 || i != rainfo->ra_datalen) {
1553 "<%s> unicast sendmsg on %s: %s",
1554 __func__, rainfo->ifname,
1563 rainfo->soliciter = NULL;
1565 /* update counter */
1566 if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
1567 rainfo->initcounter++;
1570 /* update timestamp */
1571 gettimeofday(&rainfo->lastsent, NULL);
1573 /* reset waiting conter */
1574 rainfo->waiting = 0;
1577 /* process RA timer */
1578 struct rtadvd_timer *
1579 ra_timeout(void *data)
1581 struct rainfo *rai = (struct rainfo *)data;
1584 /* if necessary, reconstruct the packet. */
1588 "<%s> RA timer on %s is expired",
1589 __func__, rai->ifname);
1596 /* update RA timer */
1598 ra_timer_update(void *data, struct timeval *tm)
1600 struct rainfo *rai = (struct rainfo *)data;
1604 * Whenever a multicast advertisement is sent from an interface,
1605 * the timer is reset to a uniformly-distributed random value
1606 * between the interface's configured MinRtrAdvInterval and
1607 * MaxRtrAdvInterval (RFC2461 6.2.4).
1609 interval = rai->mininterval;
1610 interval += random() % (rai->maxinterval - rai->mininterval);
1613 * For the first few advertisements (up to
1614 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1615 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1616 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1619 if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
1620 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1621 interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1623 tm->tv_sec = interval;
1627 "<%s> RA timer on %s is set to %ld:%ld",
1628 __func__, rai->ifname,
1629 (long int)tm->tv_sec, (long int)tm->tv_usec);