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