18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 48fae3551SRodney W. Grimes * Copyright (c) 1989, 1993 58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 68fae3551SRodney W. Grimes * 78fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 88fae3551SRodney W. Grimes * Mike Muuss. 98fae3551SRodney W. Grimes * 108fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 118fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 128fae3551SRodney W. Grimes * are met: 138fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 158fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 168fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 178fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 198fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 208fae3551SRodney W. Grimes * without specific prior written permission. 218fae3551SRodney W. Grimes * 228fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 238fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 248fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 258fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 268fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 278fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 288fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 298fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 308fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 318fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 328fae3551SRodney W. Grimes * SUCH DAMAGE. 338fae3551SRodney W. Grimes */ 348fae3551SRodney W. Grimes 35c69284caSDavid E. O'Brien #if 0 368fae3551SRodney W. Grimes #ifndef lint 3743470e3bSGarrett Wollman static const char copyright[] = 388fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 398fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 408fae3551SRodney W. Grimes #endif /* not lint */ 418fae3551SRodney W. Grimes 428fae3551SRodney W. Grimes #ifndef lint 438fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 448fae3551SRodney W. Grimes #endif /* not lint */ 45c69284caSDavid E. O'Brien #endif 46c69284caSDavid E. O'Brien #include <sys/cdefs.h> 47c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes /* 508fae3551SRodney W. Grimes * P I N G . C 518fae3551SRodney W. Grimes * 52e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 538fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 548fae3551SRodney W. Grimes * 558fae3551SRodney W. Grimes * Author - 568fae3551SRodney W. Grimes * Mike Muuss 578fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 588fae3551SRodney W. Grimes * December, 1983 598fae3551SRodney W. Grimes * 608fae3551SRodney W. Grimes * Status - 618fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 628fae3551SRodney W. Grimes * Bugs - 638fae3551SRodney W. Grimes * More statistics could always be gathered. 648fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 658fae3551SRodney W. Grimes */ 668fae3551SRodney W. Grimes 6743470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 68b881b8beSRobert Watson #include <sys/capsicum.h> 698fae3551SRodney W. Grimes #include <sys/socket.h> 70261e59bbSMaxim Konovalov #include <sys/sysctl.h> 718fae3551SRodney W. Grimes #include <sys/time.h> 72039d6aa4SBill Fenner #include <sys/uio.h> 738fae3551SRodney W. Grimes 748fae3551SRodney W. Grimes #include <netinet/in.h> 7543470e3bSGarrett Wollman #include <netinet/in_systm.h> 768fae3551SRodney W. Grimes #include <netinet/ip.h> 778fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 788fae3551SRodney W. Grimes #include <netinet/ip_var.h> 7943470e3bSGarrett Wollman #include <arpa/inet.h> 80c501d73cSMariusz Zaborski 81c501d73cSMariusz Zaborski #include <libcasper.h> 82c501d73cSMariusz Zaborski #include <casper/cap_dns.h> 838fae3551SRodney W. Grimes 849a4365d0SYoshinobu Inoue #ifdef IPSEC 858409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h> 869a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 879a4365d0SYoshinobu Inoue 88261e59bbSMaxim Konovalov #include <ctype.h> 89261e59bbSMaxim Konovalov #include <err.h> 90261e59bbSMaxim Konovalov #include <errno.h> 91261e59bbSMaxim Konovalov #include <math.h> 92261e59bbSMaxim Konovalov #include <netdb.h> 93261e59bbSMaxim Konovalov #include <signal.h> 94261e59bbSMaxim Konovalov #include <stdio.h> 95261e59bbSMaxim Konovalov #include <stdlib.h> 96261e59bbSMaxim Konovalov #include <string.h> 97261e59bbSMaxim Konovalov #include <sysexits.h> 98261e59bbSMaxim Konovalov #include <unistd.h> 99261e59bbSMaxim Konovalov 100301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10113e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 102eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 103eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 104c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1058f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1068f975bb3SBruce Evans /* runs out of buffer space */ 1074fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1084fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 109d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 110bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1110b2f8b3fSMaxim Konovalov #define MAXTOS 255 1128fae3551SRodney W. Grimes 1138fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1148fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1158fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1168fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1178fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1188fae3551SRodney W. Grimes 11913e3f0b7SMaxim Konovalov struct tv32 { 12013e3f0b7SMaxim Konovalov int32_t tv32_sec; 12113e3f0b7SMaxim Konovalov int32_t tv32_usec; 12213e3f0b7SMaxim Konovalov }; 12313e3f0b7SMaxim Konovalov 1248fae3551SRodney W. Grimes /* various options */ 1257e9489e0SHiroki Sato static int options; 12685456935SBill Fenner #define F_FLOOD 0x0001 12785456935SBill Fenner #define F_INTERVAL 0x0002 12885456935SBill Fenner #define F_NUMERIC 0x0004 12985456935SBill Fenner #define F_PINGFILLED 0x0008 13085456935SBill Fenner #define F_QUIET 0x0010 13185456935SBill Fenner #define F_RROUTE 0x0020 13285456935SBill Fenner #define F_SO_DEBUG 0x0040 13385456935SBill Fenner #define F_SO_DONTROUTE 0x0080 13485456935SBill Fenner #define F_VERBOSE 0x0100 13585456935SBill Fenner #define F_QUIET2 0x0200 13685456935SBill Fenner #define F_NOLOOP 0x0400 13785456935SBill Fenner #define F_MTTL 0x0800 13885456935SBill Fenner #define F_MIF 0x1000 139772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1409a4365d0SYoshinobu Inoue #ifdef IPSEC 1419a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1429a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1439a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1449a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 145211bfbd2SRuslan Ermilov #define F_TTL 0x8000 146ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1478025c44bSDima Dorfman #define F_ONCE 0x20000 1480b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 149143008a1SMatthew N. Dodd #define F_MASK 0x80000 150eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1519ff95228SGleb Smirnoff #define F_SWEEP 0x200000 152d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 1538fae3551SRodney W. Grimes 1548fae3551SRodney W. Grimes /* 1558fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1568fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1578fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1588fae3551SRodney W. Grimes */ 1598fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1607e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1617e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1628fae3551SRodney W. Grimes 1637e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1647e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1657e9489e0SHiroki Sato static int maxpayload; 1667e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1677e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1687e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1697e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1707e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 1717e9489e0SHiroki Sato static char DOT = '.'; 1727e9489e0SHiroki Sato static char *hostname; 1737e9489e0SHiroki Sato static char *shostname; 1747e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1757e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1767e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1777e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1787e9489e0SHiroki Sato static int phdr_len = 0; 1797e9489e0SHiroki Sato static int send_len; 1808fae3551SRodney W. Grimes 1818fae3551SRodney W. Grimes /* counters */ 1827e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1837e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1847e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1857e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1867e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1877e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1887e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 1897e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 1907e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 1917e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 1927e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 1937e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 1947e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 1958fae3551SRodney W. Grimes 1968fae3551SRodney W. Grimes /* timing */ 1977e9489e0SHiroki Sato static int timing; /* flag to do timing */ 1987e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 1997e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2007e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2017e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2028fae3551SRodney W. Grimes 2037e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2047e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2057e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 206badd8138SSean Eric Fagan 20749133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 20849133c6dSPawel Jakub Dawidek 20943470e3bSGarrett Wollman static void fill(char *, char *); 21043470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 21149133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 21243470e3bSGarrett Wollman static void check_status(void); 2138f975bb3SBruce Evans static void finish(void) __dead2; 21443470e3bSGarrett Wollman static void pinger(void); 21543470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 216eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 21743470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 21843470e3bSGarrett Wollman static void pr_iph(struct ip *); 219039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); 22043470e3bSGarrett Wollman static void pr_retip(struct ip *); 22143470e3bSGarrett Wollman static void status(int); 2228f975bb3SBruce Evans static void stopit(int); 223fafb8f11SEd Schouten static void tvsub(struct timeval *, const struct timeval *); 224e345a80dSPhilippe Charnier static void usage(void) __dead2; 2258fae3551SRodney W. Grimes 22643470e3bSGarrett Wollman int 227fafb8f11SEd Schouten main(int argc, char *const *argv) 2288fae3551SRodney W. Grimes { 22931eac03bSSean Chittenden struct sockaddr_in from, sock_in; 230efc8588dSDavid E. O'Brien struct in_addr ifaddr; 231261e59bbSMaxim Konovalov struct timeval last, intvl; 232efc8588dSDavid E. O'Brien struct iovec iov; 2330b2f8b3fSMaxim Konovalov struct ip *ip; 234efc8588dSDavid E. O'Brien struct msghdr msg; 235efc8588dSDavid E. O'Brien struct sigaction si_sa; 2360b2f8b3fSMaxim Konovalov size_t sz; 237e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 238d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 239261e59bbSMaxim Konovalov struct hostent *hp; 240efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 241efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 242efc8588dSDavid E. O'Brien #endif 243261e59bbSMaxim Konovalov struct sockaddr_in *to; 244261e59bbSMaxim Konovalov double t; 245*c0a3773aSEugene Grosbein u_long alarmtimeout; 246*c0a3773aSEugene Grosbein long ltmp; 24749133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 24849133c6dSPawel Jakub Dawidek int ssend_errno, srecv_errno, tos, ttl; 249efc8588dSDavid E. O'Brien char ctrl[CMSG_SPACE(sizeof(struct timeval))]; 250efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2518fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2524fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2538fae3551SRodney W. Grimes #endif 254261e59bbSMaxim Konovalov unsigned char loop, mttl; 255efc8588dSDavid E. O'Brien 25631eac03bSSean Chittenden payload = source = NULL; 2579a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 258efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2599a4365d0SYoshinobu Inoue #endif 26049133c6dSPawel Jakub Dawidek cap_rights_t rights; 26149133c6dSPawel Jakub Dawidek bool cansandbox; 2628fae3551SRodney W. Grimes 263f1284d7aSBill Fenner /* 264f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 265f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 266f1284d7aSBill Fenner * after arg parsing. 26749133c6dSPawel Jakub Dawidek * 26849133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 26949133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 27049133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 27149133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 27249133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 27349133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 27449133c6dSPawel Jakub Dawidek * packets. 275f1284d7aSBill Fenner */ 27649133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 27749133c6dSPawel Jakub Dawidek ssend_errno = errno; 27849133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 27949133c6dSPawel Jakub Dawidek srecv_errno = errno; 280f1284d7aSBill Fenner 2811d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2821d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 283ee2bf734SWarner Losh uid = getuid(); 284f1284d7aSBill Fenner 285eeb63943SDon Lewis if (ssend < 0) { 286eeb63943SDon Lewis errno = ssend_errno; 287eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 288eeb63943SDon Lewis } 289eeb63943SDon Lewis 290eeb63943SDon Lewis if (srecv < 0) { 291eeb63943SDon Lewis errno = srecv_errno; 292eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 293eeb63943SDon Lewis } 294eeb63943SDon Lewis 2950b2f8b3fSMaxim Konovalov alarmtimeout = df = preload = tos = 0; 296badd8138SSean Eric Fagan 2970b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 298211bfbd2SRuslan Ermilov while ((ch = getopt(argc, argv, 299d6cd1497SGleb Smirnoff "Aac:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:" 300211bfbd2SRuslan Ermilov #ifdef IPSEC 3019a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 302211bfbd2SRuslan Ermilov "P:" 3039a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 304211bfbd2SRuslan Ermilov #endif /*IPSEC*/ 305211bfbd2SRuslan Ermilov )) != -1) 3069a4365d0SYoshinobu Inoue { 3078fae3551SRodney W. Grimes switch(ch) { 308ca517ad8SPoul-Henning Kamp case 'A': 309ca517ad8SPoul-Henning Kamp options |= F_MISSED; 310ca517ad8SPoul-Henning Kamp break; 311772dfa72SDaniel O'Callaghan case 'a': 312772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 313772dfa72SDaniel O'Callaghan break; 3148fae3551SRodney W. Grimes case 'c': 315*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 316*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > LONG_MAX || ltmp <=0) 31743470e3bSGarrett Wollman errx(EX_USAGE, 31843470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 31943470e3bSGarrett Wollman optarg); 320*c0a3773aSEugene Grosbein npackets = ltmp; 3218fae3551SRodney W. Grimes break; 3220b2f8b3fSMaxim Konovalov case 'D': 3230b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3240b2f8b3fSMaxim Konovalov df = 1; 3250b2f8b3fSMaxim Konovalov break; 3268fae3551SRodney W. Grimes case 'd': 3278fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3288fae3551SRodney W. Grimes break; 3298fae3551SRodney W. Grimes case 'f': 330526f06b2SMatthew Dillon if (uid) { 33143470e3bSGarrett Wollman errno = EPERM; 33243470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3338fae3551SRodney W. Grimes } 3348fae3551SRodney W. Grimes options |= F_FLOOD; 3358fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3368fae3551SRodney W. Grimes break; 3379ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 338*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 339*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 3409ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3419ff95228SGleb Smirnoff optarg); 342*c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3439ff95228SGleb Smirnoff errno = EPERM; 3449ff95228SGleb Smirnoff err(EX_NOPERM, 345*c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 346*c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3479ff95228SGleb Smirnoff } 3489ff95228SGleb Smirnoff options |= F_SWEEP; 349*c0a3773aSEugene Grosbein sweepmax = ltmp; 3509ff95228SGleb Smirnoff break; 3519ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 352*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 353*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 3549ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3559ff95228SGleb Smirnoff optarg); 356*c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3579ff95228SGleb Smirnoff errno = EPERM; 3589ff95228SGleb Smirnoff err(EX_NOPERM, 359*c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 360*c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3619ff95228SGleb Smirnoff } 3629ff95228SGleb Smirnoff options |= F_SWEEP; 363*c0a3773aSEugene Grosbein sweepmin = ltmp; 3649ff95228SGleb Smirnoff break; 3659ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 366*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 367*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp < 1) 3689ff95228SGleb Smirnoff errx(EX_USAGE, "invalid increment size: `%s'", 3699ff95228SGleb Smirnoff optarg); 370*c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3719ff95228SGleb Smirnoff errno = EPERM; 3729ff95228SGleb Smirnoff err(EX_NOPERM, 373*c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 374*c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3759ff95228SGleb Smirnoff } 3769ff95228SGleb Smirnoff options |= F_SWEEP; 377*c0a3773aSEugene Grosbein sweepincr = ltmp; 3789ff95228SGleb Smirnoff break; 3791f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 3801f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 3811f6a4631SRuslan Ermilov errx(EX_USAGE, 3821f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 3831f6a4631SRuslan Ermilov optarg); 3841f6a4631SRuslan Ermilov options |= F_MIF; 3851f6a4631SRuslan Ermilov break; 3868fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 3871ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 3881ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 3891ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 3901ad0b1beSMaxim Konovalov optarg); 3918fae3551SRodney W. Grimes options |= F_INTERVAL; 392526f06b2SMatthew Dillon interval = (int)t; 393526f06b2SMatthew Dillon if (uid && interval < 1000) { 394526f06b2SMatthew Dillon errno = EPERM; 395526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 396526f06b2SMatthew Dillon } 3978fae3551SRodney W. Grimes break; 3981f6a4631SRuslan Ermilov case 'L': 3991f6a4631SRuslan Ermilov options |= F_NOLOOP; 4001f6a4631SRuslan Ermilov loop = 0; 40185456935SBill Fenner break; 4028fae3551SRodney W. Grimes case 'l': 403*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 404*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > INT_MAX || ltmp < 0) 40543470e3bSGarrett Wollman errx(EX_USAGE, 40643470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4075e2cc0f4SStephen McKay if (uid) { 408f78ac61bSWarner Losh errno = EPERM; 409f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 410f78ac61bSWarner Losh } 411*c0a3773aSEugene Grosbein preload = ltmp; 4128fae3551SRodney W. Grimes break; 4131f6a4631SRuslan Ermilov case 'M': 414eb1543c6SMatthew N. Dodd switch(optarg[0]) { 415eb1543c6SMatthew N. Dodd case 'M': 416eb1543c6SMatthew N. Dodd case 'm': 4171f6a4631SRuslan Ermilov options |= F_MASK; 41885456935SBill Fenner break; 419eb1543c6SMatthew N. Dodd case 'T': 420eb1543c6SMatthew N. Dodd case 't': 421eb1543c6SMatthew N. Dodd options |= F_TIME; 422eb1543c6SMatthew N. Dodd break; 423eb1543c6SMatthew N. Dodd default: 424eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 425eb1543c6SMatthew N. Dodd break; 426eb1543c6SMatthew N. Dodd } 427eb1543c6SMatthew N. Dodd break; 428211bfbd2SRuslan Ermilov case 'm': /* TTL */ 429*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 430*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTTL || ltmp < 0) 4311ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 432*c0a3773aSEugene Grosbein ttl = ltmp; 433211bfbd2SRuslan Ermilov options |= F_TTL; 434211bfbd2SRuslan Ermilov break; 4358fae3551SRodney W. Grimes case 'n': 4368fae3551SRodney W. Grimes options |= F_NUMERIC; 4378fae3551SRodney W. Grimes break; 4388025c44bSDima Dorfman case 'o': 4398025c44bSDima Dorfman options |= F_ONCE; 4408025c44bSDima Dorfman break; 4411f6a4631SRuslan Ermilov #ifdef IPSEC 4421f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4431f6a4631SRuslan Ermilov case 'P': 4441f6a4631SRuslan Ermilov options |= F_POLICY; 4451f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4461f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4471f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4481f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4491f6a4631SRuslan Ermilov else 4501f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4511f6a4631SRuslan Ermilov break; 4521f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4531f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4548fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4558fae3551SRodney W. Grimes options |= F_PINGFILLED; 456d074d39fSMatthew N. Dodd payload = optarg; 4578fae3551SRodney W. Grimes break; 458ef9e6dc7SBill Fenner case 'Q': 459ef9e6dc7SBill Fenner options |= F_QUIET2; 460ef9e6dc7SBill Fenner break; 4618fae3551SRodney W. Grimes case 'q': 4628fae3551SRodney W. Grimes options |= F_QUIET; 4638fae3551SRodney W. Grimes break; 4648fae3551SRodney W. Grimes case 'R': 4658fae3551SRodney W. Grimes options |= F_RROUTE; 4668fae3551SRodney W. Grimes break; 4678fae3551SRodney W. Grimes case 'r': 4688fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4698fae3551SRodney W. Grimes break; 4701f6a4631SRuslan Ermilov case 'S': 4711f6a4631SRuslan Ermilov source = optarg; 4721f6a4631SRuslan Ermilov break; 4738fae3551SRodney W. Grimes case 's': /* size of packet to send */ 474*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 475*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp < 0) 47643470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 47743470e3bSGarrett Wollman optarg); 478*c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 479fb7d32c7SMaxim Konovalov errno = EPERM; 480fb7d32c7SMaxim Konovalov err(EX_NOPERM, 481*c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 482*c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 483fb7d32c7SMaxim Konovalov } 484*c0a3773aSEugene Grosbein datalen = ltmp; 4858fae3551SRodney W. Grimes break; 4861f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 487*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 488*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTTL || ltmp < 0) 4891f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 4901f6a4631SRuslan Ermilov optarg); 491*c0a3773aSEugene Grosbein mttl = ltmp; 4921f6a4631SRuslan Ermilov options |= F_MTTL; 49399490edeSWarner Losh break; 4947237fd94SBill Fumerola case 't': 495bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 496bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 4977237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 4987237fd94SBill Fumerola optarg); 499bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 500bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 501bf113f1bSBill Fumerola optarg, MAXALARM); 502bf113f1bSBill Fumerola alarm((int)alarmtimeout); 5037237fd94SBill Fumerola break; 5048fae3551SRodney W. Grimes case 'v': 5058fae3551SRodney W. Grimes options |= F_VERBOSE; 5068fae3551SRodney W. Grimes break; 507d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 508d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 509d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 510d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 511d6cd1497SGleb Smirnoff optarg); 512d6cd1497SGleb Smirnoff options |= F_WAITTIME; 513d6cd1497SGleb Smirnoff waittime = (int)t; 514d6cd1497SGleb Smirnoff break; 5150b2f8b3fSMaxim Konovalov case 'z': 5160b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 517*c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 518*c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5190b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 520*c0a3773aSEugene Grosbein tos = ltmp; 5210b2f8b3fSMaxim Konovalov break; 5228fae3551SRodney W. Grimes default: 523e345a80dSPhilippe Charnier usage(); 52443470e3bSGarrett Wollman } 52543470e3bSGarrett Wollman } 52643470e3bSGarrett Wollman 52743470e3bSGarrett Wollman if (argc - optind != 1) 528e345a80dSPhilippe Charnier usage(); 52943470e3bSGarrett Wollman target = argv[optind]; 5308fae3551SRodney W. Grimes 531d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 532d829c3dfSMatthew N. Dodd case 0: break; 533d829c3dfSMatthew N. Dodd case F_MASK: 534eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 535eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 536d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 537eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 538eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 539d829c3dfSMatthew N. Dodd break; 540d829c3dfSMatthew N. Dodd case F_TIME: 541eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 542eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 543d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 544eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 545eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 546d829c3dfSMatthew N. Dodd break; 547d829c3dfSMatthew N. Dodd default: 548d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 549d829c3dfSMatthew N. Dodd break; 550eb1543c6SMatthew N. Dodd } 5510fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 552fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 553e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 554e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 555fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5561104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 557fb7d32c7SMaxim Konovalov maxpayload); 558e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5590fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 560d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 561d074d39fSMatthew N. Dodd fill((char *)datap, payload); 562d074d39fSMatthew N. Dodd } 56349133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 56499490edeSWarner Losh if (source) { 56531eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 56631eac03bSSean Chittenden sock_in.sin_family = AF_INET; 56731eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 56899490edeSWarner Losh shostname = source; 56999490edeSWarner Losh } else { 570d68e2c04SMariusz Zaborski hp = cap_gethostbyname2(capdns, source, AF_INET); 57199490edeSWarner Losh if (!hp) 57299490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 57399490edeSWarner Losh source, hstrerror(h_errno)); 57499490edeSWarner Losh 57531eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 57631eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 5774fba6582SMaxim Konovalov hp->h_length < 0) 57899490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 57931eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 58031eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 58199490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 58299490edeSWarner Losh sizeof(snamebuf) - 1); 58399490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 58499490edeSWarner Losh shostname = snamebuf; 58599490edeSWarner Losh } 58649133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 58749133c6dSPawel Jakub Dawidek -1) 58899490edeSWarner Losh err(1, "bind"); 58999490edeSWarner Losh } 59099490edeSWarner Losh 591d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 592d389e86aSMatt Jacob to = &whereto; 5938fae3551SRodney W. Grimes to->sin_family = AF_INET; 594d389e86aSMatt Jacob to->sin_len = sizeof *to; 59543470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 5968fae3551SRodney W. Grimes hostname = target; 59743470e3bSGarrett Wollman } else { 59849133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 59943470e3bSGarrett Wollman if (!hp) 60043470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 60143470e3bSGarrett Wollman target, hstrerror(h_errno)); 60243470e3bSGarrett Wollman 60331eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6041ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 60543470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6068fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 607b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6088fae3551SRodney W. Grimes hostname = hnamebuf; 6098fae3551SRodney W. Grimes } 6108fae3551SRodney W. Grimes 61149133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 61249133c6dSPawel Jakub Dawidek if (capdns != NULL) { 61349133c6dSPawel Jakub Dawidek const char *types[1]; 61449133c6dSPawel Jakub Dawidek 61549133c6dSPawel Jakub Dawidek types[0] = "ADDR"; 61649133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 61749133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 61849133c6dSPawel Jakub Dawidek } 61949133c6dSPawel Jakub Dawidek 62049133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 62149133c6dSPawel Jakub Dawidek err(1, "connect"); 62249133c6dSPawel Jakub Dawidek 62343470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 62443470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 62543470e3bSGarrett Wollman 62643470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 62743470e3bSGarrett Wollman errx(EX_USAGE, 62843470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 62943470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 63043470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 63143470e3bSGarrett Wollman errx(EX_USAGE, 63243470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6338fae3551SRodney W. Grimes 634d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6358fae3551SRodney W. Grimes timing = 1; 63643470e3bSGarrett Wollman 6378fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 638d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6398fae3551SRodney W. Grimes *datap++ = i; 6408fae3551SRodney W. Grimes 6418fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6428fae3551SRodney W. Grimes 6438fae3551SRodney W. Grimes hold = 1; 64449133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 64549133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6468fae3551SRodney W. Grimes sizeof(hold)); 64749133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 64849133c6dSPawel Jakub Dawidek sizeof(hold)); 64949133c6dSPawel Jakub Dawidek } 6508fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 65149133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6528fae3551SRodney W. Grimes sizeof(hold)); 6539a4365d0SYoshinobu Inoue #ifdef IPSEC 6549a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6559a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6569a4365d0SYoshinobu Inoue char *buf; 6579a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6589a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6599a4365d0SYoshinobu Inoue if (buf == NULL) 660ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 66149133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6629a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6631ad0b1beSMaxim Konovalov err(EX_CONFIG, 6641ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6659a4365d0SYoshinobu Inoue free(buf); 6669a4365d0SYoshinobu Inoue } 6679a4365d0SYoshinobu Inoue 6689a4365d0SYoshinobu Inoue if (policy_out != NULL) { 6699a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 6709a4365d0SYoshinobu Inoue if (buf == NULL) 671ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 67249133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 6739a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6741ad0b1beSMaxim Konovalov err(EX_CONFIG, 6751ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6769a4365d0SYoshinobu Inoue free(buf); 6779a4365d0SYoshinobu Inoue } 6789a4365d0SYoshinobu Inoue } 6799a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 6809a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 6818fae3551SRodney W. Grimes 6820b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 6830b2f8b3fSMaxim Konovalov ip = (struct ip*)outpackhdr; 6840b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 6850b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 6860b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 6870b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 6880b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 6890b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 6900b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 6910b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 6920b2f8b3fSMaxim Konovalov } 69349133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 6940b2f8b3fSMaxim Konovalov ip->ip_v = IPVERSION; 6950b2f8b3fSMaxim Konovalov ip->ip_hl = sizeof(struct ip) >> 2; 6960b2f8b3fSMaxim Konovalov ip->ip_tos = tos; 6970b2f8b3fSMaxim Konovalov ip->ip_id = 0; 698ffc610b2SAndrey V. Elsukov ip->ip_off = htons(df ? IP_DF : 0); 6990b2f8b3fSMaxim Konovalov ip->ip_ttl = ttl; 7000b2f8b3fSMaxim Konovalov ip->ip_p = IPPROTO_ICMP; 70131eac03bSSean Chittenden ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 7020b2f8b3fSMaxim Konovalov ip->ip_dst = to->sin_addr; 7030b2f8b3fSMaxim Konovalov } 70449133c6dSPawel Jakub Dawidek 70549133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 70649133c6dSPawel Jakub Dawidek cansandbox = true; 70749133c6dSPawel Jakub Dawidek else if (capdns != NULL) 7088751b03bSMariusz Zaborski cansandbox = CASPER_SUPPORT; 70949133c6dSPawel Jakub Dawidek else 71049133c6dSPawel Jakub Dawidek cansandbox = false; 71149133c6dSPawel Jakub Dawidek 71249133c6dSPawel Jakub Dawidek /* 71349133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 71449133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 71549133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 71649133c6dSPawel Jakub Dawidek */ 71749133c6dSPawel Jakub Dawidek if (cansandbox && cap_enter() < 0 && errno != ENOSYS) 71849133c6dSPawel Jakub Dawidek err(1, "cap_enter"); 71949133c6dSPawel Jakub Dawidek 72049133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 72149133c6dSPawel Jakub Dawidek if (cap_rights_limit(srecv, &rights) < 0 && errno != ENOSYS) 72249133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 72349133c6dSPawel Jakub Dawidek 72449133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 72549133c6dSPawel Jakub Dawidek if (cap_rights_limit(ssend, &rights) < 0 && errno != ENOSYS) 72649133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 72749133c6dSPawel Jakub Dawidek 7288fae3551SRodney W. Grimes /* record route option */ 7298fae3551SRodney W. Grimes if (options & F_RROUTE) { 7308fae3551SRodney W. Grimes #ifdef IP_OPTIONS 731039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7328fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7338fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7348fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 735039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 73649133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 73743470e3bSGarrett Wollman sizeof(rspace)) < 0) 73843470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7398fae3551SRodney W. Grimes #else 74043470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 74143470e3bSGarrett Wollman "record route not available in this implementation"); 7428fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7438fae3551SRodney W. Grimes } 7448fae3551SRodney W. Grimes 745211bfbd2SRuslan Ermilov if (options & F_TTL) { 74649133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 747211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 748211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 749211bfbd2SRuslan Ermilov } 750211bfbd2SRuslan Ermilov } 75185456935SBill Fenner if (options & F_NOLOOP) { 75249133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 75385456935SBill Fenner sizeof(loop)) < 0) { 75443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 75585456935SBill Fenner } 75685456935SBill Fenner } 75785456935SBill Fenner if (options & F_MTTL) { 75849133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 759211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 76043470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 76185456935SBill Fenner } 76285456935SBill Fenner } 76385456935SBill Fenner if (options & F_MIF) { 76449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 76585456935SBill Fenner sizeof(ifaddr)) < 0) { 76643470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 76785456935SBill Fenner } 76885456935SBill Fenner } 769039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 770039d6aa4SBill Fenner { int on = 1; 77149133c6dSPawel Jakub Dawidek if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 772039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 773039d6aa4SBill Fenner } 774039d6aa4SBill Fenner #endif 7759ff95228SGleb Smirnoff if (sweepmax) { 776bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 777bb7dfe5eSGleb Smirnoff errx(EX_USAGE, "Maximum packet size must be no less than the minimum packet size"); 7789ff95228SGleb Smirnoff 7799ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 7809ff95228SGleb Smirnoff errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive"); 7819ff95228SGleb Smirnoff 7829ff95228SGleb Smirnoff if (npackets > 0) { 7839ff95228SGleb Smirnoff snpackets = npackets; 7849ff95228SGleb Smirnoff npackets = 0; 7859ff95228SGleb Smirnoff } else 7869ff95228SGleb Smirnoff snpackets = 1; 7879ff95228SGleb Smirnoff datalen = sweepmin; 7889ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 7899ff95228SGleb Smirnoff } 7909ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 7919ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 79285456935SBill Fenner 7938fae3551SRodney W. Grimes /* 7948fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 7958fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 7968fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 79743470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 79843470e3bSGarrett Wollman * or multicast pings if they wish. 7998fae3551SRodney W. Grimes */ 8004fba6582SMaxim Konovalov 8014fba6582SMaxim Konovalov /* 8024fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8034fba6582SMaxim Konovalov * as well. 8044fba6582SMaxim Konovalov */ 8054fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 80649133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8078fae3551SRodney W. Grimes sizeof(hold)); 80849133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 80949133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 81049133c6dSPawel Jakub Dawidek if (cap_rights_limit(srecv, &rights) < 0 && errno != ENOSYS) 81149133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 812261e59bbSMaxim Konovalov if (uid == 0) 81349133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 814e8bd25ceSRobert Watson sizeof(hold)); 81549133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 81649133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 81749133c6dSPawel Jakub Dawidek if (cap_rights_limit(ssend, &rights) < 0 && errno != ENOSYS) 81849133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 819e8bd25ceSRobert Watson 82099490edeSWarner Losh if (to->sin_family == AF_INET) { 82199490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 82299490edeSWarner Losh inet_ntoa(to->sin_addr)); 82399490edeSWarner Losh if (source) 82499490edeSWarner Losh (void)printf(" from %s", shostname); 8259ff95228SGleb Smirnoff if (sweepmax) 8269ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8279ff95228SGleb Smirnoff sweepmin, sweepmax); 8289ff95228SGleb Smirnoff else 82999490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8309ff95228SGleb Smirnoff 8319ff95228SGleb Smirnoff } else { 8329ff95228SGleb Smirnoff if (sweepmax) 8339ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8349ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8359ff95228SGleb Smirnoff else 8368fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8379ff95228SGleb Smirnoff } 8388fae3551SRodney W. Grimes 8397d81b35cSBruce Evans /* 840a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 841a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8427d81b35cSBruce Evans */ 843a2a00888SSean Eric Fagan 844f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 84537e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 846a2a00888SSean Eric Fagan 847a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 848a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 849a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 850a2a00888SSean Eric Fagan } 851a2a00888SSean Eric Fagan 852a2a00888SSean Eric Fagan si_sa.sa_handler = status; 85337e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 854624ff938SWarner Losh err(EX_OSERR, "sigaction"); 85537e5b2c6SSean Eric Fagan } 856bf113f1bSBill Fumerola 857bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8587237fd94SBill Fumerola si_sa.sa_handler = stopit; 8597237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 8607237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 861bf113f1bSBill Fumerola } 86237e5b2c6SSean Eric Fagan 863039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 864039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 865039d6aa4SBill Fenner msg.msg_iov = &iov; 866039d6aa4SBill Fenner msg.msg_iovlen = 1; 867039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 868039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 869039d6aa4SBill Fenner #endif 870039d6aa4SBill Fenner iov.iov_base = packet; 871e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 872039d6aa4SBill Fenner 87332af342fSRuslan Ermilov if (preload == 0) 87432af342fSRuslan Ermilov pinger(); /* send the first ping */ 87532af342fSRuslan Ermilov else { 87632af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 87732af342fSRuslan Ermilov preload = npackets; 8788fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 8798fae3551SRodney W. Grimes pinger(); 88032af342fSRuslan Ermilov } 88132af342fSRuslan Ermilov (void)gettimeofday(&last, NULL); 8828fae3551SRodney W. Grimes 883039d6aa4SBill Fenner if (options & F_FLOOD) { 884039d6aa4SBill Fenner intvl.tv_sec = 0; 885039d6aa4SBill Fenner intvl.tv_usec = 10000; 886039d6aa4SBill Fenner } else { 887526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 888526f06b2SMatthew Dillon intvl.tv_usec = interval % 1000 * 1000; 889039d6aa4SBill Fenner } 890039d6aa4SBill Fenner 891261e59bbSMaxim Konovalov almost_done = 0; 8928f975bb3SBruce Evans while (!finish_up) { 893261e59bbSMaxim Konovalov struct timeval now, timeout; 894039d6aa4SBill Fenner fd_set rfds; 895261e59bbSMaxim Konovalov int cc, n; 8968fae3551SRodney W. Grimes 8977d81b35cSBruce Evans check_status(); 89849133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 8997e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 900039d6aa4SBill Fenner FD_ZERO(&rfds); 90149133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 902039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 903039d6aa4SBill Fenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 904039d6aa4SBill Fenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 905039d6aa4SBill Fenner while (timeout.tv_usec < 0) { 906039d6aa4SBill Fenner timeout.tv_usec += 1000000; 907039d6aa4SBill Fenner timeout.tv_sec--; 9088fae3551SRodney W. Grimes } 909e345a80dSPhilippe Charnier while (timeout.tv_usec >= 1000000) { 910039d6aa4SBill Fenner timeout.tv_usec -= 1000000; 911039d6aa4SBill Fenner timeout.tv_sec++; 912039d6aa4SBill Fenner } 913039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9146959b14dSXin LI timerclear(&timeout); 91549133c6dSPawel Jakub Dawidek n = select(srecv + 1, &rfds, NULL, NULL, &timeout); 9165e2cc0f4SStephen McKay if (n < 0) 9175e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 918039d6aa4SBill Fenner if (n == 1) { 91931eac03bSSean Chittenden struct timeval *tv = NULL; 920039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 921039d6aa4SBill Fenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 922039d6aa4SBill Fenner 923039d6aa4SBill Fenner msg.msg_controllen = sizeof(ctrl); 924039d6aa4SBill Fenner #endif 925039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 92649133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9278fae3551SRodney W. Grimes if (errno == EINTR) 9288fae3551SRodney W. Grimes continue; 929e345a80dSPhilippe Charnier warn("recvmsg"); 9308fae3551SRodney W. Grimes continue; 9318fae3551SRodney W. Grimes } 932039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 933039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 934039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 93531eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 936fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 937fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 93831eac03bSSean Chittenden tv = &now; 939fa05a94cSJohn Birrell } 940039d6aa4SBill Fenner #endif 94131eac03bSSean Chittenden if (tv == NULL) { 942039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 94331eac03bSSean Chittenden tv = &now; 944039d6aa4SBill Fenner } 94531eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 94631eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 94731eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9488fae3551SRodney W. Grimes break; 9498fae3551SRodney W. Grimes } 9505e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9519ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 9529ff95228SGleb Smirnoff for (i = 0; i < sweepincr ; ++i) 9539ff95228SGleb Smirnoff *datap++ = i; 9549ff95228SGleb Smirnoff datalen += sweepincr; 9559ff95228SGleb Smirnoff if (datalen > sweepmax) 9569ff95228SGleb Smirnoff break; 9579ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9589ff95228SGleb Smirnoff sntransmitted = 0; 9599ff95228SGleb Smirnoff } 960039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 961039d6aa4SBill Fenner pinger(); 962039d6aa4SBill Fenner else { 963039d6aa4SBill Fenner if (almost_done) 964039d6aa4SBill Fenner break; 965039d6aa4SBill Fenner almost_done = 1; 9665e2cc0f4SStephen McKay intvl.tv_usec = 0; 967039d6aa4SBill Fenner if (nreceived) { 968039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 969039d6aa4SBill Fenner if (!intvl.tv_sec) 970039d6aa4SBill Fenner intvl.tv_sec = 1; 971d6cd1497SGleb Smirnoff } else { 972d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 973d6cd1497SGleb Smirnoff intvl.tv_usec = waittime % 1000 * 1000; 974d6cd1497SGleb Smirnoff } 975039d6aa4SBill Fenner } 976039d6aa4SBill Fenner (void)gettimeofday(&last, NULL); 97725107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 97825107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 97925107197SIan Dowse if (options & F_MISSED) 980ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 981039d6aa4SBill Fenner } 982039d6aa4SBill Fenner } 98325107197SIan Dowse } 9848f975bb3SBruce Evans finish(); 9858fae3551SRodney W. Grimes /* NOTREACHED */ 986f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 9878fae3551SRodney W. Grimes } 9888fae3551SRodney W. Grimes 9898fae3551SRodney W. Grimes /* 9908f975bb3SBruce Evans * stopit -- 9918f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 9928f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 9938f975bb3SBruce Evans * to be called from a signal handler. 994515dd2faSJulian Elischer */ 995515dd2faSJulian Elischer void 996fafb8f11SEd Schouten stopit(int sig __unused) 997515dd2faSJulian Elischer { 998efc8588dSDavid E. O'Brien 999c8bb99e5SIan Dowse /* 1000c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 1001c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 1002c8bb99e5SIan Dowse */ 1003c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 1004c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 1005515dd2faSJulian Elischer finish_up = 1; 1006515dd2faSJulian Elischer } 1007515dd2faSJulian Elischer 1008515dd2faSJulian Elischer /* 10098fae3551SRodney W. Grimes * pinger -- 10108fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10118fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1012eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10134fba6582SMaxim Konovalov * bytes of the data portion are used to hold a UNIX "timeval" struct in 10144fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10158fae3551SRodney W. Grimes */ 101643470e3bSGarrett Wollman static void 101743470e3bSGarrett Wollman pinger(void) 10188fae3551SRodney W. Grimes { 1019eb1543c6SMatthew N. Dodd struct timeval now; 102013e3f0b7SMaxim Konovalov struct tv32 tv32; 10210b2f8b3fSMaxim Konovalov struct ip *ip; 10223d438ad6SDavid E. O'Brien struct icmp *icp; 1023efc8588dSDavid E. O'Brien int cc, i; 10240b2f8b3fSMaxim Konovalov u_char *packet; 10258fae3551SRodney W. Grimes 10260b2f8b3fSMaxim Konovalov packet = outpack; 10278fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 1028eb1543c6SMatthew N. Dodd icp->icmp_type = icmp_type; 10298fae3551SRodney W. Grimes icp->icmp_code = 0; 10308fae3551SRodney W. Grimes icp->icmp_cksum = 0; 10315db89bc7SBill Fenner icp->icmp_seq = htons(ntransmitted); 10328fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 10338fae3551SRodney W. Grimes 10345db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10358fae3551SRodney W. Grimes 1036eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 1037eb1543c6SMatthew N. Dodd (void)gettimeofday(&now, NULL); 10388fae3551SRodney W. Grimes 103913e3f0b7SMaxim Konovalov tv32.tv32_sec = htonl(now.tv_sec); 104013e3f0b7SMaxim Konovalov tv32.tv32_usec = htonl(now.tv_usec); 1041eb1543c6SMatthew N. Dodd if (options & F_TIME) 1042eb1543c6SMatthew N. Dodd icp->icmp_otime = htonl((now.tv_sec % (24*60*60)) 1043eb1543c6SMatthew N. Dodd * 1000 + now.tv_usec / 1000); 1044eb1543c6SMatthew N. Dodd if (timing) 104513e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10460fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 104713e3f0b7SMaxim Konovalov sizeof(tv32)); 1048eb1543c6SMatthew N. Dodd } 1049eb1543c6SMatthew N. Dodd 10500fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10518fae3551SRodney W. Grimes 10528fae3551SRodney W. Grimes /* compute ICMP checksum here */ 10538fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 10548fae3551SRodney W. Grimes 10550b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 10560b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 10570b2f8b3fSMaxim Konovalov ip = (struct ip *)outpackhdr; 1058ffc610b2SAndrey V. Elsukov ip->ip_len = htons(cc); 10590b2f8b3fSMaxim Konovalov ip->ip_sum = in_cksum((u_short *)outpackhdr, cc); 10600b2f8b3fSMaxim Konovalov packet = outpackhdr; 10610b2f8b3fSMaxim Konovalov } 106249133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 10638fae3551SRodney W. Grimes if (i < 0 || i != cc) { 106443470e3bSGarrett Wollman if (i < 0) { 10658f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1066515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1067515dd2faSJulian Elischer return; 1068515dd2faSJulian Elischer } 106943470e3bSGarrett Wollman warn("sendto"); 107043470e3bSGarrett Wollman } else { 107143470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1072416aa49bSPoul-Henning Kamp hostname, i, cc); 10738fae3551SRodney W. Grimes } 1074363d7bbeSJulian Elischer } 1075363d7bbeSJulian Elischer ntransmitted++; 10769ff95228SGleb Smirnoff sntransmitted++; 10778fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 10788fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 10798fae3551SRodney W. Grimes } 10808fae3551SRodney W. Grimes 10818fae3551SRodney W. Grimes /* 10828fae3551SRodney W. Grimes * pr_pack -- 10838fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 10848fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 10858fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 10868fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 10878fae3551SRodney W. Grimes */ 108843470e3bSGarrett Wollman static void 1089fafb8f11SEd Schouten pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv) 10908fae3551SRodney W. Grimes { 10919b219646SPeter Wemm struct in_addr ina; 10929b219646SPeter Wemm u_char *cp, *dp; 10933d438ad6SDavid E. O'Brien struct icmp *icp; 10948fae3551SRodney W. Grimes struct ip *ip; 10959d2b0ab8SPeter Wemm const void *tp; 10968f975bb3SBruce Evans double triptime; 1097e88178ddSMaxim Konovalov int dupflag, hlen, i, j, recv_len, seq; 1098efc8588dSDavid E. O'Brien static int old_rrlen; 1099efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 11008fae3551SRodney W. Grimes 11018fae3551SRodney W. Grimes /* Check the IP header */ 11028fae3551SRodney W. Grimes ip = (struct ip *)buf; 11038fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 1104e88178ddSMaxim Konovalov recv_len = cc; 11058fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11068fae3551SRodney W. Grimes if (options & F_VERBOSE) 110743470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 110843470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11098fae3551SRodney W. Grimes return; 11108fae3551SRodney W. Grimes } 11118fae3551SRodney W. Grimes 11128fae3551SRodney W. Grimes /* Now the ICMP part */ 11138fae3551SRodney W. Grimes cc -= hlen; 11148fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 1115eb1543c6SMatthew N. Dodd if (icp->icmp_type == icmp_type_rsp) { 11168fae3551SRodney W. Grimes if (icp->icmp_id != ident) 11178fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11188fae3551SRodney W. Grimes ++nreceived; 11198f975bb3SBruce Evans triptime = 0.0; 11208fae3551SRodney W. Grimes if (timing) { 1121d32ff037SJohn Birrell struct timeval tv1; 112213e3f0b7SMaxim Konovalov struct tv32 tv32; 11238fae3551SRodney W. Grimes #ifndef icmp_data 11249d2b0ab8SPeter Wemm tp = &icp->icmp_ip; 11258fae3551SRodney W. Grimes #else 11269d2b0ab8SPeter Wemm tp = icp->icmp_data; 11278fae3551SRodney W. Grimes #endif 11284eae39bfSStefan Farfeleder tp = (const char *)tp + phdr_len; 1129143008a1SMatthew N. Dodd 11307e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 11317e9489e0SHiroki Sato sizeof(tv1)) { 11329d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 113313e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 113413e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 113513e3f0b7SMaxim Konovalov tv1.tv_usec = ntohl(tv32.tv32_usec); 1136039d6aa4SBill Fenner tvsub(tv, &tv1); 1137039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 1138039d6aa4SBill Fenner ((double)tv->tv_usec) / 1000.0; 11398fae3551SRodney W. Grimes tsum += triptime; 11403109a910SGarrett Wollman tsumsq += triptime * triptime; 11418fae3551SRodney W. Grimes if (triptime < tmin) 11428fae3551SRodney W. Grimes tmin = triptime; 11438fae3551SRodney W. Grimes if (triptime > tmax) 11448fae3551SRodney W. Grimes tmax = triptime; 114547e9b3eaSMatthew N. Dodd } else 114647e9b3eaSMatthew N. Dodd timing = 0; 11478fae3551SRodney W. Grimes } 11488fae3551SRodney W. Grimes 11495db89bc7SBill Fenner seq = ntohs(icp->icmp_seq); 11505db89bc7SBill Fenner 11515db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 11528fae3551SRodney W. Grimes ++nrepeats; 11538fae3551SRodney W. Grimes --nreceived; 11548fae3551SRodney W. Grimes dupflag = 1; 11558fae3551SRodney W. Grimes } else { 11565db89bc7SBill Fenner SET(seq % mx_dup_ck); 11578fae3551SRodney W. Grimes dupflag = 0; 11588fae3551SRodney W. Grimes } 11598fae3551SRodney W. Grimes 11608fae3551SRodney W. Grimes if (options & F_QUIET) 11618fae3551SRodney W. Grimes return; 11628fae3551SRodney W. Grimes 1163d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1164d6cd1497SGleb Smirnoff ++nrcvtimeout; 1165d6cd1497SGleb Smirnoff return; 1166d6cd1497SGleb Smirnoff } 1167d6cd1497SGleb Smirnoff 11688fae3551SRodney W. Grimes if (options & F_FLOOD) 11698fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 11708fae3551SRodney W. Grimes else { 11718fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 11728fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 11735db89bc7SBill Fenner seq); 11748fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 11758fae3551SRodney W. Grimes if (timing) 1176d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 11778fae3551SRodney W. Grimes if (dupflag) 11788fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1179772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1180ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1181143008a1SMatthew N. Dodd if (options & F_MASK) { 1182143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1183143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1184f7fc5c91SMaxim Konovalov inet_ntoa(*(struct in_addr *)&(icp->icmp_mask))); 1185143008a1SMatthew N. Dodd } 1186eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1187eb1543c6SMatthew N. Dodd (void)printf(" tso=%s", pr_ntime(icp->icmp_otime)); 1188eb1543c6SMatthew N. Dodd (void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime)); 1189eb1543c6SMatthew N. Dodd (void)printf(" tst=%s", pr_ntime(icp->icmp_ttime)); 1190eb1543c6SMatthew N. Dodd } 1191e88178ddSMaxim Konovalov if (recv_len != send_len) { 1192e88178ddSMaxim Konovalov (void)printf( 1193e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1194e88178ddSMaxim Konovalov recv_len, send_len); 1195e88178ddSMaxim Konovalov } 11968fae3551SRodney W. Grimes /* check the data */ 1197eb1543c6SMatthew N. Dodd cp = (u_char*)&icp->icmp_data[phdr_len]; 11980fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 119947e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 120029dccd6aSMaxim Konovalov i = 0; 120129dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 120229dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 120329dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 120429dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 120529dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 120629dccd6aSMaxim Konovalov } 120729dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12088fae3551SRodney W. Grimes if (*cp != *dp) { 12098fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12108fae3551SRodney W. Grimes i, *dp, *cp); 12111ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 12128fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 1213d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1214e88178ddSMaxim Konovalov if ((i % 16) == 8) 1215d32ff037SJohn Birrell (void)printf("\n\t"); 1216e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1217d32ff037SJohn Birrell } 12181ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 12190fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1220d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1221e88178ddSMaxim Konovalov if ((i % 16) == 8) 12228fae3551SRodney W. Grimes (void)printf("\n\t"); 1223e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 12248fae3551SRodney W. Grimes } 12258fae3551SRodney W. Grimes break; 12268fae3551SRodney W. Grimes } 12278fae3551SRodney W. Grimes } 12288fae3551SRodney W. Grimes } 12298fae3551SRodney W. Grimes } else { 1230ef9e6dc7SBill Fenner /* 1231ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1232ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1233ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1234ef9e6dc7SBill Fenner * and ICMP type and ID. 1235f78ac61bSWarner Losh * 1236f78ac61bSWarner Losh * Only print all the error messages if we are running 1237f78ac61bSWarner Losh * as root to avoid leaking information not normally 1238f78ac61bSWarner Losh * available to those not running as root. 1239ef9e6dc7SBill Fenner */ 1240ef9e6dc7SBill Fenner #ifndef icmp_data 1241ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 1242ef9e6dc7SBill Fenner #else 1243ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 1244ef9e6dc7SBill Fenner #endif 1245ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 1246ef9e6dc7SBill Fenner 1247ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1248ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1249d389e86aSMatt Jacob (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && 1250ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 1251ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 1252ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 12538fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 125443470e3bSGarrett Wollman pr_addr(from->sin_addr)); 12558fae3551SRodney W. Grimes pr_icmph(icp); 1256ef9e6dc7SBill Fenner } else 1257ef9e6dc7SBill Fenner return; 12588fae3551SRodney W. Grimes } 12598fae3551SRodney W. Grimes 12608fae3551SRodney W. Grimes /* Display any IP options */ 12618fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 12628fae3551SRodney W. Grimes 12638fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 12648fae3551SRodney W. Grimes switch (*cp) { 12658fae3551SRodney W. Grimes case IPOPT_EOL: 12668fae3551SRodney W. Grimes hlen = 0; 12678fae3551SRodney W. Grimes break; 12688fae3551SRodney W. Grimes case IPOPT_LSRR: 1269cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1270cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1271cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1272301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 12738fae3551SRodney W. Grimes hlen -= 2; 1274301207dfSMaxim Konovalov cp += 2; 12753c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 12763c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 12778fae3551SRodney W. Grimes for (;;) { 1278301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1279301207dfSMaxim Konovalov if (ina.s_addr == 0) 12801ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1281301207dfSMaxim Konovalov else 12821ad0b1beSMaxim Konovalov (void)printf("\t%s", 12831ad0b1beSMaxim Konovalov pr_addr(ina)); 1284301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1285301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1286301207dfSMaxim Konovalov j -= INADDR_LEN; 1287301207dfSMaxim Konovalov if (j < INADDR_LEN) 12888fae3551SRodney W. Grimes break; 12898fae3551SRodney W. Grimes (void)putchar('\n'); 12908fae3551SRodney W. Grimes } 1291301207dfSMaxim Konovalov } else 1292301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 12938fae3551SRodney W. Grimes break; 12948fae3551SRodney W. Grimes case IPOPT_RR: 1295301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1296301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 12978fae3551SRodney W. Grimes hlen -= 2; 1298301207dfSMaxim Konovalov cp += 2; 12998fae3551SRodney W. Grimes if (i > j) 13008fae3551SRodney W. Grimes i = j; 1301301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1302301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1303301207dfSMaxim Konovalov old_rrlen = 0; 13048fae3551SRodney W. Grimes continue; 1305301207dfSMaxim Konovalov } 13068fae3551SRodney W. Grimes if (i == old_rrlen 13078fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 13088fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 13098fae3551SRodney W. Grimes (void)printf("\t(same route)"); 13108fae3551SRodney W. Grimes hlen -= i; 13118fae3551SRodney W. Grimes cp += i; 13128fae3551SRodney W. Grimes break; 13138fae3551SRodney W. Grimes } 13148fae3551SRodney W. Grimes old_rrlen = i; 13158fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 13168fae3551SRodney W. Grimes (void)printf("\nRR: "); 1317301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1318301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 13198fae3551SRodney W. Grimes for (;;) { 1320301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1321301207dfSMaxim Konovalov if (ina.s_addr == 0) 13221ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1323301207dfSMaxim Konovalov else 1324301207dfSMaxim Konovalov (void)printf("\t%s", 1325301207dfSMaxim Konovalov pr_addr(ina)); 1326301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1327301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1328301207dfSMaxim Konovalov i -= INADDR_LEN; 1329301207dfSMaxim Konovalov if (i < INADDR_LEN) 13308fae3551SRodney W. Grimes break; 13318fae3551SRodney W. Grimes (void)putchar('\n'); 13328fae3551SRodney W. Grimes } 1333301207dfSMaxim Konovalov } else 1334301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 13358fae3551SRodney W. Grimes break; 13368fae3551SRodney W. Grimes case IPOPT_NOP: 13378fae3551SRodney W. Grimes (void)printf("\nNOP"); 13388fae3551SRodney W. Grimes break; 13398fae3551SRodney W. Grimes default: 13408fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 13418fae3551SRodney W. Grimes break; 13428fae3551SRodney W. Grimes } 13438fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 13448fae3551SRodney W. Grimes (void)putchar('\n'); 13458fae3551SRodney W. Grimes (void)fflush(stdout); 13468fae3551SRodney W. Grimes } 13478fae3551SRodney W. Grimes } 13488fae3551SRodney W. Grimes 13498fae3551SRodney W. Grimes /* 13508fae3551SRodney W. Grimes * in_cksum -- 13518fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 13528fae3551SRodney W. Grimes */ 135343470e3bSGarrett Wollman u_short 1354fafb8f11SEd Schouten in_cksum(u_short *addr, int len) 13558fae3551SRodney W. Grimes { 1356efc8588dSDavid E. O'Brien int nleft, sum; 1357efc8588dSDavid E. O'Brien u_short *w; 13583ec12efeSPierre Beyssac union { 135909333e78SPierre Beyssac u_short us; 136009333e78SPierre Beyssac u_char uc[2]; 136109333e78SPierre Beyssac } last; 136209333e78SPierre Beyssac u_short answer; 13638fae3551SRodney W. Grimes 1364efc8588dSDavid E. O'Brien nleft = len; 1365efc8588dSDavid E. O'Brien sum = 0; 1366efc8588dSDavid E. O'Brien w = addr; 1367efc8588dSDavid E. O'Brien 13688fae3551SRodney W. Grimes /* 13698fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 13708fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 13718fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 13728fae3551SRodney W. Grimes */ 13738fae3551SRodney W. Grimes while (nleft > 1) { 13748fae3551SRodney W. Grimes sum += *w++; 13758fae3551SRodney W. Grimes nleft -= 2; 13768fae3551SRodney W. Grimes } 13778fae3551SRodney W. Grimes 13788fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 13798fae3551SRodney W. Grimes if (nleft == 1) { 138009333e78SPierre Beyssac last.uc[0] = *(u_char *)w; 138109333e78SPierre Beyssac last.uc[1] = 0; 138209333e78SPierre Beyssac sum += last.us; 13838fae3551SRodney W. Grimes } 13848fae3551SRodney W. Grimes 13858fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 13868fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 13878fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 138809333e78SPierre Beyssac answer = ~sum; /* truncate to 16 bits */ 138909333e78SPierre Beyssac return(answer); 13908fae3551SRodney W. Grimes } 13918fae3551SRodney W. Grimes 13928fae3551SRodney W. Grimes /* 13938fae3551SRodney W. Grimes * tvsub -- 13948fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 13958fae3551SRodney W. Grimes * be >= in. 13968fae3551SRodney W. Grimes */ 139743470e3bSGarrett Wollman static void 1398fafb8f11SEd Schouten tvsub(struct timeval *out, const struct timeval *in) 13998fae3551SRodney W. Grimes { 1400efc8588dSDavid E. O'Brien 14018fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 14028fae3551SRodney W. Grimes --out->tv_sec; 14038fae3551SRodney W. Grimes out->tv_usec += 1000000; 14048fae3551SRodney W. Grimes } 14058fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 14068fae3551SRodney W. Grimes } 14078fae3551SRodney W. Grimes 14088fae3551SRodney W. Grimes /* 1409badd8138SSean Eric Fagan * status -- 1410badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1411badd8138SSean Eric Fagan */ 1412badd8138SSean Eric Fagan 141343470e3bSGarrett Wollman static void 1414fafb8f11SEd Schouten status(int sig __unused) 14157d81b35cSBruce Evans { 1416efc8588dSDavid E. O'Brien 141737e5b2c6SSean Eric Fagan siginfo_p = 1; 141837e5b2c6SSean Eric Fagan } 141937e5b2c6SSean Eric Fagan 142043470e3bSGarrett Wollman static void 1421fafb8f11SEd Schouten check_status(void) 1422badd8138SSean Eric Fagan { 1423efc8588dSDavid E. O'Brien 142437e5b2c6SSean Eric Fagan if (siginfo_p) { 142537e5b2c6SSean Eric Fagan siginfo_p = 0; 1426aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 14277d81b35cSBruce Evans nreceived, ntransmitted, 1428aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1429aed98a27SMaxim Konovalov if (nreceived && timing) 1430aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1431aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1432aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 143337e5b2c6SSean Eric Fagan } 1434badd8138SSean Eric Fagan } 1435badd8138SSean Eric Fagan 1436badd8138SSean Eric Fagan /* 14378fae3551SRodney W. Grimes * finish -- 14388fae3551SRodney W. Grimes * Print out statistics, and give up. 14398fae3551SRodney W. Grimes */ 144043470e3bSGarrett Wollman static void 1441fafb8f11SEd Schouten finish(void) 14428fae3551SRodney W. Grimes { 14438fae3551SRodney W. Grimes 14448fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1445a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 14468fae3551SRodney W. Grimes (void)putchar('\n'); 14478fae3551SRodney W. Grimes (void)fflush(stdout); 14488fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 14498fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 14508fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 14518fae3551SRodney W. Grimes if (nrepeats) 14528fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1453ebe70c8fSWarner Losh if (ntransmitted) { 14548fae3551SRodney W. Grimes if (nreceived > ntransmitted) 14558fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 14568fae3551SRodney W. Grimes else 1457aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1458aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1459aa822c39SDima Dorfman ntransmitted); 1460ebe70c8fSWarner Losh } 1461d6cd1497SGleb Smirnoff if (nrcvtimeout) 1462d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 14638fae3551SRodney W. Grimes (void)putchar('\n'); 14643109a910SGarrett Wollman if (nreceived && timing) { 14653109a910SGarrett Wollman double n = nreceived + nrepeats; 14663109a910SGarrett Wollman double avg = tsum / n; 14673109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 14681ad0b1beSMaxim Konovalov (void)printf( 14691ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 14703109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 14713109a910SGarrett Wollman } 1472badd8138SSean Eric Fagan 14736e1173dcSDavid Greenman if (nreceived) 14748fae3551SRodney W. Grimes exit(0); 14756e1173dcSDavid Greenman else 14766e1173dcSDavid Greenman exit(2); 14778fae3551SRodney W. Grimes } 14788fae3551SRodney W. Grimes 14798fae3551SRodney W. Grimes #ifdef notdef 14808fae3551SRodney W. Grimes static char *ttab[] = { 14818fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 14828fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 14838fae3551SRodney W. Grimes "Source Quench", /* IP */ 14848fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 14858fae3551SRodney W. Grimes "Echo", 14868fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 14878fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 14888fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 14898fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 14908fae3551SRodney W. Grimes "Info Request", /* id + sq */ 14918fae3551SRodney W. Grimes "Info Reply" /* " */ 14928fae3551SRodney W. Grimes }; 14938fae3551SRodney W. Grimes #endif 14948fae3551SRodney W. Grimes 14958fae3551SRodney W. Grimes /* 14968fae3551SRodney W. Grimes * pr_icmph -- 14978fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 14988fae3551SRodney W. Grimes */ 149943470e3bSGarrett Wollman static void 1500fafb8f11SEd Schouten pr_icmph(struct icmp *icp) 15018fae3551SRodney W. Grimes { 1502efc8588dSDavid E. O'Brien 15038fae3551SRodney W. Grimes switch(icp->icmp_type) { 15048fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 15058fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 15068fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15078fae3551SRodney W. Grimes break; 15088fae3551SRodney W. Grimes case ICMP_UNREACH: 15098fae3551SRodney W. Grimes switch(icp->icmp_code) { 15108fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 15118fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 15128fae3551SRodney W. Grimes break; 15138fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 15148fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 15158fae3551SRodney W. Grimes break; 15168fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 15178fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 15188fae3551SRodney W. Grimes break; 15198fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 15208fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 15218fae3551SRodney W. Grimes break; 15228fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1523ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1524ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 15258fae3551SRodney W. Grimes break; 15268fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 15278fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 15288fae3551SRodney W. Grimes break; 1529ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1530ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1531ef9e6dc7SBill Fenner break; 15328fae3551SRodney W. Grimes default: 15338fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 15348fae3551SRodney W. Grimes icp->icmp_code); 15358fae3551SRodney W. Grimes break; 15368fae3551SRodney W. Grimes } 15378fae3551SRodney W. Grimes /* Print returned IP header information */ 15388fae3551SRodney W. Grimes #ifndef icmp_data 15398fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15408fae3551SRodney W. Grimes #else 15418fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15428fae3551SRodney W. Grimes #endif 15438fae3551SRodney W. Grimes break; 15448fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 15458fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 15468fae3551SRodney W. Grimes #ifndef icmp_data 15478fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15488fae3551SRodney W. Grimes #else 15498fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15508fae3551SRodney W. Grimes #endif 15518fae3551SRodney W. Grimes break; 15528fae3551SRodney W. Grimes case ICMP_REDIRECT: 15538fae3551SRodney W. Grimes switch(icp->icmp_code) { 15548fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 15558fae3551SRodney W. Grimes (void)printf("Redirect Network"); 15568fae3551SRodney W. Grimes break; 15578fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 15588fae3551SRodney W. Grimes (void)printf("Redirect Host"); 15598fae3551SRodney W. Grimes break; 15608fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15618fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15628fae3551SRodney W. Grimes break; 15638fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15648fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 15658fae3551SRodney W. Grimes break; 15668fae3551SRodney W. Grimes default: 15678fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 15688fae3551SRodney W. Grimes break; 15698fae3551SRodney W. Grimes } 1570ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 15718fae3551SRodney W. Grimes #ifndef icmp_data 15728fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15738fae3551SRodney W. Grimes #else 15748fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15758fae3551SRodney W. Grimes #endif 15768fae3551SRodney W. Grimes break; 15778fae3551SRodney W. Grimes case ICMP_ECHO: 15788fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 15798fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15808fae3551SRodney W. Grimes break; 15818fae3551SRodney W. Grimes case ICMP_TIMXCEED: 15828fae3551SRodney W. Grimes switch(icp->icmp_code) { 15838fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 15848fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 15858fae3551SRodney W. Grimes break; 15868fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 15878fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 15888fae3551SRodney W. Grimes break; 15898fae3551SRodney W. Grimes default: 15908fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 15918fae3551SRodney W. Grimes icp->icmp_code); 15928fae3551SRodney W. Grimes break; 15938fae3551SRodney W. Grimes } 15948fae3551SRodney W. Grimes #ifndef icmp_data 15958fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15968fae3551SRodney W. Grimes #else 15978fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15988fae3551SRodney W. Grimes #endif 15998fae3551SRodney W. Grimes break; 16008fae3551SRodney W. Grimes case ICMP_PARAMPROB: 16018fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 16028fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 16038fae3551SRodney W. Grimes #ifndef icmp_data 16048fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 16058fae3551SRodney W. Grimes #else 16068fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 16078fae3551SRodney W. Grimes #endif 16088fae3551SRodney W. Grimes break; 16098fae3551SRodney W. Grimes case ICMP_TSTAMP: 16108fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 16118fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16128fae3551SRodney W. Grimes break; 16138fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 16148fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 16158fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16168fae3551SRodney W. Grimes break; 16178fae3551SRodney W. Grimes case ICMP_IREQ: 16188fae3551SRodney W. Grimes (void)printf("Information Request\n"); 16198fae3551SRodney W. Grimes /* XXX ID + Seq */ 16208fae3551SRodney W. Grimes break; 16218fae3551SRodney W. Grimes case ICMP_IREQREPLY: 16228fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 16238fae3551SRodney W. Grimes /* XXX ID + Seq */ 16248fae3551SRodney W. Grimes break; 16258fae3551SRodney W. Grimes case ICMP_MASKREQ: 16268fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 16278fae3551SRodney W. Grimes break; 16288fae3551SRodney W. Grimes case ICMP_MASKREPLY: 16298fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 16308fae3551SRodney W. Grimes break; 1631ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1632ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1633ef9e6dc7SBill Fenner break; 1634ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1635ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1636ef9e6dc7SBill Fenner break; 16378fae3551SRodney W. Grimes default: 16388fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 16398fae3551SRodney W. Grimes } 16408fae3551SRodney W. Grimes } 16418fae3551SRodney W. Grimes 16428fae3551SRodney W. Grimes /* 16438fae3551SRodney W. Grimes * pr_iph -- 16448fae3551SRodney W. Grimes * Print an IP header with options. 16458fae3551SRodney W. Grimes */ 164643470e3bSGarrett Wollman static void 1647fafb8f11SEd Schouten pr_iph(struct ip *ip) 16488fae3551SRodney W. Grimes { 1649a94c074dSDimitry Andric struct in_addr ina; 16508fae3551SRodney W. Grimes u_char *cp; 1651efc8588dSDavid E. O'Brien int hlen; 16528fae3551SRodney W. Grimes 16538fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 16548fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 16558fae3551SRodney W. Grimes 1656ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 16578fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1658ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1659ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1660d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1661d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1662d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1663ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1664ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1665a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1666a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1667a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1668a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1669ef9e6dc7SBill Fenner /* dump any option bytes */ 16708fae3551SRodney W. Grimes while (hlen-- > 20) { 16718fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16728fae3551SRodney W. Grimes } 16738fae3551SRodney W. Grimes (void)putchar('\n'); 16748fae3551SRodney W. Grimes } 16758fae3551SRodney W. Grimes 16768fae3551SRodney W. Grimes /* 16778fae3551SRodney W. Grimes * pr_addr -- 16788fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 16798fae3551SRodney W. Grimes * a hostname. 16808fae3551SRodney W. Grimes */ 168143470e3bSGarrett Wollman static char * 1682fafb8f11SEd Schouten pr_addr(struct in_addr ina) 16838fae3551SRodney W. Grimes { 16848fae3551SRodney W. Grimes struct hostent *hp; 1685f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 16868fae3551SRodney W. Grimes 168749133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 168843470e3bSGarrett Wollman return inet_ntoa(ina); 168949133c6dSPawel Jakub Dawidek 169049133c6dSPawel Jakub Dawidek hp = cap_gethostbyaddr(capdns, (char *)&ina, 4, AF_INET); 169149133c6dSPawel Jakub Dawidek 169249133c6dSPawel Jakub Dawidek if (hp == NULL) 169349133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 169449133c6dSPawel Jakub Dawidek 1695efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 169643470e3bSGarrett Wollman inet_ntoa(ina)); 16978fae3551SRodney W. Grimes return(buf); 16988fae3551SRodney W. Grimes } 16998fae3551SRodney W. Grimes 17008fae3551SRodney W. Grimes /* 17018fae3551SRodney W. Grimes * pr_retip -- 17028fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 17038fae3551SRodney W. Grimes */ 170443470e3bSGarrett Wollman static void 1705fafb8f11SEd Schouten pr_retip(struct ip *ip) 17068fae3551SRodney W. Grimes { 17078fae3551SRodney W. Grimes u_char *cp; 1708efc8588dSDavid E. O'Brien int hlen; 17098fae3551SRodney W. Grimes 17108fae3551SRodney W. Grimes pr_iph(ip); 17118fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 17128fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 17138fae3551SRodney W. Grimes 17148fae3551SRodney W. Grimes if (ip->ip_p == 6) 17158fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 17168fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17178fae3551SRodney W. Grimes else if (ip->ip_p == 17) 17188fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 17198fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17208fae3551SRodney W. Grimes } 17218fae3551SRodney W. Grimes 1722eb1543c6SMatthew N. Dodd static char * 1723007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1724eb1543c6SMatthew N. Dodd { 1725eb1543c6SMatthew N. Dodd static char buf[10]; 1726007fe4e3SMaxim Konovalov int hour, min, sec; 1727eb1543c6SMatthew N. Dodd 1728007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1729007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1730007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1731007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1732eb1543c6SMatthew N. Dodd 1733007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1734eb1543c6SMatthew N. Dodd 1735eb1543c6SMatthew N. Dodd return (buf); 1736eb1543c6SMatthew N. Dodd } 1737eb1543c6SMatthew N. Dodd 173843470e3bSGarrett Wollman static void 1739fafb8f11SEd Schouten fill(char *bp, char *patp) 17408fae3551SRodney W. Grimes { 17418fae3551SRodney W. Grimes char *cp; 1742efc8588dSDavid E. O'Brien int pat[16]; 17434fba6582SMaxim Konovalov u_int ii, jj, kk; 17448fae3551SRodney W. Grimes 174543470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 174643470e3bSGarrett Wollman if (!isxdigit(*cp)) 174743470e3bSGarrett Wollman errx(EX_USAGE, 174843470e3bSGarrett Wollman "patterns must be specified as hex digits"); 174943470e3bSGarrett Wollman 17508fae3551SRodney W. Grimes } 17518fae3551SRodney W. Grimes ii = sscanf(patp, 17528fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 17538fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 17548fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 17558fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 17568fae3551SRodney W. Grimes 17578fae3551SRodney W. Grimes if (ii > 0) 1758d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 17598fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17608fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17618fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17628fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17638fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17648fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17658fae3551SRodney W. Grimes (void)printf("\n"); 17668fae3551SRodney W. Grimes } 17678fae3551SRodney W. Grimes } 17688fae3551SRodney W. Grimes 176949133c6dSPawel Jakub Dawidek static cap_channel_t * 177049133c6dSPawel Jakub Dawidek capdns_setup(void) 177149133c6dSPawel Jakub Dawidek { 177249133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 177349133c6dSPawel Jakub Dawidek const char *types[2]; 177449133c6dSPawel Jakub Dawidek int families[1]; 177549133c6dSPawel Jakub Dawidek 177649133c6dSPawel Jakub Dawidek capcas = cap_init(); 1777c501d73cSMariusz Zaborski if (capcas == NULL) 1778c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 177949133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 178049133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 178149133c6dSPawel Jakub Dawidek cap_close(capcas); 178249133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 178349133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 178449133c6dSPawel Jakub Dawidek types[0] = "NAME"; 178549133c6dSPawel Jakub Dawidek types[1] = "ADDR"; 178649133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 178749133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 178849133c6dSPawel Jakub Dawidek families[0] = AF_INET; 178949133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 179049133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 179149133c6dSPawel Jakub Dawidek 179249133c6dSPawel Jakub Dawidek return (capdnsloc); 179349133c6dSPawel Jakub Dawidek } 179449133c6dSPawel Jakub Dawidek 1795120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 1796120b4a93SRuslan Ermilov #define SECOPT " [-P policy]" 1797120b4a93SRuslan Ermilov #else 1798120b4a93SRuslan Ermilov #define SECOPT "" 1799120b4a93SRuslan Ermilov #endif 180043470e3bSGarrett Wollman static void 1801fafb8f11SEd Schouten usage(void) 18028fae3551SRodney W. Grimes { 180331eac03bSSean Chittenden 1804d6cd1497SGleb Smirnoff (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 1805ee3e1c4cSRuslan Ermilov "usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]", 1806ee3e1c4cSRuslan Ermilov " [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]", 1807ee3e1c4cSRuslan Ermilov " " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]", 1808d6cd1497SGleb Smirnoff " [-W waittime] [-z tos] host", 18091bd10ba2SRuslan Ermilov " ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]", 1810120b4a93SRuslan Ermilov " [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]", 1811d6cd1497SGleb Smirnoff " [-s packetsize] [-T ttl] [-t timeout] [-W waittime]", 1812d6cd1497SGleb Smirnoff " [-z tos] mcast-group"); 181343470e3bSGarrett Wollman exit(EX_USAGE); 18148fae3551SRodney W. Grimes } 1815