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