2 * SPDX-License-Identifier: BSD-3-Clause
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
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
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
45 #include <arpa/inet.h>
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <arpa/inet.h>
52 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
56 static struct in6_ifreq in6_ridreq;
57 static struct in6_aliasreq in6_addreq =
59 .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
60 static int ip6lifetime;
62 static int prefix(void *, int);
63 static char *sec2str(time_t);
64 static int explicit_prefix = 0;
65 extern char *f_inet6, *f_addr;
67 extern void setnd6flags(const char *, int, int, const struct afswtch *);
68 extern void setnd6defif(const char *, int, int, const struct afswtch *);
69 extern void nd6_status(int);
71 static char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/
74 setifprefixlen(const char *addr, int dummy __unused, int s,
75 const struct afswtch *afp)
77 if (afp->af_getprefix != NULL)
78 afp->af_getprefix(addr, MASK);
83 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
84 const struct afswtch *afp)
86 if (afp->af_af != AF_INET6)
87 err(1, "address flags can be set only for inet6 addresses");
90 in6_addreq.ifra_flags &= ~(-flag);
92 in6_addreq.ifra_flags |= flag;
96 setip6lifetime(const char *cmd, const char *val, int s,
97 const struct afswtch *afp)
103 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
104 newval = (time_t)strtoul(val, &ep, 0);
106 errx(1, "invalid %s", cmd);
107 if (afp->af_af != AF_INET6)
108 errx(1, "%s not allowed for the AF", cmd);
109 if (strcmp(cmd, "vltime") == 0) {
110 in6_addreq.ifra_lifetime.ia6t_expire = now.tv_sec + newval;
111 in6_addreq.ifra_lifetime.ia6t_vltime = newval;
112 } else if (strcmp(cmd, "pltime") == 0) {
113 in6_addreq.ifra_lifetime.ia6t_preferred = now.tv_sec + newval;
114 in6_addreq.ifra_lifetime.ia6t_pltime = newval;
119 setip6pltime(const char *seconds, int dummy __unused, int s,
120 const struct afswtch *afp)
122 setip6lifetime("pltime", seconds, s, afp);
126 setip6vltime(const char *seconds, int dummy __unused, int s,
127 const struct afswtch *afp)
129 setip6lifetime("vltime", seconds, s, afp);
133 setip6eui64(const char *cmd, int dummy __unused, int s,
134 const struct afswtch *afp)
136 struct ifaddrs *ifap, *ifa;
137 const struct sockaddr_in6 *sin6 = NULL;
138 const struct in6_addr *lladdr = NULL;
139 struct in6_addr *in6;
141 if (afp->af_af != AF_INET6)
142 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
143 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
144 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
145 errx(EXIT_FAILURE, "interface index is already filled");
146 if (getifaddrs(&ifap) != 0)
147 err(EXIT_FAILURE, "getifaddrs");
148 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
149 if (ifa->ifa_addr->sa_family == AF_INET6 &&
150 strcmp(ifa->ifa_name, name) == 0) {
151 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
152 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
153 lladdr = &sin6->sin6_addr;
159 errx(EXIT_FAILURE, "could not determine link local address");
161 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
167 in6_status(int s __unused, const struct ifaddrs *ifa)
169 struct sockaddr_in6 *sin, null_sin;
170 struct in6_ifreq ifr6;
173 struct in6_addrlifetime lifetime;
177 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
179 memset(&null_sin, 0, sizeof(null_sin));
181 sin = (struct sockaddr_in6 *)ifa->ifa_addr;
185 strlcpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name));
186 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
187 warn("socket(AF_INET6,SOCK_DGRAM)");
190 ifr6.ifr_addr = *sin;
191 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
192 warn("ioctl(SIOCGIFAFLAG_IN6)");
196 flags6 = ifr6.ifr_ifru.ifru_flags6;
197 memset(&lifetime, 0, sizeof(lifetime));
198 ifr6.ifr_addr = *sin;
199 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
200 warn("ioctl(SIOCGIFALIFETIME_IN6)");
204 lifetime = ifr6.ifr_ifru.ifru_lifetime;
207 if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
209 else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
212 n_flags = NI_NUMERICHOST;
213 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
214 addr_buf, sizeof(addr_buf), NULL, 0,
217 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
219 printf("\tinet6 %s", addr_buf);
221 if (ifa->ifa_flags & IFF_POINTOPOINT) {
222 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
224 * some of the interfaces do not have valid destination
227 if (sin != NULL && sin->sin6_family == AF_INET6) {
230 error = getnameinfo((struct sockaddr *)sin,
231 sin->sin6_len, addr_buf,
232 sizeof(addr_buf), NULL, 0,
235 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
237 printf(" --> %s", addr_buf);
241 sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
244 if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
245 printf("/%d", prefix(&sin->sin6_addr,
246 sizeof(struct in6_addr)));
248 printf(" prefixlen %d", prefix(&sin->sin6_addr,
249 sizeof(struct in6_addr)));
251 if ((flags6 & IN6_IFF_ANYCAST) != 0)
253 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
254 printf(" tentative");
255 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
256 printf(" duplicated");
257 if ((flags6 & IN6_IFF_DETACHED) != 0)
259 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
260 printf(" deprecated");
261 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
263 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
264 printf(" temporary");
265 if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
266 printf(" prefer_source");
268 if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id)
269 printf(" scopeid 0x%x",
270 ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id);
272 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
274 if (lifetime.ia6t_preferred) {
275 printf(" %s", lifetime.ia6t_preferred < now.tv_sec
277 sec2str(lifetime.ia6t_preferred - now.tv_sec));
282 if (lifetime.ia6t_expire) {
283 printf(" %s", lifetime.ia6t_expire < now.tv_sec
285 sec2str(lifetime.ia6t_expire - now.tv_sec));
290 print_vhid(ifa, " ");
295 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
296 static struct sockaddr_in6 *sin6tab[] = {
297 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
298 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
302 in6_getprefix(const char *plen, int which)
304 struct sockaddr_in6 *sin = sin6tab[which];
306 int len = atoi(plen);
308 if ((len < 0) || (len > 128))
309 errx(1, "%s: bad value", plen);
310 sin->sin6_len = sizeof(*sin);
312 sin->sin6_family = AF_INET6;
313 if ((len == 0) || (len == 128)) {
314 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
317 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
318 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
320 *cp = 0xff << (8 - len);
324 in6_getaddr(const char *s, int which)
326 struct sockaddr_in6 *sin = sin6tab[which];
327 struct addrinfo hints, *res;
332 sin->sin6_len = sizeof(*sin);
334 sin->sin6_family = AF_INET6;
338 if((p = strrchr(s, '/')) != NULL) {
340 in6_getprefix(p + 1, MASK);
345 if (sin->sin6_family == AF_INET6) {
346 bzero(&hints, sizeof(struct addrinfo));
347 hints.ai_family = AF_INET6;
348 error = getaddrinfo(s, NULL, &hints, &res);
350 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
351 errx(1, "%s: bad value", s);
353 bcopy(res->ai_addr, sin, res->ai_addrlen);
360 prefix(void *val, int size)
362 u_char *name = (u_char *)val;
363 int byte, bit, plen = 0;
365 for (byte = 0; byte < size; byte++, plen += 8)
366 if (name[byte] != 0xff)
370 for (bit = 7; bit != 0; bit--, plen++)
371 if (!(name[byte] & (1 << bit)))
373 for (; bit != 0; bit--)
374 if (name[byte] & (1 << bit))
377 for (; byte < size; byte++)
384 sec2str(time_t total)
386 static char result[256];
387 int days, hours, mins, secs;
392 days = total / 3600 / 24;
393 hours = (total / 3600) % 24;
394 mins = (total / 60) % 60;
399 p += sprintf(p, "%dd", days);
401 if (!first || hours) {
403 p += sprintf(p, "%dh", hours);
405 if (!first || mins) {
407 p += sprintf(p, "%dm", mins);
409 sprintf(p, "%ds", secs);
411 sprintf(result, "%lu", (unsigned long)total);
417 in6_postproc(int s, const struct afswtch *afp, int newaddr __unused,
418 int ifflags __unused)
420 if (explicit_prefix == 0) {
421 /* Aggregatable address architecture defines all prefixes
422 are 64. So, it is convenient to set prefixlen to 64 if
423 it is not specified. */
424 setifprefixlen("64", 0, s, afp);
425 /* in6_getprefix("64", MASK) if MASK is available here... */
430 in6_status_tunnel(int s)
432 char src[NI_MAXHOST];
433 char dst[NI_MAXHOST];
434 struct in6_ifreq in6_ifr;
435 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
437 memset(&in6_ifr, 0, sizeof(in6_ifr));
438 strlcpy(in6_ifr.ifr_name, name, sizeof(in6_ifr.ifr_name));
440 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
442 if (sa->sa_family != AF_INET6)
444 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
445 NI_NUMERICHOST) != 0)
448 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
450 if (sa->sa_family != AF_INET6)
452 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
453 NI_NUMERICHOST) != 0)
456 printf("\ttunnel inet6 %s --> %s\n", src, dst);
460 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
462 struct in6_aliasreq in6_addreq;
464 memset(&in6_addreq, 0, sizeof(in6_addreq));
465 strlcpy(in6_addreq.ifra_name, name, sizeof(in6_addreq.ifra_name));
466 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
467 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
468 dstres->ai_addr->sa_len);
470 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
471 warn("SIOCSIFPHYADDR_IN6");
474 static struct cmd inet6_cmds[] = {
475 DEF_CMD_ARG("prefixlen", setifprefixlen),
476 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags),
477 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags),
478 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags),
479 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags),
480 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags),
481 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags),
482 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags),
483 DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE, setip6flags),
484 DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
485 DEF_CMD("accept_rtadv", ND6_IFF_ACCEPT_RTADV, setnd6flags),
486 DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV, setnd6flags),
487 DEF_CMD("no_radr", ND6_IFF_NO_RADR, setnd6flags),
488 DEF_CMD("-no_radr", -ND6_IFF_NO_RADR, setnd6flags),
489 DEF_CMD("defaultif", 1, setnd6defif),
490 DEF_CMD("-defaultif", -1, setnd6defif),
491 DEF_CMD("ifdisabled", ND6_IFF_IFDISABLED, setnd6flags),
492 DEF_CMD("-ifdisabled", -ND6_IFF_IFDISABLED, setnd6flags),
493 DEF_CMD("nud", ND6_IFF_PERFORMNUD, setnd6flags),
494 DEF_CMD("-nud", -ND6_IFF_PERFORMNUD, setnd6flags),
495 DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
496 DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
497 DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
498 DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
499 DEF_CMD("no_dad", ND6_IFF_NO_DAD, setnd6flags),
500 DEF_CMD("-no_dad", -ND6_IFF_NO_DAD, setnd6flags),
501 DEF_CMD_ARG("pltime", setip6pltime),
502 DEF_CMD_ARG("vltime", setip6vltime),
503 DEF_CMD("eui64", 0, setip6eui64),
505 DEF_CMD("ipv6_only", ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
506 DEF_CMD("-ipv6_only", -ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
510 static struct afswtch af_inet6 = {
513 .af_status = in6_status,
514 .af_getaddr = in6_getaddr,
515 .af_getprefix = in6_getprefix,
516 .af_other_status = nd6_status,
517 .af_postproc = in6_postproc,
518 .af_status_tunnel = in6_status_tunnel,
519 .af_settunnel = in6_set_tunnel,
520 .af_difaddr = SIOCDIFADDR_IN6,
521 .af_aifaddr = SIOCAIFADDR_IN6,
522 .af_ridreq = &in6_addreq,
523 .af_addreq = &in6_addreq,
527 in6_Lopt_cb(const char *optarg __unused)
529 ip6lifetime++; /* print IPv6 address lifetime */
531 static struct option in6_Lopt = {
537 static __constructor void
543 if (!feature_present("inet6"))
547 for (i = 0; i < nitems(inet6_cmds); i++)
548 cmd_register(&inet6_cmds[i]);
549 af_register(&af_inet6);
550 opt_register(&in6_Lopt);