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 $
32 * $DragonFly: src/usr.sbin/rtadvd/rtadvd.c,v 1.3 2003/11/03 19:31:42 eirikn Exp $
35 #include <sys/param.h>
36 #include <sys/socket.h>
39 #include <sys/queue.h>
42 #include <net/route.h>
43 #include <net/if_dl.h>
44 #include <netinet/in.h>
45 #include <netinet/ip6.h>
46 #include <netinet6/ip6_var.h>
47 #include <netinet/icmp6.h>
49 #include <arpa/inet.h>
68 struct msghdr rcvmhdr;
69 static u_char *rcvcmsgbuf;
70 static size_t rcvcmsgbuflen;
71 static u_char *sndcmsgbuf = NULL;
72 static size_t sndcmsgbuflen;
75 struct msghdr sndmhdr;
76 struct iovec rcviov[2];
77 struct iovec sndiov[2];
78 struct sockaddr_in6 from;
79 struct sockaddr_in6 sin6_allnodes = {sizeof(sin6_allnodes), AF_INET6};
80 struct in6_addr in6a_site_allrouters;
81 static char *dumpfilename = "/var/run/rtadvd.dump"; /* XXX: should be configurable */
82 static char *pidfilename = "/var/run/rtadvd.pid"; /* should be configurable */
90 int dflag = 0, sflag = 0;
92 u_char *conffile = NULL;
94 struct rainfo *ralist = NULL;
96 struct nd_optlist *next;
97 struct nd_opt_hdr *opt;
100 struct nd_opt_hdr *nd_opt_array[7];
102 struct nd_opt_hdr *zero;
103 struct nd_opt_hdr *src_lladdr;
104 struct nd_opt_hdr *tgt_lladdr;
105 struct nd_opt_prefix_info *pi;
106 struct nd_opt_rd_hdr *rh;
107 struct nd_opt_mtu *mtu;
108 struct nd_optlist *list;
111 #define nd_opts_src_lladdr nd_opt_each.src_lladdr
112 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr
113 #define nd_opts_pi nd_opt_each.pi
114 #define nd_opts_rh nd_opt_each.rh
115 #define nd_opts_mtu nd_opt_each.mtu
116 #define nd_opts_list nd_opt_each.list
118 #define NDOPT_FLAG_SRCLINKADDR 0x1
119 #define NDOPT_FLAG_TGTLINKADDR 0x2
120 #define NDOPT_FLAG_PREFIXINFO 0x4
121 #define NDOPT_FLAG_RDHDR 0x8
122 #define NDOPT_FLAG_MTU 0x10
124 u_int32_t ndopt_flags[] = {
125 0, NDOPT_FLAG_SRCLINKADDR, NDOPT_FLAG_TGTLINKADDR,
126 NDOPT_FLAG_PREFIXINFO, NDOPT_FLAG_RDHDR, NDOPT_FLAG_MTU
129 int main(int, char *[]);
130 static void set_die(int);
131 static void die(void);
132 static void sock_open(void);
133 static void rtsock_open(void);
134 static void rtadvd_input(void);
135 static void rs_input(int, struct nd_router_solicit *,
136 struct in6_pktinfo *, struct sockaddr_in6 *);
137 static void ra_input(int, struct nd_router_advert *,
138 struct in6_pktinfo *, struct sockaddr_in6 *);
139 static int prefix_check(struct nd_opt_prefix_info *, struct rainfo *,
140 struct sockaddr_in6 *);
141 static int nd6_options(struct nd_opt_hdr *, int,
142 union nd_opts *, u_int32_t);
143 static void free_ndopts(union nd_opts *);
144 static void ra_output(struct rainfo *);
145 static void rtmsg_input(void);
146 static void rtadvd_set_dump_file(void);
155 struct timeval *timeout;
161 openlog("rtadvd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
163 /* get command line options and arguments */
165 #define OPTIONS "c:dDfM:mRs"
167 #define OPTIONS "c:dDfM:Rs"
169 while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
193 fprintf(stderr, "rtadvd: "
194 "the -R option is currently ignored.\n");
208 "usage: rtadvd [-dDfMmRs] [-c conffile] "
210 "usage: rtadvd [-dDfMRs] [-c conffile] "
218 (void)setlogmask(LOG_UPTO(LOG_ERR));
220 (void)setlogmask(LOG_UPTO(LOG_INFO));
222 /* timer initialization */
225 /* random value initialization */
226 srandom((u_long)time(NULL));
228 /* get iflist block from kernel */
234 if (inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr) != 1) {
235 fprintf(stderr, "fatal: inet_pton failed\n");
243 /* record the current PID */
245 if ((pidfp = fopen(pidfilename, "w")) == NULL) {
247 "<%s> failed to open a log file(%s), run anyway.",
248 __FUNCTION__, pidfilename);
250 fprintf(pidfp, "%d\n", pid);
255 FD_SET(sock, &fdset);
259 FD_SET(rtsock, &fdset);
265 signal(SIGTERM, (void *)set_die);
266 signal(SIGUSR1, (void *)rtadvd_set_dump_file);
269 struct fd_set select_fd = fdset; /* reinitialize */
271 if (do_dump) { /* SIGUSR1 */
273 rtadvd_dump_file(dumpfilename);
281 /* timer expiration check and reset the timer */
282 timeout = rtadvd_check_timer();
284 if (timeout != NULL) {
286 "<%s> set timer to %ld:%ld. waiting for "
287 "inputs or timeout", __FUNCTION__,
288 (long int)timeout->tv_sec,
289 (long int)timeout->tv_usec);
292 "<%s> there's no timer. waiting for inputs",
296 if ((i = select(maxfd + 1, &select_fd,
297 NULL, NULL, timeout)) < 0) {
298 /* EINTR would occur upon SIGUSR1 for status dump */
300 syslog(LOG_ERR, "<%s> select: %s",
301 __FUNCTION__, strerror(errno));
304 if (i == 0) /* timeout */
306 if (rtsock != -1 && FD_ISSET(rtsock, &select_fd))
308 if (FD_ISSET(sock, &select_fd))
311 exit(0); /* NOTREACHED */
315 rtadvd_set_dump_file()
332 const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
335 syslog(LOG_DEBUG, "<%s> cease to be an advertising router\n",
339 for (ra = ralist; ra; ra = ra->next) {
343 for (i = 0; i < retrans; i++) {
344 for (ra = ralist; ra; ra = ra->next)
346 sleep(MIN_DELAY_BETWEEN_RAS);
355 int n, type, ifindex = 0, plen;
357 char msg[2048], *next, *lim;
358 u_char ifname[IF_NAMESIZE];
359 struct prefix *prefix;
361 struct in6_addr *addr;
362 char addrbuf[INET6_ADDRSTRLEN];
364 n = read(rtsock, msg, sizeof(msg));
366 syslog(LOG_DEBUG, "<%s> received a routing message "
367 "(type = %d, len = %d)", __FUNCTION__, rtmsg_type(msg), n);
369 if (n > rtmsg_len(msg)) {
371 * This usually won't happen for messages received on
376 "<%s> received data length is larger than "
377 "1st routing message len. multiple messages? "
378 "read %d bytes, but 1st msg len = %d",
379 __FUNCTION__, n, rtmsg_len(msg));
387 for (next = msg; next < lim; next += len) {
390 next = get_next_msg(next, lim, 0, &len,
391 RTADV_TYPE2BITMASK(RTM_ADD) |
392 RTADV_TYPE2BITMASK(RTM_DELETE) |
393 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
394 RTADV_TYPE2BITMASK(RTM_DELADDR) |
395 RTADV_TYPE2BITMASK(RTM_IFINFO));
398 type = rtmsg_type(next);
402 ifindex = get_rtm_ifindex(next);
406 ifindex = get_ifam_ifindex(next);
409 ifindex = get_ifm_ifindex(next);
412 /* should not reach here */
415 "<%s:%d> unknown rtmsg %d on %s",
416 __FUNCTION__, __LINE__, type,
417 if_indextoname(ifindex, ifname));
422 if ((rai = if_indextorainfo(ifindex)) == NULL) {
425 "<%s> route changed on "
426 "non advertising interface(%s)",
428 if_indextoname(ifindex, ifname));
432 oldifflags = iflist[ifindex]->ifm_flags;
436 /* init ifflags because it may have changed */
437 iflist[ifindex]->ifm_flags =
438 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
441 break; /* we aren't interested in prefixes */
443 addr = get_addr(msg);
444 plen = get_prefixlen(msg);
445 /* sanity check for plen */
446 /* as RFC2373, prefixlen is at least 4 */
447 if (plen < 4 || plen > 127) {
448 syslog(LOG_INFO, "<%s> new interface route's"
449 "plen %d is invalid for a prefix",
453 prefix = find_prefix(rai, addr, plen);
457 * If the prefix has been invalidated,
458 * make it available again.
460 update_prefix(prefix);
461 } else if (dflag > 1) {
463 "<%s> new prefix(%s/%d) "
465 "but it was already in list",
467 inet_ntop(AF_INET6, addr,
468 (char *)addrbuf, INET6_ADDRSTRLEN),
473 make_prefix(rai, ifindex, addr, plen);
476 /* init ifflags because it may have changed */
477 iflist[ifindex]->ifm_flags =
478 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
483 addr = get_addr(msg);
484 plen = get_prefixlen(msg);
485 /* sanity check for plen */
486 /* as RFC2373, prefixlen is at least 4 */
487 if (plen < 4 || plen > 127) {
489 "<%s> deleted interface route's "
490 "plen %d is invalid for a prefix",
494 prefix = find_prefix(rai, addr, plen);
495 if (prefix == NULL) {
498 "<%s> prefix(%s/%d) was "
500 "but it was not in list",
502 inet_ntop(AF_INET6, addr,
503 (char *)addrbuf, INET6_ADDRSTRLEN),
508 invalidate_prefix(prefix);
512 /* init ifflags because it may have changed */
513 iflist[ifindex]->ifm_flags =
514 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
517 iflist[ifindex]->ifm_flags = get_ifm_flags(next);
520 /* should not reach here */
523 "<%s:%d> unknown rtmsg %d on %s",
524 __FUNCTION__, __LINE__, type,
525 if_indextoname(ifindex, ifname));
530 /* check if an interface flag is changed */
531 if ((oldifflags & IFF_UP) != 0 && /* UP to DOWN */
532 (iflist[ifindex]->ifm_flags & IFF_UP) == 0) {
534 "<%s> interface %s becomes down. stop timer.",
535 __FUNCTION__, rai->ifname);
536 rtadvd_remove_timer(&rai->timer);
537 } else if ((oldifflags & IFF_UP) == 0 && /* DOWN to UP */
538 (iflist[ifindex]->ifm_flags & IFF_UP) != 0) {
540 "<%s> interface %s becomes up. restart timer.",
541 __FUNCTION__, rai->ifname);
543 rai->initcounter = 0; /* reset the counter */
544 rai->waiting = 0; /* XXX */
545 rai->timer = rtadvd_add_timer(ra_timeout,
546 ra_timer_update, rai, rai);
547 ra_timer_update((void *)rai, &rai->timer->tm);
548 rtadvd_set_timer(&rai->timer->tm, rai->timer);
563 struct icmp6_hdr *icp;
566 struct in6_pktinfo *pi = NULL;
567 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
568 struct in6_addr dst = in6addr_any;
571 * Get message. We reset msg_controllen since the field could
572 * be modified if we had received a message before setting
575 rcvmhdr.msg_controllen = rcvcmsgbuflen;
576 if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
579 /* extract optional information via Advanced API */
580 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
582 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
583 if (cm->cmsg_level == IPPROTO_IPV6 &&
584 cm->cmsg_type == IPV6_PKTINFO &&
585 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
586 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
587 ifindex = pi->ipi6_ifindex;
590 if (cm->cmsg_level == IPPROTO_IPV6 &&
591 cm->cmsg_type == IPV6_HOPLIMIT &&
592 cm->cmsg_len == CMSG_LEN(sizeof(int)))
593 hlimp = (int *)CMSG_DATA(cm);
597 "<%s> failed to get receiving interface",
603 "<%s> failed to get receiving hop limit",
609 * If we happen to receive data on an interface which is now down,
610 * just discard the data.
612 if ((iflist[pi->ipi6_ifindex]->ifm_flags & IFF_UP) == 0) {
614 "<%s> received data on a disabled interface (%s)",
616 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
621 if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
623 "<%s> packet size(%d) is too short",
628 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
629 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
631 if (i < sizeof(struct icmp6_hdr)) {
633 "<%s> packet size(%d) is too short",
638 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
641 switch (icp->icmp6_type) {
642 case ND_ROUTER_SOLICIT:
644 * Message verification - RFC-2461 6.1.1
645 * XXX: these checks must be done in the kernel as well,
646 * but we can't completely rely on them.
650 "<%s> RS with invalid hop limit(%d) "
651 "received from %s on %s",
652 __FUNCTION__, *hlimp,
653 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
655 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
658 if (icp->icmp6_code) {
660 "<%s> RS with invalid ICMP6 code(%d) "
661 "received from %s on %s",
662 __FUNCTION__, icp->icmp6_code,
663 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
665 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
668 if (i < sizeof(struct nd_router_solicit)) {
670 "<%s> RS from %s on %s does not have enough "
673 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
675 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
678 rs_input(i, (struct nd_router_solicit *)icp, pi, &from);
680 case ND_ROUTER_ADVERT:
682 * Message verification - RFC-2461 6.1.2
683 * XXX: there's a same dilemma as above...
687 "<%s> RA with invalid hop limit(%d) "
688 "received from %s on %s",
689 __FUNCTION__, *hlimp,
690 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
692 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
695 if (icp->icmp6_code) {
697 "<%s> RA with invalid ICMP6 code(%d) "
698 "received from %s on %s",
699 __FUNCTION__, icp->icmp6_code,
700 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
702 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
705 if (i < sizeof(struct nd_router_advert)) {
707 "<%s> RA from %s on %s does not have enough "
710 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
712 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
715 ra_input(i, (struct nd_router_advert *)icp, pi, &from);
717 case ICMP6_ROUTER_RENUMBERING:
718 if (accept_rr == 0) {
719 syslog(LOG_ERR, "<%s> received a router renumbering "
720 "message, but not allowed to be accepted",
724 rr_input(i, (struct icmp6_router_renum *)icp, pi, &from,
729 * Note that this case is POSSIBLE, especially just
730 * after invocation of the daemon. This is because we
731 * could receive message after opening the socket and
732 * before setting ICMP6 type filter(see sock_open()).
734 syslog(LOG_ERR, "<%s> invalid icmp type(%d)",
735 __FUNCTION__, icp->icmp6_type);
743 rs_input(int len, struct nd_router_solicit *rs,
744 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
746 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
747 union nd_opts ndopts;
751 "<%s> RS received from %s on %s",
753 inet_ntop(AF_INET6, &from->sin6_addr,
754 ntopbuf, INET6_ADDRSTRLEN),
755 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
757 /* ND option check */
758 memset(&ndopts, 0, sizeof(ndopts));
759 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
760 len - sizeof(struct nd_router_solicit),
761 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
763 "<%s> ND option check failed for an RS from %s on %s",
765 inet_ntop(AF_INET6, &from->sin6_addr,
766 ntopbuf, INET6_ADDRSTRLEN),
767 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
772 * If the IP source address is the unspecified address, there
773 * must be no source link-layer address option in the message.
776 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
777 ndopts.nd_opts_src_lladdr) {
779 "<%s> RS from unspecified src on %s has a link-layer"
782 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
788 if (pi->ipi6_ifindex == ra->ifindex)
794 "<%s> RS received on non advertising interface(%s)",
796 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
800 ra->rsinput++; /* increment statistics */
803 * Decide whether to send RA according to the rate-limit
807 long delay; /* must not be greater than 1000000 */
808 struct timeval interval, now, min_delay, tm_tmp, *rest;
809 struct soliciter *sol;
812 * record sockaddr waiting for RA, if possible
814 sol = (struct soliciter *)malloc(sizeof(*sol));
817 /*XXX RFC2553 need clarification on flowinfo */
818 sol->addr.sin6_flowinfo = 0;
819 sol->next = ra->soliciter;
820 ra->soliciter = sol->next;
824 * If there is already a waiting RS packet, don't
831 * Compute a random delay. If the computed value
832 * corresponds to a time later than the time the next
833 * multicast RA is scheduled to be sent, ignore the random
834 * delay and send the advertisement at the
835 * already-scheduled time. RFC-2461 6.2.6
837 delay = random() % MAX_RA_DELAY_TIME;
839 interval.tv_usec = delay;
840 rest = rtadvd_timer_rest(ra->timer);
841 if (TIMEVAL_LT(*rest, interval)) {
843 "<%s> random delay is larger than "
844 "the rest of normal timer",
850 * If we sent a multicast Router Advertisement within
851 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
852 * the advertisement to be sent at a time corresponding to
853 * MIN_DELAY_BETWEEN_RAS plus the random value after the
854 * previous advertisement was sent.
856 gettimeofday(&now, NULL);
857 TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp);
858 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
859 min_delay.tv_usec = 0;
860 if (TIMEVAL_LT(tm_tmp, min_delay)) {
861 TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
862 TIMEVAL_ADD(&min_delay, &interval, &interval);
864 rtadvd_set_timer(&interval, ra->timer);
869 free_ndopts(&ndopts);
874 ra_input(int len, struct nd_router_advert *ra,
875 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
878 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
879 union nd_opts ndopts;
880 char *on_off[] = {"OFF", "ON"};
881 u_int32_t reachabletime, retranstimer, mtu;
882 int inconsistent = 0;
885 "<%s> RA received from %s on %s",
887 inet_ntop(AF_INET6, &from->sin6_addr,
888 ntopbuf, INET6_ADDRSTRLEN),
889 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
891 /* ND option check */
892 memset(&ndopts, 0, sizeof(ndopts));
893 if (nd6_options((struct nd_opt_hdr *)(ra + 1),
894 len - sizeof(struct nd_router_advert),
895 &ndopts, NDOPT_FLAG_SRCLINKADDR |
896 NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
898 "<%s> ND option check failed for an RA from %s on %s",
900 inet_ntop(AF_INET6, &from->sin6_addr,
901 ntopbuf, INET6_ADDRSTRLEN),
902 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
907 * RA consistency check according to RFC-2461 6.2.7
909 if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == 0) {
911 "<%s> received RA from %s on non-advertising"
914 inet_ntop(AF_INET6, &from->sin6_addr,
915 ntopbuf, INET6_ADDRSTRLEN),
916 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
919 rai->rainput++; /* increment statistics */
921 /* Cur Hop Limit value */
922 if (ra->nd_ra_curhoplimit && rai->hoplimit &&
923 ra->nd_ra_curhoplimit != rai->hoplimit) {
925 "<%s> CurHopLimit inconsistent on %s:"
926 " %d from %s, %d from us",
929 ra->nd_ra_curhoplimit,
930 inet_ntop(AF_INET6, &from->sin6_addr,
931 ntopbuf, INET6_ADDRSTRLEN),
936 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
939 "<%s> M flag inconsistent on %s:"
940 " %s from %s, %s from us",
943 on_off[!rai->managedflg],
944 inet_ntop(AF_INET6, &from->sin6_addr,
945 ntopbuf, INET6_ADDRSTRLEN),
946 on_off[rai->managedflg]);
950 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
953 "<%s> O flag inconsistent on %s:"
954 " %s from %s, %s from us",
957 on_off[!rai->otherflg],
958 inet_ntop(AF_INET6, &from->sin6_addr,
959 ntopbuf, INET6_ADDRSTRLEN),
960 on_off[rai->otherflg]);
964 reachabletime = ntohl(ra->nd_ra_reachable);
965 if (reachabletime && rai->reachabletime &&
966 reachabletime != rai->reachabletime) {
968 "<%s> ReachableTime inconsistent on %s:"
969 " %d from %s, %d from us",
973 inet_ntop(AF_INET6, &from->sin6_addr,
974 ntopbuf, INET6_ADDRSTRLEN),
979 retranstimer = ntohl(ra->nd_ra_retransmit);
980 if (retranstimer && rai->retranstimer &&
981 retranstimer != rai->retranstimer) {
983 "<%s> RetranceTimer inconsistent on %s:"
984 " %d from %s, %d from us",
988 inet_ntop(AF_INET6, &from->sin6_addr,
989 ntopbuf, INET6_ADDRSTRLEN),
993 /* Values in the MTU options */
994 if (ndopts.nd_opts_mtu) {
995 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
996 if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
998 "<%s> MTU option value inconsistent on %s:"
999 " %d from %s, %d from us",
1002 inet_ntop(AF_INET6, &from->sin6_addr,
1003 ntopbuf, INET6_ADDRSTRLEN),
1008 /* Preferred and Valid Lifetimes for prefixes */
1010 struct nd_optlist *optp = ndopts.nd_opts_list;
1012 if (ndopts.nd_opts_pi) {
1013 if (prefix_check(ndopts.nd_opts_pi, rai, from))
1017 if (prefix_check((struct nd_opt_prefix_info *)optp->opt,
1025 rai->rainconsistent++;
1028 free_ndopts(&ndopts);
1032 /* return a non-zero value if the received prefix is inconsitent with ours */
1034 prefix_check(struct nd_opt_prefix_info *pinfo,
1035 struct rainfo *rai, struct sockaddr_in6 *from)
1037 u_int32_t preferred_time, valid_time;
1039 int inconsistent = 0;
1040 u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
1043 #if 0 /* impossible */
1044 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1049 * log if the adveritsed prefix has link-local scope(sanity check?)
1051 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
1053 "<%s> link-local prefix %s/%d is advertised "
1056 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1057 prefixbuf, INET6_ADDRSTRLEN),
1058 pinfo->nd_opt_pi_prefix_len,
1059 inet_ntop(AF_INET6, &from->sin6_addr,
1060 ntopbuf, INET6_ADDRSTRLEN),
1064 if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1065 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1067 "<%s> prefix %s/%d from %s on %s is not in our list",
1069 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1070 prefixbuf, INET6_ADDRSTRLEN),
1071 pinfo->nd_opt_pi_prefix_len,
1072 inet_ntop(AF_INET6, &from->sin6_addr,
1073 ntopbuf, INET6_ADDRSTRLEN),
1078 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1079 if (pp->pltimeexpire) {
1081 * The lifetime is decremented in real time, so we should
1082 * compare the expiration time.
1083 * (RFC 2461 Section 6.2.7.)
1084 * XXX: can we really expect that all routers on the link
1085 * have synchronized clocks?
1087 gettimeofday(&now, NULL);
1088 preferred_time += now.tv_sec;
1090 if (rai->clockskew &&
1091 abs(preferred_time - pp->pltimeexpire) > rai->clockskew) {
1093 "<%s> prefeerred lifetime for %s/%d"
1094 " (decr. in real time) inconsistent on %s:"
1095 " %d from %s, %ld from us",
1097 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1098 prefixbuf, INET6_ADDRSTRLEN),
1099 pinfo->nd_opt_pi_prefix_len,
1100 rai->ifname, preferred_time,
1101 inet_ntop(AF_INET6, &from->sin6_addr,
1102 ntopbuf, INET6_ADDRSTRLEN),
1106 } else if (preferred_time != pp->preflifetime) {
1108 "<%s> prefeerred lifetime for %s/%d"
1109 " inconsistent on %s:"
1110 " %d from %s, %d from us",
1112 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1113 prefixbuf, INET6_ADDRSTRLEN),
1114 pinfo->nd_opt_pi_prefix_len,
1115 rai->ifname, preferred_time,
1116 inet_ntop(AF_INET6, &from->sin6_addr,
1117 ntopbuf, INET6_ADDRSTRLEN),
1121 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1122 if (pp->vltimeexpire) {
1123 gettimeofday(&now, NULL);
1124 valid_time += now.tv_sec;
1126 if (rai->clockskew &&
1127 abs(valid_time - pp->vltimeexpire) > rai->clockskew) {
1129 "<%s> valid lifetime for %s/%d"
1130 " (decr. in real time) inconsistent on %s:"
1131 " %d from %s, %ld from us",
1133 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1134 prefixbuf, INET6_ADDRSTRLEN),
1135 pinfo->nd_opt_pi_prefix_len,
1136 rai->ifname, preferred_time,
1137 inet_ntop(AF_INET6, &from->sin6_addr,
1138 ntopbuf, INET6_ADDRSTRLEN),
1142 } else if (valid_time != pp->validlifetime) {
1144 "<%s> valid lifetime for %s/%d"
1145 " inconsistent on %s:"
1146 " %d from %s, %d from us",
1148 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1149 prefixbuf, INET6_ADDRSTRLEN),
1150 pinfo->nd_opt_pi_prefix_len,
1151 rai->ifname, valid_time,
1152 inet_ntop(AF_INET6, &from->sin6_addr,
1153 ntopbuf, INET6_ADDRSTRLEN),
1158 return(inconsistent);
1162 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1165 int bytelen, bitlen;
1167 for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
1168 if (plen != pp->prefixlen)
1172 if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
1174 if (prefix->s6_addr[bytelen] >> (8 - bitlen) ==
1175 pp->prefix.s6_addr[bytelen] >> (8 - bitlen))
1182 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1184 prefix_match(struct in6_addr *p0, int plen0,
1185 struct in6_addr *p1, int plen1)
1187 int bytelen, bitlen;
1191 bytelen = plen1 / 8;
1193 if (memcmp((void *)p0, (void *)p1, bytelen))
1195 if (p0->s6_addr[bytelen] >> (8 - bitlen) ==
1196 p1->s6_addr[bytelen] >> (8 - bitlen))
1203 nd6_options(struct nd_opt_hdr *hdr, int limit,
1204 union nd_opts *ndopts, u_int32_t optflags)
1208 for (; limit > 0; limit -= optlen) {
1209 if (limit < sizeof(struct nd_opt_hdr)) {
1210 syslog(LOG_INFO, "<%s> short option header", __FUNCTION__);
1214 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1215 if (hdr->nd_opt_len == 0) {
1217 "<%s> bad ND option length(0) (type = %d)",
1218 __FUNCTION__, hdr->nd_opt_type);
1221 optlen = hdr->nd_opt_len << 3;
1222 if (optlen > limit) {
1223 syslog(LOG_INFO, "<%s> short option", __FUNCTION__);
1227 if (hdr->nd_opt_type > ND_OPT_MTU) {
1229 "<%s> unknown ND option(type %d)",
1230 __FUNCTION__, hdr->nd_opt_type);
1234 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1236 "<%s> unexpected ND option(type %d)",
1237 __FUNCTION__, hdr->nd_opt_type);
1242 * Option length check. Do it here for all fixed-length
1245 if ((hdr->nd_opt_type == ND_OPT_MTU &&
1246 (optlen != sizeof(struct nd_opt_mtu))) ||
1247 ((hdr->nd_opt_type == ND_OPT_PREFIX_INFORMATION &&
1248 optlen != sizeof(struct nd_opt_prefix_info)))) {
1249 syslog(LOG_INFO, "<%s> invalid option length",
1254 switch (hdr->nd_opt_type) {
1255 case ND_OPT_SOURCE_LINKADDR:
1256 case ND_OPT_TARGET_LINKADDR:
1257 case ND_OPT_REDIRECTED_HEADER:
1258 break; /* we don't care about these options */
1260 if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
1262 "<%s> duplicated ND option (type = %d)",
1263 __FUNCTION__, hdr->nd_opt_type);
1265 ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
1267 case ND_OPT_PREFIX_INFORMATION:
1269 struct nd_optlist *pfxlist;
1271 if (ndopts->nd_opts_pi == 0) {
1272 ndopts->nd_opts_pi =
1273 (struct nd_opt_prefix_info *)hdr;
1276 if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
1277 syslog(LOG_ERR, "<%s> can't allocate memory",
1281 pfxlist->next = ndopts->nd_opts_list;
1283 ndopts->nd_opts_list = pfxlist;
1287 default: /* impossible */
1295 free_ndopts(ndopts);
1301 free_ndopts(union nd_opts *ndopts)
1303 struct nd_optlist *opt = ndopts->nd_opts_list, *next;
1315 struct icmp6_filter filt;
1316 struct ipv6_mreq mreq;
1317 struct rainfo *ra = ralist;
1319 /* XXX: should be max MTU attached to the node */
1320 static u_char answer[1500];
1322 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1323 CMSG_SPACE(sizeof(int));
1324 rcvcmsgbuf = (u_char *)malloc(rcvcmsgbuflen);
1325 if (rcvcmsgbuf == NULL) {
1326 syslog(LOG_ERR, "<%s> not enough core", __FUNCTION__);
1330 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1331 CMSG_SPACE(sizeof(int));
1332 sndcmsgbuf = (u_char *)malloc(sndcmsgbuflen);
1333 if (sndcmsgbuf == NULL) {
1334 syslog(LOG_ERR, "<%s> not enough core", __FUNCTION__);
1338 if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1339 syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
1344 /* specify to tell receiving interface */
1346 #ifdef IPV6_RECVPKTINFO
1347 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1349 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s",
1350 __FUNCTION__, strerror(errno));
1353 #else /* old adv. API */
1354 if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
1356 syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
1357 __FUNCTION__, strerror(errno));
1363 /* specify to tell value of hoplimit field of received IP6 hdr */
1364 #ifdef IPV6_RECVHOPLIMIT
1365 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1367 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s",
1368 __FUNCTION__, strerror(errno));
1371 #else /* old adv. API */
1372 if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
1374 syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
1375 __FUNCTION__, strerror(errno));
1380 ICMP6_FILTER_SETBLOCKALL(&filt);
1381 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1382 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1384 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1385 if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1386 sizeof(filt)) < 0) {
1387 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1388 __FUNCTION__, strerror(errno));
1393 * join all routers multicast address on each advertising interface.
1395 if (inet_pton(AF_INET6, ALLROUTERS_LINK,
1396 &mreq.ipv6mr_multiaddr.s6_addr)
1398 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1403 mreq.ipv6mr_interface = ra->ifindex;
1404 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
1405 sizeof(mreq)) < 0) {
1406 syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
1407 __FUNCTION__, ra->ifname, strerror(errno));
1414 * When attending router renumbering, join all-routers site-local
1418 if (inet_pton(AF_INET6, ALLROUTERS_SITE,
1419 &in6a_site_allrouters) != 1) {
1420 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1424 mreq.ipv6mr_multiaddr = in6a_site_allrouters;
1426 if ((mreq.ipv6mr_interface = if_nametoindex(mcastif))
1429 "<%s> invalid interface: %s",
1430 __FUNCTION__, mcastif);
1434 mreq.ipv6mr_interface = ralist->ifindex;
1435 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1436 &mreq, sizeof(mreq)) < 0) {
1438 "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
1440 mcastif ? mcastif : ralist->ifname,
1446 /* initialize msghdr for receiving packets */
1447 rcviov[0].iov_base = (caddr_t)answer;
1448 rcviov[0].iov_len = sizeof(answer);
1449 rcvmhdr.msg_name = (caddr_t)&from;
1450 rcvmhdr.msg_namelen = sizeof(from);
1451 rcvmhdr.msg_iov = rcviov;
1452 rcvmhdr.msg_iovlen = 1;
1453 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1454 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1456 /* initialize msghdr for sending packets */
1457 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1458 sndmhdr.msg_iov = sndiov;
1459 sndmhdr.msg_iovlen = 1;
1460 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1461 sndmhdr.msg_controllen = sndcmsgbuflen;
1466 /* open a routing socket to watch the routing table */
1470 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1472 "<%s> socket: %s", __FUNCTION__, strerror(errno));
1478 if_indextorainfo(int index)
1480 struct rainfo *rai = ralist;
1482 for (rai = ralist; rai; rai = rai->next) {
1483 if (rai->ifindex == index)
1487 return(NULL); /* search failed */
1492 struct rainfo *rainfo;
1496 struct in6_pktinfo *pi;
1497 struct soliciter *sol, *nextsol;
1499 if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) {
1500 syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
1501 __FUNCTION__, rainfo->ifname);
1505 make_packet(rainfo); /* XXX: inefficient */
1507 sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
1508 sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
1509 sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
1511 cm = CMSG_FIRSTHDR(&sndmhdr);
1512 /* specify the outgoing interface */
1513 cm->cmsg_level = IPPROTO_IPV6;
1514 cm->cmsg_type = IPV6_PKTINFO;
1515 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1516 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1517 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1518 pi->ipi6_ifindex = rainfo->ifindex;
1520 /* specify the hop limit of the packet */
1524 cm = CMSG_NXTHDR(&sndmhdr, cm);
1525 cm->cmsg_level = IPPROTO_IPV6;
1526 cm->cmsg_type = IPV6_HOPLIMIT;
1527 cm->cmsg_len = CMSG_LEN(sizeof(int));
1528 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1532 "<%s> send RA on %s, # of waitings = %d",
1533 __FUNCTION__, rainfo->ifname, rainfo->waiting);
1535 i = sendmsg(sock, &sndmhdr, 0);
1537 if (i < 0 || i != rainfo->ra_datalen) {
1539 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1540 __FUNCTION__, rainfo->ifname,
1546 * unicast advertisements
1547 * XXX commented out. reason: though spec does not forbit it, unicast
1548 * advert does not really help
1550 for (sol = rainfo->soliciter; sol; sol = nextsol) {
1551 nextsol = sol->next;
1554 sndmhdr.msg_name = (caddr_t)&sol->addr;
1555 i = sendmsg(sock, &sndmhdr, 0);
1556 if (i < 0 || i != rainfo->ra_datalen) {
1559 "<%s> unicast sendmsg on %s: %s",
1560 __FUNCTION__, rainfo->ifname,
1569 rainfo->soliciter = NULL;
1571 /* update counter */
1572 if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
1573 rainfo->initcounter++;
1576 /* update timestamp */
1577 gettimeofday(&rainfo->lastsent, NULL);
1579 /* reset waiting conter */
1580 rainfo->waiting = 0;
1583 /* process RA timer */
1584 struct rtadvd_timer *
1585 ra_timeout(void *data)
1587 struct rainfo *rai = (struct rainfo *)data;
1590 /* if necessary, reconstruct the packet. */
1594 "<%s> RA timer on %s is expired",
1595 __FUNCTION__, rai->ifname);
1602 /* update RA timer */
1604 ra_timer_update(void *data, struct timeval *tm)
1606 struct rainfo *rai = (struct rainfo *)data;
1610 * Whenever a multicast advertisement is sent from an interface,
1611 * the timer is reset to a uniformly-distributed random value
1612 * between the interface's configured MinRtrAdvInterval and
1613 * MaxRtrAdvInterval (RFC2461 6.2.4).
1615 interval = rai->mininterval;
1616 interval += random() % (rai->maxinterval - rai->mininterval);
1619 * For the first few advertisements (up to
1620 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1621 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1622 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1625 if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
1626 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1627 interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1629 tm->tv_sec = interval;
1633 "<%s> RA timer on %s is set to %ld:%ld",
1634 __FUNCTION__, rai->ifname,
1635 (long int)tm->tv_sec, (long int)tm->tv_usec);