1*8a16b7a1SPedro F. Giffuni /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro F. Giffuni * 48fae3551SRodney W. Grimes * Copyright (c) 1989, 1993 58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 68fae3551SRodney W. Grimes * 78fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 88fae3551SRodney W. Grimes * Mike Muuss. 98fae3551SRodney W. Grimes * 108fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 118fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 128fae3551SRodney W. Grimes * are met: 138fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 158fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 168fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 178fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 198fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 208fae3551SRodney W. Grimes * without specific prior written permission. 218fae3551SRodney W. Grimes * 228fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 238fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 248fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 258fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 268fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 278fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 288fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 298fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 308fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 318fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 328fae3551SRodney W. Grimes * SUCH DAMAGE. 338fae3551SRodney W. Grimes */ 348fae3551SRodney W. Grimes 35c69284caSDavid E. O'Brien #if 0 368fae3551SRodney W. Grimes #ifndef lint 3743470e3bSGarrett Wollman static const char copyright[] = 388fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 398fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 408fae3551SRodney W. Grimes #endif /* not lint */ 418fae3551SRodney W. Grimes 428fae3551SRodney W. Grimes #ifndef lint 438fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 448fae3551SRodney W. Grimes #endif /* not lint */ 45c69284caSDavid E. O'Brien #endif 46c69284caSDavid E. O'Brien #include <sys/cdefs.h> 47c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes /* 508fae3551SRodney W. Grimes * P I N G . C 518fae3551SRodney W. Grimes * 52e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 538fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 548fae3551SRodney W. Grimes * 558fae3551SRodney W. Grimes * Author - 568fae3551SRodney W. Grimes * Mike Muuss 578fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 588fae3551SRodney W. Grimes * December, 1983 598fae3551SRodney W. Grimes * 608fae3551SRodney W. Grimes * Status - 618fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 628fae3551SRodney W. Grimes * Bugs - 638fae3551SRodney W. Grimes * More statistics could always be gathered. 648fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 658fae3551SRodney W. Grimes */ 668fae3551SRodney W. Grimes 6743470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 68b881b8beSRobert Watson #include <sys/capsicum.h> 698fae3551SRodney W. Grimes #include <sys/socket.h> 70261e59bbSMaxim Konovalov #include <sys/sysctl.h> 718fae3551SRodney W. Grimes #include <sys/time.h> 72039d6aa4SBill Fenner #include <sys/uio.h> 738fae3551SRodney W. Grimes 748fae3551SRodney W. Grimes #include <netinet/in.h> 7543470e3bSGarrett Wollman #include <netinet/in_systm.h> 768fae3551SRodney W. Grimes #include <netinet/ip.h> 778fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 788fae3551SRodney W. Grimes #include <netinet/ip_var.h> 7943470e3bSGarrett Wollman #include <arpa/inet.h> 80c501d73cSMariusz Zaborski 81c501d73cSMariusz Zaborski #include <libcasper.h> 82c501d73cSMariusz Zaborski #include <casper/cap_dns.h> 838fae3551SRodney W. Grimes 849a4365d0SYoshinobu Inoue #ifdef IPSEC 858409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h> 869a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 879a4365d0SYoshinobu Inoue 88261e59bbSMaxim Konovalov #include <ctype.h> 89261e59bbSMaxim Konovalov #include <err.h> 90261e59bbSMaxim Konovalov #include <errno.h> 91261e59bbSMaxim Konovalov #include <math.h> 92261e59bbSMaxim Konovalov #include <netdb.h> 93261e59bbSMaxim Konovalov #include <signal.h> 94261e59bbSMaxim Konovalov #include <stdio.h> 95261e59bbSMaxim Konovalov #include <stdlib.h> 96261e59bbSMaxim Konovalov #include <string.h> 97261e59bbSMaxim Konovalov #include <sysexits.h> 98261e59bbSMaxim Konovalov #include <unistd.h> 99261e59bbSMaxim Konovalov 100301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10113e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 102eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 103eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 104c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1058f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1068f975bb3SBruce Evans /* runs out of buffer space */ 1074fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1084fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 109d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 110bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1110b2f8b3fSMaxim Konovalov #define MAXTOS 255 1128fae3551SRodney W. Grimes 1138fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1148fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1158fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1168fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1178fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1188fae3551SRodney W. Grimes 11913e3f0b7SMaxim Konovalov struct tv32 { 12013e3f0b7SMaxim Konovalov int32_t tv32_sec; 12113e3f0b7SMaxim Konovalov int32_t tv32_usec; 12213e3f0b7SMaxim Konovalov }; 12313e3f0b7SMaxim Konovalov 1248fae3551SRodney W. Grimes /* various options */ 1257e9489e0SHiroki Sato static int options; 12685456935SBill Fenner #define F_FLOOD 0x0001 12785456935SBill Fenner #define F_INTERVAL 0x0002 12885456935SBill Fenner #define F_NUMERIC 0x0004 12985456935SBill Fenner #define F_PINGFILLED 0x0008 13085456935SBill Fenner #define F_QUIET 0x0010 13185456935SBill Fenner #define F_RROUTE 0x0020 13285456935SBill Fenner #define F_SO_DEBUG 0x0040 13385456935SBill Fenner #define F_SO_DONTROUTE 0x0080 13485456935SBill Fenner #define F_VERBOSE 0x0100 13585456935SBill Fenner #define F_QUIET2 0x0200 13685456935SBill Fenner #define F_NOLOOP 0x0400 13785456935SBill Fenner #define F_MTTL 0x0800 13885456935SBill Fenner #define F_MIF 0x1000 139772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1409a4365d0SYoshinobu Inoue #ifdef IPSEC 1419a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1429a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1439a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1449a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 145211bfbd2SRuslan Ermilov #define F_TTL 0x8000 146ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1478025c44bSDima Dorfman #define F_ONCE 0x20000 1480b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 149143008a1SMatthew N. Dodd #define F_MASK 0x80000 150eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1519ff95228SGleb Smirnoff #define F_SWEEP 0x200000 152d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 1538fae3551SRodney W. Grimes 1548fae3551SRodney W. Grimes /* 1558fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1568fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1578fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1588fae3551SRodney W. Grimes */ 1598fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1607e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1617e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1628fae3551SRodney W. Grimes 1637e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1647e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1657e9489e0SHiroki Sato static int maxpayload; 1667e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1677e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1687e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1697e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1707e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 1717e9489e0SHiroki Sato static char DOT = '.'; 1727e9489e0SHiroki Sato static char *hostname; 1737e9489e0SHiroki Sato static char *shostname; 1747e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1757e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1767e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1777e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1787e9489e0SHiroki Sato static int phdr_len = 0; 1797e9489e0SHiroki Sato static int send_len; 1808fae3551SRodney W. Grimes 1818fae3551SRodney W. Grimes /* counters */ 1827e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1837e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1847e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1857e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1867e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1877e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1887e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 1897e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 1907e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 1917e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 1927e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 1937e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 1947e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 1958fae3551SRodney W. Grimes 1968fae3551SRodney W. Grimes /* timing */ 1977e9489e0SHiroki Sato static int timing; /* flag to do timing */ 1987e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 1997e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2007e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2017e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2028fae3551SRodney W. Grimes 2037e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2047e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2057e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 206badd8138SSean Eric Fagan 20749133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 20849133c6dSPawel Jakub Dawidek 20943470e3bSGarrett Wollman static void fill(char *, char *); 21043470e3bSGarrett Wollman static u_short in_cksum(u_short *, int); 21149133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 21243470e3bSGarrett Wollman static void check_status(void); 2138f975bb3SBruce Evans static void finish(void) __dead2; 21443470e3bSGarrett Wollman static void pinger(void); 21543470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 216eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 21743470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 21843470e3bSGarrett Wollman static void pr_iph(struct ip *); 219039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); 22043470e3bSGarrett Wollman static void pr_retip(struct ip *); 22143470e3bSGarrett Wollman static void status(int); 2228f975bb3SBruce Evans static void stopit(int); 223fafb8f11SEd Schouten static void tvsub(struct timeval *, const struct timeval *); 224e345a80dSPhilippe Charnier static void usage(void) __dead2; 2258fae3551SRodney W. Grimes 22643470e3bSGarrett Wollman int 227fafb8f11SEd Schouten main(int argc, char *const *argv) 2288fae3551SRodney W. Grimes { 22931eac03bSSean Chittenden struct sockaddr_in from, sock_in; 230efc8588dSDavid E. O'Brien struct in_addr ifaddr; 231261e59bbSMaxim Konovalov struct timeval last, intvl; 232efc8588dSDavid E. O'Brien struct iovec iov; 2330b2f8b3fSMaxim Konovalov struct ip *ip; 234efc8588dSDavid E. O'Brien struct msghdr msg; 235efc8588dSDavid E. O'Brien struct sigaction si_sa; 2360b2f8b3fSMaxim Konovalov size_t sz; 237e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 238d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 239261e59bbSMaxim Konovalov struct hostent *hp; 240efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 241efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 242efc8588dSDavid E. O'Brien #endif 243261e59bbSMaxim Konovalov struct sockaddr_in *to; 244261e59bbSMaxim Konovalov double t; 245efc8588dSDavid E. O'Brien u_long alarmtimeout, ultmp; 24649133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 24749133c6dSPawel Jakub Dawidek int ssend_errno, srecv_errno, tos, ttl; 248efc8588dSDavid E. O'Brien char ctrl[CMSG_SPACE(sizeof(struct timeval))]; 249efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2508fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2514fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2528fae3551SRodney W. Grimes #endif 253261e59bbSMaxim Konovalov unsigned char loop, mttl; 254efc8588dSDavid E. O'Brien 25531eac03bSSean Chittenden payload = source = NULL; 2569a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 257efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2589a4365d0SYoshinobu Inoue #endif 25949133c6dSPawel Jakub Dawidek cap_rights_t rights; 26049133c6dSPawel Jakub Dawidek bool cansandbox; 2618fae3551SRodney W. Grimes 262f1284d7aSBill Fenner /* 263f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 264f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 265f1284d7aSBill Fenner * after arg parsing. 26649133c6dSPawel Jakub Dawidek * 26749133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 26849133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 26949133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 27049133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 27149133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 27249133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 27349133c6dSPawel Jakub Dawidek * packets. 274f1284d7aSBill Fenner */ 27549133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 27649133c6dSPawel Jakub Dawidek ssend_errno = errno; 27749133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 27849133c6dSPawel Jakub Dawidek srecv_errno = errno; 279f1284d7aSBill Fenner 2801d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2811d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 282ee2bf734SWarner Losh uid = getuid(); 283f1284d7aSBill Fenner 284eeb63943SDon Lewis if (ssend < 0) { 285eeb63943SDon Lewis errno = ssend_errno; 286eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 287eeb63943SDon Lewis } 288eeb63943SDon Lewis 289eeb63943SDon Lewis if (srecv < 0) { 290eeb63943SDon Lewis errno = srecv_errno; 291eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 292eeb63943SDon Lewis } 293eeb63943SDon Lewis 2940b2f8b3fSMaxim Konovalov alarmtimeout = df = preload = tos = 0; 295badd8138SSean Eric Fagan 2960b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 297211bfbd2SRuslan Ermilov while ((ch = getopt(argc, argv, 298d6cd1497SGleb Smirnoff "Aac:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:" 299211bfbd2SRuslan Ermilov #ifdef IPSEC 3009a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 301211bfbd2SRuslan Ermilov "P:" 3029a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 303211bfbd2SRuslan Ermilov #endif /*IPSEC*/ 304211bfbd2SRuslan Ermilov )) != -1) 3059a4365d0SYoshinobu Inoue { 3068fae3551SRodney W. Grimes switch(ch) { 307ca517ad8SPoul-Henning Kamp case 'A': 308ca517ad8SPoul-Henning Kamp options |= F_MISSED; 309ca517ad8SPoul-Henning Kamp break; 310772dfa72SDaniel O'Callaghan case 'a': 311772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 312772dfa72SDaniel O'Callaghan break; 3138fae3551SRodney W. Grimes case 'c': 31443470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 31543470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) 31643470e3bSGarrett Wollman errx(EX_USAGE, 31743470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 31843470e3bSGarrett Wollman optarg); 31943470e3bSGarrett Wollman npackets = ultmp; 3208fae3551SRodney W. Grimes break; 3210b2f8b3fSMaxim Konovalov case 'D': 3220b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3230b2f8b3fSMaxim Konovalov df = 1; 3240b2f8b3fSMaxim Konovalov break; 3258fae3551SRodney W. Grimes case 'd': 3268fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3278fae3551SRodney W. Grimes break; 3288fae3551SRodney W. Grimes case 'f': 329526f06b2SMatthew Dillon if (uid) { 33043470e3bSGarrett Wollman errno = EPERM; 33143470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3328fae3551SRodney W. Grimes } 3338fae3551SRodney W. Grimes options |= F_FLOOD; 3348fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3358fae3551SRodney W. Grimes break; 3369ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 3379ff95228SGleb Smirnoff ultmp = strtoul(optarg, &ep, 0); 3389ff95228SGleb Smirnoff if (*ep || ep == optarg) 3399ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3409ff95228SGleb Smirnoff optarg); 3419ff95228SGleb Smirnoff if (uid != 0 && ultmp > DEFDATALEN) { 3429ff95228SGleb Smirnoff errno = EPERM; 3439ff95228SGleb Smirnoff err(EX_NOPERM, 3449ff95228SGleb Smirnoff "packet size too large: %lu > %u", 3459ff95228SGleb Smirnoff ultmp, DEFDATALEN); 3469ff95228SGleb Smirnoff } 3479ff95228SGleb Smirnoff options |= F_SWEEP; 3489ff95228SGleb Smirnoff sweepmax = ultmp; 3499ff95228SGleb Smirnoff break; 3509ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 3519ff95228SGleb Smirnoff ultmp = strtoul(optarg, &ep, 0); 3529ff95228SGleb Smirnoff if (*ep || ep == optarg) 3539ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3549ff95228SGleb Smirnoff optarg); 3559ff95228SGleb Smirnoff if (uid != 0 && ultmp > DEFDATALEN) { 3569ff95228SGleb Smirnoff errno = EPERM; 3579ff95228SGleb Smirnoff err(EX_NOPERM, 3589ff95228SGleb Smirnoff "packet size too large: %lu > %u", 3599ff95228SGleb Smirnoff ultmp, DEFDATALEN); 3609ff95228SGleb Smirnoff } 3619ff95228SGleb Smirnoff options |= F_SWEEP; 3629ff95228SGleb Smirnoff sweepmin = ultmp; 3639ff95228SGleb Smirnoff break; 3649ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 3659ff95228SGleb Smirnoff ultmp = strtoul(optarg, &ep, 0); 3669ff95228SGleb Smirnoff if (*ep || ep == optarg || ultmp < 1) 3679ff95228SGleb Smirnoff errx(EX_USAGE, "invalid increment size: `%s'", 3689ff95228SGleb Smirnoff optarg); 3699ff95228SGleb Smirnoff if (uid != 0 && ultmp > DEFDATALEN) { 3709ff95228SGleb Smirnoff errno = EPERM; 3719ff95228SGleb Smirnoff err(EX_NOPERM, 3729ff95228SGleb Smirnoff "packet size too large: %lu > %u", 3739ff95228SGleb Smirnoff ultmp, DEFDATALEN); 3749ff95228SGleb Smirnoff } 3759ff95228SGleb Smirnoff options |= F_SWEEP; 3769ff95228SGleb Smirnoff sweepincr = ultmp; 3779ff95228SGleb Smirnoff break; 3781f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 3791f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 3801f6a4631SRuslan Ermilov errx(EX_USAGE, 3811f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 3821f6a4631SRuslan Ermilov optarg); 3831f6a4631SRuslan Ermilov options |= F_MIF; 3841f6a4631SRuslan Ermilov break; 3858fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 3861ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 3871ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 3881ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 3891ad0b1beSMaxim Konovalov optarg); 3908fae3551SRodney W. Grimes options |= F_INTERVAL; 391526f06b2SMatthew Dillon interval = (int)t; 392526f06b2SMatthew Dillon if (uid && interval < 1000) { 393526f06b2SMatthew Dillon errno = EPERM; 394526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 395526f06b2SMatthew Dillon } 3968fae3551SRodney W. Grimes break; 3971f6a4631SRuslan Ermilov case 'L': 3981f6a4631SRuslan Ermilov options |= F_NOLOOP; 3991f6a4631SRuslan Ermilov loop = 0; 40085456935SBill Fenner break; 4018fae3551SRodney W. Grimes case 'l': 40243470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 40343470e3bSGarrett Wollman if (*ep || ep == optarg || ultmp > INT_MAX) 40443470e3bSGarrett Wollman errx(EX_USAGE, 40543470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4065e2cc0f4SStephen McKay if (uid) { 407f78ac61bSWarner Losh errno = EPERM; 408f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 409f78ac61bSWarner Losh } 41043470e3bSGarrett Wollman preload = ultmp; 4118fae3551SRodney W. Grimes break; 4121f6a4631SRuslan Ermilov case 'M': 413eb1543c6SMatthew N. Dodd switch(optarg[0]) { 414eb1543c6SMatthew N. Dodd case 'M': 415eb1543c6SMatthew N. Dodd case 'm': 4161f6a4631SRuslan Ermilov options |= F_MASK; 41785456935SBill Fenner break; 418eb1543c6SMatthew N. Dodd case 'T': 419eb1543c6SMatthew N. Dodd case 't': 420eb1543c6SMatthew N. Dodd options |= F_TIME; 421eb1543c6SMatthew N. Dodd break; 422eb1543c6SMatthew N. Dodd default: 423eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 424eb1543c6SMatthew N. Dodd break; 425eb1543c6SMatthew N. Dodd } 426eb1543c6SMatthew N. Dodd break; 427211bfbd2SRuslan Ermilov case 'm': /* TTL */ 428211bfbd2SRuslan Ermilov ultmp = strtoul(optarg, &ep, 0); 4299bc1a9ecSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTTL) 4301ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 431211bfbd2SRuslan Ermilov ttl = ultmp; 432211bfbd2SRuslan Ermilov options |= F_TTL; 433211bfbd2SRuslan Ermilov break; 4348fae3551SRodney W. Grimes case 'n': 4358fae3551SRodney W. Grimes options |= F_NUMERIC; 4368fae3551SRodney W. Grimes break; 4378025c44bSDima Dorfman case 'o': 4388025c44bSDima Dorfman options |= F_ONCE; 4398025c44bSDima Dorfman break; 4401f6a4631SRuslan Ermilov #ifdef IPSEC 4411f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4421f6a4631SRuslan Ermilov case 'P': 4431f6a4631SRuslan Ermilov options |= F_POLICY; 4441f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4451f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4461f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4471f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4481f6a4631SRuslan Ermilov else 4491f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4501f6a4631SRuslan Ermilov break; 4511f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4521f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4538fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4548fae3551SRodney W. Grimes options |= F_PINGFILLED; 455d074d39fSMatthew N. Dodd payload = optarg; 4568fae3551SRodney W. Grimes break; 457ef9e6dc7SBill Fenner case 'Q': 458ef9e6dc7SBill Fenner options |= F_QUIET2; 459ef9e6dc7SBill Fenner break; 4608fae3551SRodney W. Grimes case 'q': 4618fae3551SRodney W. Grimes options |= F_QUIET; 4628fae3551SRodney W. Grimes break; 4638fae3551SRodney W. Grimes case 'R': 4648fae3551SRodney W. Grimes options |= F_RROUTE; 4658fae3551SRodney W. Grimes break; 4668fae3551SRodney W. Grimes case 'r': 4678fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4688fae3551SRodney W. Grimes break; 4691f6a4631SRuslan Ermilov case 'S': 4701f6a4631SRuslan Ermilov source = optarg; 4711f6a4631SRuslan Ermilov break; 4728fae3551SRodney W. Grimes case 's': /* size of packet to send */ 47343470e3bSGarrett Wollman ultmp = strtoul(optarg, &ep, 0); 4744fba6582SMaxim Konovalov if (*ep || ep == optarg) 47543470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 47643470e3bSGarrett Wollman optarg); 477fb7d32c7SMaxim Konovalov if (uid != 0 && ultmp > DEFDATALEN) { 478fb7d32c7SMaxim Konovalov errno = EPERM; 479fb7d32c7SMaxim Konovalov err(EX_NOPERM, 480fb7d32c7SMaxim Konovalov "packet size too large: %lu > %u", 481fb7d32c7SMaxim Konovalov ultmp, DEFDATALEN); 482fb7d32c7SMaxim Konovalov } 48343470e3bSGarrett Wollman datalen = ultmp; 4848fae3551SRodney W. Grimes break; 4851f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 4861f6a4631SRuslan Ermilov ultmp = strtoul(optarg, &ep, 0); 4871f6a4631SRuslan Ermilov if (*ep || ep == optarg || ultmp > MAXTTL) 4881f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 4891f6a4631SRuslan Ermilov optarg); 4901f6a4631SRuslan Ermilov mttl = ultmp; 4911f6a4631SRuslan Ermilov options |= F_MTTL; 49299490edeSWarner Losh break; 4937237fd94SBill Fumerola case 't': 494bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 495bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 4967237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 4977237fd94SBill Fumerola optarg); 498bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 499bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 500bf113f1bSBill Fumerola optarg, MAXALARM); 501bf113f1bSBill Fumerola alarm((int)alarmtimeout); 5027237fd94SBill Fumerola break; 5038fae3551SRodney W. Grimes case 'v': 5048fae3551SRodney W. Grimes options |= F_VERBOSE; 5058fae3551SRodney W. Grimes break; 506d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 507d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 508d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 509d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 510d6cd1497SGleb Smirnoff optarg); 511d6cd1497SGleb Smirnoff options |= F_WAITTIME; 512d6cd1497SGleb Smirnoff waittime = (int)t; 513d6cd1497SGleb Smirnoff break; 5140b2f8b3fSMaxim Konovalov case 'z': 5150b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 5160b2f8b3fSMaxim Konovalov ultmp = strtoul(optarg, &ep, 0); 5170b2f8b3fSMaxim Konovalov if (*ep || ep == optarg || ultmp > MAXTOS) 5180b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 5190b2f8b3fSMaxim Konovalov tos = ultmp; 5200b2f8b3fSMaxim Konovalov break; 5218fae3551SRodney W. Grimes default: 522e345a80dSPhilippe Charnier usage(); 52343470e3bSGarrett Wollman } 52443470e3bSGarrett Wollman } 52543470e3bSGarrett Wollman 52643470e3bSGarrett Wollman if (argc - optind != 1) 527e345a80dSPhilippe Charnier usage(); 52843470e3bSGarrett Wollman target = argv[optind]; 5298fae3551SRodney W. Grimes 530d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 531d829c3dfSMatthew N. Dodd case 0: break; 532d829c3dfSMatthew N. Dodd case F_MASK: 533eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 534eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 535d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 536eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 537eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 538d829c3dfSMatthew N. Dodd break; 539d829c3dfSMatthew N. Dodd case F_TIME: 540eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 541eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 542d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 543eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 544eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 545d829c3dfSMatthew N. Dodd break; 546d829c3dfSMatthew N. Dodd default: 547d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 548d829c3dfSMatthew N. Dodd break; 549eb1543c6SMatthew N. Dodd } 5500fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 551fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 552e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 553e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 554fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5551104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 556fb7d32c7SMaxim Konovalov maxpayload); 557e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5580fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 559d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 560d074d39fSMatthew N. Dodd fill((char *)datap, payload); 561d074d39fSMatthew N. Dodd } 56249133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 56399490edeSWarner Losh if (source) { 56431eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 56531eac03bSSean Chittenden sock_in.sin_family = AF_INET; 56631eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 56799490edeSWarner Losh shostname = source; 56899490edeSWarner Losh } else { 56949133c6dSPawel Jakub Dawidek if (capdns != NULL) 57049133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, source, 57149133c6dSPawel Jakub Dawidek AF_INET); 57249133c6dSPawel Jakub Dawidek else 57399490edeSWarner Losh hp = gethostbyname2(source, AF_INET); 57499490edeSWarner Losh if (!hp) 57599490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 57699490edeSWarner Losh source, hstrerror(h_errno)); 57799490edeSWarner Losh 57831eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 57931eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 5804fba6582SMaxim Konovalov hp->h_length < 0) 58199490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 58231eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 58331eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 58499490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 58599490edeSWarner Losh sizeof(snamebuf) - 1); 58699490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 58799490edeSWarner Losh shostname = snamebuf; 58899490edeSWarner Losh } 58949133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 59049133c6dSPawel Jakub Dawidek -1) 59199490edeSWarner Losh err(1, "bind"); 59299490edeSWarner Losh } 59399490edeSWarner Losh 594d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 595d389e86aSMatt Jacob to = &whereto; 5968fae3551SRodney W. Grimes to->sin_family = AF_INET; 597d389e86aSMatt Jacob to->sin_len = sizeof *to; 59843470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 5998fae3551SRodney W. Grimes hostname = target; 60043470e3bSGarrett Wollman } else { 60149133c6dSPawel Jakub Dawidek if (capdns != NULL) 60249133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 60349133c6dSPawel Jakub Dawidek else 60443470e3bSGarrett Wollman hp = gethostbyname2(target, AF_INET); 60543470e3bSGarrett Wollman if (!hp) 60643470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 60743470e3bSGarrett Wollman target, hstrerror(h_errno)); 60843470e3bSGarrett Wollman 60931eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6101ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 61143470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6128fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 613b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6148fae3551SRodney W. Grimes hostname = hnamebuf; 6158fae3551SRodney W. Grimes } 6168fae3551SRodney W. Grimes 61749133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 61849133c6dSPawel Jakub Dawidek if (capdns != NULL) { 61949133c6dSPawel Jakub Dawidek const char *types[1]; 62049133c6dSPawel Jakub Dawidek 62149133c6dSPawel Jakub Dawidek types[0] = "ADDR"; 62249133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 62349133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 62449133c6dSPawel Jakub Dawidek } 62549133c6dSPawel Jakub Dawidek 62649133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 62749133c6dSPawel Jakub Dawidek err(1, "connect"); 62849133c6dSPawel Jakub Dawidek 62943470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 63043470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 63143470e3bSGarrett Wollman 63243470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 63343470e3bSGarrett Wollman errx(EX_USAGE, 63443470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 63543470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 63643470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 63743470e3bSGarrett Wollman errx(EX_USAGE, 63843470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6398fae3551SRodney W. Grimes 640d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6418fae3551SRodney W. Grimes timing = 1; 64243470e3bSGarrett Wollman 6438fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 644d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6458fae3551SRodney W. Grimes *datap++ = i; 6468fae3551SRodney W. Grimes 6478fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6488fae3551SRodney W. Grimes 6498fae3551SRodney W. Grimes hold = 1; 65049133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 65149133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6528fae3551SRodney W. Grimes sizeof(hold)); 65349133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 65449133c6dSPawel Jakub Dawidek sizeof(hold)); 65549133c6dSPawel Jakub Dawidek } 6568fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 65749133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6588fae3551SRodney W. Grimes sizeof(hold)); 6599a4365d0SYoshinobu Inoue #ifdef IPSEC 6609a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6619a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6629a4365d0SYoshinobu Inoue char *buf; 6639a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6649a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6659a4365d0SYoshinobu Inoue if (buf == NULL) 666ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 66749133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6689a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6691ad0b1beSMaxim Konovalov err(EX_CONFIG, 6701ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6719a4365d0SYoshinobu Inoue free(buf); 6729a4365d0SYoshinobu Inoue } 6739a4365d0SYoshinobu Inoue 6749a4365d0SYoshinobu Inoue if (policy_out != NULL) { 6759a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 6769a4365d0SYoshinobu Inoue if (buf == NULL) 677ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 67849133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 6799a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6801ad0b1beSMaxim Konovalov err(EX_CONFIG, 6811ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6829a4365d0SYoshinobu Inoue free(buf); 6839a4365d0SYoshinobu Inoue } 6849a4365d0SYoshinobu Inoue } 6859a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 6869a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 6878fae3551SRodney W. Grimes 6880b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 6890b2f8b3fSMaxim Konovalov ip = (struct ip*)outpackhdr; 6900b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 6910b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 6920b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 6930b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 6940b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 6950b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 6960b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 6970b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 6980b2f8b3fSMaxim Konovalov } 69949133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 7000b2f8b3fSMaxim Konovalov ip->ip_v = IPVERSION; 7010b2f8b3fSMaxim Konovalov ip->ip_hl = sizeof(struct ip) >> 2; 7020b2f8b3fSMaxim Konovalov ip->ip_tos = tos; 7030b2f8b3fSMaxim Konovalov ip->ip_id = 0; 704ffc610b2SAndrey V. Elsukov ip->ip_off = htons(df ? IP_DF : 0); 7050b2f8b3fSMaxim Konovalov ip->ip_ttl = ttl; 7060b2f8b3fSMaxim Konovalov ip->ip_p = IPPROTO_ICMP; 70731eac03bSSean Chittenden ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 7080b2f8b3fSMaxim Konovalov ip->ip_dst = to->sin_addr; 7090b2f8b3fSMaxim Konovalov } 71049133c6dSPawel Jakub Dawidek 71149133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 71249133c6dSPawel Jakub Dawidek cansandbox = true; 71349133c6dSPawel Jakub Dawidek else if (capdns != NULL) 7148751b03bSMariusz Zaborski cansandbox = CASPER_SUPPORT; 71549133c6dSPawel Jakub Dawidek else 71649133c6dSPawel Jakub Dawidek cansandbox = false; 71749133c6dSPawel Jakub Dawidek 71849133c6dSPawel Jakub Dawidek /* 71949133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 72049133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 72149133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 72249133c6dSPawel Jakub Dawidek */ 72349133c6dSPawel Jakub Dawidek if (cansandbox && cap_enter() < 0 && errno != ENOSYS) 72449133c6dSPawel Jakub Dawidek err(1, "cap_enter"); 72549133c6dSPawel Jakub Dawidek 72649133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 72749133c6dSPawel Jakub Dawidek if (cap_rights_limit(srecv, &rights) < 0 && errno != ENOSYS) 72849133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 72949133c6dSPawel Jakub Dawidek 73049133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 73149133c6dSPawel Jakub Dawidek if (cap_rights_limit(ssend, &rights) < 0 && errno != ENOSYS) 73249133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 73349133c6dSPawel Jakub Dawidek 7348fae3551SRodney W. Grimes /* record route option */ 7358fae3551SRodney W. Grimes if (options & F_RROUTE) { 7368fae3551SRodney W. Grimes #ifdef IP_OPTIONS 737039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7388fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7398fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7408fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 741039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 74249133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 74343470e3bSGarrett Wollman sizeof(rspace)) < 0) 74443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7458fae3551SRodney W. Grimes #else 74643470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 74743470e3bSGarrett Wollman "record route not available in this implementation"); 7488fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7498fae3551SRodney W. Grimes } 7508fae3551SRodney W. Grimes 751211bfbd2SRuslan Ermilov if (options & F_TTL) { 75249133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 753211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 754211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 755211bfbd2SRuslan Ermilov } 756211bfbd2SRuslan Ermilov } 75785456935SBill Fenner if (options & F_NOLOOP) { 75849133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 75985456935SBill Fenner sizeof(loop)) < 0) { 76043470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 76185456935SBill Fenner } 76285456935SBill Fenner } 76385456935SBill Fenner if (options & F_MTTL) { 76449133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 765211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 76643470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 76785456935SBill Fenner } 76885456935SBill Fenner } 76985456935SBill Fenner if (options & F_MIF) { 77049133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 77185456935SBill Fenner sizeof(ifaddr)) < 0) { 77243470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 77385456935SBill Fenner } 77485456935SBill Fenner } 775039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 776039d6aa4SBill Fenner { int on = 1; 77749133c6dSPawel Jakub Dawidek if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0) 778039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 779039d6aa4SBill Fenner } 780039d6aa4SBill Fenner #endif 7819ff95228SGleb Smirnoff if (sweepmax) { 782bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 783bb7dfe5eSGleb Smirnoff errx(EX_USAGE, "Maximum packet size must be no less than the minimum packet size"); 7849ff95228SGleb Smirnoff 7859ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 7869ff95228SGleb Smirnoff errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive"); 7879ff95228SGleb Smirnoff 7889ff95228SGleb Smirnoff if (npackets > 0) { 7899ff95228SGleb Smirnoff snpackets = npackets; 7909ff95228SGleb Smirnoff npackets = 0; 7919ff95228SGleb Smirnoff } else 7929ff95228SGleb Smirnoff snpackets = 1; 7939ff95228SGleb Smirnoff datalen = sweepmin; 7949ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 7959ff95228SGleb Smirnoff } 7969ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 7979ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 79885456935SBill Fenner 7998fae3551SRodney W. Grimes /* 8008fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 8018fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 8028fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 80343470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 80443470e3bSGarrett Wollman * or multicast pings if they wish. 8058fae3551SRodney W. Grimes */ 8064fba6582SMaxim Konovalov 8074fba6582SMaxim Konovalov /* 8084fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8094fba6582SMaxim Konovalov * as well. 8104fba6582SMaxim Konovalov */ 8114fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 81249133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8138fae3551SRodney W. Grimes sizeof(hold)); 81449133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 81549133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 81649133c6dSPawel Jakub Dawidek if (cap_rights_limit(srecv, &rights) < 0 && errno != ENOSYS) 81749133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 818261e59bbSMaxim Konovalov if (uid == 0) 81949133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 820e8bd25ceSRobert Watson sizeof(hold)); 82149133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 82249133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 82349133c6dSPawel Jakub Dawidek if (cap_rights_limit(ssend, &rights) < 0 && errno != ENOSYS) 82449133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 825e8bd25ceSRobert Watson 82699490edeSWarner Losh if (to->sin_family == AF_INET) { 82799490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 82899490edeSWarner Losh inet_ntoa(to->sin_addr)); 82999490edeSWarner Losh if (source) 83099490edeSWarner Losh (void)printf(" from %s", shostname); 8319ff95228SGleb Smirnoff if (sweepmax) 8329ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8339ff95228SGleb Smirnoff sweepmin, sweepmax); 8349ff95228SGleb Smirnoff else 83599490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8369ff95228SGleb Smirnoff 8379ff95228SGleb Smirnoff } else { 8389ff95228SGleb Smirnoff if (sweepmax) 8399ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8409ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8419ff95228SGleb Smirnoff else 8428fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8439ff95228SGleb Smirnoff } 8448fae3551SRodney W. Grimes 8457d81b35cSBruce Evans /* 846a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 847a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8487d81b35cSBruce Evans */ 849a2a00888SSean Eric Fagan 850f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 85137e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 852a2a00888SSean Eric Fagan 853a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 854a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 855a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 856a2a00888SSean Eric Fagan } 857a2a00888SSean Eric Fagan 858a2a00888SSean Eric Fagan si_sa.sa_handler = status; 85937e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 860624ff938SWarner Losh err(EX_OSERR, "sigaction"); 86137e5b2c6SSean Eric Fagan } 862bf113f1bSBill Fumerola 863bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8647237fd94SBill Fumerola si_sa.sa_handler = stopit; 8657237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 8667237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 867bf113f1bSBill Fumerola } 86837e5b2c6SSean Eric Fagan 869039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 870039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 871039d6aa4SBill Fenner msg.msg_iov = &iov; 872039d6aa4SBill Fenner msg.msg_iovlen = 1; 873039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 874039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 875039d6aa4SBill Fenner #endif 876039d6aa4SBill Fenner iov.iov_base = packet; 877e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 878039d6aa4SBill Fenner 87932af342fSRuslan Ermilov if (preload == 0) 88032af342fSRuslan Ermilov pinger(); /* send the first ping */ 88132af342fSRuslan Ermilov else { 88232af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 88332af342fSRuslan Ermilov preload = npackets; 8848fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 8858fae3551SRodney W. Grimes pinger(); 88632af342fSRuslan Ermilov } 88732af342fSRuslan Ermilov (void)gettimeofday(&last, NULL); 8888fae3551SRodney W. Grimes 889039d6aa4SBill Fenner if (options & F_FLOOD) { 890039d6aa4SBill Fenner intvl.tv_sec = 0; 891039d6aa4SBill Fenner intvl.tv_usec = 10000; 892039d6aa4SBill Fenner } else { 893526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 894526f06b2SMatthew Dillon intvl.tv_usec = interval % 1000 * 1000; 895039d6aa4SBill Fenner } 896039d6aa4SBill Fenner 897261e59bbSMaxim Konovalov almost_done = 0; 8988f975bb3SBruce Evans while (!finish_up) { 899261e59bbSMaxim Konovalov struct timeval now, timeout; 900039d6aa4SBill Fenner fd_set rfds; 901261e59bbSMaxim Konovalov int cc, n; 9028fae3551SRodney W. Grimes 9037d81b35cSBruce Evans check_status(); 90449133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 9057e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 906039d6aa4SBill Fenner FD_ZERO(&rfds); 90749133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 908039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 909039d6aa4SBill Fenner timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 910039d6aa4SBill Fenner timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 911039d6aa4SBill Fenner while (timeout.tv_usec < 0) { 912039d6aa4SBill Fenner timeout.tv_usec += 1000000; 913039d6aa4SBill Fenner timeout.tv_sec--; 9148fae3551SRodney W. Grimes } 915e345a80dSPhilippe Charnier while (timeout.tv_usec >= 1000000) { 916039d6aa4SBill Fenner timeout.tv_usec -= 1000000; 917039d6aa4SBill Fenner timeout.tv_sec++; 918039d6aa4SBill Fenner } 919039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9206959b14dSXin LI timerclear(&timeout); 92149133c6dSPawel Jakub Dawidek n = select(srecv + 1, &rfds, NULL, NULL, &timeout); 9225e2cc0f4SStephen McKay if (n < 0) 9235e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 924039d6aa4SBill Fenner if (n == 1) { 92531eac03bSSean Chittenden struct timeval *tv = NULL; 926039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 927039d6aa4SBill Fenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 928039d6aa4SBill Fenner 929039d6aa4SBill Fenner msg.msg_controllen = sizeof(ctrl); 930039d6aa4SBill Fenner #endif 931039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 93249133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9338fae3551SRodney W. Grimes if (errno == EINTR) 9348fae3551SRodney W. Grimes continue; 935e345a80dSPhilippe Charnier warn("recvmsg"); 9368fae3551SRodney W. Grimes continue; 9378fae3551SRodney W. Grimes } 938039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 939039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 940039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 94131eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 942fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 943fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 94431eac03bSSean Chittenden tv = &now; 945fa05a94cSJohn Birrell } 946039d6aa4SBill Fenner #endif 94731eac03bSSean Chittenden if (tv == NULL) { 948039d6aa4SBill Fenner (void)gettimeofday(&now, NULL); 94931eac03bSSean Chittenden tv = &now; 950039d6aa4SBill Fenner } 95131eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 95231eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 95331eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9548fae3551SRodney W. Grimes break; 9558fae3551SRodney W. Grimes } 9565e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9579ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 9589ff95228SGleb Smirnoff for (i = 0; i < sweepincr ; ++i) 9599ff95228SGleb Smirnoff *datap++ = i; 9609ff95228SGleb Smirnoff datalen += sweepincr; 9619ff95228SGleb Smirnoff if (datalen > sweepmax) 9629ff95228SGleb Smirnoff break; 9639ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9649ff95228SGleb Smirnoff sntransmitted = 0; 9659ff95228SGleb Smirnoff } 966039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 967039d6aa4SBill Fenner pinger(); 968039d6aa4SBill Fenner else { 969039d6aa4SBill Fenner if (almost_done) 970039d6aa4SBill Fenner break; 971039d6aa4SBill Fenner almost_done = 1; 9725e2cc0f4SStephen McKay intvl.tv_usec = 0; 973039d6aa4SBill Fenner if (nreceived) { 974039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 975039d6aa4SBill Fenner if (!intvl.tv_sec) 976039d6aa4SBill Fenner intvl.tv_sec = 1; 977d6cd1497SGleb Smirnoff } else { 978d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 979d6cd1497SGleb Smirnoff intvl.tv_usec = waittime % 1000 * 1000; 980d6cd1497SGleb Smirnoff } 981039d6aa4SBill Fenner } 982039d6aa4SBill Fenner (void)gettimeofday(&last, NULL); 98325107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 98425107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 98525107197SIan Dowse if (options & F_MISSED) 986ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 987039d6aa4SBill Fenner } 988039d6aa4SBill Fenner } 98925107197SIan Dowse } 9908f975bb3SBruce Evans finish(); 9918fae3551SRodney W. Grimes /* NOTREACHED */ 992f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 9938fae3551SRodney W. Grimes } 9948fae3551SRodney W. Grimes 9958fae3551SRodney W. Grimes /* 9968f975bb3SBruce Evans * stopit -- 9978f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 9988f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 9998f975bb3SBruce Evans * to be called from a signal handler. 1000515dd2faSJulian Elischer */ 1001515dd2faSJulian Elischer void 1002fafb8f11SEd Schouten stopit(int sig __unused) 1003515dd2faSJulian Elischer { 1004efc8588dSDavid E. O'Brien 1005c8bb99e5SIan Dowse /* 1006c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 1007c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 1008c8bb99e5SIan Dowse */ 1009c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 1010c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 1011515dd2faSJulian Elischer finish_up = 1; 1012515dd2faSJulian Elischer } 1013515dd2faSJulian Elischer 1014515dd2faSJulian Elischer /* 10158fae3551SRodney W. Grimes * pinger -- 10168fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10178fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1018eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10194fba6582SMaxim Konovalov * bytes of the data portion are used to hold a UNIX "timeval" struct in 10204fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10218fae3551SRodney W. Grimes */ 102243470e3bSGarrett Wollman static void 102343470e3bSGarrett Wollman pinger(void) 10248fae3551SRodney W. Grimes { 1025eb1543c6SMatthew N. Dodd struct timeval now; 102613e3f0b7SMaxim Konovalov struct tv32 tv32; 10270b2f8b3fSMaxim Konovalov struct ip *ip; 10283d438ad6SDavid E. O'Brien struct icmp *icp; 1029efc8588dSDavid E. O'Brien int cc, i; 10300b2f8b3fSMaxim Konovalov u_char *packet; 10318fae3551SRodney W. Grimes 10320b2f8b3fSMaxim Konovalov packet = outpack; 10338fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 1034eb1543c6SMatthew N. Dodd icp->icmp_type = icmp_type; 10358fae3551SRodney W. Grimes icp->icmp_code = 0; 10368fae3551SRodney W. Grimes icp->icmp_cksum = 0; 10375db89bc7SBill Fenner icp->icmp_seq = htons(ntransmitted); 10388fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 10398fae3551SRodney W. Grimes 10405db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10418fae3551SRodney W. Grimes 1042eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 1043eb1543c6SMatthew N. Dodd (void)gettimeofday(&now, NULL); 10448fae3551SRodney W. Grimes 104513e3f0b7SMaxim Konovalov tv32.tv32_sec = htonl(now.tv_sec); 104613e3f0b7SMaxim Konovalov tv32.tv32_usec = htonl(now.tv_usec); 1047eb1543c6SMatthew N. Dodd if (options & F_TIME) 1048eb1543c6SMatthew N. Dodd icp->icmp_otime = htonl((now.tv_sec % (24*60*60)) 1049eb1543c6SMatthew N. Dodd * 1000 + now.tv_usec / 1000); 1050eb1543c6SMatthew N. Dodd if (timing) 105113e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10520fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 105313e3f0b7SMaxim Konovalov sizeof(tv32)); 1054eb1543c6SMatthew N. Dodd } 1055eb1543c6SMatthew N. Dodd 10560fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10578fae3551SRodney W. Grimes 10588fae3551SRodney W. Grimes /* compute ICMP checksum here */ 10598fae3551SRodney W. Grimes icp->icmp_cksum = in_cksum((u_short *)icp, cc); 10608fae3551SRodney W. Grimes 10610b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 10620b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 10630b2f8b3fSMaxim Konovalov ip = (struct ip *)outpackhdr; 1064ffc610b2SAndrey V. Elsukov ip->ip_len = htons(cc); 10650b2f8b3fSMaxim Konovalov ip->ip_sum = in_cksum((u_short *)outpackhdr, cc); 10660b2f8b3fSMaxim Konovalov packet = outpackhdr; 10670b2f8b3fSMaxim Konovalov } 106849133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 10698fae3551SRodney W. Grimes if (i < 0 || i != cc) { 107043470e3bSGarrett Wollman if (i < 0) { 10718f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1072515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1073515dd2faSJulian Elischer return; 1074515dd2faSJulian Elischer } 107543470e3bSGarrett Wollman warn("sendto"); 107643470e3bSGarrett Wollman } else { 107743470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1078416aa49bSPoul-Henning Kamp hostname, i, cc); 10798fae3551SRodney W. Grimes } 1080363d7bbeSJulian Elischer } 1081363d7bbeSJulian Elischer ntransmitted++; 10829ff95228SGleb Smirnoff sntransmitted++; 10838fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 10848fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 10858fae3551SRodney W. Grimes } 10868fae3551SRodney W. Grimes 10878fae3551SRodney W. Grimes /* 10888fae3551SRodney W. Grimes * pr_pack -- 10898fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 10908fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 10918fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 10928fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 10938fae3551SRodney W. Grimes */ 109443470e3bSGarrett Wollman static void 1095fafb8f11SEd Schouten pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv) 10968fae3551SRodney W. Grimes { 10979b219646SPeter Wemm struct in_addr ina; 10989b219646SPeter Wemm u_char *cp, *dp; 10993d438ad6SDavid E. O'Brien struct icmp *icp; 11008fae3551SRodney W. Grimes struct ip *ip; 11019d2b0ab8SPeter Wemm const void *tp; 11028f975bb3SBruce Evans double triptime; 1103e88178ddSMaxim Konovalov int dupflag, hlen, i, j, recv_len, seq; 1104efc8588dSDavid E. O'Brien static int old_rrlen; 1105efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 11068fae3551SRodney W. Grimes 11078fae3551SRodney W. Grimes /* Check the IP header */ 11088fae3551SRodney W. Grimes ip = (struct ip *)buf; 11098fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 1110e88178ddSMaxim Konovalov recv_len = cc; 11118fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11128fae3551SRodney W. Grimes if (options & F_VERBOSE) 111343470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 111443470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11158fae3551SRodney W. Grimes return; 11168fae3551SRodney W. Grimes } 11178fae3551SRodney W. Grimes 11188fae3551SRodney W. Grimes /* Now the ICMP part */ 11198fae3551SRodney W. Grimes cc -= hlen; 11208fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 1121eb1543c6SMatthew N. Dodd if (icp->icmp_type == icmp_type_rsp) { 11228fae3551SRodney W. Grimes if (icp->icmp_id != ident) 11238fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11248fae3551SRodney W. Grimes ++nreceived; 11258f975bb3SBruce Evans triptime = 0.0; 11268fae3551SRodney W. Grimes if (timing) { 1127d32ff037SJohn Birrell struct timeval tv1; 112813e3f0b7SMaxim Konovalov struct tv32 tv32; 11298fae3551SRodney W. Grimes #ifndef icmp_data 11309d2b0ab8SPeter Wemm tp = &icp->icmp_ip; 11318fae3551SRodney W. Grimes #else 11329d2b0ab8SPeter Wemm tp = icp->icmp_data; 11338fae3551SRodney W. Grimes #endif 11344eae39bfSStefan Farfeleder tp = (const char *)tp + phdr_len; 1135143008a1SMatthew N. Dodd 11367e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 11377e9489e0SHiroki Sato sizeof(tv1)) { 11389d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 113913e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 114013e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 114113e3f0b7SMaxim Konovalov tv1.tv_usec = ntohl(tv32.tv32_usec); 1142039d6aa4SBill Fenner tvsub(tv, &tv1); 1143039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 1144039d6aa4SBill Fenner ((double)tv->tv_usec) / 1000.0; 11458fae3551SRodney W. Grimes tsum += triptime; 11463109a910SGarrett Wollman tsumsq += triptime * triptime; 11478fae3551SRodney W. Grimes if (triptime < tmin) 11488fae3551SRodney W. Grimes tmin = triptime; 11498fae3551SRodney W. Grimes if (triptime > tmax) 11508fae3551SRodney W. Grimes tmax = triptime; 115147e9b3eaSMatthew N. Dodd } else 115247e9b3eaSMatthew N. Dodd timing = 0; 11538fae3551SRodney W. Grimes } 11548fae3551SRodney W. Grimes 11555db89bc7SBill Fenner seq = ntohs(icp->icmp_seq); 11565db89bc7SBill Fenner 11575db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 11588fae3551SRodney W. Grimes ++nrepeats; 11598fae3551SRodney W. Grimes --nreceived; 11608fae3551SRodney W. Grimes dupflag = 1; 11618fae3551SRodney W. Grimes } else { 11625db89bc7SBill Fenner SET(seq % mx_dup_ck); 11638fae3551SRodney W. Grimes dupflag = 0; 11648fae3551SRodney W. Grimes } 11658fae3551SRodney W. Grimes 11668fae3551SRodney W. Grimes if (options & F_QUIET) 11678fae3551SRodney W. Grimes return; 11688fae3551SRodney W. Grimes 1169d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1170d6cd1497SGleb Smirnoff ++nrcvtimeout; 1171d6cd1497SGleb Smirnoff return; 1172d6cd1497SGleb Smirnoff } 1173d6cd1497SGleb Smirnoff 11748fae3551SRodney W. Grimes if (options & F_FLOOD) 11758fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 11768fae3551SRodney W. Grimes else { 11778fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 11788fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 11795db89bc7SBill Fenner seq); 11808fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 11818fae3551SRodney W. Grimes if (timing) 1182d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 11838fae3551SRodney W. Grimes if (dupflag) 11848fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1185772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1186ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1187143008a1SMatthew N. Dodd if (options & F_MASK) { 1188143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1189143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1190f7fc5c91SMaxim Konovalov inet_ntoa(*(struct in_addr *)&(icp->icmp_mask))); 1191143008a1SMatthew N. Dodd } 1192eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1193eb1543c6SMatthew N. Dodd (void)printf(" tso=%s", pr_ntime(icp->icmp_otime)); 1194eb1543c6SMatthew N. Dodd (void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime)); 1195eb1543c6SMatthew N. Dodd (void)printf(" tst=%s", pr_ntime(icp->icmp_ttime)); 1196eb1543c6SMatthew N. Dodd } 1197e88178ddSMaxim Konovalov if (recv_len != send_len) { 1198e88178ddSMaxim Konovalov (void)printf( 1199e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1200e88178ddSMaxim Konovalov recv_len, send_len); 1201e88178ddSMaxim Konovalov } 12028fae3551SRodney W. Grimes /* check the data */ 1203eb1543c6SMatthew N. Dodd cp = (u_char*)&icp->icmp_data[phdr_len]; 12040fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 120547e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 120629dccd6aSMaxim Konovalov i = 0; 120729dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 120829dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 120929dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 121029dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 121129dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 121229dccd6aSMaxim Konovalov } 121329dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12148fae3551SRodney W. Grimes if (*cp != *dp) { 12158fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12168fae3551SRodney W. Grimes i, *dp, *cp); 12171ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 12188fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 1219d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1220e88178ddSMaxim Konovalov if ((i % 16) == 8) 1221d32ff037SJohn Birrell (void)printf("\n\t"); 1222e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1223d32ff037SJohn Birrell } 12241ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 12250fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1226d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1227e88178ddSMaxim Konovalov if ((i % 16) == 8) 12288fae3551SRodney W. Grimes (void)printf("\n\t"); 1229e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 12308fae3551SRodney W. Grimes } 12318fae3551SRodney W. Grimes break; 12328fae3551SRodney W. Grimes } 12338fae3551SRodney W. Grimes } 12348fae3551SRodney W. Grimes } 12358fae3551SRodney W. Grimes } else { 1236ef9e6dc7SBill Fenner /* 1237ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1238ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1239ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1240ef9e6dc7SBill Fenner * and ICMP type and ID. 1241f78ac61bSWarner Losh * 1242f78ac61bSWarner Losh * Only print all the error messages if we are running 1243f78ac61bSWarner Losh * as root to avoid leaking information not normally 1244f78ac61bSWarner Losh * available to those not running as root. 1245ef9e6dc7SBill Fenner */ 1246ef9e6dc7SBill Fenner #ifndef icmp_data 1247ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 1248ef9e6dc7SBill Fenner #else 1249ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 1250ef9e6dc7SBill Fenner #endif 1251ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 1252ef9e6dc7SBill Fenner 1253ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1254ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1255d389e86aSMatt Jacob (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && 1256ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 1257ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 1258ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 12598fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 126043470e3bSGarrett Wollman pr_addr(from->sin_addr)); 12618fae3551SRodney W. Grimes pr_icmph(icp); 1262ef9e6dc7SBill Fenner } else 1263ef9e6dc7SBill Fenner return; 12648fae3551SRodney W. Grimes } 12658fae3551SRodney W. Grimes 12668fae3551SRodney W. Grimes /* Display any IP options */ 12678fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 12688fae3551SRodney W. Grimes 12698fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 12708fae3551SRodney W. Grimes switch (*cp) { 12718fae3551SRodney W. Grimes case IPOPT_EOL: 12728fae3551SRodney W. Grimes hlen = 0; 12738fae3551SRodney W. Grimes break; 12748fae3551SRodney W. Grimes case IPOPT_LSRR: 1275cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1276cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1277cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1278301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 12798fae3551SRodney W. Grimes hlen -= 2; 1280301207dfSMaxim Konovalov cp += 2; 12813c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 12823c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 12838fae3551SRodney W. Grimes for (;;) { 1284301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1285301207dfSMaxim Konovalov if (ina.s_addr == 0) 12861ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1287301207dfSMaxim Konovalov else 12881ad0b1beSMaxim Konovalov (void)printf("\t%s", 12891ad0b1beSMaxim Konovalov pr_addr(ina)); 1290301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1291301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1292301207dfSMaxim Konovalov j -= INADDR_LEN; 1293301207dfSMaxim Konovalov if (j < INADDR_LEN) 12948fae3551SRodney W. Grimes break; 12958fae3551SRodney W. Grimes (void)putchar('\n'); 12968fae3551SRodney W. Grimes } 1297301207dfSMaxim Konovalov } else 1298301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 12998fae3551SRodney W. Grimes break; 13008fae3551SRodney W. Grimes case IPOPT_RR: 1301301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1302301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 13038fae3551SRodney W. Grimes hlen -= 2; 1304301207dfSMaxim Konovalov cp += 2; 13058fae3551SRodney W. Grimes if (i > j) 13068fae3551SRodney W. Grimes i = j; 1307301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1308301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1309301207dfSMaxim Konovalov old_rrlen = 0; 13108fae3551SRodney W. Grimes continue; 1311301207dfSMaxim Konovalov } 13128fae3551SRodney W. Grimes if (i == old_rrlen 13138fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 13148fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 13158fae3551SRodney W. Grimes (void)printf("\t(same route)"); 13168fae3551SRodney W. Grimes hlen -= i; 13178fae3551SRodney W. Grimes cp += i; 13188fae3551SRodney W. Grimes break; 13198fae3551SRodney W. Grimes } 13208fae3551SRodney W. Grimes old_rrlen = i; 13218fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 13228fae3551SRodney W. Grimes (void)printf("\nRR: "); 1323301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1324301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 13258fae3551SRodney W. Grimes for (;;) { 1326301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1327301207dfSMaxim Konovalov if (ina.s_addr == 0) 13281ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1329301207dfSMaxim Konovalov else 1330301207dfSMaxim Konovalov (void)printf("\t%s", 1331301207dfSMaxim Konovalov pr_addr(ina)); 1332301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1333301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1334301207dfSMaxim Konovalov i -= INADDR_LEN; 1335301207dfSMaxim Konovalov if (i < INADDR_LEN) 13368fae3551SRodney W. Grimes break; 13378fae3551SRodney W. Grimes (void)putchar('\n'); 13388fae3551SRodney W. Grimes } 1339301207dfSMaxim Konovalov } else 1340301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 13418fae3551SRodney W. Grimes break; 13428fae3551SRodney W. Grimes case IPOPT_NOP: 13438fae3551SRodney W. Grimes (void)printf("\nNOP"); 13448fae3551SRodney W. Grimes break; 13458fae3551SRodney W. Grimes default: 13468fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 13478fae3551SRodney W. Grimes break; 13488fae3551SRodney W. Grimes } 13498fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 13508fae3551SRodney W. Grimes (void)putchar('\n'); 13518fae3551SRodney W. Grimes (void)fflush(stdout); 13528fae3551SRodney W. Grimes } 13538fae3551SRodney W. Grimes } 13548fae3551SRodney W. Grimes 13558fae3551SRodney W. Grimes /* 13568fae3551SRodney W. Grimes * in_cksum -- 13578fae3551SRodney W. Grimes * Checksum routine for Internet Protocol family headers (C Version) 13588fae3551SRodney W. Grimes */ 135943470e3bSGarrett Wollman u_short 1360fafb8f11SEd Schouten in_cksum(u_short *addr, int len) 13618fae3551SRodney W. Grimes { 1362efc8588dSDavid E. O'Brien int nleft, sum; 1363efc8588dSDavid E. O'Brien u_short *w; 13643ec12efeSPierre Beyssac union { 136509333e78SPierre Beyssac u_short us; 136609333e78SPierre Beyssac u_char uc[2]; 136709333e78SPierre Beyssac } last; 136809333e78SPierre Beyssac u_short answer; 13698fae3551SRodney W. Grimes 1370efc8588dSDavid E. O'Brien nleft = len; 1371efc8588dSDavid E. O'Brien sum = 0; 1372efc8588dSDavid E. O'Brien w = addr; 1373efc8588dSDavid E. O'Brien 13748fae3551SRodney W. Grimes /* 13758fae3551SRodney W. Grimes * Our algorithm is simple, using a 32 bit accumulator (sum), we add 13768fae3551SRodney W. Grimes * sequential 16 bit words to it, and at the end, fold back all the 13778fae3551SRodney W. Grimes * carry bits from the top 16 bits into the lower 16 bits. 13788fae3551SRodney W. Grimes */ 13798fae3551SRodney W. Grimes while (nleft > 1) { 13808fae3551SRodney W. Grimes sum += *w++; 13818fae3551SRodney W. Grimes nleft -= 2; 13828fae3551SRodney W. Grimes } 13838fae3551SRodney W. Grimes 13848fae3551SRodney W. Grimes /* mop up an odd byte, if necessary */ 13858fae3551SRodney W. Grimes if (nleft == 1) { 138609333e78SPierre Beyssac last.uc[0] = *(u_char *)w; 138709333e78SPierre Beyssac last.uc[1] = 0; 138809333e78SPierre Beyssac sum += last.us; 13898fae3551SRodney W. Grimes } 13908fae3551SRodney W. Grimes 13918fae3551SRodney W. Grimes /* add back carry outs from top 16 bits to low 16 bits */ 13928fae3551SRodney W. Grimes sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */ 13938fae3551SRodney W. Grimes sum += (sum >> 16); /* add carry */ 139409333e78SPierre Beyssac answer = ~sum; /* truncate to 16 bits */ 139509333e78SPierre Beyssac return(answer); 13968fae3551SRodney W. Grimes } 13978fae3551SRodney W. Grimes 13988fae3551SRodney W. Grimes /* 13998fae3551SRodney W. Grimes * tvsub -- 14008fae3551SRodney W. Grimes * Subtract 2 timeval structs: out = out - in. Out is assumed to 14018fae3551SRodney W. Grimes * be >= in. 14028fae3551SRodney W. Grimes */ 140343470e3bSGarrett Wollman static void 1404fafb8f11SEd Schouten tvsub(struct timeval *out, const struct timeval *in) 14058fae3551SRodney W. Grimes { 1406efc8588dSDavid E. O'Brien 14078fae3551SRodney W. Grimes if ((out->tv_usec -= in->tv_usec) < 0) { 14088fae3551SRodney W. Grimes --out->tv_sec; 14098fae3551SRodney W. Grimes out->tv_usec += 1000000; 14108fae3551SRodney W. Grimes } 14118fae3551SRodney W. Grimes out->tv_sec -= in->tv_sec; 14128fae3551SRodney W. Grimes } 14138fae3551SRodney W. Grimes 14148fae3551SRodney W. Grimes /* 1415badd8138SSean Eric Fagan * status -- 1416badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1417badd8138SSean Eric Fagan */ 1418badd8138SSean Eric Fagan 141943470e3bSGarrett Wollman static void 1420fafb8f11SEd Schouten status(int sig __unused) 14217d81b35cSBruce Evans { 1422efc8588dSDavid E. O'Brien 142337e5b2c6SSean Eric Fagan siginfo_p = 1; 142437e5b2c6SSean Eric Fagan } 142537e5b2c6SSean Eric Fagan 142643470e3bSGarrett Wollman static void 1427fafb8f11SEd Schouten check_status(void) 1428badd8138SSean Eric Fagan { 1429efc8588dSDavid E. O'Brien 143037e5b2c6SSean Eric Fagan if (siginfo_p) { 143137e5b2c6SSean Eric Fagan siginfo_p = 0; 1432aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 14337d81b35cSBruce Evans nreceived, ntransmitted, 1434aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1435aed98a27SMaxim Konovalov if (nreceived && timing) 1436aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1437aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1438aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 143937e5b2c6SSean Eric Fagan } 1440badd8138SSean Eric Fagan } 1441badd8138SSean Eric Fagan 1442badd8138SSean Eric Fagan /* 14438fae3551SRodney W. Grimes * finish -- 14448fae3551SRodney W. Grimes * Print out statistics, and give up. 14458fae3551SRodney W. Grimes */ 144643470e3bSGarrett Wollman static void 1447fafb8f11SEd Schouten finish(void) 14488fae3551SRodney W. Grimes { 14498fae3551SRodney W. Grimes 14508fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1451a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 14528fae3551SRodney W. Grimes (void)putchar('\n'); 14538fae3551SRodney W. Grimes (void)fflush(stdout); 14548fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 14558fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 14568fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 14578fae3551SRodney W. Grimes if (nrepeats) 14588fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1459ebe70c8fSWarner Losh if (ntransmitted) { 14608fae3551SRodney W. Grimes if (nreceived > ntransmitted) 14618fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 14628fae3551SRodney W. Grimes else 1463aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1464aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1465aa822c39SDima Dorfman ntransmitted); 1466ebe70c8fSWarner Losh } 1467d6cd1497SGleb Smirnoff if (nrcvtimeout) 1468d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 14698fae3551SRodney W. Grimes (void)putchar('\n'); 14703109a910SGarrett Wollman if (nreceived && timing) { 14713109a910SGarrett Wollman double n = nreceived + nrepeats; 14723109a910SGarrett Wollman double avg = tsum / n; 14733109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 14741ad0b1beSMaxim Konovalov (void)printf( 14751ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 14763109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 14773109a910SGarrett Wollman } 1478badd8138SSean Eric Fagan 14796e1173dcSDavid Greenman if (nreceived) 14808fae3551SRodney W. Grimes exit(0); 14816e1173dcSDavid Greenman else 14826e1173dcSDavid Greenman exit(2); 14838fae3551SRodney W. Grimes } 14848fae3551SRodney W. Grimes 14858fae3551SRodney W. Grimes #ifdef notdef 14868fae3551SRodney W. Grimes static char *ttab[] = { 14878fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 14888fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 14898fae3551SRodney W. Grimes "Source Quench", /* IP */ 14908fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 14918fae3551SRodney W. Grimes "Echo", 14928fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 14938fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 14948fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 14958fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 14968fae3551SRodney W. Grimes "Info Request", /* id + sq */ 14978fae3551SRodney W. Grimes "Info Reply" /* " */ 14988fae3551SRodney W. Grimes }; 14998fae3551SRodney W. Grimes #endif 15008fae3551SRodney W. Grimes 15018fae3551SRodney W. Grimes /* 15028fae3551SRodney W. Grimes * pr_icmph -- 15038fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 15048fae3551SRodney W. Grimes */ 150543470e3bSGarrett Wollman static void 1506fafb8f11SEd Schouten pr_icmph(struct icmp *icp) 15078fae3551SRodney W. Grimes { 1508efc8588dSDavid E. O'Brien 15098fae3551SRodney W. Grimes switch(icp->icmp_type) { 15108fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 15118fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 15128fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15138fae3551SRodney W. Grimes break; 15148fae3551SRodney W. Grimes case ICMP_UNREACH: 15158fae3551SRodney W. Grimes switch(icp->icmp_code) { 15168fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 15178fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 15188fae3551SRodney W. Grimes break; 15198fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 15208fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 15218fae3551SRodney W. Grimes break; 15228fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 15238fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 15248fae3551SRodney W. Grimes break; 15258fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 15268fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 15278fae3551SRodney W. Grimes break; 15288fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1529ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1530ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 15318fae3551SRodney W. Grimes break; 15328fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 15338fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 15348fae3551SRodney W. Grimes break; 1535ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1536ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1537ef9e6dc7SBill Fenner break; 15388fae3551SRodney W. Grimes default: 15398fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 15408fae3551SRodney W. Grimes icp->icmp_code); 15418fae3551SRodney W. Grimes break; 15428fae3551SRodney W. Grimes } 15438fae3551SRodney W. Grimes /* Print returned IP header information */ 15448fae3551SRodney W. Grimes #ifndef icmp_data 15458fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15468fae3551SRodney W. Grimes #else 15478fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15488fae3551SRodney W. Grimes #endif 15498fae3551SRodney W. Grimes break; 15508fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 15518fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 15528fae3551SRodney W. Grimes #ifndef icmp_data 15538fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15548fae3551SRodney W. Grimes #else 15558fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15568fae3551SRodney W. Grimes #endif 15578fae3551SRodney W. Grimes break; 15588fae3551SRodney W. Grimes case ICMP_REDIRECT: 15598fae3551SRodney W. Grimes switch(icp->icmp_code) { 15608fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 15618fae3551SRodney W. Grimes (void)printf("Redirect Network"); 15628fae3551SRodney W. Grimes break; 15638fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 15648fae3551SRodney W. Grimes (void)printf("Redirect Host"); 15658fae3551SRodney W. Grimes break; 15668fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15678fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15688fae3551SRodney W. Grimes break; 15698fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15708fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 15718fae3551SRodney W. Grimes break; 15728fae3551SRodney W. Grimes default: 15738fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 15748fae3551SRodney W. Grimes break; 15758fae3551SRodney W. Grimes } 1576ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 15778fae3551SRodney W. Grimes #ifndef icmp_data 15788fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15798fae3551SRodney W. Grimes #else 15808fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15818fae3551SRodney W. Grimes #endif 15828fae3551SRodney W. Grimes break; 15838fae3551SRodney W. Grimes case ICMP_ECHO: 15848fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 15858fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15868fae3551SRodney W. Grimes break; 15878fae3551SRodney W. Grimes case ICMP_TIMXCEED: 15888fae3551SRodney W. Grimes switch(icp->icmp_code) { 15898fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 15908fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 15918fae3551SRodney W. Grimes break; 15928fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 15938fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 15948fae3551SRodney W. Grimes break; 15958fae3551SRodney W. Grimes default: 15968fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 15978fae3551SRodney W. Grimes icp->icmp_code); 15988fae3551SRodney W. Grimes break; 15998fae3551SRodney W. Grimes } 16008fae3551SRodney W. Grimes #ifndef icmp_data 16018fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 16028fae3551SRodney W. Grimes #else 16038fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 16048fae3551SRodney W. Grimes #endif 16058fae3551SRodney W. Grimes break; 16068fae3551SRodney W. Grimes case ICMP_PARAMPROB: 16078fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 16088fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 16098fae3551SRodney W. Grimes #ifndef icmp_data 16108fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 16118fae3551SRodney W. Grimes #else 16128fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 16138fae3551SRodney W. Grimes #endif 16148fae3551SRodney W. Grimes break; 16158fae3551SRodney W. Grimes case ICMP_TSTAMP: 16168fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 16178fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16188fae3551SRodney W. Grimes break; 16198fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 16208fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 16218fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16228fae3551SRodney W. Grimes break; 16238fae3551SRodney W. Grimes case ICMP_IREQ: 16248fae3551SRodney W. Grimes (void)printf("Information Request\n"); 16258fae3551SRodney W. Grimes /* XXX ID + Seq */ 16268fae3551SRodney W. Grimes break; 16278fae3551SRodney W. Grimes case ICMP_IREQREPLY: 16288fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 16298fae3551SRodney W. Grimes /* XXX ID + Seq */ 16308fae3551SRodney W. Grimes break; 16318fae3551SRodney W. Grimes case ICMP_MASKREQ: 16328fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 16338fae3551SRodney W. Grimes break; 16348fae3551SRodney W. Grimes case ICMP_MASKREPLY: 16358fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 16368fae3551SRodney W. Grimes break; 1637ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1638ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1639ef9e6dc7SBill Fenner break; 1640ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1641ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1642ef9e6dc7SBill Fenner break; 16438fae3551SRodney W. Grimes default: 16448fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 16458fae3551SRodney W. Grimes } 16468fae3551SRodney W. Grimes } 16478fae3551SRodney W. Grimes 16488fae3551SRodney W. Grimes /* 16498fae3551SRodney W. Grimes * pr_iph -- 16508fae3551SRodney W. Grimes * Print an IP header with options. 16518fae3551SRodney W. Grimes */ 165243470e3bSGarrett Wollman static void 1653fafb8f11SEd Schouten pr_iph(struct ip *ip) 16548fae3551SRodney W. Grimes { 1655a94c074dSDimitry Andric struct in_addr ina; 16568fae3551SRodney W. Grimes u_char *cp; 1657efc8588dSDavid E. O'Brien int hlen; 16588fae3551SRodney W. Grimes 16598fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 16608fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 16618fae3551SRodney W. Grimes 1662ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 16638fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1664ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1665ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1666d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1667d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1668d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1669ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1670ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1671a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1672a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1673a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1674a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1675ef9e6dc7SBill Fenner /* dump any option bytes */ 16768fae3551SRodney W. Grimes while (hlen-- > 20) { 16778fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16788fae3551SRodney W. Grimes } 16798fae3551SRodney W. Grimes (void)putchar('\n'); 16808fae3551SRodney W. Grimes } 16818fae3551SRodney W. Grimes 16828fae3551SRodney W. Grimes /* 16838fae3551SRodney W. Grimes * pr_addr -- 16848fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 16858fae3551SRodney W. Grimes * a hostname. 16868fae3551SRodney W. Grimes */ 168743470e3bSGarrett Wollman static char * 1688fafb8f11SEd Schouten pr_addr(struct in_addr ina) 16898fae3551SRodney W. Grimes { 16908fae3551SRodney W. Grimes struct hostent *hp; 1691f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 16928fae3551SRodney W. Grimes 169349133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 169443470e3bSGarrett Wollman return inet_ntoa(ina); 169549133c6dSPawel Jakub Dawidek 169649133c6dSPawel Jakub Dawidek if (capdns != NULL) 169749133c6dSPawel Jakub Dawidek hp = cap_gethostbyaddr(capdns, (char *)&ina, 4, AF_INET); 16988fae3551SRodney W. Grimes else 169949133c6dSPawel Jakub Dawidek hp = gethostbyaddr((char *)&ina, 4, AF_INET); 170049133c6dSPawel Jakub Dawidek 170149133c6dSPawel Jakub Dawidek if (hp == NULL) 170249133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 170349133c6dSPawel Jakub Dawidek 1704efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 170543470e3bSGarrett Wollman inet_ntoa(ina)); 17068fae3551SRodney W. Grimes return(buf); 17078fae3551SRodney W. Grimes } 17088fae3551SRodney W. Grimes 17098fae3551SRodney W. Grimes /* 17108fae3551SRodney W. Grimes * pr_retip -- 17118fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 17128fae3551SRodney W. Grimes */ 171343470e3bSGarrett Wollman static void 1714fafb8f11SEd Schouten pr_retip(struct ip *ip) 17158fae3551SRodney W. Grimes { 17168fae3551SRodney W. Grimes u_char *cp; 1717efc8588dSDavid E. O'Brien int hlen; 17188fae3551SRodney W. Grimes 17198fae3551SRodney W. Grimes pr_iph(ip); 17208fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 17218fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 17228fae3551SRodney W. Grimes 17238fae3551SRodney W. Grimes if (ip->ip_p == 6) 17248fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 17258fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17268fae3551SRodney W. Grimes else if (ip->ip_p == 17) 17278fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 17288fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17298fae3551SRodney W. Grimes } 17308fae3551SRodney W. Grimes 1731eb1543c6SMatthew N. Dodd static char * 1732007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1733eb1543c6SMatthew N. Dodd { 1734eb1543c6SMatthew N. Dodd static char buf[10]; 1735007fe4e3SMaxim Konovalov int hour, min, sec; 1736eb1543c6SMatthew N. Dodd 1737007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1738007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1739007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1740007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1741eb1543c6SMatthew N. Dodd 1742007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1743eb1543c6SMatthew N. Dodd 1744eb1543c6SMatthew N. Dodd return (buf); 1745eb1543c6SMatthew N. Dodd } 1746eb1543c6SMatthew N. Dodd 174743470e3bSGarrett Wollman static void 1748fafb8f11SEd Schouten fill(char *bp, char *patp) 17498fae3551SRodney W. Grimes { 17508fae3551SRodney W. Grimes char *cp; 1751efc8588dSDavid E. O'Brien int pat[16]; 17524fba6582SMaxim Konovalov u_int ii, jj, kk; 17538fae3551SRodney W. Grimes 175443470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 175543470e3bSGarrett Wollman if (!isxdigit(*cp)) 175643470e3bSGarrett Wollman errx(EX_USAGE, 175743470e3bSGarrett Wollman "patterns must be specified as hex digits"); 175843470e3bSGarrett Wollman 17598fae3551SRodney W. Grimes } 17608fae3551SRodney W. Grimes ii = sscanf(patp, 17618fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 17628fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 17638fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 17648fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 17658fae3551SRodney W. Grimes 17668fae3551SRodney W. Grimes if (ii > 0) 1767d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 17688fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17698fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17708fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17718fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17728fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17738fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17748fae3551SRodney W. Grimes (void)printf("\n"); 17758fae3551SRodney W. Grimes } 17768fae3551SRodney W. Grimes } 17778fae3551SRodney W. Grimes 177849133c6dSPawel Jakub Dawidek static cap_channel_t * 177949133c6dSPawel Jakub Dawidek capdns_setup(void) 178049133c6dSPawel Jakub Dawidek { 178149133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 178249133c6dSPawel Jakub Dawidek const char *types[2]; 178349133c6dSPawel Jakub Dawidek int families[1]; 178449133c6dSPawel Jakub Dawidek 178549133c6dSPawel Jakub Dawidek capcas = cap_init(); 1786c501d73cSMariusz Zaborski if (capcas == NULL) 1787c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 178849133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 178949133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 179049133c6dSPawel Jakub Dawidek cap_close(capcas); 179149133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 179249133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 179349133c6dSPawel Jakub Dawidek types[0] = "NAME"; 179449133c6dSPawel Jakub Dawidek types[1] = "ADDR"; 179549133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 179649133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 179749133c6dSPawel Jakub Dawidek families[0] = AF_INET; 179849133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 179949133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 180049133c6dSPawel Jakub Dawidek 180149133c6dSPawel Jakub Dawidek return (capdnsloc); 180249133c6dSPawel Jakub Dawidek } 180349133c6dSPawel Jakub Dawidek 1804120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 1805120b4a93SRuslan Ermilov #define SECOPT " [-P policy]" 1806120b4a93SRuslan Ermilov #else 1807120b4a93SRuslan Ermilov #define SECOPT "" 1808120b4a93SRuslan Ermilov #endif 180943470e3bSGarrett Wollman static void 1810fafb8f11SEd Schouten usage(void) 18118fae3551SRodney W. Grimes { 181231eac03bSSean Chittenden 1813d6cd1497SGleb Smirnoff (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 1814ee3e1c4cSRuslan Ermilov "usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]", 1815ee3e1c4cSRuslan Ermilov " [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]", 1816ee3e1c4cSRuslan Ermilov " " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]", 1817d6cd1497SGleb Smirnoff " [-W waittime] [-z tos] host", 18181bd10ba2SRuslan Ermilov " ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]", 1819120b4a93SRuslan Ermilov " [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]", 1820d6cd1497SGleb Smirnoff " [-s packetsize] [-T ttl] [-t timeout] [-W waittime]", 1821d6cd1497SGleb Smirnoff " [-z tos] mcast-group"); 182243470e3bSGarrett Wollman exit(EX_USAGE); 18238fae3551SRodney W. Grimes } 1824