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