1 /* $NetBSD: ping6.c,v 1.70 2006/09/23 16:18:04 elad Exp $ */ 2 /* $KAME: ping6.c,v 1.164 2002/11/16 14:05:37 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 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 /* BSDI ping.c,v 2.3 1996/01/21 17:56:50 jch Exp */ 34 35 /* 36 * Copyright (c) 1989, 1993 37 * The Regents of the University of California. All rights reserved. 38 * 39 * This code is derived from software contributed to Berkeley by 40 * Mike Muuss. 41 * 42 * Redistribution and use in source and binary forms, with or without 43 * modification, are permitted provided that the following conditions 44 * are met: 45 * 1. Redistributions of source code must retain the above copyright 46 * notice, this list of conditions and the following disclaimer. 47 * 2. Redistributions in binary form must reproduce the above copyright 48 * notice, this list of conditions and the following disclaimer in the 49 * documentation and/or other materials provided with the distribution. 50 * 3. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 */ 66 67 #if 0 68 #ifndef lint 69 static char copyright[] = 70 "@(#) Copyright (c) 1989, 1993\n\ 71 The Regents of the University of California. All rights reserved.\n"; 72 #endif /* not lint */ 73 74 #ifndef lint 75 static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 76 #endif /* not lint */ 77 #else 78 #include <sys/cdefs.h> 79 #ifndef lint 80 __RCSID("$NetBSD: ping6.c,v 1.70 2006/09/23 16:18:04 elad Exp $"); 81 #endif 82 #endif 83 84 /* 85 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility, 86 * measure round-trip-delays and packet loss across network paths. 87 * 88 * Author - 89 * Mike Muuss 90 * U. S. Army Ballistic Research Laboratory 91 * December, 1983 92 * 93 * Status - 94 * Public Domain. Distribution Unlimited. 95 * Bugs - 96 * More statistics could always be gathered. 97 * This program has to run SUID to ROOT to access the ICMP socket. 98 */ 99 /* 100 * NOTE: 101 * USE_SIN6_SCOPE_ID assumes that sin6_scope_id has the same semantics 102 * as IPV6_PKTINFO. Some people object it (sin6_scope_id specifies *link* 103 * while IPV6_PKTINFO specifies *interface*. Link is defined as collection of 104 * network attached to 1 or more interfaces) 105 */ 106 107 #include <sys/param.h> 108 #include <sys/uio.h> 109 #include <sys/socket.h> 110 #include <sys/time.h> 111 112 #include <net/if.h> 113 #include <net/route.h> 114 115 #include <netinet/in.h> 116 #include <netinet/ip6.h> 117 #include <netinet/icmp6.h> 118 #include <arpa/inet.h> 119 #include <arpa/nameser.h> 120 #include <netdb.h> 121 122 #include <ctype.h> 123 #include <err.h> 124 #include <errno.h> 125 #include <fcntl.h> 126 #include <math.h> 127 #include <signal.h> 128 #include <stdio.h> 129 #include <stdlib.h> 130 #include <string.h> 131 #include <unistd.h> 132 #include <poll.h> 133 134 #ifdef IPSEC 135 #include <netinet6/ah.h> 136 #include <netinet6/ipsec.h> 137 #endif 138 139 #include <md5.h> 140 141 struct tv32 { 142 u_int32_t tv32_sec; 143 u_int32_t tv32_usec; 144 }; 145 146 #define MAXPACKETLEN 131072 147 #define IP6LEN 40 148 #define ICMP6ECHOLEN 8 /* icmp echo header len excluding time */ 149 #define ICMP6ECHOTMLEN sizeof(struct tv32) 150 #define ICMP6_NIQLEN (ICMP6ECHOLEN + 8) 151 /* FQDN case, 64 bits of nonce + 32 bits ttl */ 152 #define ICMP6_NIRLEN (ICMP6ECHOLEN + 12) 153 #define EXTRA 256 /* for AH and various other headers. weird. */ 154 #define DEFDATALEN ICMP6ECHOTMLEN 155 #define MAXDATALEN MAXPACKETLEN - IP6LEN - ICMP6ECHOLEN 156 #define NROUTES 9 /* number of record route slots */ 157 158 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 159 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 160 #define SET(bit) (A(bit) |= B(bit)) 161 #define CLR(bit) (A(bit) &= (~B(bit))) 162 #define TST(bit) (A(bit) & B(bit)) 163 164 #define F_FLOOD 0x0001 165 #define F_INTERVAL 0x0002 166 #define F_PINGFILLED 0x0008 167 #define F_QUIET 0x0010 168 #define F_RROUTE 0x0020 169 #define F_SO_DEBUG 0x0040 170 #define F_VERBOSE 0x0100 171 #ifdef IPSEC 172 #ifdef IPSEC_POLICY_IPSEC 173 #define F_POLICY 0x0400 174 #else 175 #define F_AUTHHDR 0x0200 176 #define F_ENCRYPT 0x0400 177 #endif /*IPSEC_POLICY_IPSEC*/ 178 #endif /*IPSEC*/ 179 #define F_NODEADDR 0x0800 180 #define F_FQDN 0x1000 181 #define F_INTERFACE 0x2000 182 #define F_SRCADDR 0x4000 183 #ifdef IPV6_REACHCONF 184 #define F_REACHCONF 0x8000 185 #endif 186 #define F_HOSTNAME 0x10000 187 #define F_FQDNOLD 0x20000 188 #define F_NIGROUP 0x40000 189 #define F_SUPTYPES 0x80000 190 #define F_NOMINMTU 0x100000 191 #define F_NOUSERDATA (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES) 192 u_int options; 193 194 #define IN6LEN sizeof(struct in6_addr) 195 #define SA6LEN sizeof(struct sockaddr_in6) 196 #define DUMMY_PORT 10101 197 198 #define SIN6(s) ((struct sockaddr_in6 *)(s)) 199 200 /* 201 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 202 * number of received sequence numbers we can keep track of. Change 128 203 * to 8192 for complete accuracy... 204 */ 205 #define MAX_DUP_CHK (8 * 8192) 206 int mx_dup_ck = MAX_DUP_CHK; 207 char rcvd_tbl[MAX_DUP_CHK / 8]; 208 209 struct addrinfo *res; 210 struct sockaddr_in6 dst; /* who to ping6 */ 211 struct sockaddr_in6 src; /* src addr of this packet */ 212 socklen_t srclen; 213 int datalen = DEFDATALEN; 214 int s; /* socket file descriptor */ 215 u_char outpack[MAXPACKETLEN]; 216 char BSPACE = '\b'; /* characters written for flood */ 217 char DOT = '.'; 218 char *hostname; 219 int ident; /* process id to identify our packets */ 220 u_int8_t nonce[8]; /* nonce field for node information */ 221 int hoplimit = -1; /* hoplimit */ 222 int pathmtu = 0; /* path MTU for the destination. 0 = unspec. */ 223 224 /* counters */ 225 long npackets; /* max packets to transmit */ 226 long nreceived; /* # of packets we got back */ 227 long nrepeats; /* number of duplicates */ 228 long ntransmitted; /* sequence # for outbound packets = #sent */ 229 struct timeval interval = {1, 0}; /* interval between packets */ 230 231 /* timing */ 232 int timing; /* flag to do timing */ 233 double tmin = 999999999.0; /* minimum round trip time */ 234 double tmax = 0.0; /* maximum round trip time */ 235 double tsum = 0.0; /* sum of all times, for doing average */ 236 double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 237 238 /* for node addresses */ 239 u_short naflags; 240 241 /* for ancillary data(advanced API) */ 242 struct msghdr smsghdr; 243 struct iovec smsgiov; 244 char *scmsg = 0; 245 246 volatile sig_atomic_t seenalrm; 247 volatile sig_atomic_t seenint; 248 #ifdef SIGINFO 249 volatile sig_atomic_t seeninfo; 250 #endif 251 252 void fill(char *, char *); 253 int get_hoplim(struct msghdr *); 254 int get_pathmtu(struct msghdr *); 255 struct in6_pktinfo *get_rcvpktinfo(struct msghdr *); 256 void onsignal(int); 257 void retransmit(void); 258 void onint(int); 259 size_t pingerlen(void); 260 int pinger(void); 261 const char *pr_addr(struct sockaddr *, int); 262 void pr_icmph(struct icmp6_hdr *, u_char *); 263 void pr_iph(struct ip6_hdr *); 264 void pr_suptypes(struct icmp6_nodeinfo *, size_t); 265 void pr_nodeaddr(struct icmp6_nodeinfo *, int); 266 int myechoreply(const struct icmp6_hdr *); 267 int mynireply(const struct icmp6_nodeinfo *); 268 char *dnsdecode(const u_char **, const u_char *, const u_char *, 269 char *, size_t); 270 void pr_pack(u_char *, int, struct msghdr *); 271 void pr_exthdrs(struct msghdr *); 272 void pr_ip6opt(void *); 273 void pr_rthdr(void *); 274 int pr_bitrange(u_int32_t, int, int); 275 void pr_retip(struct ip6_hdr *, u_char *); 276 void summary(void); 277 void tvsub(struct timeval *, struct timeval *); 278 int setpolicy(int, char *); 279 char *nigroup(char *); 280 void usage(void); 281 282 int 283 main(int argc, char *argv[]) 284 { 285 struct itimerval itimer; 286 struct sockaddr_in6 from; 287 int timeout; 288 struct addrinfo hints; 289 struct pollfd fdmaskp[1]; 290 int cc, i; 291 int ch, hold, packlen, preload, optval, ret_ga; 292 u_char *datap, *packet; 293 char *e, *target, *ifname = NULL, *gateway = NULL; 294 int ip6optlen = 0; 295 struct cmsghdr *scmsgp = NULL; 296 #if defined(SO_SNDBUF) && defined(SO_RCVBUF) 297 u_long lsockbufsize; 298 int sockbufsize = 0; 299 #endif 300 int usepktinfo = 0; 301 struct in6_pktinfo *pktinfo = NULL; 302 struct ip6_rthdr *rthdr = NULL; 303 #ifdef IPSEC_POLICY_IPSEC 304 char *policy_in = NULL; 305 char *policy_out = NULL; 306 #endif 307 double intval; 308 size_t rthlen; 309 #ifdef IPV6_USE_MIN_MTU 310 int mflag = 0; 311 #endif 312 313 /* just to be sure */ 314 memset(&smsghdr, 0, sizeof(smsghdr)); 315 memset(&smsgiov, 0, sizeof(smsgiov)); 316 317 preload = 0; 318 datap = &outpack[ICMP6ECHOLEN + ICMP6ECHOTMLEN]; 319 #ifndef IPSEC 320 #define ADDOPTS 321 #else 322 #ifdef IPSEC_POLICY_IPSEC 323 #define ADDOPTS "P:" 324 #else 325 #define ADDOPTS "AE" 326 #endif /*IPSEC_POLICY_IPSEC*/ 327 #endif 328 while ((ch = getopt(argc, argv, 329 "a:b:c:dfHg:h:I:i:l:mnNp:qRS:s:tvwW" ADDOPTS)) != -1) { 330 #undef ADDOPTS 331 switch (ch) { 332 case 'a': 333 { 334 char *cp; 335 336 options &= ~F_NOUSERDATA; 337 options |= F_NODEADDR; 338 for (cp = optarg; *cp != '\0'; cp++) { 339 switch (*cp) { 340 case 'a': 341 naflags |= NI_NODEADDR_FLAG_ALL; 342 break; 343 case 'c': 344 case 'C': 345 naflags |= NI_NODEADDR_FLAG_COMPAT; 346 break; 347 case 'l': 348 case 'L': 349 naflags |= NI_NODEADDR_FLAG_LINKLOCAL; 350 break; 351 case 's': 352 case 'S': 353 naflags |= NI_NODEADDR_FLAG_SITELOCAL; 354 break; 355 case 'g': 356 case 'G': 357 naflags |= NI_NODEADDR_FLAG_GLOBAL; 358 break; 359 case 'A': /* experimental. not in the spec */ 360 #ifdef NI_NODEADDR_FLAG_ANYCAST 361 naflags |= NI_NODEADDR_FLAG_ANYCAST; 362 break; 363 #else 364 errx(1, 365 "-a A is not supported on the platform"); 366 /*NOTREACHED*/ 367 #endif 368 default: 369 usage(); 370 /*NOTREACHED*/ 371 } 372 } 373 break; 374 } 375 case 'b': 376 #if defined(SO_SNDBUF) && defined(SO_RCVBUF) 377 errno = 0; 378 e = NULL; 379 lsockbufsize = strtoul(optarg, &e, 10); 380 sockbufsize = lsockbufsize; 381 if (errno || !*optarg || *e || 382 sockbufsize != lsockbufsize) 383 errx(1, "invalid socket buffer size"); 384 #else 385 errx(1, 386 "-b option ignored: SO_SNDBUF/SO_RCVBUF socket options not supported"); 387 #endif 388 break; 389 case 'c': 390 npackets = strtol(optarg, &e, 10); 391 if (npackets <= 0 || *optarg == '\0' || *e != '\0') 392 errx(1, 393 "illegal number of packets -- %s", optarg); 394 break; 395 case 'd': 396 options |= F_SO_DEBUG; 397 break; 398 case 'f': 399 if (getuid()) { 400 errno = EPERM; 401 errx(1, "Must be superuser to flood ping"); 402 } 403 options |= F_FLOOD; 404 setbuf(stdout, (char *)NULL); 405 break; 406 case 'g': 407 gateway = optarg; 408 break; 409 case 'H': 410 options |= F_HOSTNAME; 411 break; 412 case 'h': /* hoplimit */ 413 hoplimit = strtol(optarg, &e, 10); 414 if (*optarg == '\0' || *e != '\0') 415 errx(1, "illegal hoplimit %s", optarg); 416 if (255 < hoplimit || hoplimit < -1) 417 errx(1, 418 "illegal hoplimit -- %s", optarg); 419 break; 420 case 'I': 421 ifname = optarg; 422 options |= F_INTERFACE; 423 #ifndef USE_SIN6_SCOPE_ID 424 usepktinfo++; 425 #endif 426 break; 427 case 'i': /* wait between sending packets */ 428 intval = strtod(optarg, &e); 429 if (*optarg == '\0' || *e != '\0') 430 errx(1, "illegal timing interval %s", optarg); 431 if (intval < 1 && getuid()) { 432 errx(1, "%s: only root may use interval < 1s", 433 strerror(EPERM)); 434 } 435 interval.tv_sec = (long)intval; 436 interval.tv_usec = 437 (long)((intval - interval.tv_sec) * 1000000); 438 if (interval.tv_sec < 0) 439 errx(1, "illegal timing interval %s", optarg); 440 /* less than 1/hz does not make sense */ 441 if (interval.tv_sec == 0 && interval.tv_usec < 10000) { 442 warnx("too small interval, raised to 0.01"); 443 interval.tv_usec = 10000; 444 } 445 options |= F_INTERVAL; 446 break; 447 case 'l': 448 if (getuid()) { 449 errno = EPERM; 450 errx(1, "Must be superuser to preload"); 451 } 452 preload = strtol(optarg, &e, 10); 453 if (preload < 0 || *optarg == '\0' || *e != '\0') 454 errx(1, "illegal preload value -- %s", optarg); 455 break; 456 case 'm': 457 #ifdef IPV6_USE_MIN_MTU 458 mflag++; 459 break; 460 #else 461 errx(1, "-%c is not supported on this platform", ch); 462 /*NOTREACHED*/ 463 #endif 464 case 'n': 465 options &= ~F_HOSTNAME; 466 break; 467 case 'N': 468 options |= F_NIGROUP; 469 break; 470 case 'p': /* fill buffer with user pattern */ 471 options |= F_PINGFILLED; 472 fill((char *)datap, optarg); 473 break; 474 case 'q': 475 options |= F_QUIET; 476 break; 477 case 'R': 478 #ifdef IPV6_REACHCONF 479 options |= F_REACHCONF; 480 break; 481 #else 482 errx(1, "-R is not supported in this configuration"); 483 #endif 484 case 'S': 485 memset(&hints, 0, sizeof(struct addrinfo)); 486 hints.ai_flags = AI_NUMERICHOST; /* allow hostname? */ 487 hints.ai_family = AF_INET6; 488 hints.ai_socktype = SOCK_RAW; 489 hints.ai_protocol = IPPROTO_ICMPV6; 490 491 ret_ga = getaddrinfo(optarg, NULL, &hints, &res); 492 if (ret_ga) { 493 errx(1, "invalid source address: %s", 494 gai_strerror(ret_ga)); 495 } 496 /* 497 * res->ai_family must be AF_INET6 and res->ai_addrlen 498 * must be sizeof(src). 499 */ 500 memcpy(&src, res->ai_addr, res->ai_addrlen); 501 srclen = res->ai_addrlen; 502 freeaddrinfo(res); 503 options |= F_SRCADDR; 504 break; 505 case 's': /* size of packet to send */ 506 datalen = strtol(optarg, &e, 10); 507 if (datalen <= 0 || *optarg == '\0' || *e != '\0') 508 errx(1, "illegal datalen value -- %s", optarg); 509 if (datalen > MAXDATALEN) { 510 errx(1, 511 "datalen value too large, maximum is %d", 512 MAXDATALEN); 513 } 514 break; 515 case 't': 516 options &= ~F_NOUSERDATA; 517 options |= F_SUPTYPES; 518 break; 519 case 'v': 520 options |= F_VERBOSE; 521 break; 522 case 'w': 523 options &= ~F_NOUSERDATA; 524 options |= F_FQDN; 525 break; 526 case 'W': 527 options &= ~F_NOUSERDATA; 528 options |= F_FQDNOLD; 529 break; 530 #ifdef IPSEC 531 #ifdef IPSEC_POLICY_IPSEC 532 case 'P': 533 options |= F_POLICY; 534 if (!strncmp("in", optarg, 2)) { 535 if ((policy_in = strdup(optarg)) == NULL) 536 errx(1, "strdup"); 537 } else if (!strncmp("out", optarg, 3)) { 538 if ((policy_out = strdup(optarg)) == NULL) 539 errx(1, "strdup"); 540 } else 541 errx(1, "invalid security policy"); 542 break; 543 #else 544 case 'A': 545 options |= F_AUTHHDR; 546 break; 547 case 'E': 548 options |= F_ENCRYPT; 549 break; 550 #endif /*IPSEC_POLICY_IPSEC*/ 551 #endif /*IPSEC*/ 552 default: 553 usage(); 554 /*NOTREACHED*/ 555 } 556 } 557 558 argc -= optind; 559 argv += optind; 560 561 if (argc < 1) { 562 usage(); 563 /*NOTREACHED*/ 564 } 565 566 if (argc > 1) { 567 rthlen = CMSG_SPACE(inet6_rth_space(IPV6_RTHDR_TYPE_0, 568 argc - 1)); 569 if (rthlen == 0) { 570 errx(1, "too many intermediate hops"); 571 /*NOTREACHED*/ 572 } 573 ip6optlen += rthlen; 574 } 575 576 if (options & F_NIGROUP) { 577 target = nigroup(argv[argc - 1]); 578 if (target == NULL) { 579 usage(); 580 /*NOTREACHED*/ 581 } 582 } else 583 target = argv[argc - 1]; 584 585 /* getaddrinfo */ 586 memset(&hints, 0, sizeof(struct addrinfo)); 587 hints.ai_flags = AI_CANONNAME; 588 hints.ai_family = AF_INET6; 589 hints.ai_socktype = SOCK_RAW; 590 hints.ai_protocol = IPPROTO_ICMPV6; 591 592 ret_ga = getaddrinfo(target, NULL, &hints, &res); 593 if (ret_ga) 594 errx(1, "%s", gai_strerror(ret_ga)); 595 if (res->ai_canonname) 596 hostname = res->ai_canonname; 597 else 598 hostname = target; 599 600 if (!res->ai_addr) 601 errx(1, "getaddrinfo failed"); 602 603 (void)memcpy(&dst, res->ai_addr, res->ai_addrlen); 604 605 if ((s = socket(res->ai_family, res->ai_socktype, 606 res->ai_protocol)) < 0) 607 err(1, "socket"); 608 609 /* set the source address if specified. */ 610 if ((options & F_SRCADDR) && 611 bind(s, (struct sockaddr *)&src, srclen) != 0) { 612 err(1, "bind"); 613 } 614 615 /* set the gateway (next hop) if specified */ 616 if (gateway) { 617 struct addrinfo ghints, *gres; 618 int error; 619 620 memset(&ghints, 0, sizeof(ghints)); 621 ghints.ai_family = AF_INET6; 622 ghints.ai_socktype = SOCK_RAW; 623 ghints.ai_protocol = IPPROTO_ICMPV6; 624 625 error = getaddrinfo(gateway, NULL, &hints, &gres); 626 if (error) { 627 errx(1, "getaddrinfo for the gateway %s: %s", 628 gateway, gai_strerror(error)); 629 } 630 if (gres->ai_next && (options & F_VERBOSE)) 631 warnx("gateway resolves to multiple addresses"); 632 633 if (setsockopt(s, IPPROTO_IPV6, IPV6_NEXTHOP, 634 gres->ai_addr, gres->ai_addrlen)) { 635 err(1, "setsockopt(IPV6_NEXTHOP)"); 636 } 637 638 freeaddrinfo(gres); 639 } 640 641 /* 642 * let the kerel pass extension headers of incoming packets, 643 * for privileged socket options 644 */ 645 if ((options & F_VERBOSE) != 0) { 646 int opton = 1; 647 648 #ifdef IPV6_RECVHOPOPTS 649 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &opton, 650 sizeof(opton))) 651 err(1, "setsockopt(IPV6_RECVHOPOPTS)"); 652 #else /* old adv. API */ 653 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPOPTS, &opton, 654 sizeof(opton))) 655 err(1, "setsockopt(IPV6_HOPOPTS)"); 656 #endif 657 #ifdef IPV6_RECVDSTOPTS 658 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &opton, 659 sizeof(opton))) 660 err(1, "setsockopt(IPV6_RECVDSTOPTS)"); 661 #else /* old adv. API */ 662 if (setsockopt(s, IPPROTO_IPV6, IPV6_DSTOPTS, &opton, 663 sizeof(opton))) 664 err(1, "setsockopt(IPV6_DSTOPTS)"); 665 #endif 666 #ifdef IPV6_RECVRTHDRDSTOPTS 667 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDRDSTOPTS, &opton, 668 sizeof(opton))) 669 err(1, "setsockopt(IPV6_RECVRTHDRDSTOPTS)"); 670 #endif 671 } 672 673 /* revoke root privilege */ 674 seteuid(getuid()); 675 setuid(getuid()); 676 677 if ((options & F_FLOOD) && (options & F_INTERVAL)) 678 errx(1, "-f and -i incompatible options"); 679 680 if ((options & F_NOUSERDATA) == 0) { 681 if (datalen >= sizeof(struct tv32)) { 682 /* we can time transfer */ 683 timing = 1; 684 } else 685 timing = 0; 686 /* in F_VERBOSE case, we may get non-echoreply packets*/ 687 if (options & F_VERBOSE) 688 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA; 689 else 690 packlen = datalen + IP6LEN + ICMP6ECHOLEN + EXTRA; 691 } else { 692 /* suppress timing for node information query */ 693 timing = 0; 694 datalen = 2048; 695 packlen = 2048 + IP6LEN + ICMP6ECHOLEN + EXTRA; 696 } 697 698 if (!(packet = (u_char *)malloc((u_int)packlen))) 699 err(1, "Unable to allocate packet"); 700 if (!(options & F_PINGFILLED)) 701 for (i = ICMP6ECHOLEN; i < packlen; ++i) 702 *datap++ = i; 703 704 ident = arc4random() & 0xFFFF; 705 memset(nonce, 0, sizeof(nonce)); 706 for (i = 0; i < sizeof(nonce); i += sizeof(u_int32_t)) 707 *((u_int32_t *)&nonce[i]) = arc4random(); 708 709 hold = 1; 710 711 if (options & F_SO_DEBUG) 712 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 713 sizeof(hold)); 714 optval = IPV6_DEFHLIM; 715 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr)) 716 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 717 &optval, sizeof(optval)) == -1) 718 err(1, "IPV6_MULTICAST_HOPS"); 719 #ifdef IPV6_USE_MIN_MTU 720 if (mflag != 1) { 721 optval = mflag > 1 ? 0 : 1; 722 723 if (setsockopt(s, IPPROTO_IPV6, IPV6_USE_MIN_MTU, 724 &optval, sizeof(optval)) == -1) 725 err(1, "setsockopt(IPV6_USE_MIN_MTU)"); 726 } 727 #ifdef IPV6_RECVPATHMTU 728 else { 729 optval = 1; 730 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPATHMTU, 731 &optval, sizeof(optval)) == -1) 732 err(1, "setsockopt(IPV6_RECVPATHMTU)"); 733 } 734 #endif /* IPV6_RECVPATHMTU */ 735 #endif /* IPV6_USE_MIN_MTU */ 736 737 #ifdef IPSEC 738 #ifdef IPSEC_POLICY_IPSEC 739 if (options & F_POLICY) { 740 if (setpolicy(s, policy_in) < 0) 741 errx(1, "%s", ipsec_strerror()); 742 if (setpolicy(s, policy_out) < 0) 743 errx(1, "%s", ipsec_strerror()); 744 } 745 #else 746 if (options & F_AUTHHDR) { 747 optval = IPSEC_LEVEL_REQUIRE; 748 #ifdef IPV6_AUTH_TRANS_LEVEL 749 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL, 750 &optval, sizeof(optval)) == -1) 751 err(1, "setsockopt(IPV6_AUTH_TRANS_LEVEL)"); 752 #else /* old def */ 753 if (setsockopt(s, IPPROTO_IPV6, IPV6_AUTH_LEVEL, 754 &optval, sizeof(optval)) == -1) 755 err(1, "setsockopt(IPV6_AUTH_LEVEL)"); 756 #endif 757 } 758 if (options & F_ENCRYPT) { 759 optval = IPSEC_LEVEL_REQUIRE; 760 if (setsockopt(s, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL, 761 &optval, sizeof(optval)) == -1) 762 err(1, "setsockopt(IPV6_ESP_TRANS_LEVEL)"); 763 } 764 #endif /*IPSEC_POLICY_IPSEC*/ 765 #endif 766 767 #ifdef ICMP6_FILTER 768 { 769 struct icmp6_filter filt; 770 if (!(options & F_VERBOSE)) { 771 ICMP6_FILTER_SETBLOCKALL(&filt); 772 if ((options & F_FQDN) || (options & F_FQDNOLD) || 773 (options & F_NODEADDR) || (options & F_SUPTYPES)) 774 ICMP6_FILTER_SETPASS(ICMP6_NI_REPLY, &filt); 775 else 776 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt); 777 } else { 778 ICMP6_FILTER_SETPASSALL(&filt); 779 } 780 if (setsockopt(s, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, 781 sizeof(filt)) < 0) 782 err(1, "setsockopt(ICMP6_FILTER)"); 783 } 784 #endif /*ICMP6_FILTER*/ 785 786 /* let the kerel pass extension headers of incoming packets */ 787 if ((options & F_VERBOSE) != 0) { 788 int opton = 1; 789 790 #ifdef IPV6_RECVRTHDR 791 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVRTHDR, &opton, 792 sizeof(opton))) 793 err(1, "setsockopt(IPV6_RECVRTHDR)"); 794 #else /* old adv. API */ 795 if (setsockopt(s, IPPROTO_IPV6, IPV6_RTHDR, &opton, 796 sizeof(opton))) 797 err(1, "setsockopt(IPV6_RTHDR)"); 798 #endif 799 } 800 801 /* 802 optval = 1; 803 if (IN6_IS_ADDR_MULTICAST(&dst.sin6_addr)) 804 if (setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, 805 &optval, sizeof(optval)) == -1) 806 err(1, "IPV6_MULTICAST_LOOP"); 807 */ 808 809 /* Specify the outgoing interface and/or the source address */ 810 if (usepktinfo) 811 ip6optlen += CMSG_SPACE(sizeof(struct in6_pktinfo)); 812 813 if (hoplimit != -1) 814 ip6optlen += CMSG_SPACE(sizeof(int)); 815 816 #ifdef IPV6_REACHCONF 817 if (options & F_REACHCONF) 818 ip6optlen += CMSG_SPACE(0); 819 #endif 820 821 /* set IP6 packet options */ 822 if (ip6optlen) { 823 if ((scmsg = (char *)malloc(ip6optlen)) == 0) 824 errx(1, "can't allocate enough memory"); 825 smsghdr.msg_control = (caddr_t)scmsg; 826 smsghdr.msg_controllen = ip6optlen; 827 scmsgp = (struct cmsghdr *)scmsg; 828 } 829 if (usepktinfo) { 830 pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp)); 831 memset(pktinfo, 0, sizeof(*pktinfo)); 832 scmsgp->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 833 scmsgp->cmsg_level = IPPROTO_IPV6; 834 scmsgp->cmsg_type = IPV6_PKTINFO; 835 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp); 836 } 837 838 /* set the outgoing interface */ 839 if (ifname) { 840 #ifndef USE_SIN6_SCOPE_ID 841 /* pktinfo must have already been allocated */ 842 if ((pktinfo->ipi6_ifindex = if_nametoindex(ifname)) == 0) 843 errx(1, "%s: invalid interface name", ifname); 844 #else 845 if ((dst.sin6_scope_id = if_nametoindex(ifname)) == 0) 846 errx(1, "%s: invalid interface name", ifname); 847 #endif 848 } 849 if (hoplimit != -1) { 850 scmsgp->cmsg_len = CMSG_LEN(sizeof(int)); 851 scmsgp->cmsg_level = IPPROTO_IPV6; 852 scmsgp->cmsg_type = IPV6_HOPLIMIT; 853 *(int *)(CMSG_DATA(scmsgp)) = hoplimit; 854 855 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp); 856 } 857 #ifdef IPV6_REACHCONF 858 if (options & F_REACHCONF) { 859 scmsgp->cmsg_len = CMSG_LEN(0); 860 scmsgp->cmsg_level = IPPROTO_IPV6; 861 scmsgp->cmsg_type = IPV6_REACHCONF; 862 863 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp); 864 } 865 #endif 866 867 if (argc > 1) { /* some intermediate addrs are specified */ 868 int hops, error; 869 int rthdrlen; 870 871 rthdrlen = inet6_rth_space(IPV6_RTHDR_TYPE_0, argc - 1); 872 scmsgp->cmsg_len = CMSG_LEN(rthdrlen); 873 scmsgp->cmsg_level = IPPROTO_IPV6; 874 scmsgp->cmsg_type = IPV6_RTHDR; 875 rthdr = (struct ip6_rthdr *)CMSG_DATA(scmsgp); 876 rthdr = inet6_rth_init((void *)rthdr, rthdrlen, 877 IPV6_RTHDR_TYPE_0, argc - 1); 878 if (rthdr == NULL) 879 errx(1, "can't initialize rthdr"); 880 881 for (hops = 0; hops < argc - 1; hops++) { 882 struct addrinfo *iaip; 883 884 if ((error = getaddrinfo(argv[hops], NULL, &hints, 885 &iaip))) 886 errx(1, "%s", gai_strerror(error)); 887 if (SIN6(iaip->ai_addr)->sin6_family != AF_INET6) 888 errx(1, 889 "bad addr family of an intermediate addr"); 890 891 if (inet6_rth_add(rthdr, 892 &(SIN6(iaip->ai_addr))->sin6_addr)) 893 errx(1, "can't add an intermediate node"); 894 freeaddrinfo(iaip); 895 } 896 897 scmsgp = CMSG_NXTHDR(&smsghdr, scmsgp); 898 } 899 900 if (!(options & F_SRCADDR)) { 901 /* 902 * get the source address. XXX since we revoked the root 903 * privilege, we cannot use a raw socket for this. 904 */ 905 int dummy; 906 socklen_t len = sizeof(src); 907 908 if ((dummy = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) 909 err(1, "UDP socket"); 910 911 src.sin6_family = AF_INET6; 912 src.sin6_addr = dst.sin6_addr; 913 src.sin6_port = ntohs(DUMMY_PORT); 914 src.sin6_scope_id = dst.sin6_scope_id; 915 916 if (pktinfo && 917 setsockopt(dummy, IPPROTO_IPV6, IPV6_PKTINFO, 918 (void *)pktinfo, sizeof(*pktinfo))) 919 err(1, "UDP setsockopt(IPV6_PKTINFO)"); 920 921 if (hoplimit != -1 && 922 setsockopt(dummy, IPPROTO_IPV6, IPV6_UNICAST_HOPS, 923 (void *)&hoplimit, sizeof(hoplimit))) 924 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)"); 925 926 if (hoplimit != -1 && 927 setsockopt(dummy, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, 928 (void *)&hoplimit, sizeof(hoplimit))) 929 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)"); 930 931 if (rthdr && 932 setsockopt(dummy, IPPROTO_IPV6, IPV6_RTHDR, 933 (void *)rthdr, (rthdr->ip6r_len + 1) << 3)) 934 err(1, "UDP setsockopt(IPV6_RTHDR)"); 935 936 if (connect(dummy, (struct sockaddr *)&src, len) < 0) 937 err(1, "UDP connect"); 938 939 if (getsockname(dummy, (struct sockaddr *)&src, &len) < 0) 940 err(1, "getsockname"); 941 942 close(dummy); 943 } 944 945 #if defined(SO_SNDBUF) && defined(SO_RCVBUF) 946 if (sockbufsize) { 947 if (datalen > sockbufsize) 948 warnx("you need -b to increase socket buffer size"); 949 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sockbufsize, 950 sizeof(sockbufsize)) < 0) 951 err(1, "setsockopt(SO_SNDBUF)"); 952 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &sockbufsize, 953 sizeof(sockbufsize)) < 0) 954 err(1, "setsockopt(SO_RCVBUF)"); 955 } 956 else { 957 if (datalen > 8 * 1024) /*XXX*/ 958 warnx("you need -b to increase socket buffer size"); 959 /* 960 * When pinging the broadcast address, you can get a lot of 961 * answers. Doing something so evil is useful if you are trying 962 * to stress the ethernet, or just want to fill the arp cache 963 * to get some stuff for /etc/ethers. 964 */ 965 hold = 48 * 1024; 966 setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 967 sizeof(hold)); 968 } 969 #endif 970 971 optval = 1; 972 #ifndef USE_SIN6_SCOPE_ID 973 #ifdef IPV6_RECVPKTINFO 974 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval, 975 sizeof(optval)) < 0) 976 warn("setsockopt(IPV6_RECVPKTINFO)"); /* XXX err? */ 977 #else /* old adv. API */ 978 if (setsockopt(s, IPPROTO_IPV6, IPV6_PKTINFO, &optval, 979 sizeof(optval)) < 0) 980 warn("setsockopt(IPV6_PKTINFO)"); /* XXX err? */ 981 #endif 982 #endif /* USE_SIN6_SCOPE_ID */ 983 #ifdef IPV6_RECVHOPLIMIT 984 if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval, 985 sizeof(optval)) < 0) 986 warn("setsockopt(IPV6_RECVHOPLIMIT)"); /* XXX err? */ 987 #else /* old adv. API */ 988 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval, 989 sizeof(optval)) < 0) 990 warn("setsockopt(IPV6_HOPLIMIT)"); /* XXX err? */ 991 #endif 992 993 printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()), 994 (unsigned long)(pingerlen() - 8)); 995 printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src))); 996 printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst))); 997 998 while (preload--) /* Fire off them quickies. */ 999 (void)pinger(); 1000 1001 (void)signal(SIGINT, onsignal); 1002 #ifdef SIGINFO 1003 (void)signal(SIGINFO, onsignal); 1004 #endif 1005 1006 if ((options & F_FLOOD) == 0) { 1007 (void)signal(SIGALRM, onsignal); 1008 itimer.it_interval = interval; 1009 itimer.it_value = interval; 1010 (void)setitimer(ITIMER_REAL, &itimer, NULL); 1011 if (ntransmitted) 1012 retransmit(); 1013 } 1014 1015 seenalrm = seenint = 0; 1016 #ifdef SIGINFO 1017 seeninfo = 0; 1018 #endif 1019 1020 for (;;) { 1021 struct msghdr m; 1022 struct cmsghdr *cm; 1023 u_char buf[1024]; 1024 struct iovec iov[2]; 1025 1026 /* signal handling */ 1027 if (seenalrm) { 1028 retransmit(); 1029 seenalrm = 0; 1030 continue; 1031 } 1032 if (seenint) { 1033 onint(SIGINT); 1034 seenint = 0; 1035 continue; 1036 } 1037 #ifdef SIGINFO 1038 if (seeninfo) { 1039 summary(); 1040 seeninfo = 0; 1041 continue; 1042 } 1043 #endif 1044 1045 if (options & F_FLOOD) { 1046 (void)pinger(); 1047 timeout = 10; 1048 } else { 1049 timeout = INFTIM; 1050 } 1051 fdmaskp[0].fd = s; 1052 fdmaskp[0].events = POLLIN; 1053 cc = poll(fdmaskp, 1, timeout); 1054 if (cc < 0) { 1055 if (errno != EINTR) { 1056 warn("poll"); 1057 sleep(1); 1058 } 1059 continue; 1060 } else if (cc == 0) 1061 continue; 1062 1063 m.msg_name = (caddr_t)&from; 1064 m.msg_namelen = sizeof(from); 1065 memset(&iov, 0, sizeof(iov)); 1066 iov[0].iov_base = (caddr_t)packet; 1067 iov[0].iov_len = packlen; 1068 m.msg_iov = iov; 1069 m.msg_iovlen = 1; 1070 cm = (struct cmsghdr *)buf; 1071 m.msg_control = (caddr_t)buf; 1072 m.msg_controllen = sizeof(buf); 1073 1074 cc = recvmsg(s, &m, 0); 1075 if (cc < 0) { 1076 if (errno != EINTR) { 1077 warn("recvmsg"); 1078 sleep(1); 1079 } 1080 continue; 1081 } else if (cc == 0) { 1082 int mtu; 1083 1084 /* 1085 * receive control messages only. Process the 1086 * exceptions (currently the only possiblity is 1087 * a path MTU notification.) 1088 */ 1089 if ((mtu = get_pathmtu(&m)) > 0) { 1090 if ((options & F_VERBOSE) != 0) { 1091 printf("new path MTU (%d) is " 1092 "notified\n", mtu); 1093 } 1094 } 1095 continue; 1096 } else { 1097 /* 1098 * an ICMPv6 message (probably an echoreply) arrived. 1099 */ 1100 pr_pack(packet, cc, &m); 1101 } 1102 if (npackets && nreceived >= npackets) 1103 break; 1104 } 1105 summary(); 1106 exit(nreceived == 0); 1107 } 1108 1109 void 1110 onsignal(int sig) 1111 { 1112 1113 switch (sig) { 1114 case SIGALRM: 1115 seenalrm++; 1116 break; 1117 case SIGINT: 1118 seenint++; 1119 break; 1120 #ifdef SIGINFO 1121 case SIGINFO: 1122 seeninfo++; 1123 break; 1124 #endif 1125 } 1126 } 1127 1128 /* 1129 * retransmit -- 1130 * This routine transmits another ping6. 1131 */ 1132 void 1133 retransmit(void) 1134 { 1135 struct itimerval itimer; 1136 1137 if (pinger() == 0) 1138 return; 1139 1140 /* 1141 * If we're not transmitting any more packets, change the timer 1142 * to wait two round-trip times if we've received any packets or 1143 * ten seconds if we haven't. 1144 */ 1145 #define MAXWAIT 10 1146 if (nreceived) { 1147 itimer.it_value.tv_sec = 2 * tmax / 1000; 1148 if (itimer.it_value.tv_sec == 0) 1149 itimer.it_value.tv_sec = 1; 1150 } else 1151 itimer.it_value.tv_sec = MAXWAIT; 1152 itimer.it_interval.tv_sec = 0; 1153 itimer.it_interval.tv_usec = 0; 1154 itimer.it_value.tv_usec = 0; 1155 1156 (void)signal(SIGALRM, onint); 1157 (void)setitimer(ITIMER_REAL, &itimer, NULL); 1158 } 1159 1160 /* 1161 * pinger -- 1162 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 1163 * will be added on by the kernel. The ID field is our UNIX process ID, 1164 * and the sequence number is an ascending integer. The first 8 bytes 1165 * of the data portion are used to hold a UNIX "timeval" struct in VAX 1166 * byte-order, to compute the round-trip time. 1167 */ 1168 size_t 1169 pingerlen(void) 1170 { 1171 size_t l; 1172 1173 if (options & F_FQDN) 1174 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr); 1175 else if (options & F_FQDNOLD) 1176 l = ICMP6_NIQLEN; 1177 else if (options & F_NODEADDR) 1178 l = ICMP6_NIQLEN + sizeof(dst.sin6_addr); 1179 else if (options & F_SUPTYPES) 1180 l = ICMP6_NIQLEN; 1181 else 1182 l = ICMP6ECHOLEN + datalen; 1183 1184 return l; 1185 } 1186 1187 int 1188 pinger(void) 1189 { 1190 struct icmp6_hdr *icp; 1191 struct iovec iov[2]; 1192 int i, cc; 1193 struct icmp6_nodeinfo *nip; 1194 int seq; 1195 1196 if (npackets && ntransmitted >= npackets) 1197 return(-1); /* no more transmission */ 1198 1199 icp = (struct icmp6_hdr *)outpack; 1200 nip = (struct icmp6_nodeinfo *)outpack; 1201 memset(icp, 0, sizeof(*icp)); 1202 icp->icmp6_cksum = 0; 1203 seq = ntransmitted++; 1204 CLR(seq % mx_dup_ck); 1205 1206 if (options & F_FQDN) { 1207 icp->icmp6_type = ICMP6_NI_QUERY; 1208 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6; 1209 nip->ni_qtype = htons(NI_QTYPE_FQDN); 1210 nip->ni_flags = htons(0); 1211 1212 memcpy(nip->icmp6_ni_nonce, nonce, 1213 sizeof(nip->icmp6_ni_nonce)); 1214 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq); 1215 1216 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr, 1217 sizeof(dst.sin6_addr)); 1218 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr); 1219 datalen = 0; 1220 } else if (options & F_FQDNOLD) { 1221 /* packet format in 03 draft - no Subject data on queries */ 1222 icp->icmp6_type = ICMP6_NI_QUERY; 1223 icp->icmp6_code = 0; /* code field is always 0 */ 1224 nip->ni_qtype = htons(NI_QTYPE_FQDN); 1225 nip->ni_flags = htons(0); 1226 1227 memcpy(nip->icmp6_ni_nonce, nonce, 1228 sizeof(nip->icmp6_ni_nonce)); 1229 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq); 1230 1231 cc = ICMP6_NIQLEN; 1232 datalen = 0; 1233 } else if (options & F_NODEADDR) { 1234 icp->icmp6_type = ICMP6_NI_QUERY; 1235 icp->icmp6_code = ICMP6_NI_SUBJ_IPV6; 1236 nip->ni_qtype = htons(NI_QTYPE_NODEADDR); 1237 nip->ni_flags = naflags; 1238 1239 memcpy(nip->icmp6_ni_nonce, nonce, 1240 sizeof(nip->icmp6_ni_nonce)); 1241 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq); 1242 1243 memcpy(&outpack[ICMP6_NIQLEN], &dst.sin6_addr, 1244 sizeof(dst.sin6_addr)); 1245 cc = ICMP6_NIQLEN + sizeof(dst.sin6_addr); 1246 datalen = 0; 1247 } else if (options & F_SUPTYPES) { 1248 icp->icmp6_type = ICMP6_NI_QUERY; 1249 icp->icmp6_code = ICMP6_NI_SUBJ_FQDN; /*empty*/ 1250 nip->ni_qtype = htons(NI_QTYPE_SUPTYPES); 1251 /* we support compressed bitmap */ 1252 nip->ni_flags = NI_SUPTYPE_FLAG_COMPRESS; 1253 1254 memcpy(nip->icmp6_ni_nonce, nonce, 1255 sizeof(nip->icmp6_ni_nonce)); 1256 *(u_int16_t *)nip->icmp6_ni_nonce = ntohs(seq); 1257 cc = ICMP6_NIQLEN; 1258 datalen = 0; 1259 } else { 1260 icp->icmp6_type = ICMP6_ECHO_REQUEST; 1261 icp->icmp6_code = 0; 1262 icp->icmp6_id = htons(ident); 1263 icp->icmp6_seq = ntohs(seq); 1264 if (timing) { 1265 struct timeval tv; 1266 struct tv32 *tv32; 1267 (void)gettimeofday(&tv, NULL); 1268 tv32 = (struct tv32 *)&outpack[ICMP6ECHOLEN]; 1269 tv32->tv32_sec = htonl(tv.tv_sec); 1270 tv32->tv32_usec = htonl(tv.tv_usec); 1271 } 1272 cc = ICMP6ECHOLEN + datalen; 1273 } 1274 1275 #ifdef DIAGNOSTIC 1276 if (pingerlen() != cc) 1277 errx(1, "internal error; length mismatch"); 1278 #endif 1279 1280 smsghdr.msg_name = (caddr_t)&dst; 1281 smsghdr.msg_namelen = sizeof(dst); 1282 memset(&iov, 0, sizeof(iov)); 1283 iov[0].iov_base = (caddr_t)outpack; 1284 iov[0].iov_len = cc; 1285 smsghdr.msg_iov = iov; 1286 smsghdr.msg_iovlen = 1; 1287 1288 i = sendmsg(s, &smsghdr, 0); 1289 1290 if (i < 0 || i != cc) { 1291 if (i < 0) 1292 warn("sendmsg"); 1293 (void)printf("ping6: wrote %s %d chars, ret=%d\n", 1294 hostname, cc, i); 1295 } 1296 if (!(options & F_QUIET) && options & F_FLOOD) 1297 (void)write(STDOUT_FILENO, &DOT, 1); 1298 1299 return(0); 1300 } 1301 1302 int 1303 myechoreply(const struct icmp6_hdr *icp) 1304 { 1305 if (ntohs(icp->icmp6_id) == ident) 1306 return 1; 1307 else 1308 return 0; 1309 } 1310 1311 int 1312 mynireply(const struct icmp6_nodeinfo *nip) 1313 { 1314 if (memcmp(nip->icmp6_ni_nonce + sizeof(u_int16_t), 1315 nonce + sizeof(u_int16_t), 1316 sizeof(nonce) - sizeof(u_int16_t)) == 0) 1317 return 1; 1318 else 1319 return 0; 1320 } 1321 1322 char * 1323 dnsdecode(const u_char **sp, const u_char *ep, const u_char *base, char *buf, 1324 size_t bufsiz) 1325 { 1326 int i; 1327 const u_char *cp; 1328 char cresult[MAXDNAME + 1]; 1329 const u_char *comp; 1330 int l; 1331 1332 i = 0; /* XXXGCC -Wuninitialized [sun2] */ 1333 1334 cp = *sp; 1335 *buf = '\0'; 1336 1337 if (cp >= ep) 1338 return NULL; 1339 while (cp < ep) { 1340 i = *cp; 1341 if (i == 0 || cp != *sp) { 1342 if (strlcat((char *)buf, ".", bufsiz) >= bufsiz) 1343 return NULL; /*result overrun*/ 1344 } 1345 if (i == 0) 1346 break; 1347 cp++; 1348 1349 if ((i & 0xc0) == 0xc0 && cp - base > (i & 0x3f)) { 1350 /* DNS compression */ 1351 if (!base) 1352 return NULL; 1353 1354 comp = base + (i & 0x3f); 1355 if (dnsdecode(&comp, cp, base, cresult, 1356 sizeof(cresult)) == NULL) 1357 return NULL; 1358 if (strlcat(buf, cresult, bufsiz) >= bufsiz) 1359 return NULL; /*result overrun*/ 1360 break; 1361 } else if ((i & 0x3f) == i) { 1362 if (i > ep - cp) 1363 return NULL; /*source overrun*/ 1364 while (i-- > 0 && cp < ep) { 1365 l = snprintf(cresult, sizeof(cresult), 1366 isprint(*cp) ? "%c" : "\\%03o", *cp & 0xff); 1367 if (l >= sizeof(cresult) || l < 0) 1368 return NULL; 1369 if (strlcat(buf, cresult, bufsiz) >= bufsiz) 1370 return NULL; /*result overrun*/ 1371 cp++; 1372 } 1373 } else 1374 return NULL; /*invalid label*/ 1375 } 1376 if (i != 0) 1377 return NULL; /*not terminated*/ 1378 cp++; 1379 *sp = cp; 1380 return buf; 1381 } 1382 1383 /* 1384 * pr_pack -- 1385 * Print out the packet, if it came from us. This logic is necessary 1386 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 1387 * which arrive ('tis only fair). This permits multiple copies of this 1388 * program to be run without having intermingled output (or statistics!). 1389 */ 1390 void 1391 pr_pack(u_char *buf, int cc, struct msghdr *mhdr) 1392 { 1393 #define safeputc(c) printf((isprint((c)) ? "%c" : "\\%03o"), c) 1394 struct icmp6_hdr *icp; 1395 struct icmp6_nodeinfo *ni; 1396 int i; 1397 int hoplim; 1398 struct sockaddr *from; 1399 int fromlen; 1400 u_char *cp = NULL, *dp, *end = buf + cc; 1401 struct in6_pktinfo *pktinfo = NULL; 1402 struct timeval tv, tp; 1403 struct tv32 *tpp; 1404 double triptime = 0; 1405 int dupflag; 1406 size_t off; 1407 int oldfqdn; 1408 u_int16_t seq; 1409 char dnsname[MAXDNAME + 1]; 1410 1411 (void)gettimeofday(&tv, NULL); 1412 1413 if (!mhdr || !mhdr->msg_name || 1414 mhdr->msg_namelen != sizeof(struct sockaddr_in6) || 1415 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET6) { 1416 if (options & F_VERBOSE) 1417 warnx("invalid peername"); 1418 return; 1419 } 1420 from = (struct sockaddr *)mhdr->msg_name; 1421 fromlen = mhdr->msg_namelen; 1422 if (cc < sizeof(struct icmp6_hdr)) { 1423 if (options & F_VERBOSE) 1424 warnx("packet too short (%d bytes) from %s", cc, 1425 pr_addr(from, fromlen)); 1426 return; 1427 } 1428 icp = (struct icmp6_hdr *)buf; 1429 ni = (struct icmp6_nodeinfo *)buf; 1430 off = 0; 1431 1432 if ((hoplim = get_hoplim(mhdr)) == -1) { 1433 warnx("failed to get receiving hop limit"); 1434 return; 1435 } 1436 if ((pktinfo = get_rcvpktinfo(mhdr)) == NULL) { 1437 warnx("failed to get receiving packet information"); 1438 return; 1439 } 1440 1441 if (icp->icmp6_type == ICMP6_ECHO_REPLY && myechoreply(icp)) { 1442 seq = ntohs(icp->icmp6_seq); 1443 ++nreceived; 1444 if (timing) { 1445 tpp = (struct tv32 *)(icp + 1); 1446 tp.tv_sec = ntohl(tpp->tv32_sec); 1447 tp.tv_usec = ntohl(tpp->tv32_usec); 1448 tvsub(&tv, &tp); 1449 triptime = ((double)tv.tv_sec) * 1000.0 + 1450 ((double)tv.tv_usec) / 1000.0; 1451 tsum += triptime; 1452 tsumsq += triptime * triptime; 1453 if (triptime < tmin) 1454 tmin = triptime; 1455 if (triptime > tmax) 1456 tmax = triptime; 1457 } 1458 1459 if (TST(seq % mx_dup_ck)) { 1460 ++nrepeats; 1461 --nreceived; 1462 dupflag = 1; 1463 } else { 1464 SET(seq % mx_dup_ck); 1465 dupflag = 0; 1466 } 1467 1468 if (options & F_QUIET) 1469 return; 1470 1471 if (options & F_FLOOD) 1472 (void)write(STDOUT_FILENO, &BSPACE, 1); 1473 else { 1474 (void)printf("%d bytes from %s, icmp_seq=%u", cc, 1475 pr_addr(from, fromlen), seq); 1476 (void)printf(" hlim=%d", hoplim); 1477 if ((options & F_VERBOSE) != 0) { 1478 struct sockaddr_in6 dstsa; 1479 1480 memset(&dstsa, 0, sizeof(dstsa)); 1481 dstsa.sin6_family = AF_INET6; 1482 #ifdef SIN6_LEN 1483 dstsa.sin6_len = sizeof(dstsa); 1484 #endif 1485 dstsa.sin6_scope_id = pktinfo->ipi6_ifindex; 1486 dstsa.sin6_addr = pktinfo->ipi6_addr; 1487 (void)printf(" dst=%s", 1488 pr_addr((struct sockaddr *)&dstsa, 1489 sizeof(dstsa))); 1490 } 1491 if (timing) 1492 (void)printf(" time=%.3f ms", triptime); 1493 if (dupflag) 1494 (void)printf("(DUP!)"); 1495 /* check the data */ 1496 cp = buf + off + ICMP6ECHOLEN + ICMP6ECHOTMLEN; 1497 dp = outpack + ICMP6ECHOLEN + ICMP6ECHOTMLEN; 1498 for (i = 8; cp < end; ++i, ++cp, ++dp) { 1499 if (*cp != *dp) { 1500 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", i, *dp, *cp); 1501 break; 1502 } 1503 } 1504 } 1505 } else if (icp->icmp6_type == ICMP6_NI_REPLY && mynireply(ni)) { 1506 seq = ntohs(*(u_int16_t *)ni->icmp6_ni_nonce); 1507 ++nreceived; 1508 if (TST(seq % mx_dup_ck)) { 1509 ++nrepeats; 1510 --nreceived; 1511 dupflag = 1; 1512 } else { 1513 SET(seq % mx_dup_ck); 1514 dupflag = 0; 1515 } 1516 1517 if (options & F_QUIET) 1518 return; 1519 1520 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen)); 1521 1522 switch (ntohs(ni->ni_code)) { 1523 case ICMP6_NI_SUCCESS: 1524 break; 1525 case ICMP6_NI_REFUSED: 1526 printf("refused, type 0x%x", ntohs(ni->ni_type)); 1527 goto fqdnend; 1528 case ICMP6_NI_UNKNOWN: 1529 printf("unknown, type 0x%x", ntohs(ni->ni_type)); 1530 goto fqdnend; 1531 default: 1532 printf("unknown code 0x%x, type 0x%x", 1533 ntohs(ni->ni_code), ntohs(ni->ni_type)); 1534 goto fqdnend; 1535 } 1536 1537 switch (ntohs(ni->ni_qtype)) { 1538 case NI_QTYPE_NOOP: 1539 printf("NodeInfo NOOP"); 1540 break; 1541 case NI_QTYPE_SUPTYPES: 1542 pr_suptypes(ni, end - (u_char *)ni); 1543 break; 1544 case NI_QTYPE_NODEADDR: 1545 pr_nodeaddr(ni, end - (u_char *)ni); 1546 break; 1547 case NI_QTYPE_FQDN: 1548 default: /* XXX: for backward compatibility */ 1549 cp = (u_char *)ni + ICMP6_NIRLEN; 1550 if (buf[off + ICMP6_NIRLEN] == 1551 cc - off - ICMP6_NIRLEN - 1) 1552 oldfqdn = 1; 1553 else 1554 oldfqdn = 0; 1555 if (oldfqdn) { 1556 cp++; /* skip length */ 1557 while (cp < end) { 1558 safeputc(*cp & 0xff); 1559 cp++; 1560 } 1561 } else { 1562 i = 0; 1563 while (cp < end) { 1564 if (dnsdecode((const u_char **)&cp, end, 1565 (const u_char *)(ni + 1), dnsname, 1566 sizeof(dnsname)) == NULL) { 1567 printf("???"); 1568 break; 1569 } 1570 /* 1571 * name-lookup special handling for 1572 * truncated name 1573 */ 1574 if (cp + 1 <= end && !*cp && 1575 strlen(dnsname) > 0) { 1576 dnsname[strlen(dnsname) - 1] = '\0'; 1577 cp++; 1578 } 1579 printf("%s%s", i > 0 ? "," : "", 1580 dnsname); 1581 } 1582 } 1583 if (options & F_VERBOSE) { 1584 int32_t ttl; 1585 int comma = 0; 1586 1587 (void)printf(" ("); /*)*/ 1588 1589 switch (ni->ni_code) { 1590 case ICMP6_NI_REFUSED: 1591 (void)printf("refused"); 1592 comma++; 1593 break; 1594 case ICMP6_NI_UNKNOWN: 1595 (void)printf("unknown qtype"); 1596 comma++; 1597 break; 1598 } 1599 1600 if ((end - (u_char *)ni) < ICMP6_NIRLEN) { 1601 /* case of refusion, unknown */ 1602 /*(*/ 1603 putchar(')'); 1604 goto fqdnend; 1605 } 1606 ttl = (int32_t)ntohl(*(u_long *)&buf[off+ICMP6ECHOLEN+8]); 1607 if (comma) 1608 printf(","); 1609 if (!(ni->ni_flags & NI_FQDN_FLAG_VALIDTTL)) { 1610 (void)printf("TTL=%d:meaningless", 1611 (int)ttl); 1612 } else { 1613 if (ttl < 0) { 1614 (void)printf("TTL=%d:invalid", 1615 ttl); 1616 } else 1617 (void)printf("TTL=%d", ttl); 1618 } 1619 comma++; 1620 1621 if (oldfqdn) { 1622 if (comma) 1623 printf(","); 1624 printf("03 draft"); 1625 comma++; 1626 } else { 1627 cp = (u_char *)ni + ICMP6_NIRLEN; 1628 if (cp == end) { 1629 if (comma) 1630 printf(","); 1631 printf("no name"); 1632 comma++; 1633 } 1634 } 1635 1636 if (buf[off + ICMP6_NIRLEN] != 1637 cc - off - ICMP6_NIRLEN - 1 && oldfqdn) { 1638 if (comma) 1639 printf(","); 1640 (void)printf("invalid namelen:%d/%lu", 1641 buf[off + ICMP6_NIRLEN], 1642 (u_long)cc - off - ICMP6_NIRLEN - 1); 1643 comma++; 1644 } 1645 /*(*/ 1646 putchar(')'); 1647 } 1648 fqdnend: 1649 ; 1650 } 1651 } else { 1652 /* We've got something other than an ECHOREPLY */ 1653 if (!(options & F_VERBOSE)) 1654 return; 1655 (void)printf("%d bytes from %s: ", cc, pr_addr(from, fromlen)); 1656 pr_icmph(icp, end); 1657 } 1658 1659 if (!(options & F_FLOOD)) { 1660 (void)putchar('\n'); 1661 if (options & F_VERBOSE) 1662 pr_exthdrs(mhdr); 1663 (void)fflush(stdout); 1664 } 1665 #undef safeputc 1666 } 1667 1668 void 1669 pr_exthdrs(struct msghdr *mhdr) 1670 { 1671 struct cmsghdr *cm; 1672 1673 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 1674 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 1675 if (cm->cmsg_level != IPPROTO_IPV6) 1676 continue; 1677 1678 switch (cm->cmsg_type) { 1679 case IPV6_HOPOPTS: 1680 printf(" HbH Options: "); 1681 pr_ip6opt(CMSG_DATA(cm)); 1682 break; 1683 case IPV6_DSTOPTS: 1684 #ifdef IPV6_RTHDRDSTOPTS 1685 case IPV6_RTHDRDSTOPTS: 1686 #endif 1687 printf(" Dst Options: "); 1688 pr_ip6opt(CMSG_DATA(cm)); 1689 break; 1690 case IPV6_RTHDR: 1691 printf(" Routing: "); 1692 pr_rthdr(CMSG_DATA(cm)); 1693 break; 1694 } 1695 } 1696 } 1697 1698 void 1699 pr_ip6opt(void *extbuf) 1700 { 1701 struct ip6_hbh *ext; 1702 int currentlen; 1703 u_int8_t type; 1704 size_t extlen; 1705 socklen_t len; 1706 void *databuf; 1707 size_t offset; 1708 u_int16_t value2; 1709 u_int32_t value4; 1710 1711 ext = (struct ip6_hbh *)extbuf; 1712 extlen = (ext->ip6h_len + 1) * 8; 1713 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt, 1714 (unsigned int)ext->ip6h_len, (unsigned long)extlen); 1715 1716 currentlen = 0; 1717 while (1) { 1718 currentlen = inet6_opt_next(extbuf, extlen, currentlen, 1719 &type, &len, &databuf); 1720 if (currentlen == -1) 1721 break; 1722 switch (type) { 1723 /* 1724 * Note that inet6_opt_next automatically skips any padding 1725 * optins. 1726 */ 1727 case IP6OPT_JUMBO: 1728 offset = 0; 1729 offset = inet6_opt_get_val(databuf, offset, 1730 &value4, sizeof(value4)); 1731 printf(" Jumbo Payload Opt: Length %u\n", 1732 (u_int32_t)ntohl(value4)); 1733 break; 1734 case IP6OPT_ROUTER_ALERT: 1735 offset = 0; 1736 offset = inet6_opt_get_val(databuf, offset, 1737 &value2, sizeof(value2)); 1738 printf(" Router Alert Opt: Type %u\n", 1739 ntohs(value2)); 1740 break; 1741 default: 1742 printf(" Received Opt %u len %lu\n", 1743 type, (unsigned long)len); 1744 break; 1745 } 1746 } 1747 return; 1748 } 1749 1750 void 1751 pr_rthdr(void *extbuf) 1752 { 1753 struct in6_addr *in6; 1754 char ntopbuf[INET6_ADDRSTRLEN]; 1755 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf; 1756 int i, segments; 1757 1758 /* print fixed part of the header */ 1759 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt, 1760 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type); 1761 if ((segments = inet6_rth_segments(extbuf)) >= 0) 1762 printf("%d segments, ", segments); 1763 else 1764 printf("segments unknown, "); 1765 printf("%d left\n", rh->ip6r_segleft); 1766 1767 for (i = 0; i < segments; i++) { 1768 in6 = inet6_rth_getaddr(extbuf, i); 1769 if (in6 == NULL) 1770 printf(" [%d]<NULL>\n", i); 1771 else { 1772 if (!inet_ntop(AF_INET6, in6, ntopbuf, 1773 sizeof(ntopbuf))) 1774 strlcpy(ntopbuf, "?", sizeof(ntopbuf)); 1775 printf(" [%d]%s\n", i, ntopbuf); 1776 } 1777 } 1778 1779 return; 1780 1781 } 1782 1783 int 1784 pr_bitrange(u_int32_t v, int soff, int ii) 1785 { 1786 int off; 1787 int i; 1788 1789 off = 0; 1790 while (off < 32) { 1791 /* shift till we have 0x01 */ 1792 if ((v & 0x01) == 0) { 1793 if (ii > 1) 1794 printf("-%u", soff + off - 1); 1795 ii = 0; 1796 switch (v & 0x0f) { 1797 case 0x00: 1798 v >>= 4; 1799 off += 4; 1800 continue; 1801 case 0x08: 1802 v >>= 3; 1803 off += 3; 1804 continue; 1805 case 0x04: case 0x0c: 1806 v >>= 2; 1807 off += 2; 1808 continue; 1809 default: 1810 v >>= 1; 1811 off += 1; 1812 continue; 1813 } 1814 } 1815 1816 /* we have 0x01 with us */ 1817 for (i = 0; i < 32 - off; i++) { 1818 if ((v & (0x01 << i)) == 0) 1819 break; 1820 } 1821 if (!ii) 1822 printf(" %u", soff + off); 1823 ii += i; 1824 v >>= i; off += i; 1825 } 1826 return ii; 1827 } 1828 1829 void 1830 pr_suptypes(struct icmp6_nodeinfo *ni /* ni->qtype must be SUPTYPES */, 1831 size_t nilen) 1832 { 1833 size_t clen; 1834 u_int32_t v; 1835 const u_char *cp, *end; 1836 u_int16_t cur; 1837 struct cbit { 1838 u_int16_t words; /*32bit count*/ 1839 u_int16_t skip; 1840 } cbit; 1841 #define MAXQTYPES (1 << 16) 1842 size_t off; 1843 int b; 1844 1845 cp = (u_char *)(ni + 1); 1846 end = ((u_char *)ni) + nilen; 1847 cur = 0; 1848 b = 0; 1849 1850 printf("NodeInfo Supported Qtypes"); 1851 if (options & F_VERBOSE) { 1852 if (ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) 1853 printf(", compressed bitmap"); 1854 else 1855 printf(", raw bitmap"); 1856 } 1857 1858 while (cp < end) { 1859 clen = (size_t)(end - cp); 1860 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) == 0) { 1861 if (clen == 0 || clen > MAXQTYPES / 8 || 1862 clen % sizeof(v)) { 1863 printf("???"); 1864 return; 1865 } 1866 } else { 1867 if (clen < sizeof(cbit) || clen % sizeof(v)) 1868 return; 1869 memcpy(&cbit, cp, sizeof(cbit)); 1870 if (sizeof(cbit) + ntohs(cbit.words) * sizeof(v) > 1871 clen) 1872 return; 1873 cp += sizeof(cbit); 1874 clen = ntohs(cbit.words) * sizeof(v); 1875 if (cur + clen * 8 + (u_long)ntohs(cbit.skip) * 32 > 1876 MAXQTYPES) 1877 return; 1878 } 1879 1880 for (off = 0; off < clen; off += sizeof(v)) { 1881 memcpy(&v, cp + off, sizeof(v)); 1882 v = (u_int32_t)ntohl(v); 1883 b = pr_bitrange(v, (int)(cur + off * 8), b); 1884 } 1885 /* flush the remaining bits */ 1886 b = pr_bitrange(0, (int)(cur + off * 8), b); 1887 1888 cp += clen; 1889 cur += clen * 8; 1890 if ((ni->ni_flags & NI_SUPTYPE_FLAG_COMPRESS) != 0) 1891 cur += ntohs(cbit.skip) * 32; 1892 } 1893 } 1894 1895 void 1896 pr_nodeaddr(struct icmp6_nodeinfo *ni, /* ni->qtype must be NODEADDR */ 1897 int nilen) 1898 { 1899 u_char *cp = (u_char *)(ni + 1); 1900 char ntop_buf[INET6_ADDRSTRLEN]; 1901 int withttl = 0; 1902 1903 nilen -= sizeof(struct icmp6_nodeinfo); 1904 1905 if (options & F_VERBOSE) { 1906 switch (ni->ni_code) { 1907 case ICMP6_NI_REFUSED: 1908 (void)printf("refused"); 1909 break; 1910 case ICMP6_NI_UNKNOWN: 1911 (void)printf("unknown qtype"); 1912 break; 1913 } 1914 if (ni->ni_flags & NI_NODEADDR_FLAG_TRUNCATE) 1915 (void)printf(" truncated"); 1916 } 1917 putchar('\n'); 1918 if (nilen <= 0) 1919 printf(" no address\n"); 1920 1921 /* 1922 * In icmp-name-lookups 05 and later, TTL of each returned address 1923 * is contained in the resposne. We try to detect the version 1924 * by the length of the data, but note that the detection algorithm 1925 * is incomplete. We assume the latest draft by default. 1926 */ 1927 if (nilen % (sizeof(u_int32_t) + sizeof(struct in6_addr)) == 0) 1928 withttl = 1; 1929 while (nilen > 0) { 1930 u_int32_t ttl = 0; 1931 1932 if (withttl) { 1933 /* XXX: alignment? */ 1934 ttl = (u_int32_t)ntohl(*(u_int32_t *)cp); 1935 cp += sizeof(u_int32_t); 1936 nilen -= sizeof(u_int32_t); 1937 } 1938 1939 if (inet_ntop(AF_INET6, cp, ntop_buf, sizeof(ntop_buf)) == 1940 NULL) 1941 strlcpy(ntop_buf, "?", sizeof(ntop_buf)); 1942 printf(" %s", ntop_buf); 1943 if (withttl) { 1944 if (ttl == 0xffffffff) { 1945 /* 1946 * XXX: can this convention be applied to all 1947 * type of TTL (i.e. non-ND TTL)? 1948 */ 1949 printf("(TTL=infty)"); 1950 } 1951 else 1952 printf("(TTL=%u)", ttl); 1953 } 1954 putchar('\n'); 1955 1956 nilen -= sizeof(struct in6_addr); 1957 cp += sizeof(struct in6_addr); 1958 } 1959 } 1960 1961 int 1962 get_hoplim(struct msghdr *mhdr) 1963 { 1964 struct cmsghdr *cm; 1965 1966 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 1967 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 1968 if (cm->cmsg_len == 0) 1969 return(-1); 1970 1971 if (cm->cmsg_level == IPPROTO_IPV6 && 1972 cm->cmsg_type == IPV6_HOPLIMIT && 1973 cm->cmsg_len == CMSG_LEN(sizeof(int))) 1974 return(*(int *)CMSG_DATA(cm)); 1975 } 1976 1977 return(-1); 1978 } 1979 1980 struct in6_pktinfo * 1981 get_rcvpktinfo(struct msghdr *mhdr) 1982 { 1983 struct cmsghdr *cm; 1984 1985 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 1986 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 1987 if (cm->cmsg_len == 0) 1988 return(NULL); 1989 1990 if (cm->cmsg_level == IPPROTO_IPV6 && 1991 cm->cmsg_type == IPV6_PKTINFO && 1992 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) 1993 return((struct in6_pktinfo *)CMSG_DATA(cm)); 1994 } 1995 1996 return(NULL); 1997 } 1998 1999 int 2000 get_pathmtu(struct msghdr *mhdr) 2001 { 2002 #ifdef IPV6_RECVPATHMTU 2003 struct cmsghdr *cm; 2004 struct ip6_mtuinfo *mtuctl = NULL; 2005 2006 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr); cm; 2007 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)) { 2008 if (cm->cmsg_len == 0) 2009 return(0); 2010 2011 if (cm->cmsg_level == IPPROTO_IPV6 && 2012 cm->cmsg_type == IPV6_PATHMTU && 2013 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))) { 2014 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm); 2015 2016 /* 2017 * If the notified destination is different from 2018 * the one we are pinging, just ignore the info. 2019 * We check the scope ID only when both notified value 2020 * and our own value have non-0 values, because we may 2021 * have used the default scope zone ID for sending, 2022 * in which case the scope ID value is 0. 2023 */ 2024 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr, 2025 &dst.sin6_addr) || 2026 (mtuctl->ip6m_addr.sin6_scope_id && 2027 dst.sin6_scope_id && 2028 mtuctl->ip6m_addr.sin6_scope_id != 2029 dst.sin6_scope_id)) { 2030 if ((options & F_VERBOSE) != 0) { 2031 printf("path MTU for %s is notified. " 2032 "(ignored)\n", 2033 pr_addr((struct sockaddr *)&mtuctl->ip6m_addr, 2034 sizeof(mtuctl->ip6m_addr))); 2035 } 2036 return(0); 2037 } 2038 2039 /* 2040 * Ignore an invalid MTU. XXX: can we just believe 2041 * the kernel check? 2042 */ 2043 if (mtuctl->ip6m_mtu < IPV6_MMTU) 2044 return(0); 2045 2046 /* notification for our destination. return the MTU. */ 2047 return((int)mtuctl->ip6m_mtu); 2048 } 2049 } 2050 #endif 2051 return(0); 2052 } 2053 2054 /* 2055 * tvsub -- 2056 * Subtract 2 timeval structs: out = out - in. Out is assumed to 2057 * be >= in. 2058 */ 2059 void 2060 tvsub(struct timeval *out, struct timeval *in) 2061 { 2062 if ((out->tv_usec -= in->tv_usec) < 0) { 2063 --out->tv_sec; 2064 out->tv_usec += 1000000; 2065 } 2066 out->tv_sec -= in->tv_sec; 2067 } 2068 2069 /* 2070 * onint -- 2071 * SIGINT handler. 2072 */ 2073 /* ARGSUSED */ 2074 void 2075 onint(int notused) 2076 { 2077 summary(); 2078 2079 (void)signal(SIGINT, SIG_DFL); 2080 (void)kill(getpid(), SIGINT); 2081 2082 /* NOTREACHED */ 2083 exit(1); 2084 } 2085 2086 /* 2087 * summary -- 2088 * Print out statistics. 2089 */ 2090 void 2091 summary(void) 2092 { 2093 2094 (void)printf("\n--- %s ping6 statistics ---\n", hostname); 2095 (void)printf("%ld packets transmitted, ", ntransmitted); 2096 (void)printf("%ld packets received, ", nreceived); 2097 if (nrepeats) 2098 (void)printf("+%ld duplicates, ", nrepeats); 2099 if (ntransmitted) { 2100 if (nreceived > ntransmitted) 2101 (void)printf("-- somebody's duplicating packets!"); 2102 else 2103 (void)printf("%.1f%% packet loss", 2104 ((((double)ntransmitted - nreceived) * 100.0) / 2105 ntransmitted)); 2106 } 2107 (void)putchar('\n'); 2108 if (nreceived && timing) { 2109 /* Only display average to microseconds */ 2110 double num = nreceived + nrepeats; 2111 double avg = tsum / num; 2112 double dev = sqrt((tsumsq - num * avg * avg) / (num - 1)); 2113 (void)printf( 2114 "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n", 2115 tmin, avg, tmax, dev); 2116 (void)fflush(stdout); 2117 } 2118 (void)fflush(stdout); 2119 } 2120 2121 /*subject type*/ 2122 static const char *niqcode[] = { 2123 "IPv6 address", 2124 "DNS label", /*or empty*/ 2125 "IPv4 address", 2126 }; 2127 2128 /*result code*/ 2129 static const char *nircode[] = { 2130 "Success", "Refused", "Unknown", 2131 }; 2132 2133 2134 /* 2135 * pr_icmph -- 2136 * Print a descriptive string about an ICMP header. 2137 */ 2138 void 2139 pr_icmph(struct icmp6_hdr *icp, u_char *end) 2140 { 2141 char ntop_buf[INET6_ADDRSTRLEN]; 2142 struct nd_redirect *red; 2143 struct icmp6_nodeinfo *ni; 2144 char dnsname[MAXDNAME + 1]; 2145 const u_char *cp; 2146 size_t l; 2147 2148 switch (icp->icmp6_type) { 2149 case ICMP6_DST_UNREACH: 2150 switch (icp->icmp6_code) { 2151 case ICMP6_DST_UNREACH_NOROUTE: 2152 (void)printf("No Route to Destination\n"); 2153 break; 2154 case ICMP6_DST_UNREACH_ADMIN: 2155 (void)printf("Destination Administratively " 2156 "Unreachable\n"); 2157 break; 2158 case ICMP6_DST_UNREACH_BEYONDSCOPE: 2159 (void)printf("Destination Unreachable Beyond Scope\n"); 2160 break; 2161 case ICMP6_DST_UNREACH_ADDR: 2162 (void)printf("Destination Host Unreachable\n"); 2163 break; 2164 case ICMP6_DST_UNREACH_NOPORT: 2165 (void)printf("Destination Port Unreachable\n"); 2166 break; 2167 default: 2168 (void)printf("Destination Unreachable, Bad Code: %d\n", 2169 icp->icmp6_code); 2170 break; 2171 } 2172 /* Print returned IP header information */ 2173 pr_retip((struct ip6_hdr *)(icp + 1), end); 2174 break; 2175 case ICMP6_PACKET_TOO_BIG: 2176 (void)printf("Packet too big mtu = %d\n", 2177 (int)ntohl(icp->icmp6_mtu)); 2178 pr_retip((struct ip6_hdr *)(icp + 1), end); 2179 break; 2180 case ICMP6_TIME_EXCEEDED: 2181 switch (icp->icmp6_code) { 2182 case ICMP6_TIME_EXCEED_TRANSIT: 2183 (void)printf("Time to live exceeded\n"); 2184 break; 2185 case ICMP6_TIME_EXCEED_REASSEMBLY: 2186 (void)printf("Frag reassembly time exceeded\n"); 2187 break; 2188 default: 2189 (void)printf("Time exceeded, Bad Code: %d\n", 2190 icp->icmp6_code); 2191 break; 2192 } 2193 pr_retip((struct ip6_hdr *)(icp + 1), end); 2194 break; 2195 case ICMP6_PARAM_PROB: 2196 (void)printf("Parameter problem: "); 2197 switch (icp->icmp6_code) { 2198 case ICMP6_PARAMPROB_HEADER: 2199 (void)printf("Erroneous Header "); 2200 break; 2201 case ICMP6_PARAMPROB_NEXTHEADER: 2202 (void)printf("Unknown Nextheader "); 2203 break; 2204 case ICMP6_PARAMPROB_OPTION: 2205 (void)printf("Unrecognized Option "); 2206 break; 2207 default: 2208 (void)printf("Bad code(%d) ", icp->icmp6_code); 2209 break; 2210 } 2211 (void)printf("pointer = 0x%02x\n", 2212 (u_int32_t)ntohl(icp->icmp6_pptr)); 2213 pr_retip((struct ip6_hdr *)(icp + 1), end); 2214 break; 2215 case ICMP6_ECHO_REQUEST: 2216 (void)printf("Echo Request"); 2217 /* XXX ID + Seq + Data */ 2218 break; 2219 case ICMP6_ECHO_REPLY: 2220 (void)printf("Echo Reply"); 2221 /* XXX ID + Seq + Data */ 2222 break; 2223 case ICMP6_MEMBERSHIP_QUERY: 2224 (void)printf("Listener Query"); 2225 break; 2226 case ICMP6_MEMBERSHIP_REPORT: 2227 (void)printf("Listener Report"); 2228 break; 2229 case ICMP6_MEMBERSHIP_REDUCTION: 2230 (void)printf("Listener Done"); 2231 break; 2232 case ND_ROUTER_SOLICIT: 2233 (void)printf("Router Solicitation"); 2234 break; 2235 case ND_ROUTER_ADVERT: 2236 (void)printf("Router Advertisement"); 2237 break; 2238 case ND_NEIGHBOR_SOLICIT: 2239 (void)printf("Neighbor Solicitation"); 2240 break; 2241 case ND_NEIGHBOR_ADVERT: 2242 (void)printf("Neighbor Advertisement"); 2243 break; 2244 case ND_REDIRECT: 2245 red = (struct nd_redirect *)icp; 2246 (void)printf("Redirect\n"); 2247 if (!inet_ntop(AF_INET6, &red->nd_rd_dst, ntop_buf, 2248 sizeof(ntop_buf))) 2249 strlcpy(ntop_buf, "?", sizeof(ntop_buf)); 2250 (void)printf("Destination: %s", ntop_buf); 2251 if (!inet_ntop(AF_INET6, &red->nd_rd_target, ntop_buf, 2252 sizeof(ntop_buf))) 2253 strlcpy(ntop_buf, "?", sizeof(ntop_buf)); 2254 (void)printf(" New Target: %s", ntop_buf); 2255 break; 2256 case ICMP6_NI_QUERY: 2257 (void)printf("Node Information Query"); 2258 /* XXX ID + Seq + Data */ 2259 ni = (struct icmp6_nodeinfo *)icp; 2260 l = end - (u_char *)(ni + 1); 2261 printf(", "); 2262 switch (ntohs(ni->ni_qtype)) { 2263 case NI_QTYPE_NOOP: 2264 (void)printf("NOOP"); 2265 break; 2266 case NI_QTYPE_SUPTYPES: 2267 (void)printf("Supported qtypes"); 2268 break; 2269 case NI_QTYPE_FQDN: 2270 (void)printf("DNS name"); 2271 break; 2272 case NI_QTYPE_NODEADDR: 2273 (void)printf("nodeaddr"); 2274 break; 2275 case NI_QTYPE_IPV4ADDR: 2276 (void)printf("IPv4 nodeaddr"); 2277 break; 2278 default: 2279 (void)printf("unknown qtype"); 2280 break; 2281 } 2282 if (options & F_VERBOSE) { 2283 switch (ni->ni_code) { 2284 case ICMP6_NI_SUBJ_IPV6: 2285 if (l == sizeof(struct in6_addr) && 2286 inet_ntop(AF_INET6, ni + 1, ntop_buf, 2287 sizeof(ntop_buf)) != NULL) { 2288 (void)printf(", subject=%s(%s)", 2289 niqcode[ni->ni_code], ntop_buf); 2290 } else { 2291 #if 1 2292 /* backward compat to -W */ 2293 (void)printf(", oldfqdn"); 2294 #else 2295 (void)printf(", invalid"); 2296 #endif 2297 } 2298 break; 2299 case ICMP6_NI_SUBJ_FQDN: 2300 if (end == (u_char *)(ni + 1)) { 2301 (void)printf(", no subject"); 2302 break; 2303 } 2304 printf(", subject=%s", niqcode[ni->ni_code]); 2305 cp = (const u_char *)(ni + 1); 2306 if (dnsdecode(&cp, end, NULL, dnsname, 2307 sizeof(dnsname)) != NULL) 2308 printf("(%s)", dnsname); 2309 else 2310 printf("(invalid)"); 2311 break; 2312 case ICMP6_NI_SUBJ_IPV4: 2313 if (l == sizeof(struct in_addr) && 2314 inet_ntop(AF_INET, ni + 1, ntop_buf, 2315 sizeof(ntop_buf)) != NULL) { 2316 (void)printf(", subject=%s(%s)", 2317 niqcode[ni->ni_code], ntop_buf); 2318 } else 2319 (void)printf(", invalid"); 2320 break; 2321 default: 2322 (void)printf(", invalid"); 2323 break; 2324 } 2325 } 2326 break; 2327 case ICMP6_NI_REPLY: 2328 (void)printf("Node Information Reply"); 2329 /* XXX ID + Seq + Data */ 2330 ni = (struct icmp6_nodeinfo *)icp; 2331 printf(", "); 2332 switch (ntohs(ni->ni_qtype)) { 2333 case NI_QTYPE_NOOP: 2334 (void)printf("NOOP"); 2335 break; 2336 case NI_QTYPE_SUPTYPES: 2337 (void)printf("Supported qtypes"); 2338 break; 2339 case NI_QTYPE_FQDN: 2340 (void)printf("DNS name"); 2341 break; 2342 case NI_QTYPE_NODEADDR: 2343 (void)printf("nodeaddr"); 2344 break; 2345 case NI_QTYPE_IPV4ADDR: 2346 (void)printf("IPv4 nodeaddr"); 2347 break; 2348 default: 2349 (void)printf("unknown qtype"); 2350 break; 2351 } 2352 if (options & F_VERBOSE) { 2353 if (ni->ni_code >= sizeof(nircode) / sizeof(nircode[0])) 2354 printf(", invalid"); 2355 else 2356 printf(", %s", nircode[ni->ni_code]); 2357 } 2358 break; 2359 default: 2360 (void)printf("Bad ICMP type: %d", icp->icmp6_type); 2361 } 2362 } 2363 2364 /* 2365 * pr_iph -- 2366 * Print an IP6 header. 2367 */ 2368 void 2369 pr_iph(struct ip6_hdr *ip6) 2370 { 2371 u_int32_t flow = ip6->ip6_flow & IPV6_FLOWLABEL_MASK; 2372 u_int8_t tc; 2373 char ntop_buf[INET6_ADDRSTRLEN]; 2374 2375 tc = *(&ip6->ip6_vfc + 1); /* XXX */ 2376 tc = (tc >> 4) & 0x0f; 2377 tc |= (ip6->ip6_vfc << 4); 2378 2379 printf("Vr TC Flow Plen Nxt Hlim\n"); 2380 printf(" %1x %02x %05x %04x %02x %02x\n", 2381 (ip6->ip6_vfc & IPV6_VERSION_MASK) >> 4, tc, (u_int32_t)ntohl(flow), 2382 ntohs(ip6->ip6_plen), ip6->ip6_nxt, ip6->ip6_hlim); 2383 if (!inet_ntop(AF_INET6, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf))) 2384 strlcpy(ntop_buf, "?", sizeof(ntop_buf)); 2385 printf("%s->", ntop_buf); 2386 if (!inet_ntop(AF_INET6, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf))) 2387 strlcpy(ntop_buf, "?", sizeof(ntop_buf)); 2388 printf("%s\n", ntop_buf); 2389 } 2390 2391 /* 2392 * pr_addr -- 2393 * Return an ascii host address as a dotted quad and optionally with 2394 * a hostname. 2395 */ 2396 const char * 2397 pr_addr(struct sockaddr *addr, int addrlen) 2398 { 2399 static char buf[NI_MAXHOST]; 2400 int flag = 0; 2401 2402 if ((options & F_HOSTNAME) == 0) 2403 flag |= NI_NUMERICHOST; 2404 2405 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL, 0, flag) == 0) 2406 return (buf); 2407 else 2408 return "?"; 2409 } 2410 2411 /* 2412 * pr_retip -- 2413 * Dump some info on a returned (via ICMPv6) IPv6 packet. 2414 */ 2415 void 2416 pr_retip(struct ip6_hdr *ip6, u_char *end) 2417 { 2418 u_char *cp = (u_char *)ip6, nh; 2419 int hlen; 2420 2421 if (end - (u_char *)ip6 < sizeof(*ip6)) { 2422 printf("IP6"); 2423 goto trunc; 2424 } 2425 pr_iph(ip6); 2426 hlen = sizeof(*ip6); 2427 2428 nh = ip6->ip6_nxt; 2429 cp += hlen; 2430 while (end - cp >= 8) { 2431 switch (nh) { 2432 case IPPROTO_HOPOPTS: 2433 printf("HBH "); 2434 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3; 2435 nh = ((struct ip6_hbh *)cp)->ip6h_nxt; 2436 break; 2437 case IPPROTO_DSTOPTS: 2438 printf("DSTOPT "); 2439 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3; 2440 nh = ((struct ip6_dest *)cp)->ip6d_nxt; 2441 break; 2442 case IPPROTO_FRAGMENT: 2443 printf("FRAG "); 2444 hlen = sizeof(struct ip6_frag); 2445 nh = ((struct ip6_frag *)cp)->ip6f_nxt; 2446 break; 2447 case IPPROTO_ROUTING: 2448 printf("RTHDR "); 2449 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3; 2450 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt; 2451 break; 2452 #ifdef IPSEC 2453 case IPPROTO_AH: 2454 printf("AH "); 2455 hlen = (((struct ah *)cp)->ah_len+2) << 2; 2456 nh = ((struct ah *)cp)->ah_nxt; 2457 break; 2458 #endif 2459 case IPPROTO_ICMPV6: 2460 printf("ICMP6: type = %d, code = %d\n", 2461 *cp, *(cp + 1)); 2462 return; 2463 case IPPROTO_ESP: 2464 printf("ESP\n"); 2465 return; 2466 case IPPROTO_TCP: 2467 printf("TCP: from port %u, to port %u (decimal)\n", 2468 (*cp * 256 + *(cp + 1)), 2469 (*(cp + 2) * 256 + *(cp + 3))); 2470 return; 2471 case IPPROTO_UDP: 2472 printf("UDP: from port %u, to port %u (decimal)\n", 2473 (*cp * 256 + *(cp + 1)), 2474 (*(cp + 2) * 256 + *(cp + 3))); 2475 return; 2476 default: 2477 printf("Unknown Header(%d)\n", nh); 2478 return; 2479 } 2480 2481 if ((cp += hlen) >= end) 2482 goto trunc; 2483 } 2484 if (end - cp < 8) 2485 goto trunc; 2486 2487 putchar('\n'); 2488 return; 2489 2490 trunc: 2491 printf("...\n"); 2492 return; 2493 } 2494 2495 void 2496 fill(char *bp, char *patp) 2497 { 2498 int ii, jj, kk; 2499 int pat[16]; 2500 char *cp; 2501 2502 for (cp = patp; *cp; cp++) 2503 if (!isxdigit((unsigned char)*cp)) 2504 errx(1, "patterns must be specified as hex digits"); 2505 ii = sscanf(patp, 2506 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 2507 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 2508 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 2509 &pat[13], &pat[14], &pat[15]); 2510 2511 /* xxx */ 2512 if (ii > 0) 2513 for (kk = 0; 2514 kk <= MAXDATALEN - (8 + sizeof(struct tv32) + ii); 2515 kk += ii) 2516 for (jj = 0; jj < ii; ++jj) 2517 bp[jj + kk] = pat[jj]; 2518 if (!(options & F_QUIET)) { 2519 (void)printf("PATTERN: 0x"); 2520 for (jj = 0; jj < ii; ++jj) 2521 (void)printf("%02x", bp[jj] & 0xFF); 2522 (void)printf("\n"); 2523 } 2524 } 2525 2526 #ifdef IPSEC 2527 #ifdef IPSEC_POLICY_IPSEC 2528 int 2529 setpolicy(int so, char *policy) 2530 { 2531 char *buf; 2532 2533 if (policy == NULL) 2534 return 0; /* ignore */ 2535 2536 buf = ipsec_set_policy(policy, strlen(policy)); 2537 if (buf == NULL) 2538 errx(1, "%s", ipsec_strerror()); 2539 if (setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf, 2540 ipsec_get_policylen(buf)) < 0) 2541 warnx("Unable to set IPsec policy"); 2542 free(buf); 2543 2544 return 0; 2545 } 2546 #endif 2547 #endif 2548 2549 char * 2550 nigroup(char *name) 2551 { 2552 char *p; 2553 char *q; 2554 MD5_CTX ctxt; 2555 u_int8_t digest[16]; 2556 u_int8_t c; 2557 size_t l; 2558 char hbuf[NI_MAXHOST]; 2559 struct in6_addr in6; 2560 2561 p = strchr(name, '.'); 2562 if (!p) 2563 p = name + strlen(name); 2564 l = p - name; 2565 if (l > 63 || l > sizeof(hbuf) - 1) 2566 return NULL; /*label too long*/ 2567 strncpy(hbuf, name, l); 2568 hbuf[(int)l] = '\0'; 2569 2570 for (q = name; *q; q++) { 2571 if (isupper(*(unsigned char *)q)) 2572 *q = tolower(*(unsigned char *)q); 2573 } 2574 2575 /* generate 8 bytes of pseudo-random value. */ 2576 memset(&ctxt, 0, sizeof(ctxt)); 2577 MD5Init(&ctxt); 2578 c = l & 0xff; 2579 MD5Update(&ctxt, &c, sizeof(c)); 2580 MD5Update(&ctxt, (unsigned char *)name, l); 2581 MD5Final(digest, &ctxt); 2582 2583 if (inet_pton(AF_INET6, "ff02::2:0000:0000", &in6) != 1) 2584 return NULL; /*XXX*/ 2585 bcopy(digest, &in6.s6_addr[12], 4); 2586 2587 if (inet_ntop(AF_INET6, &in6, hbuf, sizeof(hbuf)) == NULL) 2588 return NULL; 2589 2590 return strdup(hbuf); 2591 } 2592 2593 void 2594 usage(void) 2595 { 2596 (void)fprintf(stderr, 2597 "usage: ping6 [-dfH" 2598 #ifdef IPV6_USE_MIN_MTU 2599 "m" 2600 #endif 2601 "nNqtvwW" 2602 #ifdef IPV6_REACHCONF 2603 "R" 2604 #endif 2605 #ifdef IPSEC 2606 #ifdef IPSEC_POLICY_IPSEC 2607 "] [-P policy" 2608 #else 2609 "AE" 2610 #endif 2611 #endif 2612 "] [-a [aAclsg]] [-b sockbufsiz] [-c count] \n" 2613 "\t[-I interface] [-i wait] [-l preload] [-p pattern] " 2614 "[-S sourceaddr]\n" 2615 "\t[-s packetsize] [-h hoplimit] [-g gateway] [hops...] host\n"); 2616 exit(1); 2617 } 2618