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