1 /* $NetBSD: ndp.c,v 1.46 2015/12/14 18:24:06 christos Exp $ */ 2 /* $KAME: ndp.c,v 1.121 2005/07/13 11:30:13 keiichi Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 /* 33 * Copyright (c) 1984, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Sun Microsystems, Inc. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 */ 63 64 /* 65 * Based on: 66 * "@(#) Copyright (c) 1984, 1993\n\ 67 * The Regents of the University of California. All rights reserved.\n"; 68 * 69 * "@(#)arp.c 8.2 (Berkeley) 1/2/94"; 70 */ 71 72 /* 73 * ndp - display, set, delete and flush neighbor cache 74 */ 75 76 77 #include <sys/param.h> 78 #include <sys/file.h> 79 #include <sys/ioctl.h> 80 #include <sys/socket.h> 81 #include <sys/sysctl.h> 82 #include <sys/time.h> 83 84 #include <net/if.h> 85 #include <net/if_dl.h> 86 #include <net/if_types.h> 87 #include <net/route.h> 88 89 #include <netinet/in.h> 90 91 #include <netinet/icmp6.h> 92 #include <netinet6/in6_var.h> 93 #include <netinet6/nd6.h> 94 95 #include <arpa/inet.h> 96 97 #include <netdb.h> 98 #include <errno.h> 99 #include <nlist.h> 100 #include <stdio.h> 101 #include <string.h> 102 #include <paths.h> 103 #include <err.h> 104 #include <stdlib.h> 105 #include <fcntl.h> 106 #include <unistd.h> 107 108 #include "gmt2local.h" 109 #include "prog_ops.h" 110 111 static pid_t pid; 112 static int nflag; 113 static int tflag; 114 static int32_t thiszone; /* time difference with gmt */ 115 static int my_s = -1; 116 static unsigned int repeat = 0; 117 118 119 static char host_buf[NI_MAXHOST]; /* getnameinfo() */ 120 static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */ 121 122 static void getsocket(void); 123 static int set(int, char **); 124 static void get(char *); 125 static int delete(char *); 126 static void dump(struct in6_addr *, int); 127 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, unsigned int, int); 128 static char *ether_str(struct sockaddr_dl *); 129 static int ndp_ether_aton(char *, u_char *); 130 __dead static void usage(void); 131 static int rtmsg(int); 132 static void ifinfo(char *, int, char **); 133 static void rtrlist(void); 134 static void plist(void); 135 static void pfx_flush(void); 136 static void rtrlist(void); 137 static void rtr_flush(void); 138 static void harmonize_rtr(void); 139 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 140 static void getdefif(void); 141 static void setdefif(char *); 142 #endif 143 static const char *sec2str(time_t); 144 static char *ether_str(struct sockaddr_dl *); 145 static void ts_print(const struct timeval *); 146 147 #ifdef ICMPV6CTL_ND6_DRLIST 148 static const char *rtpref_str[] = { 149 "medium", /* 00 */ 150 "high", /* 01 */ 151 "rsv", /* 10 */ 152 "low" /* 11 */ 153 }; 154 #endif 155 156 static int mode = 0; 157 static char *arg = NULL; 158 159 int 160 main(int argc, char **argv) 161 { 162 int ch; 163 164 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1) 165 switch (ch) { 166 case 'a': 167 case 'c': 168 case 'p': 169 case 'r': 170 case 'H': 171 case 'P': 172 case 'R': 173 case 's': 174 case 'I': 175 if (mode) { 176 usage(); 177 /*NOTREACHED*/ 178 } 179 mode = ch; 180 arg = NULL; 181 break; 182 case 'd': 183 case 'f': 184 case 'i' : 185 if (mode) { 186 usage(); 187 /*NOTREACHED*/ 188 } 189 mode = ch; 190 arg = optarg; 191 break; 192 case 'n': 193 nflag = 1; 194 break; 195 case 't': 196 tflag = 1; 197 break; 198 case 'A': 199 if (mode) { 200 usage(); 201 /*NOTREACHED*/ 202 } 203 mode = 'a'; 204 repeat = atoi(optarg); 205 break; 206 default: 207 usage(); 208 } 209 210 argc -= optind; 211 argv += optind; 212 213 if (prog_init && prog_init() == -1) 214 err(1, "init failed"); 215 216 pid = prog_getpid(); 217 thiszone = gmt2local(0L); 218 219 switch (mode) { 220 case 'a': 221 case 'c': 222 if (argc != 0) { 223 usage(); 224 /*NOTREACHED*/ 225 } 226 dump(0, mode == 'c'); 227 break; 228 case 'd': 229 if (argc != 0) { 230 usage(); 231 /*NOTREACHED*/ 232 } 233 (void)delete(arg); 234 break; 235 case 'I': 236 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 237 if (argc > 1) { 238 usage(); 239 /*NOTREACHED*/ 240 } else if (argc == 1) { 241 if (strcmp(*argv, "delete") == 0 || 242 if_nametoindex(*argv)) 243 setdefif(*argv); 244 else 245 errx(1, "invalid interface %s", *argv); 246 } 247 getdefif(); /* always call it to print the result */ 248 break; 249 #else 250 errx(1, "not supported yet"); 251 /*NOTREACHED*/ 252 #endif 253 case 'p': 254 if (argc != 0) { 255 usage(); 256 /*NOTREACHED*/ 257 } 258 plist(); 259 break; 260 case 'i': 261 ifinfo(arg, argc, argv); 262 break; 263 case 'r': 264 if (argc != 0) { 265 usage(); 266 /*NOTREACHED*/ 267 } 268 rtrlist(); 269 break; 270 case 's': 271 if (argc < 2 || argc > 4) 272 usage(); 273 return(set(argc, argv) ? 1 : 0); 274 case 'H': 275 if (argc != 0) { 276 usage(); 277 /*NOTREACHED*/ 278 } 279 harmonize_rtr(); 280 break; 281 case 'P': 282 if (argc != 0) { 283 usage(); 284 /*NOTREACHED*/ 285 } 286 pfx_flush(); 287 break; 288 case 'R': 289 if (argc != 0) { 290 usage(); 291 /*NOTREACHED*/ 292 } 293 rtr_flush(); 294 break; 295 case 0: 296 if (argc != 1) { 297 usage(); 298 /*NOTREACHED*/ 299 } 300 get(argv[0]); 301 break; 302 } 303 return(0); 304 } 305 306 static void 307 makeaddr(struct sockaddr_in6 *mysin, const void *resp) 308 { 309 const struct sockaddr_in6 *res = resp; 310 mysin->sin6_addr = res->sin6_addr; 311 mysin->sin6_scope_id = res->sin6_scope_id; 312 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL); 313 } 314 315 static void 316 getsocket(void) 317 { 318 if (my_s < 0) { 319 my_s = prog_socket(PF_ROUTE, SOCK_RAW, 0); 320 if (my_s < 0) 321 err(1, "socket"); 322 } 323 } 324 325 #ifdef notdef 326 static struct sockaddr_in6 so_mask = { 327 .sin6_len = sizeof(so_mask), 328 .sin6_family = AF_INET6 329 }; 330 #endif 331 static struct sockaddr_in6 blank_sin = { 332 .sin6_len = sizeof(blank_sin), 333 .sin6_family = AF_INET6 334 }; 335 static struct sockaddr_in6 sin_m; 336 static struct sockaddr_dl blank_sdl = { 337 .sdl_len = sizeof(blank_sdl), 338 .sdl_family = AF_LINK, 339 }; 340 static struct sockaddr_dl sdl_m; 341 static int expire_time, flags, found_entry; 342 static struct { 343 struct rt_msghdr m_rtm; 344 char m_space[512]; 345 } m_rtmsg; 346 347 /* 348 * Set an individual neighbor cache entry 349 */ 350 static int 351 set(int argc, char **argv) 352 { 353 register struct sockaddr_in6 *mysin = &sin_m; 354 register struct sockaddr_dl *sdl; 355 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 356 struct addrinfo hints, *res; 357 int gai_error; 358 u_char *ea; 359 char *host = argv[0], *eaddr = argv[1]; 360 361 getsocket(); 362 argc -= 2; 363 argv += 2; 364 sdl_m = blank_sdl; 365 sin_m = blank_sin; 366 367 (void)memset(&hints, 0, sizeof(hints)); 368 hints.ai_family = AF_INET6; 369 gai_error = getaddrinfo(host, NULL, &hints, &res); 370 if (gai_error) { 371 warnx("%s: %s", host, gai_strerror(gai_error)); 372 return 1; 373 } 374 makeaddr(mysin, res->ai_addr); 375 ea = (u_char *)LLADDR(&sdl_m); 376 if (ndp_ether_aton(eaddr, ea) == 0) 377 sdl_m.sdl_alen = 6; 378 flags = expire_time = 0; 379 while (argc-- > 0) { 380 if (strncmp(argv[0], "temp", 4) == 0) { 381 struct timeval tim; 382 383 (void)gettimeofday(&tim, 0); 384 expire_time = tim.tv_sec + 20 * 60; 385 } else if (strncmp(argv[0], "proxy", 5) == 0) 386 flags |= RTF_ANNOUNCE; 387 argv++; 388 } 389 if (rtmsg(RTM_GET) < 0) { 390 errx(1, "RTM_GET(%s) failed", host); 391 /* NOTREACHED */ 392 } 393 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1); 394 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) + (char *)(void *)mysin); 395 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) { 396 if (sdl->sdl_family == AF_LINK && 397 (rtm->rtm_flags & RTF_LLINFO) && 398 !(rtm->rtm_flags & RTF_GATEWAY)) { 399 switch (sdl->sdl_type) { 400 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 401 case IFT_ISO88024: case IFT_ISO88025: 402 goto overwrite; 403 } 404 } 405 /* 406 * IPv4 arp command retries with sin_other = SIN_PROXY here. 407 */ 408 (void)fprintf(stderr, "set: cannot configure a new entry\n"); 409 return 1; 410 } 411 412 overwrite: 413 if (sdl->sdl_family != AF_LINK) { 414 warnx("cannot intuit interface index and type for %s", host); 415 return (1); 416 } 417 sdl_m.sdl_type = sdl->sdl_type; 418 sdl_m.sdl_index = sdl->sdl_index; 419 return (rtmsg(RTM_ADD)); 420 } 421 422 /* 423 * Display an individual neighbor cache entry 424 */ 425 static void 426 get(char *host) 427 { 428 struct sockaddr_in6 *mysin = &sin_m; 429 struct addrinfo hints, *res; 430 int gai_error; 431 432 sin_m = blank_sin; 433 (void)memset(&hints, 0, sizeof(hints)); 434 hints.ai_family = AF_INET6; 435 gai_error = getaddrinfo(host, NULL, &hints, &res); 436 if (gai_error) { 437 warnx("%s: %s", host, gai_strerror(gai_error)); 438 return; 439 } 440 makeaddr(mysin, res->ai_addr); 441 dump(&mysin->sin6_addr, 0); 442 if (found_entry == 0) { 443 (void)getnameinfo((struct sockaddr *)(void *)mysin, 444 (socklen_t)mysin->sin6_len, 445 host_buf, sizeof(host_buf), NULL ,0, 446 (nflag ? NI_NUMERICHOST : 0)); 447 errx(1, "%s (%s) -- no entry", host, host_buf); 448 } 449 } 450 451 /* 452 * Delete a neighbor cache entry 453 */ 454 static int 455 delete(char *host) 456 { 457 struct sockaddr_in6 *mysin = &sin_m; 458 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 459 struct sockaddr_dl *sdl; 460 struct addrinfo hints, *res; 461 int gai_error; 462 463 getsocket(); 464 sin_m = blank_sin; 465 466 bzero(&hints, sizeof(hints)); 467 hints.ai_family = AF_INET6; 468 gai_error = getaddrinfo(host, NULL, &hints, &res); 469 if (gai_error) { 470 warnx("%s: %s", host, gai_strerror(gai_error)); 471 return 1; 472 } 473 makeaddr(mysin, res->ai_addr); 474 if (rtmsg(RTM_GET) < 0) 475 errx(1, "RTM_GET(%s) failed", host); 476 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1); 477 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) + 478 (char *)(void *)mysin); 479 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) { 480 if (sdl->sdl_family == AF_LINK && 481 (rtm->rtm_flags & RTF_LLINFO) && 482 !(rtm->rtm_flags & RTF_GATEWAY)) { 483 goto delete; 484 } 485 /* 486 * IPv4 arp command retries with sin_other = SIN_PROXY here. 487 */ 488 warnx("delete: cannot delete non-NDP entry"); 489 return 1; 490 } 491 492 delete: 493 if (sdl->sdl_family != AF_LINK) { 494 (void)printf("cannot locate %s\n", host); 495 return (1); 496 } 497 if (rtmsg(RTM_DELETE) == 0) { 498 struct sockaddr_in6 s6 = *mysin; /* XXX: for safety */ 499 500 mysin->sin6_scope_id = 0; 501 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL); 502 (void)getnameinfo((struct sockaddr *)(void *)&s6, 503 (socklen_t)s6.sin6_len, host_buf, 504 sizeof(host_buf), NULL, 0, 505 (nflag ? NI_NUMERICHOST : 0)); 506 (void)printf("%s (%s) deleted\n", host, host_buf); 507 } 508 509 return 0; 510 } 511 512 #define W_ADDR 36 513 #define W_LL 17 514 #define W_IF 6 515 516 /* 517 * Dump the entire neighbor cache 518 */ 519 static void 520 dump(struct in6_addr *addr, int cflag) 521 { 522 int mib[6]; 523 size_t needed; 524 char *lim, *buf, *next; 525 struct rt_msghdr *rtm; 526 struct sockaddr_in6 *mysin; 527 struct sockaddr_dl *sdl; 528 struct in6_nbrinfo *nbi; 529 struct timeval tim; 530 int addrwidth; 531 int llwidth; 532 int ifwidth; 533 char flgbuf[8]; 534 const char *ifname; 535 536 /* Print header */ 537 if (!tflag && !cflag) 538 (void)printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n", 539 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 540 W_IF, W_IF, "Netif", "Expire", "S", "Flags"); 541 542 again:; 543 mib[0] = CTL_NET; 544 mib[1] = PF_ROUTE; 545 mib[2] = 0; 546 mib[3] = AF_INET6; 547 mib[4] = NET_RT_FLAGS; 548 mib[5] = RTF_LLINFO; 549 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 550 err(1, "sysctl(PF_ROUTE estimate)"); 551 if (needed > 0) { 552 if ((buf = malloc(needed)) == NULL) 553 err(1, "malloc"); 554 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 555 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 556 lim = buf + needed; 557 } else 558 buf = lim = NULL; 559 560 for (next = buf; next && next < lim; next += rtm->rtm_msglen) { 561 int isrouter = 0, prbs = 0; 562 563 rtm = (struct rt_msghdr *)(void *)next; 564 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1); 565 sdl = (struct sockaddr_dl *)(void *)((char *)(void *)mysin + RT_ROUNDUP(mysin->sin6_len)); 566 567 /* 568 * Some OSes can produce a route that has the LINK flag but 569 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 570 * and BSD/OS, where xx is not the interface identifier on 571 * lo0). Such routes entry would annoy getnbrinfo() below, 572 * so we skip them. 573 * XXX: such routes should have the GATEWAY flag, not the 574 * LINK flag. However, there is rotten routing software 575 * that advertises all routes that have the GATEWAY flag. 576 * Thus, KAME kernel intentionally does not set the LINK flag. 577 * What is to be fixed is not ndp, but such routing software 578 * (and the kernel workaround)... 579 */ 580 if (sdl->sdl_family != AF_LINK) 581 continue; 582 583 if (!(rtm->rtm_flags & RTF_HOST)) 584 continue; 585 586 if (addr) { 587 if (!IN6_ARE_ADDR_EQUAL(addr, &mysin->sin6_addr)) 588 continue; 589 found_entry = 1; 590 } else if (IN6_IS_ADDR_MULTICAST(&mysin->sin6_addr)) 591 continue; 592 if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr) || 593 IN6_IS_ADDR_MC_LINKLOCAL(&mysin->sin6_addr)) { 594 uint16_t scopeid = mysin->sin6_scope_id; 595 inet6_getscopeid(mysin, INET6_IS_ADDR_LINKLOCAL| 596 INET6_IS_ADDR_MC_LINKLOCAL); 597 if (scopeid == 0) 598 mysin->sin6_scope_id = sdl->sdl_index; 599 } 600 (void)getnameinfo((struct sockaddr *)(void *)mysin, 601 (socklen_t)mysin->sin6_len, 602 host_buf, sizeof(host_buf), NULL, 0, 603 (nflag ? NI_NUMERICHOST : 0)); 604 if (cflag) { 605 #ifdef RTF_WASCLONED 606 if (rtm->rtm_flags & RTF_WASCLONED) 607 (void)delete(host_buf); 608 #elif defined(RTF_CLONED) 609 if (rtm->rtm_flags & RTF_CLONED) 610 (void)delete(host_buf); 611 #else 612 (void)delete(host_buf); 613 #endif 614 continue; 615 } 616 (void)gettimeofday(&tim, 0); 617 if (tflag) 618 ts_print(&tim); 619 620 addrwidth = strlen(host_buf); 621 if (addrwidth < W_ADDR) 622 addrwidth = W_ADDR; 623 llwidth = strlen(ether_str(sdl)); 624 if (W_ADDR + W_LL - addrwidth > llwidth) 625 llwidth = W_ADDR + W_LL - addrwidth; 626 ifname = if_indextoname((unsigned int)sdl->sdl_index, ifix_buf); 627 if (!ifname) 628 ifname = "?"; 629 ifwidth = strlen(ifname); 630 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 631 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 632 633 (void)printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, 634 host_buf, llwidth, llwidth, ether_str(sdl), ifwidth, 635 ifwidth, ifname); 636 637 /* Print neighbor discovery specific informations */ 638 nbi = getnbrinfo(&mysin->sin6_addr, 639 (unsigned int)sdl->sdl_index, 1); 640 if (nbi) { 641 if (nbi->expire > tim.tv_sec) { 642 (void)printf(" %-9.9s", 643 sec2str(nbi->expire - tim.tv_sec)); 644 } else if (nbi->expire == 0) 645 (void)printf(" %-9.9s", "permanent"); 646 else 647 (void)printf(" %-9.9s", "expired"); 648 649 switch (nbi->state) { 650 case ND6_LLINFO_NOSTATE: 651 (void)printf(" N"); 652 break; 653 #ifdef ND6_LLINFO_WAITDELETE 654 case ND6_LLINFO_WAITDELETE: 655 (void)printf(" W"); 656 break; 657 #endif 658 case ND6_LLINFO_INCOMPLETE: 659 (void)printf(" I"); 660 break; 661 case ND6_LLINFO_REACHABLE: 662 (void)printf(" R"); 663 break; 664 case ND6_LLINFO_STALE: 665 (void)printf(" S"); 666 break; 667 case ND6_LLINFO_DELAY: 668 (void)printf(" D"); 669 break; 670 case ND6_LLINFO_PROBE: 671 (void)printf(" P"); 672 break; 673 default: 674 (void)printf(" ?"); 675 break; 676 } 677 678 isrouter = nbi->isrouter; 679 prbs = nbi->asked; 680 } else { 681 warnx("failed to get neighbor information"); 682 (void)printf(" "); 683 } 684 685 /* 686 * other flags. R: router, P: proxy, W: ?? 687 */ 688 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) { 689 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s", 690 isrouter ? "R" : "", 691 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 692 } else { 693 mysin = (struct sockaddr_in6 *)(void *) 694 (sdl->sdl_len + (char *)(void *)sdl); 695 #if 0 /* W and P are mystery even for us */ 696 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 697 isrouter ? "R" : "", 698 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "", 699 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "", 700 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 701 #else 702 (void)snprintf(flgbuf, sizeof(flgbuf), "%s%s", 703 isrouter ? "R" : "", 704 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 705 #endif 706 } 707 (void)printf(" %s", flgbuf); 708 709 if (prbs) 710 (void)printf(" %d", prbs); 711 712 (void)printf("\n"); 713 } 714 if (buf != NULL) 715 free(buf); 716 717 if (repeat) { 718 (void)printf("\n"); 719 (void)fflush(stdout); 720 (void)sleep(repeat); 721 goto again; 722 } 723 } 724 725 static struct in6_nbrinfo * 726 getnbrinfo(struct in6_addr *addr, unsigned int ifindex, int warning) 727 { 728 static struct in6_nbrinfo nbi; 729 int s; 730 731 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 732 err(1, "socket"); 733 734 (void)memset(&nbi, 0, sizeof(nbi)); 735 (void)if_indextoname(ifindex, nbi.ifname); 736 nbi.addr = *addr; 737 if (prog_ioctl(s, SIOCGNBRINFO_IN6, &nbi) < 0) { 738 if (warning) 739 warn("ioctl(SIOCGNBRINFO_IN6)"); 740 (void)prog_close(s); 741 return(NULL); 742 } 743 744 (void)prog_close(s); 745 return(&nbi); 746 } 747 748 static char * 749 ether_str(struct sockaddr_dl *sdl) 750 { 751 static char hbuf[NI_MAXHOST]; 752 753 if (sdl->sdl_alen) { 754 if (getnameinfo((struct sockaddr *)(void *)sdl, 755 (socklen_t)sdl->sdl_len, 756 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 757 (void)snprintf(hbuf, sizeof(hbuf), "<invalid>"); 758 } else 759 (void)snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 760 761 return(hbuf); 762 } 763 764 static int 765 ndp_ether_aton(char *a, u_char *n) 766 { 767 int i, o[6]; 768 769 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 770 &o[3], &o[4], &o[5]); 771 if (i != 6) { 772 warnx("invalid Ethernet address '%s'", a); 773 return (1); 774 } 775 for (i = 0; i < 6; i++) 776 n[i] = o[i]; 777 return (0); 778 } 779 780 static void 781 usage(void) 782 { 783 const char *pn = getprogname(); 784 785 (void)fprintf(stderr, "Usage: %s [-nt] hostname\n", pn); 786 (void)fprintf(stderr, 787 " %s [-nt] -a | -c | -p | -r | -H | -P | -R\n", pn); 788 (void)fprintf(stderr, " %s [-nt] -A wait\n", pn); 789 (void)fprintf(stderr, " %s [-nt] -d hostname\n", pn); 790 (void)fprintf(stderr, " %s [-nt] -f filename\n", pn); 791 (void)fprintf(stderr, " %s [-nt] -i interface [flags...]\n", pn); 792 #ifdef SIOCSDEFIFACE_IN6 793 (void)fprintf(stderr, " %s [-nt] -I [interface|delete]\n", pn); 794 #endif 795 (void)fprintf(stderr, 796 " %s [-nt] -s nodename etheraddr [temp] [proxy]\n", pn); 797 exit(1); 798 } 799 800 static int 801 rtmsg(int cmd) 802 { 803 static int seq; 804 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 805 register char *cp = m_rtmsg.m_space; 806 register int l; 807 808 errno = 0; 809 if (cmd == RTM_DELETE) 810 goto doit; 811 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg)); 812 rtm->rtm_flags = flags; 813 rtm->rtm_version = RTM_VERSION; 814 815 switch (cmd) { 816 default: 817 errx(1, "internal wrong cmd"); 818 /*NOTREACHED*/ 819 case RTM_ADD: 820 rtm->rtm_addrs |= RTA_GATEWAY; 821 if (expire_time) { 822 rtm->rtm_rmx.rmx_expire = expire_time; 823 rtm->rtm_inits = RTV_EXPIRE; 824 } 825 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 826 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */ 827 if (rtm->rtm_flags & RTF_ANNOUNCE) { 828 rtm->rtm_flags &= ~RTF_HOST; 829 rtm->rtm_addrs |= RTA_NETMASK; 830 } 831 #endif 832 /* FALLTHROUGH */ 833 case RTM_GET: 834 rtm->rtm_addrs |= RTA_DST; 835 } 836 #define NEXTADDR(w, s) \ 837 if (rtm->rtm_addrs & (w)) { \ 838 (void)memcpy(cp, &s, sizeof(s)); \ 839 RT_ADVANCE(cp, (struct sockaddr *)(void *)&s); \ 840 } 841 842 NEXTADDR(RTA_DST, sin_m); 843 NEXTADDR(RTA_GATEWAY, sdl_m); 844 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */ 845 (void)memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 846 NEXTADDR(RTA_NETMASK, so_mask); 847 #endif 848 849 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg; 850 doit: 851 l = rtm->rtm_msglen; 852 rtm->rtm_seq = ++seq; 853 rtm->rtm_type = cmd; 854 if (prog_write(my_s, &m_rtmsg, (size_t)l) == -1) { 855 if (errno != ESRCH || cmd != RTM_DELETE) 856 err(1, "writing to routing socket"); 857 } 858 do { 859 l = prog_read(my_s, &m_rtmsg, sizeof(m_rtmsg)); 860 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 861 if (l < 0) 862 warn("read from routing socket"); 863 return (0); 864 } 865 866 static void 867 ifinfo(char *ifname, int argc, char **argv) 868 { 869 struct in6_ndireq nd; 870 int i, s; 871 u_int32_t newflags; 872 #ifdef IPV6CTL_USETEMPADDR 873 u_int8_t nullbuf[8]; 874 #endif 875 876 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 877 err(1, "socket"); 878 (void)memset(&nd, 0, sizeof(nd)); 879 (void)strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 880 if (prog_ioctl(s, SIOCGIFINFO_IN6, &nd) < 0) 881 err(1, "ioctl(SIOCGIFINFO_IN6)"); 882 #define ND nd.ndi 883 newflags = ND.flags; 884 for (i = 0; i < argc; i++) { 885 int clear = 0; 886 char *cp = argv[i]; 887 888 if (*cp == '-') { 889 clear = 1; 890 cp++; 891 } 892 893 #define SETFLAG(s, f) \ 894 do {\ 895 if (strcmp(cp, (s)) == 0) {\ 896 if (clear)\ 897 newflags &= ~(f);\ 898 else\ 899 newflags |= (f);\ 900 }\ 901 } while (/*CONSTCOND*/0) 902 /* 903 * XXX: this macro is not 100% correct, in that it matches "nud" against 904 * "nudbogus". But we just let it go since this is minor. 905 */ 906 #define SETVALUE(f, v) \ 907 do { \ 908 char *valptr; \ 909 unsigned long newval; \ 910 v = 0; /* unspecified */ \ 911 if (strncmp(cp, f, strlen(f)) == 0) { \ 912 valptr = strchr(cp, '='); \ 913 if (valptr == NULL) \ 914 err(1, "syntax error in %s field", (f)); \ 915 errno = 0; \ 916 newval = strtoul(++valptr, NULL, 0); \ 917 if (errno) \ 918 err(1, "syntax error in %s's value", (f)); \ 919 v = newval; \ 920 } \ 921 } while (/*CONSTCOND*/0) 922 923 #ifdef ND6_IFF_IFDISABLED 924 SETFLAG("disabled", ND6_IFF_IFDISABLED); 925 #endif 926 SETFLAG("nud", ND6_IFF_PERFORMNUD); 927 #ifdef ND6_IFF_ACCEPT_RTADV 928 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 929 #endif 930 #ifdef ND6_IFF_OVERRIDE_RTADV 931 SETFLAG("override_rtadv", ND6_IFF_OVERRIDE_RTADV); 932 #endif 933 #ifdef ND6_IFF_AUTO_LINKLOCAL 934 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL); 935 #endif 936 #ifdef ND6_IFF_PREFER_SOURCE 937 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE); 938 #endif 939 #ifdef ND6_IFF_DONT_SET_IFROUTE 940 SETFLAG("dont_set_ifroute", ND6_IFF_DONT_SET_IFROUTE); 941 #endif 942 SETVALUE("basereachable", ND.basereachable); 943 SETVALUE("retrans", ND.retrans); 944 SETVALUE("curhlim", ND.chlim); 945 946 ND.flags = newflags; 947 #ifdef SIOCSIFINFO_IN6 948 if (prog_ioctl(s, SIOCSIFINFO_IN6, &nd) < 0) 949 err(1, "ioctl(SIOCSIFINFO_IN6)"); 950 #else 951 if (prog_ioctl(s, SIOCSIFINFO_FLAGS, &nd) < 0) 952 err(1, "ioctl(SIOCSIFINFO_FLAGS)"); 953 #endif 954 #undef SETFLAG 955 #undef SETVALUE 956 } 957 958 if (!ND.initialized) 959 errx(1, "%s: not initialized yet", ifname); 960 961 if (prog_ioctl(s, SIOCGIFINFO_IN6, &nd) < 0) 962 err(1, "ioctl(SIOCGIFINFO_IN6)"); 963 (void)printf("linkmtu=%d", ND.linkmtu); 964 (void)printf(", maxmtu=%d", ND.maxmtu); 965 (void)printf(", curhlim=%d", ND.chlim); 966 (void)printf(", basereachable=%ds%dms", 967 ND.basereachable / 1000, ND.basereachable % 1000); 968 (void)printf(", reachable=%ds", ND.reachable); 969 (void)printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 970 #ifdef IPV6CTL_USETEMPADDR 971 (void)memset(nullbuf, 0, sizeof(nullbuf)); 972 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 973 int j; 974 u_int8_t *rbuf; 975 976 for (i = 0; i < 3; i++) { 977 switch (i) { 978 case 0: 979 (void)printf("\nRandom seed(0): "); 980 rbuf = ND.randomseed0; 981 break; 982 case 1: 983 (void)printf("\nRandom seed(1): "); 984 rbuf = ND.randomseed1; 985 break; 986 case 2: 987 (void)printf("\nRandom ID: "); 988 rbuf = ND.randomid; 989 break; 990 default: 991 errx(1, "impossible case for tempaddr display"); 992 } 993 for (j = 0; j < 8; j++) 994 (void)printf("%02x", rbuf[j]); 995 } 996 } 997 #endif 998 if (ND.flags) { 999 (void)printf("\nFlags: "); 1000 if ((ND.flags & ND6_IFF_PERFORMNUD)) 1001 (void)printf("nud "); 1002 #ifdef ND6_IFF_IFDISABLED 1003 if ((ND.flags & ND6_IFF_IFDISABLED)) 1004 (void)printf("disabled "); 1005 #endif 1006 #ifdef ND6_IFF_ACCEPT_RTADV 1007 if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 1008 (void)printf("accept_rtadv "); 1009 #endif 1010 #ifdef ND6_IFF_OVERRIDE_RTADV 1011 if ((ND.flags & ND6_IFF_OVERRIDE_RTADV)) 1012 (void)printf("override_rtadv "); 1013 #endif 1014 #ifdef ND6_IFF_AUTO_LINKLOCAL 1015 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL)) 1016 (void)printf("auto_linklocal "); 1017 #endif 1018 #ifdef ND6_IFF_PREFER_SOURCE 1019 if ((ND.flags & ND6_IFF_PREFER_SOURCE)) 1020 (void)printf("prefer_source "); 1021 #endif 1022 } 1023 (void)putc('\n', stdout); 1024 #undef ND 1025 1026 (void)prog_close(s); 1027 } 1028 1029 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 1030 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 1031 #endif 1032 1033 static void 1034 rtrlist(void) 1035 { 1036 #ifdef ICMPV6CTL_ND6_DRLIST 1037 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 1038 char *buf; 1039 struct in6_defrouter *p, *ep; 1040 size_t l; 1041 struct timeval tim; 1042 1043 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1044 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1045 /*NOTREACHED*/ 1046 } 1047 if (l == 0) 1048 return; 1049 buf = malloc(l); 1050 if (!buf) { 1051 err(1, "malloc"); 1052 /*NOTREACHED*/ 1053 } 1054 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1055 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1056 /*NOTREACHED*/ 1057 } 1058 1059 ep = (struct in6_defrouter *)(void *)(buf + l); 1060 for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) { 1061 int rtpref; 1062 1063 if (getnameinfo((struct sockaddr *)(void *)&p->rtaddr, 1064 (socklen_t)p->rtaddr.sin6_len, host_buf, sizeof(host_buf), 1065 NULL, 0, (nflag ? NI_NUMERICHOST : 0)) != 0) 1066 (void)strlcpy(host_buf, "?", sizeof(host_buf)); 1067 1068 (void)printf("%s if=%s", host_buf, 1069 if_indextoname((unsigned int)p->if_index, ifix_buf)); 1070 (void)printf(", flags=%s%s", 1071 p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1072 p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 1073 rtpref = ((uint32_t)(p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 1074 (void)printf(", pref=%s", rtpref_str[rtpref]); 1075 1076 (void)gettimeofday(&tim, 0); 1077 if (p->expire == 0) 1078 (void)printf(", expire=Never\n"); 1079 else 1080 (void)printf(", expire=%s\n", 1081 sec2str((time_t)(p->expire - tim.tv_sec))); 1082 } 1083 free(buf); 1084 #else 1085 struct in6_drlist dr; 1086 int s, i; 1087 struct timeval time; 1088 1089 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1090 err(1, "socket"); 1091 /* NOTREACHED */ 1092 } 1093 (void)memset(&dr, 0, sizeof(dr)); 1094 (void)strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */ 1095 if (prog_ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 1096 err(1, "ioctl(SIOCGDRLST_IN6)"); 1097 /* NOTREACHED */ 1098 } 1099 #define DR dr.defrouter[i] 1100 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) { 1101 struct sockaddr_in6 sin6; 1102 1103 (void)memset(&sin6, 0, sizeof(sin6)); 1104 sin6.sin6_family = AF_INET6; 1105 sin6.sin6_len = sizeof(sin6); 1106 sin6.sin6_addr = DR.rtaddr; 1107 (void)getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 1108 host_buf, sizeof(host_buf), NULL, 0, 1109 (nflag ? NI_NUMERICHOST : 0)); 1110 1111 (void)printf("%s if=%s", host_buf, 1112 if_indextoname(DR.if_index, ifix_buf)); 1113 (void)printf(", flags=%s%s", 1114 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "", 1115 DR.flags & ND_RA_FLAG_OTHER ? "O" : ""); 1116 gettimeofday(&time, 0); 1117 if (DR.expire == 0) 1118 (void)printf(", expire=Never\n"); 1119 else 1120 (void)printf(", expire=%s\n", 1121 sec2str(DR.expire - time.tv_sec)); 1122 } 1123 #undef DR 1124 (void)prog_close(s); 1125 #endif 1126 } 1127 1128 static void 1129 plist(void) 1130 { 1131 #ifdef ICMPV6CTL_ND6_PRLIST 1132 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 1133 char *buf, *p, *ep; 1134 struct in6_prefix pfx; 1135 size_t l; 1136 struct timeval tim; 1137 const int niflags = NI_NUMERICHOST; 1138 int ninflags = nflag ? NI_NUMERICHOST : 0; 1139 char namebuf[NI_MAXHOST]; 1140 1141 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1142 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1143 /*NOTREACHED*/ 1144 } 1145 buf = malloc(l); 1146 if (!buf) { 1147 err(1, "malloc"); 1148 /*NOTREACHED*/ 1149 } 1150 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1151 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1152 /*NOTREACHED*/ 1153 } 1154 1155 ep = buf + l; 1156 for (p = buf; p < ep; ) { 1157 memcpy(&pfx, p, sizeof(pfx)); 1158 p += sizeof(pfx); 1159 1160 if (getnameinfo((struct sockaddr*)&pfx.prefix, 1161 (socklen_t)pfx.prefix.sin6_len, namebuf, sizeof(namebuf), 1162 NULL, 0, niflags) != 0) 1163 (void)strlcpy(namebuf, "?", sizeof(namebuf)); 1164 (void)printf("%s/%d if=%s\n", namebuf, pfx.prefixlen, 1165 if_indextoname((unsigned int)pfx.if_index, ifix_buf)); 1166 1167 (void)gettimeofday(&tim, 0); 1168 /* 1169 * meaning of fields, especially flags, is very different 1170 * by origin. notify the difference to the users. 1171 */ 1172 (void)printf("flags=%s%s%s%s%s", 1173 pfx.raflags.onlink ? "L" : "", 1174 pfx.raflags.autonomous ? "A" : "", 1175 (pfx.flags & NDPRF_ONLINK) != 0 ? "O" : "", 1176 (pfx.flags & NDPRF_DETACHED) != 0 ? "D" : "", 1177 #ifdef NDPRF_HOME 1178 (pfx.flags & NDPRF_HOME) != 0 ? "H" : "" 1179 #else 1180 "" 1181 #endif 1182 ); 1183 if (pfx.vltime == ND6_INFINITE_LIFETIME) 1184 (void)printf(" vltime=infinity"); 1185 else 1186 (void)printf(" vltime=%lu", (unsigned long)pfx.vltime); 1187 if (pfx.pltime == ND6_INFINITE_LIFETIME) 1188 (void)printf(", pltime=infinity"); 1189 else 1190 (void)printf(", pltime=%lu", (unsigned long)pfx.pltime); 1191 if (pfx.expire == 0) 1192 (void)printf(", expire=Never"); 1193 else if (pfx.expire >= tim.tv_sec) 1194 (void)printf(", expire=%s", 1195 sec2str(pfx.expire - tim.tv_sec)); 1196 else 1197 (void)printf(", expired"); 1198 (void)printf(", ref=%d", pfx.refcnt); 1199 (void)printf("\n"); 1200 /* 1201 * "advertising router" list is meaningful only if the prefix 1202 * information is from RA. 1203 */ 1204 if (pfx.advrtrs) { 1205 int j; 1206 struct sockaddr_in6 sin6; 1207 1208 (void)printf(" advertised by\n"); 1209 for (j = 0; j < pfx.advrtrs && p <= ep; j++) { 1210 struct in6_nbrinfo *nbi; 1211 1212 memcpy(&sin6, p, sizeof(sin6)); 1213 p += sizeof(sin6); 1214 1215 if (getnameinfo((struct sockaddr *)&sin6, 1216 (socklen_t)sin6.sin6_len, namebuf, 1217 sizeof(namebuf), NULL, 0, ninflags) != 0) 1218 (void)strlcpy(namebuf, "?", sizeof(namebuf)); 1219 (void)printf(" %s", namebuf); 1220 1221 nbi = getnbrinfo(&sin6.sin6_addr, 1222 (unsigned int)pfx.if_index, 0); 1223 if (nbi) { 1224 switch (nbi->state) { 1225 case ND6_LLINFO_REACHABLE: 1226 case ND6_LLINFO_STALE: 1227 case ND6_LLINFO_DELAY: 1228 case ND6_LLINFO_PROBE: 1229 (void)printf(" (reachable)\n"); 1230 break; 1231 default: 1232 (void)printf(" (unreachable)\n"); 1233 } 1234 } else 1235 (void)printf(" (no neighbor state)\n"); 1236 } 1237 } else 1238 (void)printf(" No advertising router\n"); 1239 } 1240 free(buf); 1241 #else 1242 struct in6_prlist pr; 1243 int s, i; 1244 struct timeval time; 1245 1246 (void)gettimeofday(&time, 0); 1247 1248 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1249 err(1, "socket"); 1250 /* NOTREACHED */ 1251 } 1252 (void)memset(&pr, 0, sizeof(pr)); 1253 (void)strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */ 1254 if (prog_ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) { 1255 err(1, "ioctl(SIOCGPRLST_IN6)"); 1256 /* NOTREACHED */ 1257 } 1258 #define PR pr.prefix[i] 1259 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) { 1260 struct sockaddr_in6 p6; 1261 char namebuf[NI_MAXHOST]; 1262 int niflags; 1263 1264 #ifdef NDPRF_ONLINK 1265 p6 = PR.prefix; 1266 #else 1267 (void)memset(&p6, 0, sizeof(p6)); 1268 p6.sin6_family = AF_INET6; 1269 p6.sin6_len = sizeof(p6); 1270 p6.sin6_addr = PR.prefix; 1271 #endif 1272 1273 niflags = NI_NUMERICHOST; 1274 if (getnameinfo((struct sockaddr *)&p6, 1275 sizeof(p6), namebuf, sizeof(namebuf), 1276 NULL, 0, niflags)) { 1277 warnx("getnameinfo failed"); 1278 continue; 1279 } 1280 (void)printf("%s/%d if=%s\n", namebuf, PR.prefixlen, 1281 if_indextoname(PR.if_index, ifix_buf)); 1282 1283 (void)gettimeofday(&time, 0); 1284 /* 1285 * meaning of fields, especially flags, is very different 1286 * by origin. notify the difference to the users. 1287 */ 1288 #if 0 1289 (void)printf(" %s", 1290 PR.origin == PR_ORIG_RA ? "" : "advertise: "); 1291 #endif 1292 #ifdef NDPRF_ONLINK 1293 (void)printf("flags=%s%s%s%s%s", 1294 PR.raflags.onlink ? "L" : "", 1295 PR.raflags.autonomous ? "A" : "", 1296 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "", 1297 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "", 1298 #ifdef NDPRF_HOME 1299 (PR.flags & NDPRF_HOME) != 0 ? "H" : "" 1300 #else 1301 "" 1302 #endif 1303 ); 1304 #else 1305 (void)printf("flags=%s%s", 1306 PR.raflags.onlink ? "L" : "", 1307 PR.raflags.autonomous ? "A" : ""); 1308 #endif 1309 if (PR.vltime == ND6_INFINITE_LIFETIME) 1310 (void)printf(" vltime=infinity"); 1311 else 1312 (void)printf(" vltime=%lu", PR.vltime); 1313 if (PR.pltime == ND6_INFINITE_LIFETIME) 1314 (void)printf(", pltime=infinity"); 1315 else 1316 (void)printf(", pltime=%lu", PR.pltime); 1317 if (PR.expire == 0) 1318 (void)printf(", expire=Never"); 1319 else if (PR.expire >= time.tv_sec) 1320 (void)printf(", expire=%s", 1321 sec2str(PR.expire - time.tv_sec)); 1322 else 1323 (void)printf(", expired"); 1324 #ifdef NDPRF_ONLINK 1325 (void)printf(", ref=%d", PR.refcnt); 1326 #endif 1327 #if 0 1328 switch (PR.origin) { 1329 case PR_ORIG_RA: 1330 (void)printf(", origin=RA"); 1331 break; 1332 case PR_ORIG_RR: 1333 (void)printf(", origin=RR"); 1334 break; 1335 case PR_ORIG_STATIC: 1336 (void)printf(", origin=static"); 1337 break; 1338 case PR_ORIG_KERNEL: 1339 (void)printf(", origin=kernel"); 1340 break; 1341 default: 1342 (void)printf(", origin=?"); 1343 break; 1344 } 1345 #endif 1346 (void)printf("\n"); 1347 /* 1348 * "advertising router" list is meaningful only if the prefix 1349 * information is from RA. 1350 */ 1351 if (0 && /* prefix origin is almost obsolted */ 1352 PR.origin != PR_ORIG_RA) 1353 ; 1354 else if (PR.advrtrs) { 1355 int j; 1356 (void)printf(" advertised by\n"); 1357 for (j = 0; j < PR.advrtrs; j++) { 1358 struct sockaddr_in6 sin6; 1359 struct in6_nbrinfo *nbi; 1360 1361 bzero(&sin6, sizeof(sin6)); 1362 sin6.sin6_family = AF_INET6; 1363 sin6.sin6_len = sizeof(sin6); 1364 sin6.sin6_addr = PR.advrtr[j]; 1365 sin6.sin6_scope_id = PR.if_index; /* XXX */ 1366 (void)getnameinfo((struct sockaddr *)&sin6, 1367 sin6.sin6_len, host_buf, 1368 sizeof(host_buf), NULL, 0, 1369 (nflag ? NI_NUMERICHOST : 0)); 1370 (void)printf(" %s", host_buf); 1371 1372 nbi = getnbrinfo(&sin6.sin6_addr, 1373 PR.if_index, 0); 1374 if (nbi) { 1375 switch (nbi->state) { 1376 case ND6_LLINFO_REACHABLE: 1377 case ND6_LLINFO_STALE: 1378 case ND6_LLINFO_DELAY: 1379 case ND6_LLINFO_PROBE: 1380 (void)printf(" (reachable)\n"); 1381 break; 1382 default: 1383 (void)printf(" (unreachable)\n"); 1384 } 1385 } else 1386 (void)printf(" (no neighbor state)\n"); 1387 } 1388 if (PR.advrtrs > DRLSTSIZ) 1389 (void)printf(" and %d routers\n", 1390 PR.advrtrs - DRLSTSIZ); 1391 } else 1392 (void)printf(" No advertising router\n"); 1393 } 1394 #undef PR 1395 (void)prog_close(s); 1396 #endif 1397 } 1398 1399 static void 1400 pfx_flush(void) 1401 { 1402 char dummyif[IFNAMSIZ+8]; 1403 int s; 1404 1405 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1406 err(1, "socket"); 1407 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1408 if (prog_ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1409 err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 1410 (void)prog_close(s); 1411 } 1412 1413 static void 1414 rtr_flush(void) 1415 { 1416 char dummyif[IFNAMSIZ+8]; 1417 int s; 1418 1419 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1420 err(1, "socket"); 1421 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1422 if (prog_ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1423 err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 1424 1425 (void)prog_close(s); 1426 } 1427 1428 static void 1429 harmonize_rtr(void) 1430 { 1431 char dummyif[IFNAMSIZ+8]; 1432 int s; 1433 1434 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1435 err(1, "socket"); 1436 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1437 if (prog_ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1438 err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 1439 1440 (void)prog_close(s); 1441 } 1442 1443 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 1444 static void 1445 setdefif(char *ifname) 1446 { 1447 struct in6_ndifreq ndifreq; 1448 unsigned int ifindex; 1449 int s; 1450 1451 if (strcasecmp(ifname, "delete") == 0) 1452 ifindex = 0; 1453 else { 1454 if ((ifindex = if_nametoindex(ifname)) == 0) 1455 err(1, "failed to resolve i/f index for %s", ifname); 1456 } 1457 1458 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1459 err(1, "socket"); 1460 1461 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1462 ndifreq.ifindex = ifindex; 1463 1464 if (prog_ioctl(s, SIOCSDEFIFACE_IN6, &ndifreq) < 0) 1465 err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 1466 1467 (void)prog_close(s); 1468 } 1469 1470 static void 1471 getdefif(void) 1472 { 1473 struct in6_ndifreq ndifreq; 1474 char ifname[IFNAMSIZ+8]; 1475 int s; 1476 1477 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1478 err(1, "socket"); 1479 1480 (void)memset(&ndifreq, 0, sizeof(ndifreq)); 1481 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1482 1483 if (prog_ioctl(s, SIOCGDEFIFACE_IN6, &ndifreq) < 0) 1484 err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 1485 1486 if (ndifreq.ifindex == 0) 1487 (void)printf("No default interface.\n"); 1488 else { 1489 if ((if_indextoname((unsigned int)ndifreq.ifindex, ifname)) == NULL) 1490 err(1, "failed to resolve ifname for index %lu", 1491 ndifreq.ifindex); 1492 (void)printf("ND default interface = %s\n", ifname); 1493 } 1494 1495 (void)prog_close(s); 1496 } 1497 #endif 1498 1499 static const char * 1500 sec2str(time_t total) 1501 { 1502 static char result[256]; 1503 int days, hours, mins, secs; 1504 int first = 1; 1505 char *p = result; 1506 char *ep = &result[sizeof(result)]; 1507 int n; 1508 1509 days = total / 3600 / 24; 1510 hours = (total / 3600) % 24; 1511 mins = (total / 60) % 60; 1512 secs = total % 60; 1513 1514 if (days) { 1515 first = 0; 1516 n = snprintf(p, (size_t)(ep - p), "%dd", days); 1517 if (n < 0 || n >= ep - p) 1518 return "?"; 1519 p += n; 1520 } 1521 if (!first || hours) { 1522 first = 0; 1523 n = snprintf(p, (size_t)(ep - p), "%dh", hours); 1524 if (n < 0 || n >= ep - p) 1525 return "?"; 1526 p += n; 1527 } 1528 if (!first || mins) { 1529 first = 0; 1530 n = snprintf(p, (size_t)(ep - p), "%dm", mins); 1531 if (n < 0 || n >= ep - p) 1532 return "?"; 1533 p += n; 1534 } 1535 (void)snprintf(p, (size_t)(ep - p), "%ds", secs); 1536 1537 return(result); 1538 } 1539 1540 /* 1541 * Print the timestamp 1542 * from tcpdump/util.c 1543 */ 1544 static void 1545 ts_print(const struct timeval *tvp) 1546 { 1547 int s; 1548 1549 /* Default */ 1550 s = (tvp->tv_sec + thiszone) % 86400; 1551 (void)printf("%02d:%02d:%02d.%06u ", 1552 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 1553 } 1554