1 /* $OpenBSD: ndp.c,v 1.90 2018/07/13 09:03:44 krw 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/ioctl.h> 78 #include <sys/socket.h> 79 #include <sys/sysctl.h> 80 #include <sys/time.h> 81 #include <sys/queue.h> 82 83 #include <net/if.h> 84 #include <net/if_dl.h> 85 #include <net/if_types.h> 86 #include <net/route.h> 87 88 #include <netinet/in.h> 89 90 #include <netinet/icmp6.h> 91 #include <netinet6/in6_var.h> 92 #include <netinet6/nd6.h> 93 94 #include <arpa/inet.h> 95 96 #include <stdio.h> 97 #include <errno.h> 98 #include <fcntl.h> 99 #include <netdb.h> 100 #include <stdlib.h> 101 #include <string.h> 102 #include <unistd.h> 103 #include <limits.h> 104 #include <err.h> 105 106 #include "gmt2local.h" 107 108 /* packing rule for routing socket */ 109 #define ROUNDUP(a) \ 110 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 111 112 static pid_t pid; 113 static int nflag; 114 static int tflag; 115 static int32_t thiszone; /* time difference with gmt */ 116 static int rtsock = -1; 117 static int repeat = 0; 118 119 char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */ 120 char host_buf[NI_MAXHOST]; /* getnameinfo() */ 121 char ifix_buf[IFNAMSIZ]; /* if_indextoname() */ 122 123 int file(char *); 124 void getsocket(void); 125 int set(int, char **); 126 void get(char *); 127 int delete(char *); 128 void dump(struct in6_addr *, int); 129 static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int); 130 static char *ether_str(struct sockaddr_dl *); 131 int ndp_ether_aton(char *, u_char *); 132 void usage(void); 133 int rtmsg(int); 134 int rtget(struct sockaddr_in6 **, struct sockaddr_dl **); 135 void ifinfo(char *); 136 static char *sec2str(time_t); 137 static void ts_print(const struct timeval *); 138 static int rdomain; 139 140 int 141 main(int argc, char *argv[]) 142 { 143 int ch; 144 int mode = 0; 145 char *arg = NULL; 146 const char *errstr; 147 148 pid = getpid(); 149 thiszone = gmt2local(0); 150 rdomain = getrtable(); 151 while ((ch = getopt(argc, argv, "acd:f:i:nstA:V:")) != -1) { 152 switch (ch) { 153 case 'a': 154 case 'c': 155 case 'p': 156 case 'r': 157 case 'P': 158 case 's': 159 if (mode) { 160 usage(); 161 /*NOTREACHED*/ 162 } 163 mode = ch; 164 arg = NULL; 165 break; 166 case 'd': 167 case 'f': 168 case 'i' : 169 if (mode) { 170 usage(); 171 /*NOTREACHED*/ 172 } 173 mode = ch; 174 arg = optarg; 175 break; 176 case 'n': 177 nflag = 1; 178 break; 179 case 't': 180 tflag = 1; 181 break; 182 case 'A': 183 if (mode) { 184 usage(); 185 /*NOTREACHED*/ 186 } 187 mode = 'a'; 188 repeat = strtonum(optarg, 1, INT_MAX, &errstr); 189 if (errstr) { 190 usage(); 191 /*NOTREACHED*/ 192 } 193 break; 194 case 'V': 195 rdomain = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr); 196 if (errstr != NULL) { 197 warn("bad rdomain: %s", errstr); 198 usage(); 199 /*NOTREACHED*/ 200 } 201 break; 202 default: 203 usage(); 204 } 205 } 206 argc -= optind; 207 argv += optind; 208 209 switch (mode) { 210 case 'a': 211 case 'c': 212 if (argc != 0) { 213 usage(); 214 /*NOTREACHED*/ 215 } 216 dump(0, mode == 'c'); 217 break; 218 case 'd': 219 if (argc != 0) { 220 usage(); 221 /*NOTREACHED*/ 222 } 223 delete(arg); 224 break; 225 case 'f': 226 if (argc != 0) 227 usage(); 228 file(arg); 229 break; 230 case 'i': 231 if (argc != 0) 232 usage(); 233 ifinfo(arg); 234 break; 235 case 's': 236 if (argc < 2 || argc > 4) 237 usage(); 238 exit(set(argc, argv) ? 1 : 0); 239 case 0: 240 if (argc != 1) { 241 usage(); 242 /*NOTREACHED*/ 243 } 244 get(argv[0]); 245 break; 246 } 247 exit(0); 248 } 249 250 /* 251 * Process a file to set standard ndp entries 252 */ 253 int 254 file(char *name) 255 { 256 FILE *fp; 257 int i, retval; 258 char line[100], arg[5][50], *args[5]; 259 260 if ((fp = fopen(name, "r")) == NULL) { 261 fprintf(stderr, "ndp: cannot open %s\n", name); 262 exit(1); 263 } 264 args[0] = &arg[0][0]; 265 args[1] = &arg[1][0]; 266 args[2] = &arg[2][0]; 267 args[3] = &arg[3][0]; 268 args[4] = &arg[4][0]; 269 retval = 0; 270 while (fgets(line, sizeof(line), fp) != NULL) { 271 i = sscanf(line, "%49s %49s %49s %49s %49s", 272 arg[0], arg[1], arg[2], arg[3], arg[4]); 273 if (i < 2) { 274 fprintf(stderr, "ndp: bad line: %s\n", line); 275 retval = 1; 276 continue; 277 } 278 if (set(i, args)) 279 retval = 1; 280 } 281 fclose(fp); 282 return (retval); 283 } 284 285 void 286 getsocket(void) 287 { 288 socklen_t len = sizeof(rdomain); 289 290 if (rtsock >= 0) 291 return; 292 rtsock = socket(PF_ROUTE, SOCK_RAW, 0); 293 if (rtsock < 0) 294 err(1, "routing socket"); 295 if (setsockopt(rtsock, PF_ROUTE, ROUTE_TABLEFILTER, &rdomain, len) < 0) 296 err(1, "ROUTE_TABLEFILTER"); 297 298 if (pledge("stdio dns", NULL) == -1) 299 err(1, "pledge"); 300 } 301 302 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 }; 303 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m; 304 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 305 struct sockaddr_dl ifp_m = { sizeof(&ifp_m), AF_LINK }; 306 time_t expire_time; 307 int flags, found_entry; 308 struct { 309 struct rt_msghdr m_rtm; 310 char m_space[512]; 311 } m_rtmsg; 312 313 /* 314 * Set an individual neighbor cache entry 315 */ 316 int 317 set(int argc, char **argv) 318 { 319 struct sockaddr_in6 *sin = &sin_m; 320 struct sockaddr_dl *sdl; 321 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 322 struct addrinfo hints, *res; 323 int gai_error; 324 u_char *ea; 325 char *host = argv[0], *eaddr = argv[1]; 326 327 getsocket(); 328 argc -= 2; 329 argv += 2; 330 sdl_m = blank_sdl; 331 sin_m = blank_sin; 332 333 bzero(&hints, sizeof(hints)); 334 hints.ai_family = AF_INET6; 335 gai_error = getaddrinfo(host, NULL, &hints, &res); 336 if (gai_error) { 337 fprintf(stderr, "ndp: %s: %s\n", host, 338 gai_strerror(gai_error)); 339 return 1; 340 } 341 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 342 #ifdef __KAME__ 343 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 344 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 345 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 346 } 347 #endif 348 freeaddrinfo(res); 349 ea = (u_char *)LLADDR(&sdl_m); 350 if (ndp_ether_aton(eaddr, ea) == 0) 351 sdl_m.sdl_alen = 6; 352 expire_time = 0; 353 flags = 0; 354 while (argc-- > 0) { 355 if (strncmp(argv[0], "temp", 4) == 0) { 356 struct timeval now; 357 358 gettimeofday(&now, 0); 359 expire_time = now.tv_sec + 20 * 60; 360 } else if (strncmp(argv[0], "proxy", 5) == 0) 361 flags |= RTF_ANNOUNCE; 362 argv++; 363 } 364 365 if (rtget(&sin, &sdl)) { 366 errx(1, "RTM_GET(%s) failed", host); 367 /* NOTREACHED */ 368 } 369 370 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 371 if (sdl->sdl_family == AF_LINK && 372 (rtm->rtm_flags & RTF_LLINFO) && 373 !(rtm->rtm_flags & RTF_GATEWAY)) { 374 switch (sdl->sdl_type) { 375 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 376 case IFT_ISO88024: case IFT_ISO88025: 377 goto overwrite; 378 } 379 } 380 /* 381 * IPv4 arp command retries with sin_other = SIN_PROXY here. 382 */ 383 fprintf(stderr, "set: cannot configure a new entry\n"); 384 return 1; 385 } 386 387 overwrite: 388 if (sdl->sdl_family != AF_LINK) { 389 printf("cannot intuit interface index and type for %s\n", host); 390 return (1); 391 } 392 sdl_m.sdl_type = sdl->sdl_type; 393 sdl_m.sdl_index = sdl->sdl_index; 394 return (rtmsg(RTM_ADD)); 395 } 396 397 /* 398 * Display an individual neighbor cache entry 399 */ 400 void 401 get(char *host) 402 { 403 struct sockaddr_in6 *sin = &sin_m; 404 struct addrinfo hints, *res; 405 int gai_error; 406 407 sin_m = blank_sin; 408 bzero(&hints, sizeof(hints)); 409 hints.ai_family = AF_INET6; 410 gai_error = getaddrinfo(host, NULL, &hints, &res); 411 if (gai_error) { 412 fprintf(stderr, "ndp: %s: %s\n", host, 413 gai_strerror(gai_error)); 414 return; 415 } 416 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 417 #ifdef __KAME__ 418 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 419 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 420 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 421 } 422 #endif 423 freeaddrinfo(res); 424 dump(&sin->sin6_addr, 0); 425 if (found_entry == 0) { 426 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 427 sizeof(host_buf), NULL ,0, 428 (nflag ? NI_NUMERICHOST : 0)); 429 printf("%s (%s) -- no entry\n", host, host_buf); 430 exit(1); 431 } 432 } 433 434 /* 435 * Delete a neighbor cache entry 436 */ 437 int 438 delete(char *host) 439 { 440 struct sockaddr_in6 *sin = &sin_m; 441 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 442 struct sockaddr_dl *sdl; 443 struct addrinfo hints, *res; 444 int gai_error; 445 446 getsocket(); 447 sin_m = blank_sin; 448 449 bzero(&hints, sizeof(hints)); 450 hints.ai_family = AF_INET6; 451 if (nflag) 452 hints.ai_flags = AI_NUMERICHOST; 453 gai_error = getaddrinfo(host, NULL, &hints, &res); 454 if (gai_error) { 455 fprintf(stderr, "ndp: %s: %s\n", host, 456 gai_strerror(gai_error)); 457 return 1; 458 } 459 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 460 #ifdef __KAME__ 461 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 462 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 463 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 464 } 465 #endif 466 freeaddrinfo(res); 467 if (rtget(&sin, &sdl)) { 468 errx(1, "RTM_GET(%s) failed", host); 469 /* NOTREACHED */ 470 } 471 472 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 473 if (sdl->sdl_family == AF_LINK && rtm->rtm_flags & RTF_LLINFO) { 474 if (rtm->rtm_flags & RTF_LOCAL) 475 return (0); 476 if (!(rtm->rtm_flags & RTF_GATEWAY)) 477 goto delete; 478 } 479 /* 480 * IPv4 arp command retries with sin_other = SIN_PROXY here. 481 */ 482 fprintf(stderr, "delete: cannot delete non-NDP entry\n"); 483 return 1; 484 } 485 486 delete: 487 if (sdl->sdl_family != AF_LINK) { 488 printf("cannot locate %s\n", host); 489 return (1); 490 } 491 if (rtmsg(RTM_DELETE) == 0) { 492 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */ 493 494 #ifdef __KAME__ 495 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) { 496 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]); 497 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0; 498 } 499 #endif 500 getnameinfo((struct sockaddr *)&s6, 501 s6.sin6_len, host_buf, 502 sizeof(host_buf), NULL, 0, 503 (nflag ? NI_NUMERICHOST : 0)); 504 printf("%s (%s) deleted\n", host, host_buf); 505 } 506 507 return 0; 508 } 509 510 #define W_ADDR 36 511 #define W_LL 17 512 #define W_IF 7 513 514 /* 515 * Dump the entire neighbor cache 516 */ 517 void 518 dump(struct in6_addr *addr, int cflag) 519 { 520 int mib[7]; 521 size_t needed; 522 char *lim, *buf = NULL, *next; 523 struct rt_msghdr *rtm; 524 struct sockaddr_in6 *sin; 525 struct sockaddr_dl *sdl; 526 struct in6_nbrinfo *nbi; 527 struct timeval now; 528 int addrwidth; 529 int llwidth; 530 int ifwidth; 531 char *ifname; 532 533 /* Print header */ 534 if (!tflag && !cflag) 535 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n", 536 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 537 W_IF, W_IF, "Netif", "Expire", "S", "Flags"); 538 539 again:; 540 lim = NULL; 541 mib[0] = CTL_NET; 542 mib[1] = PF_ROUTE; 543 mib[2] = 0; 544 mib[3] = AF_INET6; 545 mib[4] = NET_RT_FLAGS; 546 mib[5] = RTF_LLINFO; 547 mib[6] = rdomain; 548 while (1) { 549 if (sysctl(mib, 7, NULL, &needed, NULL, 0) == -1) 550 err(1, "sysctl(PF_ROUTE estimate)"); 551 if (needed == 0) 552 break; 553 if ((buf = realloc(buf, needed)) == NULL) 554 err(1, "realloc"); 555 if (sysctl(mib, 7, buf, &needed, NULL, 0) == -1) { 556 if (errno == ENOMEM) 557 continue; 558 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 559 } 560 lim = buf + needed; 561 break; 562 } 563 564 for (next = buf; next && lim && next < lim; next += rtm->rtm_msglen) { 565 int isrouter = 0, prbs = 0; 566 567 rtm = (struct rt_msghdr *)next; 568 if (rtm->rtm_version != RTM_VERSION) 569 continue; 570 sin = (struct sockaddr_in6 *)(next + rtm->rtm_hdrlen); 571 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len)); 572 573 /* 574 * Some OSes can produce a route that has the LINK flag but 575 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 576 * and BSD/OS, where xx is not the interface identifier on 577 * lo0). Such routes entry would annoy getnbrinfo() below, 578 * so we skip them. 579 * XXX: such routes should have the GATEWAY flag, not the 580 * LINK flag. However, there is rotten routing software 581 * that advertises all routes that have the GATEWAY flag. 582 * Thus, KAME kernel intentionally does not set the LINK flag. 583 * What is to be fixed is not ndp, but such routing software 584 * (and the kernel workaround)... 585 */ 586 if (sdl->sdl_family != AF_LINK) 587 continue; 588 589 if (!(rtm->rtm_flags & RTF_HOST)) 590 continue; 591 592 if (addr) { 593 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr)) 594 continue; 595 found_entry = 1; 596 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) 597 continue; 598 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) || 599 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) { 600 /* XXX: should scope id be filled in the kernel? */ 601 if (sin->sin6_scope_id == 0) 602 sin->sin6_scope_id = sdl->sdl_index; 603 #ifdef __KAME__ 604 /* KAME specific hack; removed the embedded id */ 605 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0; 606 #endif 607 } 608 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 609 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0)); 610 if (cflag) { 611 if (rtm->rtm_flags & RTF_CLONED) 612 delete(host_buf); 613 continue; 614 } 615 gettimeofday(&now, 0); 616 if (tflag) 617 ts_print(&now); 618 619 addrwidth = strlen(host_buf); 620 if (addrwidth < W_ADDR) 621 addrwidth = W_ADDR; 622 llwidth = strlen(ether_str(sdl)); 623 if (W_ADDR + W_LL - addrwidth > llwidth) 624 llwidth = W_ADDR + W_LL - addrwidth; 625 ifname = if_indextoname(sdl->sdl_index, ifix_buf); 626 if (!ifname) 627 ifname = "?"; 628 ifwidth = strlen(ifname); 629 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 630 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 631 632 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf, 633 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname); 634 635 /* Print neighbor discovery specific informations */ 636 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 637 if (nbi) { 638 if (nbi->expire > now.tv_sec) { 639 printf(" %-9.9s", 640 sec2str(nbi->expire - now.tv_sec)); 641 } else if (nbi->expire == 0) 642 printf(" %-9.9s", "permanent"); 643 else 644 printf(" %-9.9s", "expired"); 645 646 switch (nbi->state) { 647 case ND6_LLINFO_NOSTATE: 648 printf(" N"); 649 break; 650 case ND6_LLINFO_INCOMPLETE: 651 printf(" I"); 652 break; 653 case ND6_LLINFO_REACHABLE: 654 printf(" R"); 655 break; 656 case ND6_LLINFO_STALE: 657 printf(" S"); 658 break; 659 case ND6_LLINFO_DELAY: 660 printf(" D"); 661 break; 662 case ND6_LLINFO_PROBE: 663 printf(" P"); 664 break; 665 default: 666 printf(" ?"); 667 break; 668 } 669 670 isrouter = nbi->isrouter; 671 prbs = nbi->asked; 672 } else { 673 warnx("failed to get neighbor information"); 674 printf(" "); 675 } 676 677 printf(" %s%s%s", 678 (rtm->rtm_flags & RTF_LOCAL) ? "l" : "", 679 isrouter ? "R" : "", 680 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 681 682 if (prbs) 683 printf(" %d", prbs); 684 685 printf("\n"); 686 } 687 688 if (repeat) { 689 printf("\n"); 690 fflush(stdout); 691 sleep(repeat); 692 goto again; 693 } 694 695 free(buf); 696 } 697 698 static struct in6_nbrinfo * 699 getnbrinfo(struct in6_addr *addr, int ifindex, int warning) 700 { 701 static struct in6_nbrinfo nbi; 702 int s; 703 704 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 705 err(1, "socket"); 706 707 bzero(&nbi, sizeof(nbi)); 708 if_indextoname(ifindex, nbi.ifname); 709 nbi.addr = *addr; 710 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 711 if (warning) 712 warn("ioctl(SIOCGNBRINFO_IN6)"); 713 close(s); 714 return(NULL); 715 } 716 717 close(s); 718 return(&nbi); 719 } 720 721 static char * 722 ether_str(struct sockaddr_dl *sdl) 723 { 724 static char hbuf[NI_MAXHOST]; 725 u_char *cp; 726 727 if (sdl->sdl_alen) { 728 cp = (u_char *)LLADDR(sdl); 729 snprintf(hbuf, sizeof(hbuf), "%02x:%02x:%02x:%02x:%02x:%02x", 730 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 731 } else 732 snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 733 734 return(hbuf); 735 } 736 737 int 738 ndp_ether_aton(char *a, u_char *n) 739 { 740 int i, o[6]; 741 742 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 743 &o[3], &o[4], &o[5]); 744 if (i != 6) { 745 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 746 return (1); 747 } 748 for (i = 0; i < 6; i++) 749 n[i] = o[i]; 750 return (0); 751 } 752 753 void 754 usage(void) 755 { 756 printf("usage: ndp [-acnt] "); 757 printf("[-A wait] [-d hostname] [-f filename] [-i interface]\n"); 758 printf("\t[-s nodename ether_addr [temp] [proxy]] "); 759 printf("[-V rdomain] [hostname]\n"); 760 exit(1); 761 } 762 763 int 764 rtmsg(int cmd) 765 { 766 static int seq; 767 int rlen; 768 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 769 char *cp = m_rtmsg.m_space; 770 int l; 771 772 errno = 0; 773 if (cmd == RTM_DELETE) 774 goto doit; 775 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 776 rtm->rtm_flags = flags; 777 rtm->rtm_version = RTM_VERSION; 778 rtm->rtm_tableid = rdomain; 779 780 switch (cmd) { 781 default: 782 fprintf(stderr, "ndp: internal wrong cmd\n"); 783 exit(1); 784 case RTM_ADD: 785 rtm->rtm_addrs |= RTA_GATEWAY; 786 if (expire_time) { 787 rtm->rtm_rmx.rmx_expire = expire_time; 788 rtm->rtm_inits = RTV_EXPIRE; 789 } 790 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 791 #if 0 /* we don't support ipv6addr/128 type proxying. */ 792 if (rtm->rtm_flags & RTF_ANNOUNCE) { 793 rtm->rtm_flags &= ~RTF_HOST; 794 rtm->rtm_addrs |= RTA_NETMASK; 795 } 796 #endif 797 /* FALLTHROUGH */ 798 case RTM_GET: 799 rtm->rtm_addrs |= (RTA_DST | RTA_IFP); 800 } 801 #define NEXTADDR(w, s) \ 802 if (rtm->rtm_addrs & (w)) { \ 803 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));} 804 805 NEXTADDR(RTA_DST, sin_m); 806 NEXTADDR(RTA_GATEWAY, sdl_m); 807 #if 0 /* we don't support ipv6addr/128 type proxying. */ 808 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 809 NEXTADDR(RTA_NETMASK, so_mask); 810 #endif 811 NEXTADDR(RTA_IFP, ifp_m); 812 813 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 814 doit: 815 l = rtm->rtm_msglen; 816 rtm->rtm_seq = ++seq; 817 rtm->rtm_type = cmd; 818 if ((rlen = write(rtsock, (char *)&m_rtmsg, l)) < 0) { 819 if (errno != ESRCH || cmd != RTM_DELETE) { 820 err(1, "writing to routing socket"); 821 /* NOTREACHED */ 822 } 823 } 824 do { 825 l = read(rtsock, (char *)&m_rtmsg, sizeof(m_rtmsg)); 826 } while (l > 0 && (rtm->rtm_version != RTM_VERSION || 827 rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 828 if (l < 0) 829 (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 830 strerror(errno)); 831 return (0); 832 } 833 834 int 835 rtget(struct sockaddr_in6 **sinp, struct sockaddr_dl **sdlp) 836 { 837 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 838 struct sockaddr_in6 *sin = NULL; 839 struct sockaddr_dl *sdl = NULL; 840 struct sockaddr *sa; 841 char *cp; 842 unsigned int i; 843 844 if (rtmsg(RTM_GET) < 0) 845 return (1); 846 847 if (rtm->rtm_addrs) { 848 cp = ((char *)rtm + rtm->rtm_hdrlen); 849 for (i = 1; i; i <<= 1) { 850 if (i & rtm->rtm_addrs) { 851 sa = (struct sockaddr *)cp; 852 switch (i) { 853 case RTA_DST: 854 sin = (struct sockaddr_in6 *)sa; 855 break; 856 case RTA_IFP: 857 sdl = (struct sockaddr_dl *)sa; 858 break; 859 default: 860 break; 861 } 862 cp += ROUNDUP(sa->sa_len); 863 } 864 } 865 } 866 867 if (sin == NULL || sdl == NULL) 868 return (1); 869 870 *sinp = sin; 871 *sdlp = sdl; 872 873 return (0); 874 } 875 876 void 877 ifinfo(char *ifname) 878 { 879 struct in6_ndireq nd; 880 int s; 881 882 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 883 err(1, "socket"); 884 /* NOTREACHED */ 885 } 886 bzero(&nd, sizeof(nd)); 887 strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 888 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) 889 err(1, "ioctl(SIOCGIFINFO_IN6)"); 890 891 if (!nd.ndi.initialized) 892 errx(1, "%s: not initialized yet", ifname); 893 894 printf("basereachable=%ds%dms", 895 nd.ndi.basereachable / 1000, nd.ndi.basereachable % 1000); 896 printf(", reachable=%ds", nd.ndi.reachable); 897 printf(", retrans=%ds%dms\n", nd.ndi.retrans / 1000, 898 nd.ndi.retrans % 1000); 899 900 close(s); 901 } 902 903 static char * 904 sec2str(time_t total) 905 { 906 static char result[256]; 907 int days, hours, mins, secs; 908 int first = 1; 909 char *p = result; 910 char *ep = &result[sizeof(result)]; 911 int n; 912 913 days = total / 3600 / 24; 914 hours = (total / 3600) % 24; 915 mins = (total / 60) % 60; 916 secs = total % 60; 917 918 if (days) { 919 first = 0; 920 n = snprintf(p, ep - p, "%dd", days); 921 if (n < 0 || n >= ep - p) 922 return "?"; 923 p += n; 924 } 925 if (!first || hours) { 926 first = 0; 927 n = snprintf(p, ep - p, "%dh", hours); 928 if (n < 0 || n >= ep - p) 929 return "?"; 930 p += n; 931 } 932 if (!first || mins) { 933 first = 0; 934 n = snprintf(p, ep - p, "%dm", mins); 935 if (n < 0 || n >= ep - p) 936 return "?"; 937 p += n; 938 } 939 snprintf(p, ep - p, "%ds", secs); 940 941 return(result); 942 } 943 944 /* 945 * Print the timestamp 946 * from tcpdump/util.c 947 */ 948 static void 949 ts_print(const struct timeval *tvp) 950 { 951 int s; 952 953 /* Default */ 954 s = (tvp->tv_sec + thiszone) % 86400; 955 (void)printf("%02d:%02d:%02d.%06u ", 956 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 957 } 958