1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 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. 18 * 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. 30 */ 31 32 #ifndef lint 33 static const char rcsid[] = 34 "$FreeBSD$"; 35 #endif /* not lint */ 36 37 #include <sys/param.h> 38 #include <sys/ioctl.h> 39 #include <sys/socket.h> 40 #include <net/if.h> 41 42 #include <err.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 #include <time.h> 48 #include <ifaddrs.h> 49 50 #include <arpa/inet.h> 51 52 #include <netinet/in.h> 53 #include <netinet/in_var.h> 54 #include <arpa/inet.h> 55 #include <netdb.h> 56 57 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */ 58 59 #include "ifconfig.h" 60 61 static struct in6_ifreq in6_ridreq; 62 static struct in6_aliasreq in6_addreq = 63 { .ifra_flags = 0, 64 .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } }; 65 static int ip6lifetime; 66 67 static int prefix(void *, int); 68 static char *sec2str(time_t); 69 static int explicit_prefix = 0; 70 extern char *f_inet6, *f_addr; 71 72 extern void setnd6flags(const char *, int, int, const struct afswtch *); 73 extern void setnd6defif(const char *, int, int, const struct afswtch *); 74 extern void nd6_status(int); 75 76 static char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/ 77 78 static void 79 setifprefixlen(const char *addr, int dummy __unused, int s, 80 const struct afswtch *afp) 81 { 82 if (afp->af_getprefix != NULL) 83 afp->af_getprefix(addr, MASK); 84 explicit_prefix = 1; 85 } 86 87 static void 88 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused, 89 const struct afswtch *afp) 90 { 91 if (afp->af_af != AF_INET6) 92 err(1, "address flags can be set only for inet6 addresses"); 93 94 if (flag < 0) 95 in6_addreq.ifra_flags &= ~(-flag); 96 else 97 in6_addreq.ifra_flags |= flag; 98 } 99 100 static void 101 setip6lifetime(const char *cmd, const char *val, int s, 102 const struct afswtch *afp) 103 { 104 struct timespec now; 105 time_t newval; 106 char *ep; 107 108 clock_gettime(CLOCK_MONOTONIC_FAST, &now); 109 newval = (time_t)strtoul(val, &ep, 0); 110 if (val == ep) 111 errx(1, "invalid %s", cmd); 112 if (afp->af_af != AF_INET6) 113 errx(1, "%s not allowed for the AF", cmd); 114 if (strcmp(cmd, "vltime") == 0) { 115 in6_addreq.ifra_lifetime.ia6t_expire = now.tv_sec + newval; 116 in6_addreq.ifra_lifetime.ia6t_vltime = newval; 117 } else if (strcmp(cmd, "pltime") == 0) { 118 in6_addreq.ifra_lifetime.ia6t_preferred = now.tv_sec + newval; 119 in6_addreq.ifra_lifetime.ia6t_pltime = newval; 120 } 121 } 122 123 static void 124 setip6pltime(const char *seconds, int dummy __unused, int s, 125 const struct afswtch *afp) 126 { 127 setip6lifetime("pltime", seconds, s, afp); 128 } 129 130 static void 131 setip6vltime(const char *seconds, int dummy __unused, int s, 132 const struct afswtch *afp) 133 { 134 setip6lifetime("vltime", seconds, s, afp); 135 } 136 137 static void 138 setip6eui64(const char *cmd, int dummy __unused, int s, 139 const struct afswtch *afp) 140 { 141 struct ifaddrs *ifap, *ifa; 142 const struct sockaddr_in6 *sin6 = NULL; 143 const struct in6_addr *lladdr = NULL; 144 struct in6_addr *in6; 145 146 if (afp->af_af != AF_INET6) 147 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd); 148 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr; 149 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) 150 errx(EXIT_FAILURE, "interface index is already filled"); 151 if (getifaddrs(&ifap) != 0) 152 err(EXIT_FAILURE, "getifaddrs"); 153 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 154 if (ifa->ifa_addr->sa_family == AF_INET6 && 155 strcmp(ifa->ifa_name, name) == 0) { 156 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr; 157 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 158 lladdr = &sin6->sin6_addr; 159 break; 160 } 161 } 162 } 163 if (!lladdr) 164 errx(EXIT_FAILURE, "could not determine link local address"); 165 166 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8); 167 168 freeifaddrs(ifap); 169 } 170 171 static void 172 print_addr(struct sockaddr_in6 *sin) 173 { 174 int error, n_flags; 175 176 if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0) 177 n_flags = 0; 178 else if (f_addr != NULL && strcmp(f_addr, "host") == 0) 179 n_flags = NI_NOFQDN; 180 else 181 n_flags = NI_NUMERICHOST; 182 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, 183 addr_buf, sizeof(addr_buf), NULL, 0, 184 n_flags); 185 if (error != 0) 186 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, 187 sizeof(addr_buf)); 188 printf("\tinet6 %s", addr_buf); 189 } 190 191 static void 192 print_p2p(struct sockaddr_in6 *sin) 193 { 194 int error; 195 196 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf, 197 sizeof(addr_buf), NULL, 0, NI_NUMERICHOST); 198 199 if (error != 0) 200 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, sizeof(addr_buf)); 201 printf(" --> %s", addr_buf); 202 } 203 204 static void 205 print_mask(int plen) 206 { 207 if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0) 208 printf("/%d", plen); 209 else 210 printf(" prefixlen %d", plen); 211 } 212 213 static void 214 print_flags(int flags6) 215 { 216 if ((flags6 & IN6_IFF_ANYCAST) != 0) 217 printf(" anycast"); 218 if ((flags6 & IN6_IFF_TENTATIVE) != 0) 219 printf(" tentative"); 220 if ((flags6 & IN6_IFF_DUPLICATED) != 0) 221 printf(" duplicated"); 222 if ((flags6 & IN6_IFF_DETACHED) != 0) 223 printf(" detached"); 224 if ((flags6 & IN6_IFF_DEPRECATED) != 0) 225 printf(" deprecated"); 226 if ((flags6 & IN6_IFF_AUTOCONF) != 0) 227 printf(" autoconf"); 228 if ((flags6 & IN6_IFF_TEMPORARY) != 0) 229 printf(" temporary"); 230 if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0) 231 printf(" prefer_source"); 232 233 } 234 235 static void 236 print_lifetime(const char *prepend, time_t px_time, struct timespec *now) 237 { 238 printf(" %s", prepend); 239 if (px_time == 0) 240 printf(" infty"); 241 242 printf(" %s", px_time < now->tv_sec ? "0" : sec2str(px_time - now->tv_sec)); 243 } 244 245 static void 246 in6_status(int s __unused, const struct ifaddrs *ifa) 247 { 248 struct sockaddr_in6 *sin, null_sin = {}; 249 struct in6_ifreq ifr6; 250 int s6; 251 u_int32_t flags6; 252 struct in6_addrlifetime lifetime; 253 254 sin = (struct sockaddr_in6 *)ifa->ifa_addr; 255 if (sin == NULL) 256 return; 257 258 strlcpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name)); 259 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 260 warn("socket(AF_INET6,SOCK_DGRAM)"); 261 return; 262 } 263 ifr6.ifr_addr = *sin; 264 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) { 265 warn("ioctl(SIOCGIFAFLAG_IN6)"); 266 close(s6); 267 return; 268 } 269 flags6 = ifr6.ifr_ifru.ifru_flags6; 270 memset(&lifetime, 0, sizeof(lifetime)); 271 ifr6.ifr_addr = *sin; 272 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) { 273 warn("ioctl(SIOCGIFALIFETIME_IN6)"); 274 close(s6); 275 return; 276 } 277 lifetime = ifr6.ifr_ifru.ifru_lifetime; 278 close(s6); 279 280 print_addr(sin); 281 282 if (ifa->ifa_flags & IFF_POINTOPOINT) { 283 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr; 284 /* 285 * some of the interfaces do not have valid destination 286 * address. 287 */ 288 if (sin != NULL && sin->sin6_family == AF_INET6) 289 print_p2p(sin); 290 } 291 292 sin = (struct sockaddr_in6 *)ifa->ifa_netmask; 293 if (sin == NULL) 294 sin = &null_sin; 295 print_mask(prefix(&sin->sin6_addr, sizeof(struct in6_addr))); 296 297 print_flags(flags6); 298 299 if (((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id) 300 printf(" scopeid 0x%x", 301 ((struct sockaddr_in6 *)(ifa->ifa_addr))->sin6_scope_id); 302 303 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) { 304 struct timespec now; 305 306 clock_gettime(CLOCK_MONOTONIC_FAST, &now); 307 print_lifetime("pltime", lifetime.ia6t_preferred, &now); 308 print_lifetime("vltime", lifetime.ia6t_expire, &now); 309 } 310 311 print_vhid(ifa, " "); 312 313 putchar('\n'); 314 } 315 316 #define SIN6(x) ((struct sockaddr_in6 *) &(x)) 317 static struct sockaddr_in6 *sin6tab[] = { 318 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr), 319 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr) 320 }; 321 322 static void 323 in6_getprefix(const char *plen, int which) 324 { 325 struct sockaddr_in6 *sin = sin6tab[which]; 326 u_char *cp; 327 int len = atoi(plen); 328 329 if ((len < 0) || (len > 128)) 330 errx(1, "%s: bad value", plen); 331 sin->sin6_len = sizeof(*sin); 332 if (which != MASK) 333 sin->sin6_family = AF_INET6; 334 if ((len == 0) || (len == 128)) { 335 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr)); 336 return; 337 } 338 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr)); 339 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8) 340 *cp++ = 0xff; 341 *cp = 0xff << (8 - len); 342 } 343 344 static void 345 in6_getaddr(const char *s, int which) 346 { 347 struct sockaddr_in6 *sin = sin6tab[which]; 348 struct addrinfo hints, *res; 349 int error = -1; 350 351 newaddr &= 1; 352 353 sin->sin6_len = sizeof(*sin); 354 if (which != MASK) 355 sin->sin6_family = AF_INET6; 356 357 if (which == ADDR) { 358 char *p = NULL; 359 if((p = strrchr(s, '/')) != NULL) { 360 *p = '\0'; 361 in6_getprefix(p + 1, MASK); 362 explicit_prefix = 1; 363 } 364 } 365 366 if (sin->sin6_family == AF_INET6) { 367 bzero(&hints, sizeof(struct addrinfo)); 368 hints.ai_family = AF_INET6; 369 error = getaddrinfo(s, NULL, &hints, &res); 370 if (error != 0) { 371 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) 372 errx(1, "%s: bad value", s); 373 } else { 374 bcopy(res->ai_addr, sin, res->ai_addrlen); 375 freeaddrinfo(res); 376 } 377 } 378 } 379 380 static int 381 prefix(void *val, int size) 382 { 383 u_char *name = (u_char *)val; 384 int byte, bit, plen = 0; 385 386 for (byte = 0; byte < size; byte++, plen += 8) 387 if (name[byte] != 0xff) 388 break; 389 if (byte == size) 390 return (plen); 391 for (bit = 7; bit != 0; bit--, plen++) 392 if (!(name[byte] & (1 << bit))) 393 break; 394 for (; bit != 0; bit--) 395 if (name[byte] & (1 << bit)) 396 return(0); 397 byte++; 398 for (; byte < size; byte++) 399 if (name[byte]) 400 return(0); 401 return (plen); 402 } 403 404 static char * 405 sec2str(time_t total) 406 { 407 static char result[256]; 408 int days, hours, mins, secs; 409 int first = 1; 410 char *p = result; 411 412 if (0) { 413 days = total / 3600 / 24; 414 hours = (total / 3600) % 24; 415 mins = (total / 60) % 60; 416 secs = total % 60; 417 418 if (days) { 419 first = 0; 420 p += sprintf(p, "%dd", days); 421 } 422 if (!first || hours) { 423 first = 0; 424 p += sprintf(p, "%dh", hours); 425 } 426 if (!first || mins) { 427 first = 0; 428 p += sprintf(p, "%dm", mins); 429 } 430 sprintf(p, "%ds", secs); 431 } else 432 sprintf(result, "%lu", (unsigned long)total); 433 434 return(result); 435 } 436 437 static void 438 in6_postproc(int s, const struct afswtch *afp, int newaddr __unused, 439 int ifflags __unused) 440 { 441 if (explicit_prefix == 0) { 442 /* Aggregatable address architecture defines all prefixes 443 are 64. So, it is convenient to set prefixlen to 64 if 444 it is not specified. */ 445 setifprefixlen("64", 0, s, afp); 446 /* in6_getprefix("64", MASK) if MASK is available here... */ 447 } 448 } 449 450 static void 451 in6_status_tunnel(int s) 452 { 453 char src[NI_MAXHOST]; 454 char dst[NI_MAXHOST]; 455 struct in6_ifreq in6_ifr; 456 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr; 457 458 memset(&in6_ifr, 0, sizeof(in6_ifr)); 459 strlcpy(in6_ifr.ifr_name, name, sizeof(in6_ifr.ifr_name)); 460 461 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0) 462 return; 463 if (sa->sa_family != AF_INET6) 464 return; 465 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, 466 NI_NUMERICHOST) != 0) 467 src[0] = '\0'; 468 469 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0) 470 return; 471 if (sa->sa_family != AF_INET6) 472 return; 473 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, 474 NI_NUMERICHOST) != 0) 475 dst[0] = '\0'; 476 477 printf("\ttunnel inet6 %s --> %s\n", src, dst); 478 } 479 480 static void 481 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) 482 { 483 struct in6_aliasreq in6_addreq; 484 485 memset(&in6_addreq, 0, sizeof(in6_addreq)); 486 strlcpy(in6_addreq.ifra_name, name, sizeof(in6_addreq.ifra_name)); 487 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); 488 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr, 489 dstres->ai_addr->sa_len); 490 491 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0) 492 warn("SIOCSIFPHYADDR_IN6"); 493 } 494 495 static struct cmd inet6_cmds[] = { 496 DEF_CMD_ARG("prefixlen", setifprefixlen), 497 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags), 498 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags), 499 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags), 500 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags), 501 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags), 502 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags), 503 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags), 504 DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE, setip6flags), 505 DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags), 506 DEF_CMD("accept_rtadv", ND6_IFF_ACCEPT_RTADV, setnd6flags), 507 DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV, setnd6flags), 508 DEF_CMD("no_radr", ND6_IFF_NO_RADR, setnd6flags), 509 DEF_CMD("-no_radr", -ND6_IFF_NO_RADR, setnd6flags), 510 DEF_CMD("defaultif", 1, setnd6defif), 511 DEF_CMD("-defaultif", -1, setnd6defif), 512 DEF_CMD("ifdisabled", ND6_IFF_IFDISABLED, setnd6flags), 513 DEF_CMD("-ifdisabled", -ND6_IFF_IFDISABLED, setnd6flags), 514 DEF_CMD("nud", ND6_IFF_PERFORMNUD, setnd6flags), 515 DEF_CMD("-nud", -ND6_IFF_PERFORMNUD, setnd6flags), 516 DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags), 517 DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags), 518 DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags), 519 DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags), 520 DEF_CMD("no_dad", ND6_IFF_NO_DAD, setnd6flags), 521 DEF_CMD("-no_dad", -ND6_IFF_NO_DAD, setnd6flags), 522 DEF_CMD_ARG("pltime", setip6pltime), 523 DEF_CMD_ARG("vltime", setip6vltime), 524 DEF_CMD("eui64", 0, setip6eui64), 525 #ifdef EXPERIMENTAL 526 DEF_CMD("ipv6_only", ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags), 527 DEF_CMD("-ipv6_only", -ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags), 528 #endif 529 }; 530 531 static struct afswtch af_inet6 = { 532 .af_name = "inet6", 533 .af_af = AF_INET6, 534 .af_status = in6_status, 535 .af_getaddr = in6_getaddr, 536 .af_getprefix = in6_getprefix, 537 .af_other_status = nd6_status, 538 .af_postproc = in6_postproc, 539 .af_status_tunnel = in6_status_tunnel, 540 .af_settunnel = in6_set_tunnel, 541 .af_difaddr = SIOCDIFADDR_IN6, 542 .af_aifaddr = SIOCAIFADDR_IN6, 543 .af_ridreq = &in6_addreq, 544 .af_addreq = &in6_addreq, 545 }; 546 547 static void 548 in6_Lopt_cb(const char *optarg __unused) 549 { 550 ip6lifetime++; /* print IPv6 address lifetime */ 551 } 552 static struct option in6_Lopt = { 553 .opt = "L", 554 .opt_usage = "[-L]", 555 .cb = in6_Lopt_cb 556 }; 557 558 static __constructor void 559 inet6_ctor(void) 560 { 561 size_t i; 562 563 #ifndef RESCUE 564 if (!feature_present("inet6")) 565 return; 566 #endif 567 568 for (i = 0; i < nitems(inet6_cmds); i++) 569 cmd_register(&inet6_cmds[i]); 570 af_register(&af_inet6); 571 opt_register(&in6_Lopt); 572 } 573