1 /* $NetBSD: ndp.c,v 1.10 2000/07/04 20:27:38 matt Exp $ */ 2 /* $KAME: ndp.c,v 1.40 2000/06/20 21:50:17 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 /* 33 * Copyright (c) 1984, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * This code is derived from software contributed to Berkeley by 37 * Sun Microsystems, Inc. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by the University of 50 * California, Berkeley and its contributors. 51 * 4. Neither the name of the University nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 */ 67 68 /* 69 * Based on: 70 * "@(#) Copyright (c) 1984, 1993\n\ 71 * The Regents of the University of California. All rights reserved.\n"; 72 * 73 * "@(#)arp.c 8.2 (Berkeley) 1/2/94"; 74 */ 75 76 /* 77 * ndp - display, set, delete and flush neighbor cache 78 */ 79 80 81 #include <sys/param.h> 82 #include <sys/file.h> 83 #include <sys/ioctl.h> 84 #include <sys/socket.h> 85 #include <sys/sysctl.h> 86 #include <sys/time.h> 87 88 #include <net/if.h> 89 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 90 #include <net/if_var.h> 91 #endif /* __FreeBSD__ >= 3 */ 92 #include <net/if_dl.h> 93 #include <net/if_types.h> 94 #include <net/route.h> 95 96 #include <netinet/in.h> 97 #ifndef __NetBSD__ 98 #include <netinet/if_ether.h> 99 #endif 100 101 #include <netinet/icmp6.h> 102 #include <netinet6/in6_var.h> 103 #include <netinet6/nd6.h> 104 105 #include <arpa/inet.h> 106 107 #include <netdb.h> 108 #include <errno.h> 109 #include <nlist.h> 110 #include <stdio.h> 111 #include <string.h> 112 #include <paths.h> 113 #include <err.h> 114 #include <stdlib.h> 115 #include <fcntl.h> 116 #include <unistd.h> 117 #include "gmt2local.h" 118 119 #ifndef NI_WITHSCOPEID 120 #define NI_WITHSCOPEID 0 121 #endif 122 123 /* packing rule for routing socket */ 124 #define ROUNDUP(a) \ 125 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 126 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 127 128 static int pid; 129 static int fflag; 130 static int nflag; 131 static int tflag; 132 static int32_t thiszone; /* time difference with gmt */ 133 static int s = -1; 134 static int repeat = 0; 135 static int lflag = 0; 136 137 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */ 138 char host_buf[NI_MAXHOST]; /* getnameinfo() */ 139 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */ 140 141 int main __P((int, char **)); 142 int file __P((char *)); 143 void getsocket __P((void)); 144 int set __P((int, char **)); 145 void get __P((char *)); 146 int delete __P((char *)); 147 void dump __P((struct in6_addr *)); 148 static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr, 149 int ifindex, int)); 150 static char *ether_str __P((struct sockaddr_dl *)); 151 int ndp_ether_aton __P((char *, u_char *)); 152 void usage __P((void)); 153 int rtmsg __P((int)); 154 void ifinfo __P((int, char **)); 155 void rtrlist __P((void)); 156 void plist __P((void)); 157 void pfx_flush __P((void)); 158 void rtrlist __P((void)); 159 void rtr_flush __P((void)); 160 void harmonize_rtr __P((void)); 161 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 162 static void getdefif __P((void)); 163 static void setdefif __P((char *)); 164 #endif 165 static char *sec2str __P((time_t t)); 166 static char *ether_str __P((struct sockaddr_dl *sdl)); 167 static void ts_print __P((const struct timeval *)); 168 169 int 170 main(argc, argv) 171 int argc; 172 char **argv; 173 { 174 int ch; 175 int aflag = 0, cflag = 0, dflag = 0, sflag = 0, Hflag = 0, 176 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0; 177 178 pid = getpid(); 179 thiszone = gmt2local(0); 180 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != EOF) 181 switch ((char)ch) { 182 case 'a': 183 aflag = 1; 184 break; 185 case 'c': 186 fflag = 1; 187 cflag = 1; 188 break; 189 case 'd': 190 dflag = 1; 191 break; 192 case 'I': 193 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 194 if (argc > 2) 195 setdefif(argv[2]); 196 getdefif(); /* always call it to print the result */ 197 exit(0); 198 #else 199 errx(1, "not supported yet"); 200 /*NOTREACHED*/ 201 #endif 202 case 'i' : 203 argc -= optind; 204 argv += optind; 205 if (argc < 1) 206 usage(); 207 ifinfo(argc, argv); 208 exit(0); 209 case 'n': 210 nflag = 1; 211 continue; 212 case 'p': 213 pflag = 1; 214 break; 215 case 'f' : 216 if (argc != 3) 217 usage(); 218 file(argv[2]); 219 exit(0); 220 case 'l' : 221 lflag = 1; 222 break; 223 case 'r' : 224 rflag = 1; 225 break; 226 case 's': 227 sflag = 1; 228 break; 229 case 't': 230 tflag = 1; 231 break; 232 case 'A': 233 aflag = 1; 234 repeat = atoi(optarg); 235 if (repeat < 0) 236 usage(); 237 break; 238 case 'H' : 239 Hflag = 1; 240 break; 241 case 'P': 242 Pflag = 1; 243 break; 244 case 'R': 245 Rflag = 1; 246 break; 247 default: 248 usage(); 249 } 250 251 argc -= optind; 252 argv += optind; 253 254 if (aflag || cflag) { 255 dump(0); 256 exit(0); 257 } 258 if (dflag) { 259 if (argc != 1) 260 usage(); 261 delete(argv[0]); 262 exit(0); 263 } 264 if (pflag) { 265 plist(); 266 exit(0); 267 } 268 if (rflag) { 269 rtrlist(); 270 exit(0); 271 } 272 if (sflag) { 273 if (argc < 2 || argc > 4) 274 usage(); 275 exit(set(argc, argv) ? 1 : 0); 276 } 277 if (Hflag) { 278 harmonize_rtr(); 279 exit(0); 280 } 281 if (Pflag) { 282 pfx_flush(); 283 exit(0); 284 } 285 if (Rflag) { 286 rtr_flush(); 287 exit(0); 288 } 289 290 if (argc != 1) 291 usage(); 292 get(argv[0]); 293 exit(0); 294 } 295 296 /* 297 * Process a file to set standard ndp entries 298 */ 299 int 300 file(name) 301 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() 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(argc, argv) 358 int argc; 359 char **argv; 360 { 361 register struct sockaddr_in6 *sin = &sin_m; 362 register struct sockaddr_dl *sdl; 363 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 364 struct addrinfo hints, *res; 365 int gai_error; 366 u_char *ea; 367 char *host = argv[0], *eaddr = argv[1]; 368 369 getsocket(); 370 argc -= 2; 371 argv += 2; 372 sdl_m = blank_sdl; 373 sin_m = blank_sin; 374 375 bzero(&hints, sizeof(hints)); 376 hints.ai_family = AF_INET6; 377 gai_error = getaddrinfo(host, NULL, &hints, &res); 378 if (gai_error) { 379 fprintf(stderr, "ndp: %s: %s\n", host, 380 gai_strerror(gai_error)); 381 return 1; 382 } 383 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 384 #ifdef __KAME__ 385 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 386 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 387 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 388 } 389 #endif 390 ea = (u_char *)LLADDR(&sdl_m); 391 if (ndp_ether_aton(eaddr, ea) == 0) 392 sdl_m.sdl_alen = 6; 393 flags = expire_time = 0; 394 while (argc-- > 0) { 395 if (strncmp(argv[0], "temp", 4) == 0) { 396 struct timeval time; 397 gettimeofday(&time, 0); 398 expire_time = time.tv_sec + 20 * 60; 399 } else if (strncmp(argv[0], "proxy", 5) == 0) 400 flags |= RTF_ANNOUNCE; 401 argv++; 402 } 403 if (rtmsg(RTM_GET) < 0) { 404 perror(host); 405 return (1); 406 } 407 sin = (struct sockaddr_in6 *)(rtm + 1); 408 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin); 409 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 410 if (sdl->sdl_family == AF_LINK && 411 (rtm->rtm_flags & RTF_LLINFO) && 412 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 413 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 414 case IFT_ISO88024: case IFT_ISO88025: 415 goto overwrite; 416 } 417 /* 418 * IPv4 arp command retries with sin_other = SIN_PROXY here. 419 */ 420 fprintf(stderr, "set: cannot configure a new entry\n"); 421 return 1; 422 } 423 424 overwrite: 425 if (sdl->sdl_family != AF_LINK) { 426 printf("cannot intuit interface index and type for %s\n", host); 427 return (1); 428 } 429 sdl_m.sdl_type = sdl->sdl_type; 430 sdl_m.sdl_index = sdl->sdl_index; 431 return (rtmsg(RTM_ADD)); 432 } 433 434 /* 435 * Display an individual neighbor cache entry 436 */ 437 void 438 get(host) 439 char *host; 440 { 441 struct sockaddr_in6 *sin = &sin_m; 442 struct addrinfo hints, *res; 443 int gai_error; 444 445 sin_m = blank_sin; 446 bzero(&hints, sizeof(hints)); 447 hints.ai_family = AF_INET6; 448 gai_error = getaddrinfo(host, NULL, &hints, &res); 449 if (gai_error) { 450 fprintf(stderr, "ndp: %s: %s\n", host, 451 gai_strerror(gai_error)); 452 return; 453 } 454 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 455 #ifdef __KAME__ 456 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 457 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 458 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 459 } 460 #endif 461 dump(&sin->sin6_addr); 462 if (found_entry == 0) { 463 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 464 sizeof(host_buf), NULL ,0, 465 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)); 466 printf("%s (%s) -- no entry\n", host, host_buf); 467 exit(1); 468 } 469 } 470 471 /* 472 * Delete a neighbor cache entry 473 */ 474 int 475 delete(host) 476 char *host; 477 { 478 struct sockaddr_in6 *sin = &sin_m; 479 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 480 struct sockaddr_dl *sdl; 481 struct addrinfo hints, *res; 482 int gai_error; 483 484 getsocket(); 485 sin_m = blank_sin; 486 487 bzero(&hints, sizeof(hints)); 488 hints.ai_family = AF_INET6; 489 gai_error = getaddrinfo(host, NULL, &hints, &res); 490 if (gai_error) { 491 fprintf(stderr, "ndp: %s: %s\n", host, 492 gai_strerror(gai_error)); 493 return 1; 494 } 495 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 496 #ifdef __KAME__ 497 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 498 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 499 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 500 } 501 #endif 502 if (rtmsg(RTM_GET) < 0) { 503 perror(host); 504 return (1); 505 } 506 sin = (struct sockaddr_in6 *)(rtm + 1); 507 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin); 508 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 509 if (sdl->sdl_family == AF_LINK && 510 (rtm->rtm_flags & RTF_LLINFO) && 511 !(rtm->rtm_flags & RTF_GATEWAY)) { 512 switch (sdl->sdl_type) { 513 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 514 case IFT_ISO88024: case IFT_ISO88025: 515 goto delete; 516 } 517 } 518 /* 519 * IPv4 arp command retries with sin_other = SIN_PROXY here. 520 */ 521 fprintf(stderr, "delete: cannot delete non-NDP entry\n"); 522 return 1; 523 } 524 525 delete: 526 if (sdl->sdl_family != AF_LINK) { 527 printf("cannot locate %s\n", host); 528 return (1); 529 } 530 if (rtmsg(RTM_DELETE) == 0) { 531 getnameinfo((struct sockaddr *)sin, 532 sin->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 /* 542 * Dump the entire neighbor cache 543 */ 544 void 545 dump(addr) 546 struct in6_addr *addr; 547 { 548 int mib[6]; 549 size_t needed; 550 char *lim, *buf, *next; 551 struct rt_msghdr *rtm; 552 struct sockaddr_in6 *sin; 553 struct sockaddr_dl *sdl; 554 struct in6_nbrinfo *nbi; 555 struct timeval time; 556 int addrwidth; 557 char flgbuf[8]; 558 559 /* Print header */ 560 if (!tflag) 561 printf("%-31.31s %-17.17s %6.6s %-9.9s %2s %4s %4s\n", 562 "Neighbor", "Linklayer Address", "Netif", "Expire", 563 "St", "Flgs", "Prbs"); 564 565 again:; 566 mib[0] = CTL_NET; 567 mib[1] = PF_ROUTE; 568 mib[2] = 0; 569 mib[3] = AF_INET6; 570 mib[4] = NET_RT_FLAGS; 571 mib[5] = RTF_LLINFO; 572 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 573 err(1, "sysctl(PF_ROUTE estimate)"); 574 if (needed > 0) { 575 if ((buf = malloc(needed)) == NULL) 576 errx(1, "malloc"); 577 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 578 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 579 lim = buf + needed; 580 } else 581 buf = lim = NULL; 582 583 for (next = buf; next && next < lim; next += rtm->rtm_msglen) { 584 int isrouter = 0, prbs = 0; 585 586 rtm = (struct rt_msghdr *)next; 587 sin = (struct sockaddr_in6 *)(rtm + 1); 588 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len)); 589 if (addr) { 590 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr)) 591 continue; 592 found_entry = 1; 593 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) 594 continue; 595 if (fflag == 1) { 596 delete((char *)inet_ntop(AF_INET6, &sin->sin6_addr, 597 ntop_buf, sizeof(ntop_buf))); 598 continue; 599 } 600 601 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) || 602 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) { 603 /* XXX: should scope id be filled in the kernel? */ 604 if (sin->sin6_scope_id == 0) 605 sin->sin6_scope_id = sdl->sdl_index; 606 607 /* XXX: KAME specific hack; removed the embedded id */ 608 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0; 609 } 610 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 611 sizeof(host_buf), NULL, 0, 612 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)); 613 gettimeofday(&time, 0); 614 if (tflag) 615 ts_print(&time); 616 617 if (lflag) { 618 addrwidth = strlen(host_buf); 619 if (addrwidth < 31) 620 addrwidth = 31; 621 } else 622 addrwidth = 31; 623 624 printf("%-*.*s %-17.17s %6.6s", addrwidth, addrwidth, host_buf, 625 ether_str(sdl), 626 if_indextoname(sdl->sdl_index, ifix_buf)); 627 628 /* Print neighbor discovery specific informations */ 629 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 630 if (nbi) { 631 if (nbi->expire > time.tv_sec) { 632 printf(" %-9.9s", 633 sec2str(nbi->expire - time.tv_sec)); 634 } 635 else if (nbi->expire == 0) 636 printf(" %-9.9s", "permanent"); 637 else 638 printf(" %-9.9s", "expired"); 639 640 switch(nbi->state) { 641 case ND6_LLINFO_NOSTATE: 642 printf(" N"); 643 break; 644 case ND6_LLINFO_WAITDELETE: 645 printf(" W"); 646 break; 647 case ND6_LLINFO_INCOMPLETE: 648 printf(" I"); 649 break; 650 case ND6_LLINFO_REACHABLE: 651 printf(" R"); 652 break; 653 case ND6_LLINFO_STALE: 654 printf(" S"); 655 break; 656 case ND6_LLINFO_DELAY: 657 printf(" D"); 658 break; 659 case ND6_LLINFO_PROBE: 660 printf(" P"); 661 break; 662 default: 663 printf(" ?"); 664 break; 665 } 666 667 isrouter = nbi->isrouter; 668 prbs = nbi->asked; 669 } 670 else { 671 warnx("failed to get neighbor information"); 672 printf(" "); 673 } 674 putchar(' '); 675 676 /* 677 * other flags. R: router, P: proxy, W: ?? 678 */ 679 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) { 680 snprintf(flgbuf, sizeof(flgbuf), "%s%s", 681 isrouter ? "R" : "", 682 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 683 } else { 684 sin = (struct sockaddr_in6 *) 685 (sdl->sdl_len + (char *)sdl); 686 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 687 isrouter ? "R" : "", 688 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) 689 ? "P" : "", 690 (sin->sin6_len != sizeof(struct sockaddr_in6)) 691 ? "W" : "", 692 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 693 } 694 printf(" %-4.4s", flgbuf); 695 696 if (prbs) 697 printf(" %4d", prbs); 698 699 printf("\n"); 700 } 701 702 if (repeat) { 703 printf("\n"); 704 sleep(repeat); 705 goto again; 706 } 707 } 708 709 static struct in6_nbrinfo * 710 getnbrinfo(addr, ifindex, warning) 711 struct in6_addr *addr; 712 int ifindex; 713 int warning; 714 { 715 static struct in6_nbrinfo nbi; 716 int s; 717 718 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 719 err(1, "socket"); 720 721 bzero(&nbi, sizeof(nbi)); 722 if_indextoname(ifindex, nbi.ifname); 723 nbi.addr = *addr; 724 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 725 if (warning) 726 warn("ioctl(SIOCGNBRINFO_IN6)"); 727 close(s); 728 return(NULL); 729 } 730 731 close(s); 732 return(&nbi); 733 } 734 735 static char * 736 ether_str(sdl) 737 struct sockaddr_dl *sdl; 738 { 739 static char ebuf[32]; 740 u_char *cp; 741 742 if (sdl->sdl_alen) { 743 cp = (u_char *)LLADDR(sdl); 744 sprintf(ebuf, "%x:%x:%x:%x:%x:%x", 745 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 746 } 747 else { 748 sprintf(ebuf, "(incomplete)"); 749 } 750 751 return(ebuf); 752 } 753 754 int 755 ndp_ether_aton(a, n) 756 char *a; 757 u_char *n; 758 { 759 int i, o[6]; 760 761 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 762 &o[3], &o[4], &o[5]); 763 if (i != 6) { 764 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 765 return (1); 766 } 767 for (i=0; i<6; i++) 768 n[i] = o[i]; 769 return (0); 770 } 771 772 void 773 usage() 774 { 775 printf("usage: ndp hostname\n"); 776 printf(" ndp -a[ntl]\n"); 777 printf(" ndp [-ntl] -A wait\n"); 778 printf(" ndp -c[nt]\n"); 779 printf(" ndp -d[nt] hostname\n"); 780 printf(" ndp -f[nt] filename\n"); 781 printf(" ndp -i interface [flags...]\n"); 782 #ifdef SIOCSDEFIFACE_IN6 783 printf(" ndp -I [interface|delete]\n"); 784 #endif 785 printf(" ndp -p\n"); 786 printf(" ndp -r\n"); 787 printf(" ndp -s hostname ether_addr [temp] [proxy]\n"); 788 printf(" ndp -H\n"); 789 printf(" ndp -P\n"); 790 printf(" ndp -R\n"); 791 exit(1); 792 } 793 794 int 795 rtmsg(cmd) 796 int cmd; 797 { 798 static int seq; 799 int rlen; 800 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 801 register char *cp = m_rtmsg.m_space; 802 register int l; 803 804 errno = 0; 805 if (cmd == RTM_DELETE) 806 goto doit; 807 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 808 rtm->rtm_flags = flags; 809 rtm->rtm_version = RTM_VERSION; 810 811 switch (cmd) { 812 default: 813 fprintf(stderr, "ndp: internal wrong cmd\n"); 814 exit(1); 815 case RTM_ADD: 816 rtm->rtm_addrs |= RTA_GATEWAY; 817 rtm->rtm_rmx.rmx_expire = expire_time; 818 rtm->rtm_inits = RTV_EXPIRE; 819 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 820 if (rtm->rtm_flags & RTF_ANNOUNCE) { 821 rtm->rtm_flags &= ~RTF_HOST; 822 rtm->rtm_flags |= RTA_NETMASK; 823 } 824 /* FALLTHROUGH */ 825 case RTM_GET: 826 rtm->rtm_addrs |= RTA_DST; 827 } 828 #define NEXTADDR(w, s) \ 829 if (rtm->rtm_addrs & (w)) { \ 830 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);} 831 832 NEXTADDR(RTA_DST, sin_m); 833 NEXTADDR(RTA_GATEWAY, sdl_m); 834 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 835 NEXTADDR(RTA_NETMASK, so_mask); 836 837 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 838 doit: 839 l = rtm->rtm_msglen; 840 rtm->rtm_seq = ++seq; 841 rtm->rtm_type = cmd; 842 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 843 if (errno != ESRCH || cmd != RTM_DELETE) { 844 perror("writing to routing socket"); 845 return (-1); 846 } 847 } 848 do { 849 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 850 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 851 if (l < 0) 852 (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 853 strerror(errno)); 854 return (0); 855 } 856 857 void 858 ifinfo(argc, argv) 859 int argc; 860 char **argv; 861 { 862 struct in6_ndireq nd; 863 int i, s; 864 char *ifname = argv[0]; 865 u_int32_t newflags; 866 867 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 868 perror("ndp: socket"); 869 exit(1); 870 } 871 bzero(&nd, sizeof(nd)); 872 strcpy(nd.ifname, ifname); 873 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 874 perror("ioctl (SIOCGIFINFO_IN6)"); 875 exit(1); 876 } 877 #define ND nd.ndi 878 newflags = ND.flags; 879 for (i = 1; i < argc; i++) { 880 int clear = 0; 881 char *cp = argv[i]; 882 883 if (*cp == '-') { 884 clear = 1; 885 cp++; 886 } 887 888 #define SETFLAG(s, f) \ 889 do {\ 890 if (strcmp(cp, (s)) == 0) {\ 891 if (clear)\ 892 newflags &= ~(f);\ 893 else\ 894 newflags |= (f);\ 895 }\ 896 } while (0) 897 SETFLAG("nud", ND6_IFF_PERFORMNUD); 898 899 ND.flags = newflags; 900 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) { 901 perror("ioctl(SIOCSIFINFO_FLAGS)"); 902 exit(1); 903 } 904 #undef SETFLAG 905 } 906 907 printf("linkmtu=%d", ND.linkmtu); 908 printf(", curhlim=%d", ND.chlim); 909 printf(", basereachable=%ds%dms", 910 ND.basereachable / 1000, ND.basereachable % 1000); 911 printf(", reachable=%ds", ND.reachable); 912 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 913 if (ND.flags) { 914 printf("\nFlags: "); 915 if ((ND.flags & ND6_IFF_PERFORMNUD) != 0) 916 printf("PERFORMNUD "); 917 } 918 putc('\n', stdout); 919 #undef ND 920 921 close(s); 922 } 923 924 void 925 rtrlist() 926 { 927 struct in6_drlist dr; 928 int s, i; 929 struct timeval time; 930 931 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 932 perror("ndp: socket"); 933 exit(1); 934 } 935 bzero(&dr, sizeof(dr)); 936 strcpy(dr.ifname, "lo0"); /* dummy */ 937 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 938 perror("ioctl (SIOCGDRLST_IN6)"); 939 exit(1); 940 } 941 #define DR dr.defrouter[i] 942 for (i = 0 ; DR.if_index && i < PRLSTSIZ ; i++) { 943 struct sockaddr_in6 sin6; 944 945 bzero(&sin6, sizeof(sin6)); 946 sin6.sin6_family = AF_INET6; 947 sin6.sin6_len = sizeof(sin6); 948 sin6.sin6_addr = DR.rtaddr; 949 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf, 950 sizeof(host_buf), NULL, 0, 951 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)); 952 953 printf("%s if=%s", host_buf, 954 if_indextoname(DR.if_index, ifix_buf)); 955 printf(", flags=%s%s", 956 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "", 957 DR.flags & ND_RA_FLAG_OTHER ? "O" : ""); 958 gettimeofday(&time, 0); 959 if (DR.expire == 0) 960 printf(", expire=Never\n"); 961 else 962 printf(", expire=%s\n", 963 sec2str(DR.expire - time.tv_sec)); 964 } 965 #undef DR 966 close(s); 967 } 968 969 void 970 plist() 971 { 972 struct in6_prlist pr; 973 int s, i; 974 struct timeval time; 975 976 gettimeofday(&time, 0); 977 978 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 979 perror("ndp: socket"); 980 exit(1); 981 } 982 bzero(&pr, sizeof(pr)); 983 strcpy(pr.ifname, "lo0"); /* dummy */ 984 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) { 985 perror("ioctl (SIOCGPRLST_IN6)"); 986 exit(1); 987 } 988 #define PR pr.prefix[i] 989 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) { 990 printf("%s/%d if=%s\n", 991 inet_ntop(AF_INET6, &PR.prefix, ntop_buf, 992 sizeof(ntop_buf)), PR.prefixlen, 993 if_indextoname(PR.if_index, ifix_buf)); 994 gettimeofday(&time, 0); 995 /* 996 * meaning of fields, especially flags, is very different 997 * by origin. notify the difference to the users. 998 */ 999 printf(" %s", PR.origin == PR_ORIG_RA ? "" : "advertise: "); 1000 printf("flags=%s%s", 1001 PR.raflags.onlink ? "L" : "", 1002 PR.raflags.autonomous ? "A" : ""); 1003 if (PR.vltime == ND6_INFINITE_LIFETIME) 1004 printf(" vltime=infinity"); 1005 else 1006 printf(" vltime=%ld", (long)PR.vltime); 1007 if (PR.pltime == ND6_INFINITE_LIFETIME) 1008 printf(", pltime=infinity"); 1009 else 1010 printf(", pltime=%ld", (long)PR.pltime); 1011 if (PR.expire == 0) 1012 printf(", expire=Never"); 1013 else if (PR.expire >= time.tv_sec) 1014 printf(", expire=%s", 1015 sec2str(PR.expire - time.tv_sec)); 1016 else 1017 printf(", expired"); 1018 switch (PR.origin) { 1019 case PR_ORIG_RA: 1020 printf(", origin=RA"); 1021 break; 1022 case PR_ORIG_RR: 1023 printf(", origin=RR"); 1024 break; 1025 case PR_ORIG_STATIC: 1026 printf(", origin=static"); 1027 break; 1028 case PR_ORIG_KERNEL: 1029 printf(", origin=kernel"); 1030 break; 1031 default: 1032 printf(", origin=?"); 1033 break; 1034 } 1035 printf("\n"); 1036 /* 1037 * "advertising router" list is meaningful only if the prefix 1038 * information is from RA. 1039 */ 1040 if (PR.origin != PR_ORIG_RA) 1041 ; 1042 else if (PR.advrtrs) { 1043 int j; 1044 printf(" advertised by\n"); 1045 for (j = 0; j < PR.advrtrs; j++) { 1046 struct sockaddr_in6 sin6; 1047 struct in6_nbrinfo *nbi; 1048 1049 bzero(&sin6, sizeof(sin6)); 1050 sin6.sin6_family = AF_INET6; 1051 sin6.sin6_len = sizeof(sin6); 1052 sin6.sin6_addr = PR.advrtr[j]; 1053 sin6.sin6_scope_id = PR.if_index; /* XXX */ 1054 getnameinfo((struct sockaddr *)&sin6, 1055 sin6.sin6_len, host_buf, 1056 sizeof(host_buf), NULL, 0, 1057 NI_WITHSCOPEID | (nflag ? NI_NUMERICHOST : 0)); 1058 printf(" %s", host_buf); 1059 1060 nbi = getnbrinfo(&sin6.sin6_addr, PR.if_index, 1061 0); 1062 if (nbi) { 1063 switch(nbi->state) { 1064 case ND6_LLINFO_REACHABLE: 1065 case ND6_LLINFO_STALE: 1066 case ND6_LLINFO_DELAY: 1067 case ND6_LLINFO_PROBE: 1068 printf(" (reachable)\n"); 1069 break; 1070 default: 1071 printf(" (unreachable)\n"); 1072 } 1073 } 1074 else 1075 printf(" (no neighbor state)\n"); 1076 } 1077 if (PR.advrtrs > DRLSTSIZ) 1078 printf(" and %d routers\n", 1079 PR.advrtrs - DRLSTSIZ); 1080 } else 1081 printf(" No advertising router\n"); 1082 } 1083 #undef PR 1084 close(s); 1085 } 1086 1087 void 1088 pfx_flush() 1089 { 1090 char dummyif[IFNAMSIZ+8]; 1091 int s; 1092 1093 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1094 err(1, "socket"); 1095 strcpy(dummyif, "lo0"); /* dummy */ 1096 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1097 err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 1098 } 1099 1100 void 1101 rtr_flush() 1102 { 1103 char dummyif[IFNAMSIZ+8]; 1104 int s; 1105 1106 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1107 err(1, "socket"); 1108 strcpy(dummyif, "lo0"); /* dummy */ 1109 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1110 err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 1111 1112 close(s); 1113 } 1114 1115 void 1116 harmonize_rtr() 1117 { 1118 char dummyif[IFNAMSIZ+8]; 1119 int s; 1120 1121 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1122 err(1, "socket"); 1123 strcpy(dummyif, "lo0"); /* dummy */ 1124 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1125 err(1, "ioctl (SIOCSNDFLUSH_IN6)"); 1126 1127 close(s); 1128 } 1129 1130 #ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 1131 static void 1132 setdefif(ifname) 1133 char *ifname; 1134 { 1135 struct in6_ndifreq ndifreq; 1136 unsigned int ifindex; 1137 1138 if (strcasecmp(ifname, "delete") == 0) 1139 ifindex = 0; 1140 else { 1141 if ((ifindex = if_nametoindex(ifname)) == 0) 1142 err(1, "failed to resolve i/f index for %s", ifname); 1143 } 1144 1145 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1146 err(1, "socket"); 1147 1148 strcpy(ndifreq.ifname, "lo0"); /* dummy */ 1149 ndifreq.ifindex = ifindex; 1150 1151 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1152 err(1, "ioctl (SIOCSDEFIFACE_IN6)"); 1153 1154 close(s); 1155 } 1156 1157 static void 1158 getdefif() 1159 { 1160 struct in6_ndifreq ndifreq; 1161 char ifname[IFNAMSIZ+8]; 1162 1163 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1164 err(1, "socket"); 1165 1166 memset(&ndifreq, 0, sizeof(ndifreq)); 1167 strcpy(ndifreq.ifname, "lo0"); /* dummy */ 1168 1169 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1170 err(1, "ioctl (SIOCGDEFIFACE_IN6)"); 1171 1172 if (ndifreq.ifindex == 0) 1173 printf("No default interface.\n"); 1174 else { 1175 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL) 1176 err(1, "failed to resolve ifname for index %lu", 1177 ndifreq.ifindex); 1178 printf("ND default interface = %s\n", ifname); 1179 } 1180 1181 close(s); 1182 } 1183 #endif 1184 1185 static char * 1186 sec2str(total) 1187 time_t total; 1188 { 1189 static char result[256]; 1190 int days, hours, mins, secs; 1191 int first = 1; 1192 char *p = result; 1193 1194 days = total / 3600 / 24; 1195 hours = (total / 3600) % 24; 1196 mins = (total / 60) % 60; 1197 secs = total % 60; 1198 1199 if (days) { 1200 first = 0; 1201 p += sprintf(p, "%dd", days); 1202 } 1203 if (!first || hours) { 1204 first = 0; 1205 p += sprintf(p, "%dh", hours); 1206 } 1207 if (!first || mins) { 1208 first = 0; 1209 p += sprintf(p, "%dm", mins); 1210 } 1211 sprintf(p, "%ds", secs); 1212 1213 return(result); 1214 } 1215 1216 /* 1217 * Print the timestamp 1218 * from tcpdump/util.c 1219 */ 1220 static void 1221 ts_print(tvp) 1222 const struct timeval *tvp; 1223 { 1224 int s; 1225 1226 /* Default */ 1227 s = (tvp->tv_sec + thiszone) % 86400; 1228 (void)printf("%02d:%02d:%02d.%06u ", 1229 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 1230 } 1231