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