1/* 2 * Copyright (c) 1999-2013 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28/* 29 * Copyright (c) 1989, 1993 30 * The Regents of the University of California. All rights reserved. 31 * 32 * This code is derived from software contributed to Berkeley by 33 * Mike Muuss. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 4. Neither the name of the University nor the names of its contributors 44 * may be used to endorse or promote products derived from this software 45 * without specific prior written permission. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 */ 59 60#include <sys/cdefs.h> 61 62#ifndef lint 63__unused static const char copyright[] = 64"@(#) Copyright (c) 1989, 1993\n\ 65 The Regents of the University of California. All rights reserved.\n"; 66#endif /* not lint */ 67 68/* 69 * P I N G . C 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#include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 87#include <sys/socket.h> 88#include <sys/sysctl.h> 89#include <sys/time.h> 90#include <sys/uio.h> 91 92#include <netinet/in.h> 93#include <netinet/in_systm.h> 94#include <netinet/ip.h> 95#include <netinet/ip_icmp.h> 96#include <netinet/ip_var.h> 97#include <arpa/inet.h> 98#include <net/if.h> 99 100#ifdef IPSEC 101#include <netinet6/ipsec.h> 102#endif /*IPSEC*/ 103 104#include <ctype.h> 105#include <err.h> 106#include <errno.h> 107#include <math.h> 108#include <netdb.h> 109#include <signal.h> 110#include <stdio.h> 111#include <stdlib.h> 112#include <string.h> 113#include <sysexits.h> 114#include <unistd.h> 115#include <ifaddrs.h> 116 117#define INADDR_LEN ((int)sizeof(in_addr_t)) 118#define TIMEVAL_LEN ((int)sizeof(struct tv32)) 119#define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 120#define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 121#define DEFDATALEN 56 /* default data length */ 122#define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 123 /* runs out of buffer space */ 124#define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 125#define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 126#define MAXWAIT 10000 /* max ms to wait for response */ 127#define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 128#define MAXTOS 255 129 130#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 131#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 132#define SET(bit) (A(bit) |= B(bit)) 133#define CLR(bit) (A(bit) &= (~B(bit))) 134#define TST(bit) (A(bit) & B(bit)) 135 136struct tv32 { 137 u_int32_t tv32_sec; 138 u_int32_t tv32_usec; 139}; 140 141/* various options */ 142int options; 143#define F_FLOOD 0x0001 144#define F_INTERVAL 0x0002 145#define F_NUMERIC 0x0004 146#define F_PINGFILLED 0x0008 147#define F_QUIET 0x0010 148#define F_RROUTE 0x0020 149#define F_SO_DEBUG 0x0040 150#define F_SO_DONTROUTE 0x0080 151#define F_VERBOSE 0x0100 152#define F_QUIET2 0x0200 153#define F_NOLOOP 0x0400 154#define F_MTTL 0x0800 155#define F_MIF 0x1000 156#define F_AUDIBLE 0x2000 157#ifdef IPSEC 158#ifdef IPSEC_POLICY_IPSEC 159#define F_POLICY 0x4000 160#endif /*IPSEC_POLICY_IPSEC*/ 161#endif /*IPSEC*/ 162#define F_TTL 0x8000 163#define F_MISSED 0x10000 164#define F_ONCE 0x20000 165#define F_HDRINCL 0x40000 166#define F_MASK 0x80000 167#define F_TIME 0x100000 168#define F_SWEEP 0x200000 169#define F_WAITTIME 0x400000 170 171/* 172 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 173 * number of received sequence numbers we can keep track of. Change 128 174 * to 8192 for complete accuracy... 175 */ 176#define MAX_DUP_CHK (8 * 128) 177int mx_dup_ck = MAX_DUP_CHK; 178char rcvd_tbl[MAX_DUP_CHK / 8]; 179 180struct sockaddr_in whereto; /* who to ping */ 181int datalen = DEFDATALEN; 182int maxpayload; 183int s; /* socket file descriptor */ 184u_char outpackhdr[IP_MAXPACKET], *outpack; 185char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 186char BSPACE = '\b'; /* characters written for flood */ 187char DOT = '.'; 188char *hostname; 189char *shostname; 190int ident; /* process id to identify our packets */ 191int uid; /* cached uid for micro-optimization */ 192u_char icmp_type = ICMP_ECHO; 193u_char icmp_type_rsp = ICMP_ECHOREPLY; 194int phdr_len = 0; 195int send_len; 196char *boundif; 197unsigned int ifscope; 198#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED 199char boundifname[IFNAMSIZ]; 200#endif /* IP_FORCE_OUT_IFP */ 201int nocell; 202int how_traffic_class = 0; 203int traffic_class = SO_TC_CTL; /* use control class, by default */ 204int no_dup = 0; 205 206/* counters */ 207long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 208long npackets; /* max packets to transmit */ 209long nreceived; /* # of packets we got back */ 210long nrepeats; /* number of duplicates */ 211long ntransmitted; /* sequence # for outbound packets = #sent */ 212long snpackets; /* max packets to transmit in one sweep */ 213long snreceived; /* # of packets we got back in this sweep */ 214long sntransmitted; /* # of packets we sent in this sweep */ 215int sweepmax; /* max value of payload in sweep */ 216int sweepmin = 0; /* start value of payload in sweep */ 217int sweepincr = 1; /* payload increment in sweep */ 218int interval = 1000; /* interval between packets, ms */ 219int waittime = MAXWAIT; /* timeout for each packet */ 220long nrcvtimeout = 0; /* # of packets we got back after waittime */ 221 222/* timing */ 223int timing; /* flag to do timing */ 224double tmin = 999999999.0; /* minimum round trip time */ 225double tmax = 0.0; /* maximum round trip time */ 226double tsum = 0.0; /* sum of all times, for doing average */ 227double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 228 229volatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ 230volatile sig_atomic_t siginfo_p; 231 232static void fill(char *, char *); 233static u_short in_cksum(u_short *, int); 234static void check_status(void); 235static void finish(void) __dead2; 236static void pinger(void); 237static char *pr_addr(struct in_addr); 238static char *pr_ntime(n_time); 239static void pr_icmph(struct icmp *); 240static void pr_iph(struct ip *); 241static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *, int); 242static void pr_retip(struct ip *); 243static void status(int); 244static void stopit(int); 245static void tvsub(struct timeval *, const struct timeval *); 246static uint32_t str2svc(const char *); 247static void usage(void) __dead2; 248 249int 250main(int argc, char *const *argv) 251{ 252 struct sockaddr_in from, sock_in; 253 struct in_addr ifaddr; 254 struct timeval last, intvl; 255 struct iovec iov; 256 struct ip *ip; 257 struct msghdr msg; 258 struct sigaction si_sa; 259 size_t sz; 260 u_char *datap, packet[IP_MAXPACKET] __attribute__((aligned(4))); 261 char *ep, *source, *target, *payload; 262 struct hostent *hp; 263#ifdef IPSEC_POLICY_IPSEC 264 char *policy_in, *policy_out; 265#endif 266 struct sockaddr_in *to; 267 double t; 268 u_long alarmtimeout, ultmp; 269 int almost_done, ch, df, hold, i, icmp_len, mib[4], preload, sockerrno, 270 tos, ttl; 271 char ctrl[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(int))]; 272 char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 273#ifdef IP_OPTIONS 274 char rspace[MAX_IPOPTLEN]; /* record route space */ 275#endif 276 unsigned char loop, mttl; 277 278 payload = source = NULL; 279#ifdef IPSEC_POLICY_IPSEC 280 policy_in = policy_out = NULL; 281#endif 282 283 /* 284 * Do the stuff that we need root priv's for *first*, and 285 * then drop our setuid bit. Save error reporting for 286 * after arg parsing. 287 */ 288 if (getuid()) 289 s = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); 290 else 291 s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 292 sockerrno = errno; 293 294 if (setuid(getuid()) != 0) 295 err(EX_NOPERM, "setuid() failed"); 296 uid = getuid(); 297 298 alarmtimeout = df = preload = tos = 0; 299 300 outpack = outpackhdr + sizeof(struct ip); 301 while ((ch = getopt(argc, argv, 302 "Aab:Cc:DdfG:g:h:I:i:k:Ll:M:m:nop:QqRrS:s:T:t:vW:z:" 303#ifdef IPSEC 304#ifdef IPSEC_POLICY_IPSEC 305 "P:" 306#endif /*IPSEC_POLICY_IPSEC*/ 307#endif /*IPSEC*/ 308#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED 309 "B:" 310#endif /* IP_FORCE_OUT_IFP */ 311 )) != -1) 312 { 313 switch(ch) { 314 case 'A': 315 options |= F_MISSED; 316 break; 317 case 'a': 318 options |= F_AUDIBLE; 319 break; 320#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED 321 case 'B': 322 (void) snprintf(boundifname, sizeof (boundifname), 323 "%s", optarg); 324 break; 325#endif /* IP_FORCE_OUT_IFP */ 326 case 'b': 327 boundif = optarg; 328 break; 329 case 'C': 330 nocell++; 331 break; 332 case 'c': 333 ultmp = strtoul(optarg, &ep, 0); 334 if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 335 errx(EX_USAGE, 336 "invalid count of packets to transmit: `%s'", 337 optarg); 338 npackets = ultmp; 339 break; 340 case 'D': 341 options |= F_HDRINCL; 342 df = 1; 343 break; 344 case 'd': 345 options |= F_SO_DEBUG; 346 break; 347 case 'f': 348 if (uid) { 349 errno = EPERM; 350 err(EX_NOPERM, "-f flag"); 351 } 352 options |= F_FLOOD; 353 setbuf(stdout, (char *)NULL); 354 break; 355 case 'G': /* Maximum packet size for ping sweep */ 356 ultmp = strtoul(optarg, &ep, 0); 357 if (*ep || ep == optarg) 358 errx(EX_USAGE, "invalid packet size: `%s'", 359 optarg); 360#ifndef __APPLE__ 361 if (uid != 0 && ultmp > DEFDATALEN) { 362 errno = EPERM; 363 err(EX_NOPERM, 364 "packet size too large: %lu > %u", 365 ultmp, DEFDATALEN); 366 } 367#endif /* __APPLE__ */ 368 options |= F_SWEEP; 369 sweepmax = ultmp; 370 break; 371 case 'g': /* Minimum packet size for ping sweep */ 372 ultmp = strtoul(optarg, &ep, 0); 373 if (*ep || ep == optarg) 374 errx(EX_USAGE, "invalid packet size: `%s'", 375 optarg); 376#ifndef __APPLE__ 377 if (uid != 0 && ultmp > DEFDATALEN) { 378 errno = EPERM; 379 err(EX_NOPERM, 380 "packet size too large: %lu > %u", 381 ultmp, DEFDATALEN); 382 } 383#endif /* __APPLE__ */ 384 options |= F_SWEEP; 385 sweepmin = ultmp; 386 break; 387 case 'h': /* Packet size increment for ping sweep */ 388 ultmp = strtoul(optarg, &ep, 0); 389 if (*ep || ep == optarg || ultmp < 1) 390 errx(EX_USAGE, "invalid increment size: `%s'", 391 optarg); 392#ifndef __APPLE__ 393 if (uid != 0 && ultmp > DEFDATALEN) { 394 errno = EPERM; 395 err(EX_NOPERM, 396 "packet size too large: %lu > %u", 397 ultmp, DEFDATALEN); 398 } 399#endif /* __APPLE__ */ 400 options |= F_SWEEP; 401 sweepincr = ultmp; 402 break; 403 case 'I': /* multicast interface */ 404 if (inet_aton(optarg, &ifaddr) == 0) 405 errx(EX_USAGE, 406 "invalid multicast interface: `%s'", 407 optarg); 408 options |= F_MIF; 409 break; 410 case 'i': /* wait between sending packets */ 411 t = strtod(optarg, &ep) * 1000.0; 412 if (*ep || ep == optarg || t > (double)INT_MAX) 413 errx(EX_USAGE, "invalid timing interval: `%s'", 414 optarg); 415 options |= F_INTERVAL; 416 interval = (int)t; 417 if (uid && interval < 100) { 418 errno = EPERM; 419 err(EX_NOPERM, "-i interval too short"); 420 } 421 break; 422 case 'k': 423 how_traffic_class++; 424 traffic_class = str2svc(optarg); 425 if (traffic_class == UINT32_MAX) 426 errx(EX_USAGE, "bad traffic class: `%s'", 427 optarg); 428 break; 429 case 'L': 430 options |= F_NOLOOP; 431 loop = 0; 432 break; 433 case 'l': 434 ultmp = strtoul(optarg, &ep, 0); 435 if (*ep || ep == optarg || ultmp > INT_MAX) 436 errx(EX_USAGE, 437 "invalid preload value: `%s'", optarg); 438 if (uid) { 439 errno = EPERM; 440 err(EX_NOPERM, "-l flag"); 441 } 442 preload = ultmp; 443 break; 444 case 'M': 445 switch(optarg[0]) { 446 case 'M': 447 case 'm': 448 options |= F_MASK; 449 break; 450 case 'T': 451 case 't': 452 options |= F_TIME; 453 break; 454 default: 455 errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 456 break; 457 } 458 break; 459 case 'm': /* TTL */ 460 ultmp = strtoul(optarg, &ep, 0); 461 if (*ep || ep == optarg || ultmp > MAXTTL) 462 errx(EX_USAGE, "invalid TTL: `%s'", optarg); 463 ttl = ultmp; 464 options |= F_TTL; 465 break; 466 case 'n': 467 options |= F_NUMERIC; 468 break; 469 case 'o': 470 options |= F_ONCE; 471 break; 472#ifdef IPSEC 473#ifdef IPSEC_POLICY_IPSEC 474 case 'P': 475 options |= F_POLICY; 476 if (!strncmp("in", optarg, 2)) 477 policy_in = strdup(optarg); 478 else if (!strncmp("out", optarg, 3)) 479 policy_out = strdup(optarg); 480 else 481 errx(1, "invalid security policy"); 482 break; 483#endif /*IPSEC_POLICY_IPSEC*/ 484#endif /*IPSEC*/ 485 case 'p': /* fill buffer with user pattern */ 486 options |= F_PINGFILLED; 487 payload = optarg; 488 break; 489 case 'Q': 490 options |= F_QUIET2; 491 break; 492 case 'q': 493 options |= F_QUIET; 494 break; 495 case 'R': 496 options |= F_RROUTE; 497 break; 498 case 'r': 499 options |= F_SO_DONTROUTE; 500 break; 501 case 'S': 502 source = optarg; 503 break; 504 case 's': /* size of packet to send */ 505 ultmp = strtoul(optarg, &ep, 0); 506 if (*ep || ep == optarg) 507 errx(EX_USAGE, "invalid packet size: `%s'", 508 optarg); 509#ifndef __APPLE__ 510 if (uid != 0 && ultmp > DEFDATALEN) { 511 errno = EPERM; 512 err(EX_NOPERM, 513 "packet size too large: %lu > %u", 514 ultmp, DEFDATALEN); 515 } 516#endif /* __APPLE__ */ 517 datalen = ultmp; 518 break; 519 case 'T': /* multicast TTL */ 520 ultmp = strtoul(optarg, &ep, 0); 521 if (*ep || ep == optarg || ultmp > MAXTTL) 522 errx(EX_USAGE, "invalid multicast TTL: `%s'", 523 optarg); 524 mttl = ultmp; 525 options |= F_MTTL; 526 break; 527 case 't': 528 alarmtimeout = strtoul(optarg, &ep, 0); 529 if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 530 errx(EX_USAGE, "invalid timeout: `%s'", 531 optarg); 532 if (alarmtimeout > MAXALARM) 533 errx(EX_USAGE, "invalid timeout: `%s' > %d", 534 optarg, MAXALARM); 535 alarm((unsigned int)alarmtimeout); 536 break; 537 case 'v': 538 options |= F_VERBOSE; 539 break; 540 case 'W': /* wait ms for answer */ 541 t = strtod(optarg, &ep); 542 if (*ep || ep == optarg || t > (double)INT_MAX) 543 errx(EX_USAGE, "invalid timing interval: `%s'", 544 optarg); 545 options |= F_WAITTIME; 546 waittime = (int)t; 547 break; 548 case 'z': 549 options |= F_HDRINCL; 550 ultmp = strtoul(optarg, &ep, 0); 551 if (*ep || ep == optarg || ultmp > MAXTOS) 552 errx(EX_USAGE, "invalid TOS: `%s'", optarg); 553 tos = ultmp; 554 break; 555 default: 556 usage(); 557 } 558 } 559 560 if (boundif != NULL && (ifscope = if_nametoindex(boundif)) == 0) 561 errx(1, "bad interface name"); 562 563 if (argc - optind != 1) 564 usage(); 565 target = argv[optind]; 566 567 switch (options & (F_MASK|F_TIME)) { 568 case 0: break; 569 case F_MASK: 570 icmp_type = ICMP_MASKREQ; 571 icmp_type_rsp = ICMP_MASKREPLY; 572 phdr_len = MASK_LEN; 573 if (!(options & F_QUIET)) 574 (void)printf("ICMP_MASKREQ\n"); 575 break; 576 case F_TIME: 577 icmp_type = ICMP_TSTAMP; 578 icmp_type_rsp = ICMP_TSTAMPREPLY; 579 phdr_len = TS_LEN; 580 if (!(options & F_QUIET)) 581 (void)printf("ICMP_TSTAMP\n"); 582 break; 583 default: 584 errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 585 break; 586 } 587 icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 588 if (options & F_RROUTE) 589 icmp_len += MAX_IPOPTLEN; 590 maxpayload = IP_MAXPACKET - icmp_len; 591 if (datalen > maxpayload) 592 errx(EX_USAGE, "packet size too large: %d > %d", datalen, 593 maxpayload); 594 send_len = icmp_len + datalen; 595 datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 596 if (options & F_PINGFILLED) { 597 fill((char *)datap, payload); 598 } 599 if (source) { 600 bzero((char *)&sock_in, sizeof(sock_in)); 601 sock_in.sin_family = AF_INET; 602 if (inet_aton(source, &sock_in.sin_addr) != 0) { 603 shostname = source; 604 } else { 605 hp = gethostbyname2(source, AF_INET); 606 if (!hp) 607 errx(EX_NOHOST, "cannot resolve %s: %s", 608 source, hstrerror(h_errno)); 609 610 sock_in.sin_len = sizeof sock_in; 611 if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 612 hp->h_length < 0) 613 errx(1, "gethostbyname2: illegal address"); 614 memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 615 sizeof(sock_in.sin_addr)); 616 (void)strncpy(snamebuf, hp->h_name, 617 sizeof(snamebuf) - 1); 618 snamebuf[sizeof(snamebuf) - 1] = '\0'; 619 shostname = snamebuf; 620 } 621 if (bind(s, (struct sockaddr *)&sock_in, sizeof sock_in) == -1) 622 err(1, "bind"); 623 } 624 625 bzero(&whereto, sizeof(whereto)); 626 to = &whereto; 627 to->sin_family = AF_INET; 628 to->sin_len = sizeof *to; 629 if (inet_aton(target, &to->sin_addr) != 0) { 630 hostname = target; 631 } else { 632 hp = gethostbyname2(target, AF_INET); 633 if (!hp) 634 errx(EX_NOHOST, "cannot resolve %s: %s", 635 target, hstrerror(h_errno)); 636 637 if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 638 errx(1, "gethostbyname2 returned an illegal address"); 639 memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 640 (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 641 hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 642 hostname = hnamebuf; 643 } 644 645 do { 646 struct ifaddrs *ifa_list, *ifa; 647 648 if (IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)) || whereto.sin_addr.s_addr == INADDR_BROADCAST) { 649 no_dup = 1; 650 break; 651 } 652 653 if (getifaddrs(&ifa_list) == -1) 654 break; 655 for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) { 656 if (ifa->ifa_addr->sa_family != AF_INET) 657 continue; 658 if ((ifa->ifa_flags & IFF_BROADCAST) == 0 || ifa->ifa_broadaddr == NULL) 659 continue; 660 if (whereto.sin_addr.s_addr != ((struct sockaddr_in*)ifa->ifa_broadaddr)->sin_addr.s_addr) 661 continue; 662 no_dup = 1; 663 break; 664 } 665 666 freeifaddrs(ifa_list); 667 } while (0); 668 669 if (options & F_FLOOD && options & F_INTERVAL) 670 errx(EX_USAGE, "-f and -i: incompatible options"); 671 672 if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 673 errx(EX_USAGE, 674 "-f flag cannot be used with multicast destination"); 675 if (options & (F_MIF | F_NOLOOP | F_MTTL) 676 && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 677 errx(EX_USAGE, 678 "-I, -L, -T flags cannot be used with unicast destination"); 679 680 if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 681 timing = 1; 682 683 if (!(options & F_PINGFILLED)) 684 for (i = TIMEVAL_LEN; i < datalen; ++i) 685 *datap++ = i; 686 687 ident = getpid() & 0xFFFF; 688 689 if (s < 0) { 690 errno = sockerrno; 691 err(EX_OSERR, "socket"); 692 } 693 hold = 1; 694 (void) setsockopt(s, SOL_SOCKET, SO_RECV_ANYIF, (char *)&hold, 695 sizeof(hold)); 696 if (ifscope != 0) { 697 if (setsockopt(s, IPPROTO_IP, IP_BOUND_IF, 698 (char *)&ifscope, sizeof (ifscope)) != 0) 699 err(EX_OSERR, "setsockopt(IP_BOUND_IF)"); 700 } 701#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED 702 else if (boundifname[0] != 0) { 703 if (setsockopt(s, IPPROTO_IP, IP_FORCE_OUT_IFP, boundifname, 704 sizeof (boundifname)) != 0) 705 err(EX_OSERR, "setsockopt(IP_FORCE_OUT_IFP)"); 706 } 707#endif /* IP_FORCE_OUT_IFP */ 708 if (nocell) { 709 if (setsockopt(s, IPPROTO_IP, IP_NO_IFT_CELLULAR, 710 (char *)&nocell, sizeof (nocell)) != 0) 711 err(EX_OSERR, "setsockopt(IP_NO_IFT_CELLULAR)"); 712 } 713 if (options & F_SO_DEBUG) 714 (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 715 sizeof(hold)); 716 if (options & F_SO_DONTROUTE) 717 (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 718 sizeof(hold)); 719 if (how_traffic_class < 2 && traffic_class >= 0) { 720 (void) setsockopt(s, SOL_SOCKET, SO_TRAFFIC_CLASS, 721 (void *)&traffic_class, sizeof (traffic_class)); 722 } 723 if (how_traffic_class > 0) { 724 int on = 1; 725 (void) setsockopt(s, SOL_SOCKET, SO_RECV_TRAFFIC_CLASS, 726 (void *)&on, sizeof (on)); 727 } 728#ifdef IPSEC 729#ifdef IPSEC_POLICY_IPSEC 730 if (options & F_POLICY) { 731 char *buf; 732 if (policy_in != NULL) { 733 buf = ipsec_set_policy(policy_in, strlen(policy_in)); 734 if (buf == NULL) 735 errx(EX_CONFIG, "%s", ipsec_strerror()); 736 if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 737 buf, ipsec_get_policylen(buf)) < 0) 738 err(EX_CONFIG, 739 "ipsec policy cannot be configured"); 740 free(buf); 741 } 742 743 if (policy_out != NULL) { 744 buf = ipsec_set_policy(policy_out, strlen(policy_out)); 745 if (buf == NULL) 746 errx(EX_CONFIG, "%s", ipsec_strerror()); 747 if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 748 buf, ipsec_get_policylen(buf)) < 0) 749 err(EX_CONFIG, 750 "ipsec policy cannot be configured"); 751 free(buf); 752 } 753 } 754#endif /*IPSEC_POLICY_IPSEC*/ 755#endif /*IPSEC*/ 756 757 if (options & F_HDRINCL) { 758 ip = (struct ip*)outpackhdr; 759 if (!(options & (F_TTL | F_MTTL))) { 760 mib[0] = CTL_NET; 761 mib[1] = PF_INET; 762 mib[2] = IPPROTO_IP; 763 mib[3] = IPCTL_DEFTTL; 764 sz = sizeof(ttl); 765 if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 766 err(1, "sysctl(net.inet.ip.ttl)"); 767 } 768 setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 769 ip->ip_v = IPVERSION; 770 ip->ip_hl = sizeof(struct ip) >> 2; 771 ip->ip_tos = tos; 772 ip->ip_id = 0; 773 ip->ip_off = df ? IP_DF : 0; 774 ip->ip_ttl = ttl; 775 ip->ip_p = IPPROTO_ICMP; 776 ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 777 ip->ip_dst = to->sin_addr; 778 } 779 /* record route option */ 780 if (options & F_RROUTE) { 781#ifdef IP_OPTIONS 782 bzero(rspace, sizeof(rspace)); 783 rspace[IPOPT_OPTVAL] = IPOPT_RR; 784 rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 785 rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 786 rspace[sizeof(rspace) - 1] = IPOPT_EOL; 787 if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 788 sizeof(rspace)) < 0) 789 err(EX_OSERR, "setsockopt IP_OPTIONS"); 790#else 791 errx(EX_UNAVAILABLE, 792 "record route not available in this implementation"); 793#endif /* IP_OPTIONS */ 794 } 795 796 if (options & F_TTL) { 797 if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, 798 sizeof(ttl)) < 0) { 799 err(EX_OSERR, "setsockopt IP_TTL"); 800 } 801 } 802 if (options & F_NOLOOP) { 803 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 804 sizeof(loop)) < 0) { 805 err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 806 } 807 } 808 if (options & F_MTTL) { 809 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 810 sizeof(mttl)) < 0) { 811 err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 812 } 813 } 814 if (options & F_MIF) { 815 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 816 sizeof(ifaddr)) < 0) { 817 err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 818 } 819 } 820#ifdef SO_TIMESTAMP 821 { int on = 1; 822 if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 823 err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 824 } 825#endif 826 if (sweepmax) { 827 if (sweepmin >= sweepmax) 828 errx(EX_USAGE, "Maximum packet size must be greater than the minimum packet size"); 829 830 if (datalen != DEFDATALEN) 831 errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive"); 832 833 if (npackets > 0) { 834 snpackets = npackets; 835 npackets = 0; 836 } else 837 snpackets = 1; 838 datalen = sweepmin; 839 send_len = icmp_len + sweepmin; 840 } 841 if (options & F_SWEEP && !sweepmax) 842 errx(EX_USAGE, "Maximum sweep size must be specified"); 843 844 /* 845 * When pinging the broadcast address, you can get a lot of answers. 846 * Doing something so evil is useful if you are trying to stress the 847 * ethernet, or just want to fill the arp cache to get some stuff for 848 * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 849 * or multicast pings if they wish. 850 */ 851 852 /* 853 * XXX receive buffer needs undetermined space for mbuf overhead 854 * as well. 855 */ 856 hold = IP_MAXPACKET + 128; 857 (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 858 sizeof(hold)); 859 if (uid == 0) 860 (void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 861 sizeof(hold)); 862 863 if (to->sin_family == AF_INET) { 864 (void)printf("PING %s (%s)", hostname, 865 inet_ntoa(to->sin_addr)); 866 if (source) 867 (void)printf(" from %s", shostname); 868 if (sweepmax) 869 (void)printf(": (%d ... %d) data bytes\n", 870 sweepmin, sweepmax); 871 else 872 (void)printf(": %d data bytes\n", datalen); 873 874 } else { 875 if (sweepmax) 876 (void)printf("PING %s: (%d ... %d) data bytes\n", 877 hostname, sweepmin, sweepmax); 878 else 879 (void)printf("PING %s: %d data bytes\n", hostname, datalen); 880 } 881 882 /* 883 * Use sigaction() instead of signal() to get unambiguous semantics, 884 * in particular with SA_RESTART not set. 885 */ 886 887 sigemptyset(&si_sa.sa_mask); 888 si_sa.sa_flags = 0; 889 890 si_sa.sa_handler = stopit; 891 if (sigaction(SIGINT, &si_sa, 0) == -1) { 892 err(EX_OSERR, "sigaction SIGINT"); 893 } 894 si_sa.sa_handler = stopit; 895 if (sigaction(SIGQUIT, &si_sa, 0) == -1) { 896 err(EX_OSERR, "sigaction SIGQUIT"); 897 } 898 899 si_sa.sa_handler = status; 900 if (sigaction(SIGINFO, &si_sa, 0) == -1) { 901 err(EX_OSERR, "sigaction SIGINFO"); 902 } 903 904 if (alarmtimeout > 0) { 905 si_sa.sa_handler = stopit; 906 if (sigaction(SIGALRM, &si_sa, 0) == -1) 907 err(EX_OSERR, "sigaction SIGALRM"); 908 } 909 910 bzero(&msg, sizeof(msg)); 911 msg.msg_name = (caddr_t)&from; 912 msg.msg_iov = &iov; 913 msg.msg_iovlen = 1; 914#ifdef SO_TIMESTAMP 915 msg.msg_control = (caddr_t)ctrl; 916#endif 917 iov.iov_base = packet; 918 iov.iov_len = IP_MAXPACKET; 919 920 if (preload == 0) 921 pinger(); /* send the first ping */ 922 else { 923 if (npackets != 0 && preload > npackets) 924 preload = npackets; 925 while (preload--) /* fire off them quickies */ 926 pinger(); 927 } 928 (void)gettimeofday(&last, NULL); 929 930 if (options & F_FLOOD) { 931 intvl.tv_sec = 0; 932 intvl.tv_usec = 10000; 933 } else { 934 intvl.tv_sec = interval / 1000; 935 intvl.tv_usec = interval % 1000 * 1000; 936 } 937 938 almost_done = 0; 939 while (!finish_up) { 940 struct timeval now, timeout; 941 fd_set rfds; 942 int cc, n; 943 int tc = -1; 944 945 check_status(); 946 if ((unsigned)s >= FD_SETSIZE) 947 errx(EX_OSERR, "descriptor too large"); 948 FD_ZERO(&rfds); 949 FD_SET(s, &rfds); 950 (void)gettimeofday(&now, NULL); 951 timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 952 timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 953 while (timeout.tv_usec < 0) { 954 timeout.tv_usec += 1000000; 955 timeout.tv_sec--; 956 } 957 while (timeout.tv_usec >= 1000000) { 958 timeout.tv_usec -= 1000000; 959 timeout.tv_sec++; 960 } 961 if (timeout.tv_sec < 0) 962 timeout.tv_sec = timeout.tv_usec = 0; 963 n = select(s + 1, &rfds, NULL, NULL, &timeout); 964 if (n < 0) 965 continue; /* Must be EINTR. */ 966 if (n == 1) { 967 struct timeval *tv = NULL; 968#ifdef SO_TIMESTAMP 969 struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 970 971 msg.msg_controllen = sizeof(ctrl); 972#endif 973 msg.msg_namelen = sizeof(from); 974 if ((cc = recvmsg(s, &msg, 0)) < 0) { 975 if (errno == EINTR) 976 continue; 977 warn("recvmsg"); 978 continue; 979 } 980 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 981#ifdef SO_TIMESTAMP 982 if (cmsg->cmsg_level == SOL_SOCKET && 983 cmsg->cmsg_type == SCM_TIMESTAMP && 984 cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 985 /* Copy to avoid alignment problems: */ 986 memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 987 tv = &now; 988 } 989#endif 990 if (cmsg->cmsg_level == SOL_SOCKET && 991 cmsg->cmsg_type == SO_TRAFFIC_CLASS && 992 cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { 993 /* Copy to avoid alignment problems: */ 994 memcpy(&tc, CMSG_DATA(cmsg), sizeof(tc)); 995 } 996 } 997 if (tv == NULL) { 998 (void)gettimeofday(&now, NULL); 999 tv = &now; 1000 } 1001 pr_pack((char *)packet, cc, &from, tv, tc); 1002 if ((options & F_ONCE && nreceived) || 1003 (npackets && nreceived >= npackets)) 1004 break; 1005 } 1006 if (n == 0 || options & F_FLOOD) { 1007 if (sweepmax && sntransmitted == snpackets) { 1008 for (i = 0; i < sweepincr ; ++i) 1009 *datap++ = i; 1010 datalen += sweepincr; 1011 if (datalen > sweepmax) 1012 break; 1013 send_len = icmp_len + datalen; 1014 sntransmitted = 0; 1015 } 1016 if (!npackets || ntransmitted < npackets) 1017 pinger(); 1018 else { 1019 if (almost_done) 1020 break; 1021 almost_done = 1; 1022 intvl.tv_usec = 0; 1023 if (nreceived) { 1024 intvl.tv_sec = 2 * tmax / 1000; 1025 if (!intvl.tv_sec) 1026 intvl.tv_sec = 1; 1027 } else { 1028 intvl.tv_sec = waittime / 1000; 1029 intvl.tv_usec = waittime % 1000 * 1000; 1030 } 1031 } 1032 (void)gettimeofday(&last, NULL); 1033 if (ntransmitted - nreceived - 1 > nmissedmax) { 1034 nmissedmax = ntransmitted - nreceived - 1; 1035 if (options & F_MISSED) 1036 (void)write(STDOUT_FILENO, &BBELL, 1); 1037 if (!(options & F_QUIET)) 1038 printf("Request timeout for icmp_seq %ld\n", ntransmitted - 2); 1039 } 1040 } 1041 } 1042 finish(); 1043 /* NOTREACHED */ 1044 exit(0); /* Make the compiler happy */ 1045} 1046 1047/* 1048 * stopit -- 1049 * Set the global bit that causes the main loop to quit. 1050 * Do NOT call finish() from here, since finish() does far too much 1051 * to be called from a signal handler. 1052 */ 1053void 1054stopit(int sig __unused) 1055{ 1056 1057 /* 1058 * When doing reverse DNS lookups, the finish_up flag might not 1059 * be noticed for a while. Just exit if we get a second SIGINT. 1060 */ 1061 if (!(options & F_NUMERIC) && finish_up) 1062 _exit(nreceived ? 0 : 2); 1063 finish_up = 1; 1064} 1065 1066/* 1067 * pinger -- 1068 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 1069 * will be added on by the kernel. The ID field is our UNIX process ID, 1070 * and the sequence number is an ascending integer. The first TIMEVAL_LEN 1071 * bytes of the data portion are used to hold a UNIX "timeval" struct in 1072 * host byte-order, to compute the round-trip time. 1073 */ 1074static void 1075pinger(void) 1076{ 1077 struct timeval now; 1078 struct tv32 tv32; 1079 struct ip *ip; 1080 struct icmp *icp; 1081 int cc, i; 1082 u_char *packet; 1083 1084 packet = outpack; 1085 icp = (struct icmp *)outpack; 1086 icp->icmp_type = icmp_type; 1087 icp->icmp_code = 0; 1088 icp->icmp_cksum = 0; 1089 icp->icmp_seq = htons(ntransmitted); 1090 icp->icmp_id = ident; /* ID */ 1091 1092 CLR(ntransmitted % mx_dup_ck); 1093 1094 if ((options & F_TIME) || timing) { 1095 (void)gettimeofday(&now, NULL); 1096 1097 tv32.tv32_sec = htonl(now.tv_sec); 1098 tv32.tv32_usec = htonl(now.tv_usec); 1099 if (options & F_TIME) 1100 icp->icmp_otime = htonl((now.tv_sec % (24*60*60)) 1101 * 1000 + now.tv_usec / 1000); 1102 if (timing) 1103 bcopy((void *)&tv32, 1104 (void *)&outpack[ICMP_MINLEN + phdr_len], 1105 sizeof(tv32)); 1106 } 1107 1108 cc = ICMP_MINLEN + phdr_len + datalen; 1109 1110 /* compute ICMP checksum here */ 1111 icp->icmp_cksum = in_cksum((u_short *)icp, cc); 1112 1113 if (options & F_HDRINCL) { 1114 cc += sizeof(struct ip); 1115 ip = (struct ip *)outpackhdr; 1116 ip->ip_len = cc; 1117 ip->ip_sum = in_cksum((u_short *)outpackhdr, cc); 1118 packet = outpackhdr; 1119 } 1120 if (how_traffic_class > 1 && traffic_class >= 0) { 1121 struct msghdr msg; 1122 struct iovec iov; 1123 char *cmbuf[CMSG_SPACE(sizeof(int))]; 1124 struct cmsghdr *cm = (struct cmsghdr *)cmbuf; 1125 1126 msg.msg_name = &whereto; 1127 msg.msg_namelen = sizeof(whereto); 1128 1129 iov.iov_base = packet; 1130 iov.iov_len = cc; 1131 msg.msg_iov = &iov; 1132 msg.msg_iovlen = 1; 1133 1134 cm->cmsg_len = CMSG_LEN(sizeof(int)); 1135 cm->cmsg_level = SOL_SOCKET; 1136 cm->cmsg_type = SO_TRAFFIC_CLASS; 1137 *(int *)CMSG_DATA(cm) = traffic_class; 1138 msg.msg_control = cm; 1139 msg.msg_controllen = CMSG_SPACE(sizeof(int)); 1140 1141 msg.msg_flags = 0; 1142 1143 i = sendmsg(s, &msg, 0); 1144 } else { 1145 i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto, 1146 sizeof(whereto)); 1147 } 1148 if (i < 0 || i != cc) { 1149 if (i < 0) { 1150 if (options & F_FLOOD && errno == ENOBUFS) { 1151 usleep(FLOOD_BACKOFF); 1152 return; 1153 } 1154 warn("sendto"); 1155 } else { 1156 warn("%s: partial write: %d of %d bytes", 1157 hostname, i, cc); 1158 } 1159 } 1160 ntransmitted++; 1161 sntransmitted++; 1162 if (!(options & F_QUIET) && options & F_FLOOD) 1163 (void)write(STDOUT_FILENO, &DOT, 1); 1164} 1165 1166/* 1167 * pr_pack -- 1168 * Print out the packet, if it came from us. This logic is necessary 1169 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 1170 * which arrive ('tis only fair). This permits multiple copies of this 1171 * program to be run without having intermingled output (or statistics!). 1172 */ 1173static void 1174pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv, 1175 int tc) 1176{ 1177 struct in_addr ina; 1178 u_char *cp, *dp; 1179 struct icmp *icp; 1180 struct ip *ip; 1181 const void *tp; 1182 double triptime; 1183 int dupflag, hlen, i, j, recv_len, seq; 1184 static int old_rrlen; 1185 static char old_rr[MAX_IPOPTLEN]; 1186 1187 /* Check the IP header */ 1188 ip = (struct ip *)buf; 1189 hlen = ip->ip_hl << 2; 1190 recv_len = cc; 1191 if (cc < hlen + ICMP_MINLEN) { 1192 if (options & F_VERBOSE) 1193 warn("packet too short (%d bytes) from %s", cc, 1194 inet_ntoa(from->sin_addr)); 1195 return; 1196 } 1197 1198 /* Now the ICMP part */ 1199 cc -= hlen; 1200 icp = (struct icmp *)(buf + hlen); 1201 if (icp->icmp_type == icmp_type_rsp) { 1202 if (icp->icmp_id != ident) 1203 return; /* 'Twas not our ECHO */ 1204 ++nreceived; 1205 triptime = 0.0; 1206 if (timing) { 1207 struct timeval tv1; 1208 struct tv32 tv32; 1209#ifndef icmp_data 1210 tp = &icp->icmp_ip; 1211#else 1212 tp = icp->icmp_data; 1213#endif 1214 tp = (const char *)tp + phdr_len; 1215 1216 if (cc - ICMP_MINLEN - phdr_len >= sizeof(tv1)) { 1217 /* Copy to avoid alignment problems: */ 1218 memcpy(&tv32, tp, sizeof(tv32)); 1219 tv1.tv_sec = ntohl(tv32.tv32_sec); 1220 tv1.tv_usec = ntohl(tv32.tv32_usec); 1221 tvsub(tv, &tv1); 1222 triptime = ((double)tv->tv_sec) * 1000.0 + 1223 ((double)tv->tv_usec) / 1000.0; 1224 tsum += triptime; 1225 tsumsq += triptime * triptime; 1226 if (triptime < tmin) 1227 tmin = triptime; 1228 if (triptime > tmax) 1229 tmax = triptime; 1230 } else 1231 timing = 0; 1232 } 1233 1234 seq = ntohs(icp->icmp_seq); 1235 1236 if (TST(seq % mx_dup_ck)) { 1237 ++nrepeats; 1238 --nreceived; 1239 dupflag = 1; 1240 } else { 1241 SET(seq % mx_dup_ck); 1242 dupflag = 0; 1243 } 1244 1245 if (options & F_QUIET) 1246 return; 1247 1248 if (options & F_WAITTIME && triptime > waittime) { 1249 ++nrcvtimeout; 1250 return; 1251 } 1252 1253 if (options & F_FLOOD) 1254 (void)write(STDOUT_FILENO, &BSPACE, 1); 1255 else { 1256 (void)printf("%d bytes from %s: icmp_seq=%u", cc, 1257 inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 1258 seq); 1259 (void)printf(" ttl=%d", ip->ip_ttl); 1260 if (timing) 1261 (void)printf(" time=%.3f ms", triptime); 1262 if (tc != -1) { 1263 (void)printf(" tc=%d", tc); 1264 } 1265 if (dupflag && no_dup == 0) { 1266 (void)printf(" (DUP!)"); 1267 } 1268 if (options & F_AUDIBLE) 1269 (void)write(STDOUT_FILENO, &BBELL, 1); 1270 if (options & F_MASK) { 1271 /* Just prentend this cast isn't ugly */ 1272 (void)printf(" mask=%s", 1273 pr_addr(*(struct in_addr *)&(icp->icmp_mask))); 1274 } 1275 if (options & F_TIME) { 1276 (void)printf(" tso=%s", pr_ntime(icp->icmp_otime)); 1277 (void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime)); 1278 (void)printf(" tst=%s", pr_ntime(icp->icmp_ttime)); 1279 } 1280 if (recv_len != send_len) { 1281 (void)printf( 1282 "\nwrong total length %d instead of %d", 1283 recv_len, send_len); 1284 } 1285 /* check the data */ 1286 cp = (u_char*)&icp->icmp_data[phdr_len]; 1287 dp = &outpack[ICMP_MINLEN + phdr_len]; 1288 cc -= ICMP_MINLEN + phdr_len; 1289 i = 0; 1290 if (timing) { /* don't check variable timestamp */ 1291 cp += TIMEVAL_LEN; 1292 dp += TIMEVAL_LEN; 1293 cc -= TIMEVAL_LEN; 1294 i += TIMEVAL_LEN; 1295 } 1296 for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 1297 if (*cp != *dp) { 1298 (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 1299 i, *dp, *cp); 1300 (void)printf("\ncp:"); 1301 cp = (u_char*)&icp->icmp_data[0]; 1302 for (i = 0; i < datalen; ++i, ++cp) { 1303 if ((i % 16) == 8) 1304 (void)printf("\n\t"); 1305 (void)printf("%2x ", *cp); 1306 } 1307 (void)printf("\ndp:"); 1308 cp = &outpack[ICMP_MINLEN]; 1309 for (i = 0; i < datalen; ++i, ++cp) { 1310 if ((i % 16) == 8) 1311 (void)printf("\n\t"); 1312 (void)printf("%2x ", *cp); 1313 } 1314 break; 1315 } 1316 } 1317 } 1318 } else { 1319 /* 1320 * We've got something other than an ECHOREPLY. 1321 * See if it's a reply to something that we sent. 1322 * We can compare IP destination, protocol, 1323 * and ICMP type and ID. 1324 * 1325 * Only print all the error messages if we are running 1326 * as root to avoid leaking information not normally 1327 * available to those not running as root. 1328 */ 1329#ifndef icmp_data 1330 struct ip *oip = &icp->icmp_ip; 1331#else 1332 struct ip *oip = (struct ip *)icp->icmp_data; 1333#endif 1334 struct icmp *oicmp = (struct icmp *)(oip + 1); 1335 1336 if (((options & F_VERBOSE) && uid == 0) || 1337 (!(options & F_QUIET2) && 1338 (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && 1339 (oip->ip_p == IPPROTO_ICMP) && 1340 (oicmp->icmp_type == ICMP_ECHO) && 1341 (oicmp->icmp_id == ident))) { 1342 (void)printf("%d bytes from %s: ", cc, 1343 pr_addr(from->sin_addr)); 1344 pr_icmph(icp); 1345 } else 1346 return; 1347 } 1348 1349 /* Display any IP options */ 1350 cp = (u_char *)buf + sizeof(struct ip); 1351 1352 for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 1353 switch (*cp) { 1354 case IPOPT_EOL: 1355 hlen = 0; 1356 break; 1357 case IPOPT_LSRR: 1358 case IPOPT_SSRR: 1359 (void)printf(*cp == IPOPT_LSRR ? 1360 "\nLSRR: " : "\nSSRR: "); 1361 j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 1362 hlen -= 2; 1363 cp += 2; 1364 if (j >= INADDR_LEN && 1365 j <= hlen - (int)sizeof(struct ip)) { 1366 for (;;) { 1367 bcopy(++cp, &ina.s_addr, INADDR_LEN); 1368 if (ina.s_addr == 0) 1369 (void)printf("\t0.0.0.0"); 1370 else 1371 (void)printf("\t%s", 1372 pr_addr(ina)); 1373 hlen -= INADDR_LEN; 1374 cp += INADDR_LEN - 1; 1375 j -= INADDR_LEN; 1376 if (j < INADDR_LEN) 1377 break; 1378 (void)putchar('\n'); 1379 } 1380 } else 1381 (void)printf("\t(truncated route)\n"); 1382 break; 1383 case IPOPT_RR: 1384 j = cp[IPOPT_OLEN]; /* get length */ 1385 i = cp[IPOPT_OFFSET]; /* and pointer */ 1386 hlen -= 2; 1387 cp += 2; 1388 if (i > j) 1389 i = j; 1390 i = i - IPOPT_MINOFF + 1; 1391 if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1392 old_rrlen = 0; 1393 continue; 1394 } 1395 if (i == old_rrlen 1396 && !bcmp((char *)cp, old_rr, i) 1397 && !(options & F_FLOOD)) { 1398 (void)printf("\t(same route)"); 1399 hlen -= i; 1400 cp += i; 1401 break; 1402 } 1403 old_rrlen = i; 1404 bcopy((char *)cp, old_rr, i); 1405 (void)printf("\nRR: "); 1406 if (i >= INADDR_LEN && 1407 i <= hlen - (int)sizeof(struct ip)) { 1408 for (;;) { 1409 bcopy(++cp, &ina.s_addr, INADDR_LEN); 1410 if (ina.s_addr == 0) 1411 (void)printf("\t0.0.0.0"); 1412 else 1413 (void)printf("\t%s", 1414 pr_addr(ina)); 1415 hlen -= INADDR_LEN; 1416 cp += INADDR_LEN - 1; 1417 i -= INADDR_LEN; 1418 if (i < INADDR_LEN) 1419 break; 1420 (void)putchar('\n'); 1421 } 1422 } else 1423 (void)printf("\t(truncated route)"); 1424 break; 1425 case IPOPT_NOP: 1426 (void)printf("\nNOP"); 1427 break; 1428 default: 1429 (void)printf("\nunknown option %x", *cp); 1430 break; 1431 } 1432 if (!(options & F_FLOOD)) { 1433 (void)putchar('\n'); 1434 (void)fflush(stdout); 1435 } 1436} 1437 1438/* 1439 * in_cksum -- 1440 * Checksum routine for Internet Protocol family headers (C Version) 1441 */ 1442u_short 1443in_cksum(u_short *addr, int len) 1444{ 1445 int nleft, sum; 1446 u_short *w; 1447 union { 1448 u_short us; 1449 u_char uc[2]; 1450 } last; 1451 u_short answer; 1452 1453 nleft = len; 1454 sum = 0; 1455 w = addr; 1456 1457 /* 1458 * Our algorithm is simple, using a 32 bit accumulator (sum), we add 1459 * sequential 16 bit words to it, and at the end, fold back all the 1460 * carry bits from the top 16 bits into the lower 16 bits. 1461 */ 1462 while (nleft > 1) { 1463 sum += *w++; 1464 nleft -= 2; 1465 } 1466 1467 /* mop up an odd byte, if necessary */ 1468 if (nleft == 1) { 1469 last.uc[0] = *(u_char *)w; 1470 last.uc[1] = 0; 1471 sum += last.us; 1472 } 1473 1474 /* add back carry outs from top 16 bits to low 16 bits */ 1475 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 1476 sum += (sum >> 16); /* add carry */ 1477 answer = ~sum; /* truncate to 16 bits */ 1478 return(answer); 1479} 1480 1481/* 1482 * tvsub -- 1483 * Subtract 2 timeval structs: out = out - in. Out is assumed to 1484 * be >= in. 1485 */ 1486static void 1487tvsub(struct timeval *out, const struct timeval *in) 1488{ 1489 1490 if ((out->tv_usec -= in->tv_usec) < 0) { 1491 --out->tv_sec; 1492 out->tv_usec += 1000000; 1493 } 1494 out->tv_sec -= in->tv_sec; 1495} 1496 1497/* 1498 * status -- 1499 * Print out statistics when SIGINFO is received. 1500 */ 1501 1502static void 1503status(int sig __unused) 1504{ 1505 1506 siginfo_p = 1; 1507} 1508 1509static void 1510check_status(void) 1511{ 1512 1513 if (siginfo_p) { 1514 siginfo_p = 0; 1515 (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 1516 nreceived, ntransmitted, 1517 ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1518 if (nreceived && timing) 1519 (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1520 tmin, tsum / (nreceived + nrepeats), tmax); 1521 (void)fprintf(stderr, "\n"); 1522 } 1523} 1524 1525/* 1526 * finish -- 1527 * Print out statistics, and give up. 1528 */ 1529static void 1530finish(void) 1531{ 1532 1533 (void)signal(SIGINT, SIG_IGN); 1534 (void)signal(SIGALRM, SIG_IGN); 1535 (void)putchar('\n'); 1536 (void)fflush(stdout); 1537 (void)printf("--- %s ping statistics ---\n", hostname); 1538 (void)printf("%ld packets transmitted, ", ntransmitted); 1539 (void)printf("%ld packets received, ", nreceived); 1540 if (nrepeats) 1541 (void)printf("+%ld duplicates, ", nrepeats); 1542 if (ntransmitted) { 1543 if (nreceived > ntransmitted) 1544 (void)printf("-- somebody's printing up packets!"); 1545 else 1546 (void)printf("%.1f%% packet loss", 1547 ((ntransmitted - nreceived) * 100.0) / 1548 ntransmitted); 1549 } 1550 if (nrcvtimeout) 1551 (void)printf(", %ld packets out of wait time", nrcvtimeout); 1552 (void)putchar('\n'); 1553 if (nreceived && timing) { 1554 double n = nreceived + nrepeats; 1555 double avg = tsum / n; 1556 double vari = tsumsq / n - avg * avg; 1557 (void)printf( 1558 "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 1559 tmin, avg, tmax, sqrt(vari)); 1560 } 1561 1562 if (nreceived) 1563 exit(0); 1564 else 1565 exit(2); 1566} 1567 1568#ifdef notdef 1569static char *ttab[] = { 1570 "Echo Reply", /* ip + seq + udata */ 1571 "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 1572 "Source Quench", /* IP */ 1573 "Redirect", /* redirect type, gateway, + IP */ 1574 "Echo", 1575 "Time Exceeded", /* transit, frag reassem + IP */ 1576 "Parameter Problem", /* pointer + IP */ 1577 "Timestamp", /* id + seq + three timestamps */ 1578 "Timestamp Reply", /* " */ 1579 "Info Request", /* id + sq */ 1580 "Info Reply" /* " */ 1581}; 1582#endif 1583 1584/* 1585 * pr_icmph -- 1586 * Print a descriptive string about an ICMP header. 1587 */ 1588static void 1589pr_icmph(struct icmp *icp) 1590{ 1591 1592 switch(icp->icmp_type) { 1593 case ICMP_ECHOREPLY: 1594 (void)printf("Echo Reply\n"); 1595 /* XXX ID + Seq + Data */ 1596 break; 1597 case ICMP_UNREACH: 1598 switch(icp->icmp_code) { 1599 case ICMP_UNREACH_NET: 1600 (void)printf("Destination Net Unreachable\n"); 1601 break; 1602 case ICMP_UNREACH_HOST: 1603 (void)printf("Destination Host Unreachable\n"); 1604 break; 1605 case ICMP_UNREACH_PROTOCOL: 1606 (void)printf("Destination Protocol Unreachable\n"); 1607 break; 1608 case ICMP_UNREACH_PORT: 1609 (void)printf("Destination Port Unreachable\n"); 1610 break; 1611 case ICMP_UNREACH_NEEDFRAG: 1612 (void)printf("frag needed and DF set (MTU %d)\n", 1613 ntohs(icp->icmp_nextmtu)); 1614 break; 1615 case ICMP_UNREACH_SRCFAIL: 1616 (void)printf("Source Route Failed\n"); 1617 break; 1618 case ICMP_UNREACH_FILTER_PROHIB: 1619 (void)printf("Communication prohibited by filter\n"); 1620 break; 1621 default: 1622 (void)printf("Dest Unreachable, Bad Code: %d\n", 1623 icp->icmp_code); 1624 break; 1625 } 1626 /* Print returned IP header information */ 1627#ifndef icmp_data 1628 pr_retip(&icp->icmp_ip); 1629#else 1630 pr_retip((struct ip *)icp->icmp_data); 1631#endif 1632 break; 1633 case ICMP_SOURCEQUENCH: 1634 (void)printf("Source Quench\n"); 1635#ifndef icmp_data 1636 pr_retip(&icp->icmp_ip); 1637#else 1638 pr_retip((struct ip *)icp->icmp_data); 1639#endif 1640 break; 1641 case ICMP_REDIRECT: 1642 switch(icp->icmp_code) { 1643 case ICMP_REDIRECT_NET: 1644 (void)printf("Redirect Network"); 1645 break; 1646 case ICMP_REDIRECT_HOST: 1647 (void)printf("Redirect Host"); 1648 break; 1649 case ICMP_REDIRECT_TOSNET: 1650 (void)printf("Redirect Type of Service and Network"); 1651 break; 1652 case ICMP_REDIRECT_TOSHOST: 1653 (void)printf("Redirect Type of Service and Host"); 1654 break; 1655 default: 1656 (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 1657 break; 1658 } 1659 (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 1660#ifndef icmp_data 1661 pr_retip(&icp->icmp_ip); 1662#else 1663 pr_retip((struct ip *)icp->icmp_data); 1664#endif 1665 break; 1666 case ICMP_ECHO: 1667 (void)printf("Echo Request\n"); 1668 /* XXX ID + Seq + Data */ 1669 break; 1670 case ICMP_TIMXCEED: 1671 switch(icp->icmp_code) { 1672 case ICMP_TIMXCEED_INTRANS: 1673 (void)printf("Time to live exceeded\n"); 1674 break; 1675 case ICMP_TIMXCEED_REASS: 1676 (void)printf("Frag reassembly time exceeded\n"); 1677 break; 1678 default: 1679 (void)printf("Time exceeded, Bad Code: %d\n", 1680 icp->icmp_code); 1681 break; 1682 } 1683#ifndef icmp_data 1684 pr_retip(&icp->icmp_ip); 1685#else 1686 pr_retip((struct ip *)icp->icmp_data); 1687#endif 1688 break; 1689 case ICMP_PARAMPROB: 1690 (void)printf("Parameter problem: pointer = 0x%02x\n", 1691 icp->icmp_hun.ih_pptr); 1692#ifndef icmp_data 1693 pr_retip(&icp->icmp_ip); 1694#else 1695 pr_retip((struct ip *)icp->icmp_data); 1696#endif 1697 break; 1698 case ICMP_TSTAMP: 1699 (void)printf("Timestamp\n"); 1700 /* XXX ID + Seq + 3 timestamps */ 1701 break; 1702 case ICMP_TSTAMPREPLY: 1703 (void)printf("Timestamp Reply\n"); 1704 /* XXX ID + Seq + 3 timestamps */ 1705 break; 1706 case ICMP_IREQ: 1707 (void)printf("Information Request\n"); 1708 /* XXX ID + Seq */ 1709 break; 1710 case ICMP_IREQREPLY: 1711 (void)printf("Information Reply\n"); 1712 /* XXX ID + Seq */ 1713 break; 1714 case ICMP_MASKREQ: 1715 (void)printf("Address Mask Request\n"); 1716 break; 1717 case ICMP_MASKREPLY: 1718 (void)printf("Address Mask Reply\n"); 1719 break; 1720 case ICMP_ROUTERADVERT: 1721 (void)printf("Router Advertisement\n"); 1722 break; 1723 case ICMP_ROUTERSOLICIT: 1724 (void)printf("Router Solicitation\n"); 1725 break; 1726 default: 1727 (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 1728 } 1729} 1730 1731/* 1732 * pr_iph -- 1733 * Print an IP header with options. 1734 */ 1735static void 1736pr_iph(struct ip *ip) 1737{ 1738 u_char *cp; 1739 int hlen; 1740 1741 hlen = ip->ip_hl << 2; 1742 cp = (u_char *)ip + 20; /* point to options */ 1743 1744 (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 1745 (void)printf(" %1x %1x %02x %04x %04x", 1746 ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1747 ntohs(ip->ip_id)); 1748 (void)printf(" %1lx %04lx", 1749 (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1750 (u_long) ntohl(ip->ip_off) & 0x1fff); 1751 (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1752 ntohs(ip->ip_sum)); 1753 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 1754 (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 1755 /* dump any option bytes */ 1756 while (hlen-- > 20) { 1757 (void)printf("%02x", *cp++); 1758 } 1759 (void)putchar('\n'); 1760} 1761 1762/* 1763 * pr_addr -- 1764 * Return an ascii host address as a dotted quad and optionally with 1765 * a hostname. 1766 */ 1767static char * 1768pr_addr(struct in_addr ina) 1769{ 1770 struct hostent *hp; 1771 static char buf[16 + 3 + MAXHOSTNAMELEN]; 1772 1773 if ((options & F_NUMERIC) || 1774 !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 1775 return inet_ntoa(ina); 1776 else 1777 (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 1778 inet_ntoa(ina)); 1779 return(buf); 1780} 1781 1782/* 1783 * pr_retip -- 1784 * Dump some info on a returned (via ICMP) IP packet. 1785 */ 1786static void 1787pr_retip(struct ip *ip) 1788{ 1789 u_char *cp; 1790 int hlen; 1791 1792 pr_iph(ip); 1793 hlen = ip->ip_hl << 2; 1794 cp = (u_char *)ip + hlen; 1795 1796 if (ip->ip_p == 6) 1797 (void)printf("TCP: from port %u, to port %u (decimal)\n", 1798 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 1799 else if (ip->ip_p == 17) 1800 (void)printf("UDP: from port %u, to port %u (decimal)\n", 1801 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 1802} 1803 1804static char * 1805pr_ntime(n_time timestamp) 1806{ 1807 static char buf[10]; 1808 int hour, min, sec; 1809 1810 sec = ntohl(timestamp) / 1000; 1811 hour = sec / 60 / 60; 1812 min = (sec % (60 * 60)) / 60; 1813 sec = (sec % (60 * 60)) % 60; 1814 1815 (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1816 1817 return (buf); 1818} 1819 1820static void 1821fill(char *bp, char *patp) 1822{ 1823 char *cp; 1824 int pat[16]; 1825 u_int ii, jj, kk; 1826 1827 for (cp = patp; *cp; cp++) { 1828 if (!isxdigit(*cp)) 1829 errx(EX_USAGE, 1830 "patterns must be specified as hex digits"); 1831 1832 } 1833 ii = sscanf(patp, 1834 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 1835 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 1836 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 1837 &pat[13], &pat[14], &pat[15]); 1838 1839 if (ii > 0) 1840 for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 1841 for (jj = 0; jj < ii; ++jj) 1842 bp[jj + kk] = pat[jj]; 1843 if (!(options & F_QUIET)) { 1844 (void)printf("PATTERN: 0x"); 1845 for (jj = 0; jj < ii; ++jj) 1846 (void)printf("%02x", bp[jj] & 0xFF); 1847 (void)printf("\n"); 1848 } 1849} 1850 1851uint32_t 1852str2svc(const char *str) 1853{ 1854 uint32_t svc; 1855 char *endptr; 1856 1857 if (str == NULL || *str == '\0') 1858 svc = UINT32_MAX; 1859 else if (strcasecmp(str, "BK_SYS") == 0) 1860 return SO_TC_BK_SYS; 1861 else if (strcasecmp(str, "BK") == 0) 1862 return SO_TC_BK; 1863 else if (strcasecmp(str, "BE") == 0) 1864 return SO_TC_BE; 1865 else if (strcasecmp(str, "RD") == 0) 1866 return SO_TC_RD; 1867 else if (strcasecmp(str, "OAM") == 0) 1868 return SO_TC_OAM; 1869 else if (strcasecmp(str, "AV") == 0) 1870 return SO_TC_AV; 1871 else if (strcasecmp(str, "RV") == 0) 1872 return SO_TC_RV; 1873 else if (strcasecmp(str, "VI") == 0) 1874 return SO_TC_VI; 1875 else if (strcasecmp(str, "VO") == 0) 1876 return SO_TC_VO; 1877 else if (strcasecmp(str, "CTL") == 0) 1878 return SO_TC_CTL; 1879 else { 1880 svc = strtoul(str, &endptr, 0); 1881 if (*endptr != '\0') 1882 svc = UINT32_MAX; 1883 } 1884 return (svc); 1885} 1886 1887#if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 1888#define SECOPT " [-P policy]" 1889#else 1890#define SECOPT "" 1891#endif 1892static void 1893usage(void) 1894{ 1895 1896 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 1897"usage: ping [-AaDdfnoQqRrv] [-b boundif] [-c count] [-G sweepmaxsize]", 1898" [-g sweepminsize] [-h sweepincrsize] [-i wait] [−k trafficclass]", 1899" [-l preload] [-M mask | time] [-m ttl]" SECOPT " [-p pattern]", 1900" [-S src_addr] [-s packetsize] [-t timeout][-W waittime] [-z tos]", 1901" host", 1902" ping [-AaDdfLnoQqRrv] [-b boundif] [-c count] [-I iface] [-i wait]", 1903" [−k trafficclass] [-l preload] [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]", 1904" [-s packetsize] [-T ttl] [-t timeout] [-W waittime]", 1905" [-z tos] mcast-group"); 1906 exit(EX_USAGE); 1907} 1908