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