af_inet6.c revision 1.1 1 1.1 thorpej /* $NetBSD: af_inet6.c,v 1.1 2005/03/20 01:09:16 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1983, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
16 1.1 thorpej * may be used to endorse or promote products derived from this software
17 1.1 thorpej * without specific prior written permission.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 thorpej * SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #ifdef INET6
33 1.1 thorpej
34 1.1 thorpej #include <sys/cdefs.h>
35 1.1 thorpej #ifndef lint
36 1.1 thorpej __RCSID("$NetBSD: af_inet6.c,v 1.1 2005/03/20 01:09:16 thorpej Exp $");
37 1.1 thorpej #endif /* not lint */
38 1.1 thorpej
39 1.1 thorpej #include <sys/param.h>
40 1.1 thorpej #include <sys/ioctl.h>
41 1.1 thorpej #include <sys/socket.h>
42 1.1 thorpej
43 1.1 thorpej #include <net/if.h>
44 1.1 thorpej #include <netinet/in.h>
45 1.1 thorpej #include <netinet/in_var.h>
46 1.1 thorpej #include <netinet6/nd6.h>
47 1.1 thorpej
48 1.1 thorpej #include <err.h>
49 1.1 thorpej #include <errno.h>
50 1.1 thorpej #include <ifaddrs.h>
51 1.1 thorpej #include <netdb.h>
52 1.1 thorpej #include <string.h>
53 1.1 thorpej #include <stdlib.h>
54 1.1 thorpej #include <stdio.h>
55 1.1 thorpej
56 1.1 thorpej #include "extern.h"
57 1.1 thorpej #include "af_inet6.h"
58 1.1 thorpej
59 1.1 thorpej static struct in6_ifreq ifr6;
60 1.1 thorpej
61 1.1 thorpej struct in6_ifreq in6_ridreq;
62 1.1 thorpej struct in6_aliasreq in6_addreq;
63 1.1 thorpej
64 1.1 thorpej static char *
65 1.1 thorpej sec2str(time_t total)
66 1.1 thorpej {
67 1.1 thorpej static char result[256];
68 1.1 thorpej int days, hours, mins, secs;
69 1.1 thorpej int first = 1;
70 1.1 thorpej char *p = result;
71 1.1 thorpej char *end = &result[sizeof(result)];
72 1.1 thorpej int n;
73 1.1 thorpej
74 1.1 thorpej if (0) { /*XXX*/
75 1.1 thorpej days = total / 3600 / 24;
76 1.1 thorpej hours = (total / 3600) % 24;
77 1.1 thorpej mins = (total / 60) % 60;
78 1.1 thorpej secs = total % 60;
79 1.1 thorpej
80 1.1 thorpej if (days) {
81 1.1 thorpej first = 0;
82 1.1 thorpej n = snprintf(p, end - p, "%dd", days);
83 1.1 thorpej if (n < 0 || n >= end - p)
84 1.1 thorpej return(result);
85 1.1 thorpej p += n;
86 1.1 thorpej }
87 1.1 thorpej if (!first || hours) {
88 1.1 thorpej first = 0;
89 1.1 thorpej n = snprintf(p, end - p, "%dh", hours);
90 1.1 thorpej if (n < 0 || n >= end - p)
91 1.1 thorpej return(result);
92 1.1 thorpej p += n;
93 1.1 thorpej }
94 1.1 thorpej if (!first || mins) {
95 1.1 thorpej first = 0;
96 1.1 thorpej n = snprintf(p, end - p, "%dm", mins);
97 1.1 thorpej if (n < 0 || n >= end - p)
98 1.1 thorpej return(result);
99 1.1 thorpej p += n;
100 1.1 thorpej }
101 1.1 thorpej snprintf(p, end - p, "%ds", secs);
102 1.1 thorpej } else
103 1.1 thorpej snprintf(p, end - p, "%lu", (u_long)total);
104 1.1 thorpej
105 1.1 thorpej return(result);
106 1.1 thorpej }
107 1.1 thorpej
108 1.1 thorpej static int
109 1.1 thorpej prefix(void *val, int size)
110 1.1 thorpej {
111 1.1 thorpej u_char *pname = (u_char *)val;
112 1.1 thorpej int byte, bit, plen = 0;
113 1.1 thorpej
114 1.1 thorpej for (byte = 0; byte < size; byte++, plen += 8)
115 1.1 thorpej if (pname[byte] != 0xff)
116 1.1 thorpej break;
117 1.1 thorpej if (byte == size)
118 1.1 thorpej return (plen);
119 1.1 thorpej for (bit = 7; bit != 0; bit--, plen++)
120 1.1 thorpej if (!(pname[byte] & (1 << bit)))
121 1.1 thorpej break;
122 1.1 thorpej for (; bit != 0; bit--)
123 1.1 thorpej if (pname[byte] & (1 << bit))
124 1.1 thorpej return(0);
125 1.1 thorpej byte++;
126 1.1 thorpej for (; byte < size; byte++)
127 1.1 thorpej if (pname[byte])
128 1.1 thorpej return(0);
129 1.1 thorpej return (plen);
130 1.1 thorpej }
131 1.1 thorpej
132 1.1 thorpej void
133 1.1 thorpej setia6flags(const char *vname, int value)
134 1.1 thorpej {
135 1.1 thorpej
136 1.1 thorpej if (value < 0) {
137 1.1 thorpej value = -value;
138 1.1 thorpej in6_addreq.ifra_flags &= ~value;
139 1.1 thorpej } else
140 1.1 thorpej in6_addreq.ifra_flags |= value;
141 1.1 thorpej }
142 1.1 thorpej
143 1.1 thorpej void
144 1.1 thorpej setia6pltime(const char *val, int d)
145 1.1 thorpej {
146 1.1 thorpej
147 1.1 thorpej setia6lifetime("pltime", val);
148 1.1 thorpej }
149 1.1 thorpej
150 1.1 thorpej void
151 1.1 thorpej setia6vltime(const char *val, int d)
152 1.1 thorpej {
153 1.1 thorpej
154 1.1 thorpej setia6lifetime("vltime", val);
155 1.1 thorpej }
156 1.1 thorpej
157 1.1 thorpej void
158 1.1 thorpej setia6lifetime(const char *cmd, const char *val)
159 1.1 thorpej {
160 1.1 thorpej time_t newval, t;
161 1.1 thorpej char *ep;
162 1.1 thorpej
163 1.1 thorpej t = time(NULL);
164 1.1 thorpej newval = (time_t)strtoul(val, &ep, 0);
165 1.1 thorpej if (val == ep)
166 1.1 thorpej errx(EXIT_FAILURE, "invalid %s", cmd);
167 1.1 thorpej if (afp->af_af != AF_INET6)
168 1.1 thorpej errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
169 1.1 thorpej if (strcmp(cmd, "vltime") == 0) {
170 1.1 thorpej in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
171 1.1 thorpej in6_addreq.ifra_lifetime.ia6t_vltime = newval;
172 1.1 thorpej } else if (strcmp(cmd, "pltime") == 0) {
173 1.1 thorpej in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
174 1.1 thorpej in6_addreq.ifra_lifetime.ia6t_pltime = newval;
175 1.1 thorpej }
176 1.1 thorpej }
177 1.1 thorpej
178 1.1 thorpej void
179 1.1 thorpej setia6eui64(const char *cmd, int val)
180 1.1 thorpej {
181 1.1 thorpej struct ifaddrs *ifap, *ifa;
182 1.1 thorpej const struct sockaddr_in6 *sin6 = NULL;
183 1.1 thorpej const struct in6_addr *lladdr = NULL;
184 1.1 thorpej struct in6_addr *in6;
185 1.1 thorpej
186 1.1 thorpej if (afp->af_af != AF_INET6)
187 1.1 thorpej errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
188 1.1 thorpej in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
189 1.1 thorpej if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
190 1.1 thorpej errx(EXIT_FAILURE, "interface index is already filled");
191 1.1 thorpej if (getifaddrs(&ifap) != 0)
192 1.1 thorpej err(EXIT_FAILURE, "getifaddrs");
193 1.1 thorpej for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
194 1.1 thorpej if (ifa->ifa_addr->sa_family == AF_INET6 &&
195 1.1 thorpej strcmp(ifa->ifa_name, name) == 0) {
196 1.1 thorpej sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
197 1.1 thorpej if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
198 1.1 thorpej lladdr = &sin6->sin6_addr;
199 1.1 thorpej break;
200 1.1 thorpej }
201 1.1 thorpej }
202 1.1 thorpej }
203 1.1 thorpej if (!lladdr)
204 1.1 thorpej errx(EXIT_FAILURE, "could not determine link local address");
205 1.1 thorpej
206 1.1 thorpej memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
207 1.1 thorpej
208 1.1 thorpej freeifaddrs(ifap);
209 1.1 thorpej }
210 1.1 thorpej
211 1.1 thorpej void
212 1.1 thorpej in6_fillscopeid(struct sockaddr_in6 *sin6)
213 1.1 thorpej {
214 1.1 thorpej #if defined(__KAME__) && defined(KAME_SCOPEID)
215 1.1 thorpej if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
216 1.1 thorpej sin6->sin6_scope_id =
217 1.1 thorpej ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
218 1.1 thorpej sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
219 1.1 thorpej }
220 1.1 thorpej #endif
221 1.1 thorpej }
222 1.1 thorpej
223 1.1 thorpej /* XXX not really an alias */
224 1.1 thorpej void
225 1.1 thorpej in6_alias(struct in6_ifreq *creq)
226 1.1 thorpej {
227 1.1 thorpej struct sockaddr_in6 *sin6;
228 1.1 thorpej char hbuf[NI_MAXHOST];
229 1.1 thorpej u_int32_t scopeid;
230 1.1 thorpej const int niflag = NI_NUMERICHOST;
231 1.1 thorpej
232 1.1 thorpej /* Get the non-alias address for this interface. */
233 1.1 thorpej getsock(AF_INET6);
234 1.1 thorpej if (s < 0) {
235 1.1 thorpej if (errno == EPROTONOSUPPORT)
236 1.1 thorpej return;
237 1.1 thorpej err(EXIT_FAILURE, "socket");
238 1.1 thorpej }
239 1.1 thorpej
240 1.1 thorpej sin6 = (struct sockaddr_in6 *)&creq->ifr_addr;
241 1.1 thorpej
242 1.1 thorpej in6_fillscopeid(sin6);
243 1.1 thorpej scopeid = sin6->sin6_scope_id;
244 1.1 thorpej if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
245 1.1 thorpej hbuf, sizeof(hbuf), NULL, 0, niflag))
246 1.1 thorpej strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
247 1.1 thorpej printf("\tinet6 %s", hbuf);
248 1.1 thorpej
249 1.1 thorpej if (flags & IFF_POINTOPOINT) {
250 1.1 thorpej (void) memset(&ifr6, 0, sizeof(ifr6));
251 1.1 thorpej (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
252 1.1 thorpej ifr6.ifr_addr = creq->ifr_addr;
253 1.1 thorpej if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
254 1.1 thorpej if (errno != EADDRNOTAVAIL)
255 1.1 thorpej warn("SIOCGIFDSTADDR_IN6");
256 1.1 thorpej (void) memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
257 1.1 thorpej ifr6.ifr_addr.sin6_family = AF_INET6;
258 1.1 thorpej ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
259 1.1 thorpej }
260 1.1 thorpej sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
261 1.1 thorpej in6_fillscopeid(sin6);
262 1.1 thorpej hbuf[0] = '\0';
263 1.1 thorpej if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
264 1.1 thorpej hbuf, sizeof(hbuf), NULL, 0, niflag))
265 1.1 thorpej strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
266 1.1 thorpej printf(" -> %s", hbuf);
267 1.1 thorpej }
268 1.1 thorpej
269 1.1 thorpej (void) memset(&ifr6, 0, sizeof(ifr6));
270 1.1 thorpej (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
271 1.1 thorpej ifr6.ifr_addr = creq->ifr_addr;
272 1.1 thorpej if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
273 1.1 thorpej if (errno != EADDRNOTAVAIL)
274 1.1 thorpej warn("SIOCGIFNETMASK_IN6");
275 1.1 thorpej } else {
276 1.1 thorpej sin6 = (struct sockaddr_in6 *)&ifr6.ifr_addr;
277 1.1 thorpej printf(" prefixlen %d", prefix(&sin6->sin6_addr,
278 1.1 thorpej sizeof(struct in6_addr)));
279 1.1 thorpej }
280 1.1 thorpej
281 1.1 thorpej (void) memset(&ifr6, 0, sizeof(ifr6));
282 1.1 thorpej (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
283 1.1 thorpej ifr6.ifr_addr = creq->ifr_addr;
284 1.1 thorpej if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
285 1.1 thorpej if (errno != EADDRNOTAVAIL)
286 1.1 thorpej warn("SIOCGIFAFLAG_IN6");
287 1.1 thorpej } else {
288 1.1 thorpej if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
289 1.1 thorpej printf(" anycast");
290 1.1 thorpej if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
291 1.1 thorpej printf(" tentative");
292 1.1 thorpej if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
293 1.1 thorpej printf(" duplicated");
294 1.1 thorpej if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
295 1.1 thorpej printf(" detached");
296 1.1 thorpej if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
297 1.1 thorpej printf(" deprecated");
298 1.1 thorpej }
299 1.1 thorpej
300 1.1 thorpej if (scopeid)
301 1.1 thorpej printf(" scopeid 0x%x", scopeid);
302 1.1 thorpej
303 1.1 thorpej if (Lflag) {
304 1.1 thorpej struct in6_addrlifetime *lifetime;
305 1.1 thorpej (void) memset(&ifr6, 0, sizeof(ifr6));
306 1.1 thorpej (void) strncpy(ifr6.ifr_name, name, sizeof(ifr6.ifr_name));
307 1.1 thorpej ifr6.ifr_addr = creq->ifr_addr;
308 1.1 thorpej lifetime = &ifr6.ifr_ifru.ifru_lifetime;
309 1.1 thorpej if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
310 1.1 thorpej if (errno != EADDRNOTAVAIL)
311 1.1 thorpej warn("SIOCGIFALIFETIME_IN6");
312 1.1 thorpej } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
313 1.1 thorpej time_t t = time(NULL);
314 1.1 thorpej printf(" pltime ");
315 1.1 thorpej if (lifetime->ia6t_preferred) {
316 1.1 thorpej printf("%s", lifetime->ia6t_preferred < t
317 1.1 thorpej ? "0"
318 1.1 thorpej : sec2str(lifetime->ia6t_preferred - t));
319 1.1 thorpej } else
320 1.1 thorpej printf("infty");
321 1.1 thorpej
322 1.1 thorpej printf(" vltime ");
323 1.1 thorpej if (lifetime->ia6t_expire) {
324 1.1 thorpej printf("%s", lifetime->ia6t_expire < t
325 1.1 thorpej ? "0"
326 1.1 thorpej : sec2str(lifetime->ia6t_expire - t));
327 1.1 thorpej } else
328 1.1 thorpej printf("infty");
329 1.1 thorpej }
330 1.1 thorpej }
331 1.1 thorpej
332 1.1 thorpej printf("\n");
333 1.1 thorpej }
334 1.1 thorpej
335 1.1 thorpej void
336 1.1 thorpej in6_status(int force)
337 1.1 thorpej {
338 1.1 thorpej struct ifaddrs *ifap, *ifa;
339 1.1 thorpej struct in6_ifreq isifr;
340 1.1 thorpej
341 1.1 thorpej if (getifaddrs(&ifap) != 0)
342 1.1 thorpej err(EXIT_FAILURE, "getifaddrs");
343 1.1 thorpej for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
344 1.1 thorpej if (strcmp(name, ifa->ifa_name) != 0)
345 1.1 thorpej continue;
346 1.1 thorpej if (ifa->ifa_addr->sa_family != AF_INET6)
347 1.1 thorpej continue;
348 1.1 thorpej if (sizeof(isifr.ifr_addr) < ifa->ifa_addr->sa_len)
349 1.1 thorpej continue;
350 1.1 thorpej
351 1.1 thorpej memset(&isifr, 0, sizeof(isifr));
352 1.1 thorpej strncpy(isifr.ifr_name, ifa->ifa_name, sizeof(isifr.ifr_name));
353 1.1 thorpej memcpy(&isifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
354 1.1 thorpej in6_alias(&isifr);
355 1.1 thorpej }
356 1.1 thorpej freeifaddrs(ifap);
357 1.1 thorpej }
358 1.1 thorpej
359 1.1 thorpej #define SIN6(x) ((struct sockaddr_in6 *) &(x))
360 1.1 thorpej struct sockaddr_in6 *sin6tab[] = {
361 1.1 thorpej SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
362 1.1 thorpej SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)};
363 1.1 thorpej
364 1.1 thorpej void
365 1.1 thorpej in6_getaddr(const char *str, int which)
366 1.1 thorpej {
367 1.1 thorpej #if defined(__KAME__) && defined(KAME_SCOPEID)
368 1.1 thorpej struct sockaddr_in6 *sin6 = sin6tab[which];
369 1.1 thorpej struct addrinfo hints, *res;
370 1.1 thorpej int error;
371 1.1 thorpej char *slash = NULL;
372 1.1 thorpej
373 1.1 thorpej if (which == ADDR) {
374 1.1 thorpej if ((slash = strrchr(str, '/')) != NULL)
375 1.1 thorpej *slash = '\0';
376 1.1 thorpej }
377 1.1 thorpej
378 1.1 thorpej memset(&hints, 0, sizeof(hints));
379 1.1 thorpej hints.ai_family = AF_INET6;
380 1.1 thorpej hints.ai_socktype = SOCK_DGRAM;
381 1.1 thorpej #if 0 /* in_getaddr() allows FQDN */
382 1.1 thorpej hints.ai_flags = AI_NUMERICHOST;
383 1.1 thorpej #endif
384 1.1 thorpej error = getaddrinfo(str, "0", &hints, &res);
385 1.1 thorpej if (error && slash) {
386 1.1 thorpej /* try again treating the '/' as part of the name */
387 1.1 thorpej *slash = '/';
388 1.1 thorpej slash = NULL;
389 1.1 thorpej error = getaddrinfo(str, "0", &hints, &res);
390 1.1 thorpej }
391 1.1 thorpej if (error)
392 1.1 thorpej errx(EXIT_FAILURE, "%s: %s", str, gai_strerror(error));
393 1.1 thorpej if (res->ai_next)
394 1.1 thorpej errx(EXIT_FAILURE, "%s: resolved to multiple addresses", str);
395 1.1 thorpej if (res->ai_addrlen != sizeof(struct sockaddr_in6))
396 1.1 thorpej errx(EXIT_FAILURE, "%s: bad value", str);
397 1.1 thorpej memcpy(sin6, res->ai_addr, res->ai_addrlen);
398 1.1 thorpej freeaddrinfo(res);
399 1.1 thorpej if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) && sin6->sin6_scope_id) {
400 1.1 thorpej *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] =
401 1.1 thorpej htons(sin6->sin6_scope_id);
402 1.1 thorpej sin6->sin6_scope_id = 0;
403 1.1 thorpej }
404 1.1 thorpej if (slash) {
405 1.1 thorpej in6_getprefix(slash + 1, MASK);
406 1.1 thorpej explicit_prefix = 1;
407 1.1 thorpej }
408 1.1 thorpej #else
409 1.1 thorpej struct sockaddr_in6 *gasin = sin6tab[which];
410 1.1 thorpej
411 1.1 thorpej gasin->sin6_len = sizeof(*gasin);
412 1.1 thorpej if (which != MASK)
413 1.1 thorpej gasin->sin6_family = AF_INET6;
414 1.1 thorpej
415 1.1 thorpej if (which == ADDR) {
416 1.1 thorpej char *p = NULL;
417 1.1 thorpej if((p = strrchr(str, '/')) != NULL) {
418 1.1 thorpej *p = '\0';
419 1.1 thorpej in6_getprefix(p + 1, MASK);
420 1.1 thorpej explicit_prefix = 1;
421 1.1 thorpej }
422 1.1 thorpej }
423 1.1 thorpej
424 1.1 thorpej if (inet_pton(AF_INET6, str, &gasin->sin6_addr) != 1)
425 1.1 thorpej errx(EXIT_FAILURE, "%s: bad value", str);
426 1.1 thorpej #endif
427 1.1 thorpej }
428 1.1 thorpej
429 1.1 thorpej void
430 1.1 thorpej in6_getprefix(const char *plen, int which)
431 1.1 thorpej {
432 1.1 thorpej struct sockaddr_in6 *gpsin = sin6tab[which];
433 1.1 thorpej u_char *cp;
434 1.1 thorpej int len = strtol(plen, (char **)NULL, 10);
435 1.1 thorpej
436 1.1 thorpej if ((len < 0) || (len > 128))
437 1.1 thorpej errx(EXIT_FAILURE, "%s: bad value", plen);
438 1.1 thorpej gpsin->sin6_len = sizeof(*gpsin);
439 1.1 thorpej if (which != MASK)
440 1.1 thorpej gpsin->sin6_family = AF_INET6;
441 1.1 thorpej if ((len == 0) || (len == 128)) {
442 1.1 thorpej memset(&gpsin->sin6_addr, 0xff, sizeof(struct in6_addr));
443 1.1 thorpej return;
444 1.1 thorpej }
445 1.1 thorpej memset((void *)&gpsin->sin6_addr, 0x00, sizeof(gpsin->sin6_addr));
446 1.1 thorpej for (cp = (u_char *)&gpsin->sin6_addr; len > 7; len -= 8)
447 1.1 thorpej *cp++ = 0xff;
448 1.1 thorpej if (len)
449 1.1 thorpej *cp = 0xff << (8 - len);
450 1.1 thorpej }
451 1.1 thorpej
452 1.1 thorpej void
453 1.1 thorpej in6_init(void)
454 1.1 thorpej {
455 1.1 thorpej
456 1.1 thorpej in6_addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
457 1.1 thorpej in6_addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
458 1.1 thorpej }
459 1.1 thorpej
460 1.1 thorpej #endif /* INET6 */
461