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