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