ndp.c revision 293290
1/* $FreeBSD: stable/10/usr.sbin/ndp/ndp.c 293290 2016-01-07 00:40:51Z bdrewery $ */ 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_var.h> 87#include <net/if_dl.h> 88#include <net/if_types.h> 89#include <net/route.h> 90 91#include <netinet/in.h> 92#include <netinet/if_ether.h> 93 94#include <netinet/icmp6.h> 95#include <netinet6/in6_var.h> 96#include <netinet6/nd6.h> 97 98#include <arpa/inet.h> 99 100#include <ctype.h> 101#include <netdb.h> 102#include <errno.h> 103#include <nlist.h> 104#include <stdio.h> 105#include <string.h> 106#include <paths.h> 107#include <err.h> 108#include <stdlib.h> 109#include <fcntl.h> 110#include <unistd.h> 111#include "gmt2local.h" 112 113/* packing rule for routing socket */ 114#define ROUNDUP(a) \ 115 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) 116#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) 117 118#define NEXTADDR(w, s) \ 119 if (rtm->rtm_addrs & (w)) { \ 120 bcopy((char *)&s, cp, sizeof(s)); cp += SA_SIZE(&s);} 121 122 123static pid_t pid; 124static int nflag; 125static int tflag; 126static int32_t thiszone; /* time difference with gmt */ 127static int s = -1; 128static int repeat = 0; 129 130static char host_buf[NI_MAXHOST]; /* getnameinfo() */ 131static char ifix_buf[IFNAMSIZ]; /* if_indextoname() */ 132 133static int file(char *); 134static void getsocket(void); 135static int set(int, char **); 136static void get(char *); 137static int delete(char *); 138static void dump(struct sockaddr_in6 *, int); 139static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int); 140static char *ether_str(struct sockaddr_dl *); 141static int ndp_ether_aton(char *, u_char *); 142static void usage(void); 143static int rtmsg(int); 144static void ifinfo(char *, int, char **); 145static void rtrlist(void); 146static void plist(void); 147static void pfx_flush(void); 148static void rtr_flush(void); 149static void harmonize_rtr(void); 150#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 151static void getdefif(void); 152static void setdefif(char *); 153#endif 154static char *sec2str(time_t); 155static void ts_print(const struct timeval *); 156 157static char *rtpref_str[] = { 158 "medium", /* 00 */ 159 "high", /* 01 */ 160 "rsv", /* 10 */ 161 "low" /* 11 */ 162}; 163 164int 165main(int argc, char **argv) 166{ 167 int ch, mode = 0; 168 char *arg = NULL; 169 170 pid = getpid(); 171 thiszone = gmt2local(0); 172 while ((ch = getopt(argc, argv, "acd:f:Ii:nprstA:HPR")) != -1) 173 switch (ch) { 174 case 'a': 175 case 'c': 176 case 'p': 177 case 'r': 178 case 'H': 179 case 'P': 180 case 'R': 181 case 's': 182 case 'I': 183 if (mode) { 184 usage(); 185 /*NOTREACHED*/ 186 } 187 mode = ch; 188 arg = NULL; 189 break; 190 case 'f': 191 exit(file(optarg) ? 1 : 0); 192 case 'd': 193 case 'i': 194 if (mode) { 195 usage(); 196 /*NOTREACHED*/ 197 } 198 mode = ch; 199 arg = optarg; 200 break; 201 case 'n': 202 nflag = 1; 203 break; 204 case 't': 205 tflag = 1; 206 break; 207 case 'A': 208 if (mode) { 209 usage(); 210 /*NOTREACHED*/ 211 } 212 mode = 'a'; 213 repeat = atoi(optarg); 214 if (repeat < 0) { 215 usage(); 216 /*NOTREACHED*/ 217 } 218 break; 219 default: 220 usage(); 221 } 222 223 argc -= optind; 224 argv += optind; 225 226 switch (mode) { 227 case 'a': 228 case 'c': 229 if (argc != 0) { 230 usage(); 231 /*NOTREACHED*/ 232 } 233 dump(0, mode == 'c'); 234 break; 235 case 'd': 236 if (argc != 0) { 237 usage(); 238 /*NOTREACHED*/ 239 } 240 delete(arg); 241 break; 242 case 'I': 243#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 244 if (argc > 1) { 245 usage(); 246 /*NOTREACHED*/ 247 } else if (argc == 1) { 248 if (strcmp(*argv, "delete") == 0 || 249 if_nametoindex(*argv)) 250 setdefif(*argv); 251 else 252 errx(1, "invalid interface %s", *argv); 253 } 254 getdefif(); /* always call it to print the result */ 255 break; 256#else 257 errx(1, "not supported yet"); 258 /*NOTREACHED*/ 259#endif 260 case 'p': 261 if (argc != 0) { 262 usage(); 263 /*NOTREACHED*/ 264 } 265 plist(); 266 break; 267 case 'i': 268 ifinfo(arg, argc, argv); 269 break; 270 case 'r': 271 if (argc != 0) { 272 usage(); 273 /*NOTREACHED*/ 274 } 275 rtrlist(); 276 break; 277 case 's': 278 if (argc < 2 || argc > 4) 279 usage(); 280 exit(set(argc, argv) ? 1 : 0); 281 case 'H': 282 if (argc != 0) { 283 usage(); 284 /*NOTREACHED*/ 285 } 286 harmonize_rtr(); 287 break; 288 case 'P': 289 if (argc != 0) { 290 usage(); 291 /*NOTREACHED*/ 292 } 293 pfx_flush(); 294 break; 295 case 'R': 296 if (argc != 0) { 297 usage(); 298 /*NOTREACHED*/ 299 } 300 rtr_flush(); 301 break; 302 case 0: 303 if (argc != 1) { 304 usage(); 305 /*NOTREACHED*/ 306 } 307 get(argv[0]); 308 break; 309 } 310 exit(0); 311} 312 313/* 314 * Process a file to set standard ndp entries 315 */ 316static int 317file(char *name) 318{ 319 FILE *fp; 320 int i, retval; 321 char line[100], arg[5][50], *args[5], *p; 322 323 if ((fp = fopen(name, "r")) == NULL) 324 err(1, "cannot open %s", name); 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 if ((p = strchr(line, '#')) != NULL) 333 *p = '\0'; 334 for (p = line; isblank(*p); p++); 335 if (*p == '\n' || *p == '\0') 336 continue; 337 i = sscanf(line, "%49s %49s %49s %49s %49s", 338 arg[0], arg[1], arg[2], arg[3], arg[4]); 339 if (i < 2) { 340 warnx("bad line: %s", line); 341 retval = 1; 342 continue; 343 } 344 if (set(i, args)) 345 retval = 1; 346 } 347 fclose(fp); 348 return (retval); 349} 350 351static void 352getsocket() 353{ 354 if (s < 0) { 355 s = socket(PF_ROUTE, SOCK_RAW, 0); 356 if (s < 0) { 357 err(1, "socket"); 358 /* NOTREACHED */ 359 } 360 } 361} 362 363static struct sockaddr_in6 so_mask = { 364 .sin6_len = sizeof(so_mask), 365 .sin6_family = AF_INET6 366}; 367static struct sockaddr_in6 blank_sin = { 368 .sin6_len = sizeof(blank_sin), 369 .sin6_family = AF_INET6 370}; 371static struct sockaddr_in6 sin_m; 372static struct sockaddr_dl blank_sdl = { 373 .sdl_len = sizeof(blank_sdl), 374 .sdl_family = AF_LINK 375}; 376static struct sockaddr_dl sdl_m; 377static time_t expire_time; 378static int flags, found_entry; 379static struct { 380 struct rt_msghdr m_rtm; 381 char m_space[512]; 382} m_rtmsg; 383 384/* 385 * Set an individual neighbor cache entry 386 */ 387static int 388set(int argc, char **argv) 389{ 390 register struct sockaddr_in6 *sin = &sin_m; 391 register struct sockaddr_dl *sdl; 392 register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm); 393 struct addrinfo hints, *res; 394 int gai_error; 395 u_char *ea; 396 char *host = argv[0], *eaddr = argv[1]; 397 398 getsocket(); 399 argc -= 2; 400 argv += 2; 401 sdl_m = blank_sdl; 402 sin_m = blank_sin; 403 404 bzero(&hints, sizeof(hints)); 405 hints.ai_family = AF_INET6; 406 gai_error = getaddrinfo(host, NULL, &hints, &res); 407 if (gai_error) { 408 fprintf(stderr, "ndp: %s: %s\n", host, 409 gai_strerror(gai_error)); 410 return 1; 411 } 412 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 413 sin->sin6_scope_id = 414 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 415 ea = (u_char *)LLADDR(&sdl_m); 416 if (ndp_ether_aton(eaddr, ea) == 0) 417 sdl_m.sdl_alen = 6; 418 flags = expire_time = 0; 419 while (argc-- > 0) { 420 if (strncmp(argv[0], "temp", 4) == 0) { 421 struct timeval now; 422 423 gettimeofday(&now, 0); 424 expire_time = now.tv_sec + 20 * 60; 425 } else if (strncmp(argv[0], "proxy", 5) == 0) 426 flags |= RTF_ANNOUNCE; 427 argv++; 428 } 429 if (rtmsg(RTM_GET) < 0) { 430 errx(1, "RTM_GET(%s) failed", host); 431 /* NOTREACHED */ 432 } 433 sin = (struct sockaddr_in6 *)(rtm + 1); 434 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin); 435 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 436 if (sdl->sdl_family == AF_LINK && 437 !(rtm->rtm_flags & RTF_GATEWAY)) { 438 switch (sdl->sdl_type) { 439 case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023: 440 case IFT_ISO88024: case IFT_ISO88025: 441 case IFT_L2VLAN: case IFT_BRIDGE: 442 goto overwrite; 443 } 444 } 445 fprintf(stderr, "set: cannot configure a new entry\n"); 446 return 1; 447 } 448 449overwrite: 450 if (sdl->sdl_family != AF_LINK) { 451 printf("cannot intuit interface index and type for %s\n", host); 452 return (1); 453 } 454 sdl_m.sdl_type = sdl->sdl_type; 455 sdl_m.sdl_index = sdl->sdl_index; 456 return (rtmsg(RTM_ADD)); 457} 458 459/* 460 * Display an individual neighbor cache entry 461 */ 462static void 463get(char *host) 464{ 465 struct sockaddr_in6 *sin = &sin_m; 466 struct addrinfo hints, *res; 467 int gai_error; 468 469 sin_m = blank_sin; 470 bzero(&hints, sizeof(hints)); 471 hints.ai_family = AF_INET6; 472 gai_error = getaddrinfo(host, NULL, &hints, &res); 473 if (gai_error) { 474 fprintf(stderr, "ndp: %s: %s\n", host, 475 gai_strerror(gai_error)); 476 return; 477 } 478 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 479 sin->sin6_scope_id = 480 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 481 dump(sin, 0); 482 if (found_entry == 0) { 483 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 484 sizeof(host_buf), NULL ,0, 485 (nflag ? NI_NUMERICHOST : 0)); 486 printf("%s (%s) -- no entry\n", host, host_buf); 487 exit(1); 488 } 489} 490 491/* 492 * Delete a neighbor cache entry 493 */ 494static int 495delete(char *host) 496{ 497 struct sockaddr_in6 *sin = &sin_m; 498 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 499 register char *cp = m_rtmsg.m_space; 500 struct sockaddr_dl *sdl; 501 struct addrinfo hints, *res; 502 int gai_error; 503 504 getsocket(); 505 sin_m = blank_sin; 506 507 bzero(&hints, sizeof(hints)); 508 hints.ai_family = AF_INET6; 509 gai_error = getaddrinfo(host, NULL, &hints, &res); 510 if (gai_error) { 511 fprintf(stderr, "ndp: %s: %s\n", host, 512 gai_strerror(gai_error)); 513 return 1; 514 } 515 sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 516 sin->sin6_scope_id = 517 ((struct sockaddr_in6 *)res->ai_addr)->sin6_scope_id; 518 if (rtmsg(RTM_GET) < 0) { 519 errx(1, "RTM_GET(%s) failed", host); 520 /* NOTREACHED */ 521 } 522 sin = (struct sockaddr_in6 *)(rtm + 1); 523 sdl = (struct sockaddr_dl *)(ROUNDUP(sin->sin6_len) + (char *)sin); 524 if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) { 525 if (sdl->sdl_family == AF_LINK && 526 !(rtm->rtm_flags & RTF_GATEWAY)) { 527 goto delete; 528 } 529 fprintf(stderr, "delete: cannot delete non-NDP entry\n"); 530 return 1; 531 } 532 533delete: 534 if (sdl->sdl_family != AF_LINK) { 535 printf("cannot locate %s\n", host); 536 return (1); 537 } 538 /* 539 * need to reinit the field because it has rt_key 540 * but we want the actual address 541 */ 542 NEXTADDR(RTA_DST, sin_m); 543 rtm->rtm_flags |= RTF_LLDATA; 544 if (rtmsg(RTM_DELETE) == 0) { 545 getnameinfo((struct sockaddr *)sin, 546 sin->sin6_len, host_buf, 547 sizeof(host_buf), NULL, 0, 548 (nflag ? NI_NUMERICHOST : 0)); 549 printf("%s (%s) deleted\n", host, host_buf); 550 } 551 552 return 0; 553} 554 555#define W_ADDR 36 556#define W_LL 17 557#define W_IF 6 558 559/* 560 * Dump the entire neighbor cache 561 */ 562static void 563dump(struct sockaddr_in6 *addr, int cflag) 564{ 565 int mib[6]; 566 size_t needed; 567 char *lim, *buf, *next; 568 struct rt_msghdr *rtm; 569 struct sockaddr_in6 *sin; 570 struct sockaddr_dl *sdl; 571 extern int h_errno; 572 struct in6_nbrinfo *nbi; 573 struct timeval now; 574 int addrwidth; 575 int llwidth; 576 int ifwidth; 577 char flgbuf[8]; 578 char *ifname; 579 580 /* Print header */ 581 if (!tflag && !cflag) 582 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n", 583 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 584 W_IF, W_IF, "Netif", "Expire", "S", "Flags"); 585 586again:; 587 mib[0] = CTL_NET; 588 mib[1] = PF_ROUTE; 589 mib[2] = 0; 590 mib[3] = AF_INET6; 591 mib[4] = NET_RT_FLAGS; 592#ifdef RTF_LLINFO 593 mib[5] = RTF_LLINFO; 594#else 595 mib[5] = 0; 596#endif 597 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 598 err(1, "sysctl(PF_ROUTE estimate)"); 599 if (needed > 0) { 600 if ((buf = malloc(needed)) == NULL) 601 err(1, "malloc"); 602 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 603 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 604 lim = buf + needed; 605 } else 606 buf = lim = NULL; 607 608 for (next = buf; next && next < lim; next += rtm->rtm_msglen) { 609 int isrouter = 0, prbs = 0; 610 611 rtm = (struct rt_msghdr *)next; 612 sin = (struct sockaddr_in6 *)(rtm + 1); 613 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len)); 614 615 /* 616 * Some OSes can produce a route that has the LINK flag but 617 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 618 * and BSD/OS, where xx is not the interface identifier on 619 * lo0). Such routes entry would annoy getnbrinfo() below, 620 * so we skip them. 621 * XXX: such routes should have the GATEWAY flag, not the 622 * LINK flag. However, there is rotten routing software 623 * that advertises all routes that have the GATEWAY flag. 624 * Thus, KAME kernel intentionally does not set the LINK flag. 625 * What is to be fixed is not ndp, but such routing software 626 * (and the kernel workaround)... 627 */ 628 if (sdl->sdl_family != AF_LINK) 629 continue; 630 631 if (!(rtm->rtm_flags & RTF_HOST)) 632 continue; 633 634 if (addr) { 635 if (IN6_ARE_ADDR_EQUAL(&addr->sin6_addr, 636 &sin->sin6_addr) == 0 || 637 addr->sin6_scope_id != sin->sin6_scope_id) 638 continue; 639 found_entry = 1; 640 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) 641 continue; 642 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) || 643 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) { 644 /* XXX: should scope id be filled in the kernel? */ 645 if (sin->sin6_scope_id == 0) 646 sin->sin6_scope_id = sdl->sdl_index; 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(&now, 0); 663 if (tflag) 664 ts_print(&now); 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 information */ 683 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 684 if (nbi) { 685 if (nbi->expire > now.tv_sec) { 686 printf(" %-9.9s", 687 sec2str(nbi->expire - now.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 769static struct in6_nbrinfo * 770getnbrinfo(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 792static char * 793ether_str(struct sockaddr_dl *sdl) 794{ 795 static char hbuf[NI_MAXHOST]; 796 797 if (sdl->sdl_alen == ETHER_ADDR_LEN) { 798 strlcpy(hbuf, ether_ntoa((struct ether_addr *)LLADDR(sdl)), 799 sizeof(hbuf)); 800 } else if (sdl->sdl_alen) { 801 int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0; 802 snprintf(hbuf, sizeof(hbuf), "%s", link_ntoa(sdl) + n); 803 } else 804 snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 805 806 return(hbuf); 807} 808 809static int 810ndp_ether_aton(char *a, u_char *n) 811{ 812 int i, o[6]; 813 814 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 815 &o[3], &o[4], &o[5]); 816 if (i != 6) { 817 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 818 return (1); 819 } 820 for (i = 0; i < 6; i++) 821 n[i] = o[i]; 822 return (0); 823} 824 825static void 826usage() 827{ 828 printf("usage: ndp [-nt] hostname\n"); 829 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n"); 830 printf(" ndp [-nt] -A wait\n"); 831 printf(" ndp [-nt] -d hostname\n"); 832 printf(" ndp [-nt] -f filename\n"); 833 printf(" ndp [-nt] -i interface [flags...]\n"); 834#ifdef SIOCSDEFIFACE_IN6 835 printf(" ndp [-nt] -I [interface|delete]\n"); 836#endif 837 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n"); 838 exit(1); 839} 840 841static int 842rtmsg(int cmd) 843{ 844 static int seq; 845 int rlen; 846 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 847 register char *cp = m_rtmsg.m_space; 848 register int l; 849 850 errno = 0; 851 if (cmd == RTM_DELETE) 852 goto doit; 853 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 854 rtm->rtm_flags = flags; 855 rtm->rtm_version = RTM_VERSION; 856 857 switch (cmd) { 858 default: 859 fprintf(stderr, "ndp: internal wrong cmd\n"); 860 exit(1); 861 case RTM_ADD: 862 rtm->rtm_addrs |= RTA_GATEWAY; 863 if (expire_time) { 864 rtm->rtm_rmx.rmx_expire = expire_time; 865 rtm->rtm_inits = RTV_EXPIRE; 866 } 867 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA); 868#if 0 /* we don't support ipv6addr/128 type proxying */ 869 if (rtm->rtm_flags & RTF_ANNOUNCE) { 870 rtm->rtm_flags &= ~RTF_HOST; 871 rtm->rtm_addrs |= RTA_NETMASK; 872 } 873#endif 874 /* FALLTHROUGH */ 875 case RTM_GET: 876 rtm->rtm_addrs |= RTA_DST; 877 } 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; 887doit: 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 906static void 907ifinfo(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/* 947 * XXX: this macro is not 100% correct, in that it matches "nud" against 948 * "nudbogus". But we just let it go since this is minor. 949 */ 950#define SETVALUE(f, v) \ 951 do { \ 952 char *valptr; \ 953 unsigned long newval; \ 954 v = 0; /* unspecified */ \ 955 if (strncmp(cp, f, strlen(f)) == 0) { \ 956 valptr = strchr(cp, '='); \ 957 if (valptr == NULL) \ 958 err(1, "syntax error in %s field", (f)); \ 959 errno = 0; \ 960 newval = strtoul(++valptr, NULL, 0); \ 961 if (errno) \ 962 err(1, "syntax error in %s's value", (f)); \ 963 v = newval; \ 964 } \ 965 } while (0) 966 967 SETFLAG("disabled", ND6_IFF_IFDISABLED); 968 SETFLAG("nud", ND6_IFF_PERFORMNUD); 969#ifdef ND6_IFF_ACCEPT_RTADV 970 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 971#endif 972#ifdef ND6_IFF_AUTO_LINKLOCAL 973 SETFLAG("auto_linklocal", ND6_IFF_AUTO_LINKLOCAL); 974#endif 975#ifdef ND6_IFF_NO_PREFER_IFACE 976 SETFLAG("no_prefer_iface", ND6_IFF_NO_PREFER_IFACE); 977#endif 978 SETVALUE("basereachable", ND.basereachable); 979 SETVALUE("retrans", ND.retrans); 980 SETVALUE("curhlim", ND.chlim); 981 982 ND.flags = newflags; 983 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) { 984 err(1, "ioctl(SIOCSIFINFO_IN6)"); 985 /* NOTREACHED */ 986 } 987#undef SETFLAG 988#undef SETVALUE 989 } 990 991 if (!ND.initialized) { 992 errx(1, "%s: not initialized yet", ifname); 993 /* NOTREACHED */ 994 } 995 996 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 997 err(1, "ioctl(SIOCGIFINFO_IN6)"); 998 /* NOTREACHED */ 999 } 1000 printf("linkmtu=%d", ND.linkmtu); 1001 printf(", maxmtu=%d", ND.maxmtu); 1002 printf(", curhlim=%d", ND.chlim); 1003 printf(", basereachable=%ds%dms", 1004 ND.basereachable / 1000, ND.basereachable % 1000); 1005 printf(", reachable=%ds", ND.reachable); 1006 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 1007#ifdef IPV6CTL_USETEMPADDR 1008 memset(nullbuf, 0, sizeof(nullbuf)); 1009 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 1010 int j; 1011 u_int8_t *rbuf; 1012 1013 for (i = 0; i < 3; i++) { 1014 switch (i) { 1015 case 0: 1016 printf("\nRandom seed(0): "); 1017 rbuf = ND.randomseed0; 1018 break; 1019 case 1: 1020 printf("\nRandom seed(1): "); 1021 rbuf = ND.randomseed1; 1022 break; 1023 case 2: 1024 printf("\nRandom ID: "); 1025 rbuf = ND.randomid; 1026 break; 1027 default: 1028 errx(1, "impossible case for tempaddr display"); 1029 } 1030 for (j = 0; j < 8; j++) 1031 printf("%02x", rbuf[j]); 1032 } 1033 } 1034#endif 1035 if (ND.flags) { 1036 printf("\nFlags: "); 1037#ifdef ND6_IFF_IFDISABLED 1038 if ((ND.flags & ND6_IFF_IFDISABLED)) 1039 printf("disabled "); 1040#endif 1041 if ((ND.flags & ND6_IFF_PERFORMNUD)) 1042 printf("nud "); 1043#ifdef ND6_IFF_ACCEPT_RTADV 1044 if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 1045 printf("accept_rtadv "); 1046#endif 1047#ifdef ND6_IFF_AUTO_LINKLOCAL 1048 if ((ND.flags & ND6_IFF_AUTO_LINKLOCAL)) 1049 printf("auto_linklocal "); 1050#endif 1051#ifdef ND6_IFF_NO_PREFER_IFACE 1052 if ((ND.flags & ND6_IFF_NO_PREFER_IFACE)) 1053 printf("no_prefer_iface "); 1054#endif 1055 } 1056 putc('\n', stdout); 1057#undef ND 1058 1059 close(s); 1060} 1061 1062#ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 1063#define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 1064#endif 1065 1066static void 1067rtrlist() 1068{ 1069 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 1070 char *buf; 1071 struct in6_defrouter *p, *ep; 1072 size_t l; 1073 struct timeval now; 1074 1075 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1076 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1077 /*NOTREACHED*/ 1078 } 1079 if (l == 0) 1080 return; 1081 buf = malloc(l); 1082 if (!buf) { 1083 err(1, "malloc"); 1084 /*NOTREACHED*/ 1085 } 1086 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1087 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1088 /*NOTREACHED*/ 1089 } 1090 1091 ep = (struct in6_defrouter *)(buf + l); 1092 for (p = (struct in6_defrouter *)buf; p < ep; p++) { 1093 int rtpref; 1094 1095 if (getnameinfo((struct sockaddr *)&p->rtaddr, 1096 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 1097 (nflag ? NI_NUMERICHOST : 0)) != 0) 1098 strlcpy(host_buf, "?", sizeof(host_buf)); 1099 1100 printf("%s if=%s", host_buf, 1101 if_indextoname(p->if_index, ifix_buf)); 1102 printf(", flags=%s%s", 1103 p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1104 p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 1105 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 1106 printf(", pref=%s", rtpref_str[rtpref]); 1107 1108 gettimeofday(&now, 0); 1109 if (p->expire == 0) 1110 printf(", expire=Never\n"); 1111 else 1112 printf(", expire=%s\n", 1113 sec2str(p->expire - now.tv_sec)); 1114 } 1115 free(buf); 1116} 1117 1118static void 1119plist() 1120{ 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 now; 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) { 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(&now, 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 >= now.tv_sec) 1184 printf(", expire=%s", 1185 sec2str(p->expire - now.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} 1231 1232static void 1233pfx_flush() 1234{ 1235 char dummyif[IFNAMSIZ+8]; 1236 int s; 1237 1238 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1239 err(1, "socket"); 1240 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1241 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1242 err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 1243} 1244 1245static void 1246rtr_flush() 1247{ 1248 char dummyif[IFNAMSIZ+8]; 1249 int s; 1250 1251 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1252 err(1, "socket"); 1253 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1254 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1255 err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 1256 1257 close(s); 1258} 1259 1260static void 1261harmonize_rtr() 1262{ 1263 char dummyif[IFNAMSIZ+8]; 1264 int s; 1265 1266 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1267 err(1, "socket"); 1268 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1269 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1270 err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 1271 1272 close(s); 1273} 1274 1275#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 1276static void 1277setdefif(char *ifname) 1278{ 1279 struct in6_ndifreq ndifreq; 1280 unsigned int ifindex; 1281 1282 if (strcasecmp(ifname, "delete") == 0) 1283 ifindex = 0; 1284 else { 1285 if ((ifindex = if_nametoindex(ifname)) == 0) 1286 err(1, "failed to resolve i/f index for %s", ifname); 1287 } 1288 1289 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1290 err(1, "socket"); 1291 1292 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1293 ndifreq.ifindex = ifindex; 1294 1295 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1296 err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 1297 1298 close(s); 1299} 1300 1301static void 1302getdefif() 1303{ 1304 struct in6_ndifreq ndifreq; 1305 char ifname[IFNAMSIZ+8]; 1306 1307 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1308 err(1, "socket"); 1309 1310 memset(&ndifreq, 0, sizeof(ndifreq)); 1311 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1312 1313 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1314 err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 1315 1316 if (ndifreq.ifindex == 0) 1317 printf("No default interface.\n"); 1318 else { 1319 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL) 1320 err(1, "failed to resolve ifname for index %lu", 1321 ndifreq.ifindex); 1322 printf("ND default interface = %s\n", ifname); 1323 } 1324 1325 close(s); 1326} 1327#endif 1328 1329static char * 1330sec2str(time_t total) 1331{ 1332 static char result[256]; 1333 int days, hours, mins, secs; 1334 int first = 1; 1335 char *p = result; 1336 char *ep = &result[sizeof(result)]; 1337 int n; 1338 1339 days = total / 3600 / 24; 1340 hours = (total / 3600) % 24; 1341 mins = (total / 60) % 60; 1342 secs = total % 60; 1343 1344 if (days) { 1345 first = 0; 1346 n = snprintf(p, ep - p, "%dd", days); 1347 if (n < 0 || n >= ep - p) 1348 return "?"; 1349 p += n; 1350 } 1351 if (!first || hours) { 1352 first = 0; 1353 n = snprintf(p, ep - p, "%dh", hours); 1354 if (n < 0 || n >= ep - p) 1355 return "?"; 1356 p += n; 1357 } 1358 if (!first || mins) { 1359 first = 0; 1360 n = snprintf(p, ep - p, "%dm", mins); 1361 if (n < 0 || n >= ep - p) 1362 return "?"; 1363 p += n; 1364 } 1365 snprintf(p, ep - p, "%ds", secs); 1366 1367 return(result); 1368} 1369 1370/* 1371 * Print the timestamp 1372 * from tcpdump/util.c 1373 */ 1374static void 1375ts_print(const struct timeval *tvp) 1376{ 1377 int s; 1378 1379 /* Default */ 1380 s = (tvp->tv_sec + thiszone) % 86400; 1381 (void)printf("%02d:%02d:%02d.%06u ", 1382 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 1383} 1384 1385#undef NEXTADDR 1386