1 /* $OpenBSD: ndp.c,v 1.78 2016/08/15 08:52:03 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 *, int, char **); 138 void rtrlist(void); 139 void plist(void); 140 void pfx_flush(void); 141 void rtr_flush(void); 142 void harmonize_rtr(void); 143 static char *sec2str(time_t); 144 static void ts_print(const struct timeval *); 145 static int rdomain = 0; 146 147 static char *rtpref_str[] = { 148 "medium", /* 00 */ 149 "high", /* 01 */ 150 "rsv", /* 10 */ 151 "low" /* 11 */ 152 }; 153 154 int 155 main(int argc, char *argv[]) 156 { 157 int ch; 158 int mode = 0; 159 char *arg = NULL; 160 const char *errstr; 161 162 pid = getpid(); 163 thiszone = gmt2local(0); 164 while ((ch = getopt(argc, argv, "acd:f:i:nprstA:HPRV:")) != -1) 165 switch (ch) { 166 case 'a': 167 case 'c': 168 case 'p': 169 case 'r': 170 case 'H': 171 case 'P': 172 case 'R': 173 case 's': 174 if (mode) { 175 usage(); 176 /*NOTREACHED*/ 177 } 178 mode = ch; 179 arg = NULL; 180 break; 181 case 'd': 182 case 'f': 183 case 'i' : 184 if (mode) { 185 usage(); 186 /*NOTREACHED*/ 187 } 188 mode = ch; 189 arg = optarg; 190 break; 191 case 'n': 192 nflag = 1; 193 break; 194 case 't': 195 tflag = 1; 196 break; 197 case 'A': 198 if (mode) { 199 usage(); 200 /*NOTREACHED*/ 201 } 202 mode = 'a'; 203 repeat = strtonum(optarg, 1, INT_MAX, &errstr); 204 if (errstr) { 205 usage(); 206 /*NOTREACHED*/ 207 } 208 break; 209 case 'V': 210 rdomain = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr); 211 if (errstr != NULL) { 212 warn("bad rdomain: %s", errstr); 213 usage(); 214 /*NOTREACHED*/ 215 } 216 break; 217 default: 218 usage(); 219 } 220 221 argc -= optind; 222 argv += optind; 223 224 switch (mode) { 225 case 'a': 226 case 'c': 227 if (argc != 0) { 228 usage(); 229 /*NOTREACHED*/ 230 } 231 dump(0, mode == 'c'); 232 break; 233 case 'd': 234 if (argc != 0) { 235 usage(); 236 /*NOTREACHED*/ 237 } 238 delete(arg); 239 break; 240 case 'f': 241 if (argc != 0) 242 usage(); 243 file(arg); 244 break; 245 case 'p': 246 if (argc != 0) { 247 usage(); 248 /*NOTREACHED*/ 249 } 250 plist(); 251 break; 252 case 'i': 253 ifinfo(arg, argc, argv); 254 break; 255 case 'r': 256 if (argc != 0) { 257 usage(); 258 /*NOTREACHED*/ 259 } 260 rtrlist(); 261 break; 262 case 's': 263 if (argc < 2 || argc > 4) 264 usage(); 265 exit(set(argc, argv) ? 1 : 0); 266 case 'H': 267 if (argc != 0) { 268 usage(); 269 /*NOTREACHED*/ 270 } 271 harmonize_rtr(); 272 break; 273 case 'P': 274 if (argc != 0) { 275 usage(); 276 /*NOTREACHED*/ 277 } 278 pfx_flush(); 279 break; 280 case 'R': 281 if (argc != 0) { 282 usage(); 283 /*NOTREACHED*/ 284 } 285 rtr_flush(); 286 break; 287 case 0: 288 if (argc != 1) { 289 usage(); 290 /*NOTREACHED*/ 291 } 292 get(argv[0]); 293 break; 294 } 295 exit(0); 296 } 297 298 /* 299 * Process a file to set standard ndp entries 300 */ 301 int 302 file(char *name) 303 { 304 FILE *fp; 305 int i, retval; 306 char line[100], arg[5][50], *args[5]; 307 308 if ((fp = fopen(name, "r")) == NULL) { 309 fprintf(stderr, "ndp: cannot open %s\n", name); 310 exit(1); 311 } 312 args[0] = &arg[0][0]; 313 args[1] = &arg[1][0]; 314 args[2] = &arg[2][0]; 315 args[3] = &arg[3][0]; 316 args[4] = &arg[4][0]; 317 retval = 0; 318 while (fgets(line, sizeof(line), fp) != NULL) { 319 i = sscanf(line, "%49s %49s %49s %49s %49s", 320 arg[0], arg[1], arg[2], arg[3], arg[4]); 321 if (i < 2) { 322 fprintf(stderr, "ndp: bad line: %s\n", line); 323 retval = 1; 324 continue; 325 } 326 if (set(i, args)) 327 retval = 1; 328 } 329 fclose(fp); 330 return (retval); 331 } 332 333 void 334 getsocket(void) 335 { 336 if (rtsock < 0) { 337 rtsock = socket(PF_ROUTE, SOCK_RAW, 0); 338 if (rtsock < 0) { 339 err(1, "socket"); 340 /* NOTREACHED */ 341 } 342 } 343 } 344 345 struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 }; 346 struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m; 347 struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 348 struct sockaddr_dl ifp_m = { sizeof(&ifp_m), AF_LINK }; 349 time_t expire_time; 350 int flags, found_entry; 351 struct { 352 struct rt_msghdr m_rtm; 353 char m_space[512]; 354 } m_rtmsg; 355 356 /* 357 * Set an individual neighbor cache entry 358 */ 359 int 360 set(int argc, char **argv) 361 { 362 struct sockaddr_in6 *sin = &sin_m; 363 struct sockaddr_dl *sdl; 364 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 365 struct addrinfo hints, *res; 366 int gai_error; 367 u_char *ea; 368 char *host = argv[0], *eaddr = argv[1]; 369 370 getsocket(); 371 argc -= 2; 372 argv += 2; 373 sdl_m = blank_sdl; 374 sin_m = blank_sin; 375 376 bzero(&hints, sizeof(hints)); 377 hints.ai_family = AF_INET6; 378 gai_error = getaddrinfo(host, NULL, &hints, &res); 379 if (gai_error) { 380 fprintf(stderr, "ndp: %s: %s\n", host, 381 gai_strerror(gai_error)); 382 return 1; 383 } 384 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 385 #ifdef __KAME__ 386 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 387 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 388 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 389 } 390 #endif 391 ea = (u_char *)LLADDR(&sdl_m); 392 if (ndp_ether_aton(eaddr, ea) == 0) 393 sdl_m.sdl_alen = 6; 394 expire_time = 0; 395 flags = 0; 396 while (argc-- > 0) { 397 if (strncmp(argv[0], "temp", 4) == 0) { 398 struct timeval now; 399 400 gettimeofday(&now, 0); 401 expire_time = now.tv_sec + 20 * 60; 402 } else if (strncmp(argv[0], "proxy", 5) == 0) 403 flags |= RTF_ANNOUNCE; 404 argv++; 405 } 406 407 if (rtget(&sin, &sdl)) { 408 errx(1, "RTM_GET(%s) failed", host); 409 /* NOTREACHED */ 410 } 411 412 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 413 if (sdl->sdl_family == AF_LINK && 414 (rtm->rtm_flags & RTF_LLINFO) && 415 !(rtm->rtm_flags & RTF_GATEWAY)) { 416 switch (sdl->sdl_type) { 417 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 418 case IFT_ISO88024: case IFT_ISO88025: 419 goto overwrite; 420 } 421 } 422 /* 423 * IPv4 arp command retries with sin_other = SIN_PROXY here. 424 */ 425 fprintf(stderr, "set: cannot configure a new entry\n"); 426 return 1; 427 } 428 429 overwrite: 430 if (sdl->sdl_family != AF_LINK) { 431 printf("cannot intuit interface index and type for %s\n", host); 432 return (1); 433 } 434 sdl_m.sdl_type = sdl->sdl_type; 435 sdl_m.sdl_index = sdl->sdl_index; 436 return (rtmsg(RTM_ADD)); 437 } 438 439 /* 440 * Display an individual neighbor cache entry 441 */ 442 void 443 get(char *host) 444 { 445 struct sockaddr_in6 *sin = &sin_m; 446 struct addrinfo hints, *res; 447 int gai_error; 448 449 sin_m = blank_sin; 450 bzero(&hints, sizeof(hints)); 451 hints.ai_family = AF_INET6; 452 gai_error = getaddrinfo(host, NULL, &hints, &res); 453 if (gai_error) { 454 fprintf(stderr, "ndp: %s: %s\n", host, 455 gai_strerror(gai_error)); 456 return; 457 } 458 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 459 #ifdef __KAME__ 460 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 461 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 462 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 463 } 464 #endif 465 dump(&sin->sin6_addr, 0); 466 if (found_entry == 0) { 467 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 468 sizeof(host_buf), NULL ,0, 469 (nflag ? NI_NUMERICHOST : 0)); 470 printf("%s (%s) -- no entry\n", host, host_buf); 471 exit(1); 472 } 473 } 474 475 /* 476 * Delete a neighbor cache entry 477 */ 478 int 479 delete(char *host) 480 { 481 struct sockaddr_in6 *sin = &sin_m; 482 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 483 struct sockaddr_dl *sdl; 484 struct addrinfo hints, *res; 485 int gai_error; 486 487 getsocket(); 488 sin_m = blank_sin; 489 490 bzero(&hints, sizeof(hints)); 491 hints.ai_family = AF_INET6; 492 if (nflag) 493 hints.ai_flags = AI_NUMERICHOST; 494 gai_error = getaddrinfo(host, NULL, &hints, &res); 495 if (gai_error) { 496 fprintf(stderr, "ndp: %s: %s\n", host, 497 gai_strerror(gai_error)); 498 return 1; 499 } 500 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 501 #ifdef __KAME__ 502 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 503 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 504 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 505 } 506 #endif 507 508 if (rtget(&sin, &sdl)) { 509 errx(1, "RTM_GET(%s) failed", host); 510 /* NOTREACHED */ 511 } 512 513 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 514 if (sdl->sdl_family == AF_LINK && rtm->rtm_flags & RTF_LLINFO) { 515 if (rtm->rtm_flags & RTF_LOCAL) 516 return (0); 517 if (!(rtm->rtm_flags & RTF_GATEWAY)) 518 goto delete; 519 } 520 /* 521 * IPv4 arp command retries with sin_other = SIN_PROXY here. 522 */ 523 fprintf(stderr, "delete: cannot delete non-NDP entry\n"); 524 return 1; 525 } 526 527 delete: 528 if (sdl->sdl_family != AF_LINK) { 529 printf("cannot locate %s\n", host); 530 return (1); 531 } 532 if (rtmsg(RTM_DELETE) == 0) { 533 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */ 534 535 #ifdef __KAME__ 536 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) { 537 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]); 538 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0; 539 } 540 #endif 541 getnameinfo((struct sockaddr *)&s6, 542 s6.sin6_len, host_buf, 543 sizeof(host_buf), NULL, 0, 544 (nflag ? NI_NUMERICHOST : 0)); 545 printf("%s (%s) deleted\n", host, host_buf); 546 } 547 548 return 0; 549 } 550 551 #define W_ADDR 36 552 #define W_LL 17 553 #define W_IF 6 554 555 /* 556 * Dump the entire neighbor cache 557 */ 558 void 559 dump(struct in6_addr *addr, int cflag) 560 { 561 int mib[7]; 562 size_t needed; 563 char *lim, *buf = NULL, *next; 564 struct rt_msghdr *rtm; 565 struct sockaddr_in6 *sin; 566 struct sockaddr_dl *sdl; 567 struct in6_nbrinfo *nbi; 568 struct timeval now; 569 int addrwidth; 570 int llwidth; 571 int ifwidth; 572 char *ifname; 573 574 /* Print header */ 575 if (!tflag && !cflag) 576 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n", 577 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 578 W_IF, W_IF, "Netif", "Expire", "S", "Flags"); 579 580 again:; 581 lim = NULL; 582 mib[0] = CTL_NET; 583 mib[1] = PF_ROUTE; 584 mib[2] = 0; 585 mib[3] = AF_INET6; 586 mib[4] = NET_RT_FLAGS; 587 mib[5] = RTF_LLINFO; 588 mib[6] = rdomain; 589 while (1) { 590 if (sysctl(mib, 7, NULL, &needed, NULL, 0) == -1) 591 err(1, "sysctl(PF_ROUTE estimate)"); 592 if (needed == 0) 593 break; 594 if ((buf = realloc(buf, needed)) == NULL) 595 err(1, "realloc"); 596 if (sysctl(mib, 7, buf, &needed, NULL, 0) == -1) { 597 if (errno == ENOMEM) 598 continue; 599 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 600 } 601 lim = buf + needed; 602 break; 603 } 604 605 for (next = buf; next && lim && next < lim; next += rtm->rtm_msglen) { 606 int isrouter = 0, prbs = 0; 607 608 rtm = (struct rt_msghdr *)next; 609 if (rtm->rtm_version != RTM_VERSION) 610 continue; 611 sin = (struct sockaddr_in6 *)(next + rtm->rtm_hdrlen); 612 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len)); 613 614 /* 615 * Some OSes can produce a route that has the LINK flag but 616 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 617 * and BSD/OS, where xx is not the interface identifier on 618 * lo0). Such routes entry would annoy getnbrinfo() below, 619 * so we skip them. 620 * XXX: such routes should have the GATEWAY flag, not the 621 * LINK flag. However, there is rotten routing software 622 * that advertises all routes that have the GATEWAY flag. 623 * Thus, KAME kernel intentionally does not set the LINK flag. 624 * What is to be fixed is not ndp, but such routing software 625 * (and the kernel workaround)... 626 */ 627 if (sdl->sdl_family != AF_LINK) 628 continue; 629 630 if (!(rtm->rtm_flags & RTF_HOST)) 631 continue; 632 633 if (addr) { 634 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr)) 635 continue; 636 found_entry = 1; 637 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) 638 continue; 639 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) || 640 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) { 641 /* XXX: should scope id be filled in the kernel? */ 642 if (sin->sin6_scope_id == 0) 643 sin->sin6_scope_id = sdl->sdl_index; 644 #ifdef __KAME__ 645 /* KAME specific hack; removed the embedded id */ 646 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0; 647 #endif 648 } 649 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 650 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0)); 651 if (cflag) { 652 if (rtm->rtm_flags & RTF_CLONED) 653 delete(host_buf); 654 continue; 655 } 656 gettimeofday(&now, 0); 657 if (tflag) 658 ts_print(&now); 659 660 addrwidth = strlen(host_buf); 661 if (addrwidth < W_ADDR) 662 addrwidth = W_ADDR; 663 llwidth = strlen(ether_str(sdl)); 664 if (W_ADDR + W_LL - addrwidth > llwidth) 665 llwidth = W_ADDR + W_LL - addrwidth; 666 ifname = if_indextoname(sdl->sdl_index, ifix_buf); 667 if (!ifname) 668 ifname = "?"; 669 ifwidth = strlen(ifname); 670 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 671 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 672 673 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf, 674 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname); 675 676 /* Print neighbor discovery specific informations */ 677 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 678 if (nbi) { 679 if (nbi->expire > now.tv_sec) { 680 printf(" %-9.9s", 681 sec2str(nbi->expire - now.tv_sec)); 682 } else if (nbi->expire == 0) 683 printf(" %-9.9s", "permanent"); 684 else 685 printf(" %-9.9s", "expired"); 686 687 switch (nbi->state) { 688 case ND6_LLINFO_NOSTATE: 689 printf(" N"); 690 break; 691 case ND6_LLINFO_INCOMPLETE: 692 printf(" I"); 693 break; 694 case ND6_LLINFO_REACHABLE: 695 printf(" R"); 696 break; 697 case ND6_LLINFO_STALE: 698 printf(" S"); 699 break; 700 case ND6_LLINFO_DELAY: 701 printf(" D"); 702 break; 703 case ND6_LLINFO_PROBE: 704 printf(" P"); 705 break; 706 default: 707 printf(" ?"); 708 break; 709 } 710 711 isrouter = nbi->isrouter; 712 prbs = nbi->asked; 713 } else { 714 warnx("failed to get neighbor information"); 715 printf(" "); 716 } 717 718 printf(" %s%s%s", 719 (rtm->rtm_flags & RTF_LOCAL) ? "l" : "", 720 isrouter ? "R" : "", 721 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 722 723 if (prbs) 724 printf(" %d", prbs); 725 726 printf("\n"); 727 } 728 729 if (repeat) { 730 printf("\n"); 731 fflush(stdout); 732 sleep(repeat); 733 goto again; 734 } 735 736 free(buf); 737 } 738 739 static struct in6_nbrinfo * 740 getnbrinfo(struct in6_addr *addr, int ifindex, int warning) 741 { 742 static struct in6_nbrinfo nbi; 743 int s; 744 745 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 746 err(1, "socket"); 747 748 bzero(&nbi, sizeof(nbi)); 749 if_indextoname(ifindex, nbi.ifname); 750 nbi.addr = *addr; 751 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 752 if (warning) 753 warn("ioctl(SIOCGNBRINFO_IN6)"); 754 close(s); 755 return(NULL); 756 } 757 758 close(s); 759 return(&nbi); 760 } 761 762 static char * 763 ether_str(struct sockaddr_dl *sdl) 764 { 765 static char hbuf[NI_MAXHOST]; 766 u_char *cp; 767 768 if (sdl->sdl_alen) { 769 cp = (u_char *)LLADDR(sdl); 770 snprintf(hbuf, sizeof(hbuf), "%02x:%02x:%02x:%02x:%02x:%02x", 771 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 772 } else 773 snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 774 775 return(hbuf); 776 } 777 778 int 779 ndp_ether_aton(char *a, u_char *n) 780 { 781 int i, o[6]; 782 783 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 784 &o[3], &o[4], &o[5]); 785 if (i != 6) { 786 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 787 return (1); 788 } 789 for (i = 0; i < 6; i++) 790 n[i] = o[i]; 791 return (0); 792 } 793 794 void 795 usage(void) 796 { 797 printf("usage: ndp [-nrt] [-a | -c | -p] [-H | -P | -R] "); 798 printf("[-A wait] [-d hostname]\n"); 799 printf("\t[-f filename] [-i interface [flag ...]]\n"); 800 printf("\t[-s nodename ether_addr [temp] [proxy]] "); 801 printf("[-V rdomain] [hostname]\n"); 802 exit(1); 803 } 804 805 int 806 rtmsg(int cmd) 807 { 808 static int seq; 809 int rlen; 810 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 811 char *cp = m_rtmsg.m_space; 812 int l; 813 814 errno = 0; 815 if (cmd == RTM_DELETE) 816 goto doit; 817 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 818 rtm->rtm_flags = flags; 819 rtm->rtm_version = RTM_VERSION; 820 rtm->rtm_tableid = rdomain; 821 822 switch (cmd) { 823 default: 824 fprintf(stderr, "ndp: internal wrong cmd\n"); 825 exit(1); 826 case RTM_ADD: 827 rtm->rtm_addrs |= RTA_GATEWAY; 828 if (expire_time) { 829 rtm->rtm_rmx.rmx_expire = expire_time; 830 rtm->rtm_inits = RTV_EXPIRE; 831 } 832 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 833 #if 0 /* we don't support ipv6addr/128 type proxying. */ 834 if (rtm->rtm_flags & RTF_ANNOUNCE) { 835 rtm->rtm_flags &= ~RTF_HOST; 836 rtm->rtm_addrs |= RTA_NETMASK; 837 } 838 #endif 839 /* FALLTHROUGH */ 840 case RTM_GET: 841 rtm->rtm_addrs |= (RTA_DST | RTA_IFP); 842 } 843 #define NEXTADDR(w, s) \ 844 if (rtm->rtm_addrs & (w)) { \ 845 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));} 846 847 NEXTADDR(RTA_DST, sin_m); 848 NEXTADDR(RTA_GATEWAY, sdl_m); 849 #if 0 /* we don't support ipv6addr/128 type proxying. */ 850 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 851 NEXTADDR(RTA_NETMASK, so_mask); 852 #endif 853 NEXTADDR(RTA_IFP, ifp_m); 854 855 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 856 doit: 857 l = rtm->rtm_msglen; 858 rtm->rtm_seq = ++seq; 859 rtm->rtm_type = cmd; 860 if ((rlen = write(rtsock, (char *)&m_rtmsg, l)) < 0) { 861 if (errno != ESRCH || cmd != RTM_DELETE) { 862 err(1, "writing to routing socket"); 863 /* NOTREACHED */ 864 } 865 } 866 do { 867 l = read(rtsock, (char *)&m_rtmsg, sizeof(m_rtmsg)); 868 } while (l > 0 && (rtm->rtm_version != RTM_VERSION || 869 rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 870 if (l < 0) 871 (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 872 strerror(errno)); 873 return (0); 874 } 875 876 int 877 rtget(struct sockaddr_in6 **sinp, struct sockaddr_dl **sdlp) 878 { 879 struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 880 struct sockaddr_in6 *sin = NULL; 881 struct sockaddr_dl *sdl = NULL; 882 struct sockaddr *sa; 883 char *cp; 884 unsigned int i; 885 886 if (rtmsg(RTM_GET) < 0) 887 return (1); 888 889 if (rtm->rtm_addrs) { 890 cp = ((char *)rtm + rtm->rtm_hdrlen); 891 for (i = 1; i; i <<= 1) { 892 if (i & rtm->rtm_addrs) { 893 sa = (struct sockaddr *)cp; 894 switch (i) { 895 case RTA_DST: 896 sin = (struct sockaddr_in6 *)sa; 897 break; 898 case RTA_IFP: 899 sdl = (struct sockaddr_dl *)sa; 900 break; 901 default: 902 break; 903 } 904 cp += ROUNDUP(sa->sa_len); 905 } 906 } 907 } 908 909 if (sin == NULL || sdl == NULL) 910 return (1); 911 912 *sinp = sin; 913 *sdlp = sdl; 914 915 return (0); 916 } 917 918 void 919 ifinfo(char *ifname, int argc, char **argv) 920 { 921 struct in6_ndireq nd; 922 int i, s; 923 u_int32_t newflags; 924 925 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 926 err(1, "socket"); 927 /* NOTREACHED */ 928 } 929 bzero(&nd, sizeof(nd)); 930 strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 931 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 932 err(1, "ioctl(SIOCGIFINFO_IN6)"); 933 /* NOTREACHED */ 934 } 935 newflags = nd.ndi.flags; 936 for (i = 0; i < argc; i++) { 937 int clear = 0; 938 char *cp = argv[i]; 939 940 if (*cp == '-') { 941 clear = 1; 942 cp++; 943 } 944 945 #define SETFLAG(s, f) \ 946 do {\ 947 if (strcmp(cp, (s)) == 0) {\ 948 if (clear)\ 949 newflags &= ~(f);\ 950 else\ 951 newflags |= (f);\ 952 }\ 953 } while (0) 954 SETFLAG("nud", ND6_IFF_PERFORMNUD); 955 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 956 957 nd.ndi.flags = newflags; 958 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) { 959 err(1, "ioctl(SIOCSIFINFO_FLAGS)"); 960 /* NOTREACHED */ 961 } 962 #undef SETFLAG 963 } 964 965 if (!nd.ndi.initialized) { 966 errx(1, "%s: not initialized yet", ifname); 967 /* NOTREACHED */ 968 } 969 970 printf("basereachable=%ds%dms", 971 nd.ndi.basereachable / 1000, nd.ndi.basereachable % 1000); 972 printf(", reachable=%ds", nd.ndi.reachable); 973 printf(", retrans=%ds%dms", nd.ndi.retrans / 1000, 974 nd.ndi.retrans % 1000); 975 if (nd.ndi.flags) { 976 printf("\nFlags: "); 977 if ((nd.ndi.flags & ND6_IFF_PERFORMNUD)) 978 printf("nud "); 979 if ((nd.ndi.flags & ND6_IFF_ACCEPT_RTADV)) 980 printf("accept_rtadv "); 981 } 982 putc('\n', stdout); 983 984 close(s); 985 } 986 987 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 988 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 989 #endif 990 991 void 992 rtrlist(void) 993 { 994 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 995 char *buf; 996 struct in6_defrouter *p, *ep; 997 size_t l; 998 struct timeval now; 999 1000 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1001 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1002 /*NOTREACHED*/ 1003 } 1004 if (l == 0) 1005 return; 1006 buf = malloc(l); 1007 if (buf == NULL) { 1008 err(1, "malloc"); 1009 /*NOTREACHED*/ 1010 } 1011 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1012 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1013 /*NOTREACHED*/ 1014 } 1015 1016 ep = (struct in6_defrouter *)(buf + l); 1017 for (p = (struct in6_defrouter *)buf; p < ep; p++) { 1018 int rtpref; 1019 1020 if (getnameinfo((struct sockaddr *)&p->rtaddr, 1021 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 1022 (nflag ? NI_NUMERICHOST : 0)) != 0) 1023 strlcpy(host_buf, "?", sizeof(host_buf)); 1024 1025 printf("%s if=%s", host_buf, 1026 if_indextoname(p->if_index, ifix_buf)); 1027 printf(", flags=%s%s", 1028 p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1029 p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 1030 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 1031 printf(", pref=%s", rtpref_str[rtpref]); 1032 1033 gettimeofday(&now, 0); 1034 if (p->expire == 0) 1035 printf(", expire=Never\n"); 1036 else 1037 printf(", expire=%s\n", 1038 sec2str(p->expire - now.tv_sec)); 1039 } 1040 free(buf); 1041 } 1042 1043 void 1044 plist(void) 1045 { 1046 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 1047 char *buf, *p, *ep; 1048 struct in6_prefix pfx; 1049 size_t l; 1050 struct timeval now; 1051 const int niflags = NI_NUMERICHOST; 1052 int ninflags = nflag ? NI_NUMERICHOST : 0; 1053 char namebuf[NI_MAXHOST]; 1054 1055 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1056 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1057 /*NOTREACHED*/ 1058 } 1059 if (l == 0) 1060 return; 1061 buf = malloc(l); 1062 if (buf == NULL) { 1063 err(1, "malloc"); 1064 /*NOTREACHED*/ 1065 } 1066 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1067 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1068 /*NOTREACHED*/ 1069 } 1070 1071 ep = buf + l; 1072 for (p = buf; p < ep; ) { 1073 memcpy(&pfx, p, sizeof(pfx)); 1074 p += sizeof(pfx); 1075 1076 if (getnameinfo((struct sockaddr *)&pfx.prefix, 1077 pfx.prefix.sin6_len, namebuf, sizeof(namebuf), 1078 NULL, 0, niflags) != 0) 1079 strlcpy(namebuf, "?", sizeof(namebuf)); 1080 printf("%s/%d if=%s\n", namebuf, pfx.prefixlen, 1081 if_indextoname(pfx.if_index, ifix_buf)); 1082 1083 gettimeofday(&now, 0); 1084 /* 1085 * meaning of fields, especially flags, is very different 1086 * by origin. notify the difference to the users. 1087 */ 1088 printf("flags=%s%s%s%s%s", 1089 pfx.raflags.onlink ? "L" : "", 1090 pfx.raflags.autonomous ? "A" : "", 1091 (pfx.flags & NDPRF_ONLINK) != 0 ? "O" : "", 1092 (pfx.flags & NDPRF_DETACHED) != 0 ? "D" : "", 1093 (pfx.flags & NDPRF_HOME) != 0 ? "H" : "" 1094 ); 1095 if (pfx.vltime == ND6_INFINITE_LIFETIME) 1096 printf(" vltime=infinity"); 1097 else 1098 printf(" vltime=%lu", (unsigned long)pfx.vltime); 1099 if (pfx.pltime == ND6_INFINITE_LIFETIME) 1100 printf(", pltime=infinity"); 1101 else 1102 printf(", pltime=%lu", (unsigned long)pfx.pltime); 1103 if (pfx.expire == 0) 1104 printf(", expire=Never"); 1105 else if (pfx.expire >= now.tv_sec) 1106 printf(", expire=%s", 1107 sec2str(pfx.expire - now.tv_sec)); 1108 else 1109 printf(", expired"); 1110 printf(", ref=%d", pfx.refcnt); 1111 printf("\n"); 1112 /* 1113 * "advertising router" list is meaningful only if the prefix 1114 * information is from RA. 1115 */ 1116 if (pfx.advrtrs) { 1117 int j; 1118 struct sockaddr_in6 sin6; 1119 1120 printf(" advertised by\n"); 1121 for (j = 0; j < pfx.advrtrs && p <= ep; j++) { 1122 struct in6_nbrinfo *nbi; 1123 1124 memcpy(&sin6, p, sizeof(sin6)); 1125 p += sizeof(sin6); 1126 1127 if (getnameinfo((struct sockaddr *)&sin6, 1128 sin6.sin6_len, namebuf, sizeof(namebuf), 1129 NULL, 0, ninflags) != 0) 1130 strlcpy(namebuf, "?", sizeof(namebuf)); 1131 printf(" %s", namebuf); 1132 1133 nbi = getnbrinfo(&sin6.sin6_addr, 1134 pfx.if_index, 0); 1135 if (nbi) { 1136 switch (nbi->state) { 1137 case ND6_LLINFO_REACHABLE: 1138 case ND6_LLINFO_STALE: 1139 case ND6_LLINFO_DELAY: 1140 case ND6_LLINFO_PROBE: 1141 printf(" (reachable)\n"); 1142 break; 1143 default: 1144 printf(" (unreachable)\n"); 1145 } 1146 } else 1147 printf(" (no neighbor state)\n"); 1148 } 1149 } else 1150 printf(" No advertising router\n"); 1151 } 1152 free(buf); 1153 } 1154 1155 void 1156 pfx_flush(void) 1157 { 1158 char dummyif[IFNAMSIZ+8]; 1159 int s; 1160 1161 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1162 err(1, "socket"); 1163 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1164 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1165 err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 1166 close(s); 1167 } 1168 1169 void 1170 rtr_flush(void) 1171 { 1172 char dummyif[IFNAMSIZ+8]; 1173 int s; 1174 1175 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1176 err(1, "socket"); 1177 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1178 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1179 err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 1180 1181 close(s); 1182 } 1183 1184 void 1185 harmonize_rtr(void) 1186 { 1187 char dummyif[IFNAMSIZ+8]; 1188 int s; 1189 1190 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1191 err(1, "socket"); 1192 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1193 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1194 err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 1195 1196 close(s); 1197 } 1198 1199 static char * 1200 sec2str(time_t total) 1201 { 1202 static char result[256]; 1203 int days, hours, mins, secs; 1204 int first = 1; 1205 char *p = result; 1206 char *ep = &result[sizeof(result)]; 1207 int n; 1208 1209 days = total / 3600 / 24; 1210 hours = (total / 3600) % 24; 1211 mins = (total / 60) % 60; 1212 secs = total % 60; 1213 1214 if (days) { 1215 first = 0; 1216 n = snprintf(p, ep - p, "%dd", days); 1217 if (n < 0 || n >= ep - p) 1218 return "?"; 1219 p += n; 1220 } 1221 if (!first || hours) { 1222 first = 0; 1223 n = snprintf(p, ep - p, "%dh", hours); 1224 if (n < 0 || n >= ep - p) 1225 return "?"; 1226 p += n; 1227 } 1228 if (!first || mins) { 1229 first = 0; 1230 n = snprintf(p, ep - p, "%dm", mins); 1231 if (n < 0 || n >= ep - p) 1232 return "?"; 1233 p += n; 1234 } 1235 snprintf(p, ep - p, "%ds", secs); 1236 1237 return(result); 1238 } 1239 1240 /* 1241 * Print the timestamp 1242 * from tcpdump/util.c 1243 */ 1244 static void 1245 ts_print(const struct timeval *tvp) 1246 { 1247 int s; 1248 1249 /* Default */ 1250 s = (tvp->tv_sec + thiszone) % 86400; 1251 (void)printf("%02d:%02d:%02d.%06u ", 1252 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 1253 } 1254