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