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