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