ndp.c revision 186119
1/* $FreeBSD: head/usr.sbin/ndp/ndp.c 186119 2008-12-15 06:10:57Z qingli $ */ 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 122static pid_t pid; 123static int nflag; 124static int tflag; 125static int32_t thiszone; /* time difference with gmt */ 126static int s = -1; 127static int repeat = 0; 128 129char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */ 130char host_buf[NI_MAXHOST]; /* getnameinfo() */ 131char ifix_buf[IFNAMSIZ]; /* if_indextoname() */ 132 133int main(int, char **); 134int file(char *); 135void getsocket(void); 136int set(int, char **); 137void get(char *); 138int delete(char *); 139void dump(struct in6_addr *, int); 140static struct in6_nbrinfo *getnbrinfo(struct in6_addr *, int, int); 141static char *ether_str(struct sockaddr_dl *); 142int ndp_ether_aton(char *, u_char *); 143void usage(void); 144int rtmsg(int); 145void ifinfo(char *, int, char **); 146void rtrlist(void); 147void plist(void); 148void pfx_flush(void); 149void rtr_flush(void); 150void harmonize_rtr(void); 151#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 152static void getdefif(void); 153static void setdefif(char *); 154#endif 155static char *sec2str(time_t); 156static char *ether_str(struct sockaddr_dl *); 157static void ts_print(const struct timeval *); 158 159#ifdef ICMPV6CTL_ND6_DRLIST 160static char *rtpref_str[] = { 161 "medium", /* 00 */ 162 "high", /* 01 */ 163 "rsv", /* 10 */ 164 "low" /* 11 */ 165}; 166#endif 167 168int mode = 0; 169char *arg = NULL; 170 171int 172main(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 */ 323int 324file(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 356void 357getsocket() 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 368struct sockaddr_in6 so_mask = {sizeof(so_mask), AF_INET6 }; 369struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m; 370struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m; 371int expire_time, flags, found_entry; 372struct { 373 struct rt_msghdr m_rtm; 374 char m_space[512]; 375} m_rtmsg; 376 377/* 378 * Set an individual neighbor cache entry 379 */ 380int 381set(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 450overwrite: 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 */ 463void 464get(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 */ 500int 501delete(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 547delete: 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 if (rtmsg(RTM_DELETE) == 0) { 558 struct sockaddr_in6 s6 = *sin; /* XXX: for safety */ 559 560#ifdef __KAME__ 561 if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) { 562 s6.sin6_scope_id = ntohs(*(u_int16_t *)&s6.sin6_addr.s6_addr[2]); 563 *(u_int16_t *)&s6.sin6_addr.s6_addr[2] = 0; 564 } 565#endif 566 getnameinfo((struct sockaddr *)&s6, 567 s6.sin6_len, host_buf, 568 sizeof(host_buf), NULL, 0, 569 (nflag ? NI_NUMERICHOST : 0)); 570 printf("%s (%s) deleted\n", host, host_buf); 571 } 572 573 return 0; 574} 575 576#define W_ADDR 36 577#define W_LL 17 578#define W_IF 6 579 580/* 581 * Dump the entire neighbor cache 582 */ 583void 584dump(addr, cflag) 585 struct in6_addr *addr; 586 int cflag; 587{ 588 int mib[6]; 589 size_t needed; 590 char *lim, *buf, *next; 591 struct rt_msghdr *rtm; 592 struct sockaddr_in6 *sin; 593 struct sockaddr_dl *sdl; 594 extern int h_errno; 595 struct in6_nbrinfo *nbi; 596 struct timeval time; 597 int addrwidth; 598 int llwidth; 599 int ifwidth; 600 char flgbuf[8]; 601 char *ifname; 602 603 /* Print header */ 604 if (!tflag && !cflag) 605 printf("%-*.*s %-*.*s %*.*s %-9.9s %1s %5s\n", 606 W_ADDR, W_ADDR, "Neighbor", W_LL, W_LL, "Linklayer Address", 607 W_IF, W_IF, "Netif", "Expire", "S", "Flags"); 608 609again:; 610 mib[0] = CTL_NET; 611 mib[1] = PF_ROUTE; 612 mib[2] = 0; 613 mib[3] = AF_INET6; 614 mib[4] = NET_RT_FLAGS; 615#ifdef RTF_LLINFO 616 mib[5] = RTF_LLINFO; 617#else 618 mib[5] = 0; 619#endif 620 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 621 err(1, "sysctl(PF_ROUTE estimate)"); 622 if (needed > 0) { 623 if ((buf = malloc(needed)) == NULL) 624 err(1, "malloc"); 625 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 626 err(1, "sysctl(PF_ROUTE, NET_RT_FLAGS)"); 627 lim = buf + needed; 628 } else 629 buf = lim = NULL; 630 631 for (next = buf; next && next < lim; next += rtm->rtm_msglen) { 632 int isrouter = 0, prbs = 0; 633 634 rtm = (struct rt_msghdr *)next; 635 sin = (struct sockaddr_in6 *)(rtm + 1); 636 sdl = (struct sockaddr_dl *)((char *)sin + ROUNDUP(sin->sin6_len)); 637 638 /* 639 * Some OSes can produce a route that has the LINK flag but 640 * has a non-AF_LINK gateway (e.g. fe80::xx%lo0 on FreeBSD 641 * and BSD/OS, where xx is not the interface identifier on 642 * lo0). Such routes entry would annoy getnbrinfo() below, 643 * so we skip them. 644 * XXX: such routes should have the GATEWAY flag, not the 645 * LINK flag. However, there is rotten routing software 646 * that advertises all routes that have the GATEWAY flag. 647 * Thus, KAME kernel intentionally does not set the LINK flag. 648 * What is to be fixed is not ndp, but such routing software 649 * (and the kernel workaround)... 650 */ 651 if (sdl->sdl_family != AF_LINK) 652 continue; 653 654 if (!(rtm->rtm_flags & RTF_HOST)) 655 continue; 656 657 if (addr) { 658 if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr)) 659 continue; 660 found_entry = 1; 661 } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr)) 662 continue; 663 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) || 664 IN6_IS_ADDR_MC_LINKLOCAL(&sin->sin6_addr)) { 665 /* XXX: should scope id be filled in the kernel? */ 666 if (sin->sin6_scope_id == 0) 667 sin->sin6_scope_id = sdl->sdl_index; 668#ifdef __KAME__ 669 /* KAME specific hack; removed the embedded id */ 670 *(u_int16_t *)&sin->sin6_addr.s6_addr[2] = 0; 671#endif 672 } 673 getnameinfo((struct sockaddr *)sin, sin->sin6_len, host_buf, 674 sizeof(host_buf), NULL, 0, (nflag ? NI_NUMERICHOST : 0)); 675 if (cflag) { 676#ifdef RTF_WASCLONED 677 if (rtm->rtm_flags & RTF_WASCLONED) 678 delete(host_buf); 679#elif defined(RTF_CLONED) 680 if (rtm->rtm_flags & RTF_CLONED) 681 delete(host_buf); 682#else 683 delete(host_buf); 684#endif 685 continue; 686 } 687 gettimeofday(&time, 0); 688 if (tflag) 689 ts_print(&time); 690 691 addrwidth = strlen(host_buf); 692 if (addrwidth < W_ADDR) 693 addrwidth = W_ADDR; 694 llwidth = strlen(ether_str(sdl)); 695 if (W_ADDR + W_LL - addrwidth > llwidth) 696 llwidth = W_ADDR + W_LL - addrwidth; 697 ifname = if_indextoname(sdl->sdl_index, ifix_buf); 698 if (!ifname) 699 ifname = "?"; 700 ifwidth = strlen(ifname); 701 if (W_ADDR + W_LL + W_IF - addrwidth - llwidth > ifwidth) 702 ifwidth = W_ADDR + W_LL + W_IF - addrwidth - llwidth; 703 704 printf("%-*.*s %-*.*s %*.*s", addrwidth, addrwidth, host_buf, 705 llwidth, llwidth, ether_str(sdl), ifwidth, ifwidth, ifname); 706 707 /* Print neighbor discovery specific informations */ 708 nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index, 1); 709 if (nbi) { 710 if (nbi->expire > time.tv_sec) { 711 printf(" %-9.9s", 712 sec2str(nbi->expire - time.tv_sec)); 713 } else if (nbi->expire == 0) 714 printf(" %-9.9s", "permanent"); 715 else 716 printf(" %-9.9s", "expired"); 717 718 switch (nbi->state) { 719 case ND6_LLINFO_NOSTATE: 720 printf(" N"); 721 break; 722#ifdef ND6_LLINFO_WAITDELETE 723 case ND6_LLINFO_WAITDELETE: 724 printf(" W"); 725 break; 726#endif 727 case ND6_LLINFO_INCOMPLETE: 728 printf(" I"); 729 break; 730 case ND6_LLINFO_REACHABLE: 731 printf(" R"); 732 break; 733 case ND6_LLINFO_STALE: 734 printf(" S"); 735 break; 736 case ND6_LLINFO_DELAY: 737 printf(" D"); 738 break; 739 case ND6_LLINFO_PROBE: 740 printf(" P"); 741 break; 742 default: 743 printf(" ?"); 744 break; 745 } 746 747 isrouter = nbi->isrouter; 748 prbs = nbi->asked; 749 } else { 750 warnx("failed to get neighbor information"); 751 printf(" "); 752 } 753 754 /* 755 * other flags. R: router, P: proxy, W: ?? 756 */ 757 if ((rtm->rtm_addrs & RTA_NETMASK) == 0) { 758 snprintf(flgbuf, sizeof(flgbuf), "%s%s", 759 isrouter ? "R" : "", 760 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 761 } else { 762 sin = (struct sockaddr_in6 *) 763 (sdl->sdl_len + (char *)sdl); 764#if 0 /* W and P are mystery even for us */ 765 snprintf(flgbuf, sizeof(flgbuf), "%s%s%s%s", 766 isrouter ? "R" : "", 767 !IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "P" : "", 768 (sin->sin6_len != sizeof(struct sockaddr_in6)) ? "W" : "", 769 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 770#else 771 snprintf(flgbuf, sizeof(flgbuf), "%s%s", 772 isrouter ? "R" : "", 773 (rtm->rtm_flags & RTF_ANNOUNCE) ? "p" : ""); 774#endif 775 } 776 printf(" %s", flgbuf); 777 778 if (prbs) 779 printf(" %d", prbs); 780 781 printf("\n"); 782 } 783 if (buf != NULL) 784 free(buf); 785 786 if (repeat) { 787 printf("\n"); 788 fflush(stdout); 789 sleep(repeat); 790 goto again; 791 } 792} 793 794static struct in6_nbrinfo * 795getnbrinfo(addr, ifindex, warning) 796 struct in6_addr *addr; 797 int ifindex; 798 int warning; 799{ 800 static struct in6_nbrinfo nbi; 801 int s; 802 803 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 804 err(1, "socket"); 805 806 bzero(&nbi, sizeof(nbi)); 807 if_indextoname(ifindex, nbi.ifname); 808 nbi.addr = *addr; 809 if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) { 810 if (warning) 811 warn("ioctl(SIOCGNBRINFO_IN6)"); 812 close(s); 813 return(NULL); 814 } 815 816 close(s); 817 return(&nbi); 818} 819 820static char * 821ether_str(sdl) 822 struct sockaddr_dl *sdl; 823{ 824 static char hbuf[NI_MAXHOST]; 825 u_char *cp; 826 827 if (sdl->sdl_alen) { 828 cp = (u_char *)LLADDR(sdl); 829 snprintf(hbuf, sizeof(hbuf), "%x:%x:%x:%x:%x:%x", 830 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 831 } else 832 snprintf(hbuf, sizeof(hbuf), "(incomplete)"); 833 834 return(hbuf); 835} 836 837int 838ndp_ether_aton(a, n) 839 char *a; 840 u_char *n; 841{ 842 int i, o[6]; 843 844 i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], 845 &o[3], &o[4], &o[5]); 846 if (i != 6) { 847 fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a); 848 return (1); 849 } 850 for (i = 0; i < 6; i++) 851 n[i] = o[i]; 852 return (0); 853} 854 855void 856usage() 857{ 858 printf("usage: ndp [-nt] hostname\n"); 859 printf(" ndp [-nt] -a | -c | -p | -r | -H | -P | -R\n"); 860 printf(" ndp [-nt] -A wait\n"); 861 printf(" ndp [-nt] -d hostname\n"); 862 printf(" ndp [-nt] -f filename\n"); 863 printf(" ndp [-nt] -i interface [flags...]\n"); 864#ifdef SIOCSDEFIFACE_IN6 865 printf(" ndp [-nt] -I [interface|delete]\n"); 866#endif 867 printf(" ndp [-nt] -s nodename etheraddr [temp] [proxy]\n"); 868 exit(1); 869} 870 871int 872rtmsg(cmd) 873 int cmd; 874{ 875 static int seq; 876 int rlen; 877 register struct rt_msghdr *rtm = &m_rtmsg.m_rtm; 878 register char *cp = m_rtmsg.m_space; 879 register int l; 880 881 errno = 0; 882 if (cmd == RTM_DELETE) 883 goto doit; 884 bzero((char *)&m_rtmsg, sizeof(m_rtmsg)); 885 rtm->rtm_flags = flags; 886 rtm->rtm_version = RTM_VERSION; 887 888 switch (cmd) { 889 default: 890 fprintf(stderr, "ndp: internal wrong cmd\n"); 891 exit(1); 892 case RTM_ADD: 893 rtm->rtm_addrs |= RTA_GATEWAY; 894 if (expire_time) { 895 rtm->rtm_rmx.rmx_expire = expire_time; 896 rtm->rtm_inits = RTV_EXPIRE; 897 } 898 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC); 899#if 0 /* we don't support ipv6addr/128 type proxying */ 900 if (rtm->rtm_flags & RTF_ANNOUNCE) { 901 rtm->rtm_flags &= ~RTF_HOST; 902 rtm->rtm_addrs |= RTA_NETMASK; 903 } 904#endif 905 /* FALLTHROUGH */ 906 case RTM_GET: 907 rtm->rtm_addrs |= RTA_DST; 908 } 909 910 NEXTADDR(RTA_DST, sin_m); 911 NEXTADDR(RTA_GATEWAY, sdl_m); 912#if 0 /* we don't support ipv6addr/128 type proxying */ 913 memset(&so_mask.sin6_addr, 0xff, sizeof(so_mask.sin6_addr)); 914 NEXTADDR(RTA_NETMASK, so_mask); 915#endif 916 917 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 918doit: 919 l = rtm->rtm_msglen; 920 rtm->rtm_seq = ++seq; 921 rtm->rtm_type = cmd; 922 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 923 if (errno != ESRCH || cmd != RTM_DELETE) { 924 err(1, "writing to routing socket"); 925 /* NOTREACHED */ 926 } 927 } 928 do { 929 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 930 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid)); 931 if (l < 0) 932 (void) fprintf(stderr, "ndp: read from routing socket: %s\n", 933 strerror(errno)); 934 return (0); 935} 936 937void 938ifinfo(ifname, argc, argv) 939 char *ifname; 940 int argc; 941 char **argv; 942{ 943 struct in6_ndireq nd; 944 int i, s; 945 u_int32_t newflags; 946#ifdef IPV6CTL_USETEMPADDR 947 u_int8_t nullbuf[8]; 948#endif 949 950 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 951 err(1, "socket"); 952 /* NOTREACHED */ 953 } 954 bzero(&nd, sizeof(nd)); 955 strlcpy(nd.ifname, ifname, sizeof(nd.ifname)); 956 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 957 err(1, "ioctl(SIOCGIFINFO_IN6)"); 958 /* NOTREACHED */ 959 } 960#define ND nd.ndi 961 newflags = ND.flags; 962 for (i = 0; i < argc; i++) { 963 int clear = 0; 964 char *cp = argv[i]; 965 966 if (*cp == '-') { 967 clear = 1; 968 cp++; 969 } 970 971#define SETFLAG(s, f) \ 972 do {\ 973 if (strcmp(cp, (s)) == 0) {\ 974 if (clear)\ 975 newflags &= ~(f);\ 976 else\ 977 newflags |= (f);\ 978 }\ 979 } while (0) 980/* 981 * XXX: this macro is not 100% correct, in that it matches "nud" against 982 * "nudbogus". But we just let it go since this is minor. 983 */ 984#define SETVALUE(f, v) \ 985 do { \ 986 char *valptr; \ 987 unsigned long newval; \ 988 v = 0; /* unspecified */ \ 989 if (strncmp(cp, f, strlen(f)) == 0) { \ 990 valptr = strchr(cp, '='); \ 991 if (valptr == NULL) \ 992 err(1, "syntax error in %s field", (f)); \ 993 errno = 0; \ 994 newval = strtoul(++valptr, NULL, 0); \ 995 if (errno) \ 996 err(1, "syntax error in %s's value", (f)); \ 997 v = newval; \ 998 } \ 999 } while (0) 1000 1001 SETFLAG("disabled", ND6_IFF_IFDISABLED); 1002 SETFLAG("nud", ND6_IFF_PERFORMNUD); 1003#ifdef ND6_IFF_ACCEPT_RTADV 1004 SETFLAG("accept_rtadv", ND6_IFF_ACCEPT_RTADV); 1005#endif 1006#ifdef ND6_IFF_PREFER_SOURCE 1007 SETFLAG("prefer_source", ND6_IFF_PREFER_SOURCE); 1008#endif 1009 SETVALUE("basereachable", ND.basereachable); 1010 SETVALUE("retrans", ND.retrans); 1011 SETVALUE("curhlim", ND.chlim); 1012 1013 ND.flags = newflags; 1014 if (ioctl(s, SIOCSIFINFO_IN6, (caddr_t)&nd) < 0) { 1015 err(1, "ioctl(SIOCSIFINFO_IN6)"); 1016 /* NOTREACHED */ 1017 } 1018#undef SETFLAG 1019#undef SETVALUE 1020 } 1021 1022 if (!ND.initialized) { 1023 errx(1, "%s: not initialized yet", ifname); 1024 /* NOTREACHED */ 1025 } 1026 1027 if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) { 1028 err(1, "ioctl(SIOCGIFINFO_IN6)"); 1029 /* NOTREACHED */ 1030 } 1031 printf("linkmtu=%d", ND.linkmtu); 1032 printf(", maxmtu=%d", ND.maxmtu); 1033 printf(", curhlim=%d", ND.chlim); 1034 printf(", basereachable=%ds%dms", 1035 ND.basereachable / 1000, ND.basereachable % 1000); 1036 printf(", reachable=%ds", ND.reachable); 1037 printf(", retrans=%ds%dms", ND.retrans / 1000, ND.retrans % 1000); 1038#ifdef IPV6CTL_USETEMPADDR 1039 memset(nullbuf, 0, sizeof(nullbuf)); 1040 if (memcmp(nullbuf, ND.randomid, sizeof(nullbuf)) != 0) { 1041 int j; 1042 u_int8_t *rbuf; 1043 1044 for (i = 0; i < 3; i++) { 1045 switch (i) { 1046 case 0: 1047 printf("\nRandom seed(0): "); 1048 rbuf = ND.randomseed0; 1049 break; 1050 case 1: 1051 printf("\nRandom seed(1): "); 1052 rbuf = ND.randomseed1; 1053 break; 1054 case 2: 1055 printf("\nRandom ID: "); 1056 rbuf = ND.randomid; 1057 break; 1058 default: 1059 errx(1, "impossible case for tempaddr display"); 1060 } 1061 for (j = 0; j < 8; j++) 1062 printf("%02x", rbuf[j]); 1063 } 1064 } 1065#endif 1066 if (ND.flags) { 1067 printf("\nFlags: "); 1068#ifdef ND6_IFF_IFDISABLED 1069 if ((ND.flags & ND6_IFF_IFDISABLED)) 1070 printf("disabled "); 1071#endif 1072 if ((ND.flags & ND6_IFF_PERFORMNUD)) 1073 printf("nud "); 1074#ifdef ND6_IFF_ACCEPT_RTADV 1075 if ((ND.flags & ND6_IFF_ACCEPT_RTADV)) 1076 printf("accept_rtadv "); 1077#endif 1078#ifdef ND6_IFF_PREFER_SOURCE 1079 if ((ND.flags & ND6_IFF_PREFER_SOURCE)) 1080 printf("prefer_source "); 1081#endif 1082 } 1083 putc('\n', stdout); 1084#undef ND 1085 1086 close(s); 1087} 1088 1089#ifndef ND_RA_FLAG_RTPREF_MASK /* XXX: just for compilation on *BSD release */ 1090#define ND_RA_FLAG_RTPREF_MASK 0x18 /* 00011000 */ 1091#endif 1092 1093void 1094rtrlist() 1095{ 1096#ifdef ICMPV6CTL_ND6_DRLIST 1097 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST }; 1098 char *buf; 1099 struct in6_defrouter *p, *ep; 1100 size_t l; 1101 struct timeval time; 1102 1103 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1104 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1105 /*NOTREACHED*/ 1106 } 1107 if (l == 0) 1108 return; 1109 buf = malloc(l); 1110 if (!buf) { 1111 err(1, "malloc"); 1112 /*NOTREACHED*/ 1113 } 1114 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1115 err(1, "sysctl(ICMPV6CTL_ND6_DRLIST)"); 1116 /*NOTREACHED*/ 1117 } 1118 1119 ep = (struct in6_defrouter *)(buf + l); 1120 for (p = (struct in6_defrouter *)buf; p < ep; p++) { 1121 int rtpref; 1122 1123 if (getnameinfo((struct sockaddr *)&p->rtaddr, 1124 p->rtaddr.sin6_len, host_buf, sizeof(host_buf), NULL, 0, 1125 (nflag ? NI_NUMERICHOST : 0)) != 0) 1126 strlcpy(host_buf, "?", sizeof(host_buf)); 1127 1128 printf("%s if=%s", host_buf, 1129 if_indextoname(p->if_index, ifix_buf)); 1130 printf(", flags=%s%s", 1131 p->flags & ND_RA_FLAG_MANAGED ? "M" : "", 1132 p->flags & ND_RA_FLAG_OTHER ? "O" : ""); 1133 rtpref = ((p->flags & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff; 1134 printf(", pref=%s", rtpref_str[rtpref]); 1135 1136 gettimeofday(&time, 0); 1137 if (p->expire == 0) 1138 printf(", expire=Never\n"); 1139 else 1140 printf(", expire=%s\n", 1141 sec2str(p->expire - time.tv_sec)); 1142 } 1143 free(buf); 1144#else 1145 struct in6_drlist dr; 1146 int s, i; 1147 struct timeval time; 1148 1149 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1150 err(1, "socket"); 1151 /* NOTREACHED */ 1152 } 1153 bzero(&dr, sizeof(dr)); 1154 strlcpy(dr.ifname, "lo0", sizeof(dr.ifname)); /* dummy */ 1155 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 1156 err(1, "ioctl(SIOCGDRLST_IN6)"); 1157 /* NOTREACHED */ 1158 } 1159#define DR dr.defrouter[i] 1160 for (i = 0 ; DR.if_index && i < DRLSTSIZ ; i++) { 1161 struct sockaddr_in6 sin6; 1162 1163 bzero(&sin6, sizeof(sin6)); 1164 sin6.sin6_family = AF_INET6; 1165 sin6.sin6_len = sizeof(sin6); 1166 sin6.sin6_addr = DR.rtaddr; 1167 getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, host_buf, 1168 sizeof(host_buf), NULL, 0, 1169 (nflag ? NI_NUMERICHOST : 0)); 1170 1171 printf("%s if=%s", host_buf, 1172 if_indextoname(DR.if_index, ifix_buf)); 1173 printf(", flags=%s%s", 1174 DR.flags & ND_RA_FLAG_MANAGED ? "M" : "", 1175 DR.flags & ND_RA_FLAG_OTHER ? "O" : ""); 1176 gettimeofday(&time, 0); 1177 if (DR.expire == 0) 1178 printf(", expire=Never\n"); 1179 else 1180 printf(", expire=%s\n", 1181 sec2str(DR.expire - time.tv_sec)); 1182 } 1183#undef DR 1184 close(s); 1185#endif 1186} 1187 1188void 1189plist() 1190{ 1191#ifdef ICMPV6CTL_ND6_PRLIST 1192 int mib[] = { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST }; 1193 char *buf; 1194 struct in6_prefix *p, *ep, *n; 1195 struct sockaddr_in6 *advrtr; 1196 size_t l; 1197 struct timeval time; 1198 const int niflags = NI_NUMERICHOST; 1199 int ninflags = nflag ? NI_NUMERICHOST : 0; 1200 char namebuf[NI_MAXHOST]; 1201 1202 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { 1203 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1204 /*NOTREACHED*/ 1205 } 1206 buf = malloc(l); 1207 if (!buf) { 1208 err(1, "malloc"); 1209 /*NOTREACHED*/ 1210 } 1211 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { 1212 err(1, "sysctl(ICMPV6CTL_ND6_PRLIST)"); 1213 /*NOTREACHED*/ 1214 } 1215 1216 ep = (struct in6_prefix *)(buf + l); 1217 for (p = (struct in6_prefix *)buf; p < ep; p = n) { 1218 advrtr = (struct sockaddr_in6 *)(p + 1); 1219 n = (struct in6_prefix *)&advrtr[p->advrtrs]; 1220 1221 if (getnameinfo((struct sockaddr *)&p->prefix, 1222 p->prefix.sin6_len, namebuf, sizeof(namebuf), 1223 NULL, 0, niflags) != 0) 1224 strlcpy(namebuf, "?", sizeof(namebuf)); 1225 printf("%s/%d if=%s\n", namebuf, p->prefixlen, 1226 if_indextoname(p->if_index, ifix_buf)); 1227 1228 gettimeofday(&time, 0); 1229 /* 1230 * meaning of fields, especially flags, is very different 1231 * by origin. notify the difference to the users. 1232 */ 1233 printf("flags=%s%s%s%s%s", 1234 p->raflags.onlink ? "L" : "", 1235 p->raflags.autonomous ? "A" : "", 1236 (p->flags & NDPRF_ONLINK) != 0 ? "O" : "", 1237 (p->flags & NDPRF_DETACHED) != 0 ? "D" : "", 1238#ifdef NDPRF_HOME 1239 (p->flags & NDPRF_HOME) != 0 ? "H" : "" 1240#else 1241 "" 1242#endif 1243 ); 1244 if (p->vltime == ND6_INFINITE_LIFETIME) 1245 printf(" vltime=infinity"); 1246 else 1247 printf(" vltime=%lu", (unsigned long)p->vltime); 1248 if (p->pltime == ND6_INFINITE_LIFETIME) 1249 printf(", pltime=infinity"); 1250 else 1251 printf(", pltime=%lu", (unsigned long)p->pltime); 1252 if (p->expire == 0) 1253 printf(", expire=Never"); 1254 else if (p->expire >= time.tv_sec) 1255 printf(", expire=%s", 1256 sec2str(p->expire - time.tv_sec)); 1257 else 1258 printf(", expired"); 1259 printf(", ref=%d", p->refcnt); 1260 printf("\n"); 1261 /* 1262 * "advertising router" list is meaningful only if the prefix 1263 * information is from RA. 1264 */ 1265 if (p->advrtrs) { 1266 int j; 1267 struct sockaddr_in6 *sin6; 1268 1269 sin6 = advrtr; 1270 printf(" advertised by\n"); 1271 for (j = 0; j < p->advrtrs; j++) { 1272 struct in6_nbrinfo *nbi; 1273 1274 if (getnameinfo((struct sockaddr *)sin6, 1275 sin6->sin6_len, namebuf, sizeof(namebuf), 1276 NULL, 0, ninflags) != 0) 1277 strlcpy(namebuf, "?", sizeof(namebuf)); 1278 printf(" %s", namebuf); 1279 1280 nbi = getnbrinfo(&sin6->sin6_addr, 1281 p->if_index, 0); 1282 if (nbi) { 1283 switch (nbi->state) { 1284 case ND6_LLINFO_REACHABLE: 1285 case ND6_LLINFO_STALE: 1286 case ND6_LLINFO_DELAY: 1287 case ND6_LLINFO_PROBE: 1288 printf(" (reachable)\n"); 1289 break; 1290 default: 1291 printf(" (unreachable)\n"); 1292 } 1293 } else 1294 printf(" (no neighbor state)\n"); 1295 sin6++; 1296 } 1297 } else 1298 printf(" No advertising router\n"); 1299 } 1300 free(buf); 1301#else 1302 struct in6_prlist pr; 1303 int s, i; 1304 struct timeval time; 1305 1306 gettimeofday(&time, 0); 1307 1308 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 1309 err(1, "socket"); 1310 /* NOTREACHED */ 1311 } 1312 bzero(&pr, sizeof(pr)); 1313 strlcpy(pr.ifname, "lo0", sizeof(pr.ifname)); /* dummy */ 1314 if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) { 1315 err(1, "ioctl(SIOCGPRLST_IN6)"); 1316 /* NOTREACHED */ 1317 } 1318#define PR pr.prefix[i] 1319 for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) { 1320 struct sockaddr_in6 p6; 1321 char namebuf[NI_MAXHOST]; 1322 int niflags; 1323 1324#ifdef NDPRF_ONLINK 1325 p6 = PR.prefix; 1326#else 1327 memset(&p6, 0, sizeof(p6)); 1328 p6.sin6_family = AF_INET6; 1329 p6.sin6_len = sizeof(p6); 1330 p6.sin6_addr = PR.prefix; 1331#endif 1332 1333 /* 1334 * copy link index to sin6_scope_id field. 1335 * XXX: KAME specific. 1336 */ 1337 if (IN6_IS_ADDR_LINKLOCAL(&p6.sin6_addr)) { 1338 u_int16_t linkid; 1339 1340 memcpy(&linkid, &p6.sin6_addr.s6_addr[2], 1341 sizeof(linkid)); 1342 linkid = ntohs(linkid); 1343 p6.sin6_scope_id = linkid; 1344 p6.sin6_addr.s6_addr[2] = 0; 1345 p6.sin6_addr.s6_addr[3] = 0; 1346 } 1347 1348 niflags = NI_NUMERICHOST; 1349 if (getnameinfo((struct sockaddr *)&p6, 1350 sizeof(p6), namebuf, sizeof(namebuf), 1351 NULL, 0, niflags)) { 1352 warnx("getnameinfo failed"); 1353 continue; 1354 } 1355 printf("%s/%d if=%s\n", namebuf, PR.prefixlen, 1356 if_indextoname(PR.if_index, ifix_buf)); 1357 1358 gettimeofday(&time, 0); 1359 /* 1360 * meaning of fields, especially flags, is very different 1361 * by origin. notify the difference to the users. 1362 */ 1363#if 0 1364 printf(" %s", 1365 PR.origin == PR_ORIG_RA ? "" : "advertise: "); 1366#endif 1367#ifdef NDPRF_ONLINK 1368 printf("flags=%s%s%s%s%s", 1369 PR.raflags.onlink ? "L" : "", 1370 PR.raflags.autonomous ? "A" : "", 1371 (PR.flags & NDPRF_ONLINK) != 0 ? "O" : "", 1372 (PR.flags & NDPRF_DETACHED) != 0 ? "D" : "", 1373#ifdef NDPRF_HOME 1374 (PR.flags & NDPRF_HOME) != 0 ? "H" : "" 1375#else 1376 "" 1377#endif 1378 ); 1379#else 1380 printf("flags=%s%s", 1381 PR.raflags.onlink ? "L" : "", 1382 PR.raflags.autonomous ? "A" : ""); 1383#endif 1384 if (PR.vltime == ND6_INFINITE_LIFETIME) 1385 printf(" vltime=infinity"); 1386 else 1387 printf(" vltime=%lu", PR.vltime); 1388 if (PR.pltime == ND6_INFINITE_LIFETIME) 1389 printf(", pltime=infinity"); 1390 else 1391 printf(", pltime=%lu", PR.pltime); 1392 if (PR.expire == 0) 1393 printf(", expire=Never"); 1394 else if (PR.expire >= time.tv_sec) 1395 printf(", expire=%s", 1396 sec2str(PR.expire - time.tv_sec)); 1397 else 1398 printf(", expired"); 1399#ifdef NDPRF_ONLINK 1400 printf(", ref=%d", PR.refcnt); 1401#endif 1402#if 0 1403 switch (PR.origin) { 1404 case PR_ORIG_RA: 1405 printf(", origin=RA"); 1406 break; 1407 case PR_ORIG_RR: 1408 printf(", origin=RR"); 1409 break; 1410 case PR_ORIG_STATIC: 1411 printf(", origin=static"); 1412 break; 1413 case PR_ORIG_KERNEL: 1414 printf(", origin=kernel"); 1415 break; 1416 default: 1417 printf(", origin=?"); 1418 break; 1419 } 1420#endif 1421 printf("\n"); 1422 /* 1423 * "advertising router" list is meaningful only if the prefix 1424 * information is from RA. 1425 */ 1426 if (0 && /* prefix origin is almost obsolted */ 1427 PR.origin != PR_ORIG_RA) 1428 ; 1429 else if (PR.advrtrs) { 1430 int j; 1431 printf(" advertised by\n"); 1432 for (j = 0; j < PR.advrtrs; j++) { 1433 struct sockaddr_in6 sin6; 1434 struct in6_nbrinfo *nbi; 1435 1436 bzero(&sin6, sizeof(sin6)); 1437 sin6.sin6_family = AF_INET6; 1438 sin6.sin6_len = sizeof(sin6); 1439 sin6.sin6_addr = PR.advrtr[j]; 1440 sin6.sin6_scope_id = PR.if_index; /* XXX */ 1441 getnameinfo((struct sockaddr *)&sin6, 1442 sin6.sin6_len, host_buf, 1443 sizeof(host_buf), NULL, 0, 1444 (nflag ? NI_NUMERICHOST : 0)); 1445 printf(" %s", host_buf); 1446 1447 nbi = getnbrinfo(&sin6.sin6_addr, 1448 PR.if_index, 0); 1449 if (nbi) { 1450 switch (nbi->state) { 1451 case ND6_LLINFO_REACHABLE: 1452 case ND6_LLINFO_STALE: 1453 case ND6_LLINFO_DELAY: 1454 case ND6_LLINFO_PROBE: 1455 printf(" (reachable)\n"); 1456 break; 1457 default: 1458 printf(" (unreachable)\n"); 1459 } 1460 } else 1461 printf(" (no neighbor state)\n"); 1462 } 1463 if (PR.advrtrs > DRLSTSIZ) 1464 printf(" and %d routers\n", 1465 PR.advrtrs - DRLSTSIZ); 1466 } else 1467 printf(" No advertising router\n"); 1468 } 1469#undef PR 1470 close(s); 1471#endif 1472} 1473 1474void 1475pfx_flush() 1476{ 1477 char dummyif[IFNAMSIZ+8]; 1478 int s; 1479 1480 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1481 err(1, "socket"); 1482 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1483 if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0) 1484 err(1, "ioctl(SIOCSPFXFLUSH_IN6)"); 1485} 1486 1487void 1488rtr_flush() 1489{ 1490 char dummyif[IFNAMSIZ+8]; 1491 int s; 1492 1493 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1494 err(1, "socket"); 1495 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1496 if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0) 1497 err(1, "ioctl(SIOCSRTRFLUSH_IN6)"); 1498 1499 close(s); 1500} 1501 1502void 1503harmonize_rtr() 1504{ 1505 char dummyif[IFNAMSIZ+8]; 1506 int s; 1507 1508 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1509 err(1, "socket"); 1510 strlcpy(dummyif, "lo0", sizeof(dummyif)); /* dummy */ 1511 if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) 1512 err(1, "ioctl(SIOCSNDFLUSH_IN6)"); 1513 1514 close(s); 1515} 1516 1517#ifdef SIOCSDEFIFACE_IN6 /* XXX: check SIOCGDEFIFACE_IN6 as well? */ 1518static void 1519setdefif(ifname) 1520 char *ifname; 1521{ 1522 struct in6_ndifreq ndifreq; 1523 unsigned int ifindex; 1524 1525 if (strcasecmp(ifname, "delete") == 0) 1526 ifindex = 0; 1527 else { 1528 if ((ifindex = if_nametoindex(ifname)) == 0) 1529 err(1, "failed to resolve i/f index for %s", ifname); 1530 } 1531 1532 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1533 err(1, "socket"); 1534 1535 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1536 ndifreq.ifindex = ifindex; 1537 1538 if (ioctl(s, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1539 err(1, "ioctl(SIOCSDEFIFACE_IN6)"); 1540 1541 close(s); 1542} 1543 1544static void 1545getdefif() 1546{ 1547 struct in6_ndifreq ndifreq; 1548 char ifname[IFNAMSIZ+8]; 1549 1550 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 1551 err(1, "socket"); 1552 1553 memset(&ndifreq, 0, sizeof(ndifreq)); 1554 strlcpy(ndifreq.ifname, "lo0", sizeof(ndifreq.ifname)); /* dummy */ 1555 1556 if (ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq) < 0) 1557 err(1, "ioctl(SIOCGDEFIFACE_IN6)"); 1558 1559 if (ndifreq.ifindex == 0) 1560 printf("No default interface.\n"); 1561 else { 1562 if ((if_indextoname(ndifreq.ifindex, ifname)) == NULL) 1563 err(1, "failed to resolve ifname for index %lu", 1564 ndifreq.ifindex); 1565 printf("ND default interface = %s\n", ifname); 1566 } 1567 1568 close(s); 1569} 1570#endif 1571 1572static char * 1573sec2str(total) 1574 time_t total; 1575{ 1576 static char result[256]; 1577 int days, hours, mins, secs; 1578 int first = 1; 1579 char *p = result; 1580 char *ep = &result[sizeof(result)]; 1581 int n; 1582 1583 days = total / 3600 / 24; 1584 hours = (total / 3600) % 24; 1585 mins = (total / 60) % 60; 1586 secs = total % 60; 1587 1588 if (days) { 1589 first = 0; 1590 n = snprintf(p, ep - p, "%dd", days); 1591 if (n < 0 || n >= ep - p) 1592 return "?"; 1593 p += n; 1594 } 1595 if (!first || hours) { 1596 first = 0; 1597 n = snprintf(p, ep - p, "%dh", hours); 1598 if (n < 0 || n >= ep - p) 1599 return "?"; 1600 p += n; 1601 } 1602 if (!first || mins) { 1603 first = 0; 1604 n = snprintf(p, ep - p, "%dm", mins); 1605 if (n < 0 || n >= ep - p) 1606 return "?"; 1607 p += n; 1608 } 1609 snprintf(p, ep - p, "%ds", secs); 1610 1611 return(result); 1612} 1613 1614/* 1615 * Print the timestamp 1616 * from tcpdump/util.c 1617 */ 1618static void 1619ts_print(tvp) 1620 const struct timeval *tvp; 1621{ 1622 int s; 1623 1624 /* Default */ 1625 s = (tvp->tv_sec + thiszone) % 86400; 1626 (void)printf("%02d:%02d:%02d.%06u ", 1627 s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec); 1628} 1629 1630#undef NEXTADDR 1631