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.2 2001/07/03 11:02:14 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
__P((int, char *[]));
129 static void set_die
__P((int));
130 static void die
__P((void));
131 static void sock_open
__P((void));
132 static void rtsock_open
__P((void));
133 static void rtadvd_input
__P((void));
134 static void rs_input
__P((int, struct nd_router_solicit
*,
135 struct in6_pktinfo
*, struct sockaddr_in6
*));
136 static void ra_input
__P((int, struct nd_router_advert
*,
137 struct in6_pktinfo
*, struct sockaddr_in6
*));
138 static int prefix_check
__P((struct nd_opt_prefix_info
*, struct rainfo
*,
139 struct sockaddr_in6
*));
140 static int nd6_options
__P((struct nd_opt_hdr
*, int,
141 union nd_opts
*, u_int32_t
));
142 static void free_ndopts
__P((union nd_opts
*));
143 static void ra_output
__P((struct rainfo
*));
144 static void rtmsg_input
__P((void));
145 static void rtadvd_set_dump_file
__P((void));
147 struct prefix
*find_prefix
__P((struct rainfo
*, struct in6_addr
*, int));
156 struct timeval
*timeout
;
162 openlog("rtadvd", LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
164 /* get command line options and arguments */
166 #define OPTIONS "c:dDfM:mRs"
168 #define OPTIONS "c:dDfM:Rs"
170 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != -1) {
194 fprintf(stderr
, "rtadvd: "
195 "the -R option is currently ignored.\n");
209 "usage: rtadvd [-dDfMmRs] [-c conffile] "
211 "usage: rtadvd [-dDfMRs] [-c conffile] "
219 (void)setlogmask(LOG_UPTO(LOG_ERR
));
221 (void)setlogmask(LOG_UPTO(LOG_INFO
));
223 /* timer initialization */
226 /* random value initialization */
227 srandom((u_long
)time(NULL
));
229 /* get iflist block from kernel */
235 if (inet_pton(AF_INET6
, ALLNODES
, &sin6_allnodes
.sin6_addr
) != 1) {
236 fprintf(stderr
, "fatal: inet_pton failed\n");
244 /* record the current PID */
246 if ((pidfp
= fopen(pidfilename
, "w")) == NULL
)
248 "<%s> failed to open a log file(%s), run anyway.",
249 __FUNCTION__
, pidfilename
);
251 fprintf(pidfp
, "%d\n", pid
);
256 FD_SET(sock
, &fdset
);
260 FD_SET(rtsock
, &fdset
);
266 signal(SIGTERM
, (void *)set_die
);
267 signal(SIGUSR1
, (void *)rtadvd_set_dump_file
);
270 struct fd_set select_fd
= fdset
; /* reinitialize */
272 if (do_dump
) { /* SIGUSR1 */
274 rtadvd_dump_file(dumpfilename
);
282 /* timer expiration check and reset the timer */
283 timeout
= rtadvd_check_timer();
285 if (timeout
!= NULL
) {
287 "<%s> set timer to %ld:%ld. waiting for "
290 (long int)timeout
->tv_sec
,
291 (long int)timeout
->tv_usec
);
294 "<%s> there's no timer. waiting for inputs",
298 if ((i
= select(maxfd
+ 1, &select_fd
,
299 NULL
, NULL
, timeout
)) < 0) {
300 /* EINTR would occur upon SIGUSR1 for status dump */
302 syslog(LOG_ERR
, "<%s> select: %s",
303 __FUNCTION__
, strerror(errno
));
306 if (i
== 0) /* timeout */
308 if (rtsock
!= -1 && FD_ISSET(rtsock
, &select_fd
))
310 if (FD_ISSET(sock
, &select_fd
))
313 exit(0); /* NOTREACHED */
317 rtadvd_set_dump_file()
334 const int retrans
= MAX_FINAL_RTR_ADVERTISEMENTS
;
337 syslog(LOG_DEBUG
, "<%s> cease to be an advertising router\n",
341 for (ra
= ralist
; ra
; ra
= ra
->next
) {
345 for (i
= 0; i
< retrans
; i
++) {
346 for (ra
= ralist
; ra
; ra
= ra
->next
)
348 sleep(MIN_DELAY_BETWEEN_RAS
);
357 int n
, type
, ifindex
= 0, plen
;
359 char msg
[2048], *next
, *lim
;
360 u_char ifname
[IF_NAMESIZE
];
361 struct prefix
*prefix
;
363 struct in6_addr
*addr
;
364 char addrbuf
[INET6_ADDRSTRLEN
];
366 n
= read(rtsock
, msg
, sizeof(msg
));
369 "<%s> received a routing message "
370 "(type = %d, len = %d)",
374 if (n
> rtmsg_len(msg
)) {
376 * This usually won't happen for messages received on
381 "<%s> received data length is larger than"
382 "1st routing message len. multiple messages?"
383 " read %d bytes, but 1st msg len = %d",
384 __FUNCTION__
, n
, rtmsg_len(msg
));
392 for (next
= msg
; next
< lim
; next
+= len
) {
395 next
= get_next_msg(next
, lim
, 0, &len
,
396 RTADV_TYPE2BITMASK(RTM_ADD
) |
397 RTADV_TYPE2BITMASK(RTM_DELETE
) |
398 RTADV_TYPE2BITMASK(RTM_NEWADDR
) |
399 RTADV_TYPE2BITMASK(RTM_DELADDR
) |
400 RTADV_TYPE2BITMASK(RTM_IFINFO
));
403 type
= rtmsg_type(next
);
407 ifindex
= get_rtm_ifindex(next
);
411 ifindex
= get_ifam_ifindex(next
);
414 ifindex
= get_ifm_ifindex(next
);
417 /* should not reach here */
420 "<%s:%d> unknown rtmsg %d on %s",
421 __FUNCTION__
, __LINE__
, type
,
422 if_indextoname(ifindex
, ifname
));
427 if ((rai
= if_indextorainfo(ifindex
)) == NULL
) {
430 "<%s> route changed on "
431 "non advertising interface(%s)",
433 if_indextoname(ifindex
, ifname
));
437 oldifflags
= iflist
[ifindex
]->ifm_flags
;
441 /* init ifflags because it may have changed */
442 iflist
[ifindex
]->ifm_flags
=
444 iflist
[ifindex
]->ifm_flags
);
447 break; /* we aren't interested in prefixes */
449 addr
= get_addr(msg
);
450 plen
= get_prefixlen(msg
);
451 /* sanity check for plen */
452 if (plen
< 4 /* as RFC2373, prefixlen is at least 4 */
454 syslog(LOG_INFO
, "<%s> new interface route's"
455 "plen %d is invalid for a prefix",
459 prefix
= find_prefix(rai
, addr
, plen
);
463 "<%s> new prefix(%s/%d) "
465 "but it was already in list",
468 addr
, (char *)addrbuf
,
475 make_prefix(rai
, ifindex
, addr
, plen
);
478 /* init ifflags because it may have changed */
479 iflist
[ifindex
]->ifm_flags
=
481 iflist
[ifindex
]->ifm_flags
);
486 addr
= get_addr(msg
);
487 plen
= get_prefixlen(msg
);
488 /* sanity check for plen */
489 if (plen
< 4 /* as RFC2373, prefixlen is at least 4 */
491 syslog(LOG_INFO
, "<%s> deleted interface"
493 "plen %d is invalid for a prefix",
497 prefix
= find_prefix(rai
, addr
, plen
);
498 if (prefix
== NULL
) {
501 "<%s> prefix(%s/%d) was "
503 "but it was not in list",
506 addr
, (char *)addrbuf
,
513 delete_prefix(rai
, prefix
);
517 /* init ifflags because it may have changed */
518 iflist
[ifindex
]->ifm_flags
=
520 iflist
[ifindex
]->ifm_flags
);
523 iflist
[ifindex
]->ifm_flags
= get_ifm_flags(next
);
526 /* should not reach here */
529 "<%s:%d> unknown rtmsg %d on %s",
530 __FUNCTION__
, __LINE__
, type
,
531 if_indextoname(ifindex
, ifname
));
536 /* check if an interface flag is changed */
537 if ((oldifflags
& IFF_UP
) != 0 && /* UP to DOWN */
538 (iflist
[ifindex
]->ifm_flags
& IFF_UP
) == 0) {
540 "<%s> interface %s becomes down. stop timer.",
541 __FUNCTION__
, rai
->ifname
);
542 rtadvd_remove_timer(&rai
->timer
);
544 else if ((oldifflags
& IFF_UP
) == 0 && /* DOWN to UP */
545 (iflist
[ifindex
]->ifm_flags
& IFF_UP
) != 0) {
547 "<%s> interface %s becomes up. restart timer.",
548 __FUNCTION__
, rai
->ifname
);
550 rai
->initcounter
= 0; /* reset the counter */
551 rai
->waiting
= 0; /* XXX */
552 rai
->timer
= rtadvd_add_timer(ra_timeout
,
555 ra_timer_update((void *)rai
, &rai
->timer
->tm
);
556 rtadvd_set_timer(&rai
->timer
->tm
, rai
->timer
);
571 struct icmp6_hdr
*icp
;
574 struct in6_pktinfo
*pi
= NULL
;
575 u_char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
576 struct in6_addr dst
= in6addr_any
;
579 * Get message. We reset msg_controllen since the field could
580 * be modified if we had received a message before setting
583 rcvmhdr
.msg_controllen
= rcvcmsgbuflen
;
584 if ((i
= recvmsg(sock
, &rcvmhdr
, 0)) < 0)
587 /* extract optional information via Advanced API */
588 for (cm
= (struct cmsghdr
*)CMSG_FIRSTHDR(&rcvmhdr
);
590 cm
= (struct cmsghdr
*)CMSG_NXTHDR(&rcvmhdr
, cm
)) {
591 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
592 cm
->cmsg_type
== IPV6_PKTINFO
&&
593 cm
->cmsg_len
== CMSG_LEN(sizeof(struct in6_pktinfo
))) {
594 pi
= (struct in6_pktinfo
*)(CMSG_DATA(cm
));
595 ifindex
= pi
->ipi6_ifindex
;
598 if (cm
->cmsg_level
== IPPROTO_IPV6
&&
599 cm
->cmsg_type
== IPV6_HOPLIMIT
&&
600 cm
->cmsg_len
== CMSG_LEN(sizeof(int)))
601 hlimp
= (int *)CMSG_DATA(cm
);
605 "<%s> failed to get receiving interface",
611 "<%s> failed to get receiving hop limit",
617 * If we happen to receive data on an interface which is now down,
618 * just discard the data.
620 if ((iflist
[pi
->ipi6_ifindex
]->ifm_flags
& IFF_UP
) == 0) {
622 "<%s> received data on a disabled interface (%s)",
624 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
629 if (i
< sizeof(struct ip6_hdr
) + sizeof(struct icmp6_hdr
)) {
631 "<%s> packet size(%d) is too short",
636 ip
= (struct ip6_hdr
*)rcvmhdr
.msg_iov
[0].iov_base
;
637 icp
= (struct icmp6_hdr
*)(ip
+ 1); /* XXX: ext. hdr? */
639 if (i
< sizeof(struct icmp6_hdr
)) {
641 "<%s> packet size(%d) is too short",
646 icp
= (struct icmp6_hdr
*)rcvmhdr
.msg_iov
[0].iov_base
;
649 switch(icp
->icmp6_type
) {
650 case ND_ROUTER_SOLICIT
:
652 * Message verification - RFC-2461 6.1.1
653 * XXX: these checks must be done in the kernel as well,
654 * but we can't completely rely on them.
658 "<%s> RS with invalid hop limit(%d) "
659 "received from %s on %s",
660 __FUNCTION__
, *hlimp
,
661 inet_ntop(AF_INET6
, &from
.sin6_addr
, ntopbuf
,
663 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
666 if (icp
->icmp6_code
) {
668 "<%s> RS with invalid ICMP6 code(%d) "
669 "received from %s on %s",
670 __FUNCTION__
, icp
->icmp6_code
,
671 inet_ntop(AF_INET6
, &from
.sin6_addr
, ntopbuf
,
673 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
676 if (i
< sizeof(struct nd_router_solicit
)) {
678 "<%s> RS from %s on %s does not have enough "
681 inet_ntop(AF_INET6
, &from
.sin6_addr
, ntopbuf
,
683 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
), i
);
686 rs_input(i
, (struct nd_router_solicit
*)icp
, pi
, &from
);
688 case ND_ROUTER_ADVERT
:
690 * Message verification - RFC-2461 6.1.2
691 * XXX: there's a same dilemma as above...
695 "<%s> RA with invalid hop limit(%d) "
696 "received from %s on %s",
697 __FUNCTION__
, *hlimp
,
698 inet_ntop(AF_INET6
, &from
.sin6_addr
, ntopbuf
,
700 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
703 if (icp
->icmp6_code
) {
705 "<%s> RA with invalid ICMP6 code(%d) "
706 "received from %s on %s",
707 __FUNCTION__
, icp
->icmp6_code
,
708 inet_ntop(AF_INET6
, &from
.sin6_addr
, ntopbuf
,
710 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
713 if (i
< sizeof(struct nd_router_advert
)) {
715 "<%s> RA from %s on %s does not have enough "
718 inet_ntop(AF_INET6
, &from
.sin6_addr
, ntopbuf
,
720 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
), i
);
723 ra_input(i
, (struct nd_router_advert
*)icp
, pi
, &from
);
725 case ICMP6_ROUTER_RENUMBERING
:
726 if (accept_rr
== 0) {
728 "<%s> received a router renumbering "
729 "message, but not allowed to be accepted",
733 rr_input(i
, (struct icmp6_router_renum
*)icp
, pi
, &from
,
738 * Note that this case is POSSIBLE, especially just
739 * after invocation of the daemon. This is because we
740 * could receive message after opening the socket and
741 * before setting ICMP6 type filter(see sock_open()).
744 "<%s> invalid icmp type(%d)",
745 __FUNCTION__
, icp
->icmp6_type
);
753 rs_input(int len
, struct nd_router_solicit
*rs
,
754 struct in6_pktinfo
*pi
, struct sockaddr_in6
*from
)
756 u_char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
757 union nd_opts ndopts
;
761 "<%s> RS received from %s on %s",
763 inet_ntop(AF_INET6
, &from
->sin6_addr
,
764 ntopbuf
, INET6_ADDRSTRLEN
),
765 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
767 /* ND option check */
768 memset(&ndopts
, 0, sizeof(ndopts
));
769 if (nd6_options((struct nd_opt_hdr
*)(rs
+ 1),
770 len
- sizeof(struct nd_router_solicit
),
771 &ndopts
, NDOPT_FLAG_SRCLINKADDR
)) {
773 "<%s> ND option check failed for an RS from %s on %s",
775 inet_ntop(AF_INET6
, &from
->sin6_addr
,
776 ntopbuf
, INET6_ADDRSTRLEN
),
777 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
782 * If the IP source address is the unspecified address, there
783 * must be no source link-layer address option in the message.
786 if (IN6_IS_ADDR_UNSPECIFIED(&from
->sin6_addr
) &&
787 ndopts
.nd_opts_src_lladdr
) {
789 "<%s> RS from unspecified src on %s has a link-layer"
792 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
798 if (pi
->ipi6_ifindex
== ra
->ifindex
)
804 "<%s> RS received on non advertising interface(%s)",
806 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
810 ra
->rsinput
++; /* increment statistics */
813 * Decide whether to send RA according to the rate-limit
817 long delay
; /* must not be greater than 1000000 */
818 struct timeval interval
, now
, min_delay
, tm_tmp
, *rest
;
819 struct soliciter
*sol
;
822 * record sockaddr waiting for RA, if possible
824 sol
= (struct soliciter
*)malloc(sizeof(*sol
));
827 /*XXX RFC2553 need clarification on flowinfo */
828 sol
->addr
.sin6_flowinfo
= 0;
829 sol
->next
= ra
->soliciter
;
830 ra
->soliciter
= sol
->next
;
834 * If there is already a waiting RS packet, don't
841 * Compute a random delay. If the computed value
842 * corresponds to a time later than the time the next
843 * multicast RA is scheduled to be sent, ignore the random
844 * delay and send the advertisement at the
845 * already-scheduled time. RFC-2461 6.2.6
847 delay
= random() % MAX_RA_DELAY_TIME
;
849 interval
.tv_usec
= delay
;
850 rest
= rtadvd_timer_rest(ra
->timer
);
851 if (TIMEVAL_LT(*rest
, interval
)) {
853 "<%s> random delay is larger than "
854 "the rest of normal timer",
860 * If we sent a multicast Router Advertisement within
861 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
862 * the advertisement to be sent at a time corresponding to
863 * MIN_DELAY_BETWEEN_RAS plus the random value after the
864 * previous advertisement was sent.
866 gettimeofday(&now
, NULL
);
867 TIMEVAL_SUB(&now
, &ra
->lastsent
, &tm_tmp
);
868 min_delay
.tv_sec
= MIN_DELAY_BETWEEN_RAS
;
869 min_delay
.tv_usec
= 0;
870 if (TIMEVAL_LT(tm_tmp
, min_delay
)) {
871 TIMEVAL_SUB(&min_delay
, &tm_tmp
, &min_delay
);
872 TIMEVAL_ADD(&min_delay
, &interval
, &interval
);
874 rtadvd_set_timer(&interval
, ra
->timer
);
879 free_ndopts(&ndopts
);
884 ra_input(int len
, struct nd_router_advert
*ra
,
885 struct in6_pktinfo
*pi
, struct sockaddr_in6
*from
)
888 u_char ntopbuf
[INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
889 union nd_opts ndopts
;
890 char *on_off
[] = {"OFF", "ON"};
891 u_int32_t reachabletime
, retranstimer
, mtu
;
892 int inconsistent
= 0;
895 "<%s> RA received from %s on %s",
897 inet_ntop(AF_INET6
, &from
->sin6_addr
,
898 ntopbuf
, INET6_ADDRSTRLEN
),
899 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
901 /* ND option check */
902 memset(&ndopts
, 0, sizeof(ndopts
));
903 if (nd6_options((struct nd_opt_hdr
*)(ra
+ 1),
904 len
- sizeof(struct nd_router_advert
),
905 &ndopts
, NDOPT_FLAG_SRCLINKADDR
|
906 NDOPT_FLAG_PREFIXINFO
| NDOPT_FLAG_MTU
)) {
908 "<%s> ND option check failed for an RA from %s on %s",
910 inet_ntop(AF_INET6
, &from
->sin6_addr
,
911 ntopbuf
, INET6_ADDRSTRLEN
),
912 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
917 * RA consistency check according to RFC-2461 6.2.7
919 if ((rai
= if_indextorainfo(pi
->ipi6_ifindex
)) == 0) {
921 "<%s> received RA from %s on non-advertising"
924 inet_ntop(AF_INET6
, &from
->sin6_addr
,
925 ntopbuf
, INET6_ADDRSTRLEN
),
926 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
929 rai
->rainput
++; /* increment statistics */
931 /* Cur Hop Limit value */
932 if (ra
->nd_ra_curhoplimit
&& rai
->hoplimit
&&
933 ra
->nd_ra_curhoplimit
!= rai
->hoplimit
) {
935 "<%s> CurHopLimit inconsistent on %s:"
936 " %d from %s, %d from us",
939 ra
->nd_ra_curhoplimit
,
940 inet_ntop(AF_INET6
, &from
->sin6_addr
,
941 ntopbuf
, INET6_ADDRSTRLEN
),
946 if ((ra
->nd_ra_flags_reserved
& ND_RA_FLAG_MANAGED
) !=
949 "<%s> M flag inconsistent on %s:"
950 " %s from %s, %s from us",
953 on_off
[!rai
->managedflg
],
954 inet_ntop(AF_INET6
, &from
->sin6_addr
,
955 ntopbuf
, INET6_ADDRSTRLEN
),
956 on_off
[rai
->managedflg
]);
960 if ((ra
->nd_ra_flags_reserved
& ND_RA_FLAG_OTHER
) !=
963 "<%s> O flag inconsistent on %s:"
964 " %s from %s, %s from us",
967 on_off
[!rai
->otherflg
],
968 inet_ntop(AF_INET6
, &from
->sin6_addr
,
969 ntopbuf
, INET6_ADDRSTRLEN
),
970 on_off
[rai
->otherflg
]);
974 reachabletime
= ntohl(ra
->nd_ra_reachable
);
975 if (reachabletime
&& rai
->reachabletime
&&
976 reachabletime
!= rai
->reachabletime
) {
978 "<%s> ReachableTime inconsistent on %s:"
979 " %d from %s, %d from us",
983 inet_ntop(AF_INET6
, &from
->sin6_addr
,
984 ntopbuf
, INET6_ADDRSTRLEN
),
989 retranstimer
= ntohl(ra
->nd_ra_retransmit
);
990 if (retranstimer
&& rai
->retranstimer
&&
991 retranstimer
!= rai
->retranstimer
) {
993 "<%s> RetranceTimer inconsistent on %s:"
994 " %d from %s, %d from us",
998 inet_ntop(AF_INET6
, &from
->sin6_addr
,
999 ntopbuf
, INET6_ADDRSTRLEN
),
1003 /* Values in the MTU options */
1004 if (ndopts
.nd_opts_mtu
) {
1005 mtu
= ntohl(ndopts
.nd_opts_mtu
->nd_opt_mtu_mtu
);
1006 if (mtu
&& rai
->linkmtu
&& mtu
!= rai
->linkmtu
) {
1008 "<%s> MTU option value inconsistent on %s:"
1009 " %d from %s, %d from us",
1012 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1013 ntopbuf
, INET6_ADDRSTRLEN
),
1018 /* Preferred and Valid Lifetimes for prefixes */
1020 struct nd_optlist
*optp
= ndopts
.nd_opts_list
;
1022 if (ndopts
.nd_opts_pi
) {
1023 if (prefix_check(ndopts
.nd_opts_pi
, rai
, from
))
1027 if (prefix_check((struct nd_opt_prefix_info
*)optp
->opt
,
1035 rai
->rainconsistent
++;
1038 free_ndopts(&ndopts
);
1042 /* return a non-zero value if the received prefix is inconsitent with ours */
1044 prefix_check(struct nd_opt_prefix_info
*pinfo
,
1045 struct rainfo
*rai
, struct sockaddr_in6
*from
)
1047 u_int32_t preferred_time
, valid_time
;
1049 int inconsistent
= 0;
1050 u_char ntopbuf
[INET6_ADDRSTRLEN
], prefixbuf
[INET6_ADDRSTRLEN
];
1053 #if 0 /* impossible */
1054 if (pinfo
->nd_opt_pi_type
!= ND_OPT_PREFIX_INFORMATION
)
1059 * log if the adveritsed prefix has link-local scope(sanity check?)
1061 if (IN6_IS_ADDR_LINKLOCAL(&pinfo
->nd_opt_pi_prefix
)) {
1063 "<%s> link-local prefix %s/%d is advertised "
1066 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1067 prefixbuf
, INET6_ADDRSTRLEN
),
1068 pinfo
->nd_opt_pi_prefix_len
,
1069 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1070 ntopbuf
, INET6_ADDRSTRLEN
),
1074 if ((pp
= find_prefix(rai
, &pinfo
->nd_opt_pi_prefix
,
1075 pinfo
->nd_opt_pi_prefix_len
)) == NULL
) {
1077 "<%s> prefix %s/%d from %s on %s is not in our list",
1079 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1080 prefixbuf
, INET6_ADDRSTRLEN
),
1081 pinfo
->nd_opt_pi_prefix_len
,
1082 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1083 ntopbuf
, INET6_ADDRSTRLEN
),
1088 preferred_time
= ntohl(pinfo
->nd_opt_pi_preferred_time
);
1089 if (pp
->pltimeexpire
) {
1091 * The lifetime is decremented in real time, so we should
1092 * compare the expiration time.
1093 * (RFC 2461 Section 6.2.7.)
1094 * XXX: can we really expect that all routers on the link
1095 * have synchronized clocks?
1097 gettimeofday(&now
, NULL
);
1098 preferred_time
+= now
.tv_sec
;
1100 if (rai
->clockskew
&&
1101 abs(preferred_time
- pp
->pltimeexpire
) > rai
->clockskew
) {
1103 "<%s> prefeerred lifetime for %s/%d"
1104 " (decr. in real time) inconsistent on %s:"
1105 " %d from %s, %ld 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
),
1117 else if (preferred_time
!= pp
->preflifetime
) {
1119 "<%s> prefeerred lifetime for %s/%d"
1120 " inconsistent on %s:"
1121 " %d from %s, %d from us",
1123 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1124 prefixbuf
, INET6_ADDRSTRLEN
),
1125 pinfo
->nd_opt_pi_prefix_len
,
1126 rai
->ifname
, preferred_time
,
1127 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1128 ntopbuf
, INET6_ADDRSTRLEN
),
1132 valid_time
= ntohl(pinfo
->nd_opt_pi_valid_time
);
1133 if (pp
->vltimeexpire
) {
1134 gettimeofday(&now
, NULL
);
1135 valid_time
+= now
.tv_sec
;
1137 if (rai
->clockskew
&&
1138 abs(valid_time
- pp
->vltimeexpire
) > rai
->clockskew
) {
1140 "<%s> valid lifetime for %s/%d"
1141 " (decr. in real time) inconsistent on %s:"
1142 " %d from %s, %ld 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
, preferred_time
,
1148 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1149 ntopbuf
, INET6_ADDRSTRLEN
),
1154 else if (valid_time
!= pp
->validlifetime
) {
1156 "<%s> valid lifetime for %s/%d"
1157 " inconsistent on %s:"
1158 " %d from %s, %d from us",
1160 inet_ntop(AF_INET6
, &pinfo
->nd_opt_pi_prefix
,
1161 prefixbuf
, INET6_ADDRSTRLEN
),
1162 pinfo
->nd_opt_pi_prefix_len
,
1163 rai
->ifname
, valid_time
,
1164 inet_ntop(AF_INET6
, &from
->sin6_addr
,
1165 ntopbuf
, INET6_ADDRSTRLEN
),
1170 return(inconsistent
);
1174 find_prefix(struct rainfo
*rai
, struct in6_addr
*prefix
, int plen
)
1177 int bytelen
, bitlen
;
1179 for (pp
= rai
->prefix
.next
; pp
!= &rai
->prefix
; pp
= pp
->next
) {
1180 if (plen
!= pp
->prefixlen
)
1184 if (memcmp((void *)prefix
, (void *)&pp
->prefix
, bytelen
))
1186 if (prefix
->s6_addr
[bytelen
] >> (8 - bitlen
) ==
1187 pp
->prefix
.s6_addr
[bytelen
] >> (8 - bitlen
))
1194 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1196 prefix_match(struct in6_addr
*p0
, int plen0
,
1197 struct in6_addr
*p1
, int plen1
)
1199 int bytelen
, bitlen
;
1203 bytelen
= plen1
/ 8;
1205 if (memcmp((void *)p0
, (void *)p1
, bytelen
))
1207 if (p0
->s6_addr
[bytelen
] >> (8 - bitlen
) ==
1208 p1
->s6_addr
[bytelen
] >> (8 - bitlen
))
1215 nd6_options(struct nd_opt_hdr
*hdr
, int limit
,
1216 union nd_opts
*ndopts
, u_int32_t optflags
)
1220 for (; limit
> 0; limit
-= optlen
) {
1221 hdr
= (struct nd_opt_hdr
*)((caddr_t
)hdr
+ optlen
);
1222 optlen
= hdr
->nd_opt_len
<< 3;
1223 if (hdr
->nd_opt_len
== 0) {
1225 "<%s> bad ND option length(0) (type = %d)",
1226 __FUNCTION__
, hdr
->nd_opt_type
);
1230 if (hdr
->nd_opt_type
> ND_OPT_MTU
) {
1232 "<%s> unknown ND option(type %d)",
1238 if ((ndopt_flags
[hdr
->nd_opt_type
] & optflags
) == 0) {
1240 "<%s> unexpected ND option(type %d)",
1246 switch(hdr
->nd_opt_type
) {
1247 case ND_OPT_SOURCE_LINKADDR
:
1248 case ND_OPT_TARGET_LINKADDR
:
1249 case ND_OPT_REDIRECTED_HEADER
:
1251 if (ndopts
->nd_opt_array
[hdr
->nd_opt_type
]) {
1253 "<%s> duplicated ND option"
1258 ndopts
->nd_opt_array
[hdr
->nd_opt_type
] = hdr
;
1260 case ND_OPT_PREFIX_INFORMATION
:
1262 struct nd_optlist
*pfxlist
;
1264 if (ndopts
->nd_opts_pi
== 0) {
1265 ndopts
->nd_opts_pi
=
1266 (struct nd_opt_prefix_info
*)hdr
;
1269 if ((pfxlist
= malloc(sizeof(*pfxlist
))) == NULL
) {
1271 "<%s> can't allocate memory",
1275 pfxlist
->next
= ndopts
->nd_opts_list
;
1277 ndopts
->nd_opts_list
= pfxlist
;
1281 default: /* impossible */
1289 free_ndopts(ndopts
);
1295 free_ndopts(union nd_opts
*ndopts
)
1297 struct nd_optlist
*opt
= ndopts
->nd_opts_list
, *next
;
1309 struct icmp6_filter filt
;
1310 struct ipv6_mreq mreq
;
1311 struct rainfo
*ra
= ralist
;
1313 /* XXX: should be max MTU attached to the node */
1314 static u_char answer
[1500];
1316 rcvcmsgbuflen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
1317 CMSG_SPACE(sizeof(int));
1318 rcvcmsgbuf
= (u_char
*)malloc(rcvcmsgbuflen
);
1319 if (rcvcmsgbuf
== NULL
) {
1320 syslog(LOG_ERR
, "<%s> not enough core", __FUNCTION__
);
1324 sndcmsgbuflen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
1325 CMSG_SPACE(sizeof(int));
1326 sndcmsgbuf
= (u_char
*)malloc(sndcmsgbuflen
);
1327 if (sndcmsgbuf
== NULL
) {
1328 syslog(LOG_ERR
, "<%s> not enough core", __FUNCTION__
);
1332 if ((sock
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
1333 syslog(LOG_ERR
, "<%s> socket: %s", __FUNCTION__
,
1338 /* specify to tell receiving interface */
1340 #ifdef IPV6_RECVPKTINFO
1341 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &on
,
1343 syslog(LOG_ERR
, "<%s> IPV6_RECVPKTINFO: %s",
1344 __FUNCTION__
, strerror(errno
));
1347 #else /* old adv. API */
1348 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_PKTINFO
, &on
,
1350 syslog(LOG_ERR
, "<%s> IPV6_PKTINFO: %s",
1351 __FUNCTION__
, strerror(errno
));
1357 /* specify to tell value of hoplimit field of received IP6 hdr */
1358 #ifdef IPV6_RECVHOPLIMIT
1359 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_RECVHOPLIMIT
, &on
,
1361 syslog(LOG_ERR
, "<%s> IPV6_RECVHOPLIMIT: %s",
1362 __FUNCTION__
, strerror(errno
));
1365 #else /* old adv. API */
1366 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_HOPLIMIT
, &on
,
1368 syslog(LOG_ERR
, "<%s> IPV6_HOPLIMIT: %s",
1369 __FUNCTION__
, strerror(errno
));
1374 ICMP6_FILTER_SETBLOCKALL(&filt
);
1375 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT
, &filt
);
1376 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT
, &filt
);
1378 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING
, &filt
);
1379 if (setsockopt(sock
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
1380 sizeof(filt
)) < 0) {
1381 syslog(LOG_ERR
, "<%s> IICMP6_FILTER: %s",
1382 __FUNCTION__
, strerror(errno
));
1387 * join all routers multicast address on each advertising interface.
1389 if (inet_pton(AF_INET6
, ALLROUTERS_LINK
,
1390 &mreq
.ipv6mr_multiaddr
.s6_addr
)
1392 syslog(LOG_ERR
, "<%s> inet_pton failed(library bug?)",
1397 mreq
.ipv6mr_interface
= ra
->ifindex
;
1398 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
, &mreq
,
1399 sizeof(mreq
)) < 0) {
1400 syslog(LOG_ERR
, "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
1401 __FUNCTION__
, ra
->ifname
, strerror(errno
));
1408 * When attending router renumbering, join all-routers site-local
1412 if (inet_pton(AF_INET6
, ALLROUTERS_SITE
,
1413 &in6a_site_allrouters
) != 1) {
1414 syslog(LOG_ERR
, "<%s> inet_pton failed(library bug?)",
1418 mreq
.ipv6mr_multiaddr
= in6a_site_allrouters
;
1420 if ((mreq
.ipv6mr_interface
= if_nametoindex(mcastif
))
1423 "<%s> invalid interface: %s",
1424 __FUNCTION__
, mcastif
);
1428 mreq
.ipv6mr_interface
= ralist
->ifindex
;
1429 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
1430 &mreq
, sizeof(mreq
)) < 0) {
1432 "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
1434 mcastif
? mcastif
: ralist
->ifname
,
1440 /* initialize msghdr for receiving packets */
1441 rcviov
[0].iov_base
= (caddr_t
)answer
;
1442 rcviov
[0].iov_len
= sizeof(answer
);
1443 rcvmhdr
.msg_name
= (caddr_t
)&from
;
1444 rcvmhdr
.msg_namelen
= sizeof(from
);
1445 rcvmhdr
.msg_iov
= rcviov
;
1446 rcvmhdr
.msg_iovlen
= 1;
1447 rcvmhdr
.msg_control
= (caddr_t
) rcvcmsgbuf
;
1448 rcvmhdr
.msg_controllen
= rcvcmsgbuflen
;
1450 /* initialize msghdr for sending packets */
1451 sndmhdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
1452 sndmhdr
.msg_iov
= sndiov
;
1453 sndmhdr
.msg_iovlen
= 1;
1454 sndmhdr
.msg_control
= (caddr_t
)sndcmsgbuf
;
1455 sndmhdr
.msg_controllen
= sndcmsgbuflen
;
1460 /* open a routing socket to watch the routing table */
1464 if ((rtsock
= socket(PF_ROUTE
, SOCK_RAW
, 0)) < 0) {
1466 "<%s> socket: %s", __FUNCTION__
, strerror(errno
));
1472 if_indextorainfo(int index
)
1474 struct rainfo
*rai
= ralist
;
1476 for (rai
= ralist
; rai
; rai
= rai
->next
) {
1477 if (rai
->ifindex
== index
)
1481 return(NULL
); /* search failed */
1486 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 __FUNCTION__
, 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 __FUNCTION__
, 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 __FUNCTION__
, 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 __FUNCTION__
, 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 */
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 __FUNCTION__
, rai
->ifname
);
1594 /* update RA timer */
1596 ra_timer_update(void *data
, struct timeval
*tm
)
1598 struct rainfo
*rai
= (struct rainfo
*)data
;
1602 * Whenever a multicast advertisement is sent from an interface,
1603 * the timer is reset to a uniformly-distributed random value
1604 * between the interface's configured MinRtrAdvInterval and
1605 * MaxRtrAdvInterval (RFC2461 6.2.4).
1607 interval
= rai
->mininterval
;
1608 interval
+= random() % (rai
->maxinterval
- rai
->mininterval
);
1611 * For the first few advertisements (up to
1612 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1613 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1614 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1617 if (rai
->initcounter
< MAX_INITIAL_RTR_ADVERTISEMENTS
&&
1618 interval
> MAX_INITIAL_RTR_ADVERT_INTERVAL
)
1619 interval
= MAX_INITIAL_RTR_ADVERT_INTERVAL
;
1621 tm
->tv_sec
= interval
;
1625 "<%s> RA timer on %s is set to %ld:%ld",
1626 __FUNCTION__
, rai
->ifname
,
1627 (long int)tm
->tv_sec
, (long int)tm
->tv_usec
);