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