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