18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1989, 1993 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 68fae3551SRodney W. Grimes * Mike Muuss. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 98fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 108fae3551SRodney W. Grimes * are met: 118fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 128fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 138fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 158fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 168fae3551SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 178fae3551SRodney W. Grimes * must display the following acknowledgement: 188fae3551SRodney W. Grimes * This product includes software developed by the University of 198fae3551SRodney W. Grimes * California, Berkeley and its contributors. 208fae3551SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 218fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 228fae3551SRodney W. Grimes * without specific prior written permission. 238fae3551SRodney W. Grimes * 248fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 258fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 268fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 278fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 288fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 298fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 308fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 318fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 328fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 338fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 348fae3551SRodney W. Grimes * SUCH DAMAGE. 358fae3551SRodney W. Grimes */ 368fae3551SRodney W. Grimes 37c69284caSDavid E. O'Brien #if 0 388fae3551SRodney W. Grimes #ifndef lint 3943470e3bSGarrett Wollman static const char copyright[] = 408fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 418fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 428fae3551SRodney W. Grimes #endif /* not lint */ 438fae3551SRodney W. Grimes 448fae3551SRodney W. Grimes #ifndef lint 458fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 468fae3551SRodney W. Grimes #endif /* not lint */ 47c69284caSDavid E. O'Brien #endif 48c69284caSDavid E. O'Brien #include <sys/cdefs.h> 49c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 508fae3551SRodney W. Grimes 518fae3551SRodney W. Grimes /* 528fae3551SRodney W. Grimes * P I N G . C 538fae3551SRodney W. Grimes * 54e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 558fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 568fae3551SRodney W. Grimes * 578fae3551SRodney W. Grimes * Author - 588fae3551SRodney W. Grimes * Mike Muuss 598fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 608fae3551SRodney W. Grimes * December, 1983 618fae3551SRodney W. Grimes * 628fae3551SRodney W. Grimes * Status - 638fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 648fae3551SRodney W. Grimes * Bugs - 658fae3551SRodney W. Grimes * More statistics could always be gathered. 668fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 678fae3551SRodney W. Grimes */ 688fae3551SRodney W. Grimes 6943470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 708fae3551SRodney W. Grimes #include <sys/socket.h> 71261e59bbSMaxim Konovalov #include <sys/sysctl.h> 728fae3551SRodney W. Grimes #include <sys/time.h> 73039d6aa4SBill Fenner #include <sys/uio.h> 748fae3551SRodney W. Grimes 758fae3551SRodney W. Grimes #include <netinet/in.h> 7643470e3bSGarrett Wollman #include <netinet/in_systm.h> 778fae3551SRodney W. Grimes #include <netinet/ip.h> 788fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 798fae3551SRodney W. Grimes #include <netinet/ip_var.h> 8043470e3bSGarrett Wollman #include <arpa/inet.h> 818fae3551SRodney W. Grimes 829a4365d0SYoshinobu Inoue #ifdef IPSEC 839a4365d0SYoshinobu Inoue #include <netinet6/ipsec.h> 849a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 859a4365d0SYoshinobu Inoue 86261e59bbSMaxim Konovalov #include <ctype.h> 87261e59bbSMaxim Konovalov #include <err.h> 88261e59bbSMaxim Konovalov #include <errno.h> 89261e59bbSMaxim Konovalov #include <math.h> 90261e59bbSMaxim Konovalov #include <netdb.h> 91261e59bbSMaxim Konovalov #include <signal.h> 92261e59bbSMaxim Konovalov #include <stdio.h> 93261e59bbSMaxim Konovalov #include <stdlib.h> 94261e59bbSMaxim Konovalov #include <string.h> 95261e59bbSMaxim Konovalov #include <sysexits.h> 96261e59bbSMaxim Konovalov #include <termios.h> 97261e59bbSMaxim Konovalov #include <unistd.h> 98261e59bbSMaxim Konovalov 99301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 100eb1543c6SMatthew N. Dodd #define TIMEVAL_LEN ((int)sizeof(struct timeval)) 101eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 102eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 103c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1048f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1058f975bb3SBruce Evans /* runs out of buffer space */ 1064fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1074fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 1088fae3551SRodney W. Grimes #define MAXWAIT 10 /* max seconds to wait for response */ 109bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1100b2f8b3fSMaxim Konovalov #define MAXTOS 255 1118fae3551SRodney W. Grimes 1128fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1138fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1148fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1158fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1168fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1178fae3551SRodney W. Grimes 1188fae3551SRodney W. Grimes /* various options */ 1198fae3551SRodney W. Grimes int options; 12085456935SBill Fenner #define F_FLOOD 0x0001 12185456935SBill Fenner #define F_INTERVAL 0x0002 12285456935SBill Fenner #define F_NUMERIC 0x0004 12385456935SBill Fenner #define F_PINGFILLED 0x0008 12485456935SBill Fenner #define F_QUIET 0x0010 12585456935SBill Fenner #define F_RROUTE 0x0020 12685456935SBill Fenner #define F_SO_DEBUG 0x0040 12785456935SBill Fenner #define F_SO_DONTROUTE 0x0080 12885456935SBill Fenner #define F_VERBOSE 0x0100 12985456935SBill Fenner #define F_QUIET2 0x0200 13085456935SBill Fenner #define F_NOLOOP 0x0400 13185456935SBill Fenner #define F_MTTL 0x0800 13285456935SBill Fenner #define F_MIF 0x1000 133772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1349a4365d0SYoshinobu Inoue #ifdef IPSEC 1359a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1369a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1379a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1389a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 139211bfbd2SRuslan Ermilov #define F_TTL 0x8000 140ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1418025c44bSDima Dorfman #define F_ONCE 0x20000 1420b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 143143008a1SMatthew N. Dodd #define F_MASK 0x80000 144eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1458fae3551SRodney W. Grimes 1468fae3551SRodney W. Grimes /* 1478fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1488fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1498fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1508fae3551SRodney W. Grimes */ 1518fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1528fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK; 1538fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8]; 1548fae3551SRodney W. Grimes 155d389e86aSMatt Jacob struct sockaddr_in whereto; /* who to ping */ 1568fae3551SRodney W. Grimes int datalen = DEFDATALEN; 1571104dd84SBruce Evans int maxpayload; 1588fae3551SRodney W. Grimes int s; /* socket file descriptor */ 1590b2f8b3fSMaxim Konovalov u_char outpackhdr[IP_MAXPACKET], *outpack; 160ca517ad8SPoul-Henning Kamp char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 161261e59bbSMaxim Konovalov char BSPACE = '\b'; /* characters written for flood */ 1628fae3551SRodney W. Grimes char DOT = '.'; 1638fae3551SRodney W. Grimes char *hostname; 16499490edeSWarner Losh char *shostname; 1658fae3551SRodney W. Grimes int ident; /* process id to identify our packets */ 166ee2bf734SWarner Losh int uid; /* cached uid for micro-optimization */ 167eb1543c6SMatthew N. Dodd u_char icmp_type = ICMP_ECHO; 168eb1543c6SMatthew N. Dodd u_char icmp_type_rsp = ICMP_ECHOREPLY; 169eb1543c6SMatthew N. Dodd int phdr_len = 0; 170e88178ddSMaxim Konovalov int send_len; 1718fae3551SRodney W. Grimes 1728fae3551SRodney W. Grimes /* counters */ 173261e59bbSMaxim Konovalov long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1748fae3551SRodney W. Grimes long npackets; /* max packets to transmit */ 1758fae3551SRodney W. Grimes long nreceived; /* # of packets we got back */ 1768fae3551SRodney W. Grimes long nrepeats; /* number of duplicates */ 1778fae3551SRodney W. Grimes long ntransmitted; /* sequence # for outbound packets = #sent */ 178526f06b2SMatthew Dillon int interval = 1000; /* interval between packets, ms */ 1798fae3551SRodney W. Grimes 1808fae3551SRodney W. Grimes /* timing */ 1818fae3551SRodney W. Grimes int timing; /* flag to do timing */ 1828fae3551SRodney W. Grimes double tmin = 999999999.0; /* minimum round trip time */ 1838fae3551SRodney W. Grimes double tmax = 0.0; /* maximum round trip time */ 1848fae3551SRodney W. Grimes double tsum = 0.0; /* sum of all times, for doing average */ 1853109a910SGarrett Wollman double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 1868fae3551SRodney W. Grimes 1878f975bb3SBruce Evans volatile sig_atomic_t finish_up; /* nonzero if we've been told to finish up */ 188badd8138SSean Eric Fagan int reset_kerninfo; 1898f975bb3SBruce Evans volatile sig_atomic_t siginfo_p; 190badd8138SSean Eric Fagan 19143470e3bSGarrett Wollman static void fill(char *, char *); 19243470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 19343470e3bSGarrett Wollman static void check_status(void); 1948f975bb3SBruce Evans static void finish(void) __dead2; 19543470e3bSGarrett Wollman static void pinger(void); 19643470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 197eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 19843470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 19943470e3bSGarrett Wollman static void pr_iph(struct ip *); 200039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); 20143470e3bSGarrett Wollman static void pr_retip(struct ip *); 20243470e3bSGarrett Wollman static void status(int); 2038f975bb3SBruce Evans static void stopit(int); 20443470e3bSGarrett Wollman static void tvsub(struct timeval *, struct timeval *); 205e345a80dSPhilippe Charnier static void usage(void) __dead2; 2068fae3551SRodney W. Grimes 20743470e3bSGarrett Wollman int 2088fae3551SRodney W. Grimes main(argc, argv) 2098fae3551SRodney W. Grimes int argc; 21043470e3bSGarrett Wollman char *const *argv; 2118fae3551SRodney W. Grimes { 21231eac03bSSean Chittenden struct sockaddr_in from, sock_in; 213efc8588dSDavid E. O'Brien struct in_addr ifaddr; 214261e59bbSMaxim Konovalov struct timeval last, intvl; 215efc8588dSDavid E. O'Brien struct iovec iov; 2160b2f8b3fSMaxim Konovalov struct ip *ip; 217efc8588dSDavid E. O'Brien struct msghdr msg; 218efc8588dSDavid E. O'Brien struct sigaction si_sa; 219efc8588dSDavid E. O'Brien struct termios ts; 2200b2f8b3fSMaxim Konovalov size_t sz; 2214fba6582SMaxim Konovalov u_char *datap, packet[IP_MAXPACKET]; 222d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 223261e59bbSMaxim Konovalov struct hostent *hp; 224efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 225efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 226efc8588dSDavid E. O'Brien #endif 227261e59bbSMaxim Konovalov struct sockaddr_in *to; 228261e59bbSMaxim Konovalov double t; 229efc8588dSDavid E. O'Brien u_long alarmtimeout, ultmp; 230e88178ddSMaxim Konovalov int almost_done, ch, df, hold, i, icmp_len, mib[4], preload, sockerrno, 231261e59bbSMaxim Konovalov tos, ttl; 232efc8588dSDavid E. O'Brien char ctrl[CMSG_SPACE(sizeof(struct timeval))]; 233efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2348fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2354fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2368fae3551SRodney W. Grimes #endif 237261e59bbSMaxim Konovalov unsigned char loop, mttl; 238efc8588dSDavid E. O'Brien 23931eac03bSSean Chittenden payload = source = NULL; 2409a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 241efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2429a4365d0SYoshinobu Inoue #endif 2438fae3551SRodney W. Grimes 244f1284d7aSBill Fenner /* 245f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 246f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 247f1284d7aSBill Fenner * after arg parsing. 248f1284d7aSBill Fenner */ 24943470e3bSGarrett Wollman s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 250f1284d7aSBill Fenner sockerrno = errno; 251f1284d7aSBill Fenner 252f1284d7aSBill Fenner setuid(getuid()); 253ee2bf734SWarner Losh uid = getuid(); 254f1284d7aSBill Fenner 2550b2f8b3fSMaxim Konovalov alarmtimeout = df = preload = tos = 0; 256badd8138SSean Eric Fagan 2570b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 258211bfbd2SRuslan Ermilov while ((ch = getopt(argc, argv, 2592c56e246SMatthew N. Dodd "Aac:DdfI:i:Ll:M:m:nop:QqRrS:s:T:t:vz:" 260211bfbd2SRuslan Ermilov #ifdef IPSEC 2619a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 262211bfbd2SRuslan Ermilov "P:" 2639a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 264211bfbd2SRuslan Ermilov #endif /*IPSEC*/ 265211bfbd2SRuslan Ermilov )) != -1) 2669a4365d0SYoshinobu Inoue { 2678fae3551SRodney W. Grimes switch(ch) { 268ca517ad8SPoul-Henning Kamp case 'A': 269ca517ad8SPoul-Henning Kamp options |= F_MISSED; 270ca517ad8SPoul-Henning Kamp break; 271772dfa72SDaniel O'Callaghan case 'a': 272772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 273772dfa72SDaniel O'Callaghan break; 2748fae3551SRodney W. Grimes case 'c': 27543470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 27643470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 27743470e3bSGarrett Wollman errx(EX_USAGE, 27843470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 27943470e3bSGarrett Wollman optarg); 28043470e3bSGarrett Wollman npackets = ultmp; 2818fae3551SRodney W. Grimes break; 2820b2f8b3fSMaxim Konovalov case 'D': 2830b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 2840b2f8b3fSMaxim Konovalov df = 1; 2850b2f8b3fSMaxim Konovalov break; 2868fae3551SRodney W. Grimes case 'd': 2878fae3551SRodney W. Grimes options |= F_SO_DEBUG; 2888fae3551SRodney W. Grimes break; 2898fae3551SRodney W. Grimes case 'f': 290526f06b2SMatthew Dillon if (uid) { 29143470e3bSGarrett Wollman errno = EPERM; 29243470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 2938fae3551SRodney W. Grimes } 2948fae3551SRodney W. Grimes options |= F_FLOOD; 2958fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 2968fae3551SRodney W. Grimes break; 2971f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 2981f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 2991f6a4631SRuslan Ermilov errx(EX_USAGE, 3001f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 3011f6a4631SRuslan Ermilov optarg); 3021f6a4631SRuslan Ermilov options |= F_MIF; 3031f6a4631SRuslan Ermilov break; 3048fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 3051ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 3061ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 3071ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 3081ad0b1beSMaxim Konovalov optarg); 3098fae3551SRodney W. Grimes options |= F_INTERVAL; 310526f06b2SMatthew Dillon interval = (int)t; 311526f06b2SMatthew Dillon if (uid && interval < 1000) { 312526f06b2SMatthew Dillon errno = EPERM; 313526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 314526f06b2SMatthew Dillon } 3158fae3551SRodney W. Grimes break; 3161f6a4631SRuslan Ermilov case 'L': 3171f6a4631SRuslan Ermilov options |= F_NOLOOP; 3181f6a4631SRuslan Ermilov loop = 0; 31985456935SBill Fenner break; 3208fae3551SRodney W. Grimes case 'l': 32143470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 32243470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 32343470e3bSGarrett Wollman errx(EX_USAGE, 32443470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 3255e2cc0f4SStephen McKay if (uid) { 326f78ac61bSWarner Losh errno = EPERM; 327f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 328f78ac61bSWarner Losh } 32943470e3bSGarrett Wollman preload = ultmp; 3308fae3551SRodney W. Grimes break; 3311f6a4631SRuslan Ermilov case 'M': 332eb1543c6SMatthew N. Dodd switch(optarg[0]) { 333eb1543c6SMatthew N. Dodd case 'M': 334eb1543c6SMatthew N. Dodd case 'm': 3351f6a4631SRuslan Ermilov options |= F_MASK; 33685456935SBill Fenner break; 337eb1543c6SMatthew N. Dodd case 'T': 338eb1543c6SMatthew N. Dodd case 't': 339eb1543c6SMatthew N. Dodd options |= F_TIME; 340eb1543c6SMatthew N. Dodd break; 341eb1543c6SMatthew N. Dodd default: 342eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 343eb1543c6SMatthew N. Dodd break; 344eb1543c6SMatthew N. Dodd } 345eb1543c6SMatthew N. Dodd break; 346211bfbd2SRuslan Ermilov case 'm': /* TTL */ 347211bfbd2SRuslan Ermilov ultmp = strtoul(optarg, &ep, 0); 3489bc1a9ecSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTTL) 3491ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 350211bfbd2SRuslan Ermilov ttl = ultmp; 351211bfbd2SRuslan Ermilov options |= F_TTL; 352211bfbd2SRuslan Ermilov break; 3538fae3551SRodney W. Grimes case 'n': 3548fae3551SRodney W. Grimes options |= F_NUMERIC; 3558fae3551SRodney W. Grimes break; 3568025c44bSDima Dorfman case 'o': 3578025c44bSDima Dorfman options |= F_ONCE; 3588025c44bSDima Dorfman break; 3591f6a4631SRuslan Ermilov #ifdef IPSEC 3601f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 3611f6a4631SRuslan Ermilov case 'P': 3621f6a4631SRuslan Ermilov options |= F_POLICY; 3631f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 3641f6a4631SRuslan Ermilov policy_in = strdup(optarg); 3651f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 3661f6a4631SRuslan Ermilov policy_out = strdup(optarg); 3671f6a4631SRuslan Ermilov else 3681f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 3691f6a4631SRuslan Ermilov break; 3701f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 3711f6a4631SRuslan Ermilov #endif /*IPSEC*/ 3728fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 3738fae3551SRodney W. Grimes options |= F_PINGFILLED; 374d074d39fSMatthew N. Dodd payload = optarg; 3758fae3551SRodney W. Grimes break; 376ef9e6dc7SBill Fenner case 'Q': 377ef9e6dc7SBill Fenner options |= F_QUIET2; 378ef9e6dc7SBill Fenner break; 3798fae3551SRodney W. Grimes case 'q': 3808fae3551SRodney W. Grimes options |= F_QUIET; 3818fae3551SRodney W. Grimes break; 3828fae3551SRodney W. Grimes case 'R': 3838fae3551SRodney W. Grimes options |= F_RROUTE; 3848fae3551SRodney W. Grimes break; 3858fae3551SRodney W. Grimes case 'r': 3868fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 3878fae3551SRodney W. Grimes break; 3881f6a4631SRuslan Ermilov case 'S': 3891f6a4631SRuslan Ermilov source = optarg; 3901f6a4631SRuslan Ermilov break; 3918fae3551SRodney W. Grimes case 's': /* size of packet to send */ 39243470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 3934fba6582SMaxim Konovalov if (*ep || ep == optarg) 39443470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 39543470e3bSGarrett Wollman optarg); 396fb7d32c7SMaxim Konovalov if (uid != 0 && ultmp > DEFDATALEN) { 397fb7d32c7SMaxim Konovalov errno = EPERM; 398fb7d32c7SMaxim Konovalov err(EX_NOPERM, 399fb7d32c7SMaxim Konovalov "packet size too large: %lu > %u", 400fb7d32c7SMaxim Konovalov ultmp, DEFDATALEN); 401fb7d32c7SMaxim Konovalov } 40243470e3bSGarrett Wollman datalen = ultmp; 4038fae3551SRodney W. Grimes break; 4041f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 4051f6a4631SRuslan Ermilov ultmp = strtoul(optarg, &ep, 0); 4061f6a4631SRuslan Ermilov if (*ep || ep == optarg || ultmp > MAXTTL) 4071f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 4081f6a4631SRuslan Ermilov optarg); 4091f6a4631SRuslan Ermilov mttl = ultmp; 4101f6a4631SRuslan Ermilov options |= F_MTTL; 41199490edeSWarner Losh break; 4127237fd94SBill Fumerola case 't': 413bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 414bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 4157237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 4167237fd94SBill Fumerola optarg); 417bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 418bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 419bf113f1bSBill Fumerola optarg, MAXALARM); 420bf113f1bSBill Fumerola alarm((int)alarmtimeout); 4217237fd94SBill Fumerola break; 4228fae3551SRodney W. Grimes case 'v': 4238fae3551SRodney W. Grimes options |= F_VERBOSE; 4248fae3551SRodney W. Grimes break; 4250b2f8b3fSMaxim Konovalov case 'z': 4260b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 4270b2f8b3fSMaxim Konovalov ultmp = strtoul(optarg, &ep, 0); 4280b2f8b3fSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTOS) 4290b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 4300b2f8b3fSMaxim Konovalov tos = ultmp; 4310b2f8b3fSMaxim Konovalov break; 4328fae3551SRodney W. Grimes default: 433e345a80dSPhilippe Charnier usage(); 43443470e3bSGarrett Wollman } 43543470e3bSGarrett Wollman } 43643470e3bSGarrett Wollman 43743470e3bSGarrett Wollman if (argc - optind != 1) 438e345a80dSPhilippe Charnier usage(); 43943470e3bSGarrett Wollman target = argv[optind]; 4408fae3551SRodney W. Grimes 441d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 442d829c3dfSMatthew N. Dodd case 0: break; 443d829c3dfSMatthew N. Dodd case F_MASK: 444eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 445eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 446d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 447eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 448eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 449d829c3dfSMatthew N. Dodd break; 450d829c3dfSMatthew N. Dodd case F_TIME: 451eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 452eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 453d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 454eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 455eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 456d829c3dfSMatthew N. Dodd break; 457d829c3dfSMatthew N. Dodd default: 458d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 459d829c3dfSMatthew N. Dodd break; 460eb1543c6SMatthew N. Dodd } 4610fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 462fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 463e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 464e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 465fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 4661104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 467fb7d32c7SMaxim Konovalov maxpayload); 468e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 4690fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 470d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 471d074d39fSMatthew N. Dodd fill((char *)datap, payload); 472d074d39fSMatthew N. Dodd } 47399490edeSWarner Losh if (source) { 47431eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 47531eac03bSSean Chittenden sock_in.sin_family = AF_INET; 47631eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 47799490edeSWarner Losh shostname = source; 47899490edeSWarner Losh } else { 47999490edeSWarner Losh hp = gethostbyname2(source, AF_INET); 48099490edeSWarner Losh if (!hp) 48199490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 48299490edeSWarner Losh source, hstrerror(h_errno)); 48399490edeSWarner Losh 48431eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 48531eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 4864fba6582SMaxim Konovalov hp->h_length < 0) 48799490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 48831eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 48931eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 49099490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 49199490edeSWarner Losh sizeof(snamebuf) - 1); 49299490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 49399490edeSWarner Losh shostname = snamebuf; 49499490edeSWarner Losh } 49531eac03bSSean Chittenden if (bind(s, (struct sockaddr *)&sock_in, sizeof sock_in) == -1) 49699490edeSWarner Losh err(1, "bind"); 49799490edeSWarner Losh } 49899490edeSWarner Losh 499d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 500d389e86aSMatt Jacob to = &whereto; 5018fae3551SRodney W. Grimes to->sin_family = AF_INET; 502d389e86aSMatt Jacob to->sin_len = sizeof *to; 50343470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 5048fae3551SRodney W. Grimes hostname = target; 50543470e3bSGarrett Wollman } else { 50643470e3bSGarrett Wollman hp = gethostbyname2(target, AF_INET); 50743470e3bSGarrett Wollman if (!hp) 50843470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 50943470e3bSGarrett Wollman target, hstrerror(h_errno)); 51043470e3bSGarrett Wollman 51131eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 5121ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 51343470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 5148fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 515b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 5168fae3551SRodney W. Grimes hostname = hnamebuf; 5178fae3551SRodney W. Grimes } 5188fae3551SRodney W. Grimes 51943470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 52043470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 52143470e3bSGarrett Wollman 52243470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 52343470e3bSGarrett Wollman errx(EX_USAGE, 52443470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 52543470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 52643470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 52743470e3bSGarrett Wollman errx(EX_USAGE, 52843470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 5298fae3551SRodney W. Grimes 530d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 5318fae3551SRodney W. Grimes timing = 1; 53243470e3bSGarrett Wollman 5338fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 534d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 5358fae3551SRodney W. Grimes *datap++ = i; 5368fae3551SRodney W. Grimes 5378fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 5388fae3551SRodney W. Grimes 539f1284d7aSBill Fenner if (s < 0) { 540f1284d7aSBill Fenner errno = sockerrno; 54143470e3bSGarrett Wollman err(EX_OSERR, "socket"); 5428fae3551SRodney W. Grimes } 5438fae3551SRodney W. Grimes hold = 1; 5448fae3551SRodney W. Grimes if (options & F_SO_DEBUG) 5458fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, 5468fae3551SRodney W. Grimes sizeof(hold)); 5478fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 5488fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 5498fae3551SRodney W. Grimes sizeof(hold)); 5509a4365d0SYoshinobu Inoue #ifdef IPSEC 5519a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 5529a4365d0SYoshinobu Inoue if (options & F_POLICY) { 5539a4365d0SYoshinobu Inoue char *buf; 5549a4365d0SYoshinobu Inoue if (policy_in != NULL) { 5559a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 5569a4365d0SYoshinobu Inoue if (buf == NULL) 557ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 5589a4365d0SYoshinobu Inoue if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 5599a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 5601ad0b1beSMaxim Konovalov err(EX_CONFIG, 5611ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 5629a4365d0SYoshinobu Inoue free(buf); 5639a4365d0SYoshinobu Inoue } 5649a4365d0SYoshinobu Inoue 5659a4365d0SYoshinobu Inoue if (policy_out != NULL) { 5669a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 5679a4365d0SYoshinobu Inoue if (buf == NULL) 568ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 5699a4365d0SYoshinobu Inoue if (setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, 5709a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 5711ad0b1beSMaxim Konovalov err(EX_CONFIG, 5721ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 5739a4365d0SYoshinobu Inoue free(buf); 5749a4365d0SYoshinobu Inoue } 5759a4365d0SYoshinobu Inoue } 5769a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 5779a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 5788fae3551SRodney W. Grimes 5790b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 5800b2f8b3fSMaxim Konovalov ip = (struct ip*)outpackhdr; 5810b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 5820b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 5830b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 5840b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 5850b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 5860b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 5870b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 5880b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 5890b2f8b3fSMaxim Konovalov } 5900b2f8b3fSMaxim Konovalov setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 5910b2f8b3fSMaxim Konovalov ip->ip_v = IPVERSION; 5920b2f8b3fSMaxim Konovalov ip->ip_hl = sizeof(struct ip) >> 2; 5930b2f8b3fSMaxim Konovalov ip->ip_tos = tos; 5940b2f8b3fSMaxim Konovalov ip->ip_id = 0; 5950b2f8b3fSMaxim Konovalov ip->ip_off = df ? IP_DF : 0; 5960b2f8b3fSMaxim Konovalov ip->ip_ttl = ttl; 5970b2f8b3fSMaxim Konovalov ip->ip_p = IPPROTO_ICMP; 59831eac03bSSean Chittenden ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 5990b2f8b3fSMaxim Konovalov ip->ip_dst = to->sin_addr; 6000b2f8b3fSMaxim Konovalov } 6018fae3551SRodney W. Grimes /* record route option */ 6028fae3551SRodney W. Grimes if (options & F_RROUTE) { 6038fae3551SRodney W. Grimes #ifdef IP_OPTIONS 604039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 6058fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 6068fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 6078fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 608039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 6098fae3551SRodney W. Grimes if (setsockopt(s, IPPROTO_IP, IP_OPTIONS, rspace, 61043470e3bSGarrett Wollman sizeof(rspace)) < 0) 61143470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 6128fae3551SRodney W. Grimes #else 61343470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 61443470e3bSGarrett Wollman "record route not available in this implementation"); 6158fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 6168fae3551SRodney W. Grimes } 6178fae3551SRodney W. Grimes 618211bfbd2SRuslan Ermilov if (options & F_TTL) { 619211bfbd2SRuslan Ermilov if (setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, 620211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 621211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 622211bfbd2SRuslan Ermilov } 623211bfbd2SRuslan Ermilov } 62485456935SBill Fenner if (options & F_NOLOOP) { 62585456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 62685456935SBill Fenner sizeof(loop)) < 0) { 62743470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 62885456935SBill Fenner } 62985456935SBill Fenner } 63085456935SBill Fenner if (options & F_MTTL) { 631211bfbd2SRuslan Ermilov if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 632211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 63343470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 63485456935SBill Fenner } 63585456935SBill Fenner } 63685456935SBill Fenner if (options & F_MIF) { 63785456935SBill Fenner if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 63885456935SBill Fenner sizeof(ifaddr)) < 0) { 63943470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 64085456935SBill Fenner } 64185456935SBill Fenner } 642039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 643039d6aa4SBill Fenner { int on = 1; 644039d6aa4SBill Fenner if (setsockopt(s, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 645039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 646039d6aa4SBill Fenner } 647039d6aa4SBill Fenner #endif 64885456935SBill Fenner 6498fae3551SRodney W. Grimes /* 6508fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 6518fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 6528fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 65343470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 65443470e3bSGarrett Wollman * or multicast pings if they wish. 6558fae3551SRodney W. Grimes */ 6564fba6582SMaxim Konovalov 6574fba6582SMaxim Konovalov /* 6584fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 6594fba6582SMaxim Konovalov * as well. 6604fba6582SMaxim Konovalov */ 6614fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 6628fae3551SRodney W. Grimes (void)setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 6638fae3551SRodney W. Grimes sizeof(hold)); 664261e59bbSMaxim Konovalov if (uid == 0) 665e8bd25ceSRobert Watson (void)setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 666e8bd25ceSRobert Watson sizeof(hold)); 667e8bd25ceSRobert Watson 66899490edeSWarner Losh if (to->sin_family == AF_INET) { 66999490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 67099490edeSWarner Losh inet_ntoa(to->sin_addr)); 67199490edeSWarner Losh if (source) 67299490edeSWarner Losh (void)printf(" from %s", shostname); 67399490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 67499490edeSWarner Losh } else 6758fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 6768fae3551SRodney W. Grimes 6777d81b35cSBruce Evans /* 678a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 679a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 6807d81b35cSBruce Evans */ 681a2a00888SSean Eric Fagan 682f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 68337e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 684a2a00888SSean Eric Fagan 685a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 686a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 687a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 688a2a00888SSean Eric Fagan } 689a2a00888SSean Eric Fagan 690a2a00888SSean Eric Fagan si_sa.sa_handler = status; 69137e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 692624ff938SWarner Losh err(EX_OSERR, "sigaction"); 69337e5b2c6SSean Eric Fagan } 694bf113f1bSBill Fumerola 695bf113f1bSBill Fumerola if (alarmtimeout > 0) { 6967237fd94SBill Fumerola si_sa.sa_handler = stopit; 6977237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 6987237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 699bf113f1bSBill Fumerola } 70037e5b2c6SSean Eric Fagan 701039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 702039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 703039d6aa4SBill Fenner msg.msg_iov = &iov; 704039d6aa4SBill Fenner msg.msg_iovlen = 1; 705039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 706039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 707039d6aa4SBill Fenner #endif 708039d6aa4SBill Fenner iov.iov_base = packet; 709e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 710039d6aa4SBill Fenner 7114055611aSSean Eric Fagan if (tcgetattr(STDOUT_FILENO, &ts) != -1) { 7124055611aSSean Eric Fagan reset_kerninfo = !(ts.c_lflag & NOKERNINFO); 7134055611aSSean Eric Fagan ts.c_lflag |= NOKERNINFO; 7144055611aSSean Eric Fagan tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 7154055611aSSean Eric Fagan } 7164055611aSSean Eric Fagan 71732af342fSRuslan Ermilov if (preload == 0) 71832af342fSRuslan Ermilov pinger(); /* send the first ping */ 71932af342fSRuslan Ermilov else { 72032af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 72132af342fSRuslan Ermilov preload = npackets; 7228fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 7238fae3551SRodney W. Grimes pinger(); 72432af342fSRuslan Ermilov } 72532af342fSRuslan Ermilov (void)gettimeofday(&last, NULL); 7268fae3551SRodney W. Grimes 727039d6aa4SBill Fenner if (options & F_FLOOD) { 728039d6aa4SBill Fenner intvl.tv_sec = 0; 729039d6aa4SBill Fenner intvl.tv_usec = 10000; 730039d6aa4SBill Fenner } else { 731526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 732526f06b2SMatthew Dillon intvl.tv_usec = interval % 1000 * 1000; 733039d6aa4SBill Fenner } 734039d6aa4SBill Fenner 735261e59bbSMaxim Konovalov almost_done = 0; 7368f975bb3SBruce Evans while (!finish_up) { 737261e59bbSMaxim Konovalov struct timeval now, timeout; 738039d6aa4SBill Fenner fd_set rfds; 739261e59bbSMaxim Konovalov int cc, n; 7408fae3551SRodney W. Grimes 7417d81b35cSBruce Evans check_status(); 74231eac03bSSean Chittenden if ((unsigned)s >= FD_SETSIZE) 7437e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 744039d6aa4SBill Fenner FD_ZERO(&rfds); 745039d6aa4SBill Fenner FD_SET(s, &rfds); 746039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 747039d6aa4SBill Fenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 748039d6aa4SBill Fenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 749039d6aa4SBill Fenner while (timeout.tv_usec < 0) { 750039d6aa4SBill Fenner timeout.tv_usec += 1000000; 751039d6aa4SBill Fenner timeout.tv_sec--; 7528fae3551SRodney W. Grimes } 753e345a80dSPhilippe Charnier while (timeout.tv_usec >= 1000000) { 754039d6aa4SBill Fenner timeout.tv_usec -= 1000000; 755039d6aa4SBill Fenner timeout.tv_sec++; 756039d6aa4SBill Fenner } 757039d6aa4SBill Fenner if (timeout.tv_sec < 0) 758039d6aa4SBill Fenner timeout.tv_sec = timeout.tv_usec = 0; 759039d6aa4SBill Fenner n = select(s + 1, &rfds, NULL, NULL, &timeout); 7605e2cc0f4SStephen McKay if (n < 0) 7615e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 762039d6aa4SBill Fenner if (n == 1) { 76331eac03bSSean Chittenden struct timeval *tv = NULL; 764039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 765039d6aa4SBill Fenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 766039d6aa4SBill Fenner 767039d6aa4SBill Fenner msg.msg_controllen = sizeof(ctrl); 768039d6aa4SBill Fenner #endif 769039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 770039d6aa4SBill Fenner if ((cc = recvmsg(s, &msg, 0)) < 0) { 7718fae3551SRodney W. Grimes if (errno == EINTR) 7728fae3551SRodney W. Grimes continue; 773e345a80dSPhilippe Charnier warn("recvmsg"); 7748fae3551SRodney W. Grimes continue; 7758fae3551SRodney W. Grimes } 776039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 777039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 778039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 77931eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 780fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 781fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 78231eac03bSSean Chittenden tv = &now; 783fa05a94cSJohn Birrell } 784039d6aa4SBill Fenner #endif 78531eac03bSSean Chittenden if (tv == NULL) { 786039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 78731eac03bSSean Chittenden tv = &now; 788039d6aa4SBill Fenner } 78931eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 79031eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 79131eac03bSSean Chittenden (npackets && nreceived >= npackets)) 7928fae3551SRodney W. Grimes break; 7938fae3551SRodney W. Grimes } 7945e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 795039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 796039d6aa4SBill Fenner pinger(); 797039d6aa4SBill Fenner else { 798039d6aa4SBill Fenner if (almost_done) 799039d6aa4SBill Fenner break; 800039d6aa4SBill Fenner almost_done = 1; 8015e2cc0f4SStephen McKay intvl.tv_usec = 0; 802039d6aa4SBill Fenner if (nreceived) { 803039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 804039d6aa4SBill Fenner if (!intvl.tv_sec) 805039d6aa4SBill Fenner intvl.tv_sec = 1; 806039d6aa4SBill Fenner } else 807039d6aa4SBill Fenner intvl.tv_sec = MAXWAIT; 808039d6aa4SBill Fenner } 809039d6aa4SBill Fenner (void)gettimeofday(&last, NULL); 81025107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 81125107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 81225107197SIan Dowse if (options & F_MISSED) 813ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 814039d6aa4SBill Fenner } 815039d6aa4SBill Fenner } 81625107197SIan Dowse } 8178f975bb3SBruce Evans finish(); 8188fae3551SRodney W. Grimes /* NOTREACHED */ 819f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 8208fae3551SRodney W. Grimes } 8218fae3551SRodney W. Grimes 8228fae3551SRodney W. Grimes /* 8238f975bb3SBruce Evans * stopit -- 8248f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 8258f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 8268f975bb3SBruce Evans * to be called from a signal handler. 827515dd2faSJulian Elischer */ 828515dd2faSJulian Elischer void 8298f975bb3SBruce Evans stopit(sig) 830c6facae4SMaxim Konovalov int sig __unused; 831515dd2faSJulian Elischer { 832efc8588dSDavid E. O'Brien 833515dd2faSJulian Elischer finish_up = 1; 834515dd2faSJulian Elischer } 835515dd2faSJulian Elischer 836515dd2faSJulian Elischer /* 8378fae3551SRodney W. Grimes * pinger -- 8388fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 8398fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 840eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 8414fba6582SMaxim Konovalov * bytes of the data portion are used to hold a UNIX "timeval" struct in 8424fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 8438fae3551SRodney W. Grimes */ 84443470e3bSGarrett Wollman static void 84543470e3bSGarrett Wollman pinger(void) 8468fae3551SRodney W. Grimes { 847eb1543c6SMatthew N. Dodd struct timeval now; 8480b2f8b3fSMaxim Konovalov struct ip *ip; 8493d438ad6SDavid E. O'Brien struct icmp *icp; 850efc8588dSDavid E. O'Brien int cc, i; 8510b2f8b3fSMaxim Konovalov u_char *packet; 8528fae3551SRodney W. Grimes 8530b2f8b3fSMaxim Konovalov packet = outpack; 8548fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 855eb1543c6SMatthew N. Dodd icp->icmp_type = icmp_type; 8568fae3551SRodney W. Grimes icp->icmp_code = 0; 8578fae3551SRodney W. Grimes icp->icmp_cksum = 0; 8585db89bc7SBill Fenner icp->icmp_seq = htons(ntransmitted); 8598fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 8608fae3551SRodney W. Grimes 8615db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 8628fae3551SRodney W. Grimes 863eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 864eb1543c6SMatthew N. Dodd (void)gettimeofday(&now, NULL); 8658fae3551SRodney W. Grimes 866eb1543c6SMatthew N. Dodd if (options & F_TIME) 867eb1543c6SMatthew N. Dodd icp->icmp_otime = htonl((now.tv_sec % (24*60*60)) 868eb1543c6SMatthew N. Dodd * 1000 + now.tv_usec / 1000); 869eb1543c6SMatthew N. Dodd if (timing) 870d829c3dfSMatthew N. Dodd bcopy((void *)&now, 8710fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 872eb1543c6SMatthew N. Dodd sizeof(struct timeval)); 873eb1543c6SMatthew N. Dodd } 874eb1543c6SMatthew N. Dodd 8750fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 8768fae3551SRodney W. Grimes 8778fae3551SRodney W. Grimes /* compute ICMP checksum here */ 8788fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 8798fae3551SRodney W. Grimes 8800b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 8810b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 8820b2f8b3fSMaxim Konovalov ip = (struct ip *)outpackhdr; 8830b2f8b3fSMaxim Konovalov ip->ip_len = cc; 8840b2f8b3fSMaxim Konovalov ip->ip_sum = in_cksum((u_short *)outpackhdr, cc); 8850b2f8b3fSMaxim Konovalov packet = outpackhdr; 8860b2f8b3fSMaxim Konovalov } 8870b2f8b3fSMaxim Konovalov i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto, 888d389e86aSMatt Jacob sizeof(whereto)); 8898fae3551SRodney W. Grimes 8908fae3551SRodney W. Grimes if (i < 0 || i != cc) { 89143470e3bSGarrett Wollman if (i < 0) { 8928f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 893515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 894515dd2faSJulian Elischer return; 895515dd2faSJulian Elischer } 89643470e3bSGarrett Wollman warn("sendto"); 89743470e3bSGarrett Wollman } else { 89843470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 899416aa49bSPoul-Henning Kamp hostname, i, cc); 9008fae3551SRodney W. Grimes } 901363d7bbeSJulian Elischer } 902363d7bbeSJulian Elischer ntransmitted++; 9038fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 9048fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 9058fae3551SRodney W. Grimes } 9068fae3551SRodney W. Grimes 9078fae3551SRodney W. Grimes /* 9088fae3551SRodney W. Grimes * pr_pack -- 9098fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 9108fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 9118fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 9128fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 9138fae3551SRodney W. Grimes */ 91443470e3bSGarrett Wollman static void 915039d6aa4SBill Fenner pr_pack(buf, cc, from, tv) 9168fae3551SRodney W. Grimes char *buf; 9178fae3551SRodney W. Grimes int cc; 9188fae3551SRodney W. Grimes struct sockaddr_in *from; 919039d6aa4SBill Fenner struct timeval *tv; 9208fae3551SRodney W. Grimes { 9219b219646SPeter Wemm struct in_addr ina; 9229b219646SPeter Wemm u_char *cp, *dp; 9233d438ad6SDavid E. O'Brien struct icmp *icp; 9248fae3551SRodney W. Grimes struct ip *ip; 9259d2b0ab8SPeter Wemm const void *tp; 9268f975bb3SBruce Evans double triptime; 927e88178ddSMaxim Konovalov int dupflag, hlen, i, j, recv_len, seq; 928efc8588dSDavid E. O'Brien static int old_rrlen; 929efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 9308fae3551SRodney W. Grimes 9318fae3551SRodney W. Grimes /* Check the IP header */ 9328fae3551SRodney W. Grimes ip = (struct ip *)buf; 9338fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 934e88178ddSMaxim Konovalov recv_len = cc; 9358fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 9368fae3551SRodney W. Grimes if (options & F_VERBOSE) 93743470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 93843470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 9398fae3551SRodney W. Grimes return; 9408fae3551SRodney W. Grimes } 9418fae3551SRodney W. Grimes 9428fae3551SRodney W. Grimes /* Now the ICMP part */ 9438fae3551SRodney W. Grimes cc -= hlen; 9448fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 945eb1543c6SMatthew N. Dodd if (icp->icmp_type == icmp_type_rsp) { 9468fae3551SRodney W. Grimes if (icp->icmp_id != ident) 9478fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 9488fae3551SRodney W. Grimes ++nreceived; 9498f975bb3SBruce Evans triptime = 0.0; 9508fae3551SRodney W. Grimes if (timing) { 951d32ff037SJohn Birrell struct timeval tv1; 9528fae3551SRodney W. Grimes #ifndef icmp_data 9539d2b0ab8SPeter Wemm tp = &icp->icmp_ip; 9548fae3551SRodney W. Grimes #else 9559d2b0ab8SPeter Wemm tp = icp->icmp_data; 9568fae3551SRodney W. Grimes #endif 957d829c3dfSMatthew N. Dodd tp += phdr_len; 958143008a1SMatthew N. Dodd 95947e9b3eaSMatthew N. Dodd if (cc - ICMP_MINLEN - phdr_len >= sizeof(tv1)) { 9609d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 9619d2b0ab8SPeter Wemm memcpy(&tv1, tp, sizeof(tv1)); 962039d6aa4SBill Fenner tvsub(tv, &tv1); 963039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 964039d6aa4SBill Fenner ((double)tv->tv_usec) / 1000.0; 9658fae3551SRodney W. Grimes tsum += triptime; 9663109a910SGarrett Wollman tsumsq += triptime * triptime; 9678fae3551SRodney W. Grimes if (triptime < tmin) 9688fae3551SRodney W. Grimes tmin = triptime; 9698fae3551SRodney W. Grimes if (triptime > tmax) 9708fae3551SRodney W. Grimes tmax = triptime; 97147e9b3eaSMatthew N. Dodd } else 97247e9b3eaSMatthew N. Dodd timing = 0; 9738fae3551SRodney W. Grimes } 9748fae3551SRodney W. Grimes 9755db89bc7SBill Fenner seq = ntohs(icp->icmp_seq); 9765db89bc7SBill Fenner 9775db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 9788fae3551SRodney W. Grimes ++nrepeats; 9798fae3551SRodney W. Grimes --nreceived; 9808fae3551SRodney W. Grimes dupflag = 1; 9818fae3551SRodney W. Grimes } else { 9825db89bc7SBill Fenner SET(seq % mx_dup_ck); 9838fae3551SRodney W. Grimes dupflag = 0; 9848fae3551SRodney W. Grimes } 9858fae3551SRodney W. Grimes 9868fae3551SRodney W. Grimes if (options & F_QUIET) 9878fae3551SRodney W. Grimes return; 9888fae3551SRodney W. Grimes 9898fae3551SRodney W. Grimes if (options & F_FLOOD) 9908fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 9918fae3551SRodney W. Grimes else { 9928fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 9938fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 9945db89bc7SBill Fenner seq); 9958fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 9968fae3551SRodney W. Grimes if (timing) 997d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 9988fae3551SRodney W. Grimes if (dupflag) 9998fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1000772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1001ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1002143008a1SMatthew N. Dodd if (options & F_MASK) { 1003143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1004143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1005143008a1SMatthew N. Dodd pr_addr(*(struct in_addr *)&(icp->icmp_mask))); 1006143008a1SMatthew N. Dodd } 1007eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1008eb1543c6SMatthew N. Dodd (void)printf(" tso=%s", pr_ntime(icp->icmp_otime)); 1009eb1543c6SMatthew N. Dodd (void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime)); 1010eb1543c6SMatthew N. Dodd (void)printf(" tst=%s", pr_ntime(icp->icmp_ttime)); 1011eb1543c6SMatthew N. Dodd } 1012e88178ddSMaxim Konovalov if (recv_len != send_len) { 1013e88178ddSMaxim Konovalov (void)printf( 1014e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1015e88178ddSMaxim Konovalov recv_len, send_len); 1016e88178ddSMaxim Konovalov } 10178fae3551SRodney W. Grimes /* check the data */ 1018eb1543c6SMatthew N. Dodd cp = (u_char*)&icp->icmp_data[phdr_len]; 10190fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 102047e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 102129dccd6aSMaxim Konovalov i = 0; 102229dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 102329dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 102429dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 102529dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 102629dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 102729dccd6aSMaxim Konovalov } 102829dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 10298fae3551SRodney W. Grimes if (*cp != *dp) { 10308fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 10318fae3551SRodney W. Grimes i, *dp, *cp); 10321ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 10338fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 1034d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1035e88178ddSMaxim Konovalov if ((i % 16) == 8) 1036d32ff037SJohn Birrell (void)printf("\n\t"); 1037e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1038d32ff037SJohn Birrell } 10391ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 10400fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1041d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1042e88178ddSMaxim Konovalov if ((i % 16) == 8) 10438fae3551SRodney W. Grimes (void)printf("\n\t"); 1044e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 10458fae3551SRodney W. Grimes } 10468fae3551SRodney W. Grimes break; 10478fae3551SRodney W. Grimes } 10488fae3551SRodney W. Grimes } 10498fae3551SRodney W. Grimes } 10508fae3551SRodney W. Grimes } else { 1051ef9e6dc7SBill Fenner /* 1052ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1053ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1054ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1055ef9e6dc7SBill Fenner * and ICMP type and ID. 1056f78ac61bSWarner Losh * 1057f78ac61bSWarner Losh * Only print all the error messages if we are running 1058f78ac61bSWarner Losh * as root to avoid leaking information not normally 1059f78ac61bSWarner Losh * available to those not running as root. 1060ef9e6dc7SBill Fenner */ 1061ef9e6dc7SBill Fenner #ifndef icmp_data 1062ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 1063ef9e6dc7SBill Fenner #else 1064ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 1065ef9e6dc7SBill Fenner #endif 1066ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 1067ef9e6dc7SBill Fenner 1068ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1069ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1070d389e86aSMatt Jacob (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && 1071ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 1072ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 1073ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 10748fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 107543470e3bSGarrett Wollman pr_addr(from->sin_addr)); 10768fae3551SRodney W. Grimes pr_icmph(icp); 1077ef9e6dc7SBill Fenner } else 1078ef9e6dc7SBill Fenner return; 10798fae3551SRodney W. Grimes } 10808fae3551SRodney W. Grimes 10818fae3551SRodney W. Grimes /* Display any IP options */ 10828fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 10838fae3551SRodney W. Grimes 10848fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 10858fae3551SRodney W. Grimes switch (*cp) { 10868fae3551SRodney W. Grimes case IPOPT_EOL: 10878fae3551SRodney W. Grimes hlen = 0; 10888fae3551SRodney W. Grimes break; 10898fae3551SRodney W. Grimes case IPOPT_LSRR: 1090cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1091cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1092cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1093301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 10948fae3551SRodney W. Grimes hlen -= 2; 1095301207dfSMaxim Konovalov cp += 2; 10963c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 10973c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 10988fae3551SRodney W. Grimes for (;;) { 1099301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1100301207dfSMaxim Konovalov if (ina.s_addr == 0) 11011ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1102301207dfSMaxim Konovalov else 11031ad0b1beSMaxim Konovalov (void)printf("\t%s", 11041ad0b1beSMaxim Konovalov pr_addr(ina)); 1105301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1106301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1107301207dfSMaxim Konovalov j -= INADDR_LEN; 1108301207dfSMaxim Konovalov if (j < INADDR_LEN) 11098fae3551SRodney W. Grimes break; 11108fae3551SRodney W. Grimes (void)putchar('\n'); 11118fae3551SRodney W. Grimes } 1112301207dfSMaxim Konovalov } else 1113301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 11148fae3551SRodney W. Grimes break; 11158fae3551SRodney W. Grimes case IPOPT_RR: 1116301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1117301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 11188fae3551SRodney W. Grimes hlen -= 2; 1119301207dfSMaxim Konovalov cp += 2; 11208fae3551SRodney W. Grimes if (i > j) 11218fae3551SRodney W. Grimes i = j; 1122301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1123301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1124301207dfSMaxim Konovalov old_rrlen = 0; 11258fae3551SRodney W. Grimes continue; 1126301207dfSMaxim Konovalov } 11278fae3551SRodney W. Grimes if (i == old_rrlen 11288fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 11298fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 11308fae3551SRodney W. Grimes (void)printf("\t(same route)"); 11318fae3551SRodney W. Grimes hlen -= i; 11328fae3551SRodney W. Grimes cp += i; 11338fae3551SRodney W. Grimes break; 11348fae3551SRodney W. Grimes } 11358fae3551SRodney W. Grimes old_rrlen = i; 11368fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 11378fae3551SRodney W. Grimes (void)printf("\nRR: "); 1138301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1139301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 11408fae3551SRodney W. Grimes for (;;) { 1141301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1142301207dfSMaxim Konovalov if (ina.s_addr == 0) 11431ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1144301207dfSMaxim Konovalov else 1145301207dfSMaxim Konovalov (void)printf("\t%s", 1146301207dfSMaxim Konovalov pr_addr(ina)); 1147301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1148301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1149301207dfSMaxim Konovalov i -= INADDR_LEN; 1150301207dfSMaxim Konovalov if (i < INADDR_LEN) 11518fae3551SRodney W. Grimes break; 11528fae3551SRodney W. Grimes (void)putchar('\n'); 11538fae3551SRodney W. Grimes } 1154301207dfSMaxim Konovalov } else 1155301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 11568fae3551SRodney W. Grimes break; 11578fae3551SRodney W. Grimes case IPOPT_NOP: 11588fae3551SRodney W. Grimes (void)printf("\nNOP"); 11598fae3551SRodney W. Grimes break; 11608fae3551SRodney W. Grimes default: 11618fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 11628fae3551SRodney W. Grimes break; 11638fae3551SRodney W. Grimes } 11648fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 11658fae3551SRodney W. Grimes (void)putchar('\n'); 11668fae3551SRodney W. Grimes (void)fflush(stdout); 11678fae3551SRodney W. Grimes } 11688fae3551SRodney W. Grimes } 11698fae3551SRodney W. Grimes 11708fae3551SRodney W. Grimes /* 11718fae3551SRodney W. Grimes * in_cksum -- 11728fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 11738fae3551SRodney W. Grimes */ 117443470e3bSGarrett Wollman u_short 11758fae3551SRodney W. Grimes in_cksum(addr, len) 11768fae3551SRodney W. Grimes u_short *addr; 11778fae3551SRodney W. Grimes int len; 11788fae3551SRodney W. Grimes { 1179efc8588dSDavid E. O'Brien int nleft, sum; 1180efc8588dSDavid E. O'Brien u_short *w; 11813ec12efeSPierre Beyssac union { 118209333e78SPierre Beyssac u_short us; 118309333e78SPierre Beyssac u_char uc[2]; 118409333e78SPierre Beyssac } last; 118509333e78SPierre Beyssac u_short answer; 11868fae3551SRodney W. Grimes 1187efc8588dSDavid E. O'Brien nleft = len; 1188efc8588dSDavid E. O'Brien sum = 0; 1189efc8588dSDavid E. O'Brien w = addr; 1190efc8588dSDavid E. O'Brien 11918fae3551SRodney W. Grimes /* 11928fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 11938fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 11948fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 11958fae3551SRodney W. Grimes */ 11968fae3551SRodney W. Grimes while (nleft > 1) { 11978fae3551SRodney W. Grimes sum += *w++; 11988fae3551SRodney W. Grimes nleft -= 2; 11998fae3551SRodney W. Grimes } 12008fae3551SRodney W. Grimes 12018fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 12028fae3551SRodney W. Grimes if (nleft == 1) { 120309333e78SPierre Beyssac last.uc[0] = *(u_char *)w; 120409333e78SPierre Beyssac last.uc[1] = 0; 120509333e78SPierre Beyssac sum += last.us; 12068fae3551SRodney W. Grimes } 12078fae3551SRodney W. Grimes 12088fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 12098fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 12108fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 121109333e78SPierre Beyssac answer = ~sum; /* truncate to 16 bits */ 121209333e78SPierre Beyssac return(answer); 12138fae3551SRodney W. Grimes } 12148fae3551SRodney W. Grimes 12158fae3551SRodney W. Grimes /* 12168fae3551SRodney W. Grimes * tvsub -- 12178fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 12188fae3551SRodney W. Grimes * be >= in. 12198fae3551SRodney W. Grimes */ 122043470e3bSGarrett Wollman static void 12218fae3551SRodney W. Grimes tvsub(out, in) 12223d438ad6SDavid E. O'Brien struct timeval *out, *in; 12238fae3551SRodney W. Grimes { 1224efc8588dSDavid E. O'Brien 12258fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 12268fae3551SRodney W. Grimes --out->tv_sec; 12278fae3551SRodney W. Grimes out->tv_usec += 1000000; 12288fae3551SRodney W. Grimes } 12298fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 12308fae3551SRodney W. Grimes } 12318fae3551SRodney W. Grimes 12328fae3551SRodney W. Grimes /* 1233badd8138SSean Eric Fagan * status -- 1234badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1235badd8138SSean Eric Fagan */ 1236badd8138SSean Eric Fagan 123743470e3bSGarrett Wollman static void 12387d81b35cSBruce Evans status(sig) 1239c6facae4SMaxim Konovalov int sig __unused; 12407d81b35cSBruce Evans { 1241efc8588dSDavid E. O'Brien 124237e5b2c6SSean Eric Fagan siginfo_p = 1; 124337e5b2c6SSean Eric Fagan } 124437e5b2c6SSean Eric Fagan 124543470e3bSGarrett Wollman static void 124637e5b2c6SSean Eric Fagan check_status() 1247badd8138SSean Eric Fagan { 1248efc8588dSDavid E. O'Brien 124937e5b2c6SSean Eric Fagan if (siginfo_p) { 125037e5b2c6SSean Eric Fagan siginfo_p = 0; 1251aed98a27SMaxim Konovalov (void)fprintf(stderr, "\r%ld/%ld packets received (%.0f%%)", 12527d81b35cSBruce Evans nreceived, ntransmitted, 1253aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1254aed98a27SMaxim Konovalov if (nreceived && timing) 1255aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1256aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1257aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 125837e5b2c6SSean Eric Fagan } 1259badd8138SSean Eric Fagan } 1260badd8138SSean Eric Fagan 1261badd8138SSean Eric Fagan /* 12628fae3551SRodney W. Grimes * finish -- 12638fae3551SRodney W. Grimes * Print out statistics, and give up. 12648fae3551SRodney W. Grimes */ 126543470e3bSGarrett Wollman static void 12668f975bb3SBruce Evans finish() 12678fae3551SRodney W. Grimes { 1268badd8138SSean Eric Fagan struct termios ts; 12698fae3551SRodney W. Grimes 12708fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1271a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 12728fae3551SRodney W. Grimes (void)putchar('\n'); 12738fae3551SRodney W. Grimes (void)fflush(stdout); 12748fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 12758fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 12768fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 12778fae3551SRodney W. Grimes if (nrepeats) 12788fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1279ebe70c8fSWarner Losh if (ntransmitted) { 12808fae3551SRodney W. Grimes if (nreceived > ntransmitted) 12818fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 12828fae3551SRodney W. Grimes else 12838fae3551SRodney W. Grimes (void)printf("%d%% packet loss", 12848fae3551SRodney W. Grimes (int)(((ntransmitted - nreceived) * 100) / 12858fae3551SRodney W. Grimes ntransmitted)); 1286ebe70c8fSWarner Losh } 12878fae3551SRodney W. Grimes (void)putchar('\n'); 12883109a910SGarrett Wollman if (nreceived && timing) { 12893109a910SGarrett Wollman double n = nreceived + nrepeats; 12903109a910SGarrett Wollman double avg = tsum / n; 12913109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 12921ad0b1beSMaxim Konovalov (void)printf( 12931ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 12943109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 12953109a910SGarrett Wollman } 12962ceaae21SBruce Evans if (reset_kerninfo && tcgetattr(STDOUT_FILENO, &ts) != -1) { 1297badd8138SSean Eric Fagan ts.c_lflag &= ~NOKERNINFO; 12982ceaae21SBruce Evans tcsetattr(STDOUT_FILENO, TCSANOW, &ts); 1299badd8138SSean Eric Fagan } 1300badd8138SSean Eric Fagan 13016e1173dcSDavid Greenman if (nreceived) 13028fae3551SRodney W. Grimes exit(0); 13036e1173dcSDavid Greenman else 13046e1173dcSDavid Greenman exit(2); 13058fae3551SRodney W. Grimes } 13068fae3551SRodney W. Grimes 13078fae3551SRodney W. Grimes #ifdef notdef 13088fae3551SRodney W. Grimes static char *ttab[] = { 13098fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 13108fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 13118fae3551SRodney W. Grimes "Source Quench", /* IP */ 13128fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 13138fae3551SRodney W. Grimes "Echo", 13148fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 13158fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 13168fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 13178fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 13188fae3551SRodney W. Grimes "Info Request", /* id + sq */ 13198fae3551SRodney W. Grimes "Info Reply" /* " */ 13208fae3551SRodney W. Grimes }; 13218fae3551SRodney W. Grimes #endif 13228fae3551SRodney W. Grimes 13238fae3551SRodney W. Grimes /* 13248fae3551SRodney W. Grimes * pr_icmph -- 13258fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 13268fae3551SRodney W. Grimes */ 132743470e3bSGarrett Wollman static void 13288fae3551SRodney W. Grimes pr_icmph(icp) 13298fae3551SRodney W. Grimes struct icmp *icp; 13308fae3551SRodney W. Grimes { 1331efc8588dSDavid E. O'Brien 13328fae3551SRodney W. Grimes switch(icp->icmp_type) { 13338fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 13348fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 13358fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 13368fae3551SRodney W. Grimes break; 13378fae3551SRodney W. Grimes case ICMP_UNREACH: 13388fae3551SRodney W. Grimes switch(icp->icmp_code) { 13398fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 13408fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 13418fae3551SRodney W. Grimes break; 13428fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 13438fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 13448fae3551SRodney W. Grimes break; 13458fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 13468fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 13478fae3551SRodney W. Grimes break; 13488fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 13498fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 13508fae3551SRodney W. Grimes break; 13518fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1352ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1353ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 13548fae3551SRodney W. Grimes break; 13558fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 13568fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 13578fae3551SRodney W. Grimes break; 1358ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1359ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1360ef9e6dc7SBill Fenner break; 13618fae3551SRodney W. Grimes default: 13628fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 13638fae3551SRodney W. Grimes icp->icmp_code); 13648fae3551SRodney W. Grimes break; 13658fae3551SRodney W. Grimes } 13668fae3551SRodney W. Grimes /* Print returned IP header information */ 13678fae3551SRodney W. Grimes #ifndef icmp_data 13688fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13698fae3551SRodney W. Grimes #else 13708fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13718fae3551SRodney W. Grimes #endif 13728fae3551SRodney W. Grimes break; 13738fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 13748fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 13758fae3551SRodney W. Grimes #ifndef icmp_data 13768fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 13778fae3551SRodney W. Grimes #else 13788fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 13798fae3551SRodney W. Grimes #endif 13808fae3551SRodney W. Grimes break; 13818fae3551SRodney W. Grimes case ICMP_REDIRECT: 13828fae3551SRodney W. Grimes switch(icp->icmp_code) { 13838fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 13848fae3551SRodney W. Grimes (void)printf("Redirect Network"); 13858fae3551SRodney W. Grimes break; 13868fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 13878fae3551SRodney W. Grimes (void)printf("Redirect Host"); 13888fae3551SRodney W. Grimes break; 13898fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 13908fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 13918fae3551SRodney W. Grimes break; 13928fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 13938fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 13948fae3551SRodney W. Grimes break; 13958fae3551SRodney W. Grimes default: 13968fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 13978fae3551SRodney W. Grimes break; 13988fae3551SRodney W. Grimes } 1399ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 14008fae3551SRodney W. Grimes #ifndef icmp_data 14018fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 14028fae3551SRodney W. Grimes #else 14038fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 14048fae3551SRodney W. Grimes #endif 14058fae3551SRodney W. Grimes break; 14068fae3551SRodney W. Grimes case ICMP_ECHO: 14078fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 14088fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 14098fae3551SRodney W. Grimes break; 14108fae3551SRodney W. Grimes case ICMP_TIMXCEED: 14118fae3551SRodney W. Grimes switch(icp->icmp_code) { 14128fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 14138fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 14148fae3551SRodney W. Grimes break; 14158fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 14168fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 14178fae3551SRodney W. Grimes break; 14188fae3551SRodney W. Grimes default: 14198fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 14208fae3551SRodney W. Grimes icp->icmp_code); 14218fae3551SRodney W. Grimes break; 14228fae3551SRodney W. Grimes } 14238fae3551SRodney W. Grimes #ifndef icmp_data 14248fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 14258fae3551SRodney W. Grimes #else 14268fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 14278fae3551SRodney W. Grimes #endif 14288fae3551SRodney W. Grimes break; 14298fae3551SRodney W. Grimes case ICMP_PARAMPROB: 14308fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 14318fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 14328fae3551SRodney W. Grimes #ifndef icmp_data 14338fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 14348fae3551SRodney W. Grimes #else 14358fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 14368fae3551SRodney W. Grimes #endif 14378fae3551SRodney W. Grimes break; 14388fae3551SRodney W. Grimes case ICMP_TSTAMP: 14398fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 14408fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 14418fae3551SRodney W. Grimes break; 14428fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 14438fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 14448fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 14458fae3551SRodney W. Grimes break; 14468fae3551SRodney W. Grimes case ICMP_IREQ: 14478fae3551SRodney W. Grimes (void)printf("Information Request\n"); 14488fae3551SRodney W. Grimes /* XXX ID + Seq */ 14498fae3551SRodney W. Grimes break; 14508fae3551SRodney W. Grimes case ICMP_IREQREPLY: 14518fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 14528fae3551SRodney W. Grimes /* XXX ID + Seq */ 14538fae3551SRodney W. Grimes break; 14548fae3551SRodney W. Grimes case ICMP_MASKREQ: 14558fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 14568fae3551SRodney W. Grimes break; 14578fae3551SRodney W. Grimes case ICMP_MASKREPLY: 14588fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 14598fae3551SRodney W. Grimes break; 1460ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1461ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1462ef9e6dc7SBill Fenner break; 1463ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1464ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1465ef9e6dc7SBill Fenner break; 14668fae3551SRodney W. Grimes default: 14678fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 14688fae3551SRodney W. Grimes } 14698fae3551SRodney W. Grimes } 14708fae3551SRodney W. Grimes 14718fae3551SRodney W. Grimes /* 14728fae3551SRodney W. Grimes * pr_iph -- 14738fae3551SRodney W. Grimes * Print an IP header with options. 14748fae3551SRodney W. Grimes */ 147543470e3bSGarrett Wollman static void 14768fae3551SRodney W. Grimes pr_iph(ip) 14778fae3551SRodney W. Grimes struct ip *ip; 14788fae3551SRodney W. Grimes { 14798fae3551SRodney W. Grimes u_char *cp; 1480efc8588dSDavid E. O'Brien int hlen; 14818fae3551SRodney W. Grimes 14828fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 14838fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 14848fae3551SRodney W. Grimes 1485ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 14868fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1487ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1488ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1489d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1490d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1491d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1492ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1493ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 14948fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr)); 14958fae3551SRodney W. Grimes (void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr)); 1496ef9e6dc7SBill Fenner /* dump any option bytes */ 14978fae3551SRodney W. Grimes while (hlen-- > 20) { 14988fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 14998fae3551SRodney W. Grimes } 15008fae3551SRodney W. Grimes (void)putchar('\n'); 15018fae3551SRodney W. Grimes } 15028fae3551SRodney W. Grimes 15038fae3551SRodney W. Grimes /* 15048fae3551SRodney W. Grimes * pr_addr -- 15058fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 15068fae3551SRodney W. Grimes * a hostname. 15078fae3551SRodney W. Grimes */ 150843470e3bSGarrett Wollman static char * 150943470e3bSGarrett Wollman pr_addr(ina) 151043470e3bSGarrett Wollman struct in_addr ina; 15118fae3551SRodney W. Grimes { 15128fae3551SRodney W. Grimes struct hostent *hp; 1513f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 15148fae3551SRodney W. Grimes 15158fae3551SRodney W. Grimes if ((options & F_NUMERIC) || 151643470e3bSGarrett Wollman !(hp = gethostbyaddr((char *)&ina, 4, AF_INET))) 151743470e3bSGarrett Wollman return inet_ntoa(ina); 15188fae3551SRodney W. Grimes else 1519efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 152043470e3bSGarrett Wollman inet_ntoa(ina)); 15218fae3551SRodney W. Grimes return(buf); 15228fae3551SRodney W. Grimes } 15238fae3551SRodney W. Grimes 15248fae3551SRodney W. Grimes /* 15258fae3551SRodney W. Grimes * pr_retip -- 15268fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 15278fae3551SRodney W. Grimes */ 152843470e3bSGarrett Wollman static void 15298fae3551SRodney W. Grimes pr_retip(ip) 15308fae3551SRodney W. Grimes struct ip *ip; 15318fae3551SRodney W. Grimes { 15328fae3551SRodney W. Grimes u_char *cp; 1533efc8588dSDavid E. O'Brien int hlen; 15348fae3551SRodney W. Grimes 15358fae3551SRodney W. Grimes pr_iph(ip); 15368fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 15378fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 15388fae3551SRodney W. Grimes 15398fae3551SRodney W. Grimes if (ip->ip_p == 6) 15408fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 15418fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 15428fae3551SRodney W. Grimes else if (ip->ip_p == 17) 15438fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 15448fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 15458fae3551SRodney W. Grimes } 15468fae3551SRodney W. Grimes 1547eb1543c6SMatthew N. Dodd static char * 1548007fe4e3SMaxim Konovalov pr_ntime (n_time timestamp) 1549eb1543c6SMatthew N. Dodd { 1550eb1543c6SMatthew N. Dodd static char buf[10]; 1551007fe4e3SMaxim Konovalov int hour, min, sec; 1552eb1543c6SMatthew N. Dodd 1553007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1554007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1555007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1556007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1557eb1543c6SMatthew N. Dodd 1558007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1559eb1543c6SMatthew N. Dodd 1560eb1543c6SMatthew N. Dodd return (buf); 1561eb1543c6SMatthew N. Dodd } 1562eb1543c6SMatthew N. Dodd 156343470e3bSGarrett Wollman static void 15648fae3551SRodney W. Grimes fill(bp, patp) 15658fae3551SRodney W. Grimes char *bp, *patp; 15668fae3551SRodney W. Grimes { 15678fae3551SRodney W. Grimes char *cp; 1568efc8588dSDavid E. O'Brien int pat[16]; 15694fba6582SMaxim Konovalov u_int ii, jj, kk; 15708fae3551SRodney W. Grimes 157143470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 157243470e3bSGarrett Wollman if (!isxdigit(*cp)) 157343470e3bSGarrett Wollman errx(EX_USAGE, 157443470e3bSGarrett Wollman "patterns must be specified as hex digits"); 157543470e3bSGarrett Wollman 15768fae3551SRodney W. Grimes } 15778fae3551SRodney W. Grimes ii = sscanf(patp, 15788fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 15798fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 15808fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 15818fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 15828fae3551SRodney W. Grimes 15838fae3551SRodney W. Grimes if (ii > 0) 1584d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 15858fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 15868fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 15878fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 15888fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 15898fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 15908fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 15918fae3551SRodney W. Grimes (void)printf("\n"); 15928fae3551SRodney W. Grimes } 15938fae3551SRodney W. Grimes } 15948fae3551SRodney W. Grimes 1595120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 1596120b4a93SRuslan Ermilov #define SECOPT " [-P policy]" 1597120b4a93SRuslan Ermilov #else 1598120b4a93SRuslan Ermilov #define SECOPT "" 1599120b4a93SRuslan Ermilov #endif 160043470e3bSGarrett Wollman static void 1601e345a80dSPhilippe Charnier usage() 16028fae3551SRodney W. Grimes { 160331eac03bSSean Chittenden 1604120b4a93SRuslan Ermilov (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n", 16051bd10ba2SRuslan Ermilov "usage: ping [-AaDdfnoQqRrv] [-c count] [-i wait] [-l preload] [-M mask | time]", 1606120b4a93SRuslan Ermilov " [-m ttl]" SECOPT " [-p pattern] [-S src_addr] [-s packetsize]", 16071bd10ba2SRuslan Ermilov " [-t timeout] [-z tos] host", 16081bd10ba2SRuslan Ermilov " ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]", 1609120b4a93SRuslan Ermilov " [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]", 16101bd10ba2SRuslan Ermilov " [-s packetsize] [-T ttl] [-t timeout] [-z tos] mcast-group"); 161143470e3bSGarrett Wollman exit(EX_USAGE); 16128fae3551SRodney W. Grimes } 1613