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