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