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