1 /* $NetBSD: ndp.c,v 1.48 2016/09/19 19:13:14 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_GATEWAY)) { 398 switch (sdl->sdl_type) { 399 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 400 case IFT_ISO88024: case IFT_ISO88025: 401 goto overwrite; 402 } 403 } 404 /* 405 * IPv4 arp command retries with sin_other = SIN_PROXY here. 406 */ 407 (void)fprintf(stderr, "set: cannot configure a new entry\n"); 408 return 1; 409 } 410 411 overwrite: 412 if (sdl->sdl_family != AF_LINK) { 413 warnx("cannot intuit interface index and type for %s", host); 414 return (1); 415 } 416 sdl_m.sdl_type = sdl->sdl_type; 417 sdl_m.sdl_index = sdl->sdl_index; 418 return (rtmsg(RTM_ADD)); 419 } 420 421 /* 422 * Display an individual neighbor cache entry 423 */ 424 static void 425 get(char *host) 426 { 427 struct sockaddr_in6 *mysin = &sin_m; 428 struct addrinfo hints, *res; 429 int gai_error; 430 431 sin_m = blank_sin; 432 (void)memset(&hints, 0, sizeof(hints)); 433 hints.ai_family = AF_INET6; 434 gai_error = getaddrinfo(host, NULL, &hints, &res); 435 if (gai_error) { 436 warnx("%s: %s", host, gai_strerror(gai_error)); 437 return; 438 } 439 makeaddr(mysin, res->ai_addr); 440 dump(&mysin->sin6_addr, 0); 441 if (found_entry == 0) { 442 (void)getnameinfo((struct sockaddr *)(void *)mysin, 443 (socklen_t)mysin->sin6_len, 444 host_buf, sizeof(host_buf), NULL ,0, 445 (nflag ? NI_NUMERICHOST : 0)); 446 errx(1, "%s (%s) -- no entry", host, host_buf); 447 } 448 } 449 450 /* 451 * Delete a neighbor cache entry 452 */ 453 static int 454 delete(char *host) 455 { 456 struct sockaddr_in6 *mysin = &sin_m; 457 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 458 struct sockaddr_dl *sdl; 459 struct addrinfo hints, *res; 460 int gai_error; 461 462 getsocket(); 463 sin_m = blank_sin; 464 465 bzero(&hints, sizeof(hints)); 466 hints.ai_family = AF_INET6; 467 gai_error = getaddrinfo(host, NULL, &hints, &res); 468 if (gai_error) { 469 warnx("%s: %s", host, gai_strerror(gai_error)); 470 return 1; 471 } 472 makeaddr(mysin, res->ai_addr); 473 if (rtmsg(RTM_GET) < 0) 474 errx(1, "RTM_GET(%s) failed", host); 475 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1); 476 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(mysin->sin6_len) + 477 (char *)(void *)mysin); 478 if (IN6_ARE_ADDR_EQUAL(&mysin->sin6_addr, &sin_m.sin6_addr)) { 479 if (sdl->sdl_family == AF_LINK && 480 !(rtm->rtm_flags & RTF_GATEWAY)) { 481 goto delete; 482 } 483 /* 484 * IPv4 arp command retries with sin_other = SIN_PROXY here. 485 */ 486 warnx("delete: cannot delete non-NDP entry"); 487 return 1; 488 } 489 490 delete: 491 if (sdl->sdl_family != AF_LINK) { 492 (void)printf("cannot locate %s\n", host); 493 return (1); 494 } 495 if (rtmsg(RTM_DELETE) == 0) { 496 struct sockaddr_in6 s6 = *mysin; /* XXX: for safety */ 497 498 mysin->sin6_scope_id = 0; 499 inet6_putscopeid(mysin, INET6_IS_ADDR_LINKLOCAL); 500 (void)getnameinfo((struct sockaddr *)(void *)&s6, 501 (socklen_t)s6.sin6_len, host_buf, 502 sizeof(host_buf), NULL, 0, 503 (nflag ? NI_NUMERICHOST : 0)); 504 (void)printf("%s (%s) deleted\n", host, host_buf); 505 } 506 507 return 0; 508 } 509 510 #define W_ADDR (8 * 4 + 7) 511 #define W_LL 17 512 #define W_IF 6 513 514 /* 515 * Dump the entire neighbor cache 516 */ 517 static void 518 dump(struct in6_addr *addr, int cflag) 519 { 520 int mib[6]; 521 size_t needed; 522 char *lim, *buf, *next; 523 struct rt_msghdr *rtm; 524 struct sockaddr_in6 *mysin; 525 struct sockaddr_dl *sdl; 526 struct in6_nbrinfo *nbi; 527 struct timeval tim; 528 int addrwidth; 529 int llwidth; 530 int ifwidth; 531 char flgbuf[8], *fl; 532 const char *ifname; 533 534 /* Print header */ 535 if (!tflag && !cflag) 536 (void)printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %2s\n", 537 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 538 W_IF, W_IF, "Netif", "Expire", "S", "Fl"); 539 540 again:; 541 mib[0] = CTL_NET; 542 mib[1] = PF_ROUTE; 543 mib[2] = 0; 544 mib[3] = AF_INET6; 545 mib[4] = NET_RT_FLAGS; 546 mib[5] = 0; 547 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 548 err(1, "sysctl(PF_ROUTE estimate)"); 549 if (needed > 0) { 550 if ((buf = malloc(needed)) == NULL) 551 err(1, "malloc"); 552 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 553 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 554 lim = buf + needed; 555 } else 556 buf = lim = NULL; 557 558 for (next = buf; next && next < lim; next += rtm->rtm_msglen) { 559 int isrouter = 0, prbs = 0; 560 561 rtm = (struct rt_msghdr *)(void *)next; 562 mysin = (struct sockaddr_in6 *)(void *)(rtm + 1); 563 sdl = (struct sockaddr_dl *)(void *)((char *)(void *)mysin + RT_ROUNDUP(mysin->sin6_len)); 564 565 /* 566 * Some OSes can produce a route that has the LINK flag but 567 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 568 * and BSD/OS, where xx is not the interface identifier on 569 * lo0). Such routes entry would annoy getnbrinfo() below, 570 * so we skip them. 571 * XXX: such routes should have the GATEWAY flag, not the 572 * LINK flag. However, there is rotten routing software 573 * that advertises all routes that have the GATEWAY flag. 574 * Thus, KAME kernel intentionally does not set the LINK flag. 575 * What is to be fixed is not ndp, but such routing software 576 * (and the kernel workaround)... 577 */ 578 if (sdl->sdl_family != AF_LINK) 579 continue; 580 581 if (!(rtm->rtm_flags & RTF_HOST)) 582 continue; 583 584 if (addr) { 585 if (!IN6_ARE_ADDR_EQUAL(addr, &mysin->sin6_addr)) 586 continue; 587 found_entry = 1; 588 } else if (IN6_IS_ADDR_MULTICAST(&mysin->sin6_addr)) 589 continue; 590 if (IN6_IS_ADDR_LINKLOCAL(&mysin->sin6_addr) || 591 IN6_IS_ADDR_MC_LINKLOCAL(&mysin->sin6_addr)) { 592 uint16_t scopeid = mysin->sin6_scope_id; 593 inet6_getscopeid(mysin, INET6_IS_ADDR_LINKLOCAL| 594 INET6_IS_ADDR_MC_LINKLOCAL); 595 if (scopeid == 0) 596 mysin->sin6_scope_id = sdl->sdl_index; 597 } 598 (void)getnameinfo((struct sockaddr *)(void *)mysin, 599 (socklen_t)mysin->sin6_len, 600 host_buf, sizeof(host_buf), NULL, 0, 601 (nflag ? NI_NUMERICHOST : 0)); 602 if (cflag) { 603 if ((rtm->rtm_flags & RTF_STATIC) == 0) 604 (void)delete(host_buf); 605 continue; 606 } 607 (void)gettimeofday(&tim, 0); 608 if (tflag) 609 ts_print(&tim); 610 611 addrwidth = strlen(host_buf); 612 if (addrwidth < W_ADDR) 613 addrwidth = W_ADDR; 614 llwidth = strlen(ether_str(sdl)); 615 if (W_ADDR + W_LL - addrwidth > llwidth) 616 llwidth = W_ADDR + W_LL - addrwidth; 617 ifname = if_indextoname((unsigned int)sdl->sdl_index, ifix_buf); 618 if (!ifname) 619 ifname = "?"; 620 ifwidth = strlen(ifname); 621 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 622 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 623 624 (void)printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, 625 host_buf, llwidth, llwidth, ether_str(sdl), ifwidth, 626 ifwidth, ifname); 627 628 /* Print neighbor discovery specific informations */ 629 nbi = getnbrinfo(&mysin->sin6_addr, 630 (unsigned int)sdl->sdl_index, 1); 631 if (nbi) { 632 if (nbi->expire > tim.tv_sec) { 633 (void)printf(" %-9.9s", 634 sec2str(nbi->expire - tim.tv_sec)); 635 } else if (nbi->expire == 0) 636 (void)printf(" %-9.9s", "permanent"); 637 else 638 (void)printf(" %-9.9s", "expired"); 639 640 switch (nbi->state) { 641 case ND6_LLINFO_NOSTATE: 642 (void)printf(" N"); 643 break; 644 #ifdef ND6_LLINFO_WAITDELETE 645 case ND6_LLINFO_WAITDELETE: 646 (void)printf(" W"); 647 break; 648 #endif 649 case ND6_LLINFO_INCOMPLETE: 650 (void)printf(" I"); 651 break; 652 case ND6_LLINFO_REACHABLE: 653 (void)printf(" R"); 654 break; 655 case ND6_LLINFO_STALE: 656 (void)printf(" S"); 657 break; 658 case ND6_LLINFO_DELAY: 659 (void)printf(" D"); 660 break; 661 case ND6_LLINFO_PROBE: 662 (void)printf(" P"); 663 break; 664 default: 665 (void)printf(" ?"); 666 break; 667 } 668 669 isrouter = nbi->isrouter; 670 prbs = nbi->asked; 671 } else { 672 warnx("failed to get neighbor information"); 673 (void)printf(" "); 674 } 675 676 /* 677 * other flags. R: router, P: proxy, W: ?? 678 */ 679 fl = flgbuf; 680 if (isrouter) 681 *fl++ = 'R'; 682 if (rtm->rtm_flags & RTF_ANNOUNCE) 683 *fl++ = 'p'; 684 *fl++ = '\0'; 685 (void)printf(" %s", flgbuf); 686 687 if (prbs) 688 (void)printf(" %d", prbs); 689 690 (void)printf("\n"); 691 } 692 if (buf != NULL) 693 free(buf); 694 695 if (repeat) { 696 (void)printf("\n"); 697 (void)fflush(stdout); 698 (void)sleep(repeat); 699 goto again; 700 } 701 } 702 703 static struct in6_nbrinfo * 704 getnbrinfo(struct in6_addr *addr, unsigned int ifindex, int warning) 705 { 706 static struct in6_nbrinfo nbi; 707 int s; 708 709 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 710 err(1, "socket"); 711 712 (void)memset(&nbi, 0, sizeof(nbi)); 713 (void)if_indextoname(ifindex, nbi.ifname); 714 nbi.addr = *addr; 715 if (prog_ioctl(s, SIOCGNBRINFO_IN6, &nbi) < 0) { 716 if (warning) 717 warn("ioctl(SIOCGNBRINFO_IN6)"); 718 (void)prog_close(s); 719 return(NULL); 720 } 721 722 (void)prog_close(s); 723 return(&nbi); 724 } 725 726 static char * 727 ether_str(struct sockaddr_dl *sdl) 728 { 729 static char hbuf[NI_MAXHOST]; 730 731 if (sdl->sdl_alen) { 732 if (getnameinfo((struct sockaddr *)(void *)sdl, 733 (socklen_t)sdl->sdl_len, 734 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 735 (void)snprintf(hbuf, sizeof(hbuf), "<invalid>"); 736 } else 737 (void)snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 738 739 return(hbuf); 740 } 741 742 static int 743 ndp_ether_aton(char *a, u_char *n) 744 { 745 int i, o[6]; 746 747 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 748 &o[3], &o[4], &o[5]); 749 if (i != 6) { 750 warnx("invalid Ethernet address '%s'", a); 751 return (1); 752 } 753 for (i = 0; i < 6; i++) 754 n[i] = o[i]; 755 return (0); 756 } 757 758 static void 759 usage(void) 760 { 761 const char *pn = getprogname(); 762 763 (void)fprintf(stderr, "Usage: %s [-nt] hostname\n", pn); 764 (void)fprintf(stderr, 765 " %s [-nt] -a | -c | -p | -r | -H | -P | -R\n", pn); 766 (void)fprintf(stderr, " %s [-nt] -A wait\n", pn); 767 (void)fprintf(stderr, " %s [-nt] -d hostname\n", pn); 768 (void)fprintf(stderr, " %s [-nt] -f filename\n", pn); 769 (void)fprintf(stderr, " %s [-nt] -i interface [flags...]\n", pn); 770 #ifdef SIOCSDEFIFACE_IN6 771 (void)fprintf(stderr, " %s [-nt] -I [interface|delete]\n", pn); 772 #endif 773 (void)fprintf(stderr, 774 " %s [-nt] -s nodename etheraddr [temp] [proxy]\n", pn); 775 exit(1); 776 } 777 778 static int 779 rtmsg(int cmd) 780 { 781 static int seq; 782 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 783 register char *cp = m_rtmsg.m_space; 784 register int l; 785 786 errno = 0; 787 if (cmd == RTM_DELETE) { 788 rtm->rtm_flags |= RTF_LLDATA; 789 goto doit; 790 } 791 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg)); 792 rtm->rtm_flags = flags; 793 rtm->rtm_version = RTM_VERSION; 794 795 switch (cmd) { 796 default: 797 errx(1, "internal wrong cmd"); 798 /*NOTREACHED*/ 799 case RTM_ADD: 800 rtm->rtm_addrs |= RTA_GATEWAY; 801 if (expire_time) { 802 rtm->rtm_rmx.rmx_expire = expire_time; 803 rtm->rtm_inits = RTV_EXPIRE; 804 } 805 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 806 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */ 807 if (rtm->rtm_flags & RTF_ANNOUNCE) { 808 rtm->rtm_flags &= ~RTF_HOST; 809 rtm->rtm_addrs |= RTA_NETMASK; 810 } 811 #endif 812 rtm->rtm_addrs |= RTA_DST; 813 break; 814 case RTM_GET: 815 rtm->rtm_flags |= RTF_LLDATA; 816 rtm->rtm_addrs |= RTA_DST | RTA_IFP; 817 } 818 #define NEXTADDR(w, s) \ 819 if (rtm->rtm_addrs & (w)) { \ 820 (void)memcpy(cp, &s, sizeof(s)); \ 821 RT_ADVANCE(cp, (struct sockaddr *)(void *)&s); \ 822 } 823 824 NEXTADDR(RTA_DST, sin_m); 825 NEXTADDR(RTA_GATEWAY, sdl_m); 826 #ifdef notdef /* we don't support ipv6addr/128 type proxying. */ 827 (void)memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 828 NEXTADDR(RTA_NETMASK, so_mask); 829 #endif 830 831 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg; 832 doit: 833 l = rtm->rtm_msglen; 834 rtm->rtm_seq = ++seq; 835 rtm->rtm_type = cmd; 836 if (prog_write(my_s, &m_rtmsg, (size_t)l) == -1) { 837 if (errno != ESRCH || cmd != RTM_DELETE) 838 err(1, "writing to routing socket"); 839 } 840 do { 841 l = prog_read(my_s, &m_rtmsg, sizeof(m_rtmsg)); 842 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 843 if (l < 0) 844 warn("read from routing socket"); 845 return (0); 846 } 847 848 static void 849 ifinfo(char *ifname, int argc, char **argv) 850 { 851 struct in6_ndireq nd; 852 int i, s; 853 u_int32_t newflags; 854 #ifdef IPV6CTL_USETEMPADDR 855 u_int8_t nullbuf[8]; 856 #endif 857 858 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 859 err(1, "socket"); 860 (void)memset(&nd, 0, sizeof(nd)); 861 (void)strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 862 if (prog_ioctl(s, SIOCGIFINFO_IN6, &nd) < 0) 863 err(1, "ioctl(SIOCGIFINFO_IN6)"); 864 #define ND nd.ndi 865 newflags = ND.flags; 866 for (i = 0; i < argc; i++) { 867 int clear = 0; 868 char *cp = argv[i]; 869 870 if (*cp == '-') { 871 clear = 1; 872 cp++; 873 } 874 875 #define SETFLAG(s, f) \ 876 do {\ 877 if (strcmp(cp, (s)) == 0) {\ 878 if (clear)\ 879 newflags &= ~(f);\ 880 else\ 881 newflags |= (f);\ 882 }\ 883 } while (/*CONSTCOND*/0) 884 /* 885 * XXX: this macro is not 100% correct, in that it matches "nud" against 886 * "nudbogus". But we just let it go since this is minor. 887 */ 888 #define SETVALUE(f, v) \ 889 do { \ 890 char *valptr; \ 891 unsigned long newval; \ 892 v = 0; /* unspecified */ \ 893 if (strncmp(cp, f, strlen(f)) == 0) { \ 894 valptr = strchr(cp, '='); \ 895 if (valptr == NULL) \ 896 err(1, "syntax error in %s field", (f)); \ 897 errno = 0; \ 898 newval = strtoul(++valptr, NULL, 0); \ 899 if (errno) \ 900 err(1, "syntax error in %s's value", (f)); \ 901 v = newval; \ 902 } \ 903 } while (/*CONSTCOND*/0) 904 905 #ifdef ND6_IFF_IFDISABLED 906 SETFLAG("disabled", ND6_IFF_IFDISABLED); 907 #endif 908 SETFLAG("nud", ND6_IFF_PERFORMNUD); 909 #ifdef ND6_IFF_ACCEPT_RTADV 910 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 911 #endif 912 #ifdef ND6_IFF_OVERRIDE_RTADV 913 SETFLAG("override_rtadv", ND6_IFF_OVERRIDE_RTADV); 914 #endif 915 #ifdef ND6_IFF_AUTO_LINKLOCAL 916 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL); 917 #endif 918 #ifdef ND6_IFF_PREFER_SOURCE 919 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE); 920 #endif 921 #ifdef ND6_IFF_DONT_SET_IFROUTE 922 SETFLAG("dont_set_ifroute", ND6_IFF_DONT_SET_IFROUTE); 923 #endif 924 SETVALUE("basereachable", ND.basereachable); 925 SETVALUE("retrans", ND.retrans); 926 SETVALUE("curhlim", ND.chlim); 927 928 ND.flags = newflags; 929 #ifdef SIOCSIFINFO_IN6 930 if (prog_ioctl(s, SIOCSIFINFO_IN6, &nd) < 0) 931 err(1, "ioctl(SIOCSIFINFO_IN6)"); 932 #else 933 if (prog_ioctl(s, SIOCSIFINFO_FLAGS, &nd) < 0) 934 err(1, "ioctl(SIOCSIFINFO_FLAGS)"); 935 #endif 936 #undef SETFLAG 937 #undef SETVALUE 938 } 939 940 if (!ND.initialized) 941 errx(1, "%s: not initialized yet", ifname); 942 943 if (prog_ioctl(s, SIOCGIFINFO_IN6, &nd) < 0) 944 err(1, "ioctl(SIOCGIFINFO_IN6)"); 945 (void)printf("linkmtu=%d", ND.linkmtu); 946 (void)printf(", maxmtu=%d", ND.maxmtu); 947 (void)printf(", curhlim=%d", ND.chlim); 948 (void)printf(", basereachable=%ds%dms", 949 ND.basereachable / 1000, ND.basereachable % 1000); 950 (void)printf(", reachable=%ds", ND.reachable); 951 (void)printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 952 #ifdef IPV6CTL_USETEMPADDR 953 (void)memset(nullbuf, 0, sizeof(nullbuf)); 954 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 955 int j; 956 u_int8_t *rbuf; 957 958 for (i = 0; i < 3; i++) { 959 switch (i) { 960 case 0: 961 (void)printf("\nRandom seed(0): "); 962 rbuf = ND.randomseed0; 963 break; 964 case 1: 965 (void)printf("\nRandom seed(1): "); 966 rbuf = ND.randomseed1; 967 break; 968 case 2: 969 (void)printf("\nRandom ID: "); 970 rbuf = ND.randomid; 971 break; 972 default: 973 errx(1, "impossible case for tempaddr display"); 974 } 975 for (j = 0; j < 8; j++) 976 (void)printf("%02x", rbuf[j]); 977 } 978 } 979 #endif 980 if (ND.flags) { 981 (void)printf("\nFlags: "); 982 if ((ND.flags & ND6_IFF_PERFORMNUD)) 983 (void)printf("nud "); 984 #ifdef ND6_IFF_IFDISABLED 985 if ((ND.flags & ND6_IFF_IFDISABLED)) 986 (void)printf("disabled "); 987 #endif 988 #ifdef ND6_IFF_ACCEPT_RTADV 989 if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 990 (void)printf("accept_rtadv "); 991 #endif 992 #ifdef ND6_IFF_OVERRIDE_RTADV 993 if ((ND.flags & ND6_IFF_OVERRIDE_RTADV)) 994 (void)printf("override_rtadv "); 995 #endif 996 #ifdef ND6_IFF_AUTO_LINKLOCAL 997 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL)) 998 (void)printf("auto_linklocal "); 999 #endif 1000 #ifdef ND6_IFF_PREFER_SOURCE 1001 if ((ND.flags & ND6_IFF_PREFER_SOURCE)) 1002 (void)printf("prefer_source "); 1003 #endif 1004 } 1005 (void)putc('\n', stdout); 1006 #undef ND 1007 1008 (void)prog_close(s); 1009 } 1010 1011 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 1012 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 1013 #endif 1014 1015 static void 1016 rtrlist(void) 1017 { 1018 #ifdef ICMPV6CTL_ND6_DRLIST 1019 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 1020 char *buf; 1021 struct in6_defrouter *p, *ep; 1022 size_t l; 1023 struct timeval tim; 1024 1025 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1026 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1027 /*NOTREACHED*/ 1028 } 1029 if (l == 0) 1030 return; 1031 buf = malloc(l); 1032 if (!buf) { 1033 err(1, "malloc"); 1034 /*NOTREACHED*/ 1035 } 1036 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1037 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1038 /*NOTREACHED*/ 1039 } 1040 1041 ep = (struct in6_defrouter *)(void *)(buf + l); 1042 for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) { 1043 int rtpref; 1044 1045 if (getnameinfo((struct sockaddr *)(void *)&p->rtaddr, 1046 (socklen_t)p->rtaddr.sin6_len, host_buf, sizeof(host_buf), 1047 NULL, 0, (nflag ? NI_NUMERICHOST : 0)) != 0) 1048 (void)strlcpy(host_buf, "?", sizeof(host_buf)); 1049 1050 (void)printf("%s if=%s", host_buf, 1051 if_indextoname((unsigned int)p->if_index, ifix_buf)); 1052 (void)printf(", flags=%s%s", 1053 p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1054 p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 1055 rtpref = ((uint32_t)(p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 1056 (void)printf(", pref=%s", rtpref_str[rtpref]); 1057 1058 (void)gettimeofday(&tim, 0); 1059 if (p->expire == 0) 1060 (void)printf(", expire=Never\n"); 1061 else 1062 (void)printf(", expire=%s\n", 1063 sec2str((time_t)(p->expire - tim.tv_sec))); 1064 } 1065 free(buf); 1066 #else 1067 struct in6_drlist dr; 1068 int s, i; 1069 struct timeval time; 1070 1071 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1072 err(1, "socket"); 1073 /* NOTREACHED */ 1074 } 1075 (void)memset(&dr, 0, sizeof(dr)); 1076 (void)strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */ 1077 if (prog_ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 1078 err(1, "ioctl(SIOCGDRLST_IN6)"); 1079 /* NOTREACHED */ 1080 } 1081 #define DR dr.defrouter[i] 1082 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) { 1083 struct sockaddr_in6 sin6; 1084 1085 (void)memset(&sin6, 0, sizeof(sin6)); 1086 sin6.sin6_family = AF_INET6; 1087 sin6.sin6_len = sizeof(sin6); 1088 sin6.sin6_addr = DR.rtaddr; 1089 (void)getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 1090 host_buf, sizeof(host_buf), NULL, 0, 1091 (nflag ? NI_NUMERICHOST : 0)); 1092 1093 (void)printf("%s if=%s", host_buf, 1094 if_indextoname(DR.if_index, ifix_buf)); 1095 (void)printf(", flags=%s%s", 1096 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "", 1097 DR.flags & ND_RA_FLAG_OTHER ? "O" : ""); 1098 gettimeofday(&time, 0); 1099 if (DR.expire == 0) 1100 (void)printf(", expire=Never\n"); 1101 else 1102 (void)printf(", expire=%s\n", 1103 sec2str(DR.expire - time.tv_sec)); 1104 } 1105 #undef DR 1106 (void)prog_close(s); 1107 #endif 1108 } 1109 1110 static void 1111 plist(void) 1112 { 1113 #ifdef ICMPV6CTL_ND6_PRLIST 1114 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 1115 char *buf, *p, *ep; 1116 struct in6_prefix pfx; 1117 size_t l; 1118 struct timeval tim; 1119 const int niflags = NI_NUMERICHOST; 1120 int ninflags = nflag ? NI_NUMERICHOST : 0; 1121 char namebuf[NI_MAXHOST]; 1122 1123 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1124 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1125 /*NOTREACHED*/ 1126 } 1127 buf = malloc(l); 1128 if (!buf) { 1129 err(1, "malloc"); 1130 /*NOTREACHED*/ 1131 } 1132 if (prog_sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1133 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1134 /*NOTREACHED*/ 1135 } 1136 1137 ep = buf + l; 1138 for (p = buf; p < ep; ) { 1139 memcpy(&pfx, p, sizeof(pfx)); 1140 p += sizeof(pfx); 1141 1142 if (getnameinfo((struct sockaddr*)&pfx.prefix, 1143 (socklen_t)pfx.prefix.sin6_len, namebuf, sizeof(namebuf), 1144 NULL, 0, niflags) != 0) 1145 (void)strlcpy(namebuf, "?", sizeof(namebuf)); 1146 (void)printf("%s/%d if=%s\n", namebuf, pfx.prefixlen, 1147 if_indextoname((unsigned int)pfx.if_index, ifix_buf)); 1148 1149 (void)gettimeofday(&tim, 0); 1150 /* 1151 * meaning of fields, especially flags, is very different 1152 * by origin. notify the difference to the users. 1153 */ 1154 (void)printf("flags=%s%s%s%s%s", 1155 pfx.raflags.onlink ? "L" : "", 1156 pfx.raflags.autonomous ? "A" : "", 1157 (pfx.flags & NDPRF_ONLINK) != 0 ? "O" : "", 1158 (pfx.flags & NDPRF_DETACHED) != 0 ? "D" : "", 1159 #ifdef NDPRF_HOME 1160 (pfx.flags & NDPRF_HOME) != 0 ? "H" : "" 1161 #else 1162 "" 1163 #endif 1164 ); 1165 if (pfx.vltime == ND6_INFINITE_LIFETIME) 1166 (void)printf(" vltime=infinity"); 1167 else 1168 (void)printf(" vltime=%lu", (unsigned long)pfx.vltime); 1169 if (pfx.pltime == ND6_INFINITE_LIFETIME) 1170 (void)printf(", pltime=infinity"); 1171 else 1172 (void)printf(", pltime=%lu", (unsigned long)pfx.pltime); 1173 if (pfx.expire == 0) 1174 (void)printf(", expire=Never"); 1175 else if (pfx.expire >= tim.tv_sec) 1176 (void)printf(", expire=%s", 1177 sec2str(pfx.expire - tim.tv_sec)); 1178 else 1179 (void)printf(", expired"); 1180 (void)printf(", ref=%d", pfx.refcnt); 1181 (void)printf("\n"); 1182 /* 1183 * "advertising router" list is meaningful only if the prefix 1184 * information is from RA. 1185 */ 1186 if (pfx.advrtrs) { 1187 int j; 1188 struct sockaddr_in6 sin6; 1189 1190 (void)printf(" advertised by\n"); 1191 for (j = 0; j < pfx.advrtrs && p <= ep; j++) { 1192 struct in6_nbrinfo *nbi; 1193 1194 memcpy(&sin6, p, sizeof(sin6)); 1195 p += sizeof(sin6); 1196 1197 if (getnameinfo((struct sockaddr *)&sin6, 1198 (socklen_t)sin6.sin6_len, namebuf, 1199 sizeof(namebuf), NULL, 0, ninflags) != 0) 1200 (void)strlcpy(namebuf, "?", sizeof(namebuf)); 1201 (void)printf(" %s", namebuf); 1202 1203 nbi = getnbrinfo(&sin6.sin6_addr, 1204 (unsigned int)pfx.if_index, 0); 1205 if (nbi) { 1206 switch (nbi->state) { 1207 case ND6_LLINFO_REACHABLE: 1208 case ND6_LLINFO_STALE: 1209 case ND6_LLINFO_DELAY: 1210 case ND6_LLINFO_PROBE: 1211 (void)printf(" (reachable)\n"); 1212 break; 1213 default: 1214 (void)printf(" (unreachable)\n"); 1215 } 1216 } else 1217 (void)printf(" (no neighbor state)\n"); 1218 } 1219 } else 1220 (void)printf(" No advertising router\n"); 1221 } 1222 free(buf); 1223 #else 1224 struct in6_prlist pr; 1225 int s, i; 1226 struct timeval time; 1227 1228 (void)gettimeofday(&time, 0); 1229 1230 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1231 err(1, "socket"); 1232 /* NOTREACHED */ 1233 } 1234 (void)memset(&pr, 0, sizeof(pr)); 1235 (void)strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */ 1236 if (prog_ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) { 1237 err(1, "ioctl(SIOCGPRLST_IN6)"); 1238 /* NOTREACHED */ 1239 } 1240 #define PR pr.prefix[i] 1241 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) { 1242 struct sockaddr_in6 p6; 1243 char namebuf[NI_MAXHOST]; 1244 int niflags; 1245 1246 #ifdef NDPRF_ONLINK 1247 p6 = PR.prefix; 1248 #else 1249 (void)memset(&p6, 0, sizeof(p6)); 1250 p6.sin6_family = AF_INET6; 1251 p6.sin6_len = sizeof(p6); 1252 p6.sin6_addr = PR.prefix; 1253 #endif 1254 1255 niflags = NI_NUMERICHOST; 1256 if (getnameinfo((struct sockaddr *)&p6, 1257 sizeof(p6), namebuf, sizeof(namebuf), 1258 NULL, 0, niflags)) { 1259 warnx("getnameinfo failed"); 1260 continue; 1261 } 1262 (void)printf("%s/%d if=%s\n", namebuf, PR.prefixlen, 1263 if_indextoname(PR.if_index, ifix_buf)); 1264 1265 (void)gettimeofday(&time, 0); 1266 /* 1267 * meaning of fields, especially flags, is very different 1268 * by origin. notify the difference to the users. 1269 */ 1270 #if 0 1271 (void)printf(" %s", 1272 PR.origin == PR_ORIG_RA ? "" : "advertise: "); 1273 #endif 1274 #ifdef NDPRF_ONLINK 1275 (void)printf("flags=%s%s%s%s%s", 1276 PR.raflags.onlink ? "L" : "", 1277 PR.raflags.autonomous ? "A" : "", 1278 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "", 1279 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "", 1280 #ifdef NDPRF_HOME 1281 (PR.flags & NDPRF_HOME) != 0 ? "H" : "" 1282 #else 1283 "" 1284 #endif 1285 ); 1286 #else 1287 (void)printf("flags=%s%s", 1288 PR.raflags.onlink ? "L" : "", 1289 PR.raflags.autonomous ? "A" : ""); 1290 #endif 1291 if (PR.vltime == ND6_INFINITE_LIFETIME) 1292 (void)printf(" vltime=infinity"); 1293 else 1294 (void)printf(" vltime=%lu", PR.vltime); 1295 if (PR.pltime == ND6_INFINITE_LIFETIME) 1296 (void)printf(", pltime=infinity"); 1297 else 1298 (void)printf(", pltime=%lu", PR.pltime); 1299 if (PR.expire == 0) 1300 (void)printf(", expire=Never"); 1301 else if (PR.expire >= time.tv_sec) 1302 (void)printf(", expire=%s", 1303 sec2str(PR.expire - time.tv_sec)); 1304 else 1305 (void)printf(", expired"); 1306 #ifdef NDPRF_ONLINK 1307 (void)printf(", ref=%d", PR.refcnt); 1308 #endif 1309 #if 0 1310 switch (PR.origin) { 1311 case PR_ORIG_RA: 1312 (void)printf(", origin=RA"); 1313 break; 1314 case PR_ORIG_RR: 1315 (void)printf(", origin=RR"); 1316 break; 1317 case PR_ORIG_STATIC: 1318 (void)printf(", origin=static"); 1319 break; 1320 case PR_ORIG_KERNEL: 1321 (void)printf(", origin=kernel"); 1322 break; 1323 default: 1324 (void)printf(", origin=?"); 1325 break; 1326 } 1327 #endif 1328 (void)printf("\n"); 1329 /* 1330 * "advertising router" list is meaningful only if the prefix 1331 * information is from RA. 1332 */ 1333 if (0 && /* prefix origin is almost obsolted */ 1334 PR.origin != PR_ORIG_RA) 1335 ; 1336 else if (PR.advrtrs) { 1337 int j; 1338 (void)printf(" advertised by\n"); 1339 for (j = 0; j < PR.advrtrs; j++) { 1340 struct sockaddr_in6 sin6; 1341 struct in6_nbrinfo *nbi; 1342 1343 bzero(&sin6, sizeof(sin6)); 1344 sin6.sin6_family = AF_INET6; 1345 sin6.sin6_len = sizeof(sin6); 1346 sin6.sin6_addr = PR.advrtr[j]; 1347 sin6.sin6_scope_id = PR.if_index; /* XXX */ 1348 (void)getnameinfo((struct sockaddr *)&sin6, 1349 sin6.sin6_len, host_buf, 1350 sizeof(host_buf), NULL, 0, 1351 (nflag ? NI_NUMERICHOST : 0)); 1352 (void)printf(" %s", host_buf); 1353 1354 nbi = getnbrinfo(&sin6.sin6_addr, 1355 PR.if_index, 0); 1356 if (nbi) { 1357 switch (nbi->state) { 1358 case ND6_LLINFO_REACHABLE: 1359 case ND6_LLINFO_STALE: 1360 case ND6_LLINFO_DELAY: 1361 case ND6_LLINFO_PROBE: 1362 (void)printf(" (reachable)\n"); 1363 break; 1364 default: 1365 (void)printf(" (unreachable)\n"); 1366 } 1367 } else 1368 (void)printf(" (no neighbor state)\n"); 1369 } 1370 if (PR.advrtrs > DRLSTSIZ) 1371 (void)printf(" and %d routers\n", 1372 PR.advrtrs - DRLSTSIZ); 1373 } else 1374 (void)printf(" No advertising router\n"); 1375 } 1376 #undef PR 1377 (void)prog_close(s); 1378 #endif 1379 } 1380 1381 static void 1382 pfx_flush(void) 1383 { 1384 char dummyif[IFNAMSIZ+8]; 1385 int s; 1386 1387 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1388 err(1, "socket"); 1389 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1390 if (prog_ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1391 err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 1392 (void)prog_close(s); 1393 } 1394 1395 static void 1396 rtr_flush(void) 1397 { 1398 char dummyif[IFNAMSIZ+8]; 1399 int s; 1400 1401 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1402 err(1, "socket"); 1403 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1404 if (prog_ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1405 err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 1406 1407 (void)prog_close(s); 1408 } 1409 1410 static void 1411 harmonize_rtr(void) 1412 { 1413 char dummyif[IFNAMSIZ+8]; 1414 int s; 1415 1416 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1417 err(1, "socket"); 1418 (void)strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1419 if (prog_ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1420 err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 1421 1422 (void)prog_close(s); 1423 } 1424 1425 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 1426 static void 1427 setdefif(char *ifname) 1428 { 1429 struct in6_ndifreq ndifreq; 1430 unsigned int ifindex; 1431 int s; 1432 1433 if (strcasecmp(ifname, "delete") == 0) 1434 ifindex = 0; 1435 else { 1436 if ((ifindex = if_nametoindex(ifname)) == 0) 1437 err(1, "failed to resolve i/f index for %s", ifname); 1438 } 1439 1440 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1441 err(1, "socket"); 1442 1443 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1444 ndifreq.ifindex = ifindex; 1445 1446 if (prog_ioctl(s, SIOCSDEFIFACE_IN6, &ndifreq) < 0) 1447 err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 1448 1449 (void)prog_close(s); 1450 } 1451 1452 static void 1453 getdefif(void) 1454 { 1455 struct in6_ndifreq ndifreq; 1456 char ifname[IFNAMSIZ+8]; 1457 int s; 1458 1459 if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1460 err(1, "socket"); 1461 1462 (void)memset(&ndifreq, 0, sizeof(ndifreq)); 1463 (void)strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1464 1465 if (prog_ioctl(s, SIOCGDEFIFACE_IN6, &ndifreq) < 0) 1466 err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 1467 1468 if (ndifreq.ifindex == 0) 1469 (void)printf("No default interface.\n"); 1470 else { 1471 if ((if_indextoname((unsigned int)ndifreq.ifindex, ifname)) == NULL) 1472 err(1, "failed to resolve ifname for index %lu", 1473 ndifreq.ifindex); 1474 (void)printf("ND default interface = %s\n", ifname); 1475 } 1476 1477 (void)prog_close(s); 1478 } 1479 #endif 1480 1481 static const char * 1482 sec2str(time_t total) 1483 { 1484 static char result[256]; 1485 int days, hours, mins, secs; 1486 int first = 1; 1487 char *p = result; 1488 char *ep = &result[sizeof(result)]; 1489 int n; 1490 1491 days = total / 3600 / 24; 1492 hours = (total / 3600) % 24; 1493 mins = (total / 60) % 60; 1494 secs = total % 60; 1495 1496 if (days) { 1497 first = 0; 1498 n = snprintf(p, (size_t)(ep - p), "%dd", days); 1499 if (n < 0 || n >= ep - p) 1500 return "?"; 1501 p += n; 1502 } 1503 if (!first || hours) { 1504 first = 0; 1505 n = snprintf(p, (size_t)(ep - p), "%dh", hours); 1506 if (n < 0 || n >= ep - p) 1507 return "?"; 1508 p += n; 1509 } 1510 if (!first || mins) { 1511 first = 0; 1512 n = snprintf(p, (size_t)(ep - p), "%dm", mins); 1513 if (n < 0 || n >= ep - p) 1514 return "?"; 1515 p += n; 1516 } 1517 (void)snprintf(p, (size_t)(ep - p), "%ds", secs); 1518 1519 return(result); 1520 } 1521 1522 /* 1523 * Print the timestamp 1524 * from tcpdump/util.c 1525 */ 1526 static void 1527 ts_print(const struct timeval *tvp) 1528 { 1529 int s; 1530 1531 /* Default */ 1532 s = (tvp->tv_sec + thiszone) % 86400; 1533 (void)printf("%02d:%02d:%02d.%06u ", 1534 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 1535 } 1536