1 /* $OpenBSD: ndp.c,v 1.53 2013/10/21 12:41:52 jmc 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/param.h> 78 #include <sys/file.h> 79 #include <sys/ioctl.h> 80 #include <sys/socket.h> 81 #include <sys/sysctl.h> 82 #include <sys/time.h> 83 #include <sys/queue.h> 84 85 #include <net/if.h> 86 #include <net/if_dl.h> 87 #include <net/if_types.h> 88 #include <net/route.h> 89 90 #include <netinet/in.h> 91 92 #include <netinet/icmp6.h> 93 #include <netinet6/in6_var.h> 94 #include <netinet6/nd6.h> 95 96 #include <arpa/inet.h> 97 98 #include <stdio.h> 99 #include <errno.h> 100 #include <fcntl.h> 101 #include <netdb.h> 102 #include <paths.h> 103 #include <stdlib.h> 104 #include <string.h> 105 #include <unistd.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 = atoi(optarg); 207 if (repeat < 0) { 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 && 510 (rtm->rtm_flags & RTF_LLINFO) && 511 !(rtm->rtm_flags & RTF_GATEWAY)) { 512 goto delete; 513 } 514 /* 515 * IPv4 arp command retries with sin_other = SIN_PROXY here. 516 */ 517 fprintf(stderr, "delete: cannot delete non-NDP entry\n"); 518 return 1; 519 } 520 521 delete: 522 if (sdl->sdl_family != AF_LINK) { 523 printf("cannot locate %s\n", host); 524 return (1); 525 } 526 if (rtmsg(RTM_DELETE) == 0) { 527 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */ 528 529 #ifdef __KAME__ 530 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) { 531 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]); 532 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0; 533 } 534 #endif 535 getnameinfo((struct sockaddr *)&s6, 536 s6.sin6_len, host_buf, 537 sizeof(host_buf), NULL, 0, 538 (nflag ? NI_NUMERICHOST : 0)); 539 printf("%s (%s) deleted\n", host, host_buf); 540 } 541 542 return 0; 543 } 544 545 #define W_ADDR 36 546 #define W_LL 17 547 #define W_IF 6 548 549 /* 550 * Dump the entire neighbor cache 551 */ 552 void 553 dump(struct in6_addr *addr, int cflag) 554 { 555 int mib[7]; 556 size_t needed; 557 char *lim, *buf = NULL, *next; 558 struct rt_msghdr *rtm; 559 struct sockaddr_in6 *sin; 560 struct sockaddr_dl *sdl; 561 struct in6_nbrinfo *nbi; 562 struct timeval now; 563 int addrwidth; 564 int llwidth; 565 int ifwidth; 566 char flgbuf[8]; 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 /* 713 * other flags. R: router, P: proxy, W: ?? 714 */ 715 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) { 716 snprintf(flgbuf, sizeof(flgbuf), "%s%s", 717 isrouter ? "R" : "", 718 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 719 } else { 720 sin = (struct sockaddr_in6 *) 721 (sdl->sdl_len + (char *)sdl); 722 #if 0 /* W and P are mystery even for us */ 723 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 724 isrouter ? "R" : "", 725 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "", 726 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "", 727 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 728 #else 729 snprintf(flgbuf, sizeof(flgbuf), "%s%s", 730 isrouter ? "R" : "", 731 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 732 #endif 733 } 734 printf(" %s", flgbuf); 735 736 if (prbs) 737 printf(" %d", prbs); 738 739 printf("\n"); 740 } 741 742 if (repeat) { 743 printf("\n"); 744 fflush(stdout); 745 sleep(repeat); 746 goto again; 747 } 748 749 free(buf); 750 } 751 752 static struct in6_nbrinfo * 753 getnbrinfo(struct in6_addr *addr, int ifindex, int warning) 754 { 755 static struct in6_nbrinfo nbi; 756 int s; 757 758 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 759 err(1, "socket"); 760 761 bzero(&nbi, sizeof(nbi)); 762 if_indextoname(ifindex, nbi.ifname); 763 nbi.addr = *addr; 764 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 765 if (warning) 766 warn("ioctl(SIOCGNBRINFO_IN6)"); 767 close(s); 768 return(NULL); 769 } 770 771 close(s); 772 return(&nbi); 773 } 774 775 static char * 776 ether_str(struct sockaddr_dl *sdl) 777 { 778 static char hbuf[NI_MAXHOST]; 779 u_char *cp; 780 781 if (sdl->sdl_alen) { 782 cp = (u_char *)LLADDR(sdl); 783 snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x", 784 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 785 } else 786 snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 787 788 return(hbuf); 789 } 790 791 int 792 ndp_ether_aton(char *a, u_char *n) 793 { 794 int i, o[6]; 795 796 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 797 &o[3], &o[4], &o[5]); 798 if (i != 6) { 799 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 800 return (1); 801 } 802 for (i = 0; i < 6; i++) 803 n[i] = o[i]; 804 return (0); 805 } 806 807 void 808 usage(void) 809 { 810 printf("usage: ndp [-nrt] [-a | -c | -p] [-H | -P | -R] "); 811 printf("[-A wait] [-d hostname]\n"); 812 printf("\t[-f filename] [-i interface [flag ...]]\n"); 813 printf("\t[-s nodename etheraddr [temp] [proxy]] "); 814 printf("[-V rdomain] [hostname]\n"); 815 exit(1); 816 } 817 818 int 819 rtmsg(int cmd) 820 { 821 static int seq; 822 int rlen; 823 struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 824 char *cp = m_rtmsg.m_space; 825 int l; 826 827 errno = 0; 828 if (cmd == RTM_DELETE) 829 goto doit; 830 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 831 rtm->rtm_flags = flags; 832 rtm->rtm_version = RTM_VERSION; 833 rtm->rtm_tableid = rdomain; 834 835 switch (cmd) { 836 default: 837 fprintf(stderr, "ndp: internal wrong cmd\n"); 838 exit(1); 839 case RTM_ADD: 840 rtm->rtm_addrs |= RTA_GATEWAY; 841 if (expire_time) { 842 rtm->rtm_rmx.rmx_expire = expire_time; 843 rtm->rtm_inits = RTV_EXPIRE; 844 } 845 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 846 #if 0 /* we don't support ipv6addr/128 type proxying. */ 847 if (rtm->rtm_flags & RTF_ANNOUNCE) { 848 rtm->rtm_flags &= ~RTF_HOST; 849 rtm->rtm_addrs |= RTA_NETMASK; 850 } 851 #endif 852 /* FALLTHROUGH */ 853 case RTM_GET: 854 rtm->rtm_addrs |= RTA_DST; 855 } 856 #define NEXTADDR(w, s) \ 857 if (rtm->rtm_addrs & (w)) { \ 858 bcopy((char *)&s, cp, sizeof(s)); cp += ROUNDUP(sizeof(s));} 859 860 NEXTADDR(RTA_DST, sin_m); 861 NEXTADDR(RTA_GATEWAY, sdl_m); 862 #if 0 /* we don't support ipv6addr/128 type proxying. */ 863 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 864 NEXTADDR(RTA_NETMASK, so_mask); 865 #endif 866 867 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 868 doit: 869 l = rtm->rtm_msglen; 870 rtm->rtm_seq = ++seq; 871 rtm->rtm_type = cmd; 872 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 873 if (errno != ESRCH || cmd != RTM_DELETE) { 874 err(1, "writing to routing socket"); 875 /* NOTREACHED */ 876 } 877 } 878 do { 879 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 880 } while (l > 0 && (rtm->rtm_version != RTM_VERSION || 881 rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 882 if (l < 0) 883 (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 884 strerror(errno)); 885 return (0); 886 } 887 888 void 889 ifinfo(char *ifname, int argc, char **argv) 890 { 891 struct in6_ndireq nd; 892 int i, s; 893 u_int32_t newflags; 894 895 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 896 err(1, "socket"); 897 /* NOTREACHED */ 898 } 899 bzero(&nd, sizeof(nd)); 900 strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 901 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 902 err(1, "ioctl(SIOCGIFINFO_IN6)"); 903 /* NOTREACHED */ 904 } 905 #define ND nd.ndi 906 newflags = ND.flags; 907 for (i = 0; i < argc; i++) { 908 int clear = 0; 909 char *cp = argv[i]; 910 911 if (*cp == '-') { 912 clear = 1; 913 cp++; 914 } 915 916 #define SETFLAG(s, f) \ 917 do {\ 918 if (strcmp(cp, (s)) == 0) {\ 919 if (clear)\ 920 newflags &= ~(f);\ 921 else\ 922 newflags |= (f);\ 923 }\ 924 } while (0) 925 SETFLAG("nud", ND6_IFF_PERFORMNUD); 926 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 927 928 ND.flags = newflags; 929 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) { 930 err(1, "ioctl(SIOCSIFINFO_FLAGS)"); 931 /* NOTREACHED */ 932 } 933 #undef SETFLAG 934 } 935 936 if (!ND.initialized) { 937 errx(1, "%s: not initialized yet", ifname); 938 /* NOTREACHED */ 939 } 940 941 printf("linkmtu=%d", ND.linkmtu); 942 printf(", curhlim=%d", ND.chlim); 943 printf(", basereachable=%ds%dms", 944 ND.basereachable / 1000, ND.basereachable % 1000); 945 printf(", reachable=%ds", ND.reachable); 946 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 947 if (ND.flags) { 948 printf("\nFlags: "); 949 if ((ND.flags & ND6_IFF_PERFORMNUD)) 950 printf("nud "); 951 if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 952 printf("accept_rtadv "); 953 } 954 putc('\n', stdout); 955 #undef ND 956 957 close(s); 958 } 959 960 #ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 961 #define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 962 #endif 963 964 void 965 rtrlist(void) 966 { 967 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 968 char *buf; 969 struct in6_defrouter *p, *ep; 970 size_t l; 971 struct timeval now; 972 973 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 974 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 975 /*NOTREACHED*/ 976 } 977 if (l == 0) 978 return; 979 buf = malloc(l); 980 if (buf == NULL) { 981 err(1, "malloc"); 982 /*NOTREACHED*/ 983 } 984 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 985 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 986 /*NOTREACHED*/ 987 } 988 989 ep = (struct in6_defrouter *)(buf + l); 990 for (p = (struct in6_defrouter *)buf; p < ep; p++) { 991 int rtpref; 992 993 if (getnameinfo((struct sockaddr *)&p->rtaddr, 994 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 995 (nflag ? NI_NUMERICHOST : 0)) != 0) 996 strlcpy(host_buf, "?", sizeof(host_buf)); 997 998 printf("%s if=%s", host_buf, 999 if_indextoname(p->if_index, ifix_buf)); 1000 printf(", flags=%s%s", 1001 p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1002 p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 1003 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 1004 printf(", pref=%s", rtpref_str[rtpref]); 1005 1006 gettimeofday(&now, 0); 1007 if (p->expire == 0) 1008 printf(", expire=Never\n"); 1009 else 1010 printf(", expire=%s\n", 1011 sec2str(p->expire - now.tv_sec)); 1012 } 1013 free(buf); 1014 } 1015 1016 void 1017 plist(void) 1018 { 1019 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 1020 char *buf; 1021 struct in6_prefix *p, *ep, *n; 1022 struct sockaddr_in6 *advrtr; 1023 size_t l; 1024 struct timeval now; 1025 const int niflags = NI_NUMERICHOST; 1026 int ninflags = nflag ? NI_NUMERICHOST : 0; 1027 char namebuf[NI_MAXHOST]; 1028 1029 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1030 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1031 /*NOTREACHED*/ 1032 } 1033 buf = malloc(l); 1034 if (buf == NULL) { 1035 err(1, "malloc"); 1036 /*NOTREACHED*/ 1037 } 1038 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1039 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1040 /*NOTREACHED*/ 1041 } 1042 1043 ep = (struct in6_prefix *)(buf + l); 1044 for (p = (struct in6_prefix *)buf; p < ep; p = n) { 1045 advrtr = (struct sockaddr_in6 *)(p + 1); 1046 n = (struct in6_prefix *)&advrtr[p->advrtrs]; 1047 1048 if (getnameinfo((struct sockaddr *)&p->prefix, 1049 p->prefix.sin6_len, namebuf, sizeof(namebuf), 1050 NULL, 0, niflags) != 0) 1051 strlcpy(namebuf, "?", sizeof(namebuf)); 1052 printf("%s/%d if=%s\n", namebuf, p->prefixlen, 1053 if_indextoname(p->if_index, ifix_buf)); 1054 1055 gettimeofday(&now, 0); 1056 /* 1057 * meaning of fields, especially flags, is very different 1058 * by origin. notify the difference to the users. 1059 */ 1060 printf("flags=%s%s%s%s%s", 1061 p->raflags.onlink ? "L" : "", 1062 p->raflags.autonomous ? "A" : "", 1063 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "", 1064 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "", 1065 (p->flags & NDPRF_HOME) != 0 ? "H" : "" 1066 ); 1067 if (p->vltime == ND6_INFINITE_LIFETIME) 1068 printf(" vltime=infinity"); 1069 else 1070 printf(" vltime=%lu", (unsigned long)p->vltime); 1071 if (p->pltime == ND6_INFINITE_LIFETIME) 1072 printf(", pltime=infinity"); 1073 else 1074 printf(", pltime=%lu", (unsigned long)p->pltime); 1075 if (p->expire == 0) 1076 printf(", expire=Never"); 1077 else if (p->expire >= now.tv_sec) 1078 printf(", expire=%s", 1079 sec2str(p->expire - now.tv_sec)); 1080 else 1081 printf(", expired"); 1082 printf(", ref=%d", p->refcnt); 1083 printf("\n"); 1084 /* 1085 * "advertising router" list is meaningful only if the prefix 1086 * information is from RA. 1087 */ 1088 if (p->advrtrs) { 1089 int j; 1090 struct sockaddr_in6 *sin6; 1091 1092 sin6 = advrtr; 1093 printf(" advertised by\n"); 1094 for (j = 0; j < p->advrtrs; j++) { 1095 struct in6_nbrinfo *nbi; 1096 1097 if (getnameinfo((struct sockaddr *)sin6, 1098 sin6->sin6_len, namebuf, sizeof(namebuf), 1099 NULL, 0, ninflags) != 0) 1100 strlcpy(namebuf, "?", sizeof(namebuf)); 1101 printf(" %s", namebuf); 1102 1103 nbi = getnbrinfo(&sin6->sin6_addr, 1104 p->if_index, 0); 1105 if (nbi) { 1106 switch (nbi->state) { 1107 case ND6_LLINFO_REACHABLE: 1108 case ND6_LLINFO_STALE: 1109 case ND6_LLINFO_DELAY: 1110 case ND6_LLINFO_PROBE: 1111 printf(" (reachable)\n"); 1112 break; 1113 default: 1114 printf(" (unreachable)\n"); 1115 } 1116 } else 1117 printf(" (no neighbor state)\n"); 1118 sin6++; 1119 } 1120 } else 1121 printf(" No advertising router\n"); 1122 } 1123 free(buf); 1124 } 1125 1126 void 1127 pfx_flush(void) 1128 { 1129 char dummyif[IFNAMSIZ+8]; 1130 int s; 1131 1132 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1133 err(1, "socket"); 1134 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1135 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1136 err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 1137 close(s); 1138 } 1139 1140 void 1141 rtr_flush(void) 1142 { 1143 char dummyif[IFNAMSIZ+8]; 1144 int s; 1145 1146 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1147 err(1, "socket"); 1148 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1149 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1150 err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 1151 1152 close(s); 1153 } 1154 1155 void 1156 harmonize_rtr(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, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1165 err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 1166 1167 close(s); 1168 } 1169 1170 static char * 1171 sec2str(time_t total) 1172 { 1173 static char result[256]; 1174 int days, hours, mins, secs; 1175 int first = 1; 1176 char *p = result; 1177 char *ep = &result[sizeof(result)]; 1178 int n; 1179 1180 days = total / 3600 / 24; 1181 hours = (total / 3600) % 24; 1182 mins = (total / 60) % 60; 1183 secs = total % 60; 1184 1185 if (days) { 1186 first = 0; 1187 n = snprintf(p, ep - p, "%dd", days); 1188 if (n < 0 || n >= ep - p) 1189 return "?"; 1190 p += n; 1191 } 1192 if (!first || hours) { 1193 first = 0; 1194 n = snprintf(p, ep - p, "%dh", hours); 1195 if (n < 0 || n >= ep - p) 1196 return "?"; 1197 p += n; 1198 } 1199 if (!first || mins) { 1200 first = 0; 1201 n = snprintf(p, ep - p, "%dm", mins); 1202 if (n < 0 || n >= ep - p) 1203 return "?"; 1204 p += n; 1205 } 1206 snprintf(p, ep - p, "%ds", secs); 1207 1208 return(result); 1209 } 1210 1211 /* 1212 * Print the timestamp 1213 * from tcpdump/util.c 1214 */ 1215 static void 1216 ts_print(const struct timeval *tvp) 1217 { 1218 int s; 1219 1220 /* Default */ 1221 s = (tvp->tv_sec + thiszone) % 86400; 1222 (void)printf("%02d:%02d:%02d.%06u ", 1223 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 1224 } 1225