18a16b7a1SPedro F. Giffuni /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 48fae3551SRodney W. Grimes * Copyright (c) 1989, 1993 58fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 68fae3551SRodney W. Grimes * 78fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 88fae3551SRodney W. Grimes * Mike Muuss. 98fae3551SRodney W. Grimes * 108fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 118fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 128fae3551SRodney W. Grimes * are met: 138fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 158fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 168fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 178fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 198fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 208fae3551SRodney W. Grimes * without specific prior written permission. 218fae3551SRodney W. Grimes * 228fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 238fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 248fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 258fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 268fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 278fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 288fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 298fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 308fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 318fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 328fae3551SRodney W. Grimes * SUCH DAMAGE. 338fae3551SRodney W. Grimes */ 348fae3551SRodney W. Grimes 35c69284caSDavid E. O'Brien #if 0 368fae3551SRodney W. Grimes #ifndef lint 3743470e3bSGarrett Wollman static const char copyright[] = 388fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\ 398fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 408fae3551SRodney W. Grimes #endif /* not lint */ 418fae3551SRodney W. Grimes 428fae3551SRodney W. Grimes #ifndef lint 438fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c 8.1 (Berkeley) 6/5/93"; 448fae3551SRodney W. Grimes #endif /* not lint */ 45c69284caSDavid E. O'Brien #endif 46c69284caSDavid E. O'Brien #include <sys/cdefs.h> 47c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 488fae3551SRodney W. Grimes 498fae3551SRodney W. Grimes /* 508fae3551SRodney W. Grimes * P I N G . C 518fae3551SRodney W. Grimes * 52e345a80dSPhilippe Charnier * Using the Internet Control Message Protocol (ICMP) "ECHO" facility, 538fae3551SRodney W. Grimes * measure round-trip-delays and packet loss across network paths. 548fae3551SRodney W. Grimes * 558fae3551SRodney W. Grimes * Author - 568fae3551SRodney W. Grimes * Mike Muuss 578fae3551SRodney W. Grimes * U. S. Army Ballistic Research Laboratory 588fae3551SRodney W. Grimes * December, 1983 598fae3551SRodney W. Grimes * 608fae3551SRodney W. Grimes * Status - 618fae3551SRodney W. Grimes * Public Domain. Distribution Unlimited. 628fae3551SRodney W. Grimes * Bugs - 638fae3551SRodney W. Grimes * More statistics could always be gathered. 648fae3551SRodney W. Grimes * This program has to run SUID to ROOT to access the ICMP socket. 658fae3551SRodney W. Grimes */ 668fae3551SRodney W. Grimes 6743470e3bSGarrett Wollman #include <sys/param.h> /* NB: we rely on this for <sys/types.h> */ 68b881b8beSRobert Watson #include <sys/capsicum.h> 698fae3551SRodney W. Grimes #include <sys/socket.h> 70261e59bbSMaxim Konovalov #include <sys/sysctl.h> 718fae3551SRodney W. Grimes #include <sys/time.h> 72039d6aa4SBill Fenner #include <sys/uio.h> 738fae3551SRodney W. Grimes 748fae3551SRodney W. Grimes #include <netinet/in.h> 7543470e3bSGarrett Wollman #include <netinet/in_systm.h> 768fae3551SRodney W. Grimes #include <netinet/ip.h> 778fae3551SRodney W. Grimes #include <netinet/ip_icmp.h> 788fae3551SRodney W. Grimes #include <netinet/ip_var.h> 7943470e3bSGarrett Wollman #include <arpa/inet.h> 80c501d73cSMariusz Zaborski 81c501d73cSMariusz Zaborski #include <libcasper.h> 82c501d73cSMariusz Zaborski #include <casper/cap_dns.h> 838fae3551SRodney W. Grimes 849a4365d0SYoshinobu Inoue #ifdef IPSEC 858409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h> 869a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 879a4365d0SYoshinobu Inoue 887bdc3291SMark Johnston #include <capsicum_helpers.h> 89261e59bbSMaxim Konovalov #include <ctype.h> 90261e59bbSMaxim Konovalov #include <err.h> 91261e59bbSMaxim Konovalov #include <errno.h> 92261e59bbSMaxim Konovalov #include <math.h> 93261e59bbSMaxim Konovalov #include <netdb.h> 94d9cacf60SAlan Somers #include <stddef.h> 95261e59bbSMaxim Konovalov #include <signal.h> 96261e59bbSMaxim Konovalov #include <stdio.h> 97261e59bbSMaxim Konovalov #include <stdlib.h> 98261e59bbSMaxim Konovalov #include <string.h> 99261e59bbSMaxim Konovalov #include <sysexits.h> 1001ad76f1bSAlan Somers #include <time.h> 101261e59bbSMaxim Konovalov #include <unistd.h> 102261e59bbSMaxim Konovalov 1033cde9171SAlan Somers #include "main.h" 1043cde9171SAlan Somers #include "ping.h" 105ff77ab83SAlan Somers #include "utils.h" 106ff77ab83SAlan Somers 107301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10813e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 109eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 110eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 111c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1128f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1138f975bb3SBruce Evans /* runs out of buffer space */ 1144fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1154fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 116d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 117bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1180b2f8b3fSMaxim Konovalov #define MAXTOS 255 1198fae3551SRodney W. Grimes 1208fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1218fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1228fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1238fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1248fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1258fae3551SRodney W. Grimes 12613e3f0b7SMaxim Konovalov struct tv32 { 12713e3f0b7SMaxim Konovalov int32_t tv32_sec; 1281ad76f1bSAlan Somers int32_t tv32_nsec; 12913e3f0b7SMaxim Konovalov }; 13013e3f0b7SMaxim Konovalov 1318fae3551SRodney W. Grimes /* various options */ 1327e9489e0SHiroki Sato static int options; 13385456935SBill Fenner #define F_FLOOD 0x0001 13485456935SBill Fenner #define F_INTERVAL 0x0002 13585456935SBill Fenner #define F_NUMERIC 0x0004 13685456935SBill Fenner #define F_PINGFILLED 0x0008 13785456935SBill Fenner #define F_QUIET 0x0010 13885456935SBill Fenner #define F_RROUTE 0x0020 13985456935SBill Fenner #define F_SO_DEBUG 0x0040 14085456935SBill Fenner #define F_SO_DONTROUTE 0x0080 14185456935SBill Fenner #define F_VERBOSE 0x0100 14285456935SBill Fenner #define F_QUIET2 0x0200 14385456935SBill Fenner #define F_NOLOOP 0x0400 14485456935SBill Fenner #define F_MTTL 0x0800 14585456935SBill Fenner #define F_MIF 0x1000 146772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1479a4365d0SYoshinobu Inoue #ifdef IPSEC 1489a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1499a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1509a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1519a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 152211bfbd2SRuslan Ermilov #define F_TTL 0x8000 153ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1548025c44bSDima Dorfman #define F_ONCE 0x20000 1550b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 156143008a1SMatthew N. Dodd #define F_MASK 0x80000 157eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1589ff95228SGleb Smirnoff #define F_SWEEP 0x200000 159d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 16081a6f4c7SRichard Scheffenegger #define F_IP_VLAN_PCP 0x800000 161d399eb3eSPiotr Pawel Stefaniak #define F_DOT 0x1000000 1628fae3551SRodney W. Grimes 1638fae3551SRodney W. Grimes /* 1648fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1658fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1668fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1678fae3551SRodney W. Grimes */ 1688fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1697e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1707e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1718fae3551SRodney W. Grimes 1727e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1737e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1747e9489e0SHiroki Sato static int maxpayload; 1757e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1767e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1777e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1787e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1797e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 180d399eb3eSPiotr Pawel Stefaniak static const char *DOT = "."; 181d399eb3eSPiotr Pawel Stefaniak static size_t DOTlen = 1; 182d399eb3eSPiotr Pawel Stefaniak static size_t DOTidx = 0; 1837e9489e0SHiroki Sato static char *hostname; 1847e9489e0SHiroki Sato static char *shostname; 1857e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1867e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1877e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1887e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1897e9489e0SHiroki Sato static int phdr_len = 0; 1907e9489e0SHiroki Sato static int send_len; 1918fae3551SRodney W. Grimes 1928fae3551SRodney W. Grimes /* counters */ 1937e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1947e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1957e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1967e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1977e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1987e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1997e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 2007e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 2017e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 2027e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 2037e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 2047e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 2057e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 2068fae3551SRodney W. Grimes 2078fae3551SRodney W. Grimes /* timing */ 2087e9489e0SHiroki Sato static int timing; /* flag to do timing */ 2097e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 2107e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2117e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2127e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2138fae3551SRodney W. Grimes 2147e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2157e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2167e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 217badd8138SSean Eric Fagan 21849133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 21949133c6dSPawel Jakub Dawidek 22043470e3bSGarrett Wollman static void fill(char *, char *); 22149133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 22243470e3bSGarrett Wollman static void check_status(void); 2238f975bb3SBruce Evans static void finish(void) __dead2; 22443470e3bSGarrett Wollman static void pinger(void); 22543470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 226eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 227d9cacf60SAlan Somers static void pr_icmph(struct icmp *, struct ip *, const u_char *const); 22843470e3bSGarrett Wollman static void pr_iph(struct ip *); 229d9cacf60SAlan Somers static void pr_pack(char *, ssize_t, struct sockaddr_in *, struct timespec *); 230d9cacf60SAlan Somers static void pr_retip(struct ip *, const u_char *); 23143470e3bSGarrett Wollman static void status(int); 2328f975bb3SBruce Evans static void stopit(int); 2338fae3551SRodney W. Grimes 23443470e3bSGarrett Wollman int 2353cde9171SAlan Somers ping(int argc, char *const *argv) 2368fae3551SRodney W. Grimes { 23731eac03bSSean Chittenden struct sockaddr_in from, sock_in; 238efc8588dSDavid E. O'Brien struct in_addr ifaddr; 2391ad76f1bSAlan Somers struct timespec last, intvl; 240efc8588dSDavid E. O'Brien struct iovec iov; 241efc8588dSDavid E. O'Brien struct msghdr msg; 242efc8588dSDavid E. O'Brien struct sigaction si_sa; 2430b2f8b3fSMaxim Konovalov size_t sz; 244e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 24578e1f68eSMark Johnston const char *errstr; 246d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 247261e59bbSMaxim Konovalov struct hostent *hp; 248efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 249efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 250efc8588dSDavid E. O'Brien #endif 251261e59bbSMaxim Konovalov struct sockaddr_in *to; 252261e59bbSMaxim Konovalov double t; 253c0a3773aSEugene Grosbein u_long alarmtimeout; 25478e1f68eSMark Johnston long long ltmp; 25549133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 25681a6f4c7SRichard Scheffenegger int ssend_errno, srecv_errno, tos, ttl, pcp; 2571ad76f1bSAlan Somers char ctrl[CMSG_SPACE(sizeof(struct timespec))]; 258efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2598fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2604fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2618fae3551SRodney W. Grimes #endif 262261e59bbSMaxim Konovalov unsigned char loop, mttl; 263efc8588dSDavid E. O'Brien 26431eac03bSSean Chittenden payload = source = NULL; 2659a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 266efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2679a4365d0SYoshinobu Inoue #endif 26849133c6dSPawel Jakub Dawidek cap_rights_t rights; 2698fae3551SRodney W. Grimes 270299e2c58SAlan Somers options |= F_NUMERIC; 271299e2c58SAlan Somers 272f1284d7aSBill Fenner /* 273f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 274f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 275f1284d7aSBill Fenner * after arg parsing. 27649133c6dSPawel Jakub Dawidek * 27749133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 27849133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 27949133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 28049133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 28149133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 28249133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 28349133c6dSPawel Jakub Dawidek * packets. 284f1284d7aSBill Fenner */ 28549133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28649133c6dSPawel Jakub Dawidek ssend_errno = errno; 28749133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28849133c6dSPawel Jakub Dawidek srecv_errno = errno; 289f1284d7aSBill Fenner 2901d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2911d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 292ee2bf734SWarner Losh uid = getuid(); 293f1284d7aSBill Fenner 294eeb63943SDon Lewis if (ssend < 0) { 295eeb63943SDon Lewis errno = ssend_errno; 296eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 297eeb63943SDon Lewis } 298eeb63943SDon Lewis 299eeb63943SDon Lewis if (srecv < 0) { 300eeb63943SDon Lewis errno = srecv_errno; 301eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 302eeb63943SDon Lewis } 303eeb63943SDon Lewis 30481a6f4c7SRichard Scheffenegger alarmtimeout = df = preload = tos = pcp = 0; 305badd8138SSean Eric Fagan 3060b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 3079ce201f2SAlan Somers while ((ch = getopt(argc, argv, PING4OPTS)) != -1) { 3088fae3551SRodney W. Grimes switch(ch) { 309d399eb3eSPiotr Pawel Stefaniak case '.': 310d399eb3eSPiotr Pawel Stefaniak options |= F_DOT; 311d399eb3eSPiotr Pawel Stefaniak if (optarg != NULL) { 312d399eb3eSPiotr Pawel Stefaniak DOT = optarg; 313d399eb3eSPiotr Pawel Stefaniak DOTlen = strlen(optarg); 314d399eb3eSPiotr Pawel Stefaniak } 315d399eb3eSPiotr Pawel Stefaniak break; 3163cde9171SAlan Somers case '4': 3173cde9171SAlan Somers /* This option is processed in main(). */ 3183cde9171SAlan Somers break; 319ca517ad8SPoul-Henning Kamp case 'A': 320ca517ad8SPoul-Henning Kamp options |= F_MISSED; 321ca517ad8SPoul-Henning Kamp break; 322772dfa72SDaniel O'Callaghan case 'a': 323772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 324772dfa72SDaniel O'Callaghan break; 32581a6f4c7SRichard Scheffenegger case 'C': 32681a6f4c7SRichard Scheffenegger options |= F_IP_VLAN_PCP; 32778e1f68eSMark Johnston ltmp = strtonum(optarg, -1, 7, &errstr); 32878e1f68eSMark Johnston if (errstr != NULL) 32981a6f4c7SRichard Scheffenegger errx(EX_USAGE, "invalid PCP: `%s'", optarg); 33081a6f4c7SRichard Scheffenegger pcp = ltmp; 33181a6f4c7SRichard Scheffenegger break; 3328fae3551SRodney W. Grimes case 'c': 33378e1f68eSMark Johnston ltmp = strtonum(optarg, 1, LONG_MAX, &errstr); 33478e1f68eSMark Johnston if (errstr != NULL) 33543470e3bSGarrett Wollman errx(EX_USAGE, 33643470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 33743470e3bSGarrett Wollman optarg); 33878e1f68eSMark Johnston npackets = (long)ltmp; 3398fae3551SRodney W. Grimes break; 3400b2f8b3fSMaxim Konovalov case 'D': 3410b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3420b2f8b3fSMaxim Konovalov df = 1; 3430b2f8b3fSMaxim Konovalov break; 3448fae3551SRodney W. Grimes case 'd': 3458fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3468fae3551SRodney W. Grimes break; 3478fae3551SRodney W. Grimes case 'f': 348526f06b2SMatthew Dillon if (uid) { 34943470e3bSGarrett Wollman errno = EPERM; 35043470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3518fae3551SRodney W. Grimes } 3528fae3551SRodney W. Grimes options |= F_FLOOD; 353d399eb3eSPiotr Pawel Stefaniak options |= F_DOT; 3548fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3558fae3551SRodney W. Grimes break; 3569ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 35778e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 35878e1f68eSMark Johnston if (errstr != NULL) { 3599ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3609ff95228SGleb Smirnoff optarg); 36178e1f68eSMark Johnston } 36278e1f68eSMark Johnston sweepmax = (int)ltmp; 36378e1f68eSMark Johnston if (uid != 0 && sweepmax > DEFDATALEN) { 36478e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 36578e1f68eSMark Johnston "packet size too large: %d > %u", 36678e1f68eSMark Johnston sweepmax, DEFDATALEN); 3679ff95228SGleb Smirnoff } 3689ff95228SGleb Smirnoff options |= F_SWEEP; 3699ff95228SGleb Smirnoff break; 3709ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 37178e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 37278e1f68eSMark Johnston if (errstr != NULL) { 3739ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3749ff95228SGleb Smirnoff optarg); 37578e1f68eSMark Johnston } 37678e1f68eSMark Johnston sweepmin = (int)ltmp; 37778e1f68eSMark Johnston if (uid != 0 && sweepmin > DEFDATALEN) { 37878e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 37978e1f68eSMark Johnston "packet size too large: %d > %u", 38078e1f68eSMark Johnston sweepmin, DEFDATALEN); 3819ff95228SGleb Smirnoff } 3829ff95228SGleb Smirnoff options |= F_SWEEP; 3839ff95228SGleb Smirnoff break; 38499f13ae1SAlan Somers case 'H': 38599f13ae1SAlan Somers options &= ~F_NUMERIC; 38699f13ae1SAlan Somers break; 3879ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 38878e1f68eSMark Johnston ltmp = strtonum(optarg, 1, INT_MAX, &errstr); 38978e1f68eSMark Johnston if (errstr != NULL) { 39078e1f68eSMark Johnston errx(EX_USAGE, "invalid packet size: `%s'", 3919ff95228SGleb Smirnoff optarg); 39278e1f68eSMark Johnston } 39378e1f68eSMark Johnston sweepincr = (int)ltmp; 39478e1f68eSMark Johnston if (uid != 0 && sweepincr > DEFDATALEN) { 39578e1f68eSMark Johnston errc(EX_NOPERM, EPERM, 39678e1f68eSMark Johnston "packet size too large: %d > %u", 39778e1f68eSMark Johnston sweepincr, DEFDATALEN); 3989ff95228SGleb Smirnoff } 3999ff95228SGleb Smirnoff options |= F_SWEEP; 4009ff95228SGleb Smirnoff break; 4011f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 4021f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 4031f6a4631SRuslan Ermilov errx(EX_USAGE, 4041f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 4051f6a4631SRuslan Ermilov optarg); 4061f6a4631SRuslan Ermilov options |= F_MIF; 4071f6a4631SRuslan Ermilov break; 4088fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 4091ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 4101ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 4111ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 4121ad0b1beSMaxim Konovalov optarg); 4138fae3551SRodney W. Grimes options |= F_INTERVAL; 414526f06b2SMatthew Dillon interval = (int)t; 415526f06b2SMatthew Dillon if (uid && interval < 1000) { 416526f06b2SMatthew Dillon errno = EPERM; 417526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 418526f06b2SMatthew Dillon } 4198fae3551SRodney W. Grimes break; 4201f6a4631SRuslan Ermilov case 'L': 4211f6a4631SRuslan Ermilov options |= F_NOLOOP; 4221f6a4631SRuslan Ermilov loop = 0; 42385456935SBill Fenner break; 4248fae3551SRodney W. Grimes case 'l': 42578e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 42678e1f68eSMark Johnston if (errstr != NULL) 42743470e3bSGarrett Wollman errx(EX_USAGE, 42843470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4295e2cc0f4SStephen McKay if (uid) { 430f78ac61bSWarner Losh errno = EPERM; 431f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 432f78ac61bSWarner Losh } 43378e1f68eSMark Johnston preload = (int)ltmp; 4348fae3551SRodney W. Grimes break; 4351f6a4631SRuslan Ermilov case 'M': 436eb1543c6SMatthew N. Dodd switch(optarg[0]) { 437eb1543c6SMatthew N. Dodd case 'M': 438eb1543c6SMatthew N. Dodd case 'm': 4391f6a4631SRuslan Ermilov options |= F_MASK; 44085456935SBill Fenner break; 441eb1543c6SMatthew N. Dodd case 'T': 442eb1543c6SMatthew N. Dodd case 't': 443eb1543c6SMatthew N. Dodd options |= F_TIME; 444eb1543c6SMatthew N. Dodd break; 445eb1543c6SMatthew N. Dodd default: 446eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 447eb1543c6SMatthew N. Dodd break; 448eb1543c6SMatthew N. Dodd } 449eb1543c6SMatthew N. Dodd break; 450211bfbd2SRuslan Ermilov case 'm': /* TTL */ 45178e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 45278e1f68eSMark Johnston if (errstr != NULL) 4531ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 45478e1f68eSMark Johnston ttl = (int)ltmp; 455211bfbd2SRuslan Ermilov options |= F_TTL; 456211bfbd2SRuslan Ermilov break; 4578fae3551SRodney W. Grimes case 'n': 4588fae3551SRodney W. Grimes options |= F_NUMERIC; 4598fae3551SRodney W. Grimes break; 4608025c44bSDima Dorfman case 'o': 4618025c44bSDima Dorfman options |= F_ONCE; 4628025c44bSDima Dorfman break; 4631f6a4631SRuslan Ermilov #ifdef IPSEC 4641f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4651f6a4631SRuslan Ermilov case 'P': 4661f6a4631SRuslan Ermilov options |= F_POLICY; 4671f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4681f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4691f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4701f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4711f6a4631SRuslan Ermilov else 4721f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4731f6a4631SRuslan Ermilov break; 4741f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4751f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4768fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4778fae3551SRodney W. Grimes options |= F_PINGFILLED; 478d074d39fSMatthew N. Dodd payload = optarg; 4798fae3551SRodney W. Grimes break; 480ef9e6dc7SBill Fenner case 'Q': 481ef9e6dc7SBill Fenner options |= F_QUIET2; 482ef9e6dc7SBill Fenner break; 4838fae3551SRodney W. Grimes case 'q': 4848fae3551SRodney W. Grimes options |= F_QUIET; 4858fae3551SRodney W. Grimes break; 4868fae3551SRodney W. Grimes case 'R': 4878fae3551SRodney W. Grimes options |= F_RROUTE; 4888fae3551SRodney W. Grimes break; 4898fae3551SRodney W. Grimes case 'r': 4908fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4918fae3551SRodney W. Grimes break; 4921f6a4631SRuslan Ermilov case 'S': 4931f6a4631SRuslan Ermilov source = optarg; 4941f6a4631SRuslan Ermilov break; 4958fae3551SRodney W. Grimes case 's': /* size of packet to send */ 49678e1f68eSMark Johnston ltmp = strtonum(optarg, 0, INT_MAX, &errstr); 49778e1f68eSMark Johnston if (errstr != NULL) 49843470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 49943470e3bSGarrett Wollman optarg); 50078e1f68eSMark Johnston datalen = (int)ltmp; 50178e1f68eSMark Johnston if (uid != 0 && datalen > DEFDATALEN) { 502fb7d32c7SMaxim Konovalov errno = EPERM; 503fb7d32c7SMaxim Konovalov err(EX_NOPERM, 50478e1f68eSMark Johnston "packet size too large: %d > %u", 50578e1f68eSMark Johnston datalen, DEFDATALEN); 506fb7d32c7SMaxim Konovalov } 5078fae3551SRodney W. Grimes break; 5081f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 50978e1f68eSMark Johnston ltmp = strtonum(optarg, 0, MAXTTL, &errstr); 51078e1f68eSMark Johnston if (errstr != NULL) 5111f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 5121f6a4631SRuslan Ermilov optarg); 51378e1f68eSMark Johnston mttl = (unsigned char)ltmp; 5141f6a4631SRuslan Ermilov options |= F_MTTL; 51599490edeSWarner Losh break; 5167237fd94SBill Fumerola case 't': 517bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 518bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 5197237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 5207237fd94SBill Fumerola optarg); 521bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 522bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 523bf113f1bSBill Fumerola optarg, MAXALARM); 5242eb6acc2SAlan Somers { 5252eb6acc2SAlan Somers struct itimerval itv; 5262eb6acc2SAlan Somers 5272eb6acc2SAlan Somers timerclear(&itv.it_interval); 5282eb6acc2SAlan Somers timerclear(&itv.it_value); 5292eb6acc2SAlan Somers itv.it_value.tv_sec = (time_t)alarmtimeout; 5302eb6acc2SAlan Somers if (setitimer(ITIMER_REAL, &itv, NULL) != 0) 5312eb6acc2SAlan Somers err(1, "setitimer"); 5322eb6acc2SAlan Somers } 5337237fd94SBill Fumerola break; 5348fae3551SRodney W. Grimes case 'v': 5358fae3551SRodney W. Grimes options |= F_VERBOSE; 5368fae3551SRodney W. Grimes break; 537d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 538d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 539d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 540d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 541d6cd1497SGleb Smirnoff optarg); 542d6cd1497SGleb Smirnoff options |= F_WAITTIME; 543d6cd1497SGleb Smirnoff waittime = (int)t; 544d6cd1497SGleb Smirnoff break; 5450b2f8b3fSMaxim Konovalov case 'z': 5460b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 547c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 548c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5490b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 550c0a3773aSEugene Grosbein tos = ltmp; 5510b2f8b3fSMaxim Konovalov break; 5528fae3551SRodney W. Grimes default: 553e345a80dSPhilippe Charnier usage(); 55443470e3bSGarrett Wollman } 55543470e3bSGarrett Wollman } 55643470e3bSGarrett Wollman 55743470e3bSGarrett Wollman if (argc - optind != 1) 558e345a80dSPhilippe Charnier usage(); 55943470e3bSGarrett Wollman target = argv[optind]; 5608fae3551SRodney W. Grimes 561d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 562d829c3dfSMatthew N. Dodd case 0: break; 563d829c3dfSMatthew N. Dodd case F_MASK: 564eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 565eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 566d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 567eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 568eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 569d829c3dfSMatthew N. Dodd break; 570d829c3dfSMatthew N. Dodd case F_TIME: 571eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 572eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 573d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 574eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 575eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 576d829c3dfSMatthew N. Dodd break; 577d829c3dfSMatthew N. Dodd default: 578d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 579d829c3dfSMatthew N. Dodd break; 580eb1543c6SMatthew N. Dodd } 5810fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 582fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 583e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 584e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 585fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5861104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 587fb7d32c7SMaxim Konovalov maxpayload); 588e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5890fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 590d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 591d074d39fSMatthew N. Dodd fill((char *)datap, payload); 592d074d39fSMatthew N. Dodd } 59349133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 59499490edeSWarner Losh if (source) { 59531eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 59631eac03bSSean Chittenden sock_in.sin_family = AF_INET; 59731eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 59899490edeSWarner Losh shostname = source; 59999490edeSWarner Losh } else { 600d68e2c04SMariusz Zaborski hp = cap_gethostbyname2(capdns, source, AF_INET); 60199490edeSWarner Losh if (!hp) 60299490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 60399490edeSWarner Losh source, hstrerror(h_errno)); 60499490edeSWarner Losh 60531eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 60631eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 6074fba6582SMaxim Konovalov hp->h_length < 0) 60899490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 60931eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 61031eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 61199490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 61299490edeSWarner Losh sizeof(snamebuf) - 1); 61399490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 61499490edeSWarner Losh shostname = snamebuf; 61599490edeSWarner Losh } 61649133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 61749133c6dSPawel Jakub Dawidek -1) 61899490edeSWarner Losh err(1, "bind"); 61999490edeSWarner Losh } 62099490edeSWarner Losh 621d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 622d389e86aSMatt Jacob to = &whereto; 6238fae3551SRodney W. Grimes to->sin_family = AF_INET; 624d389e86aSMatt Jacob to->sin_len = sizeof *to; 62543470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 6268fae3551SRodney W. Grimes hostname = target; 62743470e3bSGarrett Wollman } else { 62849133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 62943470e3bSGarrett Wollman if (!hp) 63043470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 63143470e3bSGarrett Wollman target, hstrerror(h_errno)); 63243470e3bSGarrett Wollman 63331eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6341ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 63543470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6368fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 637b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6388fae3551SRodney W. Grimes hostname = hnamebuf; 6398fae3551SRodney W. Grimes } 6408fae3551SRodney W. Grimes 64149133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 642a3ce7698SAlan Somers #ifdef WITH_CASPER 64349133c6dSPawel Jakub Dawidek if (capdns != NULL) { 64449133c6dSPawel Jakub Dawidek const char *types[1]; 64549133c6dSPawel Jakub Dawidek 646752d135eSMariusz Zaborski types[0] = "ADDR2NAME"; 64749133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 64849133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 64949133c6dSPawel Jakub Dawidek } 650a3ce7698SAlan Somers #endif 65149133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 65249133c6dSPawel Jakub Dawidek err(1, "connect"); 65349133c6dSPawel Jakub Dawidek 65443470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 65543470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 65643470e3bSGarrett Wollman 65743470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 65843470e3bSGarrett Wollman errx(EX_USAGE, 65943470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 66043470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 66143470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 66243470e3bSGarrett Wollman errx(EX_USAGE, 66343470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6648fae3551SRodney W. Grimes 665d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6668fae3551SRodney W. Grimes timing = 1; 66743470e3bSGarrett Wollman 66878e1f68eSMark Johnston if ((options & (F_PINGFILLED | F_SWEEP)) == 0) 669d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6708fae3551SRodney W. Grimes *datap++ = i; 6718fae3551SRodney W. Grimes 6728fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6738fae3551SRodney W. Grimes 6748fae3551SRodney W. Grimes hold = 1; 67549133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 67649133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6778fae3551SRodney W. Grimes sizeof(hold)); 67849133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 67949133c6dSPawel Jakub Dawidek sizeof(hold)); 68049133c6dSPawel Jakub Dawidek } 6818fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 68249133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6838fae3551SRodney W. Grimes sizeof(hold)); 68481a6f4c7SRichard Scheffenegger if (options & F_IP_VLAN_PCP) { 68581a6f4c7SRichard Scheffenegger (void)setsockopt(ssend, IPPROTO_IP, IP_VLAN_PCP, (char *)&pcp, 68681a6f4c7SRichard Scheffenegger sizeof(pcp)); 68781a6f4c7SRichard Scheffenegger } 6889a4365d0SYoshinobu Inoue #ifdef IPSEC 6899a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6909a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6919a4365d0SYoshinobu Inoue char *buf; 6929a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6939a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6949a4365d0SYoshinobu Inoue if (buf == NULL) 695ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 69649133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6979a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6981ad0b1beSMaxim Konovalov err(EX_CONFIG, 6991ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 7009a4365d0SYoshinobu Inoue free(buf); 7019a4365d0SYoshinobu Inoue } 7029a4365d0SYoshinobu Inoue 7039a4365d0SYoshinobu Inoue if (policy_out != NULL) { 7049a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 7059a4365d0SYoshinobu Inoue if (buf == NULL) 706ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 70749133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 7089a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 7091ad0b1beSMaxim Konovalov err(EX_CONFIG, 7101ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 7119a4365d0SYoshinobu Inoue free(buf); 7129a4365d0SYoshinobu Inoue } 7139a4365d0SYoshinobu Inoue } 7149a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 7159a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 7168fae3551SRodney W. Grimes 7170b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 718d9cacf60SAlan Somers struct ip ip; 719d9cacf60SAlan Somers 720d9cacf60SAlan Somers memcpy(&ip, outpackhdr, sizeof(ip)); 7210b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 7220b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 7230b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 7240b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 7250b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 7260b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 7270b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 7280b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 7290b2f8b3fSMaxim Konovalov } 73049133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 731d9cacf60SAlan Somers ip.ip_v = IPVERSION; 732d9cacf60SAlan Somers ip.ip_hl = sizeof(struct ip) >> 2; 733d9cacf60SAlan Somers ip.ip_tos = tos; 734d9cacf60SAlan Somers ip.ip_id = 0; 735d9cacf60SAlan Somers ip.ip_off = htons(df ? IP_DF : 0); 736d9cacf60SAlan Somers ip.ip_ttl = ttl; 737d9cacf60SAlan Somers ip.ip_p = IPPROTO_ICMP; 738d9cacf60SAlan Somers ip.ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 739d9cacf60SAlan Somers ip.ip_dst = to->sin_addr; 740d9cacf60SAlan Somers memcpy(outpackhdr, &ip, sizeof(ip)); 7410b2f8b3fSMaxim Konovalov } 74249133c6dSPawel Jakub Dawidek 74349133c6dSPawel Jakub Dawidek /* 74449133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 74549133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 74649133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 74749133c6dSPawel Jakub Dawidek */ 7487bdc3291SMark Johnston caph_cache_catpages(); 74918fcfaa4SMark Johnston if (caph_enter_casper() < 0) 750301bc9f9SAlan Somers err(1, "caph_enter_casper"); 75149133c6dSPawel Jakub Dawidek 75249133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 7537bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 75449133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 75549133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 7567bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 75749133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 75849133c6dSPawel Jakub Dawidek 7598fae3551SRodney W. Grimes /* record route option */ 7608fae3551SRodney W. Grimes if (options & F_RROUTE) { 7618fae3551SRodney W. Grimes #ifdef IP_OPTIONS 762039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7638fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7648fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7658fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 766039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 76749133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 76843470e3bSGarrett Wollman sizeof(rspace)) < 0) 76943470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7708fae3551SRodney W. Grimes #else 77143470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 77243470e3bSGarrett Wollman "record route not available in this implementation"); 7738fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7748fae3551SRodney W. Grimes } 7758fae3551SRodney W. Grimes 776211bfbd2SRuslan Ermilov if (options & F_TTL) { 77749133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 778211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 779211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 780211bfbd2SRuslan Ermilov } 781211bfbd2SRuslan Ermilov } 78285456935SBill Fenner if (options & F_NOLOOP) { 78349133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 78485456935SBill Fenner sizeof(loop)) < 0) { 78543470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 78685456935SBill Fenner } 78785456935SBill Fenner } 78885456935SBill Fenner if (options & F_MTTL) { 78949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 790211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 79143470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 79285456935SBill Fenner } 79385456935SBill Fenner } 79485456935SBill Fenner if (options & F_MIF) { 79549133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 79685456935SBill Fenner sizeof(ifaddr)) < 0) { 79743470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 79885456935SBill Fenner } 79985456935SBill Fenner } 800039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 80184633ef1SAlan Somers { 80284633ef1SAlan Somers int on = 1; 80384633ef1SAlan Somers int ts_clock = SO_TS_MONOTONIC; 80484633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, 80584633ef1SAlan Somers sizeof(on)) < 0) 806039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 80784633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TS_CLOCK, &ts_clock, 80884633ef1SAlan Somers sizeof(ts_clock)) < 0) 80984633ef1SAlan Somers err(EX_OSERR, "setsockopt SO_TS_CLOCK"); 810039d6aa4SBill Fenner } 811039d6aa4SBill Fenner #endif 8129ff95228SGleb Smirnoff if (sweepmax) { 813bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 81478e1f68eSMark Johnston errx(EX_USAGE, 81578e1f68eSMark Johnston "Maximum packet size must be no less than the minimum packet size"); 81678e1f68eSMark Johnston 81778e1f68eSMark Johnston if (sweepmax > maxpayload - TIMEVAL_LEN) 81878e1f68eSMark Johnston errx(EX_USAGE, "Invalid sweep maximum"); 8199ff95228SGleb Smirnoff 8209ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 82178e1f68eSMark Johnston errx(EX_USAGE, 82278e1f68eSMark Johnston "Packet size and ping sweep are mutually exclusive"); 8239ff95228SGleb Smirnoff 8249ff95228SGleb Smirnoff if (npackets > 0) { 8259ff95228SGleb Smirnoff snpackets = npackets; 8269ff95228SGleb Smirnoff npackets = 0; 8279ff95228SGleb Smirnoff } else 8289ff95228SGleb Smirnoff snpackets = 1; 8299ff95228SGleb Smirnoff datalen = sweepmin; 8309ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 8319ff95228SGleb Smirnoff } 8329ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 8339ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 83485456935SBill Fenner 8358fae3551SRodney W. Grimes /* 8368fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 8378fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 8388fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 83943470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 84043470e3bSGarrett Wollman * or multicast pings if they wish. 8418fae3551SRodney W. Grimes */ 8424fba6582SMaxim Konovalov 8434fba6582SMaxim Konovalov /* 8444fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8454fba6582SMaxim Konovalov * as well. 8464fba6582SMaxim Konovalov */ 8474fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 84849133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8498fae3551SRodney W. Grimes sizeof(hold)); 85049133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 85149133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 8527bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 85349133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 854261e59bbSMaxim Konovalov if (uid == 0) 85549133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 856e8bd25ceSRobert Watson sizeof(hold)); 85749133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 85849133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 8597bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 86049133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 861e8bd25ceSRobert Watson 86299490edeSWarner Losh if (to->sin_family == AF_INET) { 86399490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 86499490edeSWarner Losh inet_ntoa(to->sin_addr)); 86599490edeSWarner Losh if (source) 86699490edeSWarner Losh (void)printf(" from %s", shostname); 8679ff95228SGleb Smirnoff if (sweepmax) 8689ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8699ff95228SGleb Smirnoff sweepmin, sweepmax); 8709ff95228SGleb Smirnoff else 87199490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8729ff95228SGleb Smirnoff 8739ff95228SGleb Smirnoff } else { 8749ff95228SGleb Smirnoff if (sweepmax) 8759ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8769ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8779ff95228SGleb Smirnoff else 8788fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8799ff95228SGleb Smirnoff } 8808fae3551SRodney W. Grimes 8817d81b35cSBruce Evans /* 882a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 883a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8847d81b35cSBruce Evans */ 885a2a00888SSean Eric Fagan 886f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 88737e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 888a2a00888SSean Eric Fagan 889a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 890a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 891a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 892a2a00888SSean Eric Fagan } 893a2a00888SSean Eric Fagan 894a2a00888SSean Eric Fagan si_sa.sa_handler = status; 89537e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 896624ff938SWarner Losh err(EX_OSERR, "sigaction"); 89737e5b2c6SSean Eric Fagan } 898bf113f1bSBill Fumerola 899bf113f1bSBill Fumerola if (alarmtimeout > 0) { 9007237fd94SBill Fumerola si_sa.sa_handler = stopit; 9017237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 9027237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 903bf113f1bSBill Fumerola } 90437e5b2c6SSean Eric Fagan 905039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 906039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 907039d6aa4SBill Fenner msg.msg_iov = &iov; 908039d6aa4SBill Fenner msg.msg_iovlen = 1; 909039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 910039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 91167511a4cSAlan Somers msg.msg_controllen = sizeof(ctrl); 912039d6aa4SBill Fenner #endif 913039d6aa4SBill Fenner iov.iov_base = packet; 914e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 915039d6aa4SBill Fenner 91632af342fSRuslan Ermilov if (preload == 0) 91732af342fSRuslan Ermilov pinger(); /* send the first ping */ 91832af342fSRuslan Ermilov else { 91932af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 92032af342fSRuslan Ermilov preload = npackets; 9218fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 9228fae3551SRodney W. Grimes pinger(); 92332af342fSRuslan Ermilov } 9241ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 9258fae3551SRodney W. Grimes 926039d6aa4SBill Fenner if (options & F_FLOOD) { 927039d6aa4SBill Fenner intvl.tv_sec = 0; 9281ad76f1bSAlan Somers intvl.tv_nsec = 10000000; 929039d6aa4SBill Fenner } else { 930526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 9311ad76f1bSAlan Somers intvl.tv_nsec = interval % 1000 * 1000000; 932039d6aa4SBill Fenner } 933039d6aa4SBill Fenner 934261e59bbSMaxim Konovalov almost_done = 0; 9358f975bb3SBruce Evans while (!finish_up) { 9361ad76f1bSAlan Somers struct timespec now, timeout; 937039d6aa4SBill Fenner fd_set rfds; 938d9cacf60SAlan Somers int n; 939d9cacf60SAlan Somers ssize_t cc; 9408fae3551SRodney W. Grimes 9417d81b35cSBruce Evans check_status(); 94249133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 9437e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 944039d6aa4SBill Fenner FD_ZERO(&rfds); 94549133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 9461ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 9471ad76f1bSAlan Somers timespecadd(&last, &intvl, &timeout); 9481ad76f1bSAlan Somers timespecsub(&timeout, &now, &timeout); 949039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9501ad76f1bSAlan Somers timespecclear(&timeout); 9511ad76f1bSAlan Somers n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL); 9525e2cc0f4SStephen McKay if (n < 0) 9535e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 954039d6aa4SBill Fenner if (n == 1) { 9551ad76f1bSAlan Somers struct timespec *tv = NULL; 956039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 95767511a4cSAlan Somers struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); 958039d6aa4SBill Fenner #endif 959039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 96049133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9618fae3551SRodney W. Grimes if (errno == EINTR) 9628fae3551SRodney W. Grimes continue; 963e345a80dSPhilippe Charnier warn("recvmsg"); 9648fae3551SRodney W. Grimes continue; 9658fae3551SRodney W. Grimes } 96646d7b45aSTom Jones /* If we have a 0 byte read from recvfrom continue */ 96746d7b45aSTom Jones if (cc == 0) 96846d7b45aSTom Jones continue; 969039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 970b17fb992SAlan Somers if (cmsg != NULL && 971b17fb992SAlan Somers cmsg->cmsg_level == SOL_SOCKET && 972039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 97331eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 974fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 975fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 97631eac03bSSean Chittenden tv = &now; 977fa05a94cSJohn Birrell } 978039d6aa4SBill Fenner #endif 97931eac03bSSean Chittenden if (tv == NULL) { 9801ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 98131eac03bSSean Chittenden tv = &now; 982039d6aa4SBill Fenner } 98331eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 98431eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 98531eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9868fae3551SRodney W. Grimes break; 9878fae3551SRodney W. Grimes } 9885e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9899ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 99078e1f68eSMark Johnston if (datalen + sweepincr > sweepmax) 99178e1f68eSMark Johnston break; 99278e1f68eSMark Johnston for (i = 0; i < sweepincr; i++) 9939ff95228SGleb Smirnoff *datap++ = i; 9949ff95228SGleb Smirnoff datalen += sweepincr; 9959ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9969ff95228SGleb Smirnoff sntransmitted = 0; 9979ff95228SGleb Smirnoff } 998039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 999039d6aa4SBill Fenner pinger(); 1000039d6aa4SBill Fenner else { 1001039d6aa4SBill Fenner if (almost_done) 1002039d6aa4SBill Fenner break; 1003039d6aa4SBill Fenner almost_done = 1; 10041ad76f1bSAlan Somers intvl.tv_nsec = 0; 1005039d6aa4SBill Fenner if (nreceived) { 1006039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 1007039d6aa4SBill Fenner if (!intvl.tv_sec) 1008039d6aa4SBill Fenner intvl.tv_sec = 1; 1009d6cd1497SGleb Smirnoff } else { 1010d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 10111ad76f1bSAlan Somers intvl.tv_nsec = waittime % 1000 * 1000000; 1012d6cd1497SGleb Smirnoff } 1013039d6aa4SBill Fenner } 10141ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 101525107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 101625107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 101725107197SIan Dowse if (options & F_MISSED) 1018ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1019039d6aa4SBill Fenner } 1020039d6aa4SBill Fenner } 102125107197SIan Dowse } 10228f975bb3SBruce Evans finish(); 10238fae3551SRodney W. Grimes /* NOTREACHED */ 1024f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 10258fae3551SRodney W. Grimes } 10268fae3551SRodney W. Grimes 10278fae3551SRodney W. Grimes /* 10288f975bb3SBruce Evans * stopit -- 10298f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 10308f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 10318f975bb3SBruce Evans * to be called from a signal handler. 1032515dd2faSJulian Elischer */ 1033515dd2faSJulian Elischer void 1034fafb8f11SEd Schouten stopit(int sig __unused) 1035515dd2faSJulian Elischer { 1036efc8588dSDavid E. O'Brien 1037c8bb99e5SIan Dowse /* 1038c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 1039c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 1040c8bb99e5SIan Dowse */ 1041c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 1042c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 1043515dd2faSJulian Elischer finish_up = 1; 1044515dd2faSJulian Elischer } 1045515dd2faSJulian Elischer 1046515dd2faSJulian Elischer /* 10478fae3551SRodney W. Grimes * pinger -- 10488fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10498fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1050eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10511ad76f1bSAlan Somers * bytes of the data portion are used to hold a UNIX "timespec" struct in 10524fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10538fae3551SRodney W. Grimes */ 105443470e3bSGarrett Wollman static void 105543470e3bSGarrett Wollman pinger(void) 10568fae3551SRodney W. Grimes { 10571ad76f1bSAlan Somers struct timespec now; 105813e3f0b7SMaxim Konovalov struct tv32 tv32; 1059d9cacf60SAlan Somers struct icmp icp; 1060efc8588dSDavid E. O'Brien int cc, i; 10610b2f8b3fSMaxim Konovalov u_char *packet; 10628fae3551SRodney W. Grimes 10630b2f8b3fSMaxim Konovalov packet = outpack; 1064d9cacf60SAlan Somers memcpy(&icp, outpack, ICMP_MINLEN + phdr_len); 1065d9cacf60SAlan Somers icp.icmp_type = icmp_type; 1066d9cacf60SAlan Somers icp.icmp_code = 0; 1067d9cacf60SAlan Somers icp.icmp_cksum = 0; 1068d9cacf60SAlan Somers icp.icmp_seq = htons(ntransmitted); 1069d9cacf60SAlan Somers icp.icmp_id = ident; /* ID */ 10708fae3551SRodney W. Grimes 10715db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10728fae3551SRodney W. Grimes 1073eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 10741ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 10751ad76f1bSAlan Somers /* 10761ad76f1bSAlan Somers * Truncate seconds down to 32 bits in order 10771ad76f1bSAlan Somers * to fit the timestamp within 8 bytes of the 10781ad76f1bSAlan Somers * packet. We're only concerned with 10791ad76f1bSAlan Somers * durations, not absolute times. 10801ad76f1bSAlan Somers */ 10811ad76f1bSAlan Somers tv32.tv32_sec = (uint32_t)htonl(now.tv_sec); 10821ad76f1bSAlan Somers tv32.tv32_nsec = (uint32_t)htonl(now.tv_nsec); 1083eb1543c6SMatthew N. Dodd if (options & F_TIME) 1084d9cacf60SAlan Somers icp.icmp_otime = htonl((now.tv_sec % (24*60*60)) 10851ad76f1bSAlan Somers * 1000 + now.tv_nsec / 1000000); 1086eb1543c6SMatthew N. Dodd if (timing) 108713e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10880fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 108913e3f0b7SMaxim Konovalov sizeof(tv32)); 1090eb1543c6SMatthew N. Dodd } 1091eb1543c6SMatthew N. Dodd 1092d9cacf60SAlan Somers memcpy(outpack, &icp, ICMP_MINLEN + phdr_len); 1093d9cacf60SAlan Somers 10940fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10958fae3551SRodney W. Grimes 10968fae3551SRodney W. Grimes /* compute ICMP checksum here */ 1097d9cacf60SAlan Somers icp.icmp_cksum = in_cksum(outpack, cc); 1098d9cacf60SAlan Somers /* Update icmp_cksum in the raw packet data buffer. */ 1099d9cacf60SAlan Somers memcpy(outpack + offsetof(struct icmp, icmp_cksum), &icp.icmp_cksum, 1100d9cacf60SAlan Somers sizeof(icp.icmp_cksum)); 11018fae3551SRodney W. Grimes 11020b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 1103d9cacf60SAlan Somers struct ip ip; 1104d9cacf60SAlan Somers 11050b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 1106d9cacf60SAlan Somers ip.ip_len = htons(cc); 1107d9cacf60SAlan Somers /* Update ip_len in the raw packet data buffer. */ 1108d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_len), &ip.ip_len, 1109d9cacf60SAlan Somers sizeof(ip.ip_len)); 1110d9cacf60SAlan Somers ip.ip_sum = in_cksum(outpackhdr, cc); 1111d9cacf60SAlan Somers /* Update ip_sum in the raw packet data buffer. */ 1112d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_sum), &ip.ip_sum, 1113d9cacf60SAlan Somers sizeof(ip.ip_sum)); 11140b2f8b3fSMaxim Konovalov packet = outpackhdr; 11150b2f8b3fSMaxim Konovalov } 111649133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 11178fae3551SRodney W. Grimes if (i < 0 || i != cc) { 111843470e3bSGarrett Wollman if (i < 0) { 11198f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1120515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1121515dd2faSJulian Elischer return; 1122515dd2faSJulian Elischer } 112343470e3bSGarrett Wollman warn("sendto"); 112443470e3bSGarrett Wollman } else { 112543470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1126416aa49bSPoul-Henning Kamp hostname, i, cc); 11278fae3551SRodney W. Grimes } 1128363d7bbeSJulian Elischer } 1129363d7bbeSJulian Elischer ntransmitted++; 11309ff95228SGleb Smirnoff sntransmitted++; 1131d399eb3eSPiotr Pawel Stefaniak if (!(options & F_QUIET) && options & F_DOT) 1132d399eb3eSPiotr Pawel Stefaniak (void)write(STDOUT_FILENO, &DOT[DOTidx++ % DOTlen], 1); 11338fae3551SRodney W. Grimes } 11348fae3551SRodney W. Grimes 11358fae3551SRodney W. Grimes /* 11368fae3551SRodney W. Grimes * pr_pack -- 11378fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 11388fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 11398fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 11408fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 11418fae3551SRodney W. Grimes */ 114243470e3bSGarrett Wollman static void 1143d9cacf60SAlan Somers pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv) 11448fae3551SRodney W. Grimes { 11459b219646SPeter Wemm struct in_addr ina; 1146d9cacf60SAlan Somers u_char *cp, *dp, l; 1147d9cacf60SAlan Somers struct icmp icp; 1148d9cacf60SAlan Somers struct ip ip; 1149d9cacf60SAlan Somers const u_char *icmp_data_raw; 115046d7b45aSTom Jones ssize_t icmp_data_raw_len; 11518f975bb3SBruce Evans double triptime; 115246d7b45aSTom Jones int dupflag, i, j, recv_len; 1153*70960bb8SCy Schubert int8_t hlen; 11542c29d74cSAlan Somers uint16_t seq; 1155efc8588dSDavid E. O'Brien static int old_rrlen; 1156efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 1157d9cacf60SAlan Somers struct ip oip; 1158d9cacf60SAlan Somers u_char oip_header_len; 1159d9cacf60SAlan Somers struct icmp oicmp; 1160d9cacf60SAlan Somers const u_char *oicmp_raw; 1161d9cacf60SAlan Somers 1162d9cacf60SAlan Somers /* 116346d7b45aSTom Jones * Get size of IP header of the received packet. 116446d7b45aSTom Jones * The header length is contained in the lower four bits of the first 116546d7b45aSTom Jones * byte and represents the number of 4 byte octets the header takes up. 116646d7b45aSTom Jones * 116746d7b45aSTom Jones * The IHL minimum value is 5 (20 bytes) and its maximum value is 15 116846d7b45aSTom Jones * (60 bytes). 1169d9cacf60SAlan Somers */ 1170d9cacf60SAlan Somers memcpy(&l, buf, sizeof(l)); 1171d9cacf60SAlan Somers hlen = (l & 0x0f) << 2; 11728fae3551SRodney W. Grimes 117346d7b45aSTom Jones /* Reject IP packets with a short header */ 1174*70960bb8SCy Schubert if (hlen < (int8_t) sizeof(struct ip)) { 117546d7b45aSTom Jones if (options & F_VERBOSE) 117646d7b45aSTom Jones warn("IHL too short (%d bytes) from %s", hlen, 117746d7b45aSTom Jones inet_ntoa(from->sin_addr)); 117846d7b45aSTom Jones return; 117946d7b45aSTom Jones } 118046d7b45aSTom Jones 118146d7b45aSTom Jones memcpy(&ip, buf, sizeof(struct ip)); 118246d7b45aSTom Jones 118346d7b45aSTom Jones /* Check packet has enough data to carry a valid ICMP header */ 1184e88178ddSMaxim Konovalov recv_len = cc; 11858fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11868fae3551SRodney W. Grimes if (options & F_VERBOSE) 1187d9cacf60SAlan Somers warn("packet too short (%zd bytes) from %s", cc, 118843470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11898fae3551SRodney W. Grimes return; 11908fae3551SRodney W. Grimes } 11918fae3551SRodney W. Grimes 1192d9cacf60SAlan Somers #ifndef icmp_data 1193d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_ip); 1194d9cacf60SAlan Somers #else 119546d7b45aSTom Jones icmp_data_raw_len = cc - (hlen + offsetof(struct icmp, icmp_data)); 1196d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_data); 1197d9cacf60SAlan Somers #endif 1198d9cacf60SAlan Somers 11998fae3551SRodney W. Grimes /* Now the ICMP part */ 12008fae3551SRodney W. Grimes cc -= hlen; 1201d9cacf60SAlan Somers memcpy(&icp, buf + hlen, MIN((ssize_t)sizeof(icp), cc)); 1202d9cacf60SAlan Somers if (icp.icmp_type == icmp_type_rsp) { 1203d9cacf60SAlan Somers if (icp.icmp_id != ident) 12048fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 12058fae3551SRodney W. Grimes ++nreceived; 12068f975bb3SBruce Evans triptime = 0.0; 12078fae3551SRodney W. Grimes if (timing) { 12081ad76f1bSAlan Somers struct timespec tv1; 120913e3f0b7SMaxim Konovalov struct tv32 tv32; 1210d9cacf60SAlan Somers const u_char *tp; 1211d9cacf60SAlan Somers 1212d9cacf60SAlan Somers tp = icmp_data_raw + phdr_len; 1213143008a1SMatthew N. Dodd 12147e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 12157e9489e0SHiroki Sato sizeof(tv1)) { 12169d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 121713e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 121813e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 12191ad76f1bSAlan Somers tv1.tv_nsec = ntohl(tv32.tv32_nsec); 12201ad76f1bSAlan Somers timespecsub(tv, &tv1, tv); 1221039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 12221ad76f1bSAlan Somers ((double)tv->tv_nsec) / 1000000.0; 12238fae3551SRodney W. Grimes tsum += triptime; 12243109a910SGarrett Wollman tsumsq += triptime * triptime; 12258fae3551SRodney W. Grimes if (triptime < tmin) 12268fae3551SRodney W. Grimes tmin = triptime; 12278fae3551SRodney W. Grimes if (triptime > tmax) 12288fae3551SRodney W. Grimes tmax = triptime; 122947e9b3eaSMatthew N. Dodd } else 123047e9b3eaSMatthew N. Dodd timing = 0; 12318fae3551SRodney W. Grimes } 12328fae3551SRodney W. Grimes 1233d9cacf60SAlan Somers seq = ntohs(icp.icmp_seq); 12345db89bc7SBill Fenner 12355db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 12368fae3551SRodney W. Grimes ++nrepeats; 12378fae3551SRodney W. Grimes --nreceived; 12388fae3551SRodney W. Grimes dupflag = 1; 12398fae3551SRodney W. Grimes } else { 12405db89bc7SBill Fenner SET(seq % mx_dup_ck); 12418fae3551SRodney W. Grimes dupflag = 0; 12428fae3551SRodney W. Grimes } 12438fae3551SRodney W. Grimes 12448fae3551SRodney W. Grimes if (options & F_QUIET) 12458fae3551SRodney W. Grimes return; 12468fae3551SRodney W. Grimes 1247d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1248d6cd1497SGleb Smirnoff ++nrcvtimeout; 1249d6cd1497SGleb Smirnoff return; 1250d6cd1497SGleb Smirnoff } 1251d6cd1497SGleb Smirnoff 1252d399eb3eSPiotr Pawel Stefaniak if (options & F_DOT) 12538fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 12548fae3551SRodney W. Grimes else { 1255d9cacf60SAlan Somers (void)printf("%zd bytes from %s: icmp_seq=%u", cc, 1256229e8bf2SAlan Somers pr_addr(from->sin_addr), seq); 1257d9cacf60SAlan Somers (void)printf(" ttl=%d", ip.ip_ttl); 12588fae3551SRodney W. Grimes if (timing) 1259d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 12608fae3551SRodney W. Grimes if (dupflag) 12618fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1262772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1263ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1264143008a1SMatthew N. Dodd if (options & F_MASK) { 1265143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1266143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1267d9cacf60SAlan Somers inet_ntoa(*(struct in_addr *)&(icp.icmp_mask))); 1268143008a1SMatthew N. Dodd } 1269eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1270d9cacf60SAlan Somers (void)printf(" tso=%s", pr_ntime(icp.icmp_otime)); 1271d9cacf60SAlan Somers (void)printf(" tsr=%s", pr_ntime(icp.icmp_rtime)); 1272d9cacf60SAlan Somers (void)printf(" tst=%s", pr_ntime(icp.icmp_ttime)); 1273eb1543c6SMatthew N. Dodd } 1274e88178ddSMaxim Konovalov if (recv_len != send_len) { 1275e88178ddSMaxim Konovalov (void)printf( 1276e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1277e88178ddSMaxim Konovalov recv_len, send_len); 1278e88178ddSMaxim Konovalov } 12798fae3551SRodney W. Grimes /* check the data */ 1280d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + offsetof(struct icmp, 1281d9cacf60SAlan Somers icmp_data) + phdr_len); 12820fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 128347e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 128429dccd6aSMaxim Konovalov i = 0; 128529dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 128629dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 128729dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 128829dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 128929dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 129029dccd6aSMaxim Konovalov } 129129dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12928fae3551SRodney W. Grimes if (*cp != *dp) { 12938fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12948fae3551SRodney W. Grimes i, *dp, *cp); 12951ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 1296d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + 1297d9cacf60SAlan Somers offsetof(struct icmp, icmp_data)); 1298d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1299e88178ddSMaxim Konovalov if ((i % 16) == 8) 1300d32ff037SJohn Birrell (void)printf("\n\t"); 1301e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1302d32ff037SJohn Birrell } 13031ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 13040fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1305d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1306e88178ddSMaxim Konovalov if ((i % 16) == 8) 13078fae3551SRodney W. Grimes (void)printf("\n\t"); 1308e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 13098fae3551SRodney W. Grimes } 13108fae3551SRodney W. Grimes break; 13118fae3551SRodney W. Grimes } 13128fae3551SRodney W. Grimes } 13138fae3551SRodney W. Grimes } 13148fae3551SRodney W. Grimes } else { 1315ef9e6dc7SBill Fenner /* 1316ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1317ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1318ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1319ef9e6dc7SBill Fenner * and ICMP type and ID. 1320f78ac61bSWarner Losh * 1321f78ac61bSWarner Losh * Only print all the error messages if we are running 1322f78ac61bSWarner Losh * as root to avoid leaking information not normally 1323f78ac61bSWarner Losh * available to those not running as root. 1324ef9e6dc7SBill Fenner */ 132546d7b45aSTom Jones 132646d7b45aSTom Jones /* 132746d7b45aSTom Jones * If we don't have enough bytes for a quoted IP header and an 132846d7b45aSTom Jones * ICMP header then stop. 132946d7b45aSTom Jones */ 133046d7b45aSTom Jones if (icmp_data_raw_len < 133146d7b45aSTom Jones (ssize_t)(sizeof(struct ip) + sizeof(struct icmp))) { 133246d7b45aSTom Jones if (options & F_VERBOSE) 133346d7b45aSTom Jones warnx("quoted data too short (%zd bytes) from %s", 133446d7b45aSTom Jones icmp_data_raw_len, inet_ntoa(from->sin_addr)); 133546d7b45aSTom Jones return; 133646d7b45aSTom Jones } 133746d7b45aSTom Jones 1338d9cacf60SAlan Somers memcpy(&oip_header_len, icmp_data_raw, sizeof(oip_header_len)); 1339d9cacf60SAlan Somers oip_header_len = (oip_header_len & 0x0f) << 2; 134046d7b45aSTom Jones 134146d7b45aSTom Jones /* Reject IP packets with a short header */ 134246d7b45aSTom Jones if (oip_header_len < sizeof(struct ip)) { 134346d7b45aSTom Jones if (options & F_VERBOSE) 134446d7b45aSTom Jones warnx("inner IHL too short (%d bytes) from %s", 134546d7b45aSTom Jones oip_header_len, inet_ntoa(from->sin_addr)); 134646d7b45aSTom Jones return; 134746d7b45aSTom Jones } 134846d7b45aSTom Jones 134946d7b45aSTom Jones /* 135046d7b45aSTom Jones * Check against the actual IHL length, to protect against 135146d7b45aSTom Jones * quoated packets carrying IP options. 135246d7b45aSTom Jones */ 135346d7b45aSTom Jones if (icmp_data_raw_len < 135446d7b45aSTom Jones (ssize_t)(oip_header_len + sizeof(struct icmp))) { 135546d7b45aSTom Jones if (options & F_VERBOSE) 135646d7b45aSTom Jones warnx("inner packet too short (%zd bytes) from %s", 135746d7b45aSTom Jones icmp_data_raw_len, inet_ntoa(from->sin_addr)); 135846d7b45aSTom Jones return; 135946d7b45aSTom Jones } 136046d7b45aSTom Jones 136146d7b45aSTom Jones memcpy(&oip, icmp_data_raw, sizeof(struct ip)); 1362d9cacf60SAlan Somers oicmp_raw = icmp_data_raw + oip_header_len; 136346d7b45aSTom Jones memcpy(&oicmp, oicmp_raw, sizeof(struct icmp)); 1364ef9e6dc7SBill Fenner 1365ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1366ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1367d9cacf60SAlan Somers (oip.ip_dst.s_addr == whereto.sin_addr.s_addr) && 1368d9cacf60SAlan Somers (oip.ip_p == IPPROTO_ICMP) && 1369d9cacf60SAlan Somers (oicmp.icmp_type == ICMP_ECHO) && 1370d9cacf60SAlan Somers (oicmp.icmp_id == ident))) { 1371d9cacf60SAlan Somers (void)printf("%zd bytes from %s: ", cc, 137243470e3bSGarrett Wollman pr_addr(from->sin_addr)); 1373d9cacf60SAlan Somers pr_icmph(&icp, &oip, oicmp_raw); 1374ef9e6dc7SBill Fenner } else 1375ef9e6dc7SBill Fenner return; 13768fae3551SRodney W. Grimes } 13778fae3551SRodney W. Grimes 13788fae3551SRodney W. Grimes /* Display any IP options */ 13798fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 13808fae3551SRodney W. Grimes 13818fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 13828fae3551SRodney W. Grimes switch (*cp) { 13838fae3551SRodney W. Grimes case IPOPT_EOL: 13848fae3551SRodney W. Grimes hlen = 0; 13858fae3551SRodney W. Grimes break; 13868fae3551SRodney W. Grimes case IPOPT_LSRR: 1387cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1388cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1389cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1390301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 13918fae3551SRodney W. Grimes hlen -= 2; 1392301207dfSMaxim Konovalov cp += 2; 13933c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 13943c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 13958fae3551SRodney W. Grimes for (;;) { 1396301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1397301207dfSMaxim Konovalov if (ina.s_addr == 0) 13981ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1399301207dfSMaxim Konovalov else 14001ad0b1beSMaxim Konovalov (void)printf("\t%s", 14011ad0b1beSMaxim Konovalov pr_addr(ina)); 1402301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1403301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1404301207dfSMaxim Konovalov j -= INADDR_LEN; 1405301207dfSMaxim Konovalov if (j < INADDR_LEN) 14068fae3551SRodney W. Grimes break; 14078fae3551SRodney W. Grimes (void)putchar('\n'); 14088fae3551SRodney W. Grimes } 1409301207dfSMaxim Konovalov } else 1410301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 14118fae3551SRodney W. Grimes break; 14128fae3551SRodney W. Grimes case IPOPT_RR: 1413301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1414301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 14158fae3551SRodney W. Grimes hlen -= 2; 1416301207dfSMaxim Konovalov cp += 2; 14178fae3551SRodney W. Grimes if (i > j) 14188fae3551SRodney W. Grimes i = j; 1419301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1420301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1421301207dfSMaxim Konovalov old_rrlen = 0; 14228fae3551SRodney W. Grimes continue; 1423301207dfSMaxim Konovalov } 14248fae3551SRodney W. Grimes if (i == old_rrlen 14258fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 1426d399eb3eSPiotr Pawel Stefaniak && !(options & F_DOT)) { 14278fae3551SRodney W. Grimes (void)printf("\t(same route)"); 14288fae3551SRodney W. Grimes hlen -= i; 14298fae3551SRodney W. Grimes cp += i; 14308fae3551SRodney W. Grimes break; 14318fae3551SRodney W. Grimes } 14328fae3551SRodney W. Grimes old_rrlen = i; 14338fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 14348fae3551SRodney W. Grimes (void)printf("\nRR: "); 1435301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1436301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 14378fae3551SRodney W. Grimes for (;;) { 1438301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1439301207dfSMaxim Konovalov if (ina.s_addr == 0) 14401ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1441301207dfSMaxim Konovalov else 1442301207dfSMaxim Konovalov (void)printf("\t%s", 1443301207dfSMaxim Konovalov pr_addr(ina)); 1444301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1445301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1446301207dfSMaxim Konovalov i -= INADDR_LEN; 1447301207dfSMaxim Konovalov if (i < INADDR_LEN) 14488fae3551SRodney W. Grimes break; 14498fae3551SRodney W. Grimes (void)putchar('\n'); 14508fae3551SRodney W. Grimes } 1451301207dfSMaxim Konovalov } else 1452301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 14538fae3551SRodney W. Grimes break; 14548fae3551SRodney W. Grimes case IPOPT_NOP: 14558fae3551SRodney W. Grimes (void)printf("\nNOP"); 14568fae3551SRodney W. Grimes break; 14578fae3551SRodney W. Grimes default: 14588fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 14598fae3551SRodney W. Grimes break; 14608fae3551SRodney W. Grimes } 1461d399eb3eSPiotr Pawel Stefaniak if (!(options & F_DOT)) { 14628fae3551SRodney W. Grimes (void)putchar('\n'); 14638fae3551SRodney W. Grimes (void)fflush(stdout); 14648fae3551SRodney W. Grimes } 14658fae3551SRodney W. Grimes } 14668fae3551SRodney W. Grimes 14678fae3551SRodney W. Grimes /* 1468badd8138SSean Eric Fagan * status -- 1469badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1470badd8138SSean Eric Fagan */ 1471badd8138SSean Eric Fagan 147243470e3bSGarrett Wollman static void 1473fafb8f11SEd Schouten status(int sig __unused) 14747d81b35cSBruce Evans { 1475efc8588dSDavid E. O'Brien 147637e5b2c6SSean Eric Fagan siginfo_p = 1; 147737e5b2c6SSean Eric Fagan } 147837e5b2c6SSean Eric Fagan 147943470e3bSGarrett Wollman static void 1480fafb8f11SEd Schouten check_status(void) 1481badd8138SSean Eric Fagan { 1482efc8588dSDavid E. O'Brien 148337e5b2c6SSean Eric Fagan if (siginfo_p) { 148437e5b2c6SSean Eric Fagan siginfo_p = 0; 1485aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 14867d81b35cSBruce Evans nreceived, ntransmitted, 1487aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1488aed98a27SMaxim Konovalov if (nreceived && timing) 1489aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1490aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1491aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 149237e5b2c6SSean Eric Fagan } 1493badd8138SSean Eric Fagan } 1494badd8138SSean Eric Fagan 1495badd8138SSean Eric Fagan /* 14968fae3551SRodney W. Grimes * finish -- 14978fae3551SRodney W. Grimes * Print out statistics, and give up. 14988fae3551SRodney W. Grimes */ 149943470e3bSGarrett Wollman static void 1500fafb8f11SEd Schouten finish(void) 15018fae3551SRodney W. Grimes { 15028fae3551SRodney W. Grimes 15038fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1504a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 15058fae3551SRodney W. Grimes (void)putchar('\n'); 15068fae3551SRodney W. Grimes (void)fflush(stdout); 15078fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 15088fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 15098fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 15108fae3551SRodney W. Grimes if (nrepeats) 15118fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1512ebe70c8fSWarner Losh if (ntransmitted) { 15138fae3551SRodney W. Grimes if (nreceived > ntransmitted) 15148fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 15158fae3551SRodney W. Grimes else 1516aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1517aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1518aa822c39SDima Dorfman ntransmitted); 1519ebe70c8fSWarner Losh } 1520d6cd1497SGleb Smirnoff if (nrcvtimeout) 1521d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 15228fae3551SRodney W. Grimes (void)putchar('\n'); 15233109a910SGarrett Wollman if (nreceived && timing) { 15243109a910SGarrett Wollman double n = nreceived + nrepeats; 15253109a910SGarrett Wollman double avg = tsum / n; 15263109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 15271ad0b1beSMaxim Konovalov (void)printf( 15281ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 15293109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 15303109a910SGarrett Wollman } 1531badd8138SSean Eric Fagan 15326e1173dcSDavid Greenman if (nreceived) 15338fae3551SRodney W. Grimes exit(0); 15346e1173dcSDavid Greenman else 15356e1173dcSDavid Greenman exit(2); 15368fae3551SRodney W. Grimes } 15378fae3551SRodney W. Grimes 15388fae3551SRodney W. Grimes /* 15398fae3551SRodney W. Grimes * pr_icmph -- 15408fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 15418fae3551SRodney W. Grimes */ 154243470e3bSGarrett Wollman static void 1543d9cacf60SAlan Somers pr_icmph(struct icmp *icp, struct ip *oip, const u_char *const oicmp_raw) 15448fae3551SRodney W. Grimes { 1545efc8588dSDavid E. O'Brien 15468fae3551SRodney W. Grimes switch(icp->icmp_type) { 15478fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 15488fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 15498fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15508fae3551SRodney W. Grimes break; 15518fae3551SRodney W. Grimes case ICMP_UNREACH: 15528fae3551SRodney W. Grimes switch(icp->icmp_code) { 15538fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 15548fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 15558fae3551SRodney W. Grimes break; 15568fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 15578fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 15588fae3551SRodney W. Grimes break; 15598fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 15608fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 15618fae3551SRodney W. Grimes break; 15628fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 15638fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 15648fae3551SRodney W. Grimes break; 15658fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1566ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1567ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 15688fae3551SRodney W. Grimes break; 15698fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 15708fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 15718fae3551SRodney W. Grimes break; 1572ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1573ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1574ef9e6dc7SBill Fenner break; 15758fae3551SRodney W. Grimes default: 15768fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 15778fae3551SRodney W. Grimes icp->icmp_code); 15788fae3551SRodney W. Grimes break; 15798fae3551SRodney W. Grimes } 15808fae3551SRodney W. Grimes /* Print returned IP header information */ 1581d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15828fae3551SRodney W. Grimes break; 15838fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 15848fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 1585d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15868fae3551SRodney W. Grimes break; 15878fae3551SRodney W. Grimes case ICMP_REDIRECT: 15888fae3551SRodney W. Grimes switch(icp->icmp_code) { 15898fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 15908fae3551SRodney W. Grimes (void)printf("Redirect Network"); 15918fae3551SRodney W. Grimes break; 15928fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 15938fae3551SRodney W. Grimes (void)printf("Redirect Host"); 15948fae3551SRodney W. Grimes break; 15958fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15968fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15978fae3551SRodney W. Grimes break; 15988fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15998fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 16008fae3551SRodney W. Grimes break; 16018fae3551SRodney W. Grimes default: 16028fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 16038fae3551SRodney W. Grimes break; 16048fae3551SRodney W. Grimes } 1605ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 1606d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 16078fae3551SRodney W. Grimes break; 16088fae3551SRodney W. Grimes case ICMP_ECHO: 16098fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 16108fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 16118fae3551SRodney W. Grimes break; 16128fae3551SRodney W. Grimes case ICMP_TIMXCEED: 16138fae3551SRodney W. Grimes switch(icp->icmp_code) { 16148fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 16158fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 16168fae3551SRodney W. Grimes break; 16178fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 16188fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 16198fae3551SRodney W. Grimes break; 16208fae3551SRodney W. Grimes default: 16218fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 16228fae3551SRodney W. Grimes icp->icmp_code); 16238fae3551SRodney W. Grimes break; 16248fae3551SRodney W. Grimes } 1625d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 16268fae3551SRodney W. Grimes break; 16278fae3551SRodney W. Grimes case ICMP_PARAMPROB: 16288fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 16298fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 1630d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 16318fae3551SRodney W. Grimes break; 16328fae3551SRodney W. Grimes case ICMP_TSTAMP: 16338fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 16348fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16358fae3551SRodney W. Grimes break; 16368fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 16378fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 16388fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 16398fae3551SRodney W. Grimes break; 16408fae3551SRodney W. Grimes case ICMP_IREQ: 16418fae3551SRodney W. Grimes (void)printf("Information Request\n"); 16428fae3551SRodney W. Grimes /* XXX ID + Seq */ 16438fae3551SRodney W. Grimes break; 16448fae3551SRodney W. Grimes case ICMP_IREQREPLY: 16458fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 16468fae3551SRodney W. Grimes /* XXX ID + Seq */ 16478fae3551SRodney W. Grimes break; 16488fae3551SRodney W. Grimes case ICMP_MASKREQ: 16498fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 16508fae3551SRodney W. Grimes break; 16518fae3551SRodney W. Grimes case ICMP_MASKREPLY: 16528fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 16538fae3551SRodney W. Grimes break; 1654ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1655ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1656ef9e6dc7SBill Fenner break; 1657ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1658ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1659ef9e6dc7SBill Fenner break; 16608fae3551SRodney W. Grimes default: 16618fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 16628fae3551SRodney W. Grimes } 16638fae3551SRodney W. Grimes } 16648fae3551SRodney W. Grimes 16658fae3551SRodney W. Grimes /* 16668fae3551SRodney W. Grimes * pr_iph -- 16678fae3551SRodney W. Grimes * Print an IP header with options. 16688fae3551SRodney W. Grimes */ 166943470e3bSGarrett Wollman static void 1670fafb8f11SEd Schouten pr_iph(struct ip *ip) 16718fae3551SRodney W. Grimes { 1672a94c074dSDimitry Andric struct in_addr ina; 16738fae3551SRodney W. Grimes u_char *cp; 1674efc8588dSDavid E. O'Brien int hlen; 16758fae3551SRodney W. Grimes 16768fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 16778fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 16788fae3551SRodney W. Grimes 1679ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 16808fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1681ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1682ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1683d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1684d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1685d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1686ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1687ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1688a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1689a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1690a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1691a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1692ef9e6dc7SBill Fenner /* dump any option bytes */ 16938fae3551SRodney W. Grimes while (hlen-- > 20) { 16948fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16958fae3551SRodney W. Grimes } 16968fae3551SRodney W. Grimes (void)putchar('\n'); 16978fae3551SRodney W. Grimes } 16988fae3551SRodney W. Grimes 16998fae3551SRodney W. Grimes /* 17008fae3551SRodney W. Grimes * pr_addr -- 17018fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 17028fae3551SRodney W. Grimes * a hostname. 17038fae3551SRodney W. Grimes */ 170443470e3bSGarrett Wollman static char * 1705fafb8f11SEd Schouten pr_addr(struct in_addr ina) 17068fae3551SRodney W. Grimes { 17078fae3551SRodney W. Grimes struct hostent *hp; 1708f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 17098fae3551SRodney W. Grimes 171049133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 171143470e3bSGarrett Wollman return inet_ntoa(ina); 171249133c6dSPawel Jakub Dawidek 171349133c6dSPawel Jakub Dawidek hp = cap_gethostbyaddr(capdns, (char *)&ina, 4, AF_INET); 171449133c6dSPawel Jakub Dawidek 171549133c6dSPawel Jakub Dawidek if (hp == NULL) 171649133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 171749133c6dSPawel Jakub Dawidek 1718efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 171943470e3bSGarrett Wollman inet_ntoa(ina)); 17208fae3551SRodney W. Grimes return(buf); 17218fae3551SRodney W. Grimes } 17228fae3551SRodney W. Grimes 17238fae3551SRodney W. Grimes /* 17248fae3551SRodney W. Grimes * pr_retip -- 17258fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 17268fae3551SRodney W. Grimes */ 172743470e3bSGarrett Wollman static void 1728d9cacf60SAlan Somers pr_retip(struct ip *ip, const u_char *cp) 17298fae3551SRodney W. Grimes { 17308fae3551SRodney W. Grimes pr_iph(ip); 17318fae3551SRodney W. Grimes 17328fae3551SRodney W. Grimes if (ip->ip_p == 6) 17338fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 17348fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17358fae3551SRodney W. Grimes else if (ip->ip_p == 17) 17368fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 17378fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 17388fae3551SRodney W. Grimes } 17398fae3551SRodney W. Grimes 1740eb1543c6SMatthew N. Dodd static char * 1741007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1742eb1543c6SMatthew N. Dodd { 17437898770aSAlan Somers static char buf[11]; 1744007fe4e3SMaxim Konovalov int hour, min, sec; 1745eb1543c6SMatthew N. Dodd 1746007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1747007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1748007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1749007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1750eb1543c6SMatthew N. Dodd 1751007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1752eb1543c6SMatthew N. Dodd 1753eb1543c6SMatthew N. Dodd return (buf); 1754eb1543c6SMatthew N. Dodd } 1755eb1543c6SMatthew N. Dodd 175643470e3bSGarrett Wollman static void 1757fafb8f11SEd Schouten fill(char *bp, char *patp) 17588fae3551SRodney W. Grimes { 17598fae3551SRodney W. Grimes char *cp; 1760efc8588dSDavid E. O'Brien int pat[16]; 17614fba6582SMaxim Konovalov u_int ii, jj, kk; 17628fae3551SRodney W. Grimes 176343470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 176443470e3bSGarrett Wollman if (!isxdigit(*cp)) 176543470e3bSGarrett Wollman errx(EX_USAGE, 176643470e3bSGarrett Wollman "patterns must be specified as hex digits"); 176743470e3bSGarrett Wollman 17688fae3551SRodney W. Grimes } 17698fae3551SRodney W. Grimes ii = sscanf(patp, 17708fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 17718fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 17728fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 17738fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 17748fae3551SRodney W. Grimes 17758fae3551SRodney W. Grimes if (ii > 0) 1776d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 17778fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17788fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17798fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17808fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17818fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17828fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17838fae3551SRodney W. Grimes (void)printf("\n"); 17848fae3551SRodney W. Grimes } 17858fae3551SRodney W. Grimes } 17868fae3551SRodney W. Grimes 178749133c6dSPawel Jakub Dawidek static cap_channel_t * 178849133c6dSPawel Jakub Dawidek capdns_setup(void) 178949133c6dSPawel Jakub Dawidek { 179049133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 1791a3ce7698SAlan Somers #ifdef WITH_CASPER 179249133c6dSPawel Jakub Dawidek const char *types[2]; 179349133c6dSPawel Jakub Dawidek int families[1]; 1794a3ce7698SAlan Somers #endif 179549133c6dSPawel Jakub Dawidek capcas = cap_init(); 1796c501d73cSMariusz Zaborski if (capcas == NULL) 1797c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 179849133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 179949133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 180049133c6dSPawel Jakub Dawidek cap_close(capcas); 180149133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 180249133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 1803a3ce7698SAlan Somers #ifdef WITH_CASPER 1804752d135eSMariusz Zaborski types[0] = "NAME2ADDR"; 1805752d135eSMariusz Zaborski types[1] = "ADDR2NAME"; 180649133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 180749133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 180849133c6dSPawel Jakub Dawidek families[0] = AF_INET; 180949133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 181049133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 1811a3ce7698SAlan Somers #endif 181249133c6dSPawel Jakub Dawidek return (capdnsloc); 181349133c6dSPawel Jakub Dawidek } 1814