rtadvd.c revision 1.22 1 /* $NetBSD: rtadvd.c,v 1.22 2002/09/08 01:42:55 itojun Exp $ */
2 /* $KAME: rtadvd.c,v 1.74 2002/09/08 01:25:17 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/uio.h>
36 #include <sys/time.h>
37 #include <sys/queue.h>
38
39 #include <net/if.h>
40 #include <net/route.h>
41 #include <net/if_dl.h>
42 #include <netinet/in.h>
43 #include <netinet/ip6.h>
44 #include <netinet6/ip6_var.h>
45 #include <netinet/icmp6.h>
46
47 #include <arpa/inet.h>
48
49 #include <time.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <string.h>
56 #include <stdlib.h>
57 #include <syslog.h>
58 #include <util.h>
59
60 #include "rtadvd.h"
61 #include "rrenum.h"
62 #include "advcap.h"
63 #include "timer.h"
64 #include "if.h"
65 #include "config.h"
66 #include "dump.h"
67
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;
73 volatile sig_atomic_t do_dump;
74 volatile sig_atomic_t do_die;
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 *mcastif;
83 int sock;
84 int rtsock = -1;
85 int accept_rr = 0;
86 int dflag = 0, sflag = 0;
87
88 u_char *conffile = NULL;
89
90 struct rainfo *ralist = NULL;
91 struct nd_optlist {
92 struct nd_optlist *next;
93 struct nd_opt_hdr *opt;
94 };
95 union nd_opts {
96 struct nd_opt_hdr *nd_opt_array[9];
97 struct {
98 struct nd_opt_hdr *zero;
99 struct nd_opt_hdr *src_lladdr;
100 struct nd_opt_hdr *tgt_lladdr;
101 struct nd_opt_prefix_info *pi;
102 struct nd_opt_rd_hdr *rh;
103 struct nd_opt_mtu *mtu;
104 struct nd_optlist *list;
105 } nd_opt_each;
106 };
107 #define nd_opts_src_lladdr nd_opt_each.src_lladdr
108 #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr
109 #define nd_opts_pi nd_opt_each.pi
110 #define nd_opts_rh nd_opt_each.rh
111 #define nd_opts_mtu nd_opt_each.mtu
112 #define nd_opts_list nd_opt_each.list
113
114 #define NDOPT_FLAG_SRCLINKADDR 0x1
115 #define NDOPT_FLAG_TGTLINKADDR 0x2
116 #define NDOPT_FLAG_PREFIXINFO 0x4
117 #define NDOPT_FLAG_RDHDR 0x8
118 #define NDOPT_FLAG_MTU 0x10
119
120 u_int32_t ndopt_flags[] = {
121 0, NDOPT_FLAG_SRCLINKADDR, NDOPT_FLAG_TGTLINKADDR,
122 NDOPT_FLAG_PREFIXINFO, NDOPT_FLAG_RDHDR, NDOPT_FLAG_MTU,
123 };
124
125 int main __P((int, char *[]));
126 static void set_die __P((int));
127 static void die __P((void));
128 static void sock_open __P((void));
129 static void rtsock_open __P((void));
130 static void rtadvd_input __P((void));
131 static void rs_input __P((int, struct nd_router_solicit *,
132 struct in6_pktinfo *, struct sockaddr_in6 *));
133 static void ra_input __P((int, struct nd_router_advert *,
134 struct in6_pktinfo *, struct sockaddr_in6 *));
135 static int prefix_check __P((struct nd_opt_prefix_info *, struct rainfo *,
136 struct sockaddr_in6 *));
137 static int nd6_options __P((struct nd_opt_hdr *, int,
138 union nd_opts *, u_int32_t));
139 static void free_ndopts __P((union nd_opts *));
140 static void ra_output __P((struct rainfo *));
141 static void rtmsg_input __P((void));
142 static void rtadvd_set_dump_file __P((int));
143
144 int
145 main(argc, argv)
146 int argc;
147 char *argv[];
148 {
149 fd_set *fdsetp, *selectfdp;
150 int fdmasks;
151 int maxfd = 0;
152 struct timeval *timeout;
153 int i, ch;
154 int fflag = 0, logopt;
155
156 /* get command line options and arguments */
157 #define OPTIONS "c:dDfM:Rs"
158 while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
159 #undef OPTIONS
160 switch (ch) {
161 case 'c':
162 conffile = optarg;
163 break;
164 case 'd':
165 dflag = 1;
166 break;
167 case 'D':
168 dflag = 2;
169 break;
170 case 'f':
171 fflag = 1;
172 break;
173 case 'M':
174 mcastif = optarg;
175 break;
176 case 'R':
177 fprintf(stderr, "rtadvd: "
178 "the -R option is currently ignored.\n");
179 /* accept_rr = 1; */
180 /* run anyway... */
181 break;
182 case 's':
183 sflag = 1;
184 break;
185 }
186 }
187 argc -= optind;
188 argv += optind;
189 if (argc == 0) {
190 fprintf(stderr,
191 "usage: rtadvd [-dDfMRs] [-c conffile] "
192 "interfaces...\n");
193 exit(1);
194 }
195
196 logopt = LOG_NDELAY | LOG_PID;
197 if (fflag)
198 logopt |= LOG_PERROR;
199 openlog("rtadvd", logopt, LOG_DAEMON);
200
201 /* set log level */
202 if (dflag == 0)
203 (void)setlogmask(LOG_UPTO(LOG_ERR));
204 if (dflag == 1)
205 (void)setlogmask(LOG_UPTO(LOG_INFO));
206
207 /* timer initialization */
208 rtadvd_timer_init();
209
210 /* get iflist block from kernel */
211 init_iflist();
212
213 while (argc--)
214 getconfig(*argv++);
215
216 if (inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr) != 1) {
217 fprintf(stderr, "fatal: inet_pton failed\n");
218 exit(1);
219 }
220
221 if (!fflag)
222 daemon(1, 0);
223
224 sock_open();
225
226 /* record the current PID */
227 if (pidfile(NULL) < 0) {
228 syslog(LOG_ERR,
229 "<%s> failed to open the pid log file, run anyway.",
230 __func__);
231 }
232
233 maxfd = sock;
234 if (sflag == 0) {
235 rtsock_open();
236 if (rtsock > sock)
237 maxfd = rtsock;
238 } else
239 rtsock = -1;
240
241 fdmasks = howmany(maxfd + 1, NFDBITS) * sizeof(fd_mask);
242 if ((fdsetp = malloc(fdmasks)) == NULL) {
243 err(1, "malloc");
244 /*NOTREACHED*/
245 }
246 if ((selectfdp = malloc(fdmasks)) == NULL) {
247 err(1, "malloc");
248 /*NOTREACHED*/
249 }
250 memset(fdsetp, 0, fdmasks);
251 FD_SET(sock, fdsetp);
252 if (rtsock >= 0)
253 FD_SET(rtsock, fdsetp);
254
255 signal(SIGTERM, set_die);
256 signal(SIGUSR1, rtadvd_set_dump_file);
257
258 while (1) {
259 memcpy(selectfdp, fdsetp, fdmasks); /* reinitialize */
260
261 if (do_dump) { /* SIGUSR1 */
262 do_dump = 0;
263 rtadvd_dump_file(dumpfilename);
264 }
265
266 if (do_die) {
267 die();
268 /*NOTREACHED*/
269 }
270
271 /* timer expiration check and reset the timer */
272 timeout = rtadvd_check_timer();
273
274 if (timeout != NULL) {
275 syslog(LOG_DEBUG,
276 "<%s> set timer to %ld:%ld. waiting for "
277 "inputs or timeout", __func__,
278 (long int)timeout->tv_sec,
279 (long int)timeout->tv_usec);
280 } else {
281 syslog(LOG_DEBUG,
282 "<%s> there's no timer. waiting for inputs",
283 __func__);
284 }
285
286 if ((i = select(maxfd + 1, selectfdp, NULL, NULL,
287 timeout)) < 0) {
288 /* EINTR would occur upon SIGUSR1 for status dump */
289 if (errno != EINTR)
290 syslog(LOG_ERR, "<%s> select: %s",
291 __func__, strerror(errno));
292 continue;
293 }
294 if (i == 0) /* timeout */
295 continue;
296 if (rtsock != -1 && FD_ISSET(rtsock, selectfdp))
297 rtmsg_input();
298 if (FD_ISSET(sock, selectfdp))
299 rtadvd_input();
300 }
301 exit(0); /* NOTREACHED */
302 }
303
304 static void
305 rtadvd_set_dump_file(sig)
306 int sig;
307 {
308 do_dump = 1;
309 }
310
311 static void
312 set_die(sig)
313 int sig;
314 {
315 do_die = 1;
316 }
317
318 static void
319 die()
320 {
321 struct rainfo *ra;
322 int i;
323 const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
324
325 if (dflag > 1) {
326 syslog(LOG_DEBUG, "<%s> cease to be an advertising router\n",
327 __func__);
328 }
329
330 for (ra = ralist; ra; ra = ra->next) {
331 ra->lifetime = 0;
332 make_packet(ra);
333 }
334 for (i = 0; i < retrans; i++) {
335 for (ra = ralist; ra; ra = ra->next)
336 ra_output(ra);
337 sleep(MIN_DELAY_BETWEEN_RAS);
338 }
339 exit(0);
340 /*NOTREACHED*/
341 }
342
343 static void
344 rtmsg_input()
345 {
346 int n, type, ifindex = 0, plen;
347 size_t len;
348 char msg[2048], *next, *lim;
349 u_char ifname[IF_NAMESIZE];
350 struct prefix *prefix;
351 struct rainfo *rai;
352 struct in6_addr *addr;
353 char addrbuf[INET6_ADDRSTRLEN];
354
355 n = read(rtsock, msg, sizeof(msg));
356 if (dflag > 1) {
357 syslog(LOG_DEBUG, "<%s> received a routing message "
358 "(type = %d, len = %d)", __func__, rtmsg_type(msg), n);
359 }
360 if (n > rtmsg_len(msg)) {
361 /*
362 * This usually won't happen for messages received on
363 * a routing socket.
364 */
365 if (dflag > 1)
366 syslog(LOG_DEBUG,
367 "<%s> received data length is larger than "
368 "1st routing message len. multiple messages? "
369 "read %d bytes, but 1st msg len = %d",
370 __func__, n, rtmsg_len(msg));
371 #if 0
372 /* adjust length */
373 n = rtmsg_len(msg);
374 #endif
375 }
376
377 lim = msg + n;
378 for (next = msg; next < lim; next += len) {
379 int oldifflags;
380
381 next = get_next_msg(next, lim, 0, &len,
382 RTADV_TYPE2BITMASK(RTM_ADD) |
383 RTADV_TYPE2BITMASK(RTM_DELETE) |
384 RTADV_TYPE2BITMASK(RTM_NEWADDR) |
385 RTADV_TYPE2BITMASK(RTM_DELADDR) |
386 RTADV_TYPE2BITMASK(RTM_IFINFO));
387 if (len == 0)
388 break;
389 type = rtmsg_type(next);
390 switch (type) {
391 case RTM_ADD:
392 case RTM_DELETE:
393 ifindex = get_rtm_ifindex(next);
394 break;
395 case RTM_NEWADDR:
396 case RTM_DELADDR:
397 ifindex = get_ifam_ifindex(next);
398 break;
399 case RTM_IFINFO:
400 ifindex = get_ifm_ifindex(next);
401 break;
402 default:
403 /* should not reach here */
404 if (dflag > 1) {
405 syslog(LOG_DEBUG,
406 "<%s:%d> unknown rtmsg %d on %s",
407 __func__, __LINE__, type,
408 if_indextoname(ifindex, ifname));
409 }
410 continue;
411 }
412
413 if ((rai = if_indextorainfo(ifindex)) == NULL) {
414 if (dflag > 1) {
415 syslog(LOG_DEBUG,
416 "<%s> route changed on "
417 "non advertising interface(%s)",
418 __func__,
419 if_indextoname(ifindex, ifname));
420 }
421 continue;
422 }
423 oldifflags = iflist[ifindex]->ifm_flags;
424
425 switch (type) {
426 case RTM_ADD:
427 /* init ifflags because it may have changed */
428 iflist[ifindex]->ifm_flags =
429 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
430
431 if (sflag)
432 break; /* we aren't interested in prefixes */
433
434 addr = get_addr(msg);
435 plen = get_prefixlen(msg);
436 /* sanity check for plen */
437 /* as RFC2373, prefixlen is at least 4 */
438 if (plen < 4 || plen > 127) {
439 syslog(LOG_INFO, "<%s> new interface route's"
440 "plen %d is invalid for a prefix",
441 __func__, plen);
442 break;
443 }
444 prefix = find_prefix(rai, addr, plen);
445 if (prefix) {
446 if (dflag > 1) {
447 syslog(LOG_DEBUG,
448 "<%s> new prefix(%s/%d) "
449 "added on %s, "
450 "but it was already in list",
451 __func__,
452 inet_ntop(AF_INET6, addr,
453 (char *)addrbuf, INET6_ADDRSTRLEN),
454 plen, rai->ifname);
455 }
456 break;
457 }
458 make_prefix(rai, ifindex, addr, plen);
459 break;
460 case RTM_DELETE:
461 /* init ifflags because it may have changed */
462 iflist[ifindex]->ifm_flags =
463 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
464
465 if (sflag)
466 break;
467
468 addr = get_addr(msg);
469 plen = get_prefixlen(msg);
470 /* sanity check for plen */
471 /* as RFC2373, prefixlen is at least 4 */
472 if (plen < 4 || plen > 127) {
473 syslog(LOG_INFO,
474 "<%s> deleted interface route's "
475 "plen %d is invalid for a prefix",
476 __func__, plen);
477 break;
478 }
479 prefix = find_prefix(rai, addr, plen);
480 if (prefix == NULL) {
481 if (dflag > 1) {
482 syslog(LOG_DEBUG,
483 "<%s> prefix(%s/%d) was "
484 "deleted on %s, "
485 "but it was not in list",
486 __func__,
487 inet_ntop(AF_INET6, addr,
488 (char *)addrbuf, INET6_ADDRSTRLEN),
489 plen, rai->ifname);
490 }
491 break;
492 }
493 delete_prefix(rai, prefix);
494 break;
495 case RTM_NEWADDR:
496 case RTM_DELADDR:
497 /* init ifflags because it may have changed */
498 iflist[ifindex]->ifm_flags =
499 if_getflags(ifindex, iflist[ifindex]->ifm_flags);
500 break;
501 case RTM_IFINFO:
502 iflist[ifindex]->ifm_flags = get_ifm_flags(next);
503 break;
504 default:
505 /* should not reach here */
506 if (dflag > 1) {
507 syslog(LOG_DEBUG,
508 "<%s:%d> unknown rtmsg %d on %s",
509 __func__, __LINE__, type,
510 if_indextoname(ifindex, ifname));
511 }
512 return;
513 }
514
515 /* check if an interface flag is changed */
516 if ((oldifflags & IFF_UP) != 0 && /* UP to DOWN */
517 (iflist[ifindex]->ifm_flags & IFF_UP) == 0) {
518 syslog(LOG_INFO,
519 "<%s> interface %s becomes down. stop timer.",
520 __func__, rai->ifname);
521 rtadvd_remove_timer(&rai->timer);
522 } else if ((oldifflags & IFF_UP) == 0 && /* DOWN to UP */
523 (iflist[ifindex]->ifm_flags & IFF_UP) != 0) {
524 syslog(LOG_INFO,
525 "<%s> interface %s becomes up. restart timer.",
526 __func__, rai->ifname);
527
528 rai->initcounter = 0; /* reset the counter */
529 rai->waiting = 0; /* XXX */
530 rai->timer = rtadvd_add_timer(ra_timeout,
531 ra_timer_update, rai, rai);
532 ra_timer_update((void *)rai, &rai->timer->tm);
533 rtadvd_set_timer(&rai->timer->tm, rai->timer);
534 }
535 }
536
537 return;
538 }
539
540 void
541 rtadvd_input()
542 {
543 int i;
544 int *hlimp = NULL;
545 #ifdef OLDRAWSOCKET
546 struct ip6_hdr *ip;
547 #endif
548 struct icmp6_hdr *icp;
549 int ifindex = 0;
550 struct cmsghdr *cm;
551 struct in6_pktinfo *pi = NULL;
552 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
553 struct in6_addr dst = in6addr_any;
554
555 /*
556 * Get message. We reset msg_controllen since the field could
557 * be modified if we had received a message before setting
558 * receive options.
559 */
560 rcvmhdr.msg_controllen = rcvcmsgbuflen;
561 if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
562 return;
563
564 /* extract optional information via Advanced API */
565 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
566 cm;
567 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
568 if (cm->cmsg_level == IPPROTO_IPV6 &&
569 cm->cmsg_type == IPV6_PKTINFO &&
570 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
571 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
572 ifindex = pi->ipi6_ifindex;
573 dst = pi->ipi6_addr;
574 }
575 if (cm->cmsg_level == IPPROTO_IPV6 &&
576 cm->cmsg_type == IPV6_HOPLIMIT &&
577 cm->cmsg_len == CMSG_LEN(sizeof(int)))
578 hlimp = (int *)CMSG_DATA(cm);
579 }
580 if (ifindex == 0) {
581 syslog(LOG_ERR,
582 "<%s> failed to get receiving interface",
583 __func__);
584 return;
585 }
586 if (hlimp == NULL) {
587 syslog(LOG_ERR,
588 "<%s> failed to get receiving hop limit",
589 __func__);
590 return;
591 }
592
593 /*
594 * If we happen to receive data on an interface which is now down,
595 * just discard the data.
596 */
597 if ((iflist[pi->ipi6_ifindex]->ifm_flags & IFF_UP) == 0) {
598 syslog(LOG_INFO,
599 "<%s> received data on a disabled interface (%s)",
600 __func__,
601 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
602 return;
603 }
604
605 #ifdef OLDRAWSOCKET
606 if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
607 syslog(LOG_ERR,
608 "<%s> packet size(%d) is too short",
609 __func__, i);
610 return;
611 }
612
613 ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
614 icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
615 #else
616 if (i < sizeof(struct icmp6_hdr)) {
617 syslog(LOG_ERR,
618 "<%s> packet size(%d) is too short",
619 __func__, i);
620 return;
621 }
622
623 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
624 #endif
625
626 switch (icp->icmp6_type) {
627 case ND_ROUTER_SOLICIT:
628 /*
629 * Message verification - RFC-2461 6.1.1
630 * XXX: these checks must be done in the kernel as well,
631 * but we can't completely rely on them.
632 */
633 if (*hlimp != 255) {
634 syslog(LOG_NOTICE,
635 "<%s> RS with invalid hop limit(%d) "
636 "received from %s on %s",
637 __func__, *hlimp,
638 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
639 INET6_ADDRSTRLEN),
640 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
641 return;
642 }
643 if (icp->icmp6_code) {
644 syslog(LOG_NOTICE,
645 "<%s> RS with invalid ICMP6 code(%d) "
646 "received from %s on %s",
647 __func__, icp->icmp6_code,
648 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
649 INET6_ADDRSTRLEN),
650 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
651 return;
652 }
653 if (i < sizeof(struct nd_router_solicit)) {
654 syslog(LOG_NOTICE,
655 "<%s> RS from %s on %s does not have enough "
656 "length (len = %d)",
657 __func__,
658 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
659 INET6_ADDRSTRLEN),
660 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
661 return;
662 }
663 rs_input(i, (struct nd_router_solicit *)icp, pi, &from);
664 break;
665 case ND_ROUTER_ADVERT:
666 /*
667 * Message verification - RFC-2461 6.1.2
668 * XXX: there's a same dilemma as above...
669 */
670 if (*hlimp != 255) {
671 syslog(LOG_NOTICE,
672 "<%s> RA with invalid hop limit(%d) "
673 "received from %s on %s",
674 __func__, *hlimp,
675 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
676 INET6_ADDRSTRLEN),
677 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
678 return;
679 }
680 if (icp->icmp6_code) {
681 syslog(LOG_NOTICE,
682 "<%s> RA with invalid ICMP6 code(%d) "
683 "received from %s on %s",
684 __func__, icp->icmp6_code,
685 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
686 INET6_ADDRSTRLEN),
687 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
688 return;
689 }
690 if (i < sizeof(struct nd_router_advert)) {
691 syslog(LOG_NOTICE,
692 "<%s> RA from %s on %s does not have enough "
693 "length (len = %d)",
694 __func__,
695 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
696 INET6_ADDRSTRLEN),
697 if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
698 return;
699 }
700 ra_input(i, (struct nd_router_advert *)icp, pi, &from);
701 break;
702 case ICMP6_ROUTER_RENUMBERING:
703 if (accept_rr == 0) {
704 syslog(LOG_ERR, "<%s> received a router renumbering "
705 "message, but not allowed to be accepted",
706 __func__);
707 break;
708 }
709 rr_input(i, (struct icmp6_router_renum *)icp, pi, &from,
710 &dst);
711 break;
712 default:
713 /*
714 * Note that this case is POSSIBLE, especially just
715 * after invocation of the daemon. This is because we
716 * could receive message after opening the socket and
717 * before setting ICMP6 type filter(see sock_open()).
718 */
719 syslog(LOG_ERR, "<%s> invalid icmp type(%d)",
720 __func__, icp->icmp6_type);
721 return;
722 }
723
724 return;
725 }
726
727 static void
728 rs_input(int len, struct nd_router_solicit *rs,
729 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
730 {
731 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
732 union nd_opts ndopts;
733 struct rainfo *ra;
734
735 syslog(LOG_DEBUG,
736 "<%s> RS received from %s on %s",
737 __func__,
738 inet_ntop(AF_INET6, &from->sin6_addr,
739 ntopbuf, INET6_ADDRSTRLEN),
740 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
741
742 /* ND option check */
743 memset(&ndopts, 0, sizeof(ndopts));
744 if (nd6_options((struct nd_opt_hdr *)(rs + 1),
745 len - sizeof(struct nd_router_solicit),
746 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
747 syslog(LOG_DEBUG,
748 "<%s> ND option check failed for an RS from %s on %s",
749 __func__,
750 inet_ntop(AF_INET6, &from->sin6_addr,
751 ntopbuf, INET6_ADDRSTRLEN),
752 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
753 return;
754 }
755
756 /*
757 * If the IP source address is the unspecified address, there
758 * must be no source link-layer address option in the message.
759 * (RFC-2461 6.1.1)
760 */
761 if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
762 ndopts.nd_opts_src_lladdr) {
763 syslog(LOG_ERR,
764 "<%s> RS from unspecified src on %s has a link-layer"
765 " address option",
766 __func__,
767 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
768 goto done;
769 }
770
771 ra = ralist;
772 while (ra != NULL) {
773 if (pi->ipi6_ifindex == ra->ifindex)
774 break;
775 ra = ra->next;
776 }
777 if (ra == NULL) {
778 syslog(LOG_INFO,
779 "<%s> RS received on non advertising interface(%s)",
780 __func__,
781 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
782 goto done;
783 }
784
785 ra->rsinput++; /* increment statistics */
786
787 /*
788 * Decide whether to send RA according to the rate-limit
789 * consideration.
790 */
791 {
792 long delay; /* must not be greater than 1000000 */
793 struct timeval interval, now, min_delay, tm_tmp, *rest;
794 struct soliciter *sol;
795
796 /*
797 * record sockaddr waiting for RA, if possible
798 */
799 sol = (struct soliciter *)malloc(sizeof(*sol));
800 if (sol) {
801 sol->addr = *from;
802 /*XXX RFC2553 need clarification on flowinfo */
803 sol->addr.sin6_flowinfo = 0;
804 sol->next = ra->soliciter;
805 ra->soliciter = sol;
806 }
807
808 /*
809 * If there is already a waiting RS packet, don't
810 * update the timer.
811 */
812 if (ra->waiting++)
813 goto done;
814
815 /*
816 * Compute a random delay. If the computed value
817 * corresponds to a time later than the time the next
818 * multicast RA is scheduled to be sent, ignore the random
819 * delay and send the advertisement at the
820 * already-scheduled time. RFC-2461 6.2.6
821 */
822 delay = arc4random() % MAX_RA_DELAY_TIME;
823 interval.tv_sec = 0;
824 interval.tv_usec = delay;
825 rest = rtadvd_timer_rest(ra->timer);
826 if (TIMEVAL_LT(*rest, interval)) {
827 syslog(LOG_DEBUG,
828 "<%s> random delay is larger than "
829 "the rest of normal timer",
830 __func__);
831 interval = *rest;
832 }
833
834 /*
835 * If we sent a multicast Router Advertisement within
836 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
837 * the advertisement to be sent at a time corresponding to
838 * MIN_DELAY_BETWEEN_RAS plus the random value after the
839 * previous advertisement was sent.
840 */
841 gettimeofday(&now, NULL);
842 TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp);
843 min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
844 min_delay.tv_usec = 0;
845 if (TIMEVAL_LT(tm_tmp, min_delay)) {
846 TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
847 TIMEVAL_ADD(&min_delay, &interval, &interval);
848 }
849 rtadvd_set_timer(&interval, ra->timer);
850 goto done;
851 }
852
853 done:
854 free_ndopts(&ndopts);
855 return;
856 }
857
858 static void
859 ra_input(int len, struct nd_router_advert *ra,
860 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
861 {
862 struct rainfo *rai;
863 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
864 union nd_opts ndopts;
865 char *on_off[] = {"OFF", "ON"};
866 u_int32_t reachabletime, retranstimer, mtu;
867 int inconsistent = 0;
868
869 syslog(LOG_DEBUG,
870 "<%s> RA received from %s on %s",
871 __func__,
872 inet_ntop(AF_INET6, &from->sin6_addr,
873 ntopbuf, INET6_ADDRSTRLEN),
874 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
875
876 /* ND option check */
877 memset(&ndopts, 0, sizeof(ndopts));
878 if (nd6_options((struct nd_opt_hdr *)(ra + 1),
879 len - sizeof(struct nd_router_advert),
880 &ndopts, NDOPT_FLAG_SRCLINKADDR |
881 NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
882 syslog(LOG_ERR,
883 "<%s> ND option check failed for an RA from %s on %s",
884 __func__,
885 inet_ntop(AF_INET6, &from->sin6_addr,
886 ntopbuf, INET6_ADDRSTRLEN),
887 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
888 return;
889 }
890
891 /*
892 * RA consistency check according to RFC-2461 6.2.7
893 */
894 if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == 0) {
895 syslog(LOG_INFO,
896 "<%s> received RA from %s on non-advertising"
897 " interface(%s)",
898 __func__,
899 inet_ntop(AF_INET6, &from->sin6_addr,
900 ntopbuf, INET6_ADDRSTRLEN),
901 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
902 goto done;
903 }
904 rai->rainput++; /* increment statistics */
905
906 /* Cur Hop Limit value */
907 if (ra->nd_ra_curhoplimit && rai->hoplimit &&
908 ra->nd_ra_curhoplimit != rai->hoplimit) {
909 syslog(LOG_INFO,
910 "<%s> CurHopLimit inconsistent on %s:"
911 " %d from %s, %d from us",
912 __func__,
913 rai->ifname,
914 ra->nd_ra_curhoplimit,
915 inet_ntop(AF_INET6, &from->sin6_addr,
916 ntopbuf, INET6_ADDRSTRLEN),
917 rai->hoplimit);
918 inconsistent++;
919 }
920 /* M flag */
921 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
922 rai->managedflg) {
923 syslog(LOG_INFO,
924 "<%s> M flag inconsistent on %s:"
925 " %s from %s, %s from us",
926 __func__,
927 rai->ifname,
928 on_off[!rai->managedflg],
929 inet_ntop(AF_INET6, &from->sin6_addr,
930 ntopbuf, INET6_ADDRSTRLEN),
931 on_off[rai->managedflg]);
932 inconsistent++;
933 }
934 /* O flag */
935 if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
936 rai->otherflg) {
937 syslog(LOG_INFO,
938 "<%s> O flag inconsistent on %s:"
939 " %s from %s, %s from us",
940 __func__,
941 rai->ifname,
942 on_off[!rai->otherflg],
943 inet_ntop(AF_INET6, &from->sin6_addr,
944 ntopbuf, INET6_ADDRSTRLEN),
945 on_off[rai->otherflg]);
946 inconsistent++;
947 }
948 /* Reachable Time */
949 reachabletime = ntohl(ra->nd_ra_reachable);
950 if (reachabletime && rai->reachabletime &&
951 reachabletime != rai->reachabletime) {
952 syslog(LOG_INFO,
953 "<%s> ReachableTime inconsistent on %s:"
954 " %d from %s, %d from us",
955 __func__,
956 rai->ifname,
957 reachabletime,
958 inet_ntop(AF_INET6, &from->sin6_addr,
959 ntopbuf, INET6_ADDRSTRLEN),
960 rai->reachabletime);
961 inconsistent++;
962 }
963 /* Retrans Timer */
964 retranstimer = ntohl(ra->nd_ra_retransmit);
965 if (retranstimer && rai->retranstimer &&
966 retranstimer != rai->retranstimer) {
967 syslog(LOG_INFO,
968 "<%s> RetranceTimer inconsistent on %s:"
969 " %d from %s, %d from us",
970 __func__,
971 rai->ifname,
972 retranstimer,
973 inet_ntop(AF_INET6, &from->sin6_addr,
974 ntopbuf, INET6_ADDRSTRLEN),
975 rai->retranstimer);
976 inconsistent++;
977 }
978 /* Values in the MTU options */
979 if (ndopts.nd_opts_mtu) {
980 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
981 if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
982 syslog(LOG_INFO,
983 "<%s> MTU option value inconsistent on %s:"
984 " %d from %s, %d from us",
985 __func__,
986 rai->ifname, mtu,
987 inet_ntop(AF_INET6, &from->sin6_addr,
988 ntopbuf, INET6_ADDRSTRLEN),
989 rai->linkmtu);
990 inconsistent++;
991 }
992 }
993 /* Preferred and Valid Lifetimes for prefixes */
994 {
995 struct nd_optlist *optp = ndopts.nd_opts_list;
996
997 if (ndopts.nd_opts_pi) {
998 if (prefix_check(ndopts.nd_opts_pi, rai, from))
999 inconsistent++;
1000 }
1001 while (optp) {
1002 if (prefix_check((struct nd_opt_prefix_info *)optp->opt,
1003 rai, from))
1004 inconsistent++;
1005 optp = optp->next;
1006 }
1007 }
1008
1009 if (inconsistent)
1010 rai->rainconsistent++;
1011
1012 done:
1013 free_ndopts(&ndopts);
1014 return;
1015 }
1016
1017 /* return a non-zero value if the received prefix is inconsitent with ours */
1018 static int
1019 prefix_check(struct nd_opt_prefix_info *pinfo,
1020 struct rainfo *rai, struct sockaddr_in6 *from)
1021 {
1022 u_int32_t preferred_time, valid_time;
1023 struct prefix *pp;
1024 int inconsistent = 0;
1025 u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
1026 struct timeval now;
1027
1028 #if 0 /* impossible */
1029 if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1030 return(0);
1031 #endif
1032
1033 /*
1034 * log if the adveritsed prefix has link-local scope(sanity check?)
1035 */
1036 if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
1037 syslog(LOG_INFO,
1038 "<%s> link-local prefix %s/%d is advertised "
1039 "from %s on %s",
1040 __func__,
1041 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1042 prefixbuf, INET6_ADDRSTRLEN),
1043 pinfo->nd_opt_pi_prefix_len,
1044 inet_ntop(AF_INET6, &from->sin6_addr,
1045 ntopbuf, INET6_ADDRSTRLEN),
1046 rai->ifname);
1047 }
1048
1049 if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1050 pinfo->nd_opt_pi_prefix_len)) == NULL) {
1051 syslog(LOG_INFO,
1052 "<%s> prefix %s/%d from %s on %s is not in our list",
1053 __func__,
1054 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1055 prefixbuf, INET6_ADDRSTRLEN),
1056 pinfo->nd_opt_pi_prefix_len,
1057 inet_ntop(AF_INET6, &from->sin6_addr,
1058 ntopbuf, INET6_ADDRSTRLEN),
1059 rai->ifname);
1060 return(0);
1061 }
1062
1063 preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1064 if (pp->pltimeexpire) {
1065 /*
1066 * The lifetime is decremented in real time, so we should
1067 * compare the expiration time.
1068 * (RFC 2461 Section 6.2.7.)
1069 * XXX: can we really expect that all routers on the link
1070 * have synchronized clocks?
1071 */
1072 gettimeofday(&now, NULL);
1073 preferred_time += now.tv_sec;
1074
1075 if (rai->clockskew &&
1076 abs(preferred_time - pp->pltimeexpire) > rai->clockskew) {
1077 syslog(LOG_INFO,
1078 "<%s> prefeerred lifetime for %s/%d"
1079 " (decr. in real time) inconsistent on %s:"
1080 " %d from %s, %ld from us",
1081 __func__,
1082 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1083 prefixbuf, INET6_ADDRSTRLEN),
1084 pinfo->nd_opt_pi_prefix_len,
1085 rai->ifname, preferred_time,
1086 inet_ntop(AF_INET6, &from->sin6_addr,
1087 ntopbuf, INET6_ADDRSTRLEN),
1088 pp->pltimeexpire);
1089 inconsistent++;
1090 }
1091 } else if (preferred_time != pp->preflifetime) {
1092 syslog(LOG_INFO,
1093 "<%s> prefeerred lifetime for %s/%d"
1094 " inconsistent on %s:"
1095 " %d from %s, %d from us",
1096 __func__,
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),
1103 pp->preflifetime);
1104 }
1105
1106 valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1107 if (pp->vltimeexpire) {
1108 gettimeofday(&now, NULL);
1109 valid_time += now.tv_sec;
1110
1111 if (rai->clockskew &&
1112 abs(valid_time - pp->vltimeexpire) > rai->clockskew) {
1113 syslog(LOG_INFO,
1114 "<%s> valid lifetime for %s/%d"
1115 " (decr. in real time) inconsistent on %s:"
1116 " %d from %s, %ld from us",
1117 __func__,
1118 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1119 prefixbuf, INET6_ADDRSTRLEN),
1120 pinfo->nd_opt_pi_prefix_len,
1121 rai->ifname, preferred_time,
1122 inet_ntop(AF_INET6, &from->sin6_addr,
1123 ntopbuf, INET6_ADDRSTRLEN),
1124 pp->vltimeexpire);
1125 inconsistent++;
1126 }
1127 } else if (valid_time != pp->validlifetime) {
1128 syslog(LOG_INFO,
1129 "<%s> valid lifetime for %s/%d"
1130 " inconsistent on %s:"
1131 " %d from %s, %d from us",
1132 __func__,
1133 inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
1134 prefixbuf, INET6_ADDRSTRLEN),
1135 pinfo->nd_opt_pi_prefix_len,
1136 rai->ifname, valid_time,
1137 inet_ntop(AF_INET6, &from->sin6_addr,
1138 ntopbuf, INET6_ADDRSTRLEN),
1139 pp->validlifetime);
1140 inconsistent++;
1141 }
1142
1143 return(inconsistent);
1144 }
1145
1146 struct prefix *
1147 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1148 {
1149 struct prefix *pp;
1150 int bytelen, bitlen;
1151 u_char bitmask;
1152
1153 for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
1154 if (plen != pp->prefixlen)
1155 continue;
1156 bytelen = plen / 8;
1157 bitlen = plen % 8;
1158 bitmask = 0xff << (8 - bitlen);
1159 if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
1160 continue;
1161 if (bitlen == 0 ||
1162 ((prefix->s6_addr[bytelen] & bitmask) ==
1163 (pp->prefix.s6_addr[bytelen] & bitmask))) {
1164 return(pp);
1165 }
1166 }
1167
1168 return(NULL);
1169 }
1170
1171 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1172 int
1173 prefix_match(struct in6_addr *p0, int plen0,
1174 struct in6_addr *p1, int plen1)
1175 {
1176 int bytelen, bitlen;
1177 u_char bitmask;
1178
1179 if (plen0 < plen1)
1180 return(0);
1181 bytelen = plen1 / 8;
1182 bitlen = plen1 % 8;
1183 bitmask = 0xff << (8 - bitlen);
1184 if (memcmp((void *)p0, (void *)p1, bytelen))
1185 return(0);
1186 if (bitlen == 0 ||
1187 ((p0->s6_addr[bytelen] & bitmask) ==
1188 (p1->s6_addr[bytelen] & bitmask))) {
1189 return(1);
1190 }
1191
1192 return(0);
1193 }
1194
1195 static int
1196 nd6_options(struct nd_opt_hdr *hdr, int limit,
1197 union nd_opts *ndopts, u_int32_t optflags)
1198 {
1199 int optlen = 0;
1200
1201 for (; limit > 0; limit -= optlen) {
1202 hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1203 optlen = hdr->nd_opt_len << 3;
1204 if (hdr->nd_opt_len == 0) {
1205 syslog(LOG_ERR,
1206 "<%s> bad ND option length(0) (type = %d)",
1207 __func__, hdr->nd_opt_type);
1208 goto bad;
1209 }
1210
1211 if (hdr->nd_opt_type > ND_OPT_MTU)
1212 {
1213 syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1214 __func__, hdr->nd_opt_type);
1215 continue;
1216 }
1217
1218 if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1219 syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1220 __func__, hdr->nd_opt_type);
1221 continue;
1222 }
1223
1224 switch (hdr->nd_opt_type) {
1225 case ND_OPT_SOURCE_LINKADDR:
1226 case ND_OPT_TARGET_LINKADDR:
1227 case ND_OPT_REDIRECTED_HEADER:
1228 case ND_OPT_MTU:
1229 if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
1230 syslog(LOG_INFO,
1231 "<%s> duplicated ND option (type = %d)",
1232 __func__, hdr->nd_opt_type);
1233 }
1234 ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
1235 break;
1236 case ND_OPT_PREFIX_INFORMATION:
1237 {
1238 struct nd_optlist *pfxlist;
1239
1240 if (ndopts->nd_opts_pi == 0) {
1241 ndopts->nd_opts_pi =
1242 (struct nd_opt_prefix_info *)hdr;
1243 continue;
1244 }
1245 if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
1246 syslog(LOG_ERR, "<%s> can't allocate memory",
1247 __func__);
1248 goto bad;
1249 }
1250 pfxlist->next = ndopts->nd_opts_list;
1251 pfxlist->opt = hdr;
1252 ndopts->nd_opts_list = pfxlist;
1253
1254 break;
1255 }
1256 default: /* impossible */
1257 break;
1258 }
1259 }
1260
1261 return(0);
1262
1263 bad:
1264 free_ndopts(ndopts);
1265
1266 return(-1);
1267 }
1268
1269 static void
1270 free_ndopts(union nd_opts *ndopts)
1271 {
1272 struct nd_optlist *opt = ndopts->nd_opts_list, *next;
1273
1274 while (opt) {
1275 next = opt->next;
1276 free(opt);
1277 opt = next;
1278 }
1279 }
1280
1281 void
1282 sock_open()
1283 {
1284 struct icmp6_filter filt;
1285 struct ipv6_mreq mreq;
1286 struct rainfo *ra = ralist;
1287 int on;
1288 /* XXX: should be max MTU attached to the node */
1289 static u_char answer[1500];
1290
1291 rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1292 CMSG_SPACE(sizeof(int));
1293 rcvcmsgbuf = (u_char *)malloc(rcvcmsgbuflen);
1294 if (rcvcmsgbuf == NULL) {
1295 syslog(LOG_ERR, "<%s> not enough core", __func__);
1296 exit(1);
1297 }
1298
1299 sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1300 CMSG_SPACE(sizeof(int));
1301 sndcmsgbuf = (u_char *)malloc(sndcmsgbuflen);
1302 if (sndcmsgbuf == NULL) {
1303 syslog(LOG_ERR, "<%s> not enough core", __func__);
1304 exit(1);
1305 }
1306
1307 if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1308 syslog(LOG_ERR, "<%s> socket: %s", __func__,
1309 strerror(errno));
1310 exit(1);
1311 }
1312
1313 /* specify to tell receiving interface */
1314 on = 1;
1315 #ifdef IPV6_RECVPKTINFO
1316 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1317 sizeof(on)) < 0) {
1318 syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s",
1319 __func__, strerror(errno));
1320 exit(1);
1321 }
1322 #else /* old adv. API */
1323 if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
1324 sizeof(on)) < 0) {
1325 syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
1326 __func__, strerror(errno));
1327 exit(1);
1328 }
1329 #endif
1330
1331 on = 1;
1332 /* specify to tell value of hoplimit field of received IP6 hdr */
1333 #ifdef IPV6_RECVHOPLIMIT
1334 if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1335 sizeof(on)) < 0) {
1336 syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s",
1337 __func__, strerror(errno));
1338 exit(1);
1339 }
1340 #else /* old adv. API */
1341 if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
1342 sizeof(on)) < 0) {
1343 syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
1344 __func__, strerror(errno));
1345 exit(1);
1346 }
1347 #endif
1348
1349 ICMP6_FILTER_SETBLOCKALL(&filt);
1350 ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1351 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1352 if (accept_rr)
1353 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1354 if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1355 sizeof(filt)) < 0) {
1356 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1357 __func__, strerror(errno));
1358 exit(1);
1359 }
1360
1361 /*
1362 * join all routers multicast address on each advertising interface.
1363 */
1364 if (inet_pton(AF_INET6, ALLROUTERS_LINK,
1365 &mreq.ipv6mr_multiaddr.s6_addr)
1366 != 1) {
1367 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1368 __func__);
1369 exit(1);
1370 }
1371 while (ra) {
1372 mreq.ipv6mr_interface = ra->ifindex;
1373 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
1374 sizeof(mreq)) < 0) {
1375 syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
1376 __func__, ra->ifname, strerror(errno));
1377 exit(1);
1378 }
1379 ra = ra->next;
1380 }
1381
1382 /*
1383 * When attending router renumbering, join all-routers site-local
1384 * multicast group.
1385 */
1386 if (accept_rr) {
1387 if (inet_pton(AF_INET6, ALLROUTERS_SITE,
1388 &in6a_site_allrouters) != 1) {
1389 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1390 __func__);
1391 exit(1);
1392 }
1393 mreq.ipv6mr_multiaddr = in6a_site_allrouters;
1394 if (mcastif) {
1395 if ((mreq.ipv6mr_interface = if_nametoindex(mcastif))
1396 == 0) {
1397 syslog(LOG_ERR,
1398 "<%s> invalid interface: %s",
1399 __func__, mcastif);
1400 exit(1);
1401 }
1402 } else
1403 mreq.ipv6mr_interface = ralist->ifindex;
1404 if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1405 &mreq, sizeof(mreq)) < 0) {
1406 syslog(LOG_ERR,
1407 "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
1408 __func__,
1409 mcastif ? mcastif : ralist->ifname,
1410 strerror(errno));
1411 exit(1);
1412 }
1413 }
1414
1415 /* initialize msghdr for receiving packets */
1416 rcviov[0].iov_base = (caddr_t)answer;
1417 rcviov[0].iov_len = sizeof(answer);
1418 rcvmhdr.msg_name = (caddr_t)&from;
1419 rcvmhdr.msg_namelen = sizeof(from);
1420 rcvmhdr.msg_iov = rcviov;
1421 rcvmhdr.msg_iovlen = 1;
1422 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1423 rcvmhdr.msg_controllen = rcvcmsgbuflen;
1424
1425 /* initialize msghdr for sending packets */
1426 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1427 sndmhdr.msg_iov = sndiov;
1428 sndmhdr.msg_iovlen = 1;
1429 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1430 sndmhdr.msg_controllen = sndcmsgbuflen;
1431
1432 return;
1433 }
1434
1435 /* open a routing socket to watch the routing table */
1436 static void
1437 rtsock_open()
1438 {
1439 if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1440 syslog(LOG_ERR,
1441 "<%s> socket: %s", __func__, strerror(errno));
1442 exit(1);
1443 }
1444 }
1445
1446 struct rainfo *
1447 if_indextorainfo(int index)
1448 {
1449 struct rainfo *rai = ralist;
1450
1451 for (rai = ralist; rai; rai = rai->next) {
1452 if (rai->ifindex == index)
1453 return(rai);
1454 }
1455
1456 return(NULL); /* search failed */
1457 }
1458
1459 static void
1460 ra_output(rainfo)
1461 struct rainfo *rainfo;
1462 {
1463 int i;
1464 struct cmsghdr *cm;
1465 struct in6_pktinfo *pi;
1466 struct soliciter *sol, *nextsol;
1467
1468 if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) {
1469 syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
1470 __func__, rainfo->ifname);
1471 return;
1472 }
1473
1474 make_packet(rainfo); /* XXX: inefficient */
1475
1476 sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
1477 sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
1478 sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
1479
1480 cm = CMSG_FIRSTHDR(&sndmhdr);
1481 /* specify the outgoing interface */
1482 cm->cmsg_level = IPPROTO_IPV6;
1483 cm->cmsg_type = IPV6_PKTINFO;
1484 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1485 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1486 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1487 pi->ipi6_ifindex = rainfo->ifindex;
1488
1489 /* specify the hop limit of the packet */
1490 {
1491 int hoplimit = 255;
1492
1493 cm = CMSG_NXTHDR(&sndmhdr, cm);
1494 cm->cmsg_level = IPPROTO_IPV6;
1495 cm->cmsg_type = IPV6_HOPLIMIT;
1496 cm->cmsg_len = CMSG_LEN(sizeof(int));
1497 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1498 }
1499
1500 syslog(LOG_DEBUG,
1501 "<%s> send RA on %s, # of waitings = %d",
1502 __func__, rainfo->ifname, rainfo->waiting);
1503
1504 i = sendmsg(sock, &sndmhdr, 0);
1505
1506 if (i < 0 || i != rainfo->ra_datalen) {
1507 if (i < 0) {
1508 syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1509 __func__, rainfo->ifname,
1510 strerror(errno));
1511 }
1512 }
1513
1514 /*
1515 * unicast advertisements
1516 * XXX commented out. reason: though spec does not forbit it, unicast
1517 * advert does not really help
1518 */
1519 for (sol = rainfo->soliciter; sol; sol = nextsol) {
1520 nextsol = sol->next;
1521
1522 #if 0
1523 sndmhdr.msg_name = (caddr_t)&sol->addr;
1524 i = sendmsg(sock, &sndmhdr, 0);
1525 if (i < 0 || i != rainfo->ra_datalen) {
1526 if (i < 0) {
1527 syslog(LOG_ERR,
1528 "<%s> unicast sendmsg on %s: %s",
1529 __func__, rainfo->ifname,
1530 strerror(errno));
1531 }
1532 }
1533 #endif
1534
1535 sol->next = NULL;
1536 free(sol);
1537 }
1538 rainfo->soliciter = NULL;
1539
1540 /* update counter */
1541 if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
1542 rainfo->initcounter++;
1543 rainfo->raoutput++;
1544
1545 /* update timestamp */
1546 gettimeofday(&rainfo->lastsent, NULL);
1547
1548 /* reset waiting conter */
1549 rainfo->waiting = 0;
1550 }
1551
1552 /* process RA timer */
1553 void
1554 ra_timeout(void *data)
1555 {
1556 struct rainfo *rai = (struct rainfo *)data;
1557
1558 #ifdef notyet
1559 /* if necessary, reconstruct the packet. */
1560 #endif
1561
1562 syslog(LOG_DEBUG,
1563 "<%s> RA timer on %s is expired",
1564 __func__, rai->ifname);
1565
1566 ra_output(rai);
1567 }
1568
1569 /* update RA timer */
1570 void
1571 ra_timer_update(void *data, struct timeval *tm)
1572 {
1573 struct rainfo *rai = (struct rainfo *)data;
1574 long interval;
1575
1576 /*
1577 * Whenever a multicast advertisement is sent from an interface,
1578 * the timer is reset to a uniformly-distributed random value
1579 * between the interface's configured MinRtrAdvInterval and
1580 * MaxRtrAdvInterval (RFC2461 6.2.4).
1581 */
1582 interval = rai->mininterval;
1583 interval += arc4random() % (rai->maxinterval - rai->mininterval);
1584
1585 /*
1586 * For the first few advertisements (up to
1587 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1588 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1589 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1590 * (RFC-2461 6.2.4)
1591 */
1592 if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
1593 interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1594 interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1595
1596 tm->tv_sec = interval;
1597 tm->tv_usec = 0;
1598
1599 syslog(LOG_DEBUG,
1600 "<%s> RA timer on %s is set to %ld:%ld",
1601 __func__, rai->ifname,
1602 (long int)tm->tv_sec, (long int)tm->tv_usec);
1603
1604 return;
1605 }
1606