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