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