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