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 88*7bdc3291SMark Johnston #include <capsicum_helpers.h> 89261e59bbSMaxim Konovalov #include <ctype.h> 90261e59bbSMaxim Konovalov #include <err.h> 91261e59bbSMaxim Konovalov #include <errno.h> 92261e59bbSMaxim Konovalov #include <math.h> 93261e59bbSMaxim Konovalov #include <netdb.h> 94261e59bbSMaxim Konovalov #include <signal.h> 95261e59bbSMaxim Konovalov #include <stdio.h> 96261e59bbSMaxim Konovalov #include <stdlib.h> 97261e59bbSMaxim Konovalov #include <string.h> 98261e59bbSMaxim Konovalov #include <sysexits.h> 99261e59bbSMaxim Konovalov #include <unistd.h> 100261e59bbSMaxim Konovalov 101301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10213e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 103eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 104eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 105c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1068f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1078f975bb3SBruce Evans /* runs out of buffer space */ 1084fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1094fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 110d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 111bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1120b2f8b3fSMaxim Konovalov #define MAXTOS 255 1138fae3551SRodney W. Grimes 1148fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1158fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1168fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1178fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1188fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1198fae3551SRodney W. Grimes 12013e3f0b7SMaxim Konovalov struct tv32 { 12113e3f0b7SMaxim Konovalov int32_t tv32_sec; 12213e3f0b7SMaxim Konovalov int32_t tv32_usec; 12313e3f0b7SMaxim Konovalov }; 12413e3f0b7SMaxim Konovalov 1258fae3551SRodney W. Grimes /* various options */ 1267e9489e0SHiroki Sato static int options; 12785456935SBill Fenner #define F_FLOOD 0x0001 12885456935SBill Fenner #define F_INTERVAL 0x0002 12985456935SBill Fenner #define F_NUMERIC 0x0004 13085456935SBill Fenner #define F_PINGFILLED 0x0008 13185456935SBill Fenner #define F_QUIET 0x0010 13285456935SBill Fenner #define F_RROUTE 0x0020 13385456935SBill Fenner #define F_SO_DEBUG 0x0040 13485456935SBill Fenner #define F_SO_DONTROUTE 0x0080 13585456935SBill Fenner #define F_VERBOSE 0x0100 13685456935SBill Fenner #define F_QUIET2 0x0200 13785456935SBill Fenner #define F_NOLOOP 0x0400 13885456935SBill Fenner #define F_MTTL 0x0800 13985456935SBill Fenner #define F_MIF 0x1000 140772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1419a4365d0SYoshinobu Inoue #ifdef IPSEC 1429a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1439a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1449a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1459a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 146211bfbd2SRuslan Ermilov #define F_TTL 0x8000 147ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1488025c44bSDima Dorfman #define F_ONCE 0x20000 1490b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 150143008a1SMatthew N. Dodd #define F_MASK 0x80000 151eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1529ff95228SGleb Smirnoff #define F_SWEEP 0x200000 153d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 1548fae3551SRodney W. Grimes 1558fae3551SRodney W. Grimes /* 1568fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1578fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1588fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1598fae3551SRodney W. Grimes */ 1608fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1617e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1627e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1638fae3551SRodney W. Grimes 1647e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1657e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1667e9489e0SHiroki Sato static int maxpayload; 1677e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1687e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1697e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1707e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1717e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 1727e9489e0SHiroki Sato static char DOT = '.'; 1737e9489e0SHiroki Sato static char *hostname; 1747e9489e0SHiroki Sato static char *shostname; 1757e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1767e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1777e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1787e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1797e9489e0SHiroki Sato static int phdr_len = 0; 1807e9489e0SHiroki Sato static int send_len; 1818fae3551SRodney W. Grimes 1828fae3551SRodney W. Grimes /* counters */ 1837e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1847e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1857e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1867e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1877e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1887e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1897e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 1907e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 1917e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 1927e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 1937e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 1947e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 1957e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 1968fae3551SRodney W. Grimes 1978fae3551SRodney W. Grimes /* timing */ 1987e9489e0SHiroki Sato static int timing; /* flag to do timing */ 1997e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 2007e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2017e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2027e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2038fae3551SRodney W. Grimes 2047e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2057e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2067e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 207badd8138SSean Eric Fagan 20849133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 20949133c6dSPawel Jakub Dawidek 21043470e3bSGarrett Wollman static void fill(char *, char *); 21143470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 21249133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 21343470e3bSGarrett Wollman static void check_status(void); 2148f975bb3SBruce Evans static void finish(void) __dead2; 21543470e3bSGarrett Wollman static void pinger(void); 21643470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 217eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 21843470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 21943470e3bSGarrett Wollman static void pr_iph(struct ip *); 220039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); 22143470e3bSGarrett Wollman static void pr_retip(struct ip *); 22243470e3bSGarrett Wollman static void status(int); 2238f975bb3SBruce Evans static void stopit(int); 224fafb8f11SEd Schouten static void tvsub(struct timeval *, const struct timeval *); 225e345a80dSPhilippe Charnier static void usage(void) __dead2; 2268fae3551SRodney W. Grimes 22743470e3bSGarrett Wollman int 228fafb8f11SEd Schouten main(int argc, char *const *argv) 2298fae3551SRodney W. Grimes { 23031eac03bSSean Chittenden struct sockaddr_in from, sock_in; 231efc8588dSDavid E. O'Brien struct in_addr ifaddr; 232261e59bbSMaxim Konovalov struct timeval last, intvl; 233efc8588dSDavid E. O'Brien struct iovec iov; 2340b2f8b3fSMaxim Konovalov struct ip *ip; 235efc8588dSDavid E. O'Brien struct msghdr msg; 236efc8588dSDavid E. O'Brien struct sigaction si_sa; 2370b2f8b3fSMaxim Konovalov size_t sz; 238e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 239d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 240261e59bbSMaxim Konovalov struct hostent *hp; 241efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 242efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 243efc8588dSDavid E. O'Brien #endif 244261e59bbSMaxim Konovalov struct sockaddr_in *to; 245261e59bbSMaxim Konovalov double t; 246c0a3773aSEugene Grosbein u_long alarmtimeout; 247c0a3773aSEugene Grosbein long ltmp; 24849133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 24949133c6dSPawel Jakub Dawidek int ssend_errno, srecv_errno, tos, ttl; 250efc8588dSDavid E. O'Brien char ctrl[CMSG_SPACE(sizeof(struct timeval))]; 251efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2528fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2534fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2548fae3551SRodney W. Grimes #endif 255261e59bbSMaxim Konovalov unsigned char loop, mttl; 256efc8588dSDavid E. O'Brien 25731eac03bSSean Chittenden payload = source = NULL; 2589a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 259efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2609a4365d0SYoshinobu Inoue #endif 26149133c6dSPawel Jakub Dawidek cap_rights_t rights; 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': 315c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 31665c3a67dSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 31743470e3bSGarrett Wollman errx(EX_USAGE, 31843470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 31943470e3bSGarrett Wollman optarg); 320c0a3773aSEugene 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 */ 338c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 339c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 3409ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3419ff95228SGleb Smirnoff optarg); 342c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3439ff95228SGleb Smirnoff errno = EPERM; 3449ff95228SGleb Smirnoff err(EX_NOPERM, 345c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 346c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3479ff95228SGleb Smirnoff } 3489ff95228SGleb Smirnoff options |= F_SWEEP; 349c0a3773aSEugene Grosbein sweepmax = ltmp; 3509ff95228SGleb Smirnoff break; 3519ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 352c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 353c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 3549ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3559ff95228SGleb Smirnoff optarg); 356c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3579ff95228SGleb Smirnoff errno = EPERM; 3589ff95228SGleb Smirnoff err(EX_NOPERM, 359c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 360c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3619ff95228SGleb Smirnoff } 3629ff95228SGleb Smirnoff options |= F_SWEEP; 363c0a3773aSEugene Grosbein sweepmin = ltmp; 3649ff95228SGleb Smirnoff break; 3659ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 366c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 367c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp < 1) 3689ff95228SGleb Smirnoff errx(EX_USAGE, "invalid increment size: `%s'", 3699ff95228SGleb Smirnoff optarg); 370c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3719ff95228SGleb Smirnoff errno = EPERM; 3729ff95228SGleb Smirnoff err(EX_NOPERM, 373c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 374c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3759ff95228SGleb Smirnoff } 3769ff95228SGleb Smirnoff options |= F_SWEEP; 377c0a3773aSEugene 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': 403c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 404c0a3773aSEugene 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 } 411c0a3773aSEugene 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 */ 429c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 430c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTTL || ltmp < 0) 4311ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 432c0a3773aSEugene 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 */ 474c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 475c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp < 0) 47643470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 47743470e3bSGarrett Wollman optarg); 478c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 479fb7d32c7SMaxim Konovalov errno = EPERM; 480fb7d32c7SMaxim Konovalov err(EX_NOPERM, 481c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 482c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 483fb7d32c7SMaxim Konovalov } 484c0a3773aSEugene Grosbein datalen = ltmp; 4858fae3551SRodney W. Grimes break; 4861f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 487c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 488c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTTL || ltmp < 0) 4891f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 4901f6a4631SRuslan Ermilov optarg); 491c0a3773aSEugene 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; 517c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 518c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5190b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 520c0a3773aSEugene 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 615752d135eSMariusz Zaborski types[0] = "ADDR2NAME"; 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 /* 70649133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 70749133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 70849133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 70949133c6dSPawel Jakub Dawidek */ 710*7bdc3291SMark Johnston caph_cache_catpages(); 711*7bdc3291SMark Johnston if (caph_enter() < 0) 71249133c6dSPawel Jakub Dawidek err(1, "cap_enter"); 71349133c6dSPawel Jakub Dawidek 71449133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 715*7bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 71649133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 71749133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 718*7bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 71949133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 72049133c6dSPawel Jakub Dawidek 7218fae3551SRodney W. Grimes /* record route option */ 7228fae3551SRodney W. Grimes if (options & F_RROUTE) { 7238fae3551SRodney W. Grimes #ifdef IP_OPTIONS 724039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7258fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7268fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7278fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 728039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 72949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 73043470e3bSGarrett Wollman sizeof(rspace)) < 0) 73143470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7328fae3551SRodney W. Grimes #else 73343470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 73443470e3bSGarrett Wollman "record route not available in this implementation"); 7358fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7368fae3551SRodney W. Grimes } 7378fae3551SRodney W. Grimes 738211bfbd2SRuslan Ermilov if (options & F_TTL) { 73949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 740211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 741211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 742211bfbd2SRuslan Ermilov } 743211bfbd2SRuslan Ermilov } 74485456935SBill Fenner if (options & F_NOLOOP) { 74549133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 74685456935SBill Fenner sizeof(loop)) < 0) { 74743470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 74885456935SBill Fenner } 74985456935SBill Fenner } 75085456935SBill Fenner if (options & F_MTTL) { 75149133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 752211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 75343470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 75485456935SBill Fenner } 75585456935SBill Fenner } 75685456935SBill Fenner if (options & F_MIF) { 75749133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 75885456935SBill Fenner sizeof(ifaddr)) < 0) { 75943470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 76085456935SBill Fenner } 76185456935SBill Fenner } 762039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 763039d6aa4SBill Fenner { int on = 1; 76449133c6dSPawel Jakub Dawidek if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 765039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 766039d6aa4SBill Fenner } 767039d6aa4SBill Fenner #endif 7689ff95228SGleb Smirnoff if (sweepmax) { 769bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 770bb7dfe5eSGleb Smirnoff errx(EX_USAGE, "Maximum packet size must be no less than the minimum packet size"); 7719ff95228SGleb Smirnoff 7729ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 7739ff95228SGleb Smirnoff errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive"); 7749ff95228SGleb Smirnoff 7759ff95228SGleb Smirnoff if (npackets > 0) { 7769ff95228SGleb Smirnoff snpackets = npackets; 7779ff95228SGleb Smirnoff npackets = 0; 7789ff95228SGleb Smirnoff } else 7799ff95228SGleb Smirnoff snpackets = 1; 7809ff95228SGleb Smirnoff datalen = sweepmin; 7819ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 7829ff95228SGleb Smirnoff } 7839ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 7849ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 78585456935SBill Fenner 7868fae3551SRodney W. Grimes /* 7878fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 7888fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 7898fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 79043470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 79143470e3bSGarrett Wollman * or multicast pings if they wish. 7928fae3551SRodney W. Grimes */ 7934fba6582SMaxim Konovalov 7944fba6582SMaxim Konovalov /* 7954fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 7964fba6582SMaxim Konovalov * as well. 7974fba6582SMaxim Konovalov */ 7984fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 79949133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8008fae3551SRodney W. Grimes sizeof(hold)); 80149133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 80249133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 803*7bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 80449133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 805261e59bbSMaxim Konovalov if (uid == 0) 80649133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 807e8bd25ceSRobert Watson sizeof(hold)); 80849133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 80949133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 810*7bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 81149133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 812e8bd25ceSRobert Watson 81399490edeSWarner Losh if (to->sin_family == AF_INET) { 81499490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 81599490edeSWarner Losh inet_ntoa(to->sin_addr)); 81699490edeSWarner Losh if (source) 81799490edeSWarner Losh (void)printf(" from %s", shostname); 8189ff95228SGleb Smirnoff if (sweepmax) 8199ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8209ff95228SGleb Smirnoff sweepmin, sweepmax); 8219ff95228SGleb Smirnoff else 82299490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8239ff95228SGleb Smirnoff 8249ff95228SGleb Smirnoff } else { 8259ff95228SGleb Smirnoff if (sweepmax) 8269ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8279ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8289ff95228SGleb Smirnoff else 8298fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8309ff95228SGleb Smirnoff } 8318fae3551SRodney W. Grimes 8327d81b35cSBruce Evans /* 833a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 834a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8357d81b35cSBruce Evans */ 836a2a00888SSean Eric Fagan 837f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 83837e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 839a2a00888SSean Eric Fagan 840a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 841a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 842a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 843a2a00888SSean Eric Fagan } 844a2a00888SSean Eric Fagan 845a2a00888SSean Eric Fagan si_sa.sa_handler = status; 84637e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 847624ff938SWarner Losh err(EX_OSERR, "sigaction"); 84837e5b2c6SSean Eric Fagan } 849bf113f1bSBill Fumerola 850bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8517237fd94SBill Fumerola si_sa.sa_handler = stopit; 8527237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 8537237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 854bf113f1bSBill Fumerola } 85537e5b2c6SSean Eric Fagan 856039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 857039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 858039d6aa4SBill Fenner msg.msg_iov = &iov; 859039d6aa4SBill Fenner msg.msg_iovlen = 1; 860039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 861039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 862039d6aa4SBill Fenner #endif 863039d6aa4SBill Fenner iov.iov_base = packet; 864e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 865039d6aa4SBill Fenner 86632af342fSRuslan Ermilov if (preload == 0) 86732af342fSRuslan Ermilov pinger(); /* send the first ping */ 86832af342fSRuslan Ermilov else { 86932af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 87032af342fSRuslan Ermilov preload = npackets; 8718fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 8728fae3551SRodney W. Grimes pinger(); 87332af342fSRuslan Ermilov } 87432af342fSRuslan Ermilov (void)gettimeofday(&last, NULL); 8758fae3551SRodney W. Grimes 876039d6aa4SBill Fenner if (options & F_FLOOD) { 877039d6aa4SBill Fenner intvl.tv_sec = 0; 878039d6aa4SBill Fenner intvl.tv_usec = 10000; 879039d6aa4SBill Fenner } else { 880526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 881526f06b2SMatthew Dillon intvl.tv_usec = interval % 1000 * 1000; 882039d6aa4SBill Fenner } 883039d6aa4SBill Fenner 884261e59bbSMaxim Konovalov almost_done = 0; 8858f975bb3SBruce Evans while (!finish_up) { 886261e59bbSMaxim Konovalov struct timeval now, timeout; 887039d6aa4SBill Fenner fd_set rfds; 888261e59bbSMaxim Konovalov int cc, n; 8898fae3551SRodney W. Grimes 8907d81b35cSBruce Evans check_status(); 89149133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 8927e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 893039d6aa4SBill Fenner FD_ZERO(&rfds); 89449133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 895039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 896039d6aa4SBill Fenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 897039d6aa4SBill Fenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 898039d6aa4SBill Fenner while (timeout.tv_usec < 0) { 899039d6aa4SBill Fenner timeout.tv_usec += 1000000; 900039d6aa4SBill Fenner timeout.tv_sec--; 9018fae3551SRodney W. Grimes } 902e345a80dSPhilippe Charnier while (timeout.tv_usec >= 1000000) { 903039d6aa4SBill Fenner timeout.tv_usec -= 1000000; 904039d6aa4SBill Fenner timeout.tv_sec++; 905039d6aa4SBill Fenner } 906039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9076959b14dSXin LI timerclear(&timeout); 90849133c6dSPawel Jakub Dawidek n = select(srecv + 1, &rfds, NULL, NULL, &timeout); 9095e2cc0f4SStephen McKay if (n < 0) 9105e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 911039d6aa4SBill Fenner if (n == 1) { 91231eac03bSSean Chittenden struct timeval *tv = NULL; 913039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 914039d6aa4SBill Fenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 915039d6aa4SBill Fenner 916039d6aa4SBill Fenner msg.msg_controllen = sizeof(ctrl); 917039d6aa4SBill Fenner #endif 918039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 91949133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9208fae3551SRodney W. Grimes if (errno == EINTR) 9218fae3551SRodney W. Grimes continue; 922e345a80dSPhilippe Charnier warn("recvmsg"); 9238fae3551SRodney W. Grimes continue; 9248fae3551SRodney W. Grimes } 925039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 926039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 927039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 92831eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 929fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 930fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 93131eac03bSSean Chittenden tv = &now; 932fa05a94cSJohn Birrell } 933039d6aa4SBill Fenner #endif 93431eac03bSSean Chittenden if (tv == NULL) { 935039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 93631eac03bSSean Chittenden tv = &now; 937039d6aa4SBill Fenner } 93831eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 93931eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 94031eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9418fae3551SRodney W. Grimes break; 9428fae3551SRodney W. Grimes } 9435e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9449ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 9459ff95228SGleb Smirnoff for (i = 0; i < sweepincr ; ++i) 9469ff95228SGleb Smirnoff *datap++ = i; 9479ff95228SGleb Smirnoff datalen += sweepincr; 9489ff95228SGleb Smirnoff if (datalen > sweepmax) 9499ff95228SGleb Smirnoff break; 9509ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9519ff95228SGleb Smirnoff sntransmitted = 0; 9529ff95228SGleb Smirnoff } 953039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 954039d6aa4SBill Fenner pinger(); 955039d6aa4SBill Fenner else { 956039d6aa4SBill Fenner if (almost_done) 957039d6aa4SBill Fenner break; 958039d6aa4SBill Fenner almost_done = 1; 9595e2cc0f4SStephen McKay intvl.tv_usec = 0; 960039d6aa4SBill Fenner if (nreceived) { 961039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 962039d6aa4SBill Fenner if (!intvl.tv_sec) 963039d6aa4SBill Fenner intvl.tv_sec = 1; 964d6cd1497SGleb Smirnoff } else { 965d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 966d6cd1497SGleb Smirnoff intvl.tv_usec = waittime % 1000 * 1000; 967d6cd1497SGleb Smirnoff } 968039d6aa4SBill Fenner } 969039d6aa4SBill Fenner (void)gettimeofday(&last, NULL); 97025107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 97125107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 97225107197SIan Dowse if (options & F_MISSED) 973ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 974039d6aa4SBill Fenner } 975039d6aa4SBill Fenner } 97625107197SIan Dowse } 9778f975bb3SBruce Evans finish(); 9788fae3551SRodney W. Grimes /* NOTREACHED */ 979f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 9808fae3551SRodney W. Grimes } 9818fae3551SRodney W. Grimes 9828fae3551SRodney W. Grimes /* 9838f975bb3SBruce Evans * stopit -- 9848f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 9858f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 9868f975bb3SBruce Evans * to be called from a signal handler. 987515dd2faSJulian Elischer */ 988515dd2faSJulian Elischer void 989fafb8f11SEd Schouten stopit(int sig __unused) 990515dd2faSJulian Elischer { 991efc8588dSDavid E. O'Brien 992c8bb99e5SIan Dowse /* 993c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 994c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 995c8bb99e5SIan Dowse */ 996c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 997c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 998515dd2faSJulian Elischer finish_up = 1; 999515dd2faSJulian Elischer } 1000515dd2faSJulian Elischer 1001515dd2faSJulian Elischer /* 10028fae3551SRodney W. Grimes * pinger -- 10038fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10048fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1005eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10064fba6582SMaxim Konovalov * bytes of the data portion are used to hold a UNIX "timeval" struct in 10074fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10088fae3551SRodney W. Grimes */ 100943470e3bSGarrett Wollman static void 101043470e3bSGarrett Wollman pinger(void) 10118fae3551SRodney W. Grimes { 1012eb1543c6SMatthew N. Dodd struct timeval now; 101313e3f0b7SMaxim Konovalov struct tv32 tv32; 10140b2f8b3fSMaxim Konovalov struct ip *ip; 10153d438ad6SDavid E. O'Brien struct icmp *icp; 1016efc8588dSDavid E. O'Brien int cc, i; 10170b2f8b3fSMaxim Konovalov u_char *packet; 10188fae3551SRodney W. Grimes 10190b2f8b3fSMaxim Konovalov packet = outpack; 10208fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 1021eb1543c6SMatthew N. Dodd icp->icmp_type = icmp_type; 10228fae3551SRodney W. Grimes icp->icmp_code = 0; 10238fae3551SRodney W. Grimes icp->icmp_cksum = 0; 10245db89bc7SBill Fenner icp->icmp_seq = htons(ntransmitted); 10258fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 10268fae3551SRodney W. Grimes 10275db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10288fae3551SRodney W. Grimes 1029eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 1030eb1543c6SMatthew N. Dodd (void)gettimeofday(&now, NULL); 10318fae3551SRodney W. Grimes 103213e3f0b7SMaxim Konovalov tv32.tv32_sec = htonl(now.tv_sec); 103313e3f0b7SMaxim Konovalov tv32.tv32_usec = htonl(now.tv_usec); 1034eb1543c6SMatthew N. Dodd if (options & F_TIME) 1035eb1543c6SMatthew N. Dodd icp->icmp_otime = htonl((now.tv_sec % (24*60*60)) 1036eb1543c6SMatthew N. Dodd * 1000 + now.tv_usec / 1000); 1037eb1543c6SMatthew N. Dodd if (timing) 103813e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10390fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 104013e3f0b7SMaxim Konovalov sizeof(tv32)); 1041eb1543c6SMatthew N. Dodd } 1042eb1543c6SMatthew N. Dodd 10430fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10448fae3551SRodney W. Grimes 10458fae3551SRodney W. Grimes /* compute ICMP checksum here */ 10468fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 10478fae3551SRodney W. Grimes 10480b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 10490b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 10500b2f8b3fSMaxim Konovalov ip = (struct ip *)outpackhdr; 1051ffc610b2SAndrey V. Elsukov ip->ip_len = htons(cc); 10520b2f8b3fSMaxim Konovalov ip->ip_sum = in_cksum((u_short *)outpackhdr, cc); 10530b2f8b3fSMaxim Konovalov packet = outpackhdr; 10540b2f8b3fSMaxim Konovalov } 105549133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 10568fae3551SRodney W. Grimes if (i < 0 || i != cc) { 105743470e3bSGarrett Wollman if (i < 0) { 10588f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1059515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1060515dd2faSJulian Elischer return; 1061515dd2faSJulian Elischer } 106243470e3bSGarrett Wollman warn("sendto"); 106343470e3bSGarrett Wollman } else { 106443470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1065416aa49bSPoul-Henning Kamp hostname, i, cc); 10668fae3551SRodney W. Grimes } 1067363d7bbeSJulian Elischer } 1068363d7bbeSJulian Elischer ntransmitted++; 10699ff95228SGleb Smirnoff sntransmitted++; 10708fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 10718fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 10728fae3551SRodney W. Grimes } 10738fae3551SRodney W. Grimes 10748fae3551SRodney W. Grimes /* 10758fae3551SRodney W. Grimes * pr_pack -- 10768fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 10778fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 10788fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 10798fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 10808fae3551SRodney W. Grimes */ 108143470e3bSGarrett Wollman static void 1082fafb8f11SEd Schouten pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv) 10838fae3551SRodney W. Grimes { 10849b219646SPeter Wemm struct in_addr ina; 10859b219646SPeter Wemm u_char *cp, *dp; 10863d438ad6SDavid E. O'Brien struct icmp *icp; 10878fae3551SRodney W. Grimes struct ip *ip; 10889d2b0ab8SPeter Wemm const void *tp; 10898f975bb3SBruce Evans double triptime; 1090e88178ddSMaxim Konovalov int dupflag, hlen, i, j, recv_len, seq; 1091efc8588dSDavid E. O'Brien static int old_rrlen; 1092efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 10938fae3551SRodney W. Grimes 10948fae3551SRodney W. Grimes /* Check the IP header */ 10958fae3551SRodney W. Grimes ip = (struct ip *)buf; 10968fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 1097e88178ddSMaxim Konovalov recv_len = cc; 10988fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 10998fae3551SRodney W. Grimes if (options & F_VERBOSE) 110043470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 110143470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11028fae3551SRodney W. Grimes return; 11038fae3551SRodney W. Grimes } 11048fae3551SRodney W. Grimes 11058fae3551SRodney W. Grimes /* Now the ICMP part */ 11068fae3551SRodney W. Grimes cc -= hlen; 11078fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 1108eb1543c6SMatthew N. Dodd if (icp->icmp_type == icmp_type_rsp) { 11098fae3551SRodney W. Grimes if (icp->icmp_id != ident) 11108fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11118fae3551SRodney W. Grimes ++nreceived; 11128f975bb3SBruce Evans triptime = 0.0; 11138fae3551SRodney W. Grimes if (timing) { 1114d32ff037SJohn Birrell struct timeval tv1; 111513e3f0b7SMaxim Konovalov struct tv32 tv32; 11168fae3551SRodney W. Grimes #ifndef icmp_data 11179d2b0ab8SPeter Wemm tp = &icp->icmp_ip; 11188fae3551SRodney W. Grimes #else 11199d2b0ab8SPeter Wemm tp = icp->icmp_data; 11208fae3551SRodney W. Grimes #endif 11214eae39bfSStefan Farfeleder tp = (const char *)tp + phdr_len; 1122143008a1SMatthew N. Dodd 11237e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 11247e9489e0SHiroki Sato sizeof(tv1)) { 11259d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 112613e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 112713e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 112813e3f0b7SMaxim Konovalov tv1.tv_usec = ntohl(tv32.tv32_usec); 1129039d6aa4SBill Fenner tvsub(tv, &tv1); 1130039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 1131039d6aa4SBill Fenner ((double)tv->tv_usec) / 1000.0; 11328fae3551SRodney W. Grimes tsum += triptime; 11333109a910SGarrett Wollman tsumsq += triptime * triptime; 11348fae3551SRodney W. Grimes if (triptime < tmin) 11358fae3551SRodney W. Grimes tmin = triptime; 11368fae3551SRodney W. Grimes if (triptime > tmax) 11378fae3551SRodney W. Grimes tmax = triptime; 113847e9b3eaSMatthew N. Dodd } else 113947e9b3eaSMatthew N. Dodd timing = 0; 11408fae3551SRodney W. Grimes } 11418fae3551SRodney W. Grimes 11425db89bc7SBill Fenner seq = ntohs(icp->icmp_seq); 11435db89bc7SBill Fenner 11445db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 11458fae3551SRodney W. Grimes ++nrepeats; 11468fae3551SRodney W. Grimes --nreceived; 11478fae3551SRodney W. Grimes dupflag = 1; 11488fae3551SRodney W. Grimes } else { 11495db89bc7SBill Fenner SET(seq % mx_dup_ck); 11508fae3551SRodney W. Grimes dupflag = 0; 11518fae3551SRodney W. Grimes } 11528fae3551SRodney W. Grimes 11538fae3551SRodney W. Grimes if (options & F_QUIET) 11548fae3551SRodney W. Grimes return; 11558fae3551SRodney W. Grimes 1156d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1157d6cd1497SGleb Smirnoff ++nrcvtimeout; 1158d6cd1497SGleb Smirnoff return; 1159d6cd1497SGleb Smirnoff } 1160d6cd1497SGleb Smirnoff 11618fae3551SRodney W. Grimes if (options & F_FLOOD) 11628fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 11638fae3551SRodney W. Grimes else { 11648fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 11658fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 11665db89bc7SBill Fenner seq); 11678fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 11688fae3551SRodney W. Grimes if (timing) 1169d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 11708fae3551SRodney W. Grimes if (dupflag) 11718fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1172772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1173ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1174143008a1SMatthew N. Dodd if (options & F_MASK) { 1175143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1176143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1177f7fc5c91SMaxim Konovalov inet_ntoa(*(struct in_addr *)&(icp->icmp_mask))); 1178143008a1SMatthew N. Dodd } 1179eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1180eb1543c6SMatthew N. Dodd (void)printf(" tso=%s", pr_ntime(icp->icmp_otime)); 1181eb1543c6SMatthew N. Dodd (void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime)); 1182eb1543c6SMatthew N. Dodd (void)printf(" tst=%s", pr_ntime(icp->icmp_ttime)); 1183eb1543c6SMatthew N. Dodd } 1184e88178ddSMaxim Konovalov if (recv_len != send_len) { 1185e88178ddSMaxim Konovalov (void)printf( 1186e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1187e88178ddSMaxim Konovalov recv_len, send_len); 1188e88178ddSMaxim Konovalov } 11898fae3551SRodney W. Grimes /* check the data */ 1190eb1543c6SMatthew N. Dodd cp = (u_char*)&icp->icmp_data[phdr_len]; 11910fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 119247e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 119329dccd6aSMaxim Konovalov i = 0; 119429dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 119529dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 119629dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 119729dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 119829dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 119929dccd6aSMaxim Konovalov } 120029dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12018fae3551SRodney W. Grimes if (*cp != *dp) { 12028fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12038fae3551SRodney W. Grimes i, *dp, *cp); 12041ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 12058fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 1206d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1207e88178ddSMaxim Konovalov if ((i % 16) == 8) 1208d32ff037SJohn Birrell (void)printf("\n\t"); 1209e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1210d32ff037SJohn Birrell } 12111ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 12120fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1213d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1214e88178ddSMaxim Konovalov if ((i % 16) == 8) 12158fae3551SRodney W. Grimes (void)printf("\n\t"); 1216e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 12178fae3551SRodney W. Grimes } 12188fae3551SRodney W. Grimes break; 12198fae3551SRodney W. Grimes } 12208fae3551SRodney W. Grimes } 12218fae3551SRodney W. Grimes } 12228fae3551SRodney W. Grimes } else { 1223ef9e6dc7SBill Fenner /* 1224ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1225ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1226ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1227ef9e6dc7SBill Fenner * and ICMP type and ID. 1228f78ac61bSWarner Losh * 1229f78ac61bSWarner Losh * Only print all the error messages if we are running 1230f78ac61bSWarner Losh * as root to avoid leaking information not normally 1231f78ac61bSWarner Losh * available to those not running as root. 1232ef9e6dc7SBill Fenner */ 1233ef9e6dc7SBill Fenner #ifndef icmp_data 1234ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 1235ef9e6dc7SBill Fenner #else 1236ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 1237ef9e6dc7SBill Fenner #endif 1238ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 1239ef9e6dc7SBill Fenner 1240ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1241ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1242d389e86aSMatt Jacob (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && 1243ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 1244ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 1245ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 12468fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 124743470e3bSGarrett Wollman pr_addr(from->sin_addr)); 12488fae3551SRodney W. Grimes pr_icmph(icp); 1249ef9e6dc7SBill Fenner } else 1250ef9e6dc7SBill Fenner return; 12518fae3551SRodney W. Grimes } 12528fae3551SRodney W. Grimes 12538fae3551SRodney W. Grimes /* Display any IP options */ 12548fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 12558fae3551SRodney W. Grimes 12568fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 12578fae3551SRodney W. Grimes switch (*cp) { 12588fae3551SRodney W. Grimes case IPOPT_EOL: 12598fae3551SRodney W. Grimes hlen = 0; 12608fae3551SRodney W. Grimes break; 12618fae3551SRodney W. Grimes case IPOPT_LSRR: 1262cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1263cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1264cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1265301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 12668fae3551SRodney W. Grimes hlen -= 2; 1267301207dfSMaxim Konovalov cp += 2; 12683c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 12693c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 12708fae3551SRodney W. Grimes for (;;) { 1271301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1272301207dfSMaxim Konovalov if (ina.s_addr == 0) 12731ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1274301207dfSMaxim Konovalov else 12751ad0b1beSMaxim Konovalov (void)printf("\t%s", 12761ad0b1beSMaxim Konovalov pr_addr(ina)); 1277301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1278301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1279301207dfSMaxim Konovalov j -= INADDR_LEN; 1280301207dfSMaxim Konovalov if (j < INADDR_LEN) 12818fae3551SRodney W. Grimes break; 12828fae3551SRodney W. Grimes (void)putchar('\n'); 12838fae3551SRodney W. Grimes } 1284301207dfSMaxim Konovalov } else 1285301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 12868fae3551SRodney W. Grimes break; 12878fae3551SRodney W. Grimes case IPOPT_RR: 1288301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1289301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 12908fae3551SRodney W. Grimes hlen -= 2; 1291301207dfSMaxim Konovalov cp += 2; 12928fae3551SRodney W. Grimes if (i > j) 12938fae3551SRodney W. Grimes i = j; 1294301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1295301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1296301207dfSMaxim Konovalov old_rrlen = 0; 12978fae3551SRodney W. Grimes continue; 1298301207dfSMaxim Konovalov } 12998fae3551SRodney W. Grimes if (i == old_rrlen 13008fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 13018fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 13028fae3551SRodney W. Grimes (void)printf("\t(same route)"); 13038fae3551SRodney W. Grimes hlen -= i; 13048fae3551SRodney W. Grimes cp += i; 13058fae3551SRodney W. Grimes break; 13068fae3551SRodney W. Grimes } 13078fae3551SRodney W. Grimes old_rrlen = i; 13088fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 13098fae3551SRodney W. Grimes (void)printf("\nRR: "); 1310301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1311301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 13128fae3551SRodney W. Grimes for (;;) { 1313301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1314301207dfSMaxim Konovalov if (ina.s_addr == 0) 13151ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1316301207dfSMaxim Konovalov else 1317301207dfSMaxim Konovalov (void)printf("\t%s", 1318301207dfSMaxim Konovalov pr_addr(ina)); 1319301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1320301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1321301207dfSMaxim Konovalov i -= INADDR_LEN; 1322301207dfSMaxim Konovalov if (i < INADDR_LEN) 13238fae3551SRodney W. Grimes break; 13248fae3551SRodney W. Grimes (void)putchar('\n'); 13258fae3551SRodney W. Grimes } 1326301207dfSMaxim Konovalov } else 1327301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 13288fae3551SRodney W. Grimes break; 13298fae3551SRodney W. Grimes case IPOPT_NOP: 13308fae3551SRodney W. Grimes (void)printf("\nNOP"); 13318fae3551SRodney W. Grimes break; 13328fae3551SRodney W. Grimes default: 13338fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 13348fae3551SRodney W. Grimes break; 13358fae3551SRodney W. Grimes } 13368fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 13378fae3551SRodney W. Grimes (void)putchar('\n'); 13388fae3551SRodney W. Grimes (void)fflush(stdout); 13398fae3551SRodney W. Grimes } 13408fae3551SRodney W. Grimes } 13418fae3551SRodney W. Grimes 13428fae3551SRodney W. Grimes /* 13438fae3551SRodney W. Grimes * in_cksum -- 13448fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 13458fae3551SRodney W. Grimes */ 134643470e3bSGarrett Wollman u_short 1347fafb8f11SEd Schouten in_cksum(u_short *addr, int len) 13488fae3551SRodney W. Grimes { 1349efc8588dSDavid E. O'Brien int nleft, sum; 1350efc8588dSDavid E. O'Brien u_short *w; 13513ec12efeSPierre Beyssac union { 135209333e78SPierre Beyssac u_short us; 135309333e78SPierre Beyssac u_char uc[2]; 135409333e78SPierre Beyssac } last; 135509333e78SPierre Beyssac u_short answer; 13568fae3551SRodney W. Grimes 1357efc8588dSDavid E. O'Brien nleft = len; 1358efc8588dSDavid E. O'Brien sum = 0; 1359efc8588dSDavid E. O'Brien w = addr; 1360efc8588dSDavid E. O'Brien 13618fae3551SRodney W. Grimes /* 13628fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 13638fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 13648fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 13658fae3551SRodney W. Grimes */ 13668fae3551SRodney W. Grimes while (nleft > 1) { 13678fae3551SRodney W. Grimes sum += *w++; 13688fae3551SRodney W. Grimes nleft -= 2; 13698fae3551SRodney W. Grimes } 13708fae3551SRodney W. Grimes 13718fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 13728fae3551SRodney W. Grimes if (nleft == 1) { 137309333e78SPierre Beyssac last.uc[0] = *(u_char *)w; 137409333e78SPierre Beyssac last.uc[1] = 0; 137509333e78SPierre Beyssac sum += last.us; 13768fae3551SRodney W. Grimes } 13778fae3551SRodney W. Grimes 13788fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 13798fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 13808fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 138109333e78SPierre Beyssac answer = ~sum; /* truncate to 16 bits */ 138209333e78SPierre Beyssac return(answer); 13838fae3551SRodney W. Grimes } 13848fae3551SRodney W. Grimes 13858fae3551SRodney W. Grimes /* 13868fae3551SRodney W. Grimes * tvsub -- 13878fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 13888fae3551SRodney W. Grimes * be >= in. 13898fae3551SRodney W. Grimes */ 139043470e3bSGarrett Wollman static void 1391fafb8f11SEd Schouten tvsub(struct timeval *out, const struct timeval *in) 13928fae3551SRodney W. Grimes { 1393efc8588dSDavid E. O'Brien 13948fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 13958fae3551SRodney W. Grimes --out->tv_sec; 13968fae3551SRodney W. Grimes out->tv_usec += 1000000; 13978fae3551SRodney W. Grimes } 13988fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 13998fae3551SRodney W. Grimes } 14008fae3551SRodney W. Grimes 14018fae3551SRodney W. Grimes /* 1402badd8138SSean Eric Fagan * status -- 1403badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1404badd8138SSean Eric Fagan */ 1405badd8138SSean Eric Fagan 140643470e3bSGarrett Wollman static void 1407fafb8f11SEd Schouten status(int sig __unused) 14087d81b35cSBruce Evans { 1409efc8588dSDavid E. O'Brien 141037e5b2c6SSean Eric Fagan siginfo_p = 1; 141137e5b2c6SSean Eric Fagan } 141237e5b2c6SSean Eric Fagan 141343470e3bSGarrett Wollman static void 1414fafb8f11SEd Schouten check_status(void) 1415badd8138SSean Eric Fagan { 1416efc8588dSDavid E. O'Brien 141737e5b2c6SSean Eric Fagan if (siginfo_p) { 141837e5b2c6SSean Eric Fagan siginfo_p = 0; 1419aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 14207d81b35cSBruce Evans nreceived, ntransmitted, 1421aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1422aed98a27SMaxim Konovalov if (nreceived && timing) 1423aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1424aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1425aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 142637e5b2c6SSean Eric Fagan } 1427badd8138SSean Eric Fagan } 1428badd8138SSean Eric Fagan 1429badd8138SSean Eric Fagan /* 14308fae3551SRodney W. Grimes * finish -- 14318fae3551SRodney W. Grimes * Print out statistics, and give up. 14328fae3551SRodney W. Grimes */ 143343470e3bSGarrett Wollman static void 1434fafb8f11SEd Schouten finish(void) 14358fae3551SRodney W. Grimes { 14368fae3551SRodney W. Grimes 14378fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1438a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 14398fae3551SRodney W. Grimes (void)putchar('\n'); 14408fae3551SRodney W. Grimes (void)fflush(stdout); 14418fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 14428fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 14438fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 14448fae3551SRodney W. Grimes if (nrepeats) 14458fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1446ebe70c8fSWarner Losh if (ntransmitted) { 14478fae3551SRodney W. Grimes if (nreceived > ntransmitted) 14488fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 14498fae3551SRodney W. Grimes else 1450aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1451aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1452aa822c39SDima Dorfman ntransmitted); 1453ebe70c8fSWarner Losh } 1454d6cd1497SGleb Smirnoff if (nrcvtimeout) 1455d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 14568fae3551SRodney W. Grimes (void)putchar('\n'); 14573109a910SGarrett Wollman if (nreceived && timing) { 14583109a910SGarrett Wollman double n = nreceived + nrepeats; 14593109a910SGarrett Wollman double avg = tsum / n; 14603109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 14611ad0b1beSMaxim Konovalov (void)printf( 14621ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 14633109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 14643109a910SGarrett Wollman } 1465badd8138SSean Eric Fagan 14666e1173dcSDavid Greenman if (nreceived) 14678fae3551SRodney W. Grimes exit(0); 14686e1173dcSDavid Greenman else 14696e1173dcSDavid Greenman exit(2); 14708fae3551SRodney W. Grimes } 14718fae3551SRodney W. Grimes 14728fae3551SRodney W. Grimes #ifdef notdef 14738fae3551SRodney W. Grimes static char *ttab[] = { 14748fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 14758fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 14768fae3551SRodney W. Grimes "Source Quench", /* IP */ 14778fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 14788fae3551SRodney W. Grimes "Echo", 14798fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 14808fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 14818fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 14828fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 14838fae3551SRodney W. Grimes "Info Request", /* id + sq */ 14848fae3551SRodney W. Grimes "Info Reply" /* " */ 14858fae3551SRodney W. Grimes }; 14868fae3551SRodney W. Grimes #endif 14878fae3551SRodney W. Grimes 14888fae3551SRodney W. Grimes /* 14898fae3551SRodney W. Grimes * pr_icmph -- 14908fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 14918fae3551SRodney W. Grimes */ 149243470e3bSGarrett Wollman static void 1493fafb8f11SEd Schouten pr_icmph(struct icmp *icp) 14948fae3551SRodney W. Grimes { 1495efc8588dSDavid E. O'Brien 14968fae3551SRodney W. Grimes switch(icp->icmp_type) { 14978fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 14988fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 14998fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15008fae3551SRodney W. Grimes break; 15018fae3551SRodney W. Grimes case ICMP_UNREACH: 15028fae3551SRodney W. Grimes switch(icp->icmp_code) { 15038fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 15048fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 15058fae3551SRodney W. Grimes break; 15068fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 15078fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 15088fae3551SRodney W. Grimes break; 15098fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 15108fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 15118fae3551SRodney W. Grimes break; 15128fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 15138fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 15148fae3551SRodney W. Grimes break; 15158fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1516ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1517ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 15188fae3551SRodney W. Grimes break; 15198fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 15208fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 15218fae3551SRodney W. Grimes break; 1522ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1523ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1524ef9e6dc7SBill Fenner break; 15258fae3551SRodney W. Grimes default: 15268fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 15278fae3551SRodney W. Grimes icp->icmp_code); 15288fae3551SRodney W. Grimes break; 15298fae3551SRodney W. Grimes } 15308fae3551SRodney W. Grimes /* Print returned IP header information */ 15318fae3551SRodney W. Grimes #ifndef icmp_data 15328fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15338fae3551SRodney W. Grimes #else 15348fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15358fae3551SRodney W. Grimes #endif 15368fae3551SRodney W. Grimes break; 15378fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 15388fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 15398fae3551SRodney W. Grimes #ifndef icmp_data 15408fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15418fae3551SRodney W. Grimes #else 15428fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15438fae3551SRodney W. Grimes #endif 15448fae3551SRodney W. Grimes break; 15458fae3551SRodney W. Grimes case ICMP_REDIRECT: 15468fae3551SRodney W. Grimes switch(icp->icmp_code) { 15478fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 15488fae3551SRodney W. Grimes (void)printf("Redirect Network"); 15498fae3551SRodney W. Grimes break; 15508fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 15518fae3551SRodney W. Grimes (void)printf("Redirect Host"); 15528fae3551SRodney W. Grimes break; 15538fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15548fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15558fae3551SRodney W. Grimes break; 15568fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15578fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 15588fae3551SRodney W. Grimes break; 15598fae3551SRodney W. Grimes default: 15608fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 15618fae3551SRodney W. Grimes break; 15628fae3551SRodney W. Grimes } 1563ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 15648fae3551SRodney W. Grimes #ifndef icmp_data 15658fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15668fae3551SRodney W. Grimes #else 15678fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15688fae3551SRodney W. Grimes #endif 15698fae3551SRodney W. Grimes break; 15708fae3551SRodney W. Grimes case ICMP_ECHO: 15718fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 15728fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15738fae3551SRodney W. Grimes break; 15748fae3551SRodney W. Grimes case ICMP_TIMXCEED: 15758fae3551SRodney W. Grimes switch(icp->icmp_code) { 15768fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 15778fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 15788fae3551SRodney W. Grimes break; 15798fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 15808fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 15818fae3551SRodney W. Grimes break; 15828fae3551SRodney W. Grimes default: 15838fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 15848fae3551SRodney W. Grimes icp->icmp_code); 15858fae3551SRodney W. Grimes break; 15868fae3551SRodney W. Grimes } 15878fae3551SRodney W. Grimes #ifndef icmp_data 15888fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15898fae3551SRodney W. Grimes #else 15908fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15918fae3551SRodney W. Grimes #endif 15928fae3551SRodney W. Grimes break; 15938fae3551SRodney W. Grimes case ICMP_PARAMPROB: 15948fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 15958fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 15968fae3551SRodney W. Grimes #ifndef icmp_data 15978fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15988fae3551SRodney W. Grimes #else 15998fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 16008fae3551SRodney W. Grimes #endif 16018fae3551SRodney W. Grimes break; 16028fae3551SRodney W. Grimes case ICMP_TSTAMP: 16038fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 16048fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16058fae3551SRodney W. Grimes break; 16068fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 16078fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 16088fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16098fae3551SRodney W. Grimes break; 16108fae3551SRodney W. Grimes case ICMP_IREQ: 16118fae3551SRodney W. Grimes (void)printf("Information Request\n"); 16128fae3551SRodney W. Grimes /* XXX ID + Seq */ 16138fae3551SRodney W. Grimes break; 16148fae3551SRodney W. Grimes case ICMP_IREQREPLY: 16158fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 16168fae3551SRodney W. Grimes /* XXX ID + Seq */ 16178fae3551SRodney W. Grimes break; 16188fae3551SRodney W. Grimes case ICMP_MASKREQ: 16198fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 16208fae3551SRodney W. Grimes break; 16218fae3551SRodney W. Grimes case ICMP_MASKREPLY: 16228fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 16238fae3551SRodney W. Grimes break; 1624ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1625ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1626ef9e6dc7SBill Fenner break; 1627ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1628ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1629ef9e6dc7SBill Fenner break; 16308fae3551SRodney W. Grimes default: 16318fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 16328fae3551SRodney W. Grimes } 16338fae3551SRodney W. Grimes } 16348fae3551SRodney W. Grimes 16358fae3551SRodney W. Grimes /* 16368fae3551SRodney W. Grimes * pr_iph -- 16378fae3551SRodney W. Grimes * Print an IP header with options. 16388fae3551SRodney W. Grimes */ 163943470e3bSGarrett Wollman static void 1640fafb8f11SEd Schouten pr_iph(struct ip *ip) 16418fae3551SRodney W. Grimes { 1642a94c074dSDimitry Andric struct in_addr ina; 16438fae3551SRodney W. Grimes u_char *cp; 1644efc8588dSDavid E. O'Brien int hlen; 16458fae3551SRodney W. Grimes 16468fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 16478fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 16488fae3551SRodney W. Grimes 1649ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 16508fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1651ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1652ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1653d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1654d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1655d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1656ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1657ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1658a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1659a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1660a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1661a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1662ef9e6dc7SBill Fenner /* dump any option bytes */ 16638fae3551SRodney W. Grimes while (hlen-- > 20) { 16648fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16658fae3551SRodney W. Grimes } 16668fae3551SRodney W. Grimes (void)putchar('\n'); 16678fae3551SRodney W. Grimes } 16688fae3551SRodney W. Grimes 16698fae3551SRodney W. Grimes /* 16708fae3551SRodney W. Grimes * pr_addr -- 16718fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 16728fae3551SRodney W. Grimes * a hostname. 16738fae3551SRodney W. Grimes */ 167443470e3bSGarrett Wollman static char * 1675fafb8f11SEd Schouten pr_addr(struct in_addr ina) 16768fae3551SRodney W. Grimes { 16778fae3551SRodney W. Grimes struct hostent *hp; 1678f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 16798fae3551SRodney W. Grimes 168049133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 168143470e3bSGarrett Wollman return inet_ntoa(ina); 168249133c6dSPawel Jakub Dawidek 168349133c6dSPawel Jakub Dawidek hp = cap_gethostbyaddr(capdns, (char *)&ina, 4, AF_INET); 168449133c6dSPawel Jakub Dawidek 168549133c6dSPawel Jakub Dawidek if (hp == NULL) 168649133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 168749133c6dSPawel Jakub Dawidek 1688efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 168943470e3bSGarrett Wollman inet_ntoa(ina)); 16908fae3551SRodney W. Grimes return(buf); 16918fae3551SRodney W. Grimes } 16928fae3551SRodney W. Grimes 16938fae3551SRodney W. Grimes /* 16948fae3551SRodney W. Grimes * pr_retip -- 16958fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 16968fae3551SRodney W. Grimes */ 169743470e3bSGarrett Wollman static void 1698fafb8f11SEd Schouten pr_retip(struct ip *ip) 16998fae3551SRodney W. Grimes { 17008fae3551SRodney W. Grimes u_char *cp; 1701efc8588dSDavid E. O'Brien int hlen; 17028fae3551SRodney W. Grimes 17038fae3551SRodney W. Grimes pr_iph(ip); 17048fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 17058fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 17068fae3551SRodney W. Grimes 17078fae3551SRodney W. Grimes if (ip->ip_p == 6) 17088fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 17098fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17108fae3551SRodney W. Grimes else if (ip->ip_p == 17) 17118fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 17128fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17138fae3551SRodney W. Grimes } 17148fae3551SRodney W. Grimes 1715eb1543c6SMatthew N. Dodd static char * 1716007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1717eb1543c6SMatthew N. Dodd { 1718eb1543c6SMatthew N. Dodd static char buf[10]; 1719007fe4e3SMaxim Konovalov int hour, min, sec; 1720eb1543c6SMatthew N. Dodd 1721007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1722007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1723007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1724007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1725eb1543c6SMatthew N. Dodd 1726007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1727eb1543c6SMatthew N. Dodd 1728eb1543c6SMatthew N. Dodd return (buf); 1729eb1543c6SMatthew N. Dodd } 1730eb1543c6SMatthew N. Dodd 173143470e3bSGarrett Wollman static void 1732fafb8f11SEd Schouten fill(char *bp, char *patp) 17338fae3551SRodney W. Grimes { 17348fae3551SRodney W. Grimes char *cp; 1735efc8588dSDavid E. O'Brien int pat[16]; 17364fba6582SMaxim Konovalov u_int ii, jj, kk; 17378fae3551SRodney W. Grimes 173843470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 173943470e3bSGarrett Wollman if (!isxdigit(*cp)) 174043470e3bSGarrett Wollman errx(EX_USAGE, 174143470e3bSGarrett Wollman "patterns must be specified as hex digits"); 174243470e3bSGarrett Wollman 17438fae3551SRodney W. Grimes } 17448fae3551SRodney W. Grimes ii = sscanf(patp, 17458fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 17468fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 17478fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 17488fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 17498fae3551SRodney W. Grimes 17508fae3551SRodney W. Grimes if (ii > 0) 1751d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 17528fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17538fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17548fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17558fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17568fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17578fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17588fae3551SRodney W. Grimes (void)printf("\n"); 17598fae3551SRodney W. Grimes } 17608fae3551SRodney W. Grimes } 17618fae3551SRodney W. Grimes 176249133c6dSPawel Jakub Dawidek static cap_channel_t * 176349133c6dSPawel Jakub Dawidek capdns_setup(void) 176449133c6dSPawel Jakub Dawidek { 176549133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 176649133c6dSPawel Jakub Dawidek const char *types[2]; 176749133c6dSPawel Jakub Dawidek int families[1]; 176849133c6dSPawel Jakub Dawidek 176949133c6dSPawel Jakub Dawidek capcas = cap_init(); 1770c501d73cSMariusz Zaborski if (capcas == NULL) 1771c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 177249133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 177349133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 177449133c6dSPawel Jakub Dawidek cap_close(capcas); 177549133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 177649133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 1777752d135eSMariusz Zaborski types[0] = "NAME2ADDR"; 1778752d135eSMariusz Zaborski types[1] = "ADDR2NAME"; 177949133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 178049133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 178149133c6dSPawel Jakub Dawidek families[0] = AF_INET; 178249133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 178349133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 178449133c6dSPawel Jakub Dawidek 178549133c6dSPawel Jakub Dawidek return (capdnsloc); 178649133c6dSPawel Jakub Dawidek } 178749133c6dSPawel Jakub Dawidek 1788120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 1789120b4a93SRuslan Ermilov #define SECOPT " [-P policy]" 1790120b4a93SRuslan Ermilov #else 1791120b4a93SRuslan Ermilov #define SECOPT "" 1792120b4a93SRuslan Ermilov #endif 179343470e3bSGarrett Wollman static void 1794fafb8f11SEd Schouten usage(void) 17958fae3551SRodney W. Grimes { 179631eac03bSSean Chittenden 1797d6cd1497SGleb Smirnoff (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 1798ee3e1c4cSRuslan Ermilov "usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]", 1799ee3e1c4cSRuslan Ermilov " [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]", 1800ee3e1c4cSRuslan Ermilov " " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]", 1801d6cd1497SGleb Smirnoff " [-W waittime] [-z tos] host", 18021bd10ba2SRuslan Ermilov " ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]", 1803120b4a93SRuslan Ermilov " [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]", 1804d6cd1497SGleb Smirnoff " [-s packetsize] [-T ttl] [-t timeout] [-W waittime]", 1805d6cd1497SGleb Smirnoff " [-z tos] mcast-group"); 180643470e3bSGarrett Wollman exit(EX_USAGE); 18078fae3551SRodney W. Grimes } 1808