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