config.c revision 1.1 1 1.1 itojun /*
2 1.1 itojun * Copyright (C) 1998 WIDE Project.
3 1.1 itojun * All rights reserved.
4 1.1 itojun *
5 1.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1 itojun * modification, are permitted provided that the following conditions
7 1.1 itojun * are met:
8 1.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1 itojun * may be used to endorse or promote products derived from this software
15 1.1 itojun * without specific prior written permission.
16 1.1 itojun *
17 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 itojun * SUCH DAMAGE.
28 1.1 itojun */
29 1.1 itojun
30 1.1 itojun #include <sys/param.h>
31 1.1 itojun #include <sys/ioctl.h>
32 1.1 itojun #include <sys/socket.h>
33 1.1 itojun #include <sys/time.h>
34 1.1 itojun
35 1.1 itojun #include <net/if.h>
36 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
37 1.1 itojun #include <net/if_var.h>
38 1.1 itojun #endif /* __FreeBSD__ >= 3 */
39 1.1 itojun #include <net/route.h>
40 1.1 itojun #include <net/if_dl.h>
41 1.1 itojun
42 1.1 itojun #include <netinet/in.h>
43 1.1 itojun #include <netinet/in_var.h>
44 1.1 itojun #include <netinet6/ip6.h>
45 1.1 itojun #include <netinet6/ip6_var.h>
46 1.1 itojun #include <netinet6/icmp6.h>
47 1.1 itojun
48 1.1 itojun #include <arpa/inet.h>
49 1.1 itojun
50 1.1 itojun #include <stdio.h>
51 1.1 itojun #include <syslog.h>
52 1.1 itojun #include <errno.h>
53 1.1 itojun #include <string.h>
54 1.1 itojun #include <stdlib.h>
55 1.1 itojun #ifdef __NetBSD__
56 1.1 itojun #include <search.h>
57 1.1 itojun #endif
58 1.1 itojun #include <unistd.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.1 itojun static void makeentry __P((char *, int, char *, int));
67 1.1 itojun static void make_packet __P((struct rainfo *));
68 1.1 itojun static void get_prefix __P((struct rainfo *));
69 1.1 itojun
70 1.1 itojun extern struct rainfo *ralist;
71 1.1 itojun
72 1.1 itojun void
73 1.1 itojun getconfig(intface)
74 1.1 itojun char *intface;
75 1.1 itojun {
76 1.1 itojun int stat, pfxs, i;
77 1.1 itojun char tbuf[BUFSIZ];
78 1.1 itojun struct rainfo *tmp;
79 1.1 itojun char val8;
80 1.1 itojun short val16;
81 1.1 itojun int val32;
82 1.1 itojun char buf[BUFSIZ];
83 1.1 itojun char *bp = buf;
84 1.1 itojun char *addr;
85 1.1 itojun
86 1.1 itojun #define MUSTHAVE(var, cap) \
87 1.1 itojun { \
88 1.1 itojun int t; \
89 1.1 itojun if ((t = agetnum(cap)) < 0) { \
90 1.1 itojun fprintf(stderr, "rtadvd: need %s for interface %s\n", \
91 1.1 itojun cap, intface); \
92 1.1 itojun exit(1); \
93 1.1 itojun } \
94 1.1 itojun var = t; \
95 1.1 itojun }
96 1.1 itojun #define MAYHAVE(var, cap, def) \
97 1.1 itojun { \
98 1.1 itojun if ((var = agetnum(cap)) < 0) \
99 1.1 itojun var = def; \
100 1.1 itojun }
101 1.1 itojun
102 1.1 itojun if ((stat = agetent(tbuf, intface)) <= 0) {
103 1.1 itojun memset(tbuf, 0, sizeof(tbuf));
104 1.1 itojun syslog(LOG_INFO,
105 1.1 itojun "<%s> %s isn't defined in the configuration file"
106 1.1 itojun " or the configuration file doesn't exist."
107 1.1 itojun " Treat it as default",
108 1.1 itojun __FUNCTION__, intface);
109 1.1 itojun }
110 1.1 itojun
111 1.1 itojun tmp = (struct rainfo *)malloc(sizeof(*ralist));
112 1.1 itojun memset(tmp, 0, sizeof(*tmp));
113 1.1 itojun tmp->prefix.next = tmp->prefix.prev = &tmp->prefix;
114 1.1 itojun
115 1.1 itojun /* get interface information */
116 1.1 itojun if (agetflag("nolladdr"))
117 1.1 itojun tmp->advlinkopt = 0;
118 1.1 itojun else
119 1.1 itojun tmp->advlinkopt = 1;
120 1.1 itojun if (tmp->advlinkopt) {
121 1.1 itojun if ((tmp->sdl = if_nametosdl(intface)) == NULL) {
122 1.1 itojun syslog(LOG_ERR,
123 1.1 itojun "<%s> can't get information of %s",
124 1.1 itojun __FUNCTION__, intface);
125 1.1 itojun exit(1);
126 1.1 itojun }
127 1.1 itojun tmp->ifindex = tmp->sdl->sdl_index;
128 1.1 itojun } else
129 1.1 itojun tmp->ifindex = if_nametoindex(intface);
130 1.1 itojun strncpy(tmp->ifname, intface, sizeof(tmp->ifname));
131 1.1 itojun if ((tmp->phymtu = if_getmtu(intface)) == 0) {
132 1.1 itojun tmp->phymtu = IPV6_MMTU;
133 1.1 itojun syslog(LOG_WARNING,
134 1.1 itojun "<%s> can't get interface mtu of %s. Treat as %d",
135 1.1 itojun __FUNCTION__, intface, IPV6_MMTU);
136 1.1 itojun }
137 1.1 itojun
138 1.1 itojun /*
139 1.1 itojun * set router configuration variables.
140 1.1 itojun */
141 1.1 itojun MAYHAVE(val32, "maxinterval", DEF_MAXRTRADVINTERVAL);
142 1.1 itojun tmp->maxinterval = val32;
143 1.1 itojun if (tmp->maxinterval < MIN_MAXINTERVAL ||
144 1.1 itojun tmp->maxinterval > MAX_MAXINTERVAL) {
145 1.1 itojun syslog(LOG_ERR,
146 1.1 itojun "<%s> maxinterval must be between %d and %d",
147 1.1 itojun __FUNCTION__, MIN_MAXINTERVAL, MAX_MAXINTERVAL);
148 1.1 itojun exit(1);
149 1.1 itojun }
150 1.1 itojun MAYHAVE(val32, "mininterval", tmp->maxinterval/3);
151 1.1 itojun tmp->mininterval = val32;
152 1.1 itojun if (tmp->mininterval < MIN_MININTERVAL ||
153 1.1 itojun tmp->mininterval > (tmp->maxinterval * 3) / 4) {
154 1.1 itojun syslog(LOG_ERR,
155 1.1 itojun "<%s> mininterval must be between %d and %d",
156 1.1 itojun __FUNCTION__,
157 1.1 itojun MIN_MININTERVAL,
158 1.1 itojun (tmp->maxinterval * 3) / 4);
159 1.1 itojun exit(1);
160 1.1 itojun }
161 1.1 itojun
162 1.1 itojun MAYHAVE(val16, "chlim", DEF_ADVCURHOPLIMIT);
163 1.1 itojun tmp->hoplimit = 0xff & val16;
164 1.1 itojun
165 1.1 itojun MAYHAVE(val8, "raflags", 0);
166 1.1 itojun tmp->managedflg= val8 & ND_RA_FLAG_MANAGED;
167 1.1 itojun tmp->otherflg = val8 & ND_RA_FLAG_OTHER;
168 1.1 itojun
169 1.1 itojun MAYHAVE(val32, "rltime", tmp->maxinterval * 3);
170 1.1 itojun tmp->lifetime = (u_short)val32;
171 1.1 itojun if (tmp->lifetime &&
172 1.1 itojun (tmp->lifetime < tmp->maxinterval ||
173 1.1 itojun tmp->lifetime > MAXROUTERLIFETIME)) {
174 1.1 itojun syslog(LOG_ERR,
175 1.1 itojun "<%s> router lifetime on %s must be 0 or"
176 1.1 itojun " between %d and %d",
177 1.1 itojun __FUNCTION__, intface,
178 1.1 itojun tmp->maxinterval, MAXROUTERLIFETIME);
179 1.1 itojun exit(1);
180 1.1 itojun }
181 1.1 itojun
182 1.1 itojun MAYHAVE(val32, "rtime", DEF_ADVREACHABLETIME);
183 1.1 itojun tmp->reachabletime = (u_int32_t)val32;
184 1.1 itojun if (tmp->reachabletime > MAXREACHABLETIME) {
185 1.1 itojun syslog(LOG_ERR,
186 1.1 itojun "<%s> reachable time must be no greater than %d",
187 1.1 itojun __FUNCTION__, MAXREACHABLETIME);
188 1.1 itojun exit(1);
189 1.1 itojun }
190 1.1 itojun
191 1.1 itojun MAYHAVE(val32, "retrans", DEF_ADVRETRANSTIMER);
192 1.1 itojun tmp->retranstimer = val32;
193 1.1 itojun
194 1.1 itojun /* prefix information */
195 1.1 itojun if ((pfxs = agetnum("addrs")) < 0) {
196 1.1 itojun /* auto configure prefix information */
197 1.1 itojun if (agetstr("addr", &bp) || agetstr("addr1", &bp)) {
198 1.1 itojun syslog(LOG_ERR,
199 1.1 itojun "<%s> conflicting prefix configuration for %s: "
200 1.1 itojun "automatic and manual config at the same time",
201 1.1 itojun __FUNCTION__, intface);
202 1.1 itojun exit(1);
203 1.1 itojun }
204 1.1 itojun get_prefix(tmp);
205 1.1 itojun }
206 1.1 itojun else {
207 1.1 itojun tmp->pfxs = pfxs;
208 1.1 itojun for (i = 0; i < pfxs; i++) {
209 1.1 itojun struct prefix *pfx;
210 1.1 itojun char entbuf[256];
211 1.1 itojun short val16;
212 1.1 itojun int val32;
213 1.1 itojun int added = (pfxs > 1) ? 1 : 0;
214 1.1 itojun
215 1.1 itojun /* allocate memory to store prefix information */
216 1.1 itojun if ((pfx = malloc(sizeof(struct prefix))) == NULL) {
217 1.1 itojun syslog(LOG_ERR,
218 1.1 itojun "<%s> can't allocate enough memory",
219 1.1 itojun __FUNCTION__);
220 1.1 itojun exit(1);
221 1.1 itojun }
222 1.1 itojun /* link into chain */
223 1.1 itojun insque(pfx, &tmp->prefix);
224 1.1 itojun
225 1.1 itojun makeentry(entbuf, i, "prefixlen", added);
226 1.1 itojun MAYHAVE(val32, entbuf, 64);
227 1.1 itojun pfx->prefixlen = (int)val32;
228 1.1 itojun
229 1.1 itojun makeentry(entbuf, i, "pinfoflags", added);
230 1.1 itojun MAYHAVE(val16, entbuf,
231 1.1 itojun (ND_OPT_PI_FLAG_ONLINK|ND_OPT_PI_FLAG_AUTO));
232 1.1 itojun val16 &= 0xc0;
233 1.1 itojun pfx->onlinkflg = val16 & ND_OPT_PI_FLAG_ONLINK;
234 1.1 itojun pfx->autoconfflg = val16 & ND_OPT_PI_FLAG_AUTO;
235 1.1 itojun
236 1.1 itojun makeentry(entbuf, i, "vltime", added);
237 1.1 itojun MAYHAVE(val32, entbuf, DEF_ADVVALIDLIFETIME);
238 1.1 itojun pfx->validlifetime = (u_int32_t)val32;
239 1.1 itojun
240 1.1 itojun makeentry(entbuf, i, "pltime", added);
241 1.1 itojun MAYHAVE(val32, entbuf, DEF_ADVPREFERREDLIFETIME);
242 1.1 itojun pfx->preflifetime = (u_int32_t)val32;
243 1.1 itojun
244 1.1 itojun makeentry(entbuf, i, "addr", added);
245 1.1 itojun addr = (char *)agetstr(entbuf, &bp);
246 1.1 itojun if (addr == NULL) {
247 1.1 itojun syslog(LOG_ERR,
248 1.1 itojun "<%s> need %s as an prefix for "
249 1.1 itojun "interface %s",
250 1.1 itojun __FUNCTION__, entbuf, intface);
251 1.1 itojun exit(1);
252 1.1 itojun }
253 1.1 itojun if (inet_pton(AF_INET6, addr,
254 1.1 itojun &pfx->prefix) != 1) {
255 1.1 itojun syslog(LOG_ERR,
256 1.1 itojun "<%s> inet_pton failed for %s",
257 1.1 itojun __FUNCTION__, addr);
258 1.1 itojun exit(1);
259 1.1 itojun }
260 1.1 itojun if (IN6_IS_ADDR_MULTICAST(&pfx->prefix)) {
261 1.1 itojun syslog(LOG_ERR,
262 1.1 itojun "<%s> multicast prefix(%s) must "
263 1.1 itojun "not be advertised (IF=%s)",
264 1.1 itojun __FUNCTION__, addr, intface);
265 1.1 itojun exit(1);
266 1.1 itojun }
267 1.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&pfx->prefix))
268 1.1 itojun syslog(LOG_NOTICE,
269 1.1 itojun "<%s> link-local prefix(%s) will be"
270 1.1 itojun " advertised on %s",
271 1.1 itojun __FUNCTION__, addr, intface);
272 1.1 itojun }
273 1.1 itojun }
274 1.1 itojun
275 1.1 itojun MAYHAVE(val32, "mtu", 0);
276 1.1 itojun tmp->linkmtu = (u_int32_t)val32;
277 1.1 itojun if (tmp->linkmtu == 0) {
278 1.1 itojun char *mtustr;
279 1.1 itojun
280 1.1 itojun if ((mtustr = (char *)agetstr("mtu", &bp)) &&
281 1.1 itojun strcmp(mtustr, "auto") == 0)
282 1.1 itojun tmp->linkmtu = tmp->phymtu;
283 1.1 itojun }
284 1.1 itojun else if (tmp->linkmtu < IPV6_MMTU || tmp->linkmtu > tmp->phymtu) {
285 1.1 itojun syslog(LOG_ERR,
286 1.1 itojun "<%s> advertised link mtu must be between"
287 1.1 itojun " least MTU and physical link MTU",
288 1.1 itojun __FUNCTION__);
289 1.1 itojun exit(1);
290 1.1 itojun }
291 1.1 itojun
292 1.1 itojun /* okey */
293 1.1 itojun tmp->next = ralist;
294 1.1 itojun ralist = tmp;
295 1.1 itojun
296 1.1 itojun /* construct the sending packet */
297 1.1 itojun make_packet(tmp);
298 1.1 itojun
299 1.1 itojun /* set timer */
300 1.1 itojun tmp->timer = rtadvd_add_timer(ra_timeout, ra_timer_update,
301 1.1 itojun tmp, tmp);
302 1.1 itojun ra_timer_update((void *)tmp, &tmp->timer->tm);
303 1.1 itojun rtadvd_set_timer(&tmp->timer->tm, tmp->timer);
304 1.1 itojun }
305 1.1 itojun
306 1.1 itojun static void
307 1.1 itojun get_prefix(struct rainfo *rai)
308 1.1 itojun {
309 1.1 itojun int len;
310 1.1 itojun u_char *buf, *lim, *next;
311 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN];
312 1.1 itojun
313 1.1 itojun if ((len = rtbuf_len()) < 0) {
314 1.1 itojun syslog(LOG_ERR,
315 1.1 itojun "<%s> can't get buffer length for routing info",
316 1.1 itojun __FUNCTION__);
317 1.1 itojun exit(1);
318 1.1 itojun }
319 1.1 itojun if ((buf = malloc(len)) == NULL) {
320 1.1 itojun syslog(LOG_ERR,
321 1.1 itojun "<%s> can't allocate buffer", __FUNCTION__);
322 1.1 itojun exit(1);
323 1.1 itojun }
324 1.1 itojun if (get_rtinfo(buf, &len) < 0) {
325 1.1 itojun syslog(LOG_ERR,
326 1.1 itojun "<%s> can't get routing inforamtion", __FUNCTION__);
327 1.1 itojun exit(1);
328 1.1 itojun }
329 1.1 itojun
330 1.1 itojun lim = buf + len;
331 1.1 itojun next = get_next_msg(buf, lim, rai->ifindex, &len,
332 1.1 itojun RTADV_TYPE2BITMASK(RTM_GET));
333 1.1 itojun while (next < lim) {
334 1.1 itojun struct prefix *pp;
335 1.1 itojun struct in6_addr *a;
336 1.1 itojun
337 1.1 itojun /* allocate memory to store prefix info. */
338 1.1 itojun if ((pp = malloc(sizeof(*pp))) == NULL) {
339 1.1 itojun syslog(LOG_ERR,
340 1.1 itojun "<%s> can't get allocate buffer for prefix",
341 1.1 itojun __FUNCTION__);
342 1.1 itojun exit(1);
343 1.1 itojun }
344 1.1 itojun memset(pp, 0, sizeof(*pp));
345 1.1 itojun
346 1.1 itojun /* set prefix and its length */
347 1.1 itojun a = get_addr(next);
348 1.1 itojun memcpy(&pp->prefix, a, sizeof(*a));
349 1.1 itojun if ((pp->prefixlen = get_prefixlen(next)) < 0) {
350 1.1 itojun syslog(LOG_ERR,
351 1.1 itojun "<%s> failed to get prefixlen "
352 1.1 itojun "or prefixl is invalid",
353 1.1 itojun __FUNCTION__);
354 1.1 itojun exit(1);
355 1.1 itojun }
356 1.1 itojun syslog(LOG_DEBUG,
357 1.1 itojun "<%s> add %s/%d to prefix list on %s",
358 1.1 itojun __FUNCTION__,
359 1.1 itojun inet_ntop(AF_INET6, a, ntopbuf, INET6_ADDRSTRLEN),
360 1.1 itojun pp->prefixlen, rai->ifname);
361 1.1 itojun
362 1.1 itojun /* set other fields with protocol defaults */
363 1.1 itojun pp->validlifetime = DEF_ADVVALIDLIFETIME;
364 1.1 itojun pp->preflifetime = DEF_ADVPREFERREDLIFETIME;
365 1.1 itojun pp->onlinkflg = 1;
366 1.1 itojun pp->autoconfflg = 1;
367 1.1 itojun
368 1.1 itojun /* link into chain */
369 1.1 itojun insque(pp, &rai->prefix);
370 1.1 itojun
371 1.1 itojun /* counter increment */
372 1.1 itojun rai->pfxs++;
373 1.1 itojun
374 1.1 itojun /* forward pointer and get next prefix(if any) */
375 1.1 itojun next += len;
376 1.1 itojun next = get_next_msg(next, lim, rai->ifindex,
377 1.1 itojun &len, RTADV_TYPE2BITMASK(RTM_GET));
378 1.1 itojun }
379 1.1 itojun
380 1.1 itojun free(buf);
381 1.1 itojun }
382 1.1 itojun
383 1.1 itojun static void
384 1.1 itojun makeentry(buf, id, string, add)
385 1.1 itojun char *buf, *string;
386 1.1 itojun int id, add;
387 1.1 itojun {
388 1.1 itojun strcpy(buf, string);
389 1.1 itojun if (add) {
390 1.1 itojun char *cp;
391 1.1 itojun
392 1.1 itojun cp = (char *)index(buf, '\0');
393 1.1 itojun cp += sprintf(cp, "%d", id);
394 1.1 itojun *cp = '\0';
395 1.1 itojun }
396 1.1 itojun }
397 1.1 itojun
398 1.1 itojun /*
399 1.1 itojun * Add a prefix to the list of specified interface and reconstruct
400 1.1 itojun * the outgoing packet.
401 1.1 itojun * The prefix must not be in the list.
402 1.1 itojun * XXX: other parameter of the prefix(e.g. lifetime) shoule be
403 1.1 itojun * able to be specified.
404 1.1 itojun */
405 1.1 itojun static void
406 1.1 itojun add_prefix(struct rainfo *rai, struct in6_prefixreq *ipr)
407 1.1 itojun {
408 1.1 itojun struct prefix *prefix;
409 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN];
410 1.1 itojun
411 1.1 itojun if ((prefix = malloc(sizeof(*prefix))) == NULL) {
412 1.1 itojun syslog(LOG_ERR, "<%s> memory allocation failed",
413 1.1 itojun __FUNCTION__);
414 1.1 itojun return; /* XXX: error or exit? */
415 1.1 itojun }
416 1.1 itojun prefix->prefix = ipr->ipr_prefix.sin6_addr;
417 1.1 itojun prefix->prefixlen = ipr->ipr_plen;
418 1.1 itojun prefix->validlifetime = ipr->ipr_vltime;
419 1.1 itojun prefix->preflifetime = ipr->ipr_pltime;
420 1.1 itojun prefix->onlinkflg = ipr->ipr_raf_onlink;
421 1.1 itojun prefix->autoconfflg = ipr->ipr_raf_auto;
422 1.1 itojun
423 1.1 itojun insque(prefix, &rai->prefix);
424 1.1 itojun
425 1.1 itojun syslog(LOG_DEBUG, "<%s> new prefix %s/%d was added on %s",
426 1.1 itojun __FUNCTION__, inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr,
427 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
428 1.1 itojun ipr->ipr_plen, rai->ifname);
429 1.1 itojun
430 1.1 itojun /* free the previous packet */
431 1.1 itojun free(rai->ra_data);
432 1.1 itojun rai->ra_data = 0;
433 1.1 itojun
434 1.1 itojun /* reconstruct the packet */
435 1.1 itojun rai->pfxs++;
436 1.1 itojun make_packet(rai);
437 1.1 itojun
438 1.1 itojun /*
439 1.1 itojun * reset the timer so that the new prefix will be advertised quickly.
440 1.1 itojun */
441 1.1 itojun rai->initcounter = 0;
442 1.1 itojun ra_timer_update((void *)rai, &rai->timer->tm);
443 1.1 itojun rtadvd_set_timer(&rai->timer->tm, rai->timer);
444 1.1 itojun }
445 1.1 itojun
446 1.1 itojun /*
447 1.1 itojun * Delete a prefix to the list of specified interface and reconstruct
448 1.1 itojun * the outgoing packet.
449 1.1 itojun * The prefix must be in the list
450 1.1 itojun */
451 1.1 itojun void
452 1.1 itojun delete_prefix(struct rainfo *rai, struct prefix *prefix)
453 1.1 itojun {
454 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN];
455 1.1 itojun
456 1.1 itojun remque(prefix);
457 1.1 itojun syslog(LOG_DEBUG, "<%s> prefix %s/%d was deleted on %s",
458 1.1 itojun __FUNCTION__, inet_ntop(AF_INET6, &prefix->prefix,
459 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
460 1.1 itojun prefix->prefixlen, rai->ifname);
461 1.1 itojun free(prefix);
462 1.1 itojun rai->pfxs--;
463 1.1 itojun make_packet(rai);
464 1.1 itojun }
465 1.1 itojun
466 1.1 itojun /*
467 1.1 itojun * Try to get an in6_prefixreq contents for a prefix which matches
468 1.1 itojun * ipr->ipr_prefix and ipr->ipr_plen and belongs to
469 1.1 itojun * the interface whose name is ipr->ipr_name[].
470 1.1 itojun */
471 1.1 itojun static int
472 1.1 itojun init_prefix(struct in6_prefixreq *ipr)
473 1.1 itojun {
474 1.1 itojun int s;
475 1.1 itojun
476 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
477 1.1 itojun syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
478 1.1 itojun strerror(errno));
479 1.1 itojun exit(1);
480 1.1 itojun }
481 1.1 itojun
482 1.1 itojun if (ioctl(s, SIOCGIFPREFIX_IN6, (caddr_t)ipr) < 0) {
483 1.1 itojun syslog(LOG_INFO, "<%s> ioctl:SIOCGIFPREFIX %s", __FUNCTION__,
484 1.1 itojun strerror(errno));
485 1.1 itojun
486 1.1 itojun ipr->ipr_vltime = DEF_ADVVALIDLIFETIME;
487 1.1 itojun ipr->ipr_pltime = DEF_ADVPREFERREDLIFETIME;
488 1.1 itojun ipr->ipr_raf_onlink = 1;
489 1.1 itojun ipr->ipr_raf_auto = 1;
490 1.1 itojun /* omit other field initialization */
491 1.1 itojun }
492 1.1 itojun else if (ipr->ipr_origin < PR_ORIG_RR) {
493 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN];
494 1.1 itojun
495 1.1 itojun syslog(LOG_WARNING, "<%s> Added prefix(%s)'s origin %d is"
496 1.1 itojun "lower than PR_ORIG_RR(router renumbering)."
497 1.1 itojun "This should not happen if I am router", __FUNCTION__,
498 1.1 itojun inet_ntop(AF_INET6, &ipr->ipr_prefix.sin6_addr, ntopbuf,
499 1.1 itojun sizeof(ntopbuf)), ipr->ipr_origin);
500 1.1 itojun return 1;
501 1.1 itojun }
502 1.1 itojun
503 1.1 itojun close(s);
504 1.1 itojun return 0;
505 1.1 itojun }
506 1.1 itojun
507 1.1 itojun void
508 1.1 itojun make_prefix(struct rainfo *rai, int ifindex, struct in6_addr *addr, int plen)
509 1.1 itojun {
510 1.1 itojun struct in6_prefixreq ipr;
511 1.1 itojun
512 1.1 itojun memset(&ipr, 0, sizeof(ipr));
513 1.1 itojun if (if_indextoname(ifindex, ipr.ipr_name) == NULL) {
514 1.1 itojun syslog(LOG_ERR, "<%s> Prefix added interface No.%d doesn't"
515 1.1 itojun "exist. This should not happen! %s", __FUNCTION__,
516 1.1 itojun ifindex, strerror(errno));
517 1.1 itojun exit(1);
518 1.1 itojun }
519 1.1 itojun ipr.ipr_prefix.sin6_len = sizeof(ipr.ipr_prefix);
520 1.1 itojun ipr.ipr_prefix.sin6_family = AF_INET6;
521 1.1 itojun ipr.ipr_prefix.sin6_addr = *addr;
522 1.1 itojun ipr.ipr_plen = plen;
523 1.1 itojun
524 1.1 itojun if (init_prefix(&ipr))
525 1.1 itojun return; /* init failed by some error */
526 1.1 itojun add_prefix(rai, &ipr);
527 1.1 itojun }
528 1.1 itojun
529 1.1 itojun static void
530 1.1 itojun make_packet(struct rainfo *rainfo)
531 1.1 itojun {
532 1.1 itojun size_t packlen, lladdroptlen = 0;
533 1.1 itojun char *buf;
534 1.1 itojun struct nd_router_advert *ra;
535 1.1 itojun struct nd_opt_prefix_info *ndopt_pi;
536 1.1 itojun struct nd_opt_mtu *ndopt_mtu;
537 1.1 itojun struct prefix *pfx;
538 1.1 itojun
539 1.1 itojun /* calculate total length */
540 1.1 itojun packlen = sizeof(struct nd_router_advert);
541 1.1 itojun if (rainfo->advlinkopt) {
542 1.1 itojun if ((lladdroptlen = lladdropt_length(rainfo->sdl)) == 0) {
543 1.1 itojun syslog(LOG_INFO,
544 1.1 itojun "<%s> link-layer address option has"
545 1.1 itojun " null length on %s."
546 1.1 itojun " Treat as not included.",
547 1.1 itojun __FUNCTION__, rainfo->ifname);
548 1.1 itojun rainfo->advlinkopt = 0;
549 1.1 itojun }
550 1.1 itojun packlen += lladdroptlen;
551 1.1 itojun }
552 1.1 itojun if (rainfo->pfxs)
553 1.1 itojun packlen += sizeof(struct nd_opt_prefix_info) * rainfo->pfxs;
554 1.1 itojun if (rainfo->linkmtu)
555 1.1 itojun packlen += sizeof(struct nd_opt_mtu);
556 1.1 itojun
557 1.1 itojun /* allocate memory for the packet */
558 1.1 itojun if ((buf = malloc(packlen)) == NULL) {
559 1.1 itojun syslog(LOG_ERR,
560 1.1 itojun "<%s> can't get enough memory for an RA packet",
561 1.1 itojun __FUNCTION__);
562 1.1 itojun exit(1);
563 1.1 itojun }
564 1.1 itojun rainfo->ra_data = buf;
565 1.1 itojun /* XXX: what if packlen > 576? */
566 1.1 itojun rainfo->ra_datalen = packlen;
567 1.1 itojun
568 1.1 itojun /*
569 1.1 itojun * construct the packet
570 1.1 itojun */
571 1.1 itojun ra = (struct nd_router_advert *)buf;
572 1.1 itojun ra->nd_ra_type = ND_ROUTER_ADVERT;
573 1.1 itojun ra->nd_ra_code = 0;
574 1.1 itojun ra->nd_ra_cksum = 0;
575 1.1 itojun ra->nd_ra_curhoplimit = (u_int8_t)(0xff & rainfo->hoplimit);
576 1.1 itojun ra->nd_ra_flags_reserved = 0;
577 1.1 itojun ra->nd_ra_flags_reserved |=
578 1.1 itojun rainfo->managedflg ? ND_RA_FLAG_MANAGED : 0;
579 1.1 itojun ra->nd_ra_flags_reserved |=
580 1.1 itojun rainfo->otherflg ? ND_RA_FLAG_OTHER : 0;
581 1.1 itojun ra->nd_ra_router_lifetime = htons(rainfo->lifetime);
582 1.1 itojun ra->nd_ra_reachable = htonl(rainfo->reachabletime);
583 1.1 itojun ra->nd_ra_retransmit = htonl(rainfo->retranstimer);
584 1.1 itojun buf += sizeof(*ra);
585 1.1 itojun
586 1.1 itojun if (rainfo->advlinkopt) {
587 1.1 itojun lladdropt_fill(rainfo->sdl, (struct nd_opt_hdr *)buf);
588 1.1 itojun buf += lladdroptlen;
589 1.1 itojun }
590 1.1 itojun
591 1.1 itojun if (rainfo->linkmtu) {
592 1.1 itojun ndopt_mtu = (struct nd_opt_mtu *)buf;
593 1.1 itojun ndopt_mtu->nd_opt_mtu_type = ND_OPT_MTU;
594 1.1 itojun ndopt_mtu->nd_opt_mtu_len = 1;
595 1.1 itojun ndopt_mtu->nd_opt_mtu_reserved = 0;
596 1.1 itojun ndopt_mtu->nd_opt_mtu_mtu = ntohl(rainfo->linkmtu);
597 1.1 itojun buf += sizeof(struct nd_opt_mtu);
598 1.1 itojun }
599 1.1 itojun
600 1.1 itojun for (pfx = rainfo->prefix.next;
601 1.1 itojun pfx != &rainfo->prefix; pfx = pfx->next) {
602 1.1 itojun ndopt_pi = (struct nd_opt_prefix_info *)buf;
603 1.1 itojun ndopt_pi->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
604 1.1 itojun ndopt_pi->nd_opt_pi_len = 4;
605 1.1 itojun ndopt_pi->nd_opt_pi_prefix_len = pfx->prefixlen;
606 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved = 0;
607 1.1 itojun if (pfx->onlinkflg)
608 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved |=
609 1.1 itojun ND_OPT_PI_FLAG_ONLINK;
610 1.1 itojun if (pfx->autoconfflg)
611 1.1 itojun ndopt_pi->nd_opt_pi_flags_reserved |=
612 1.1 itojun ND_OPT_PI_FLAG_AUTO;
613 1.1 itojun ndopt_pi->nd_opt_pi_valid_time = ntohl(pfx->validlifetime);
614 1.1 itojun ndopt_pi->nd_opt_pi_preferred_time =
615 1.1 itojun ntohl(pfx->preflifetime);
616 1.1 itojun ndopt_pi->nd_opt_pi_reserved2 = 0;
617 1.1 itojun ndopt_pi->nd_opt_pi_prefix = pfx->prefix;
618 1.1 itojun
619 1.1 itojun buf += sizeof(struct nd_opt_prefix_info);
620 1.1 itojun }
621 1.1 itojun
622 1.1 itojun return;
623 1.1 itojun }
624