config.c revision 1.40 1 1.40 roy /* $NetBSD: config.c,v 1.40 2018/04/20 15:57:23 roy Exp $ */
2 1.22 rpaulo /* $KAME: config.c,v 1.93 2005/10/17 14:40:02 suz Exp $ */
3 1.3 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (C) 1998 WIDE Project.
6 1.1 itojun * All rights reserved.
7 1.38 roy *
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.38 roy *
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/ioctl.h>
35 1.1 itojun #include <sys/socket.h>
36 1.1 itojun #include <sys/time.h>
37 1.8 itojun #include <sys/sysctl.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.30 roy #ifdef __FreeBSD__
43 1.30 roy #include <net/if_var.h>
44 1.30 roy #endif
45 1.1 itojun
46 1.1 itojun #include <netinet/in.h>
47 1.1 itojun #include <netinet/in_var.h>
48 1.5 itojun #include <netinet/ip6.h>
49 1.1 itojun #include <netinet6/ip6_var.h>
50 1.5 itojun #include <netinet/icmp6.h>
51 1.22 rpaulo #include <netinet6/nd6.h>
52 1.1 itojun
53 1.1 itojun #include <arpa/inet.h>
54 1.1 itojun
55 1.1 itojun #include <stdio.h>
56 1.1 itojun #include <syslog.h>
57 1.1 itojun #include <errno.h>
58 1.1 itojun #include <string.h>
59 1.1 itojun #include <stdlib.h>
60 1.1 itojun #include <search.h>
61 1.1 itojun #include <unistd.h>
62 1.8 itojun #include <ifaddrs.h>
63 1.30 roy #include <inttypes.h>
64 1.1 itojun
65 1.1 itojun #include "rtadvd.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.37 christos #include "logit.h"
71 1.35 ozaki #include "prog_ops.h"
72 1.1 itojun
73 1.30 roy #ifndef __arraycount
74 1.30 roy #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
75 1.30 roy #endif
76 1.30 roy
77 1.22 rpaulo static time_t prefix_timo = (60 * 120); /* 2 hours.
78 1.22 rpaulo * XXX: should be configurable. */
79 1.26 roy static struct rtadvd_timer *prefix_timeout(void *);
80 1.26 roy static void makeentry(char *, size_t, int, const char *);
81 1.26 roy static int getinet6sysctl(int);
82 1.22 rpaulo
83 1.26 roy static size_t
84 1.26 roy encode_domain(char *dst, const char *src)
85 1.26 roy {
86 1.26 roy ssize_t len;
87 1.26 roy char *odst, *p;
88 1.26 roy
89 1.26 roy odst = dst;
90 1.26 roy while (src && (len = strlen(src)) != 0) {
91 1.26 roy p = strchr(src, '.');
92 1.26 roy *dst++ = len = MIN(63, p == NULL ? len : p - src);
93 1.26 roy memcpy(dst, src, len);
94 1.26 roy dst += len;
95 1.26 roy if (p == NULL)
96 1.26 roy break;
97 1.26 roy src = p + 1;
98 1.26 roy }
99 1.26 roy *dst++ = '\0';
100 1.38 roy
101 1.26 roy return dst - odst;
102 1.26 roy }
103 1.1 itojun
104 1.1 itojun void
105 1.30 roy free_rainfo(struct rainfo *rai)
106 1.30 roy {
107 1.39 roy struct soliciter *sol;
108 1.30 roy struct prefix *pfx;
109 1.30 roy struct rtinfo *rti;
110 1.30 roy struct rdnss *rdnss;
111 1.30 roy struct rdnss_addr *rdnsa;
112 1.30 roy struct dnssl *dnssl;
113 1.30 roy struct dnssl_domain *dnsd;
114 1.30 roy
115 1.30 roy rtadvd_remove_timer(&rai->timer);
116 1.40 roy rtadvd_remove_timer(&rai->timer_sol);
117 1.30 roy
118 1.39 roy while ((sol = TAILQ_FIRST(&rai->soliciter))) {
119 1.39 roy TAILQ_REMOVE(&rai->soliciter, sol, next);
120 1.39 roy free(sol);
121 1.39 roy }
122 1.39 roy
123 1.30 roy while ((pfx = TAILQ_FIRST(&rai->prefix))) {
124 1.30 roy TAILQ_REMOVE(&rai->prefix, pfx, next);
125 1.30 roy free(pfx);
126 1.30 roy }
127 1.30 roy
128 1.30 roy while ((rti = TAILQ_FIRST(&rai->route))) {
129 1.30 roy TAILQ_REMOVE(&rai->route, rti, next);
130 1.30 roy free(rti);
131 1.30 roy }
132 1.30 roy
133 1.30 roy while ((rdnss = TAILQ_FIRST(&rai->rdnss))) {
134 1.30 roy TAILQ_REMOVE(&rai->rdnss, rdnss, next);
135 1.30 roy while ((rdnsa = TAILQ_FIRST(&rdnss->list))) {
136 1.30 roy TAILQ_REMOVE(&rdnss->list, rdnsa, next);
137 1.30 roy free(rdnsa);
138 1.30 roy }
139 1.30 roy free(rdnss);
140 1.30 roy }
141 1.30 roy
142 1.30 roy while ((dnssl = TAILQ_FIRST(&rai->dnssl))) {
143 1.30 roy TAILQ_REMOVE(&rai->dnssl, dnssl, next);
144 1.30 roy while ((dnsd = TAILQ_FIRST(&dnssl->list))) {
145 1.30 roy TAILQ_REMOVE(&dnssl->list, dnsd, next);
146 1.30 roy free(dnsd);
147 1.30 roy }
148 1.30 roy free(dnssl);
149 1.30 roy }
150 1.30 roy
151 1.30 roy free(rai->sdl);
152 1.30 roy free(rai->ra_data);
153 1.30 roy free(rai);
154 1.30 roy }
155 1.30 roy
156 1.30 roy void
157 1.30 roy getconfig(const char *intface, int exithard)
158 1.1 itojun {
159 1.26 roy int stat, c, i;
160 1.1 itojun char tbuf[BUFSIZ];
161 1.30 roy struct rainfo *tmp, *rai;
162 1.26 roy int32_t val;
163 1.14 itojun int64_t val64;
164 1.1 itojun char buf[BUFSIZ];
165 1.1 itojun char *bp = buf;
166 1.26 roy char *addr, *flagstr, *ap;
167 1.8 itojun static int forwarding = -1;
168 1.26 roy char entbuf[256], abuf[256];
169 1.30 roy struct rdnss *rdnss;
170 1.30 roy struct dnssl *dnssl;
171 1.1 itojun
172 1.1 itojun #define MUSTHAVE(var, cap) \
173 1.6 itojun do { \
174 1.14 itojun int64_t t; \
175 1.1 itojun if ((t = agetnum(cap)) < 0) { \
176 1.1 itojun fprintf(stderr, "rtadvd: need %s for interface %s\n", \
177 1.1 itojun cap, intface); \
178 1.30 roy goto errexit; \
179 1.1 itojun } \
180 1.1 itojun var = t; \
181 1.6 itojun } while (0)
182 1.1 itojun #define MAYHAVE(var, cap, def) \
183 1.6 itojun do { \
184 1.1 itojun if ((var = agetnum(cap)) < 0) \
185 1.1 itojun var = def; \
186 1.6 itojun } while (0)
187 1.30 roy #define ELM_MALLOC(p) \
188 1.26 roy do { \
189 1.27 christos p = calloc(1, sizeof(*p)); \
190 1.26 roy if (p == NULL) { \
191 1.37 christos logit(LOG_ERR, "<%s> calloc failed: %m", \
192 1.27 christos __func__); \
193 1.30 roy goto errexit; \
194 1.26 roy } \
195 1.27 christos } while(/*CONSTCOND*/0)
196 1.26 roy
197 1.30 roy if (if_nametoindex(intface) == 0) {
198 1.37 christos logit(LOG_INFO, "<%s> interface %s not found, ignoring",
199 1.30 roy __func__, intface);
200 1.30 roy return;
201 1.30 roy }
202 1.30 roy
203 1.37 christos logit(LOG_DEBUG, "<%s> loading configuration for interface %s",
204 1.30 roy __func__, intface);
205 1.1 itojun
206 1.1 itojun if ((stat = agetent(tbuf, intface)) <= 0) {
207 1.1 itojun memset(tbuf, 0, sizeof(tbuf));
208 1.37 christos logit(LOG_INFO,
209 1.1 itojun "<%s> %s isn't defined in the configuration file"
210 1.1 itojun " or the configuration file doesn't exist."
211 1.1 itojun " Treat it as default",
212 1.18 itojun __func__, intface);
213 1.1 itojun }
214 1.1 itojun
215 1.30 roy ELM_MALLOC(tmp);
216 1.39 roy TAILQ_INIT(&tmp->soliciter);
217 1.30 roy TAILQ_INIT(&tmp->prefix);
218 1.30 roy TAILQ_INIT(&tmp->route);
219 1.30 roy TAILQ_INIT(&tmp->rdnss);
220 1.30 roy TAILQ_INIT(&tmp->dnssl);
221 1.1 itojun
222 1.8 itojun /* check if we are allowed to forward packets (if not determined) */
223 1.8 itojun if (forwarding < 0) {
224 1.8 itojun if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
225 1.8 itojun exit(1);
226 1.8 itojun }
227 1.8 itojun
228 1.1 itojun /* get interface information */
229 1.1 itojun if (agetflag("nolladdr"))
230 1.1 itojun tmp->advlinkopt = 0;
231 1.1 itojun else
232 1.1 itojun tmp->advlinkopt = 1;
233 1.1 itojun if (tmp->advlinkopt) {
234 1.1 itojun if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
235 1.37 christos logit(LOG_ERR,
236 1.1 itojun "<%s> can't get information of %s",
237 1.18 itojun __func__, intface);
238 1.30 roy goto errexit;
239 1.1 itojun }
240 1.1 itojun tmp->ifindex = tmp->sdl->sdl_index;
241 1.30 roy } else {
242 1.1 itojun tmp->ifindex = if_nametoindex(intface);
243 1.30 roy if (tmp->ifindex == 0) {
244 1.37 christos logit(LOG_ERR,
245 1.30 roy "<%s> can't get information of %s",
246 1.30 roy __func__, intface);
247 1.30 roy goto errexit;
248 1.30 roy }
249 1.30 roy }
250 1.30 roy tmp->ifflags = if_getflags(tmp->ifindex, 0);
251 1.20 itojun strlcpy(tmp->ifname, intface, sizeof(tmp->ifname));
252 1.1 itojun if ((tmp->phymtu = if_getmtu(intface)) == 0) {
253 1.1 itojun tmp->phymtu = IPV6_MMTU;
254 1.37 christos logit(LOG_WARNING,
255 1.1 itojun "<%s> can't get interface mtu of %s. Treat as %d",
256 1.18 itojun __func__, intface, IPV6_MMTU);
257 1.1 itojun }
258 1.1 itojun
259 1.1 itojun /*
260 1.1 itojun * set router configuration variables.
261 1.1 itojun */
262 1.24 rpaulo MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
263 1.2 itojun if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
264 1.37 christos logit(LOG_ERR,
265 1.26 roy "<%s> maxinterval (%d) on %s is invalid "
266 1.22 rpaulo "(must be between %u and %u)", __func__, val,
267 1.14 itojun intface, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
268 1.30 roy goto errexit;
269 1.1 itojun }
270 1.26 roy tmp->maxinterval = val;
271 1.2 itojun MAYHAVE(val, "mininterval", tmp->maxinterval/3);
272 1.2 itojun if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
273 1.37 christos logit(LOG_ERR,
274 1.26 roy "<%s> mininterval (%d) on %s is invalid "
275 1.22 rpaulo "(must be between %u and %d)",
276 1.18 itojun __func__, val, intface, MIN_MININTERVAL,
277 1.1 itojun (tmp->maxinterval * 3) / 4);
278 1.30 roy goto errexit;
279 1.1 itojun }
280 1.26 roy tmp->mininterval = val;
281 1.1 itojun
282 1.2 itojun MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
283 1.2 itojun tmp->hoplimit = val & 0xff;
284 1.1 itojun
285 1.22 rpaulo if ((flagstr = (char *)agetstr("raflags", &bp))) {
286 1.22 rpaulo val = 0;
287 1.22 rpaulo if (strchr(flagstr, 'm'))
288 1.22 rpaulo val |= ND_RA_FLAG_MANAGED;
289 1.22 rpaulo if (strchr(flagstr, 'o'))
290 1.22 rpaulo val |= ND_RA_FLAG_OTHER;
291 1.22 rpaulo if (strchr(flagstr, 'h'))
292 1.22 rpaulo val |= ND_RA_FLAG_RTPREF_HIGH;
293 1.22 rpaulo if (strchr(flagstr, 'l')) {
294 1.22 rpaulo if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
295 1.37 christos logit(LOG_ERR, "<%s> the \'h\' and \'l\'"
296 1.22 rpaulo " router flags are exclusive", __func__);
297 1.30 roy goto errexit;
298 1.22 rpaulo }
299 1.22 rpaulo val |= ND_RA_FLAG_RTPREF_LOW;
300 1.22 rpaulo }
301 1.22 rpaulo } else {
302 1.22 rpaulo MAYHAVE(val, "raflags", 0);
303 1.22 rpaulo }
304 1.14 itojun tmp->managedflg = val & ND_RA_FLAG_MANAGED;
305 1.2 itojun tmp->otherflg = val & ND_RA_FLAG_OTHER;
306 1.14 itojun #ifndef ND_RA_FLAG_RTPREF_MASK
307 1.14 itojun #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
308 1.14 itojun #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
309 1.14 itojun #endif
310 1.14 itojun tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
311 1.14 itojun if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
312 1.37 christos logit(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
313 1.18 itojun __func__, tmp->rtpref, intface);
314 1.30 roy goto errexit;
315 1.14 itojun }
316 1.2 itojun
317 1.36 ozaki MAYHAVE(val, "rltime", DEF_ADVROUTERLIFETIME);
318 1.2 itojun if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
319 1.37 christos logit(LOG_ERR,
320 1.26 roy "<%s> router lifetime (%d) on %s is invalid "
321 1.14 itojun "(must be 0 or between %d and %d)",
322 1.18 itojun __func__, val, intface,
323 1.1 itojun tmp->maxinterval, MAXROUTERLIFETIME);
324 1.30 roy goto errexit;
325 1.1 itojun }
326 1.8 itojun /*
327 1.8 itojun * Basically, hosts MUST NOT send Router Advertisement messages at any
328 1.8 itojun * time (RFC 2461, Section 6.2.3). However, it would sometimes be
329 1.8 itojun * useful to allow hosts to advertise some parameters such as prefix
330 1.8 itojun * information and link MTU. Thus, we allow hosts to invoke rtadvd
331 1.8 itojun * only when router lifetime (on every advertising interface) is
332 1.8 itojun * explicitly set zero. (see also the above section)
333 1.8 itojun */
334 1.8 itojun if (val && forwarding == 0) {
335 1.37 christos logit(LOG_ERR,
336 1.8 itojun "<%s> non zero router lifetime is specified for %s, "
337 1.14 itojun "which must not be allowed for hosts. you must "
338 1.14 itojun "change router lifetime or enable IPv6 forwarding.",
339 1.18 itojun __func__, intface);
340 1.30 roy goto errexit;
341 1.8 itojun }
342 1.2 itojun tmp->lifetime = val & 0xffff;
343 1.1 itojun
344 1.2 itojun MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
345 1.14 itojun if (val < 0 || val > MAXREACHABLETIME) {
346 1.37 christos logit(LOG_ERR,
347 1.26 roy "<%s> reachable time (%d) on %s is invalid "
348 1.14 itojun "(must be no greater than %d)",
349 1.18 itojun __func__, val, intface, MAXREACHABLETIME);
350 1.30 roy goto errexit;
351 1.1 itojun }
352 1.26 roy tmp->reachabletime = (uint32_t)val;
353 1.1 itojun
354 1.11 itojun MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
355 1.11 itojun if (val64 < 0 || val64 > 0xffffffff) {
356 1.37 christos logit(LOG_ERR, "<%s> retrans time (%lld) on %s out of range",
357 1.18 itojun __func__, (long long)val64, intface);
358 1.30 roy goto errexit;
359 1.2 itojun }
360 1.26 roy tmp->retranstimer = (uint32_t)val64;
361 1.1 itojun
362 1.14 itojun if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
363 1.37 christos logit(LOG_ERR,
364 1.11 itojun "<%s> mobile-ip6 configuration not supported",
365 1.18 itojun __func__);
366 1.30 roy goto errexit;
367 1.11 itojun }
368 1.1 itojun /* prefix information */
369 1.8 itojun
370 1.8 itojun /*
371 1.21 itojun * This is an implementation specific parameter to consider
372 1.8 itojun * link propagation delays and poorly synchronized clocks when
373 1.8 itojun * checking consistency of advertised lifetimes.
374 1.8 itojun */
375 1.8 itojun MAYHAVE(val, "clockskew", 0);
376 1.8 itojun tmp->clockskew = val;
377 1.8 itojun
378 1.28 msaitoh tmp->pfxs = 0;
379 1.21 itojun for (i = -1; i < MAXPREFIX; i++) {
380 1.21 itojun struct prefix *pfx;
381 1.21 itojun
382 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "addr");
383 1.21 itojun addr = (char *)agetstr(entbuf, &bp);
384 1.21 itojun if (addr == NULL)
385 1.21 itojun continue;
386 1.21 itojun
387 1.21 itojun /* allocate memory to store prefix information */
388 1.27 christos if ((pfx = calloc(1, sizeof(*pfx))) == NULL) {
389 1.37 christos logit(LOG_ERR,
390 1.27 christos "<%s> can't allocate memory: %m",
391 1.21 itojun __func__);
392 1.30 roy goto errexit;
393 1.21 itojun }
394 1.21 itojun
395 1.26 roy TAILQ_INSERT_TAIL(&tmp->prefix, pfx, next);
396 1.26 roy tmp->pfxs++;
397 1.22 rpaulo pfx->rainfo = tmp;
398 1.21 itojun
399 1.21 itojun pfx->origin = PREFIX_FROM_CONFIG;
400 1.21 itojun
401 1.21 itojun if (inet_pton(AF_INET6, addr, &pfx->prefix) != 1) {
402 1.37 christos logit(LOG_ERR,
403 1.21 itojun "<%s> inet_pton failed for %s",
404 1.21 itojun __func__, addr);
405 1.30 roy goto errexit;
406 1.21 itojun }
407 1.21 itojun if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
408 1.37 christos logit(LOG_ERR,
409 1.21 itojun "<%s> multicast prefix (%s) must "
410 1.21 itojun "not be advertised on %s",
411 1.21 itojun __func__, addr, intface);
412 1.30 roy goto errexit;
413 1.21 itojun }
414 1.21 itojun if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
415 1.37 christos logit(LOG_NOTICE,
416 1.21 itojun "<%s> link-local prefix (%s) will be"
417 1.21 itojun " advertised on %s",
418 1.21 itojun __func__, addr, intface);
419 1.21 itojun
420 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
421 1.21 itojun MAYHAVE(val, entbuf, 64);
422 1.21 itojun if (val < 0 || val > 128) {
423 1.37 christos logit(LOG_ERR, "<%s> prefixlen (%d) for %s "
424 1.21 itojun "on %s out of range",
425 1.21 itojun __func__, val, addr, intface);
426 1.30 roy goto errexit;
427 1.21 itojun }
428 1.21 itojun pfx->prefixlen = (int)val;
429 1.21 itojun
430 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
431 1.22 rpaulo if ((flagstr = (char *)agetstr(entbuf, &bp))) {
432 1.22 rpaulo val = 0;
433 1.22 rpaulo if (strchr(flagstr, 'l'))
434 1.22 rpaulo val |= ND_OPT_PI_FLAG_ONLINK;
435 1.22 rpaulo if (strchr(flagstr, 'a'))
436 1.22 rpaulo val |= ND_OPT_PI_FLAG_AUTO;
437 1.22 rpaulo } else {
438 1.22 rpaulo MAYHAVE(val, entbuf,
439 1.22 rpaulo (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
440 1.22 rpaulo }
441 1.21 itojun pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
442 1.21 itojun pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
443 1.21 itojun
444 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "vltime");
445 1.21 itojun MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
446 1.21 itojun if (val64 < 0 || val64 > 0xffffffff) {
447 1.37 christos logit(LOG_ERR, "<%s> vltime (%lld) for "
448 1.21 itojun "%s/%d on %s is out of range",
449 1.21 itojun __func__, (long long)val64,
450 1.21 itojun addr, pfx->prefixlen, intface);
451 1.30 roy goto errexit;
452 1.21 itojun }
453 1.26 roy pfx->validlifetime = (uint32_t)val64;
454 1.21 itojun
455 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
456 1.21 itojun if (agetflag(entbuf)) {
457 1.34 roy struct timespec now;
458 1.35 ozaki prog_clock_gettime(CLOCK_MONOTONIC, &now);
459 1.21 itojun pfx->vltimeexpire =
460 1.21 itojun now.tv_sec + pfx->validlifetime;
461 1.21 itojun }
462 1.21 itojun
463 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pltime");
464 1.21 itojun MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
465 1.21 itojun if (val64 < 0 || val64 > 0xffffffff) {
466 1.37 christos logit(LOG_ERR,
467 1.21 itojun "<%s> pltime (%lld) for %s/%d on %s "
468 1.21 itojun "is out of range",
469 1.21 itojun __func__, (long long)val64,
470 1.21 itojun addr, pfx->prefixlen, intface);
471 1.30 roy goto errexit;
472 1.1 itojun }
473 1.26 roy pfx->preflifetime = (uint32_t)val64;
474 1.21 itojun
475 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
476 1.21 itojun if (agetflag(entbuf)) {
477 1.34 roy struct timespec now;
478 1.35 ozaki prog_clock_gettime(CLOCK_MONOTONIC, &now);
479 1.21 itojun pfx->pltimeexpire =
480 1.21 itojun now.tv_sec + pfx->preflifetime;
481 1.1 itojun }
482 1.1 itojun }
483 1.29 roy if (TAILQ_FIRST(&tmp->prefix) == NULL && !agetflag("noifprefix"))
484 1.21 itojun get_prefix(tmp);
485 1.1 itojun
486 1.26 roy MAYHAVE(val64, "mtu", 0);
487 1.26 roy if (val64 < 0 || val64 > 0xffffffff) {
488 1.37 christos logit(LOG_ERR,
489 1.26 roy "<%s> mtu (%" PRIi64 ") on %s out of range",
490 1.26 roy __func__, val64, intface);
491 1.30 roy goto errexit;
492 1.2 itojun }
493 1.26 roy tmp->linkmtu = (uint32_t)val64;
494 1.1 itojun if (tmp->linkmtu == 0) {
495 1.1 itojun char *mtustr;
496 1.1 itojun
497 1.1 itojun if ((mtustr = (char *)agetstr("mtu", &bp)) &&
498 1.1 itojun strcmp(mtustr, "auto") == 0)
499 1.1 itojun tmp->linkmtu = tmp->phymtu;
500 1.1 itojun }
501 1.1 itojun else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
502 1.37 christos logit(LOG_ERR,
503 1.26 roy "<%s> advertised link mtu (%d) on %s is invalid (must "
504 1.14 itojun "be between least MTU (%d) and physical link MTU (%d)",
505 1.26 roy __func__, tmp->linkmtu, intface,
506 1.14 itojun IPV6_MMTU, tmp->phymtu);
507 1.30 roy goto errexit;
508 1.1 itojun }
509 1.1 itojun
510 1.22 rpaulo #ifdef SIOCSIFINFO_IN6
511 1.22 rpaulo {
512 1.22 rpaulo struct in6_ndireq ndi;
513 1.22 rpaulo int s;
514 1.22 rpaulo
515 1.35 ozaki if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
516 1.37 christos logit(LOG_ERR, "<%s> socket: %m", __func__);
517 1.30 roy goto errexit;
518 1.22 rpaulo }
519 1.22 rpaulo memset(&ndi, 0, sizeof(ndi));
520 1.22 rpaulo strncpy(ndi.ifname, intface, IFNAMSIZ);
521 1.35 ozaki if (prog_ioctl(s, SIOCGIFINFO_IN6, &ndi) < 0) {
522 1.37 christos logit(LOG_INFO, "<%s> ioctl:SIOCGIFINFO_IN6 at %s: %m",
523 1.27 christos __func__, intface);
524 1.22 rpaulo }
525 1.22 rpaulo
526 1.22 rpaulo /* reflect the RA info to the host variables in kernel */
527 1.22 rpaulo ndi.ndi.chlim = tmp->hoplimit;
528 1.22 rpaulo ndi.ndi.retrans = tmp->retranstimer;
529 1.22 rpaulo ndi.ndi.basereachable = tmp->reachabletime;
530 1.35 ozaki if (prog_ioctl(s, SIOCSIFINFO_IN6, &ndi) < 0) {
531 1.37 christos logit(LOG_INFO, "<%s> ioctl:SIOCSIFINFO_IN6 at %s: %m",
532 1.27 christos __func__, intface);
533 1.22 rpaulo }
534 1.35 ozaki prog_close(s);
535 1.22 rpaulo }
536 1.22 rpaulo #endif
537 1.22 rpaulo
538 1.14 itojun /* route information */
539 1.22 rpaulo for (i = -1; i < MAXROUTE; i++) {
540 1.22 rpaulo struct rtinfo *rti;
541 1.26 roy char oentbuf[256];
542 1.22 rpaulo
543 1.22 rpaulo makeentry(entbuf, sizeof(entbuf), i, "rtprefix");
544 1.22 rpaulo addr = (char *)agetstr(entbuf, &bp);
545 1.22 rpaulo if (addr == NULL) {
546 1.22 rpaulo makeentry(oentbuf, sizeof(oentbuf), i, "rtrprefix");
547 1.22 rpaulo addr = (char *)agetstr(oentbuf, &bp);
548 1.22 rpaulo if (addr) {
549 1.22 rpaulo fprintf(stderr, "%s was obsoleted. Use %s.\n",
550 1.22 rpaulo oentbuf, entbuf);
551 1.22 rpaulo }
552 1.22 rpaulo }
553 1.22 rpaulo if (addr == NULL)
554 1.22 rpaulo continue;
555 1.22 rpaulo
556 1.30 roy ELM_MALLOC(rti);
557 1.22 rpaulo memset(rti, 0, sizeof(*rti));
558 1.22 rpaulo
559 1.22 rpaulo /* link into chain */
560 1.26 roy TAILQ_INSERT_TAIL(&tmp->route, rti, next);
561 1.22 rpaulo
562 1.22 rpaulo if (inet_pton(AF_INET6, addr, &rti->prefix) != 1) {
563 1.37 christos logit(LOG_ERR, "<%s> inet_pton failed for %s",
564 1.22 rpaulo __func__, addr);
565 1.30 roy goto errexit;
566 1.22 rpaulo }
567 1.22 rpaulo #if 0
568 1.22 rpaulo /*
569 1.22 rpaulo * XXX: currently there's no restriction in route information
570 1.22 rpaulo * prefix according to
571 1.22 rpaulo * draft-ietf-ipngwg-router-selection-00.txt.
572 1.22 rpaulo * However, I think the similar restriction be necessary.
573 1.22 rpaulo */
574 1.22 rpaulo MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
575 1.22 rpaulo if (IN6_IS_ADDR_MULTICAST(&rti->prefix)) {
576 1.37 christos logit(LOG_ERR,
577 1.22 rpaulo "<%s> multicast route (%s) must "
578 1.22 rpaulo "not be advertised on %s",
579 1.22 rpaulo __func__, addr, intface);
580 1.30 roy goto errexit;
581 1.22 rpaulo }
582 1.22 rpaulo if (IN6_IS_ADDR_LINKLOCAL(&rti->prefix)) {
583 1.37 christos logit(LOG_NOTICE,
584 1.22 rpaulo "<%s> link-local route (%s) will "
585 1.22 rpaulo "be advertised on %s",
586 1.22 rpaulo __func__, addr, intface);
587 1.30 roy goto errexit;
588 1.22 rpaulo }
589 1.22 rpaulo #endif
590 1.22 rpaulo
591 1.22 rpaulo makeentry(entbuf, sizeof(entbuf), i, "rtplen");
592 1.22 rpaulo /* XXX: 256 is a magic number for compatibility check. */
593 1.22 rpaulo MAYHAVE(val, entbuf, 256);
594 1.22 rpaulo if (val == 256) {
595 1.22 rpaulo makeentry(oentbuf, sizeof(oentbuf), i, "rtrplen");
596 1.22 rpaulo MAYHAVE(val, oentbuf, 256);
597 1.22 rpaulo if (val != 256) {
598 1.22 rpaulo fprintf(stderr, "%s was obsoleted. Use %s.\n",
599 1.22 rpaulo oentbuf, entbuf);
600 1.22 rpaulo } else
601 1.22 rpaulo val = 64;
602 1.22 rpaulo }
603 1.22 rpaulo if (val < 0 || val > 128) {
604 1.37 christos logit(LOG_ERR, "<%s> prefixlen (%d) for %s on %s "
605 1.22 rpaulo "out of range",
606 1.22 rpaulo __func__, val, addr, intface);
607 1.30 roy goto errexit;
608 1.22 rpaulo }
609 1.22 rpaulo rti->prefixlen = (int)val;
610 1.22 rpaulo
611 1.22 rpaulo makeentry(entbuf, sizeof(entbuf), i, "rtflags");
612 1.22 rpaulo if ((flagstr = (char *)agetstr(entbuf, &bp))) {
613 1.22 rpaulo val = 0;
614 1.22 rpaulo if (strchr(flagstr, 'h'))
615 1.22 rpaulo val |= ND_RA_FLAG_RTPREF_HIGH;
616 1.22 rpaulo if (strchr(flagstr, 'l')) {
617 1.22 rpaulo if ((val & ND_RA_FLAG_RTPREF_HIGH)) {
618 1.37 christos logit(LOG_ERR,
619 1.22 rpaulo "<%s> the \'h\' and \'l\' route"
620 1.22 rpaulo " preferences are exclusive",
621 1.22 rpaulo __func__);
622 1.30 roy goto errexit;
623 1.22 rpaulo }
624 1.22 rpaulo val |= ND_RA_FLAG_RTPREF_LOW;
625 1.22 rpaulo }
626 1.22 rpaulo } else
627 1.22 rpaulo MAYHAVE(val, entbuf, 256); /* XXX */
628 1.22 rpaulo if (val == 256) {
629 1.22 rpaulo makeentry(oentbuf, sizeof(oentbuf), i, "rtrflags");
630 1.22 rpaulo MAYHAVE(val, oentbuf, 256);
631 1.22 rpaulo if (val != 256) {
632 1.22 rpaulo fprintf(stderr, "%s was obsoleted. Use %s.\n",
633 1.22 rpaulo oentbuf, entbuf);
634 1.22 rpaulo } else
635 1.22 rpaulo val = 0;
636 1.22 rpaulo }
637 1.22 rpaulo rti->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
638 1.22 rpaulo if (rti->rtpref == ND_RA_FLAG_RTPREF_RSV) {
639 1.37 christos logit(LOG_ERR, "<%s> invalid route preference (%02x) "
640 1.22 rpaulo "for %s/%d on %s",
641 1.22 rpaulo __func__, rti->rtpref, addr,
642 1.22 rpaulo rti->prefixlen, intface);
643 1.30 roy goto errexit;
644 1.22 rpaulo }
645 1.22 rpaulo
646 1.22 rpaulo /*
647 1.22 rpaulo * Since the spec does not a default value, we should make
648 1.22 rpaulo * this entry mandatory. However, FreeBSD 4.4 has shipped
649 1.22 rpaulo * with this field being optional, we use the router lifetime
650 1.22 rpaulo * as an ad-hoc default value with a warning message.
651 1.22 rpaulo */
652 1.22 rpaulo makeentry(entbuf, sizeof(entbuf), i, "rtltime");
653 1.22 rpaulo MAYHAVE(val64, entbuf, -1);
654 1.22 rpaulo if (val64 == -1) {
655 1.22 rpaulo makeentry(oentbuf, sizeof(oentbuf), i, "rtrltime");
656 1.22 rpaulo MAYHAVE(val64, oentbuf, -1);
657 1.22 rpaulo if (val64 != -1) {
658 1.22 rpaulo fprintf(stderr, "%s was obsoleted. Use %s.\n",
659 1.22 rpaulo oentbuf, entbuf);
660 1.22 rpaulo } else {
661 1.22 rpaulo fprintf(stderr, "%s should be specified "
662 1.22 rpaulo "for interface %s.\n",
663 1.22 rpaulo entbuf, intface);
664 1.22 rpaulo val64 = tmp->lifetime;
665 1.22 rpaulo }
666 1.22 rpaulo }
667 1.22 rpaulo if (val64 < 0 || val64 > 0xffffffff) {
668 1.37 christos logit(LOG_ERR, "<%s> route lifetime (%lld) for "
669 1.22 rpaulo "%s/%d on %s out of range", __func__,
670 1.22 rpaulo (long long)val64, addr, rti->prefixlen, intface);
671 1.30 roy goto errexit;
672 1.22 rpaulo }
673 1.26 roy rti->ltime = (uint32_t)val64;
674 1.26 roy }
675 1.26 roy
676 1.26 roy /* RDNSS */
677 1.26 roy for (i = -1; i < MAXRDNSS; i++) {
678 1.26 roy struct rdnss_addr *rdnsa;
679 1.26 roy
680 1.26 roy makeentry(entbuf, sizeof(entbuf), i, "rdnss");
681 1.26 roy addr = (char *)agetstr(entbuf, &bp);
682 1.26 roy if (addr == NULL)
683 1.26 roy continue;
684 1.26 roy
685 1.30 roy ELM_MALLOC(rdnss);
686 1.30 roy TAILQ_INSERT_TAIL(&tmp->rdnss, rdnss, next);
687 1.26 roy TAILQ_INIT(&rdnss->list);
688 1.26 roy
689 1.26 roy for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
690 1.26 roy c = strcspn(ap, ",");
691 1.26 roy strncpy(abuf, ap, c);
692 1.26 roy abuf[c] = '\0';
693 1.30 roy ELM_MALLOC(rdnsa);
694 1.30 roy TAILQ_INSERT_TAIL(&rdnss->list, rdnsa, next);
695 1.26 roy if (inet_pton(AF_INET6, abuf, &rdnsa->addr) != 1) {
696 1.37 christos logit(LOG_ERR, "<%s> inet_pton failed for %s",
697 1.26 roy __func__, addr);
698 1.30 roy goto errexit;
699 1.26 roy }
700 1.26 roy }
701 1.26 roy
702 1.26 roy makeentry(entbuf, sizeof(entbuf), i, "rdnssltime");
703 1.26 roy MAYHAVE(val64, entbuf, tmp->maxinterval * 3 / 2);
704 1.26 roy if (val64 < tmp->maxinterval ||
705 1.26 roy val64 > tmp->maxinterval * 2)
706 1.26 roy {
707 1.37 christos logit(LOG_ERR, "<%s> %s (%lld) on %s is invalid",
708 1.38 roy __func__, entbuf, (long long)val64, intface);
709 1.30 roy goto errexit;
710 1.26 roy }
711 1.26 roy rdnss->lifetime = (uint32_t)val64;
712 1.26 roy
713 1.26 roy }
714 1.26 roy
715 1.26 roy /* DNSSL */
716 1.26 roy TAILQ_INIT(&tmp->dnssl);
717 1.26 roy for (i = -1; i < MAXDNSSL; i++) {
718 1.26 roy struct dnssl_domain *dnsd;
719 1.26 roy
720 1.26 roy makeentry(entbuf, sizeof(entbuf), i, "dnssl");
721 1.26 roy addr = (char *)agetstr(entbuf, &bp);
722 1.26 roy if (addr == NULL)
723 1.26 roy continue;
724 1.26 roy
725 1.30 roy ELM_MALLOC(dnssl);
726 1.30 roy TAILQ_INSERT_TAIL(&tmp->dnssl, dnssl, next);
727 1.26 roy TAILQ_INIT(&dnssl->list);
728 1.26 roy
729 1.26 roy for (ap = addr; ap - addr < (ssize_t)strlen(addr); ap += c+1) {
730 1.26 roy c = strcspn(ap, ",");
731 1.26 roy strncpy(abuf, ap, c);
732 1.26 roy abuf[c] = '\0';
733 1.30 roy ELM_MALLOC(dnsd);
734 1.30 roy TAILQ_INSERT_TAIL(&dnssl->list, dnsd, next);
735 1.26 roy dnsd->len = encode_domain(dnsd->domain, abuf);
736 1.26 roy }
737 1.26 roy
738 1.26 roy makeentry(entbuf, sizeof(entbuf), i, "dnsslltime");
739 1.26 roy MAYHAVE(val64, entbuf, tmp->maxinterval * 3 / 2);
740 1.26 roy if (val64 < tmp->maxinterval ||
741 1.26 roy val64 > tmp->maxinterval * 2)
742 1.26 roy {
743 1.37 christos logit(LOG_ERR, "<%s> %s (%lld) on %s is invalid",
744 1.38 roy __func__, entbuf, (long long)val64, intface);
745 1.30 roy goto errexit;
746 1.26 roy }
747 1.26 roy dnssl->lifetime = (uint32_t)val64;
748 1.26 roy
749 1.30 roy }
750 1.30 roy
751 1.30 roy TAILQ_FOREACH(rai, &ralist, next) {
752 1.30 roy if (rai->ifindex == tmp->ifindex) {
753 1.30 roy TAILQ_REMOVE(&ralist, rai, next);
754 1.30 roy /* If we already have a leaving RA use that
755 1.30 roy * as this config hasn't been advertised */
756 1.30 roy if (rai->leaving) {
757 1.30 roy tmp->leaving = rai->leaving;
758 1.30 roy free_rainfo(rai);
759 1.30 roy rai = tmp->leaving;
760 1.30 roy rai->leaving_for = tmp;
761 1.30 roy break;
762 1.30 roy }
763 1.30 roy rai->lifetime = 0;
764 1.30 roy TAILQ_FOREACH(rdnss, &rai->rdnss, next)
765 1.30 roy rdnss->lifetime = 0;
766 1.30 roy TAILQ_FOREACH(dnssl, &rai->dnssl, next)
767 1.30 roy dnssl->lifetime = 0;
768 1.30 roy rai->leaving_for = tmp;
769 1.30 roy tmp->leaving = rai;
770 1.31 roy rai->initcounter = MAX_INITIAL_RTR_ADVERTISEMENTS;
771 1.30 roy rai->mininterval = MIN_DELAY_BETWEEN_RAS;
772 1.30 roy rai->maxinterval = MIN_DELAY_BETWEEN_RAS;
773 1.30 roy rai->leaving_adv = MAX_FINAL_RTR_ADVERTISEMENTS;
774 1.30 roy if (rai->timer == NULL)
775 1.30 roy rai->timer = rtadvd_add_timer(ra_timeout,
776 1.30 roy ra_timer_update,
777 1.30 roy rai, rai);
778 1.30 roy ra_timer_update((void *)rai, &rai->timer->tm);
779 1.30 roy rtadvd_set_timer(&rai->timer->tm, rai->timer);
780 1.30 roy break;
781 1.30 roy }
782 1.22 rpaulo }
783 1.14 itojun
784 1.1 itojun /* okey */
785 1.26 roy TAILQ_INSERT_TAIL(&ralist, tmp, next);
786 1.1 itojun
787 1.1 itojun /* construct the sending packet */
788 1.1 itojun make_packet(tmp);
789 1.1 itojun
790 1.1 itojun /* set timer */
791 1.30 roy if (rai)
792 1.30 roy return;
793 1.1 itojun tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
794 1.1 itojun tmp, tmp);
795 1.40 roy ra_timer_set_short_delay(tmp, tmp->timer);
796 1.40 roy tmp->timer_sol = rtadvd_add_timer(ra_timeout_sol, NULL, tmp, NULL);
797 1.30 roy
798 1.30 roy return;
799 1.30 roy
800 1.30 roy errexit:
801 1.30 roy if (exithard)
802 1.30 roy exit(1);
803 1.30 roy free_rainfo(tmp);
804 1.1 itojun }
805 1.1 itojun
806 1.21 itojun void
807 1.1 itojun get_prefix(struct rainfo *rai)
808 1.1 itojun {
809 1.8 itojun struct ifaddrs *ifap, *ifa;
810 1.8 itojun struct prefix *pp;
811 1.8 itojun struct in6_addr *a;
812 1.26 roy unsigned char *p, *ep, *m, *lim;
813 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN];
814 1.8 itojun
815 1.8 itojun if (getifaddrs(&ifap) < 0) {
816 1.37 christos logit(LOG_ERR,
817 1.8 itojun "<%s> can't get interface addresses",
818 1.18 itojun __func__);
819 1.8 itojun exit(1);
820 1.8 itojun }
821 1.21 itojun
822 1.8 itojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
823 1.14 itojun int plen;
824 1.14 itojun
825 1.8 itojun if (strcmp(ifa->ifa_name, rai->ifname) != 0)
826 1.8 itojun continue;
827 1.8 itojun if (ifa->ifa_addr->sa_family != AF_INET6)
828 1.8 itojun continue;
829 1.8 itojun a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
830 1.8 itojun if (IN6_IS_ADDR_LINKLOCAL(a))
831 1.8 itojun continue;
832 1.14 itojun /* get prefix length */
833 1.26 roy m = (unsigned char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
834 1.26 roy lim = (unsigned char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
835 1.14 itojun plen = prefixlen(m, lim);
836 1.14 itojun if (plen <= 0 || plen > 128) {
837 1.37 christos logit(LOG_ERR, "<%s> failed to get prefixlen "
838 1.14 itojun "or prefix is invalid",
839 1.18 itojun __func__);
840 1.14 itojun exit(1);
841 1.14 itojun }
842 1.14 itojun if (plen == 128) /* XXX */
843 1.14 itojun continue;
844 1.14 itojun if (find_prefix(rai, a, plen)) {
845 1.14 itojun /* ignore a duplicated prefix. */
846 1.14 itojun continue;
847 1.14 itojun }
848 1.8 itojun
849 1.8 itojun /* allocate memory to store prefix info. */
850 1.27 christos if ((pp = calloc(1, sizeof(*pp))) == NULL) {
851 1.37 christos logit(LOG_ERR,
852 1.8 itojun "<%s> can't get allocate buffer for prefix",
853 1.18 itojun __func__);
854 1.8 itojun exit(1);
855 1.8 itojun }
856 1.8 itojun
857 1.8 itojun /* set prefix, sweep bits outside of prefixlen */
858 1.14 itojun pp->prefixlen = plen;
859 1.8 itojun memcpy(&pp->prefix, a, sizeof(*a));
860 1.21 itojun if (1)
861 1.14 itojun {
862 1.26 roy p = (unsigned char *)&pp->prefix;
863 1.26 roy ep = (unsigned char *)(&pp->prefix + 1);
864 1.23 itojun while (m < lim && p < ep)
865 1.14 itojun *p++ &= *m++;
866 1.14 itojun while (p < ep)
867 1.14 itojun *p++ = 0x00;
868 1.14 itojun }
869 1.8 itojun if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
870 1.8 itojun sizeof(ntopbuf))) {
871 1.37 christos logit(LOG_ERR, "<%s> inet_ntop failed", __func__);
872 1.8 itojun exit(1);
873 1.8 itojun }
874 1.37 christos logit(LOG_DEBUG,
875 1.8 itojun "<%s> add %s/%d to prefix list on %s",
876 1.18 itojun __func__, ntopbuf, pp->prefixlen, rai->ifname);
877 1.8 itojun
878 1.8 itojun /* set other fields with protocol defaults */
879 1.8 itojun pp->validlifetime = DEF_ADVVALIDLIFETIME;
880 1.8 itojun pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
881 1.8 itojun pp->onlinkflg = 1;
882 1.8 itojun pp->autoconfflg = 1;
883 1.8 itojun pp->origin = PREFIX_FROM_KERNEL;
884 1.22 rpaulo pp->rainfo = rai;
885 1.8 itojun
886 1.8 itojun /* link into chain */
887 1.26 roy TAILQ_INSERT_TAIL(&rai->prefix, pp, next);
888 1.28 msaitoh rai->pfxs++;
889 1.8 itojun }
890 1.8 itojun
891 1.8 itojun freeifaddrs(ifap);
892 1.1 itojun }
893 1.1 itojun
894 1.1 itojun static void
895 1.26 roy makeentry(char *buf, size_t len, int id, const char *string)
896 1.1 itojun {
897 1.14 itojun
898 1.21 itojun if (id < 0)
899 1.21 itojun strlcpy(buf, string, len);
900 1.21 itojun else
901 1.21 itojun snprintf(buf, len, "%s%d", string, id);
902 1.1 itojun }
903 1.1 itojun
904 1.1 itojun /*
905 1.1 itojun * Add a prefix to the list of specified interface and reconstruct
906 1.1 itojun * the outgoing packet.
907 1.1 itojun * The prefix must not be in the list.
908 1.22 rpaulo * XXX: other parameters of the prefix(e.g. lifetime) should be
909 1.1 itojun * able to be specified.
910 1.1 itojun */
911 1.1 itojun static void
912 1.1 itojun add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
913 1.1 itojun {
914 1.1 itojun struct prefix *prefix;
915 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN];
916 1.1 itojun
917 1.27 christos if ((prefix = calloc(1, sizeof(*prefix))) == NULL) {
918 1.37 christos logit(LOG_ERR, "<%s> memory allocation failed",
919 1.18 itojun __func__);
920 1.1 itojun return; /* XXX: error or exit? */
921 1.1 itojun }
922 1.1 itojun prefix->prefix = ipr->ipr_prefix.sin6_addr;
923 1.1 itojun prefix->prefixlen = ipr->ipr_plen;
924 1.1 itojun prefix->validlifetime = ipr->ipr_vltime;
925 1.1 itojun prefix->preflifetime = ipr->ipr_pltime;
926 1.1 itojun prefix->onlinkflg = ipr->ipr_raf_onlink;
927 1.1 itojun prefix->autoconfflg = ipr->ipr_raf_auto;
928 1.7 itojun prefix->origin = PREFIX_FROM_DYNAMIC;
929 1.1 itojun
930 1.22 rpaulo prefix->rainfo = rai;
931 1.26 roy TAILQ_INSERT_TAIL(&rai->prefix, prefix, next);
932 1.26 roy rai->pfxs++;
933 1.1 itojun
934 1.37 christos logit(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
935 1.18 itojun __func__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
936 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
937 1.1 itojun ipr->ipr_plen, rai->ifname);
938 1.1 itojun
939 1.1 itojun /* free the previous packet */
940 1.1 itojun free(rai->ra_data);
941 1.14 itojun rai->ra_data = NULL;
942 1.1 itojun
943 1.1 itojun /* reconstruct the packet */
944 1.1 itojun make_packet(rai);
945 1.1 itojun }
946 1.1 itojun
947 1.1 itojun /*
948 1.1 itojun * Delete a prefix to the list of specified interface and reconstruct
949 1.1 itojun * the outgoing packet.
950 1.7 itojun * The prefix must be in the list.
951 1.1 itojun */
952 1.1 itojun void
953 1.22 rpaulo delete_prefix(struct prefix *prefix)
954 1.1 itojun {
955 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN];
956 1.22 rpaulo struct rainfo *rai = prefix->rainfo;
957 1.1 itojun
958 1.26 roy TAILQ_REMOVE(&rai->prefix, prefix, next);
959 1.26 roy rai->pfxs--;
960 1.37 christos logit(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
961 1.18 itojun __func__, inet_ntop(AF_INET6, &prefix->prefix,
962 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
963 1.1 itojun prefix->prefixlen, rai->ifname);
964 1.30 roy rtadvd_remove_timer(&prefix->timer);
965 1.1 itojun free(prefix);
966 1.22 rpaulo }
967 1.22 rpaulo
968 1.22 rpaulo void
969 1.22 rpaulo invalidate_prefix(struct prefix *prefix)
970 1.22 rpaulo {
971 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN];
972 1.34 roy struct timespec timo;
973 1.22 rpaulo struct rainfo *rai = prefix->rainfo;
974 1.22 rpaulo
975 1.22 rpaulo if (prefix->timer) { /* sanity check */
976 1.37 christos logit(LOG_ERR,
977 1.22 rpaulo "<%s> assumption failure: timer already exists",
978 1.22 rpaulo __func__);
979 1.22 rpaulo exit(1);
980 1.22 rpaulo }
981 1.22 rpaulo
982 1.37 christos logit(LOG_DEBUG, "<%s> prefix %s/%d was invalidated on %s, "
983 1.22 rpaulo "will expire in %ld seconds", __func__,
984 1.22 rpaulo inet_ntop(AF_INET6, &prefix->prefix, ntopbuf, INET6_ADDRSTRLEN),
985 1.22 rpaulo prefix->prefixlen, rai->ifname, (long)prefix_timo);
986 1.22 rpaulo
987 1.22 rpaulo /* set the expiration timer */
988 1.22 rpaulo prefix->timer = rtadvd_add_timer(prefix_timeout, NULL, prefix, NULL);
989 1.22 rpaulo if (prefix->timer == NULL) {
990 1.37 christos logit(LOG_ERR, "<%s> failed to add a timer for a prefix. "
991 1.22 rpaulo "remove the prefix", __func__);
992 1.22 rpaulo delete_prefix(prefix);
993 1.22 rpaulo }
994 1.22 rpaulo timo.tv_sec = prefix_timo;
995 1.34 roy timo.tv_nsec = 0;
996 1.22 rpaulo rtadvd_set_timer(&timo, prefix->timer);
997 1.22 rpaulo }
998 1.22 rpaulo
999 1.22 rpaulo static struct rtadvd_timer *
1000 1.22 rpaulo prefix_timeout(void *arg)
1001 1.22 rpaulo {
1002 1.22 rpaulo struct prefix *prefix = (struct prefix *)arg;
1003 1.22 rpaulo
1004 1.22 rpaulo delete_prefix(prefix);
1005 1.22 rpaulo
1006 1.22 rpaulo return(NULL);
1007 1.22 rpaulo }
1008 1.22 rpaulo
1009 1.22 rpaulo void
1010 1.22 rpaulo update_prefix(struct prefix * prefix)
1011 1.22 rpaulo {
1012 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN];
1013 1.22 rpaulo struct rainfo *rai = prefix->rainfo;
1014 1.22 rpaulo
1015 1.22 rpaulo if (prefix->timer == NULL) { /* sanity check */
1016 1.37 christos logit(LOG_ERR,
1017 1.22 rpaulo "<%s> assumption failure: timer does not exist",
1018 1.22 rpaulo __func__);
1019 1.22 rpaulo exit(1);
1020 1.22 rpaulo }
1021 1.22 rpaulo
1022 1.37 christos logit(LOG_DEBUG, "<%s> prefix %s/%d was re-enabled on %s",
1023 1.22 rpaulo __func__, inet_ntop(AF_INET6, &prefix->prefix, ntopbuf,
1024 1.22 rpaulo INET6_ADDRSTRLEN), prefix->prefixlen, rai->ifname);
1025 1.22 rpaulo
1026 1.22 rpaulo /* stop the expiration timer */
1027 1.22 rpaulo rtadvd_remove_timer(&prefix->timer);
1028 1.1 itojun }
1029 1.1 itojun
1030 1.1 itojun /*
1031 1.1 itojun * Try to get an in6_prefixreq contents for a prefix which matches
1032 1.1 itojun * ipr->ipr_prefix and ipr->ipr_plen and belongs to
1033 1.1 itojun * the interface whose name is ipr->ipr_name[].
1034 1.1 itojun */
1035 1.1 itojun static int
1036 1.1 itojun init_prefix(struct in6_prefixreq *ipr)
1037 1.1 itojun {
1038 1.15 itojun #if 0
1039 1.1 itojun int s;
1040 1.1 itojun
1041 1.35 ozaki if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1042 1.37 christos logit(LOG_ERR, "<%s> socket: %m", __func__);
1043 1.1 itojun exit(1);
1044 1.1 itojun }
1045 1.1 itojun
1046 1.35 ozaki if (prog_ioctl(s, SIOCGIFPREFIX_IN6, ipr) < 0) {
1047 1.37 christos logit(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX: %m", __func__);
1048 1.1 itojun
1049 1.1 itojun ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1050 1.1 itojun ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1051 1.1 itojun ipr->ipr_raf_onlink = 1;
1052 1.1 itojun ipr->ipr_raf_auto = 1;
1053 1.1 itojun /* omit other field initialization */
1054 1.1 itojun }
1055 1.1 itojun else if (ipr->ipr_origin < PR_ORIG_RR) {
1056 1.25 mrg char ntopbuf[INET6_ADDRSTRLEN];
1057 1.1 itojun
1058 1.37 christos logit(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
1059 1.1 itojun "lower than PR_ORIG_RR(router renumbering)."
1060 1.18 itojun "This should not happen if I am router", __func__,
1061 1.1 itojun inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
1062 1.1 itojun sizeof(ntopbuf)), ipr->ipr_origin);
1063 1.35 ozaki prog_close(s);
1064 1.1 itojun return 1;
1065 1.1 itojun }
1066 1.1 itojun
1067 1.35 ozaki prog_close(s);
1068 1.1 itojun return 0;
1069 1.15 itojun #else
1070 1.15 itojun ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
1071 1.15 itojun ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
1072 1.15 itojun ipr->ipr_raf_onlink = 1;
1073 1.15 itojun ipr->ipr_raf_auto = 1;
1074 1.15 itojun return 0;
1075 1.15 itojun #endif
1076 1.1 itojun }
1077 1.1 itojun
1078 1.1 itojun void
1079 1.1 itojun make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
1080 1.1 itojun {
1081 1.1 itojun struct in6_prefixreq ipr;
1082 1.1 itojun
1083 1.1 itojun memset(&ipr, 0, sizeof(ipr));
1084 1.1 itojun if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
1085 1.37 christos logit(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
1086 1.27 christos "exist. This should not happen: %m", __func__,
1087 1.27 christos ifindex);
1088 1.1 itojun exit(1);
1089 1.1 itojun }
1090 1.1 itojun ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
1091 1.1 itojun ipr.ipr_prefix.sin6_family = AF_INET6;
1092 1.1 itojun ipr.ipr_prefix.sin6_addr = *addr;
1093 1.1 itojun ipr.ipr_plen = plen;
1094 1.1 itojun
1095 1.1 itojun if (init_prefix(&ipr))
1096 1.1 itojun return; /* init failed by some error */
1097 1.1 itojun add_prefix(rai, &ipr);
1098 1.1 itojun }
1099 1.1 itojun
1100 1.4 itojun void
1101 1.1 itojun make_packet(struct rainfo *rainfo)
1102 1.1 itojun {
1103 1.1 itojun size_t packlen, lladdroptlen = 0;
1104 1.26 roy char *buf;
1105 1.1 itojun struct nd_router_advert *ra;
1106 1.1 itojun struct nd_opt_prefix_info *ndopt_pi;
1107 1.1 itojun struct nd_opt_mtu *ndopt_mtu;
1108 1.1 itojun struct prefix *pfx;
1109 1.22 rpaulo struct nd_opt_route_info *ndopt_rti;
1110 1.22 rpaulo struct rtinfo *rti;
1111 1.26 roy struct nd_opt_rdnss *ndopt_rdnss;
1112 1.26 roy struct rdnss *rdns;
1113 1.26 roy struct rdnss_addr *rdnsa;
1114 1.26 roy struct nd_opt_dnssl *ndopt_dnssl;
1115 1.26 roy struct dnssl *dnsl;
1116 1.26 roy struct dnssl_domain *dnsd;
1117 1.26 roy size_t len, plen;
1118 1.1 itojun
1119 1.1 itojun /* calculate total length */
1120 1.1 itojun packlen = sizeof(struct nd_router_advert);
1121 1.1 itojun if (rainfo->advlinkopt) {
1122 1.1 itojun if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
1123 1.37 christos logit(LOG_INFO,
1124 1.1 itojun "<%s> link-layer address option has"
1125 1.14 itojun " null length on %s. Treat as not included.",
1126 1.18 itojun __func__, rainfo->ifname);
1127 1.1 itojun rainfo->advlinkopt = 0;
1128 1.1 itojun }
1129 1.1 itojun packlen += lladdroptlen;
1130 1.1 itojun }
1131 1.26 roy if (TAILQ_FIRST(&rainfo->prefix) != NULL)
1132 1.1 itojun packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
1133 1.1 itojun if (rainfo->linkmtu)
1134 1.1 itojun packlen += sizeof(struct nd_opt_mtu);
1135 1.38 roy TAILQ_FOREACH(rti, &rainfo->route, next)
1136 1.38 roy packlen += sizeof(struct nd_opt_route_info) +
1137 1.22 rpaulo ((rti->prefixlen + 0x3f) >> 6) * 8;
1138 1.26 roy
1139 1.26 roy TAILQ_FOREACH(rdns, &rainfo->rdnss, next) {
1140 1.26 roy packlen += sizeof(struct nd_opt_rdnss);
1141 1.26 roy TAILQ_FOREACH(rdnsa, &rdns->list, next)
1142 1.26 roy packlen += sizeof(rdnsa->addr);
1143 1.26 roy }
1144 1.26 roy TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) {
1145 1.26 roy packlen += sizeof(struct nd_opt_dnssl);
1146 1.26 roy len = 0;
1147 1.26 roy TAILQ_FOREACH(dnsd, &dnsl->list, next)
1148 1.26 roy len += dnsd->len;
1149 1.26 roy len += len % 8 ? 8 - len % 8 : 0;
1150 1.26 roy packlen += len;
1151 1.26 roy }
1152 1.1 itojun
1153 1.1 itojun /* allocate memory for the packet */
1154 1.27 christos if ((buf = realloc(rainfo->ra_data, packlen)) == NULL) {
1155 1.37 christos logit(LOG_ERR,
1156 1.27 christos "<%s> can't get enough memory for an RA packet %m",
1157 1.18 itojun __func__);
1158 1.1 itojun exit(1);
1159 1.1 itojun }
1160 1.1 itojun rainfo->ra_data = buf;
1161 1.1 itojun /* XXX: what if packlen > 576? */
1162 1.1 itojun rainfo->ra_datalen = packlen;
1163 1.27 christos #define CHECKLEN(size) \
1164 1.27 christos do { \
1165 1.27 christos if (buf + size > rainfo->ra_data + packlen) { \
1166 1.37 christos logit(LOG_ERR, \
1167 1.27 christos "<%s, %d> RA packet does not fit in %zu",\
1168 1.27 christos __func__, __LINE__, packlen); \
1169 1.27 christos exit(1); \
1170 1.27 christos } \
1171 1.27 christos } while (/*CONSTCOND*/0)
1172 1.1 itojun /*
1173 1.1 itojun * construct the packet
1174 1.1 itojun */
1175 1.27 christos CHECKLEN(sizeof(*ra));
1176 1.1 itojun ra = (struct nd_router_advert *)buf;
1177 1.1 itojun ra->nd_ra_type = ND_ROUTER_ADVERT;
1178 1.1 itojun ra->nd_ra_code = 0;
1179 1.1 itojun ra->nd_ra_cksum = 0;
1180 1.26 roy ra->nd_ra_curhoplimit = (uint8_t)(0xff & rainfo->hoplimit);
1181 1.14 itojun ra->nd_ra_flags_reserved = 0; /* just in case */
1182 1.14 itojun /*
1183 1.14 itojun * XXX: the router preference field, which is a 2-bit field, should be
1184 1.14 itojun * initialized before other fields.
1185 1.14 itojun */
1186 1.14 itojun ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
1187 1.1 itojun ra->nd_ra_flags_reserved |=
1188 1.1 itojun rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
1189 1.1 itojun ra->nd_ra_flags_reserved |=
1190 1.1 itojun rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
1191 1.1 itojun ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
1192 1.1 itojun ra->nd_ra_reachable = htonl(rainfo->reachabletime);
1193 1.1 itojun ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
1194 1.1 itojun buf += sizeof(*ra);
1195 1.1 itojun
1196 1.1 itojun if (rainfo->advlinkopt) {
1197 1.27 christos CHECKLEN(sizeof(struct nd_opt_hdr));
1198 1.1 itojun lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
1199 1.1 itojun buf += lladdroptlen;
1200 1.1 itojun }
1201 1.1 itojun
1202 1.1 itojun if (rainfo->linkmtu) {
1203 1.27 christos CHECKLEN(sizeof(*ndopt_mtu));
1204 1.1 itojun ndopt_mtu = (struct nd_opt_mtu *)buf;
1205 1.1 itojun ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
1206 1.1 itojun ndopt_mtu->nd_opt_mtu_len = 1;
1207 1.1 itojun ndopt_mtu->nd_opt_mtu_reserved = 0;
1208 1.12 itojun ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
1209 1.1 itojun buf += sizeof(struct nd_opt_mtu);
1210 1.1 itojun }
1211 1.1 itojun
1212 1.38 roy TAILQ_FOREACH(pfx, &rainfo->prefix, next) {
1213 1.26 roy uint32_t vltime, pltime;
1214 1.34 roy struct timespec now;
1215 1.8 itojun
1216 1.27 christos CHECKLEN(sizeof(*ndopt_pi));
1217 1.1 itojun ndopt_pi = (struct nd_opt_prefix_info *)buf;
1218 1.1 itojun ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
1219 1.1 itojun ndopt_pi->nd_opt_pi_len = 4;
1220 1.1 itojun ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
1221 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved = 0;
1222 1.1 itojun if (pfx->onlinkflg)
1223 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved |=
1224 1.1 itojun ND_OPT_PI_FLAG_ONLINK;
1225 1.1 itojun if (pfx->autoconfflg)
1226 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved |=
1227 1.1 itojun ND_OPT_PI_FLAG_AUTO;
1228 1.22 rpaulo if (pfx->timer)
1229 1.22 rpaulo vltime = 0;
1230 1.22 rpaulo else {
1231 1.22 rpaulo if (pfx->vltimeexpire || pfx->pltimeexpire)
1232 1.35 ozaki prog_clock_gettime(CLOCK_MONOTONIC, &now);
1233 1.22 rpaulo if (pfx->vltimeexpire == 0)
1234 1.22 rpaulo vltime = pfx->validlifetime;
1235 1.22 rpaulo else
1236 1.22 rpaulo vltime = (pfx->vltimeexpire > now.tv_sec) ?
1237 1.22 rpaulo pfx->vltimeexpire - now.tv_sec : 0;
1238 1.22 rpaulo }
1239 1.22 rpaulo if (pfx->timer)
1240 1.22 rpaulo pltime = 0;
1241 1.22 rpaulo else {
1242 1.22 rpaulo if (pfx->pltimeexpire == 0)
1243 1.22 rpaulo pltime = pfx->preflifetime;
1244 1.22 rpaulo else
1245 1.22 rpaulo pltime = (pfx->pltimeexpire > now.tv_sec) ?
1246 1.22 rpaulo pfx->pltimeexpire - now.tv_sec : 0;
1247 1.22 rpaulo }
1248 1.8 itojun if (vltime < pltime) {
1249 1.8 itojun /*
1250 1.8 itojun * this can happen if vltime is decrement but pltime
1251 1.8 itojun * is not.
1252 1.8 itojun */
1253 1.8 itojun pltime = vltime;
1254 1.8 itojun }
1255 1.12 itojun ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
1256 1.12 itojun ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
1257 1.1 itojun ndopt_pi->nd_opt_pi_reserved2 = 0;
1258 1.1 itojun ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
1259 1.1 itojun
1260 1.1 itojun buf += sizeof(struct nd_opt_prefix_info);
1261 1.1 itojun }
1262 1.1 itojun
1263 1.26 roy TAILQ_FOREACH(rti, &rainfo->route, next) {
1264 1.26 roy uint8_t psize = (rti->prefixlen + 0x3f) >> 6;
1265 1.22 rpaulo
1266 1.27 christos CHECKLEN(sizeof(*ndopt_rti));
1267 1.22 rpaulo ndopt_rti = (struct nd_opt_route_info *)buf;
1268 1.22 rpaulo ndopt_rti->nd_opt_rti_type = ND_OPT_ROUTE_INFO;
1269 1.22 rpaulo ndopt_rti->nd_opt_rti_len = 1 + psize;
1270 1.22 rpaulo ndopt_rti->nd_opt_rti_prefixlen = rti->prefixlen;
1271 1.22 rpaulo ndopt_rti->nd_opt_rti_flags = 0xff & rti->rtpref;
1272 1.22 rpaulo ndopt_rti->nd_opt_rti_lifetime = htonl(rti->ltime);
1273 1.22 rpaulo memcpy(ndopt_rti + 1, &rti->prefix, psize * 8);
1274 1.22 rpaulo buf += sizeof(struct nd_opt_route_info) + psize * 8;
1275 1.22 rpaulo }
1276 1.22 rpaulo
1277 1.26 roy TAILQ_FOREACH(rdns, &rainfo->rdnss, next) {
1278 1.27 christos CHECKLEN(sizeof(*ndopt_rdnss));
1279 1.26 roy ndopt_rdnss = (struct nd_opt_rdnss *)buf;
1280 1.26 roy ndopt_rdnss->nd_opt_rdnss_type = ND_OPT_RDNSS;
1281 1.26 roy ndopt_rdnss->nd_opt_rdnss_len = 1;
1282 1.26 roy ndopt_rdnss->nd_opt_rdnss_reserved = 0;
1283 1.26 roy ndopt_rdnss->nd_opt_rdnss_lifetime = htonl(rdns->lifetime);
1284 1.26 roy buf += sizeof(*ndopt_rdnss);
1285 1.38 roy
1286 1.26 roy TAILQ_FOREACH(rdnsa, &rdns->list, next) {
1287 1.27 christos CHECKLEN(sizeof(rdnsa->addr));
1288 1.26 roy memcpy(buf, &rdnsa->addr, sizeof(rdnsa->addr));
1289 1.26 roy ndopt_rdnss->nd_opt_rdnss_len += 2;
1290 1.26 roy buf += sizeof(rdnsa->addr);
1291 1.26 roy }
1292 1.26 roy }
1293 1.26 roy
1294 1.26 roy TAILQ_FOREACH(dnsl, &rainfo->dnssl, next) {
1295 1.27 christos CHECKLEN(sizeof(*ndopt_dnssl));
1296 1.26 roy ndopt_dnssl = (struct nd_opt_dnssl *)buf;
1297 1.26 roy ndopt_dnssl->nd_opt_dnssl_type = ND_OPT_DNSSL;
1298 1.26 roy ndopt_dnssl->nd_opt_dnssl_len = 0;
1299 1.26 roy ndopt_dnssl->nd_opt_dnssl_reserved = 0;
1300 1.26 roy ndopt_dnssl->nd_opt_dnssl_lifetime = htonl(dnsl->lifetime);
1301 1.26 roy buf += sizeof(*ndopt_dnssl);
1302 1.38 roy
1303 1.26 roy TAILQ_FOREACH(dnsd, &dnsl->list, next) {
1304 1.27 christos CHECKLEN(dnsd->len);
1305 1.26 roy memcpy(buf, dnsd->domain, dnsd->len);
1306 1.26 roy buf += dnsd->len;
1307 1.26 roy }
1308 1.26 roy /* Ensure our length is padded correctly */
1309 1.26 roy len = buf - (char *)ndopt_dnssl;
1310 1.26 roy plen = len % 8 ? 8 - len % 8 : 0;
1311 1.27 christos CHECKLEN(plen);
1312 1.26 roy memset(buf, 0, plen);
1313 1.26 roy buf += plen;
1314 1.26 roy ndopt_dnssl->nd_opt_dnssl_len = (len + plen) / 8;
1315 1.26 roy }
1316 1.27 christos memset(buf, 0, packlen - (buf - rainfo->ra_data));
1317 1.8 itojun }
1318 1.8 itojun
1319 1.8 itojun static int
1320 1.8 itojun getinet6sysctl(int code)
1321 1.8 itojun {
1322 1.33 christos const int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, code };
1323 1.8 itojun int value;
1324 1.8 itojun size_t size;
1325 1.8 itojun
1326 1.8 itojun size = sizeof(value);
1327 1.35 ozaki if (prog_sysctl(mib, __arraycount(mib), &value, &size, NULL, 0)
1328 1.8 itojun < 0) {
1329 1.37 christos logit(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %m",
1330 1.27 christos __func__, code);
1331 1.27 christos return -1;
1332 1.8 itojun }
1333 1.8 itojun else
1334 1.27 christos return value;
1335 1.1 itojun }
1336