config.c revision 1.21 1 1.21 itojun /* $NetBSD: config.c,v 1.21 2003/06/17 08:08:48 itojun Exp $ */
2 1.14 itojun /* $KAME: config.c,v 1.62 2002/05/29 10:13:10 itojun 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.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/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.1 itojun
43 1.1 itojun #include <netinet/in.h>
44 1.1 itojun #include <netinet/in_var.h>
45 1.5 itojun #include <netinet/ip6.h>
46 1.1 itojun #include <netinet6/ip6_var.h>
47 1.5 itojun #include <netinet/icmp6.h>
48 1.1 itojun
49 1.1 itojun #include <arpa/inet.h>
50 1.1 itojun
51 1.1 itojun #include <stdio.h>
52 1.1 itojun #include <syslog.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 <search.h>
57 1.1 itojun #include <unistd.h>
58 1.8 itojun #include <ifaddrs.h>
59 1.1 itojun
60 1.1 itojun #include "rtadvd.h"
61 1.1 itojun #include "advcap.h"
62 1.1 itojun #include "timer.h"
63 1.1 itojun #include "if.h"
64 1.1 itojun #include "config.h"
65 1.1 itojun
66 1.21 itojun static void makeentry __P((char *, size_t, int, char *));
67 1.8 itojun static int getinet6sysctl __P((int));
68 1.1 itojun
69 1.1 itojun extern struct rainfo *ralist;
70 1.1 itojun
71 1.1 itojun void
72 1.1 itojun getconfig(intface)
73 1.1 itojun char *intface;
74 1.1 itojun {
75 1.21 itojun int stat, i;
76 1.1 itojun char tbuf[BUFSIZ];
77 1.1 itojun struct rainfo *tmp;
78 1.2 itojun long val;
79 1.14 itojun int64_t val64;
80 1.1 itojun char buf[BUFSIZ];
81 1.1 itojun char *bp = buf;
82 1.1 itojun char *addr;
83 1.8 itojun static int forwarding = -1;
84 1.1 itojun
85 1.1 itojun #define MUSTHAVE(var, cap) \
86 1.6 itojun do { \
87 1.14 itojun int64_t t; \
88 1.1 itojun if ((t = agetnum(cap)) < 0) { \
89 1.1 itojun fprintf(stderr, "rtadvd: need %s for interface %s\n", \
90 1.1 itojun cap, intface); \
91 1.1 itojun exit(1); \
92 1.1 itojun } \
93 1.1 itojun var = t; \
94 1.6 itojun } while (0)
95 1.1 itojun #define MAYHAVE(var, cap, def) \
96 1.6 itojun do { \
97 1.1 itojun if ((var = agetnum(cap)) < 0) \
98 1.1 itojun var = def; \
99 1.6 itojun } while (0)
100 1.1 itojun
101 1.1 itojun if ((stat = agetent(tbuf, intface)) <= 0) {
102 1.1 itojun memset(tbuf, 0, sizeof(tbuf));
103 1.1 itojun syslog(LOG_INFO,
104 1.1 itojun "<%s> %s isn't defined in the configuration file"
105 1.1 itojun " or the configuration file doesn't exist."
106 1.1 itojun " Treat it as default",
107 1.18 itojun __func__, intface);
108 1.1 itojun }
109 1.1 itojun
110 1.1 itojun tmp = (struct rainfo *)malloc(sizeof(*ralist));
111 1.19 itojun if (tmp == NULL) {
112 1.19 itojun syslog(LOG_INFO, "<%s> %s: can't allocate enough memory",
113 1.19 itojun __func__, intface);
114 1.19 itojun exit(1);
115 1.19 itojun }
116 1.1 itojun memset(tmp, 0, sizeof(*tmp));
117 1.1 itojun tmp->prefix.next = tmp->prefix.prev = &tmp->prefix;
118 1.1 itojun
119 1.8 itojun /* check if we are allowed to forward packets (if not determined) */
120 1.8 itojun if (forwarding < 0) {
121 1.8 itojun if ((forwarding = getinet6sysctl(IPV6CTL_FORWARDING)) < 0)
122 1.8 itojun exit(1);
123 1.8 itojun }
124 1.8 itojun
125 1.1 itojun /* get interface information */
126 1.1 itojun if (agetflag("nolladdr"))
127 1.1 itojun tmp->advlinkopt = 0;
128 1.1 itojun else
129 1.1 itojun tmp->advlinkopt = 1;
130 1.1 itojun if (tmp->advlinkopt) {
131 1.1 itojun if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
132 1.1 itojun syslog(LOG_ERR,
133 1.1 itojun "<%s> can't get information of %s",
134 1.18 itojun __func__, intface);
135 1.1 itojun exit(1);
136 1.1 itojun }
137 1.1 itojun tmp->ifindex = tmp->sdl->sdl_index;
138 1.1 itojun } else
139 1.1 itojun tmp->ifindex = if_nametoindex(intface);
140 1.20 itojun strlcpy(tmp->ifname, intface, sizeof(tmp->ifname));
141 1.1 itojun if ((tmp->phymtu = if_getmtu(intface)) == 0) {
142 1.1 itojun tmp->phymtu = IPV6_MMTU;
143 1.1 itojun syslog(LOG_WARNING,
144 1.1 itojun "<%s> can't get interface mtu of %s. Treat as %d",
145 1.18 itojun __func__, intface, IPV6_MMTU);
146 1.1 itojun }
147 1.1 itojun
148 1.1 itojun /*
149 1.1 itojun * set router configuration variables.
150 1.1 itojun */
151 1.2 itojun MAYHAVE(val, "maxinterval", DEF_MAXRTRADVINTERVAL);
152 1.2 itojun if (val < MIN_MAXINTERVAL || val > MAX_MAXINTERVAL) {
153 1.1 itojun syslog(LOG_ERR,
154 1.14 itojun "<%s> maxinterval (%ld) on %s is invalid "
155 1.18 itojun "(must be between %e and %u)", __func__, val,
156 1.14 itojun intface, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
157 1.1 itojun exit(1);
158 1.1 itojun }
159 1.2 itojun tmp->maxinterval = (u_int)val;
160 1.2 itojun MAYHAVE(val, "mininterval", tmp->maxinterval/3);
161 1.2 itojun if (val < MIN_MININTERVAL || val > (tmp->maxinterval * 3) / 4) {
162 1.1 itojun syslog(LOG_ERR,
163 1.14 itojun "<%s> mininterval (%ld) on %s is invalid "
164 1.14 itojun "(must be between %e and %d)",
165 1.18 itojun __func__, val, intface, MIN_MININTERVAL,
166 1.1 itojun (tmp->maxinterval * 3) / 4);
167 1.1 itojun exit(1);
168 1.1 itojun }
169 1.2 itojun tmp->mininterval = (u_int)val;
170 1.1 itojun
171 1.2 itojun MAYHAVE(val, "chlim", DEF_ADVCURHOPLIMIT);
172 1.2 itojun tmp->hoplimit = val & 0xff;
173 1.1 itojun
174 1.2 itojun MAYHAVE(val, "raflags", 0);
175 1.14 itojun tmp->managedflg = val & ND_RA_FLAG_MANAGED;
176 1.2 itojun tmp->otherflg = val & ND_RA_FLAG_OTHER;
177 1.14 itojun #ifndef ND_RA_FLAG_RTPREF_MASK
178 1.14 itojun #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */
179 1.14 itojun #define ND_RA_FLAG_RTPREF_RSV 0x10 /* 00010000 */
180 1.14 itojun #endif
181 1.14 itojun tmp->rtpref = val & ND_RA_FLAG_RTPREF_MASK;
182 1.14 itojun if (tmp->rtpref == ND_RA_FLAG_RTPREF_RSV) {
183 1.14 itojun syslog(LOG_ERR, "<%s> invalid router preference (%02x) on %s",
184 1.18 itojun __func__, tmp->rtpref, intface);
185 1.14 itojun exit(1);
186 1.14 itojun }
187 1.2 itojun
188 1.2 itojun MAYHAVE(val, "rltime", tmp->maxinterval * 3);
189 1.2 itojun if (val && (val < tmp->maxinterval || val > MAXROUTERLIFETIME)) {
190 1.1 itojun syslog(LOG_ERR,
191 1.14 itojun "<%s> router lifetime (%ld) on %s is invalid "
192 1.14 itojun "(must be 0 or between %d and %d)",
193 1.18 itojun __func__, val, intface,
194 1.1 itojun tmp->maxinterval, MAXROUTERLIFETIME);
195 1.1 itojun exit(1);
196 1.1 itojun }
197 1.8 itojun /*
198 1.8 itojun * Basically, hosts MUST NOT send Router Advertisement messages at any
199 1.8 itojun * time (RFC 2461, Section 6.2.3). However, it would sometimes be
200 1.8 itojun * useful to allow hosts to advertise some parameters such as prefix
201 1.8 itojun * information and link MTU. Thus, we allow hosts to invoke rtadvd
202 1.8 itojun * only when router lifetime (on every advertising interface) is
203 1.8 itojun * explicitly set zero. (see also the above section)
204 1.8 itojun */
205 1.8 itojun if (val && forwarding == 0) {
206 1.14 itojun syslog(LOG_ERR,
207 1.8 itojun "<%s> non zero router lifetime is specified for %s, "
208 1.14 itojun "which must not be allowed for hosts. you must "
209 1.14 itojun "change router lifetime or enable IPv6 forwarding.",
210 1.18 itojun __func__, intface);
211 1.8 itojun exit(1);
212 1.8 itojun }
213 1.2 itojun tmp->lifetime = val & 0xffff;
214 1.1 itojun
215 1.2 itojun MAYHAVE(val, "rtime", DEF_ADVREACHABLETIME);
216 1.14 itojun if (val < 0 || val > MAXREACHABLETIME) {
217 1.1 itojun syslog(LOG_ERR,
218 1.14 itojun "<%s> reachable time (%ld) on %s is invalid "
219 1.14 itojun "(must be no greater than %d)",
220 1.18 itojun __func__, val, intface, MAXREACHABLETIME);
221 1.1 itojun exit(1);
222 1.1 itojun }
223 1.2 itojun tmp->reachabletime = (u_int32_t)val;
224 1.1 itojun
225 1.11 itojun MAYHAVE(val64, "retrans", DEF_ADVRETRANSTIMER);
226 1.11 itojun if (val64 < 0 || val64 > 0xffffffff) {
227 1.14 itojun syslog(LOG_ERR, "<%s> retrans time (%lld) on %s out of range",
228 1.18 itojun __func__, (long long)val64, intface);
229 1.2 itojun exit(1);
230 1.2 itojun }
231 1.11 itojun tmp->retranstimer = (u_int32_t)val64;
232 1.1 itojun
233 1.14 itojun if (agetnum("hapref") != -1 || agetnum("hatime") != -1) {
234 1.11 itojun syslog(LOG_ERR,
235 1.11 itojun "<%s> mobile-ip6 configuration not supported",
236 1.18 itojun __func__);
237 1.11 itojun exit(1);
238 1.11 itojun }
239 1.1 itojun /* prefix information */
240 1.8 itojun
241 1.8 itojun /*
242 1.21 itojun * This is an implementation specific parameter to consider
243 1.8 itojun * link propagation delays and poorly synchronized clocks when
244 1.8 itojun * checking consistency of advertised lifetimes.
245 1.8 itojun */
246 1.8 itojun MAYHAVE(val, "clockskew", 0);
247 1.8 itojun tmp->clockskew = val;
248 1.8 itojun
249 1.21 itojun tmp->pfxs = 0;
250 1.21 itojun for (i = -1; i < MAXPREFIX; i++) {
251 1.21 itojun struct prefix *pfx;
252 1.21 itojun char entbuf[256];
253 1.21 itojun
254 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "addr");
255 1.21 itojun addr = (char *)agetstr(entbuf, &bp);
256 1.21 itojun if (addr == NULL)
257 1.21 itojun continue;
258 1.21 itojun
259 1.21 itojun /* allocate memory to store prefix information */
260 1.21 itojun if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
261 1.21 itojun syslog(LOG_ERR,
262 1.21 itojun "<%s> can't allocate enough memory",
263 1.21 itojun __func__);
264 1.21 itojun exit(1);
265 1.21 itojun }
266 1.21 itojun memset(pfx, 0, sizeof(*pfx));
267 1.21 itojun
268 1.21 itojun /* link into chain */
269 1.21 itojun insque(pfx, &tmp->prefix);
270 1.21 itojun tmp->pfxs++;
271 1.21 itojun
272 1.21 itojun pfx->origin = PREFIX_FROM_CONFIG;
273 1.21 itojun
274 1.21 itojun if (inet_pton(AF_INET6, addr, &pfx->prefix) != 1) {
275 1.21 itojun syslog(LOG_ERR,
276 1.21 itojun "<%s> inet_pton failed for %s",
277 1.21 itojun __func__, addr);
278 1.21 itojun exit(1);
279 1.21 itojun }
280 1.21 itojun if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
281 1.21 itojun syslog(LOG_ERR,
282 1.21 itojun "<%s> multicast prefix (%s) must "
283 1.21 itojun "not be advertised on %s",
284 1.21 itojun __func__, addr, intface);
285 1.21 itojun exit(1);
286 1.21 itojun }
287 1.21 itojun if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
288 1.21 itojun syslog(LOG_NOTICE,
289 1.21 itojun "<%s> link-local prefix (%s) will be"
290 1.21 itojun " advertised on %s",
291 1.21 itojun __func__, addr, intface);
292 1.21 itojun
293 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "prefixlen");
294 1.21 itojun MAYHAVE(val, entbuf, 64);
295 1.21 itojun if (val < 0 || val > 128) {
296 1.21 itojun syslog(LOG_ERR, "<%s> prefixlen (%ld) for %s "
297 1.21 itojun "on %s out of range",
298 1.21 itojun __func__, val, addr, intface);
299 1.21 itojun exit(1);
300 1.21 itojun }
301 1.21 itojun pfx->prefixlen = (int)val;
302 1.21 itojun
303 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pinfoflags");
304 1.21 itojun MAYHAVE(val, entbuf,
305 1.21 itojun (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
306 1.21 itojun pfx->onlinkflg = val & ND_OPT_PI_FLAG_ONLINK;
307 1.21 itojun pfx->autoconfflg = val & ND_OPT_PI_FLAG_AUTO;
308 1.21 itojun
309 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "vltime");
310 1.21 itojun MAYHAVE(val64, entbuf, DEF_ADVVALIDLIFETIME);
311 1.21 itojun if (val64 < 0 || val64 > 0xffffffff) {
312 1.21 itojun syslog(LOG_ERR, "<%s> vltime (%lld) for "
313 1.21 itojun "%s/%d on %s is out of range",
314 1.21 itojun __func__, (long long)val64,
315 1.21 itojun addr, pfx->prefixlen, intface);
316 1.21 itojun exit(1);
317 1.21 itojun }
318 1.21 itojun pfx->validlifetime = (u_int32_t)val64;
319 1.21 itojun
320 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "vltimedecr");
321 1.21 itojun if (agetflag(entbuf)) {
322 1.21 itojun struct timeval now;
323 1.21 itojun gettimeofday(&now, 0);
324 1.21 itojun pfx->vltimeexpire =
325 1.21 itojun now.tv_sec + pfx->validlifetime;
326 1.21 itojun }
327 1.21 itojun
328 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pltime");
329 1.21 itojun MAYHAVE(val64, entbuf, DEF_ADVPREFERREDLIFETIME);
330 1.21 itojun if (val64 < 0 || val64 > 0xffffffff) {
331 1.1 itojun syslog(LOG_ERR,
332 1.21 itojun "<%s> pltime (%lld) for %s/%d on %s "
333 1.21 itojun "is out of range",
334 1.21 itojun __func__, (long long)val64,
335 1.21 itojun addr, pfx->prefixlen, intface);
336 1.1 itojun exit(1);
337 1.1 itojun }
338 1.21 itojun pfx->preflifetime = (u_int32_t)val64;
339 1.21 itojun
340 1.21 itojun makeentry(entbuf, sizeof(entbuf), i, "pltimedecr");
341 1.21 itojun if (agetflag(entbuf)) {
342 1.21 itojun struct timeval now;
343 1.21 itojun gettimeofday(&now, 0);
344 1.21 itojun pfx->pltimeexpire =
345 1.21 itojun now.tv_sec + pfx->preflifetime;
346 1.1 itojun }
347 1.1 itojun }
348 1.21 itojun if (tmp->pfxs == 0)
349 1.21 itojun get_prefix(tmp);
350 1.1 itojun
351 1.2 itojun MAYHAVE(val, "mtu", 0);
352 1.2 itojun if (val < 0 || val > 0xffffffff) {
353 1.2 itojun syslog(LOG_ERR,
354 1.14 itojun "<%s> mtu (%ld) on %s out of range",
355 1.18 itojun __func__, val, intface);
356 1.2 itojun exit(1);
357 1.2 itojun }
358 1.2 itojun tmp->linkmtu = (u_int32_t)val;
359 1.1 itojun if (tmp->linkmtu == 0) {
360 1.1 itojun char *mtustr;
361 1.1 itojun
362 1.1 itojun if ((mtustr = (char *)agetstr("mtu", &bp)) &&
363 1.1 itojun strcmp(mtustr, "auto") == 0)
364 1.1 itojun tmp->linkmtu = tmp->phymtu;
365 1.1 itojun }
366 1.1 itojun else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
367 1.1 itojun syslog(LOG_ERR,
368 1.14 itojun "<%s> advertised link mtu (%lu) on %s is invalid (must "
369 1.14 itojun "be between least MTU (%d) and physical link MTU (%d)",
370 1.18 itojun __func__, (unsigned long)tmp->linkmtu, intface,
371 1.14 itojun IPV6_MMTU, tmp->phymtu);
372 1.1 itojun exit(1);
373 1.1 itojun }
374 1.1 itojun
375 1.14 itojun /* route information */
376 1.14 itojun MAYHAVE(val, "routes", -1);
377 1.14 itojun if (val != -1)
378 1.14 itojun syslog(LOG_INFO, "route information option is not available");
379 1.14 itojun
380 1.1 itojun /* okey */
381 1.1 itojun tmp->next = ralist;
382 1.1 itojun ralist = tmp;
383 1.1 itojun
384 1.1 itojun /* construct the sending packet */
385 1.1 itojun make_packet(tmp);
386 1.1 itojun
387 1.1 itojun /* set timer */
388 1.1 itojun tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
389 1.1 itojun tmp, tmp);
390 1.1 itojun ra_timer_update((void *)tmp, &tmp->timer->tm);
391 1.1 itojun rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
392 1.1 itojun }
393 1.1 itojun
394 1.21 itojun void
395 1.1 itojun get_prefix(struct rainfo *rai)
396 1.1 itojun {
397 1.8 itojun struct ifaddrs *ifap, *ifa;
398 1.8 itojun struct prefix *pp;
399 1.8 itojun struct in6_addr *a;
400 1.8 itojun u_char *p, *ep, *m, *lim;
401 1.8 itojun u_char ntopbuf[INET6_ADDRSTRLEN];
402 1.8 itojun
403 1.8 itojun if (getifaddrs(&ifap) < 0) {
404 1.8 itojun syslog(LOG_ERR,
405 1.8 itojun "<%s> can't get interface addresses",
406 1.18 itojun __func__);
407 1.8 itojun exit(1);
408 1.8 itojun }
409 1.21 itojun
410 1.8 itojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
411 1.14 itojun int plen;
412 1.14 itojun
413 1.8 itojun if (strcmp(ifa->ifa_name, rai->ifname) != 0)
414 1.8 itojun continue;
415 1.8 itojun if (ifa->ifa_addr->sa_family != AF_INET6)
416 1.8 itojun continue;
417 1.8 itojun a = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
418 1.8 itojun if (IN6_IS_ADDR_LINKLOCAL(a))
419 1.8 itojun continue;
420 1.14 itojun /* get prefix length */
421 1.14 itojun m = (u_char *)&((struct sockaddr_in6 *)ifa->ifa_netmask)->sin6_addr;
422 1.14 itojun lim = (u_char *)(ifa->ifa_netmask) + ifa->ifa_netmask->sa_len;
423 1.14 itojun plen = prefixlen(m, lim);
424 1.14 itojun if (plen <= 0 || plen > 128) {
425 1.14 itojun syslog(LOG_ERR, "<%s> failed to get prefixlen "
426 1.14 itojun "or prefix is invalid",
427 1.18 itojun __func__);
428 1.14 itojun exit(1);
429 1.14 itojun }
430 1.14 itojun if (plen == 128) /* XXX */
431 1.14 itojun continue;
432 1.14 itojun if (find_prefix(rai, a, plen)) {
433 1.14 itojun /* ignore a duplicated prefix. */
434 1.14 itojun continue;
435 1.14 itojun }
436 1.8 itojun
437 1.8 itojun /* allocate memory to store prefix info. */
438 1.8 itojun if ((pp = malloc(sizeof(*pp))) == NULL) {
439 1.8 itojun syslog(LOG_ERR,
440 1.8 itojun "<%s> can't get allocate buffer for prefix",
441 1.18 itojun __func__);
442 1.8 itojun exit(1);
443 1.8 itojun }
444 1.8 itojun memset(pp, 0, sizeof(*pp));
445 1.8 itojun
446 1.8 itojun /* set prefix, sweep bits outside of prefixlen */
447 1.14 itojun pp->prefixlen = plen;
448 1.8 itojun memcpy(&pp->prefix, a, sizeof(*a));
449 1.21 itojun if (1)
450 1.14 itojun {
451 1.14 itojun p = (u_char *)&pp->prefix;
452 1.14 itojun ep = (u_char *)(&pp->prefix + 1);
453 1.14 itojun while (m < lim)
454 1.14 itojun *p++ &= *m++;
455 1.14 itojun while (p < ep)
456 1.14 itojun *p++ = 0x00;
457 1.14 itojun }
458 1.8 itojun if (!inet_ntop(AF_INET6, &pp->prefix, ntopbuf,
459 1.8 itojun sizeof(ntopbuf))) {
460 1.18 itojun syslog(LOG_ERR, "<%s> inet_ntop failed", __func__);
461 1.8 itojun exit(1);
462 1.8 itojun }
463 1.8 itojun syslog(LOG_DEBUG,
464 1.8 itojun "<%s> add %s/%d to prefix list on %s",
465 1.18 itojun __func__, ntopbuf, pp->prefixlen, rai->ifname);
466 1.8 itojun
467 1.8 itojun /* set other fields with protocol defaults */
468 1.8 itojun pp->validlifetime = DEF_ADVVALIDLIFETIME;
469 1.8 itojun pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
470 1.8 itojun pp->onlinkflg = 1;
471 1.8 itojun pp->autoconfflg = 1;
472 1.8 itojun pp->origin = PREFIX_FROM_KERNEL;
473 1.8 itojun
474 1.8 itojun /* link into chain */
475 1.8 itojun insque(pp, &rai->prefix);
476 1.8 itojun
477 1.8 itojun /* counter increment */
478 1.8 itojun rai->pfxs++;
479 1.8 itojun }
480 1.8 itojun
481 1.8 itojun freeifaddrs(ifap);
482 1.1 itojun }
483 1.1 itojun
484 1.1 itojun static void
485 1.21 itojun makeentry(buf, len, id, string)
486 1.14 itojun char *buf;
487 1.14 itojun size_t len;
488 1.14 itojun int id;
489 1.14 itojun char *string;
490 1.1 itojun {
491 1.14 itojun
492 1.21 itojun if (id < 0)
493 1.21 itojun strlcpy(buf, string, len);
494 1.21 itojun else
495 1.21 itojun snprintf(buf, len, "%s%d", string, id);
496 1.1 itojun }
497 1.1 itojun
498 1.1 itojun /*
499 1.1 itojun * Add a prefix to the list of specified interface and reconstruct
500 1.1 itojun * the outgoing packet.
501 1.1 itojun * The prefix must not be in the list.
502 1.1 itojun * XXX: other parameter of the prefix(e.g. lifetime) shoule be
503 1.1 itojun * able to be specified.
504 1.1 itojun */
505 1.1 itojun static void
506 1.1 itojun add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
507 1.1 itojun {
508 1.1 itojun struct prefix *prefix;
509 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN];
510 1.1 itojun
511 1.1 itojun if ((prefix = malloc(sizeof(*prefix))) == NULL) {
512 1.1 itojun syslog(LOG_ERR, "<%s> memory allocation failed",
513 1.18 itojun __func__);
514 1.1 itojun return; /* XXX: error or exit? */
515 1.1 itojun }
516 1.8 itojun memset(prefix, 0, sizeof(*prefix));
517 1.1 itojun prefix->prefix = ipr->ipr_prefix.sin6_addr;
518 1.1 itojun prefix->prefixlen = ipr->ipr_plen;
519 1.1 itojun prefix->validlifetime = ipr->ipr_vltime;
520 1.1 itojun prefix->preflifetime = ipr->ipr_pltime;
521 1.1 itojun prefix->onlinkflg = ipr->ipr_raf_onlink;
522 1.1 itojun prefix->autoconfflg = ipr->ipr_raf_auto;
523 1.7 itojun prefix->origin = PREFIX_FROM_DYNAMIC;
524 1.1 itojun
525 1.1 itojun insque(prefix, &rai->prefix);
526 1.1 itojun
527 1.1 itojun syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
528 1.18 itojun __func__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
529 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
530 1.1 itojun ipr->ipr_plen, rai->ifname);
531 1.1 itojun
532 1.1 itojun /* free the previous packet */
533 1.1 itojun free(rai->ra_data);
534 1.14 itojun rai->ra_data = NULL;
535 1.1 itojun
536 1.1 itojun /* reconstruct the packet */
537 1.1 itojun rai->pfxs++;
538 1.1 itojun make_packet(rai);
539 1.1 itojun
540 1.1 itojun /*
541 1.1 itojun * reset the timer so that the new prefix will be advertised quickly.
542 1.1 itojun */
543 1.1 itojun rai->initcounter = 0;
544 1.1 itojun ra_timer_update((void *)rai, &rai->timer->tm);
545 1.1 itojun rtadvd_set_timer(&rai->timer->tm, rai->timer);
546 1.1 itojun }
547 1.1 itojun
548 1.1 itojun /*
549 1.1 itojun * Delete a prefix to the list of specified interface and reconstruct
550 1.1 itojun * the outgoing packet.
551 1.7 itojun * The prefix must be in the list.
552 1.1 itojun */
553 1.1 itojun void
554 1.1 itojun delete_prefix(struct rainfo *rai, struct prefix *prefix)
555 1.1 itojun {
556 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN];
557 1.1 itojun
558 1.1 itojun remque(prefix);
559 1.1 itojun syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
560 1.18 itojun __func__, inet_ntop(AF_INET6, &prefix->prefix,
561 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
562 1.1 itojun prefix->prefixlen, rai->ifname);
563 1.1 itojun free(prefix);
564 1.1 itojun rai->pfxs--;
565 1.1 itojun make_packet(rai);
566 1.1 itojun }
567 1.1 itojun
568 1.1 itojun /*
569 1.1 itojun * Try to get an in6_prefixreq contents for a prefix which matches
570 1.1 itojun * ipr->ipr_prefix and ipr->ipr_plen and belongs to
571 1.1 itojun * the interface whose name is ipr->ipr_name[].
572 1.1 itojun */
573 1.1 itojun static int
574 1.1 itojun init_prefix(struct in6_prefixreq *ipr)
575 1.1 itojun {
576 1.15 itojun #if 0
577 1.1 itojun int s;
578 1.1 itojun
579 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
580 1.18 itojun syslog(LOG_ERR, "<%s> socket: %s", __func__,
581 1.1 itojun strerror(errno));
582 1.1 itojun exit(1);
583 1.1 itojun }
584 1.1 itojun
585 1.1 itojun if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
586 1.18 itojun syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __func__,
587 1.1 itojun strerror(errno));
588 1.1 itojun
589 1.1 itojun ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
590 1.1 itojun ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
591 1.1 itojun ipr->ipr_raf_onlink = 1;
592 1.1 itojun ipr->ipr_raf_auto = 1;
593 1.1 itojun /* omit other field initialization */
594 1.1 itojun }
595 1.1 itojun else if (ipr->ipr_origin < PR_ORIG_RR) {
596 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN];
597 1.1 itojun
598 1.1 itojun syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
599 1.1 itojun "lower than PR_ORIG_RR(router renumbering)."
600 1.18 itojun "This should not happen if I am router", __func__,
601 1.1 itojun inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
602 1.1 itojun sizeof(ntopbuf)), ipr->ipr_origin);
603 1.6 itojun close(s);
604 1.1 itojun return 1;
605 1.1 itojun }
606 1.1 itojun
607 1.1 itojun close(s);
608 1.1 itojun return 0;
609 1.15 itojun #else
610 1.15 itojun ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
611 1.15 itojun ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
612 1.15 itojun ipr->ipr_raf_onlink = 1;
613 1.15 itojun ipr->ipr_raf_auto = 1;
614 1.15 itojun return 0;
615 1.15 itojun #endif
616 1.1 itojun }
617 1.1 itojun
618 1.1 itojun void
619 1.1 itojun make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
620 1.1 itojun {
621 1.1 itojun struct in6_prefixreq ipr;
622 1.1 itojun
623 1.1 itojun memset(&ipr, 0, sizeof(ipr));
624 1.1 itojun if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
625 1.1 itojun syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
626 1.18 itojun "exist. This should not happen! %s", __func__,
627 1.1 itojun ifindex, strerror(errno));
628 1.1 itojun exit(1);
629 1.1 itojun }
630 1.1 itojun ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
631 1.1 itojun ipr.ipr_prefix.sin6_family = AF_INET6;
632 1.1 itojun ipr.ipr_prefix.sin6_addr = *addr;
633 1.1 itojun ipr.ipr_plen = plen;
634 1.1 itojun
635 1.1 itojun if (init_prefix(&ipr))
636 1.1 itojun return; /* init failed by some error */
637 1.1 itojun add_prefix(rai, &ipr);
638 1.1 itojun }
639 1.1 itojun
640 1.4 itojun void
641 1.1 itojun make_packet(struct rainfo *rainfo)
642 1.1 itojun {
643 1.1 itojun size_t packlen, lladdroptlen = 0;
644 1.1 itojun char *buf;
645 1.1 itojun struct nd_router_advert *ra;
646 1.1 itojun struct nd_opt_prefix_info *ndopt_pi;
647 1.1 itojun struct nd_opt_mtu *ndopt_mtu;
648 1.1 itojun struct prefix *pfx;
649 1.1 itojun
650 1.1 itojun /* calculate total length */
651 1.1 itojun packlen = sizeof(struct nd_router_advert);
652 1.1 itojun if (rainfo->advlinkopt) {
653 1.1 itojun if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
654 1.1 itojun syslog(LOG_INFO,
655 1.1 itojun "<%s> link-layer address option has"
656 1.14 itojun " null length on %s. Treat as not included.",
657 1.18 itojun __func__, rainfo->ifname);
658 1.1 itojun rainfo->advlinkopt = 0;
659 1.1 itojun }
660 1.1 itojun packlen += lladdroptlen;
661 1.1 itojun }
662 1.1 itojun if (rainfo->pfxs)
663 1.1 itojun packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
664 1.1 itojun if (rainfo->linkmtu)
665 1.1 itojun packlen += sizeof(struct nd_opt_mtu);
666 1.1 itojun
667 1.1 itojun /* allocate memory for the packet */
668 1.1 itojun if ((buf = malloc(packlen)) == NULL) {
669 1.1 itojun syslog(LOG_ERR,
670 1.1 itojun "<%s> can't get enough memory for an RA packet",
671 1.18 itojun __func__);
672 1.1 itojun exit(1);
673 1.1 itojun }
674 1.14 itojun if (rainfo->ra_data) {
675 1.14 itojun /* free the previous packet */
676 1.14 itojun free(rainfo->ra_data);
677 1.14 itojun rainfo->ra_data = NULL;
678 1.14 itojun }
679 1.1 itojun rainfo->ra_data = buf;
680 1.1 itojun /* XXX: what if packlen > 576? */
681 1.1 itojun rainfo->ra_datalen = packlen;
682 1.1 itojun
683 1.1 itojun /*
684 1.1 itojun * construct the packet
685 1.1 itojun */
686 1.1 itojun ra = (struct nd_router_advert *)buf;
687 1.1 itojun ra->nd_ra_type = ND_ROUTER_ADVERT;
688 1.1 itojun ra->nd_ra_code = 0;
689 1.1 itojun ra->nd_ra_cksum = 0;
690 1.1 itojun ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
691 1.14 itojun ra->nd_ra_flags_reserved = 0; /* just in case */
692 1.14 itojun /*
693 1.14 itojun * XXX: the router preference field, which is a 2-bit field, should be
694 1.14 itojun * initialized before other fields.
695 1.14 itojun */
696 1.14 itojun ra->nd_ra_flags_reserved = 0xff & rainfo->rtpref;
697 1.1 itojun ra->nd_ra_flags_reserved |=
698 1.1 itojun rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
699 1.1 itojun ra->nd_ra_flags_reserved |=
700 1.1 itojun rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
701 1.1 itojun ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
702 1.1 itojun ra->nd_ra_reachable = htonl(rainfo->reachabletime);
703 1.1 itojun ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
704 1.1 itojun buf += sizeof(*ra);
705 1.1 itojun
706 1.1 itojun if (rainfo->advlinkopt) {
707 1.1 itojun lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
708 1.1 itojun buf += lladdroptlen;
709 1.1 itojun }
710 1.1 itojun
711 1.1 itojun if (rainfo->linkmtu) {
712 1.1 itojun ndopt_mtu = (struct nd_opt_mtu *)buf;
713 1.1 itojun ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
714 1.1 itojun ndopt_mtu->nd_opt_mtu_len = 1;
715 1.1 itojun ndopt_mtu->nd_opt_mtu_reserved = 0;
716 1.12 itojun ndopt_mtu->nd_opt_mtu_mtu = htonl(rainfo->linkmtu);
717 1.1 itojun buf += sizeof(struct nd_opt_mtu);
718 1.1 itojun }
719 1.1 itojun
720 1.6 itojun
721 1.6 itojun
722 1.1 itojun for (pfx = rainfo->prefix.next;
723 1.1 itojun pfx != &rainfo->prefix; pfx = pfx->next) {
724 1.8 itojun u_int32_t vltime, pltime;
725 1.8 itojun struct timeval now;
726 1.8 itojun
727 1.1 itojun ndopt_pi = (struct nd_opt_prefix_info *)buf;
728 1.1 itojun ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
729 1.1 itojun ndopt_pi->nd_opt_pi_len = 4;
730 1.1 itojun ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
731 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved = 0;
732 1.1 itojun if (pfx->onlinkflg)
733 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved |=
734 1.1 itojun ND_OPT_PI_FLAG_ONLINK;
735 1.1 itojun if (pfx->autoconfflg)
736 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved |=
737 1.1 itojun ND_OPT_PI_FLAG_AUTO;
738 1.8 itojun if (pfx->vltimeexpire || pfx->pltimeexpire)
739 1.8 itojun gettimeofday(&now, NULL);
740 1.8 itojun if (pfx->vltimeexpire == 0)
741 1.8 itojun vltime = pfx->validlifetime;
742 1.8 itojun else
743 1.8 itojun vltime = (pfx->vltimeexpire > now.tv_sec) ?
744 1.8 itojun pfx->vltimeexpire - now.tv_sec : 0;
745 1.8 itojun if (pfx->pltimeexpire == 0)
746 1.8 itojun pltime = pfx->preflifetime;
747 1.8 itojun else
748 1.8 itojun pltime = (pfx->pltimeexpire > now.tv_sec) ?
749 1.8 itojun pfx->pltimeexpire - now.tv_sec : 0;
750 1.8 itojun if (vltime < pltime) {
751 1.8 itojun /*
752 1.8 itojun * this can happen if vltime is decrement but pltime
753 1.8 itojun * is not.
754 1.8 itojun */
755 1.8 itojun pltime = vltime;
756 1.8 itojun }
757 1.12 itojun ndopt_pi->nd_opt_pi_valid_time = htonl(vltime);
758 1.12 itojun ndopt_pi->nd_opt_pi_preferred_time = htonl(pltime);
759 1.1 itojun ndopt_pi->nd_opt_pi_reserved2 = 0;
760 1.1 itojun ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
761 1.1 itojun
762 1.1 itojun buf += sizeof(struct nd_opt_prefix_info);
763 1.1 itojun }
764 1.1 itojun
765 1.1 itojun return;
766 1.8 itojun }
767 1.8 itojun
768 1.8 itojun static int
769 1.8 itojun getinet6sysctl(int code)
770 1.8 itojun {
771 1.8 itojun int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, 0 };
772 1.8 itojun int value;
773 1.8 itojun size_t size;
774 1.8 itojun
775 1.8 itojun mib[3] = code;
776 1.8 itojun size = sizeof(value);
777 1.8 itojun if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), &value, &size, NULL, 0)
778 1.8 itojun < 0) {
779 1.8 itojun syslog(LOG_ERR, "<%s>: failed to get ip6 sysctl(%d): %s",
780 1.18 itojun __func__, code,
781 1.8 itojun strerror(errno));
782 1.8 itojun return(-1);
783 1.8 itojun }
784 1.8 itojun else
785 1.8 itojun return(value);
786 1.1 itojun }
787