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