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