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