ndp.c revision 121162
1/* $FreeBSD: head/usr.sbin/ndp/ndp.c 121162 2003-10-17 16:17:47Z ume $ */ 2/* $KAME: ndp.c,v 1.65 2001/05/08 04:36:34 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. All advertising materials mentioning features or use of this software 48 * must display the following acknowledgement: 49 * This product includes software developed by the University of 50 * California, Berkeley and its contributors. 51 * 4. Neither the name of the University nor the names of its contributors 52 * may be used to endorse or promote products derived from this software 53 * without specific prior written permission. 54 * 55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 65 * SUCH DAMAGE. 66 */ 67 68/* 69 * Based on: 70 * "@(#) Copyright (c) 1984, 1993\n\ 71 * The Regents of the University of California. All rights reserved.\n"; 72 * 73 * "@(#)arp.c 8.2 (Berkeley) 1/2/94"; 74 */ 75 76/* 77 * ndp - display, set, delete and flush neighbor cache 78 */ 79 80 81#include <sys/param.h> 82#include <sys/file.h> 83#include <sys/ioctl.h> 84#include <sys/socket.h> 85#include <sys/sysctl.h> 86#include <sys/time.h> 87#include <sys/queue.h> 88 89#include <net/if.h> 90#include <net/if_var.h> 91#include <net/if_dl.h> 92#include <net/if_types.h> 93#include <net/route.h> 94 95#include <netinet/in.h> 96#include <netinet/if_ether.h> 97 98#include <netinet/icmp6.h> 99#include <netinet6/in6_var.h> 100#include <netinet6/nd6.h> 101 102#include <arpa/inet.h> 103 104#include <netdb.h> 105#include <errno.h> 106#include <nlist.h> 107#include <stdio.h> 108#include <string.h> 109#include <paths.h> 110#include <err.h> 111#include <stdlib.h> 112#include <fcntl.h> 113#include <unistd.h> 114#include "gmt2local.h" 115 116/* packing rule for routing socket */ 117#define ROUNDUP(a) \ 118 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 119#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 120 121static pid_t pid; 122static int cflag; 123static int nflag; 124static int tflag; 125static int32_t thiszone; /* time difference with gmt */ 126static int s = -1; 127static int repeat = 0; 128 129char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */ 130char host_buf[NI_MAXHOST]; /* getnameinfo() */ 131char ifix_buf[IFNAMSIZ]; /* if_indextoname() */ 132 133int main __P((int, char **)); 134int file __P((char *)); 135void getsocket __P((void)); 136int set __P((int, char **)); 137void get __P((char *)); 138int delete __P((char *)); 139void dump __P((struct in6_addr *)); 140static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr, 141 int ifindex, int)); 142static char *ether_str __P((struct sockaddr_dl *)); 143int ndp_ether_aton __P((char *, u_char *)); 144void usage __P((void)); 145int rtmsg __P((int)); 146void ifinfo __P((int, char **)); 147void rtrlist __P((void)); 148void plist __P((void)); 149void pfx_flush __P((void)); 150void rtrlist __P((void)); 151void rtr_flush __P((void)); 152void harmonize_rtr __P((void)); 153#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 154static void getdefif __P((void)); 155static void setdefif __P((char *)); 156#endif 157static char *sec2str __P((time_t t)); 158static char *ether_str __P((struct sockaddr_dl *sdl)); 159static void ts_print __P((const struct timeval *)); 160 161static char *rtpref_str[] = { 162 "medium", /* 00 */ 163 "high", /* 01 */ 164 "rsv", /* 10 */ 165 "low" /* 11 */ 166}; 167 168int 169main(argc, argv) 170 int argc; 171 char **argv; 172{ 173 int ch; 174 int aflag = 0, dflag = 0, sflag = 0, Hflag = 0, 175 pflag = 0, rflag = 0, Pflag = 0, Rflag = 0; 176 177 pid = getpid(); 178 thiszone = gmt2local(0); 179 while ((ch = getopt(argc, argv, "acndfIilprstA:HPR")) != -1) 180 switch (ch) { 181 case 'a': 182 aflag = 1; 183 break; 184 case 'c': 185 cflag = 1; 186 break; 187 case 'd': 188 dflag = 1; 189 break; 190 case 'I': 191#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 192 if (argc > 2) 193 setdefif(argv[2]); 194 getdefif(); /* always call it to print the result */ 195 exit(0); 196#else 197 errx(1, "not supported yet"); 198 /*NOTREACHED*/ 199#endif 200 case 'i' : 201 argc -= optind; 202 argv += optind; 203 if (argc < 1) 204 usage(); 205 ifinfo(argc, argv); 206 exit(0); 207 case 'n': 208 nflag = 1; 209 continue; 210 case 'p': 211 pflag = 1; 212 break; 213 case 'f' : 214 if (argc != 3) 215 usage(); 216 file(argv[2]); 217 exit(0); 218 case 'l' : 219 /* obsolete, ignored */ 220 break; 221 case 'r' : 222 rflag = 1; 223 break; 224 case 's': 225 sflag = 1; 226 break; 227 case 't': 228 tflag = 1; 229 break; 230 case 'A': 231 aflag = 1; 232 repeat = atoi(optarg); 233 if (repeat < 0) 234 usage(); 235 break; 236 case 'H' : 237 Hflag = 1; 238 break; 239 case 'P': 240 Pflag = 1; 241 break; 242 case 'R': 243 Rflag = 1; 244 break; 245 default: 246 usage(); 247 } 248 249 argc -= optind; 250 argv += optind; 251 252 if (aflag || cflag) { 253 dump(0); 254 exit(0); 255 } 256 if (dflag) { 257 if (argc != 1) 258 usage(); 259 delete(argv[0]); 260 exit(0); 261 } 262 if (pflag) { 263 plist(); 264 exit(0); 265 } 266 if (rflag) { 267 rtrlist(); 268 exit(0); 269 } 270 if (sflag) { 271 if (argc < 2 || argc > 4) 272 usage(); 273 exit(set(argc, argv) ? 1 : 0); 274 } 275 if (Hflag) { 276 harmonize_rtr(); 277 exit(0); 278 } 279 if (Pflag) { 280 pfx_flush(); 281 exit(0); 282 } 283 if (Rflag) { 284 rtr_flush(); 285 exit(0); 286 } 287 288 if (argc != 1) 289 usage(); 290 get(argv[0]); 291 exit(0); 292} 293 294/* 295 * Process a file to set standard ndp entries 296 */ 297int 298file(name) 299 char *name; 300{ 301 FILE *fp; 302 int i, retval; 303 char line[100], arg[5][50], *args[5]; 304 305 if ((fp = fopen(name, "r")) == NULL) { 306 fprintf(stderr, "ndp: cannot open %s\n", name); 307 exit(1); 308 } 309 args[0] = &arg[0][0]; 310 args[1] = &arg[1][0]; 311 args[2] = &arg[2][0]; 312 args[3] = &arg[3][0]; 313 args[4] = &arg[4][0]; 314 retval = 0; 315 while (fgets(line, 100, fp) != NULL) { 316 i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2], 317 arg[3], arg[4]); 318 if (i < 2) { 319 fprintf(stderr, "ndp: bad line: %s\n", line); 320 retval = 1; 321 continue; 322 } 323 if (set(i, args)) 324 retval = 1; 325 } 326 fclose(fp); 327 return (retval); 328} 329 330void 331getsocket() 332{ 333 if (s < 0) { 334 s = socket(PF_ROUTE, SOCK_RAW, 0); 335 if (s < 0) { 336 err(1, "socket"); 337 /* NOTREACHED */ 338 } 339 } 340} 341 342struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 }; 343struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m; 344struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 345int expire_time, flags, found_entry; 346struct { 347 struct rt_msghdr m_rtm; 348 char m_space[512]; 349} m_rtmsg; 350 351/* 352 * Set an individual neighbor cache entry 353 */ 354int 355set(argc, argv) 356 int argc; 357 char **argv; 358{ 359 register struct sockaddr_in6 *sin = &sin_m; 360 register struct sockaddr_dl *sdl; 361 register 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 flags = expire_time = 0; 392 while (argc-- > 0) { 393 if (strncmp(argv[0], "temp", 4) == 0) { 394 struct timeval time; 395 396 gettimeofday(&time, 0); 397 expire_time = time.tv_sec + 20 * 60; 398 } else if (strncmp(argv[0], "proxy", 5) == 0) 399 flags |= RTF_ANNOUNCE; 400 argv++; 401 } 402 if (rtmsg(RTM_GET) < 0) { 403 errx(1, "RTM_GET(%s) failed", host); 404 /* NOTREACHED */ 405 } 406 sin = (struct sockaddr_in6 *)(rtm + 1); 407 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin); 408 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 409 if (sdl->sdl_family == AF_LINK && 410 (rtm->rtm_flags & RTF_LLINFO) && 411 !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) { 412 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 413 case IFT_ISO88024: case IFT_ISO88025: 414 goto overwrite; 415 } 416 /* 417 * IPv4 arp command retries with sin_other = SIN_PROXY here. 418 */ 419 fprintf(stderr, "set: cannot configure a new entry\n"); 420 return 1; 421 } 422 423overwrite: 424 if (sdl->sdl_family != AF_LINK) { 425 printf("cannot intuit interface index and type for %s\n", host); 426 return (1); 427 } 428 sdl_m.sdl_type = sdl->sdl_type; 429 sdl_m.sdl_index = sdl->sdl_index; 430 return (rtmsg(RTM_ADD)); 431} 432 433/* 434 * Display an individual neighbor cache entry 435 */ 436void 437get(host) 438 char *host; 439{ 440 struct sockaddr_in6 *sin = &sin_m; 441 struct addrinfo hints, *res; 442 int gai_error; 443 444 sin_m = blank_sin; 445 bzero(&hints, sizeof(hints)); 446 hints.ai_family = AF_INET6; 447 gai_error = getaddrinfo(host, NULL, &hints, &res); 448 if (gai_error) { 449 fprintf(stderr, "ndp: %s: %s\n", host, 450 gai_strerror(gai_error)); 451 return; 452 } 453 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 454#ifdef __KAME__ 455 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 456 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 457 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 458 } 459#endif 460 dump(&sin->sin6_addr); 461 if (found_entry == 0) { 462 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 463 sizeof(host_buf), NULL ,0, 464 (nflag ? NI_NUMERICHOST : 0)); 465 printf("%s (%s) -- no entry\n", host, host_buf); 466 exit(1); 467 } 468} 469 470/* 471 * Delete a neighbor cache entry 472 */ 473int 474delete(host) 475 char *host; 476{ 477 struct sockaddr_in6 *sin = &sin_m; 478 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 479 struct sockaddr_dl *sdl; 480 struct addrinfo hints, *res; 481 int gai_error; 482 483 getsocket(); 484 sin_m = blank_sin; 485 486 bzero(&hints, sizeof(hints)); 487 hints.ai_family = AF_INET6; 488 gai_error = getaddrinfo(host, NULL, &hints, &res); 489 if (gai_error) { 490 fprintf(stderr, "ndp: %s: %s\n", host, 491 gai_strerror(gai_error)); 492 return 1; 493 } 494 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 495#ifdef __KAME__ 496 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { 497 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 498 htons(((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id); 499 } 500#endif 501 if (rtmsg(RTM_GET) < 0) { 502 errx(1, "RTM_GET(%s) failed", host); 503 /* NOTREACHED */ 504 } 505 sin = (struct sockaddr_in6 *)(rtm + 1); 506 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin); 507 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 508 if (sdl->sdl_family == AF_LINK && 509 (rtm->rtm_flags & RTF_LLINFO) && 510 !(rtm->rtm_flags & RTF_GATEWAY)) { 511 goto delete; 512 } 513 /* 514 * IPv4 arp command retries with sin_other = SIN_PROXY here. 515 */ 516 fprintf(stderr, "delete: cannot delete non-NDP entry\n"); 517 return 1; 518 } 519 520delete: 521 if (sdl->sdl_family != AF_LINK) { 522 printf("cannot locate %s\n", host); 523 return (1); 524 } 525 if (rtmsg(RTM_DELETE) == 0) { 526 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */ 527 528#ifdef __KAME__ 529 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) { 530 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]); 531 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0; 532 } 533#endif 534 getnameinfo((struct sockaddr *)&s6, 535 s6.sin6_len, host_buf, 536 sizeof(host_buf), NULL, 0, 537 (nflag ? NI_NUMERICHOST : 0)); 538 printf("%s (%s) deleted\n", host, host_buf); 539 } 540 541 return 0; 542} 543 544#define W_ADDR 31 545#define W_LL 17 546#define W_IF 6 547 548/* 549 * Dump the entire neighbor cache 550 */ 551void 552dump(addr) 553 struct in6_addr *addr; 554{ 555 int mib[6]; 556 size_t needed; 557 char *lim, *buf, *next; 558 struct rt_msghdr *rtm; 559 struct sockaddr_in6 *sin; 560 struct sockaddr_dl *sdl; 561 extern int h_errno; 562 struct in6_nbrinfo *nbi; 563 struct timeval time; 564 int addrwidth; 565 int llwidth; 566 int ifwidth; 567 char flgbuf[8]; 568 char *ifname; 569 570 /* Print header */ 571 if (!tflag && !cflag) 572 printf("%-*.*s %-*.*s %*.*s %-9.9s %2s %4s %4s\n", 573 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 574 W_IF, W_IF, "Netif", "Expire", "St", "Flgs", "Prbs"); 575 576again:; 577 mib[0] = CTL_NET; 578 mib[1] = PF_ROUTE; 579 mib[2] = 0; 580 mib[3] = AF_INET6; 581 mib[4] = NET_RT_FLAGS; 582 mib[5] = RTF_LLINFO; 583 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 584 err(1, "sysctl(PF_ROUTE estimate)"); 585 if (needed > 0) { 586 if ((buf = malloc(needed)) == NULL) 587 err(1, "malloc"); 588 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 589 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 590 lim = buf + needed; 591 } else 592 buf = lim = NULL; 593 594 for (next = buf; next && next < lim; next += rtm->rtm_msglen) { 595 int isrouter = 0, prbs = 0; 596 597 rtm = (struct rt_msghdr *)next; 598 sin = (struct sockaddr_in6 *)(rtm + 1); 599 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len)); 600 601 /* 602 * Some OSes can produce a route that has the LINK flag but 603 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 604 * and BSD/OS, where xx is not the interface identifier on 605 * lo0). Such routes entry would annoy getnbrinfo() below, 606 * so we skip them. 607 * XXX: such routes should have the GATEWAY flag, not the 608 * LINK flag. However, there is rotten routing software 609 * that advertises all routes that have the GATEWAY flag. 610 * Thus, KAME kernel intentionally does not set the LINK flag. 611 * What is to be fixed is not ndp, but such routing software 612 * (and the kernel workaround)... 613 */ 614 if (sdl->sdl_family != AF_LINK) 615 continue; 616 617 if (addr) { 618 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr)) 619 continue; 620 found_entry = 1; 621 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) 622 continue; 623 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) || 624 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) { 625 /* XXX: should scope id be filled in the kernel? */ 626 if (sin->sin6_scope_id == 0) 627 sin->sin6_scope_id = sdl->sdl_index; 628#ifdef __KAME__ 629 /* KAME specific hack; removed the embedded id */ 630 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0; 631#endif 632 } 633 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 634 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0)); 635 if (cflag == 1) { 636#ifdef RTF_WASCLONED 637 if (rtm->rtm_flags & RTF_WASCLONED) 638 delete(host_buf); 639#else 640 delete(host_buf); 641#endif 642 continue; 643 } 644 gettimeofday(&time, 0); 645 if (tflag) 646 ts_print(&time); 647 648 addrwidth = strlen(host_buf); 649 if (addrwidth < W_ADDR) 650 addrwidth = W_ADDR; 651 llwidth = strlen(ether_str(sdl)); 652 if (W_ADDR + W_LL - addrwidth > llwidth) 653 llwidth = W_ADDR + W_LL - addrwidth; 654 ifname = if_indextoname(sdl->sdl_index, ifix_buf); 655 if (!ifname) 656 ifname = "?"; 657 ifwidth = strlen(ifname); 658 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 659 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 660 661 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf, 662 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname); 663 664 /* Print neighbor discovery specific informations */ 665 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 666 if (nbi) { 667 if (nbi->expire > time.tv_sec) { 668 printf(" %-9.9s", 669 sec2str(nbi->expire - time.tv_sec)); 670 } else if (nbi->expire == 0) 671 printf(" %-9.9s", "permanent"); 672 else 673 printf(" %-9.9s", "expired"); 674 675 switch (nbi->state) { 676 case ND6_LLINFO_NOSTATE: 677 printf(" N"); 678 break; 679#ifdef ND6_LLINFO_WAITDELETE 680 case ND6_LLINFO_WAITDELETE: 681 printf(" W"); 682 break; 683#endif 684 case ND6_LLINFO_INCOMPLETE: 685 printf(" I"); 686 break; 687 case ND6_LLINFO_REACHABLE: 688 printf(" R"); 689 break; 690 case ND6_LLINFO_STALE: 691 printf(" S"); 692 break; 693 case ND6_LLINFO_DELAY: 694 printf(" D"); 695 break; 696 case ND6_LLINFO_PROBE: 697 printf(" P"); 698 break; 699 default: 700 printf(" ?"); 701 break; 702 } 703 704 isrouter = nbi->isrouter; 705 prbs = nbi->asked; 706 } else { 707 warnx("failed to get neighbor information"); 708 printf(" "); 709 } 710 putchar(' '); 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 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 723 isrouter ? "R" : "", 724 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "", 725 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "", 726 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 727 } 728 printf(" %-4.4s", flgbuf); 729 730 if (prbs) 731 printf(" %4d", prbs); 732 733 printf("\n"); 734 } 735 if (buf != NULL) 736 free(buf); 737 738 if (repeat) { 739 printf("\n"); 740 sleep(repeat); 741 goto again; 742 } 743} 744 745static struct in6_nbrinfo * 746getnbrinfo(addr, ifindex, warning) 747 struct in6_addr *addr; 748 int ifindex; 749 int warning; 750{ 751 static struct in6_nbrinfo nbi; 752 int s; 753 754 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 755 err(1, "socket"); 756 757 bzero(&nbi, sizeof(nbi)); 758 if_indextoname(ifindex, nbi.ifname); 759 nbi.addr = *addr; 760 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 761 if (warning) 762 warn("ioctl(SIOCGNBRINFO_IN6)"); 763 close(s); 764 return(NULL); 765 } 766 767 close(s); 768 return(&nbi); 769} 770 771static char * 772ether_str(sdl) 773 struct sockaddr_dl *sdl; 774{ 775 static char hbuf[NI_MAXHOST]; 776 u_char *cp; 777 778 if (sdl->sdl_alen) { 779 cp = (u_char *)LLADDR(sdl); 780 snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x", 781 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 782 } else 783 snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 784 785 return(hbuf); 786} 787 788int 789ndp_ether_aton(a, n) 790 char *a; 791 u_char *n; 792{ 793 int i, o[6]; 794 795 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 796 &o[3], &o[4], &o[5]); 797 if (i != 6) { 798 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 799 return (1); 800 } 801 for (i = 0; i < 6; i++) 802 n[i] = o[i]; 803 return (0); 804} 805 806void 807usage() 808{ 809 printf("usage: ndp hostname\n"); 810 printf(" ndp -a[nt]\n"); 811 printf(" ndp [-nt] -A wait\n"); 812 printf(" ndp -c[nt]\n"); 813 printf(" ndp -d[nt] hostname\n"); 814 printf(" ndp -f[nt] filename\n"); 815 printf(" ndp -i interface [flags...]\n"); 816#ifdef SIOCSDEFIFACE_IN6 817 printf(" ndp -I [interface|delete]\n"); 818#endif 819 printf(" ndp -p\n"); 820 printf(" ndp -r\n"); 821 printf(" ndp -s hostname ether_addr [temp] [proxy]\n"); 822 printf(" ndp -H\n"); 823 printf(" ndp -P\n"); 824 printf(" ndp -R\n"); 825 exit(1); 826} 827 828int 829rtmsg(cmd) 830 int cmd; 831{ 832 static int seq; 833 int rlen; 834 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 835 register char *cp = m_rtmsg.m_space; 836 register int l; 837 838 errno = 0; 839 if (cmd == RTM_DELETE) 840 goto doit; 841 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 842 rtm->rtm_flags = flags; 843 rtm->rtm_version = RTM_VERSION; 844 845 switch (cmd) { 846 default: 847 fprintf(stderr, "ndp: internal wrong cmd\n"); 848 exit(1); 849 case RTM_ADD: 850 rtm->rtm_addrs |= RTA_GATEWAY; 851 rtm->rtm_rmx.rmx_expire = expire_time; 852 rtm->rtm_inits = RTV_EXPIRE; 853 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 854 if (rtm->rtm_flags & RTF_ANNOUNCE) { 855 rtm->rtm_flags &= ~RTF_HOST; 856 rtm->rtm_flags |= RTA_NETMASK; 857 } 858 /* FALLTHROUGH */ 859 case RTM_GET: 860 rtm->rtm_addrs |= RTA_DST; 861 } 862#define NEXTADDR(w, s) \ 863 if (rtm->rtm_addrs & (w)) { \ 864 bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);} 865 866 NEXTADDR(RTA_DST, sin_m); 867 NEXTADDR(RTA_GATEWAY, sdl_m); 868 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 869 NEXTADDR(RTA_NETMASK, so_mask); 870 871 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 872doit: 873 l = rtm->rtm_msglen; 874 rtm->rtm_seq = ++seq; 875 rtm->rtm_type = cmd; 876 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 877 if (errno != ESRCH || cmd != RTM_DELETE) { 878 err(1, "writing to routing socket"); 879 /* NOTREACHED */ 880 } 881 } 882 do { 883 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 884 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 885 if (l < 0) 886 (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 887 strerror(errno)); 888 return (0); 889} 890 891void 892ifinfo(argc, argv) 893 int argc; 894 char **argv; 895{ 896 struct in6_ndireq nd; 897 int i, s; 898 char *ifname = argv[0]; 899 u_int32_t newflags; 900#ifdef IPV6CTL_USETEMPADDR 901 u_int8_t nullbuf[8]; 902#endif 903 904 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 905 err(1, "socket"); 906 /* NOTREACHED */ 907 } 908 bzero(&nd, sizeof(nd)); 909 strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 910 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 911 err(1, "ioctl(SIOCGIFINFO_IN6)"); 912 /* NOTREACHED */ 913 } 914#define ND nd.ndi 915 newflags = ND.flags; 916 for (i = 1; i < argc; i++) { 917 int clear = 0; 918 char *cp = argv[i]; 919 920 if (*cp == '-') { 921 clear = 1; 922 cp++; 923 } 924 925#define SETFLAG(s, f) \ 926 do {\ 927 if (strcmp(cp, (s)) == 0) {\ 928 if (clear)\ 929 newflags &= ~(f);\ 930 else\ 931 newflags |= (f);\ 932 }\ 933 } while (0) 934 SETFLAG("nud", ND6_IFF_PERFORMNUD); 935#ifdef ND6_IFF_ACCEPT_RTADV 936 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 937#endif 938 939 ND.flags = newflags; 940 if (ioctl(s, SIOCSIFINFO_FLAGS, (caddr_t)&nd) < 0) { 941 err(1, "ioctl(SIOCSIFINFO_FLAGS)"); 942 /* NOTREACHED */ 943 } 944#undef SETFLAG 945 } 946 947 if (!ND.initialized) { 948 errx(1, "%s: not initialized yet", ifname); 949 /* NOTREACHED */ 950 } 951 952 printf("linkmtu=%d", ND.linkmtu); 953 printf(", curhlim=%d", ND.chlim); 954 printf(", basereachable=%ds%dms", 955 ND.basereachable / 1000, ND.basereachable % 1000); 956 printf(", reachable=%ds", ND.reachable); 957 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 958#ifdef IPV6CTL_USETEMPADDR 959 memset(nullbuf, 0, sizeof(nullbuf)); 960 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 961 int j; 962 u_int8_t *rbuf; 963 964 for (i = 0; i < 3; i++) { 965 switch (i) { 966 case 0: 967 printf("\nRandom seed(0): "); 968 rbuf = ND.randomseed0; 969 break; 970 case 1: 971 printf("\nRandom seed(1): "); 972 rbuf = ND.randomseed1; 973 break; 974 case 2: 975 printf("\nRandom ID: "); 976 rbuf = ND.randomid; 977 break; 978 } 979 for (j = 0; j < 8; j++) 980 printf("%02x", rbuf[j]); 981 } 982 } 983#endif 984 if (ND.flags) { 985 printf("\nFlags: "); 986 if ((ND.flags & ND6_IFF_PERFORMNUD)) 987 printf("nud "); 988#ifdef ND6_IFF_ACCEPT_RTADV 989 if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 990 printf("accept_rtadv "); 991#endif 992} 993 putc('\n', stdout); 994#undef ND 995 996 close(s); 997} 998 999#ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 1000#define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 1001#endif 1002 1003void 1004rtrlist() 1005{ 1006#ifdef ICMPV6CTL_ND6_DRLIST 1007 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 1008 char *buf; 1009 struct in6_defrouter *p, *ep; 1010 size_t l; 1011 struct timeval time; 1012 1013 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1014 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1015 /*NOTREACHED*/ 1016 } 1017 buf = malloc(l); 1018 if (!buf) { 1019 err(1, "malloc"); 1020 /*NOTREACHED*/ 1021 } 1022 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1023 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1024 /*NOTREACHED*/ 1025 } 1026 1027 ep = (struct in6_defrouter *)(buf + l); 1028 for (p = (struct in6_defrouter *)buf; p < ep; p++) { 1029 int rtpref; 1030 1031 if (getnameinfo((struct sockaddr *)&p->rtaddr, 1032 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 1033 (nflag ? NI_NUMERICHOST : 0)) != 0) 1034 strlcpy(host_buf, "?", sizeof(host_buf)); 1035 1036 printf("%s if=%s", host_buf, 1037 if_indextoname(p->if_index, ifix_buf)); 1038 printf(", flags=%s%s", 1039 p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1040 p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 1041 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 1042 printf(", pref=%s", rtpref_str[rtpref]); 1043 1044 gettimeofday(&time, 0); 1045 if (p->expire == 0) 1046 printf(", expire=Never\n"); 1047 else 1048 printf(", expire=%s\n", 1049 sec2str(p->expire - time.tv_sec)); 1050 } 1051 free(buf); 1052#else 1053 struct in6_drlist dr; 1054 int s, i; 1055 struct timeval time; 1056 1057 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1058 err(1, "socket"); 1059 /* NOTREACHED */ 1060 } 1061 bzero(&dr, sizeof(dr)); 1062 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */ 1063 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 1064 err(1, "ioctl(SIOCGDRLST_IN6)"); 1065 /* NOTREACHED */ 1066 } 1067#define DR dr.defrouter[i] 1068 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) { 1069 struct sockaddr_in6 sin6; 1070 1071 bzero(&sin6, sizeof(sin6)); 1072 sin6.sin6_family = AF_INET6; 1073 sin6.sin6_len = sizeof(sin6); 1074 sin6.sin6_addr = DR.rtaddr; 1075 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf, 1076 sizeof(host_buf), NULL, 0, 1077 (nflag ? NI_NUMERICHOST : 0)); 1078 1079 printf("%s if=%s", host_buf, 1080 if_indextoname(DR.if_index, ifix_buf)); 1081 printf(", flags=%s%s", 1082 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "", 1083 DR.flags & ND_RA_FLAG_OTHER ? "O" : ""); 1084 gettimeofday(&time, 0); 1085 if (DR.expire == 0) 1086 printf(", expire=Never\n"); 1087 else 1088 printf(", expire=%s\n", 1089 sec2str(DR.expire - time.tv_sec)); 1090 } 1091#undef DR 1092 close(s); 1093#endif 1094} 1095 1096void 1097plist() 1098{ 1099#ifdef ICMPV6CTL_ND6_PRLIST 1100 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 1101 char *buf; 1102 struct in6_prefix *p, *ep, *n; 1103 struct sockaddr_in6 *advrtr; 1104 size_t l; 1105 struct timeval time; 1106 const int niflags = NI_NUMERICHOST; 1107 int ninflags = nflag ? NI_NUMERICHOST : 0; 1108 char namebuf[NI_MAXHOST]; 1109 1110 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1111 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1112 /*NOTREACHED*/ 1113 } 1114 buf = malloc(l); 1115 if (!buf) { 1116 err(1, "malloc"); 1117 /*NOTREACHED*/ 1118 } 1119 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1120 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1121 /*NOTREACHED*/ 1122 } 1123 1124 ep = (struct in6_prefix *)(buf + l); 1125 for (p = (struct in6_prefix *)buf; p < ep; p = n) { 1126 advrtr = (struct sockaddr_in6 *)(p + 1); 1127 n = (struct in6_prefix *)&advrtr[p->advrtrs]; 1128 1129 if (getnameinfo((struct sockaddr *)&p->prefix, 1130 p->prefix.sin6_len, namebuf, sizeof(namebuf), 1131 NULL, 0, niflags) != 0) 1132 strlcpy(namebuf, "?", sizeof(namebuf)); 1133 printf("%s/%d if=%s\n", namebuf, p->prefixlen, 1134 if_indextoname(p->if_index, ifix_buf)); 1135 1136 gettimeofday(&time, 0); 1137 /* 1138 * meaning of fields, especially flags, is very different 1139 * by origin. notify the difference to the users. 1140 */ 1141 printf("flags=%s%s%s%s%s", 1142 p->raflags.onlink ? "L" : "", 1143 p->raflags.autonomous ? "A" : "", 1144 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "", 1145 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "", 1146#ifdef NDPRF_HOME 1147 (p->flags & NDPRF_HOME) != 0 ? "H" : "" 1148#else 1149 "" 1150#endif 1151 ); 1152 if (p->vltime == ND6_INFINITE_LIFETIME) 1153 printf(" vltime=infinity"); 1154 else 1155 printf(" vltime=%ld", (long)p->vltime); 1156 if (p->pltime == ND6_INFINITE_LIFETIME) 1157 printf(", pltime=infinity"); 1158 else 1159 printf(", pltime=%ld", (long)p->pltime); 1160 if (p->expire == 0) 1161 printf(", expire=Never"); 1162 else if (p->expire >= time.tv_sec) 1163 printf(", expire=%s", 1164 sec2str(p->expire - time.tv_sec)); 1165 else 1166 printf(", expired"); 1167 printf(", ref=%d", p->refcnt); 1168 printf("\n"); 1169 /* 1170 * "advertising router" list is meaningful only if the prefix 1171 * information is from RA. 1172 */ 1173 if (p->advrtrs) { 1174 int j; 1175 struct sockaddr_in6 *sin6; 1176 1177 sin6 = (struct sockaddr_in6 *)(p + 1); 1178 printf(" advertised by\n"); 1179 for (j = 0; j < p->advrtrs; j++) { 1180 struct in6_nbrinfo *nbi; 1181 1182 if (getnameinfo((struct sockaddr *)sin6, 1183 sin6->sin6_len, namebuf, sizeof(namebuf), 1184 NULL, 0, ninflags) != 0) 1185 strlcpy(namebuf, "?", sizeof(namebuf)); 1186 printf(" %s", namebuf); 1187 1188 nbi = getnbrinfo(&sin6->sin6_addr, 1189 p->if_index, 0); 1190 if (nbi) { 1191 switch (nbi->state) { 1192 case ND6_LLINFO_REACHABLE: 1193 case ND6_LLINFO_STALE: 1194 case ND6_LLINFO_DELAY: 1195 case ND6_LLINFO_PROBE: 1196 printf(" (reachable)\n"); 1197 break; 1198 default: 1199 printf(" (unreachable)\n"); 1200 } 1201 } else 1202 printf(" (no neighbor state)\n"); 1203 sin6++; 1204 } 1205 } else 1206 printf(" No advertising router\n"); 1207 } 1208 free(buf); 1209#else 1210 struct in6_prlist pr; 1211 int s, i; 1212 struct timeval time; 1213 1214 gettimeofday(&time, 0); 1215 1216 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1217 err(1, "socket"); 1218 /* NOTREACHED */ 1219 } 1220 bzero(&pr, sizeof(pr)); 1221 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */ 1222 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) { 1223 err(1, "ioctl(SIOCGPRLST_IN6)"); 1224 /* NOTREACHED */ 1225 } 1226#define PR pr.prefix[i] 1227 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) { 1228 struct sockaddr_in6 p6; 1229 char namebuf[NI_MAXHOST]; 1230 int niflags; 1231 1232#ifdef NDPRF_ONLINK 1233 p6 = PR.prefix; 1234#else 1235 memset(&p6, 0, sizeof(p6)); 1236 p6.sin6_family = AF_INET6; 1237 p6.sin6_len = sizeof(p6); 1238 p6.sin6_addr = PR.prefix; 1239#endif 1240 1241 /* 1242 * copy link index to sin6_scope_id field. 1243 * XXX: KAME specific. 1244 */ 1245 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) { 1246 u_int16_t linkid; 1247 1248 memcpy(&linkid, &p6.sin6_addr.s6_addr[2], 1249 sizeof(linkid)); 1250 linkid = ntohs(linkid); 1251 p6.sin6_scope_id = linkid; 1252 p6.sin6_addr.s6_addr[2] = 0; 1253 p6.sin6_addr.s6_addr[3] = 0; 1254 } 1255 1256 niflags = NI_NUMERICHOST; 1257 if (getnameinfo((struct sockaddr *)&p6, 1258 sizeof(p6), namebuf, sizeof(namebuf), 1259 NULL, 0, niflags)) { 1260 warnx("getnameinfo failed"); 1261 continue; 1262 } 1263 printf("%s/%d if=%s\n", namebuf, PR.prefixlen, 1264 if_indextoname(PR.if_index, ifix_buf)); 1265 1266 gettimeofday(&time, 0); 1267 /* 1268 * meaning of fields, especially flags, is very different 1269 * by origin. notify the difference to the users. 1270 */ 1271#if 0 1272 printf(" %s", 1273 PR.origin == PR_ORIG_RA ? "" : "advertise: "); 1274#endif 1275#ifdef NDPRF_ONLINK 1276 printf("flags=%s%s%s%s%s", 1277 PR.raflags.onlink ? "L" : "", 1278 PR.raflags.autonomous ? "A" : "", 1279 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "", 1280 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "", 1281#ifdef NDPRF_HOME 1282 (PR.flags & NDPRF_HOME) != 0 ? "H" : "" 1283#else 1284 "" 1285#endif 1286 ); 1287#else 1288 printf("flags=%s%s", 1289 PR.raflags.onlink ? "L" : "", 1290 PR.raflags.autonomous ? "A" : ""); 1291#endif 1292 if (PR.vltime == ND6_INFINITE_LIFETIME) 1293 printf(" vltime=infinity"); 1294 else 1295 printf(" vltime=%ld", (long)PR.vltime); 1296 if (PR.pltime == ND6_INFINITE_LIFETIME) 1297 printf(", pltime=infinity"); 1298 else 1299 printf(", pltime=%ld", (long)PR.pltime); 1300 if (PR.expire == 0) 1301 printf(", expire=Never"); 1302 else if (PR.expire >= time.tv_sec) 1303 printf(", expire=%s", 1304 sec2str(PR.expire - time.tv_sec)); 1305 else 1306 printf(", expired"); 1307#ifdef NDPRF_ONLINK 1308 printf(", ref=%d", PR.refcnt); 1309#endif 1310#if 0 1311 switch (PR.origin) { 1312 case PR_ORIG_RA: 1313 printf(", origin=RA"); 1314 break; 1315 case PR_ORIG_RR: 1316 printf(", origin=RR"); 1317 break; 1318 case PR_ORIG_STATIC: 1319 printf(", origin=static"); 1320 break; 1321 case PR_ORIG_KERNEL: 1322 printf(", origin=kernel"); 1323 break; 1324 default: 1325 printf(", origin=?"); 1326 break; 1327 } 1328#endif 1329 printf("\n"); 1330 /* 1331 * "advertising router" list is meaningful only if the prefix 1332 * information is from RA. 1333 */ 1334 if (0 && /* prefix origin is almost obsolted */ 1335 PR.origin != PR_ORIG_RA) 1336 ; 1337 else if (PR.advrtrs) { 1338 int j; 1339 printf(" advertised by\n"); 1340 for (j = 0; j < PR.advrtrs; j++) { 1341 struct sockaddr_in6 sin6; 1342 struct in6_nbrinfo *nbi; 1343 1344 bzero(&sin6, sizeof(sin6)); 1345 sin6.sin6_family = AF_INET6; 1346 sin6.sin6_len = sizeof(sin6); 1347 sin6.sin6_addr = PR.advrtr[j]; 1348 sin6.sin6_scope_id = PR.if_index; /* XXX */ 1349 getnameinfo((struct sockaddr *)&sin6, 1350 sin6.sin6_len, host_buf, 1351 sizeof(host_buf), NULL, 0, 1352 (nflag ? NI_NUMERICHOST : 0)); 1353 printf(" %s", host_buf); 1354 1355 nbi = getnbrinfo(&sin6.sin6_addr, 1356 PR.if_index, 0); 1357 if (nbi) { 1358 switch (nbi->state) { 1359 case ND6_LLINFO_REACHABLE: 1360 case ND6_LLINFO_STALE: 1361 case ND6_LLINFO_DELAY: 1362 case ND6_LLINFO_PROBE: 1363 printf(" (reachable)\n"); 1364 break; 1365 default: 1366 printf(" (unreachable)\n"); 1367 } 1368 } else 1369 printf(" (no neighbor state)\n"); 1370 } 1371 if (PR.advrtrs > DRLSTSIZ) 1372 printf(" and %d routers\n", 1373 PR.advrtrs - DRLSTSIZ); 1374 } else 1375 printf(" No advertising router\n"); 1376 } 1377#undef PR 1378 close(s); 1379#endif 1380} 1381 1382void 1383pfx_flush() 1384{ 1385 char dummyif[IFNAMSIZ+8]; 1386 int s; 1387 1388 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1389 err(1, "socket"); 1390 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1391 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1392 err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 1393} 1394 1395void 1396rtr_flush() 1397{ 1398 char dummyif[IFNAMSIZ+8]; 1399 int s; 1400 1401 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1402 err(1, "socket"); 1403 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1404 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1405 err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 1406 1407 close(s); 1408} 1409 1410void 1411harmonize_rtr() 1412{ 1413 char dummyif[IFNAMSIZ+8]; 1414 int s; 1415 1416 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1417 err(1, "socket"); 1418 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1419 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1420 err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 1421 1422 close(s); 1423} 1424 1425#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 1426static void 1427setdefif(ifname) 1428 char *ifname; 1429{ 1430 struct in6_ndifreq ndifreq; 1431 unsigned int ifindex; 1432 1433 if (strcasecmp(ifname, "delete") == 0) 1434 ifindex = 0; 1435 else { 1436 if ((ifindex = if_nametoindex(ifname)) == 0) 1437 err(1, "failed to resolve i/f index for %s", ifname); 1438 } 1439 1440 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1441 err(1, "socket"); 1442 1443 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1444 ndifreq.ifindex = ifindex; 1445 1446 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1447 err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 1448 1449 close(s); 1450} 1451 1452static void 1453getdefif() 1454{ 1455 struct in6_ndifreq ndifreq; 1456 char ifname[IFNAMSIZ+8]; 1457 1458 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1459 err(1, "socket"); 1460 1461 memset(&ndifreq, 0, sizeof(ndifreq)); 1462 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1463 1464 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1465 err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 1466 1467 if (ndifreq.ifindex == 0) 1468 printf("No default interface.\n"); 1469 else { 1470 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL) 1471 err(1, "failed to resolve ifname for index %lu", 1472 ndifreq.ifindex); 1473 printf("ND default interface = %s\n", ifname); 1474 } 1475 1476 close(s); 1477} 1478#endif 1479 1480static char * 1481sec2str(total) 1482 time_t total; 1483{ 1484 static char result[256]; 1485 int days, hours, mins, secs; 1486 int first = 1; 1487 char *p = result; 1488 char *ep = &result[sizeof(result)]; 1489 int n; 1490 1491 days = total / 3600 / 24; 1492 hours = (total / 3600) % 24; 1493 mins = (total / 60) % 60; 1494 secs = total % 60; 1495 1496 if (days) { 1497 first = 0; 1498 n = snprintf(p, ep - p, "%dd", days); 1499 if (n < 0 || n >= ep - p) 1500 return "?"; 1501 p += n; 1502 } 1503 if (!first || hours) { 1504 first = 0; 1505 n = snprintf(p, ep - p, "%dh", hours); 1506 if (n < 0 || n >= ep - p) 1507 return "?"; 1508 p += n; 1509 } 1510 if (!first || mins) { 1511 first = 0; 1512 n = snprintf(p, ep - p, "%dm", mins); 1513 if (n < 0 || n >= ep - p) 1514 return "?"; 1515 p += n; 1516 } 1517 snprintf(p, ep - p, "%ds", secs); 1518 1519 return(result); 1520} 1521 1522/* 1523 * Print the timestamp 1524 * from tcpdump/util.c 1525 */ 1526static void 1527ts_print(tvp) 1528 const struct timeval *tvp; 1529{ 1530 int s; 1531 1532 /* Default */ 1533 s = (tvp->tv_sec + thiszone) % 86400; 1534 (void)printf("%02d:%02d:%02d.%06u ", 1535 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 1536} 1537