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