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