Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / sbin / ifconfig / af_inet6.c
blobc703896603f23870af75ac818c6592352fcaf07c
1 /* $NetBSD: af_inet6.c,v 1.24 2009/08/07 18:53:37 dyoung Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: af_inet6.c,v 1.24 2009/08/07 18:53:37 dyoung Exp $");
35 #endif /* not lint */
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44 #include <netinet6/nd6.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <ifaddrs.h>
49 #include <netdb.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <util.h>
55 #include "env.h"
56 #include "extern.h"
57 #include "parse.h"
58 #include "extern.h"
59 #include "af_inetany.h"
61 static void in6_constructor(void) __attribute__((constructor));
62 static void in6_alias(const char *, prop_dictionary_t, prop_dictionary_t,
63 struct in6_ifreq *);
64 static void in6_commit_address(prop_dictionary_t, prop_dictionary_t);
66 static int setia6eui64_impl(prop_dictionary_t, struct in6_aliasreq *);
67 static int setia6flags_impl(prop_dictionary_t, struct in6_aliasreq *);
68 static int setia6pltime_impl(prop_dictionary_t, struct in6_aliasreq *);
69 static int setia6vltime_impl(prop_dictionary_t, struct in6_aliasreq *);
71 static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
73 static void in6_delscopeid(struct sockaddr_in6 *sin6);
74 static void in6_status(prop_dictionary_t, prop_dictionary_t, bool);
76 static struct usage_func usage;
77 static cmdloop_branch_t branch[2];
79 static const struct kwinst ia6flagskw[] = {
80 IFKW("anycast", IN6_IFF_ANYCAST)
81 , IFKW("tentative", IN6_IFF_TENTATIVE)
82 , IFKW("deprecated", IN6_IFF_DEPRECATED)
85 static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
86 "pltime", 0, NULL, "pltime", &command_root.pb_parser);
88 static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
89 "vltime", 0, NULL, "vltime", &command_root.pb_parser);
91 static const struct kwinst inet6kw[] = {
92 {.k_word = "pltime", .k_nextparser = &parse_pltime.pi_parser}
93 , {.k_word = "vltime", .k_nextparser = &parse_vltime.pi_parser}
94 , {.k_word = "eui64", .k_key = "eui64", .k_type = KW_T_BOOL,
95 .k_bool = true, .k_nextparser = &command_root.pb_parser}
98 struct pkw ia6flags = PKW_INITIALIZER(&ia6flags, "ia6flags", NULL,
99 "ia6flag", ia6flagskw, __arraycount(ia6flagskw), &command_root.pb_parser);
100 struct pkw inet6 = PKW_INITIALIZER(&inet6, "IPv6 keywords", NULL,
101 NULL, inet6kw, __arraycount(inet6kw), NULL);
103 static struct afswtch in6af = {
104 .af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
105 .af_addr_commit = in6_commit_address
108 static int
109 prefix(void *val, int size)
111 u_char *pname = (u_char *)val;
112 int byte, bit, plen = 0;
114 for (byte = 0; byte < size; byte++, plen += 8)
115 if (pname[byte] != 0xff)
116 break;
117 if (byte == size)
118 return (plen);
119 for (bit = 7; bit != 0; bit--, plen++)
120 if (!(pname[byte] & (1 << bit)))
121 break;
122 for (; bit != 0; bit--)
123 if (pname[byte] & (1 << bit))
124 return(0);
125 byte++;
126 for (; byte < size; byte++)
127 if (pname[byte])
128 return(0);
129 return (plen);
133 setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
135 int64_t ia6flag;
137 if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
138 errno = ENOENT;
139 return -1;
142 if (ia6flag < 0) {
143 ia6flag = -ia6flag;
144 ifra->ifra_flags &= ~ia6flag;
145 } else
146 ifra->ifra_flags |= ia6flag;
147 return 0;
151 setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
153 int64_t pltime;
155 if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
156 errno = ENOENT;
157 return -1;
160 return setia6lifetime(env, pltime,
161 &ifra->ifra_lifetime.ia6t_preferred,
162 &ifra->ifra_lifetime.ia6t_pltime);
166 setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
168 int64_t vltime;
170 if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
171 errno = ENOENT;
172 return -1;
175 return setia6lifetime(env, vltime,
176 &ifra->ifra_lifetime.ia6t_expire,
177 &ifra->ifra_lifetime.ia6t_vltime);
180 static int
181 setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
182 uint32_t *ivalp)
184 time_t t;
185 int af;
187 if ((af = getaf(env)) == -1 || af != AF_INET6) {
188 errx(EXIT_FAILURE,
189 "inet6 address lifetime not allowed for the AF");
192 t = time(NULL);
193 *timep = t + val;
194 *ivalp = val;
195 return 0;
199 setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
201 char buf[2][80];
202 struct ifaddrs *ifap, *ifa;
203 const struct sockaddr_in6 *sin6 = NULL;
204 const struct in6_addr *lladdr = NULL;
205 struct in6_addr *in6;
206 const char *ifname;
207 bool doit = false;
208 int af;
210 if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
211 errno = ENOENT;
212 return -1;
215 if ((ifname = getifname(env)) == NULL)
216 return -1;
218 af = getaf(env);
219 if (af != AF_INET6) {
220 errx(EXIT_FAILURE,
221 "eui64 address modifier not allowed for the AF");
223 in6 = &ifra->ifra_addr.sin6_addr;
224 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
225 union {
226 struct sockaddr_in6 sin6;
227 struct sockaddr sa;
228 } any = {.sin6 = {.sin6_family = AF_INET6}};
229 memcpy(&any.sin6.sin6_addr, &in6addr_any,
230 sizeof(any.sin6.sin6_addr));
231 (void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
232 &any.sa);
233 (void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
234 (const struct sockaddr *)&ifra->ifra_addr);
235 errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
236 buf[0], buf[1]);
238 if (getifaddrs(&ifap) != 0)
239 err(EXIT_FAILURE, "getifaddrs");
240 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
241 if (ifa->ifa_addr->sa_family == AF_INET6 &&
242 strcmp(ifa->ifa_name, ifname) == 0) {
243 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
244 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
245 lladdr = &sin6->sin6_addr;
246 break;
250 if (!lladdr)
251 errx(EXIT_FAILURE, "could not determine link local address");
253 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
255 freeifaddrs(ifap);
256 return 0;
259 /* KAME idiosyncrasy */
260 static void
261 in6_delscopeid(struct sockaddr_in6 *sin6)
263 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
264 sin6->sin6_scope_id == 0)
265 return;
267 *(u_int16_t *)&sin6->sin6_addr.s6_addr[2] = htons(sin6->sin6_scope_id);
268 sin6->sin6_scope_id = 0;
271 /* XXX not really an alias */
272 void
273 in6_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
274 struct in6_ifreq *creq)
276 struct in6_ifreq ifr6;
277 struct sockaddr_in6 *sin6;
278 char hbuf[NI_MAXHOST];
279 u_int32_t scopeid;
280 int s;
281 const int niflag = Nflag ? 0 : NI_NUMERICHOST;
282 unsigned short flags;
284 /* Get the non-alias address for this interface. */
285 if ((s = getsock(AF_INET6)) == -1) {
286 if (errno == EAFNOSUPPORT)
287 return;
288 err(EXIT_FAILURE, "socket");
291 sin6 = &creq->ifr_addr;
293 in6_fillscopeid(sin6);
294 scopeid = sin6->sin6_scope_id;
295 if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
296 hbuf, sizeof(hbuf), NULL, 0, niflag))
297 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
298 printf("\tinet6 %s", hbuf);
300 if (getifflags(env, oenv, &flags) == -1)
301 err(EXIT_FAILURE, "%s: getifflags", __func__);
303 if (flags & IFF_POINTOPOINT) {
304 ifr6 = *creq;
305 if (ioctl(s, SIOCGIFDSTADDR_IN6, &ifr6) == -1) {
306 if (errno != EADDRNOTAVAIL)
307 warn("SIOCGIFDSTADDR_IN6");
308 memset(&ifr6.ifr_addr, 0, sizeof(ifr6.ifr_addr));
309 ifr6.ifr_addr.sin6_family = AF_INET6;
310 ifr6.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
312 sin6 = &ifr6.ifr_addr;
313 in6_fillscopeid(sin6);
314 hbuf[0] = '\0';
315 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
316 hbuf, sizeof(hbuf), NULL, 0, niflag))
317 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
318 printf(" -> %s", hbuf);
321 ifr6 = *creq;
322 if (ioctl(s, SIOCGIFNETMASK_IN6, &ifr6) == -1) {
323 if (errno != EADDRNOTAVAIL)
324 warn("SIOCGIFNETMASK_IN6");
325 } else {
326 sin6 = &ifr6.ifr_addr;
327 printf(" prefixlen %d", prefix(&sin6->sin6_addr,
328 sizeof(struct in6_addr)));
331 ifr6 = *creq;
332 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) == -1) {
333 if (errno != EADDRNOTAVAIL)
334 warn("SIOCGIFAFLAG_IN6");
335 } else {
336 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_ANYCAST)
337 printf(" anycast");
338 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_TENTATIVE)
339 printf(" tentative");
340 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DUPLICATED)
341 printf(" duplicated");
342 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DETACHED)
343 printf(" detached");
344 if (ifr6.ifr_ifru.ifru_flags6 & IN6_IFF_DEPRECATED)
345 printf(" deprecated");
348 if (scopeid)
349 printf(" scopeid 0x%x", scopeid);
351 if (get_flag('L')) {
352 struct in6_addrlifetime *lifetime;
353 ifr6 = *creq;
354 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
355 if (ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
356 if (errno != EADDRNOTAVAIL)
357 warn("SIOCGIFALIFETIME_IN6");
358 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
359 time_t t = time(NULL);
360 printf(" pltime ");
361 if (lifetime->ia6t_preferred) {
362 printf("%lu",
363 (unsigned long)(lifetime->ia6t_preferred -
364 MIN(t, lifetime->ia6t_preferred)));
365 } else
366 printf("infty");
368 printf(" vltime ");
369 if (lifetime->ia6t_expire) {
370 printf("%lu",
371 (unsigned long)(lifetime->ia6t_expire -
372 MIN(t, lifetime->ia6t_expire)));
373 } else
374 printf("infty");
379 static void
380 in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
382 struct ifaddrs *ifap, *ifa;
383 struct in6_ifreq ifr;
384 const char *ifname;
385 bool printprefs = false;
387 if ((ifname = getifname(env)) == NULL)
388 err(EXIT_FAILURE, "%s: getifname", __func__);
390 if (getifaddrs(&ifap) != 0)
391 err(EXIT_FAILURE, "getifaddrs");
392 printprefs = ifa_any_preferences(ifname, ifap, AF_INET6);
393 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
394 if (strcmp(ifname, ifa->ifa_name) != 0)
395 continue;
396 if (ifa->ifa_addr->sa_family != AF_INET6)
397 continue;
398 if (sizeof(ifr.ifr_addr) < ifa->ifa_addr->sa_len)
399 continue;
401 memset(&ifr, 0, sizeof(ifr));
402 estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
403 memcpy(&ifr.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
404 in6_alias(ifname, env, oenv, &ifr);
405 if (printprefs)
406 ifa_print_preference(ifa->ifa_name, ifa->ifa_addr);
407 printf("\n");
409 freeifaddrs(ifap);
412 static int
413 in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param)
415 struct in6_aliasreq *ifra = param->req.buf;
417 setia6eui64_impl(env, ifra);
418 setia6vltime_impl(env, ifra);
419 setia6pltime_impl(env, ifra);
420 setia6flags_impl(env, ifra);
421 in6_delscopeid(&ifra->ifra_addr);
422 in6_delscopeid(&ifra->ifra_dstaddr);
424 return 0;
427 static void
428 in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
430 struct in6_ifreq in6_ifr = {
431 .ifr_addr = {
432 .sin6_family = AF_INET6,
433 .sin6_len = sizeof(in6_ifr.ifr_addr),
434 .sin6_addr = {
435 .s6_addr =
436 {0xff, 0xff, 0xff, 0xff,
437 0xff, 0xff, 0xff, 0xff}
441 static struct sockaddr_in6 in6_defmask = {
442 .sin6_family = AF_INET6,
443 .sin6_len = sizeof(in6_defmask),
444 .sin6_addr = {
445 .s6_addr = {0xff, 0xff, 0xff, 0xff,
446 0xff, 0xff, 0xff, 0xff}
450 struct in6_aliasreq in6_ifra = {
451 .ifra_prefixmask = {
452 .sin6_family = AF_INET6,
453 .sin6_len = sizeof(in6_ifra.ifra_prefixmask),
454 .sin6_addr = {
455 .s6_addr =
456 {0xff, 0xff, 0xff, 0xff,
457 0xff, 0xff, 0xff, 0xff}}},
458 .ifra_lifetime = {
459 .ia6t_pltime = ND6_INFINITE_LIFETIME
460 , .ia6t_vltime = ND6_INFINITE_LIFETIME
463 struct afparam in6param = {
464 .req = BUFPARAM(in6_ifra)
465 , .dgreq = BUFPARAM(in6_ifr)
466 , .name = {
467 {.buf = in6_ifr.ifr_name,
468 .buflen = sizeof(in6_ifr.ifr_name)},
469 {.buf = in6_ifra.ifra_name,
470 .buflen = sizeof(in6_ifra.ifra_name)}
472 , .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
473 , .addr = BUFPARAM(in6_ifra.ifra_addr)
474 , .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
475 , .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
476 , .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
477 , .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
478 , .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
479 , .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
480 , .defmask = BUFPARAM(in6_defmask)
481 , .pre_aifaddr = in6_pre_aifaddr
483 commit_address(env, oenv, &in6param);
486 static void
487 in6_usage(prop_dictionary_t env)
489 fprintf(stderr,
490 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
491 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] "
492 "[ eui64 ]\n");
495 static void
496 in6_constructor(void)
498 if (register_flag('L') != 0)
499 err(EXIT_FAILURE, __func__);
500 register_family(&in6af);
501 usage_func_init(&usage, in6_usage);
502 register_usage(&usage);
503 cmdloop_branch_init(&branch[0], &ia6flags.pk_parser);
504 cmdloop_branch_init(&branch[1], &inet6.pk_parser);
505 register_cmdloop_branch(&branch[0]);
506 register_cmdloop_branch(&branch[1]);