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