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