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