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