2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char rcsid[] =
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
47 #include <arpa/inet.h>
49 #include <netinet/in.h>
50 #include <net/if_var.h> /* for struct ifaddr */
51 #include <netinet/in_var.h>
52 #include <arpa/inet.h>
55 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
59 static struct in6_ifreq in6_ridreq;
60 static struct in6_aliasreq in6_addreq =
62 .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
63 static int ip6lifetime;
65 static void in6_fillscopeid(struct sockaddr_in6 *sin6);
66 static int prefix(void *, int);
67 static char *sec2str(time_t);
68 static int explicit_prefix = 0;
70 extern void setnd6flags(const char *, int, int, const struct afswtch *);
71 extern void setnd6defif(const char *, int, int, const struct afswtch *);
72 extern void nd6_status(int);
74 static char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
77 setifprefixlen(const char *addr, int dummy __unused, int s,
78 const struct afswtch *afp)
80 if (afp->af_getprefix != NULL)
81 afp->af_getprefix(addr, MASK);
86 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
87 const struct afswtch *afp)
89 if (afp->af_af != AF_INET6)
90 err(1, "address flags can be set only for inet6 addresses");
93 in6_addreq.ifra_flags &= ~(-flag);
95 in6_addreq.ifra_flags |= flag;
99 setip6lifetime(const char *cmd, const char *val, int s,
100 const struct afswtch *afp)
106 newval = (time_t)strtoul(val, &ep, 0);
108 errx(1, "invalid %s", cmd);
109 if (afp->af_af != AF_INET6)
110 errx(1, "%s not allowed for the AF", cmd);
111 if (strcmp(cmd, "vltime") == 0) {
112 in6_addreq.ifra_lifetime.ia6t_expire = t + newval;
113 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
114 } else if (strcmp(cmd, "pltime") == 0) {
115 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval;
116 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
121 setip6pltime(const char *seconds, int dummy __unused, int s,
122 const struct afswtch *afp)
124 setip6lifetime("pltime", seconds, s, afp);
128 setip6vltime(const char *seconds, int dummy __unused, int s,
129 const struct afswtch *afp)
131 setip6lifetime("vltime", seconds, s, afp);
135 setip6eui64(const char *cmd, int dummy __unused, int s,
136 const struct afswtch *afp)
138 struct ifaddrs *ifap, *ifa;
139 const struct sockaddr_in6 *sin6 = NULL;
140 const struct in6_addr *lladdr = NULL;
141 struct in6_addr *in6;
143 if (afp->af_af != AF_INET6)
144 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
145 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
146 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
147 errx(EXIT_FAILURE, "interface index is already filled");
148 if (getifaddrs(&ifap) != 0)
149 err(EXIT_FAILURE, "getifaddrs");
150 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
151 if (ifa->ifa_addr->sa_family == AF_INET6 &&
152 strcmp(ifa->ifa_name, name) == 0) {
153 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
154 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
155 lladdr = &sin6->sin6_addr;
161 errx(EXIT_FAILURE, "could not determine link local address");
163 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
169 in6_fillscopeid(struct sockaddr_in6 *sin6)
171 #if defined(__KAME__) && defined(KAME_SCOPEID)
172 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
173 sin6->sin6_scope_id =
174 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]);
175 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0;
181 in6_status(int s __unused, const struct ifaddrs *ifa)
183 struct sockaddr_in6 *sin, null_sin;
184 struct in6_ifreq ifr6;
187 struct in6_addrlifetime lifetime;
188 time_t t = time(NULL);
192 memset(&null_sin, 0, sizeof(null_sin));
194 sin = (struct sockaddr_in6 *)ifa->ifa_addr;
198 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
199 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
200 warn("socket(AF_INET6,SOCK_DGRAM)");
203 ifr6.ifr_addr = *sin;
204 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
205 warn("ioctl(SIOCGIFAFLAG_IN6)");
209 flags6 = ifr6.ifr_ifru.ifru_flags6;
210 memset(&lifetime, 0, sizeof(lifetime));
211 ifr6.ifr_addr = *sin;
212 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
213 warn("ioctl(SIOCGIFALIFETIME_IN6)");
217 lifetime = ifr6.ifr_ifru.ifru_lifetime;
220 /* XXX: embedded link local addr check */
221 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
222 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
225 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
226 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
227 if (sin->sin6_scope_id == 0)
228 sin->sin6_scope_id = ntohs(index);
230 scopeid = sin->sin6_scope_id;
232 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
233 sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
235 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
237 printf("\tinet6 %s ", addr_buf);
239 if (ifa->ifa_flags & IFF_POINTOPOINT) {
240 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
242 * some of the interfaces do not have valid destination
245 if (sin != NULL && sin->sin6_family == AF_INET6) {
248 /* XXX: embedded link local addr check */
249 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) &&
250 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) {
253 index = *(u_short *)&sin->sin6_addr.s6_addr[2];
254 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0;
255 if (sin->sin6_scope_id == 0)
256 sin->sin6_scope_id = ntohs(index);
259 error = getnameinfo((struct sockaddr *)sin,
260 sin->sin6_len, addr_buf,
261 sizeof(addr_buf), NULL, 0,
264 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
266 printf("--> %s ", addr_buf);
270 sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
273 printf("prefixlen %d ", prefix(&sin->sin6_addr,
274 sizeof(struct in6_addr)));
276 if ((flags6 & IN6_IFF_ANYCAST) != 0)
278 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
279 printf("tentative ");
280 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
281 printf("duplicated ");
282 if ((flags6 & IN6_IFF_DETACHED) != 0)
284 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
285 printf("deprecated ");
286 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
288 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
289 printf("temporary ");
292 printf("scopeid 0x%x ", scopeid);
294 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
296 if (lifetime.ia6t_preferred) {
297 printf("%s ", lifetime.ia6t_preferred < t
298 ? "0" : sec2str(lifetime.ia6t_preferred - t));
303 if (lifetime.ia6t_expire) {
304 printf("%s ", lifetime.ia6t_expire < t
305 ? "0" : sec2str(lifetime.ia6t_expire - t));
313 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
314 static struct sockaddr_in6 *sin6tab[] = {
315 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
316 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
320 in6_getprefix(const char *plen, int which)
322 struct sockaddr_in6 *sin = sin6tab[which];
324 int len = atoi(plen);
326 if ((len < 0) || (len > 128))
327 errx(1, "%s: bad value", plen);
328 sin->sin6_len = sizeof(*sin);
330 sin->sin6_family = AF_INET6;
331 if ((len == 0) || (len == 128)) {
332 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
335 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
336 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
338 *cp = 0xff << (8 - len);
342 in6_getaddr(const char *s, int which)
344 struct sockaddr_in6 *sin = sin6tab[which];
345 struct addrinfo hints, *res;
350 sin->sin6_len = sizeof(*sin);
352 sin->sin6_family = AF_INET6;
356 if((p = strrchr(s, '/')) != NULL) {
358 in6_getprefix(p + 1, MASK);
363 if (sin->sin6_family == AF_INET6) {
364 bzero(&hints, sizeof(struct addrinfo));
365 hints.ai_family = AF_INET6;
366 error = getaddrinfo(s, NULL, &hints, &res);
369 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
370 errx(1, "%s: bad value", s);
372 bcopy(res->ai_addr, sin, res->ai_addrlen);
376 prefix(void *val, int size)
378 u_char *name = (u_char *)val;
379 int byte, bit, plen = 0;
381 for (byte = 0; byte < size; byte++, plen += 8)
382 if (name[byte] != 0xff)
386 for (bit = 7; bit != 0; bit--, plen++)
387 if (!(name[byte] & (1 << bit)))
389 for (; bit != 0; bit--)
390 if (name[byte] & (1 << bit))
393 for (; byte < size; byte++)
400 sec2str(time_t total)
402 static char result[256];
403 int days, hours, mins, secs;
408 days = total / 3600 / 24;
409 hours = (total / 3600) % 24;
410 mins = (total / 60) % 60;
415 p += sprintf(p, "%dd", days);
417 if (!first || hours) {
419 p += sprintf(p, "%dh", hours);
421 if (!first || mins) {
423 p += sprintf(p, "%dm", mins);
425 sprintf(p, "%ds", secs);
427 sprintf(result, "%lu", (unsigned long)total);
433 in6_postproc(int s, const struct afswtch *afp)
435 if (explicit_prefix == 0) {
436 /* Aggregatable address architecture defines all prefixes
437 are 64. So, it is convenient to set prefixlen to 64 if
438 it is not specified. */
439 setifprefixlen("64", 0, s, afp);
440 /* in6_getprefix("64", MASK) if MASK is available here... */
445 in6_status_tunnel(int s)
447 char src[NI_MAXHOST];
448 char dst[NI_MAXHOST];
449 struct in6_ifreq in6_ifr;
450 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
452 memset(&in6_ifr, 0, sizeof(in6_ifr));
453 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
455 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
457 if (sa->sa_family != AF_INET6)
459 in6_fillscopeid(&in6_ifr.ifr_addr);
460 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
461 NI_NUMERICHOST) != 0)
464 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
466 if (sa->sa_family != AF_INET6)
468 in6_fillscopeid(&in6_ifr.ifr_addr);
469 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
470 NI_NUMERICHOST) != 0)
473 printf("\ttunnel inet6 %s --> %s\n", src, dst);
477 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
479 struct in6_aliasreq in6_addreq;
481 memset(&in6_addreq, 0, sizeof(in6_addreq));
482 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
483 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
484 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
485 dstres->ai_addr->sa_len);
487 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
488 warn("SIOCSIFPHYADDR_IN6");
491 static struct cmd inet6_cmds[] = {
492 DEF_CMD_ARG("prefixlen", setifprefixlen),
493 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags),
494 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags),
495 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags),
496 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags),
497 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags),
498 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags),
499 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags),
500 DEF_CMD("accept_rtadv", ND6_IFF_ACCEPT_RTADV, setnd6flags),
501 DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV, setnd6flags),
502 DEF_CMD("no_radr", ND6_IFF_NO_RADR, setnd6flags),
503 DEF_CMD("-no_radr", -ND6_IFF_NO_RADR, setnd6flags),
504 DEF_CMD("defaultif", 1, setnd6defif),
505 DEF_CMD("-defaultif", -1, setnd6defif),
506 DEF_CMD("ifdisabled", ND6_IFF_IFDISABLED, setnd6flags),
507 DEF_CMD("-ifdisabled", -ND6_IFF_IFDISABLED, setnd6flags),
508 DEF_CMD("nud", ND6_IFF_PERFORMNUD, setnd6flags),
509 DEF_CMD("-nud", -ND6_IFF_PERFORMNUD, setnd6flags),
510 DEF_CMD("prefer_source",ND6_IFF_PREFER_SOURCE, setnd6flags),
511 DEF_CMD("-prefer_source",-ND6_IFF_PREFER_SOURCE,setnd6flags),
512 DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
513 DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
514 DEF_CMD_ARG("pltime", setip6pltime),
515 DEF_CMD_ARG("vltime", setip6vltime),
516 DEF_CMD("eui64", 0, setip6eui64),
519 static struct afswtch af_inet6 = {
522 .af_status = in6_status,
523 .af_getaddr = in6_getaddr,
524 .af_getprefix = in6_getprefix,
525 .af_other_status = nd6_status,
526 .af_postproc = in6_postproc,
527 .af_status_tunnel = in6_status_tunnel,
528 .af_settunnel = in6_set_tunnel,
529 .af_difaddr = SIOCDIFADDR_IN6,
530 .af_aifaddr = SIOCAIFADDR_IN6,
531 .af_ridreq = &in6_addreq,
532 .af_addreq = &in6_addreq,
536 in6_Lopt_cb(const char *optarg __unused)
538 ip6lifetime++; /* print IPv6 address lifetime */
540 static struct option in6_Lopt = { .opt = "L", .opt_usage = "[-L]", .cb = in6_Lopt_cb };
542 static __constructor void
545 #define N(a) (sizeof(a) / sizeof(a[0]))
549 if (!feature_present("inet6"))
553 for (i = 0; i < N(inet6_cmds); i++)
554 cmd_register(&inet6_cmds[i]);
555 af_register(&af_inet6);
556 opt_register(&in6_Lopt);