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