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 103ff77ab83SAlan Somers #include "utils.h" 104ff77ab83SAlan Somers 105301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10613e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 107eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 108eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 109c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1108f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1118f975bb3SBruce Evans /* runs out of buffer space */ 1124fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1134fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 114d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 115bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1160b2f8b3fSMaxim Konovalov #define MAXTOS 255 1178fae3551SRodney W. Grimes 1188fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1198fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1208fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1218fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1228fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1238fae3551SRodney W. Grimes 12413e3f0b7SMaxim Konovalov struct tv32 { 12513e3f0b7SMaxim Konovalov int32_t tv32_sec; 1261ad76f1bSAlan Somers int32_t tv32_nsec; 12713e3f0b7SMaxim Konovalov }; 12813e3f0b7SMaxim Konovalov 1298fae3551SRodney W. Grimes /* various options */ 1307e9489e0SHiroki Sato static int options; 13185456935SBill Fenner #define F_FLOOD 0x0001 13285456935SBill Fenner #define F_INTERVAL 0x0002 13385456935SBill Fenner #define F_NUMERIC 0x0004 13485456935SBill Fenner #define F_PINGFILLED 0x0008 13585456935SBill Fenner #define F_QUIET 0x0010 13685456935SBill Fenner #define F_RROUTE 0x0020 13785456935SBill Fenner #define F_SO_DEBUG 0x0040 13885456935SBill Fenner #define F_SO_DONTROUTE 0x0080 13985456935SBill Fenner #define F_VERBOSE 0x0100 14085456935SBill Fenner #define F_QUIET2 0x0200 14185456935SBill Fenner #define F_NOLOOP 0x0400 14285456935SBill Fenner #define F_MTTL 0x0800 14385456935SBill Fenner #define F_MIF 0x1000 144772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1459a4365d0SYoshinobu Inoue #ifdef IPSEC 1469a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1479a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1489a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1499a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 150211bfbd2SRuslan Ermilov #define F_TTL 0x8000 151ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1528025c44bSDima Dorfman #define F_ONCE 0x20000 1530b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 154143008a1SMatthew N. Dodd #define F_MASK 0x80000 155eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1569ff95228SGleb Smirnoff #define F_SWEEP 0x200000 157d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 1588fae3551SRodney W. Grimes 1598fae3551SRodney W. Grimes /* 1608fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1618fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1628fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1638fae3551SRodney W. Grimes */ 1648fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1657e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1667e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1678fae3551SRodney W. Grimes 1687e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1697e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1707e9489e0SHiroki Sato static int maxpayload; 1717e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1727e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1737e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1747e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1757e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 1767e9489e0SHiroki Sato static char DOT = '.'; 1777e9489e0SHiroki Sato static char *hostname; 1787e9489e0SHiroki Sato static char *shostname; 1797e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1807e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1817e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1827e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1837e9489e0SHiroki Sato static int phdr_len = 0; 1847e9489e0SHiroki Sato static int send_len; 1858fae3551SRodney W. Grimes 1868fae3551SRodney W. Grimes /* counters */ 1877e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1887e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1897e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1907e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1917e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1927e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1937e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 1947e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 1957e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 1967e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 1977e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 1987e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 1997e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 2008fae3551SRodney W. Grimes 2018fae3551SRodney W. Grimes /* timing */ 2027e9489e0SHiroki Sato static int timing; /* flag to do timing */ 2037e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 2047e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2057e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2067e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2078fae3551SRodney W. Grimes 2087e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2097e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2107e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 211badd8138SSean Eric Fagan 21249133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 21349133c6dSPawel Jakub Dawidek 21443470e3bSGarrett Wollman static void fill(char *, char *); 21549133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 21643470e3bSGarrett Wollman static void check_status(void); 2178f975bb3SBruce Evans static void finish(void) __dead2; 21843470e3bSGarrett Wollman static void pinger(void); 21943470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 220eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 221d9cacf60SAlan Somers static void pr_icmph(struct icmp *, struct ip *, const u_char *const); 22243470e3bSGarrett Wollman static void pr_iph(struct ip *); 223d9cacf60SAlan Somers static void pr_pack(char *, ssize_t, struct sockaddr_in *, struct timespec *); 224d9cacf60SAlan Somers static void pr_retip(struct ip *, const u_char *); 22543470e3bSGarrett Wollman static void status(int); 2268f975bb3SBruce Evans static void stopit(int); 227e345a80dSPhilippe Charnier static void usage(void) __dead2; 2288fae3551SRodney W. Grimes 22943470e3bSGarrett Wollman int 230fafb8f11SEd Schouten main(int argc, char *const *argv) 2318fae3551SRodney W. Grimes { 23231eac03bSSean Chittenden struct sockaddr_in from, sock_in; 233efc8588dSDavid E. O'Brien struct in_addr ifaddr; 2341ad76f1bSAlan Somers struct timespec last, intvl; 235efc8588dSDavid E. O'Brien struct iovec iov; 236efc8588dSDavid E. O'Brien struct msghdr msg; 237efc8588dSDavid E. O'Brien struct sigaction si_sa; 2380b2f8b3fSMaxim Konovalov size_t sz; 239e81f5049SOlivier Houchard u_char *datap, packet[IP_MAXPACKET] __aligned(4); 240d074d39fSMatthew N. Dodd char *ep, *source, *target, *payload; 241261e59bbSMaxim Konovalov struct hostent *hp; 242efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC 243efc8588dSDavid E. O'Brien char *policy_in, *policy_out; 244efc8588dSDavid E. O'Brien #endif 245261e59bbSMaxim Konovalov struct sockaddr_in *to; 246261e59bbSMaxim Konovalov double t; 247c0a3773aSEugene Grosbein u_long alarmtimeout; 248c0a3773aSEugene Grosbein long ltmp; 24949133c6dSPawel Jakub Dawidek int almost_done, ch, df, hold, i, icmp_len, mib[4], preload; 25049133c6dSPawel Jakub Dawidek int ssend_errno, srecv_errno, tos, ttl; 2511ad76f1bSAlan Somers char ctrl[CMSG_SPACE(sizeof(struct timespec))]; 252efc8588dSDavid E. O'Brien char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; 2538fae3551SRodney W. Grimes #ifdef IP_OPTIONS 2544fba6582SMaxim Konovalov char rspace[MAX_IPOPTLEN]; /* record route space */ 2558fae3551SRodney W. Grimes #endif 256261e59bbSMaxim Konovalov unsigned char loop, mttl; 257efc8588dSDavid E. O'Brien 25831eac03bSSean Chittenden payload = source = NULL; 2599a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 260efc8588dSDavid E. O'Brien policy_in = policy_out = NULL; 2619a4365d0SYoshinobu Inoue #endif 26249133c6dSPawel Jakub Dawidek cap_rights_t rights; 2638fae3551SRodney W. Grimes 264299e2c58SAlan Somers options |= F_NUMERIC; 265299e2c58SAlan Somers 266f1284d7aSBill Fenner /* 267f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 268f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 269f1284d7aSBill Fenner * after arg parsing. 27049133c6dSPawel Jakub Dawidek * 27149133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 27249133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 27349133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 27449133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 27549133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 27649133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 27749133c6dSPawel Jakub Dawidek * packets. 278f1284d7aSBill Fenner */ 27949133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28049133c6dSPawel Jakub Dawidek ssend_errno = errno; 28149133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28249133c6dSPawel Jakub Dawidek srecv_errno = errno; 283f1284d7aSBill Fenner 2841d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2851d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 286ee2bf734SWarner Losh uid = getuid(); 287f1284d7aSBill Fenner 288eeb63943SDon Lewis if (ssend < 0) { 289eeb63943SDon Lewis errno = ssend_errno; 290eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 291eeb63943SDon Lewis } 292eeb63943SDon Lewis 293eeb63943SDon Lewis if (srecv < 0) { 294eeb63943SDon Lewis errno = srecv_errno; 295eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 296eeb63943SDon Lewis } 297eeb63943SDon Lewis 2980b2f8b3fSMaxim Konovalov alarmtimeout = df = preload = tos = 0; 299badd8138SSean Eric Fagan 3000b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 301211bfbd2SRuslan Ermilov while ((ch = getopt(argc, argv, 30299f13ae1SAlan Somers "Aac:DdfG:g:Hh:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:" 303211bfbd2SRuslan Ermilov #ifdef IPSEC 3049a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 305211bfbd2SRuslan Ermilov "P:" 3069a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 307211bfbd2SRuslan Ermilov #endif /*IPSEC*/ 308211bfbd2SRuslan Ermilov )) != -1) 3099a4365d0SYoshinobu Inoue { 3108fae3551SRodney W. Grimes switch(ch) { 311ca517ad8SPoul-Henning Kamp case 'A': 312ca517ad8SPoul-Henning Kamp options |= F_MISSED; 313ca517ad8SPoul-Henning Kamp break; 314772dfa72SDaniel O'Callaghan case 'a': 315772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 316772dfa72SDaniel O'Callaghan break; 3178fae3551SRodney W. Grimes case 'c': 318c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 31965c3a67dSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 32043470e3bSGarrett Wollman errx(EX_USAGE, 32143470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 32243470e3bSGarrett Wollman optarg); 323c0a3773aSEugene Grosbein npackets = ltmp; 3248fae3551SRodney W. Grimes break; 3250b2f8b3fSMaxim Konovalov case 'D': 3260b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3270b2f8b3fSMaxim Konovalov df = 1; 3280b2f8b3fSMaxim Konovalov break; 3298fae3551SRodney W. Grimes case 'd': 3308fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3318fae3551SRodney W. Grimes break; 3328fae3551SRodney W. Grimes case 'f': 333526f06b2SMatthew Dillon if (uid) { 33443470e3bSGarrett Wollman errno = EPERM; 33543470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3368fae3551SRodney W. Grimes } 3378fae3551SRodney W. Grimes options |= F_FLOOD; 3388fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3398fae3551SRodney W. Grimes break; 3409ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 341c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 342c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 3439ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3449ff95228SGleb Smirnoff optarg); 345c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3469ff95228SGleb Smirnoff errno = EPERM; 3479ff95228SGleb Smirnoff err(EX_NOPERM, 348c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 349c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3509ff95228SGleb Smirnoff } 3519ff95228SGleb Smirnoff options |= F_SWEEP; 352c0a3773aSEugene Grosbein sweepmax = ltmp; 3539ff95228SGleb Smirnoff break; 3549ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 355c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 356c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 3579ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3589ff95228SGleb Smirnoff optarg); 359c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3609ff95228SGleb Smirnoff errno = EPERM; 3619ff95228SGleb Smirnoff err(EX_NOPERM, 362c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 363c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3649ff95228SGleb Smirnoff } 3659ff95228SGleb Smirnoff options |= F_SWEEP; 366c0a3773aSEugene Grosbein sweepmin = ltmp; 3679ff95228SGleb Smirnoff break; 36899f13ae1SAlan Somers case 'H': 36999f13ae1SAlan Somers options &= ~F_NUMERIC; 37099f13ae1SAlan Somers break; 3719ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 372c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 373c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp < 1) 3749ff95228SGleb Smirnoff errx(EX_USAGE, "invalid increment size: `%s'", 3759ff95228SGleb Smirnoff optarg); 376c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3779ff95228SGleb Smirnoff errno = EPERM; 3789ff95228SGleb Smirnoff err(EX_NOPERM, 379c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 380c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3819ff95228SGleb Smirnoff } 3829ff95228SGleb Smirnoff options |= F_SWEEP; 383c0a3773aSEugene Grosbein sweepincr = ltmp; 3849ff95228SGleb Smirnoff break; 3851f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 3861f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 3871f6a4631SRuslan Ermilov errx(EX_USAGE, 3881f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 3891f6a4631SRuslan Ermilov optarg); 3901f6a4631SRuslan Ermilov options |= F_MIF; 3911f6a4631SRuslan Ermilov break; 3928fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 3931ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 3941ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 3951ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 3961ad0b1beSMaxim Konovalov optarg); 3978fae3551SRodney W. Grimes options |= F_INTERVAL; 398526f06b2SMatthew Dillon interval = (int)t; 399526f06b2SMatthew Dillon if (uid && interval < 1000) { 400526f06b2SMatthew Dillon errno = EPERM; 401526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 402526f06b2SMatthew Dillon } 4038fae3551SRodney W. Grimes break; 4041f6a4631SRuslan Ermilov case 'L': 4051f6a4631SRuslan Ermilov options |= F_NOLOOP; 4061f6a4631SRuslan Ermilov loop = 0; 40785456935SBill Fenner break; 4088fae3551SRodney W. Grimes case 'l': 409c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 410c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > INT_MAX || ltmp < 0) 41143470e3bSGarrett Wollman errx(EX_USAGE, 41243470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4135e2cc0f4SStephen McKay if (uid) { 414f78ac61bSWarner Losh errno = EPERM; 415f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 416f78ac61bSWarner Losh } 417c0a3773aSEugene Grosbein preload = ltmp; 4188fae3551SRodney W. Grimes break; 4191f6a4631SRuslan Ermilov case 'M': 420eb1543c6SMatthew N. Dodd switch(optarg[0]) { 421eb1543c6SMatthew N. Dodd case 'M': 422eb1543c6SMatthew N. Dodd case 'm': 4231f6a4631SRuslan Ermilov options |= F_MASK; 42485456935SBill Fenner break; 425eb1543c6SMatthew N. Dodd case 'T': 426eb1543c6SMatthew N. Dodd case 't': 427eb1543c6SMatthew N. Dodd options |= F_TIME; 428eb1543c6SMatthew N. Dodd break; 429eb1543c6SMatthew N. Dodd default: 430eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 431eb1543c6SMatthew N. Dodd break; 432eb1543c6SMatthew N. Dodd } 433eb1543c6SMatthew N. Dodd break; 434211bfbd2SRuslan Ermilov case 'm': /* TTL */ 435c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 436c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTTL || ltmp < 0) 4371ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 438c0a3773aSEugene Grosbein ttl = ltmp; 439211bfbd2SRuslan Ermilov options |= F_TTL; 440211bfbd2SRuslan Ermilov break; 4418fae3551SRodney W. Grimes case 'n': 4428fae3551SRodney W. Grimes options |= F_NUMERIC; 4438fae3551SRodney W. Grimes break; 4448025c44bSDima Dorfman case 'o': 4458025c44bSDima Dorfman options |= F_ONCE; 4468025c44bSDima Dorfman break; 4471f6a4631SRuslan Ermilov #ifdef IPSEC 4481f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4491f6a4631SRuslan Ermilov case 'P': 4501f6a4631SRuslan Ermilov options |= F_POLICY; 4511f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4521f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4531f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4541f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4551f6a4631SRuslan Ermilov else 4561f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4571f6a4631SRuslan Ermilov break; 4581f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4591f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4608fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4618fae3551SRodney W. Grimes options |= F_PINGFILLED; 462d074d39fSMatthew N. Dodd payload = optarg; 4638fae3551SRodney W. Grimes break; 464ef9e6dc7SBill Fenner case 'Q': 465ef9e6dc7SBill Fenner options |= F_QUIET2; 466ef9e6dc7SBill Fenner break; 4678fae3551SRodney W. Grimes case 'q': 4688fae3551SRodney W. Grimes options |= F_QUIET; 4698fae3551SRodney W. Grimes break; 4708fae3551SRodney W. Grimes case 'R': 4718fae3551SRodney W. Grimes options |= F_RROUTE; 4728fae3551SRodney W. Grimes break; 4738fae3551SRodney W. Grimes case 'r': 4748fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4758fae3551SRodney W. Grimes break; 4761f6a4631SRuslan Ermilov case 'S': 4771f6a4631SRuslan Ermilov source = optarg; 4781f6a4631SRuslan Ermilov break; 4798fae3551SRodney W. Grimes case 's': /* size of packet to send */ 480c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 481c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp < 0) 48243470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 48343470e3bSGarrett Wollman optarg); 484c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 485fb7d32c7SMaxim Konovalov errno = EPERM; 486fb7d32c7SMaxim Konovalov err(EX_NOPERM, 487c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 488c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 489fb7d32c7SMaxim Konovalov } 490c0a3773aSEugene Grosbein datalen = ltmp; 4918fae3551SRodney W. Grimes break; 4921f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 493c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 494c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTTL || ltmp < 0) 4951f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 4961f6a4631SRuslan Ermilov optarg); 497c0a3773aSEugene Grosbein mttl = ltmp; 4981f6a4631SRuslan Ermilov options |= F_MTTL; 49999490edeSWarner Losh break; 5007237fd94SBill Fumerola case 't': 501bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 502bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 5037237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 5047237fd94SBill Fumerola optarg); 505bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 506bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 507bf113f1bSBill Fumerola optarg, MAXALARM); 508bf113f1bSBill Fumerola alarm((int)alarmtimeout); 5097237fd94SBill Fumerola break; 5108fae3551SRodney W. Grimes case 'v': 5118fae3551SRodney W. Grimes options |= F_VERBOSE; 5128fae3551SRodney W. Grimes break; 513d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 514d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 515d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 516d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 517d6cd1497SGleb Smirnoff optarg); 518d6cd1497SGleb Smirnoff options |= F_WAITTIME; 519d6cd1497SGleb Smirnoff waittime = (int)t; 520d6cd1497SGleb Smirnoff break; 5210b2f8b3fSMaxim Konovalov case 'z': 5220b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 523c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 524c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5250b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 526c0a3773aSEugene Grosbein tos = ltmp; 5270b2f8b3fSMaxim Konovalov break; 5288fae3551SRodney W. Grimes default: 529e345a80dSPhilippe Charnier usage(); 53043470e3bSGarrett Wollman } 53143470e3bSGarrett Wollman } 53243470e3bSGarrett Wollman 53343470e3bSGarrett Wollman if (argc - optind != 1) 534e345a80dSPhilippe Charnier usage(); 53543470e3bSGarrett Wollman target = argv[optind]; 5368fae3551SRodney W. Grimes 537d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 538d829c3dfSMatthew N. Dodd case 0: break; 539d829c3dfSMatthew N. Dodd case F_MASK: 540eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 541eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 542d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 543eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 544eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 545d829c3dfSMatthew N. Dodd break; 546d829c3dfSMatthew N. Dodd case F_TIME: 547eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 548eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 549d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 550eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 551eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 552d829c3dfSMatthew N. Dodd break; 553d829c3dfSMatthew N. Dodd default: 554d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 555d829c3dfSMatthew N. Dodd break; 556eb1543c6SMatthew N. Dodd } 5570fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 558fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 559e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 560e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 561fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5621104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 563fb7d32c7SMaxim Konovalov maxpayload); 564e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5650fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 566d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 567d074d39fSMatthew N. Dodd fill((char *)datap, payload); 568d074d39fSMatthew N. Dodd } 56949133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 57099490edeSWarner Losh if (source) { 57131eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 57231eac03bSSean Chittenden sock_in.sin_family = AF_INET; 57331eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 57499490edeSWarner Losh shostname = source; 57599490edeSWarner Losh } else { 576d68e2c04SMariusz Zaborski hp = cap_gethostbyname2(capdns, source, AF_INET); 57799490edeSWarner Losh if (!hp) 57899490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 57999490edeSWarner Losh source, hstrerror(h_errno)); 58099490edeSWarner Losh 58131eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 58231eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 5834fba6582SMaxim Konovalov hp->h_length < 0) 58499490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 58531eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 58631eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 58799490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 58899490edeSWarner Losh sizeof(snamebuf) - 1); 58999490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 59099490edeSWarner Losh shostname = snamebuf; 59199490edeSWarner Losh } 59249133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 59349133c6dSPawel Jakub Dawidek -1) 59499490edeSWarner Losh err(1, "bind"); 59599490edeSWarner Losh } 59699490edeSWarner Losh 597d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 598d389e86aSMatt Jacob to = &whereto; 5998fae3551SRodney W. Grimes to->sin_family = AF_INET; 600d389e86aSMatt Jacob to->sin_len = sizeof *to; 60143470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 6028fae3551SRodney W. Grimes hostname = target; 60343470e3bSGarrett Wollman } else { 60449133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 60543470e3bSGarrett Wollman if (!hp) 60643470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 60743470e3bSGarrett Wollman target, hstrerror(h_errno)); 60843470e3bSGarrett Wollman 60931eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6101ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 61143470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6128fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 613b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6148fae3551SRodney W. Grimes hostname = hnamebuf; 6158fae3551SRodney W. Grimes } 6168fae3551SRodney W. Grimes 61749133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 618a3ce7698SAlan Somers #ifdef WITH_CASPER 61949133c6dSPawel Jakub Dawidek if (capdns != NULL) { 62049133c6dSPawel Jakub Dawidek const char *types[1]; 62149133c6dSPawel Jakub Dawidek 622752d135eSMariusz Zaborski types[0] = "ADDR2NAME"; 62349133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 62449133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 62549133c6dSPawel Jakub Dawidek } 626a3ce7698SAlan Somers #endif 62749133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 62849133c6dSPawel Jakub Dawidek err(1, "connect"); 62949133c6dSPawel Jakub Dawidek 63043470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 63143470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 63243470e3bSGarrett Wollman 63343470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 63443470e3bSGarrett Wollman errx(EX_USAGE, 63543470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 63643470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 63743470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 63843470e3bSGarrett Wollman errx(EX_USAGE, 63943470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6408fae3551SRodney W. Grimes 641d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6428fae3551SRodney W. Grimes timing = 1; 64343470e3bSGarrett Wollman 6448fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 645d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6468fae3551SRodney W. Grimes *datap++ = i; 6478fae3551SRodney W. Grimes 6488fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6498fae3551SRodney W. Grimes 6508fae3551SRodney W. Grimes hold = 1; 65149133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 65249133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6538fae3551SRodney W. Grimes sizeof(hold)); 65449133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 65549133c6dSPawel Jakub Dawidek sizeof(hold)); 65649133c6dSPawel Jakub Dawidek } 6578fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 65849133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6598fae3551SRodney W. Grimes sizeof(hold)); 6609a4365d0SYoshinobu Inoue #ifdef IPSEC 6619a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6629a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6639a4365d0SYoshinobu Inoue char *buf; 6649a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6659a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6669a4365d0SYoshinobu Inoue if (buf == NULL) 667ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 66849133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6699a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6701ad0b1beSMaxim Konovalov err(EX_CONFIG, 6711ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6729a4365d0SYoshinobu Inoue free(buf); 6739a4365d0SYoshinobu Inoue } 6749a4365d0SYoshinobu Inoue 6759a4365d0SYoshinobu Inoue if (policy_out != NULL) { 6769a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 6779a4365d0SYoshinobu Inoue if (buf == NULL) 678ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 67949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 6809a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6811ad0b1beSMaxim Konovalov err(EX_CONFIG, 6821ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6839a4365d0SYoshinobu Inoue free(buf); 6849a4365d0SYoshinobu Inoue } 6859a4365d0SYoshinobu Inoue } 6869a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 6879a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 6888fae3551SRodney W. Grimes 6890b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 690d9cacf60SAlan Somers struct ip ip; 691d9cacf60SAlan Somers 692d9cacf60SAlan Somers memcpy(&ip, outpackhdr, sizeof(ip)); 6930b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 6940b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 6950b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 6960b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 6970b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 6980b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 6990b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 7000b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 7010b2f8b3fSMaxim Konovalov } 70249133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 703d9cacf60SAlan Somers ip.ip_v = IPVERSION; 704d9cacf60SAlan Somers ip.ip_hl = sizeof(struct ip) >> 2; 705d9cacf60SAlan Somers ip.ip_tos = tos; 706d9cacf60SAlan Somers ip.ip_id = 0; 707d9cacf60SAlan Somers ip.ip_off = htons(df ? IP_DF : 0); 708d9cacf60SAlan Somers ip.ip_ttl = ttl; 709d9cacf60SAlan Somers ip.ip_p = IPPROTO_ICMP; 710d9cacf60SAlan Somers ip.ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 711d9cacf60SAlan Somers ip.ip_dst = to->sin_addr; 712d9cacf60SAlan Somers memcpy(outpackhdr, &ip, sizeof(ip)); 7130b2f8b3fSMaxim Konovalov } 71449133c6dSPawel Jakub Dawidek 71549133c6dSPawel Jakub Dawidek /* 71649133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 71749133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 71849133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 71949133c6dSPawel Jakub Dawidek */ 7207bdc3291SMark Johnston caph_cache_catpages(); 72118fcfaa4SMark Johnston if (caph_enter_casper() < 0) 72249133c6dSPawel Jakub Dawidek err(1, "cap_enter"); 72349133c6dSPawel Jakub Dawidek 72449133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 7257bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 72649133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 72749133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 7287bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 72949133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 73049133c6dSPawel Jakub Dawidek 7318fae3551SRodney W. Grimes /* record route option */ 7328fae3551SRodney W. Grimes if (options & F_RROUTE) { 7338fae3551SRodney W. Grimes #ifdef IP_OPTIONS 734039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7358fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7368fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7378fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 738039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 73949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 74043470e3bSGarrett Wollman sizeof(rspace)) < 0) 74143470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7428fae3551SRodney W. Grimes #else 74343470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 74443470e3bSGarrett Wollman "record route not available in this implementation"); 7458fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7468fae3551SRodney W. Grimes } 7478fae3551SRodney W. Grimes 748211bfbd2SRuslan Ermilov if (options & F_TTL) { 74949133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 750211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 751211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 752211bfbd2SRuslan Ermilov } 753211bfbd2SRuslan Ermilov } 75485456935SBill Fenner if (options & F_NOLOOP) { 75549133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 75685456935SBill Fenner sizeof(loop)) < 0) { 75743470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 75885456935SBill Fenner } 75985456935SBill Fenner } 76085456935SBill Fenner if (options & F_MTTL) { 76149133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 762211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 76343470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 76485456935SBill Fenner } 76585456935SBill Fenner } 76685456935SBill Fenner if (options & F_MIF) { 76749133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 76885456935SBill Fenner sizeof(ifaddr)) < 0) { 76943470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 77085456935SBill Fenner } 77185456935SBill Fenner } 772039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 77384633ef1SAlan Somers { 77484633ef1SAlan Somers int on = 1; 77584633ef1SAlan Somers int ts_clock = SO_TS_MONOTONIC; 77684633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, 77784633ef1SAlan Somers sizeof(on)) < 0) 778039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 77984633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TS_CLOCK, &ts_clock, 78084633ef1SAlan Somers sizeof(ts_clock)) < 0) 78184633ef1SAlan Somers err(EX_OSERR, "setsockopt SO_TS_CLOCK"); 782039d6aa4SBill Fenner } 783039d6aa4SBill Fenner #endif 7849ff95228SGleb Smirnoff if (sweepmax) { 785bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 786bb7dfe5eSGleb Smirnoff errx(EX_USAGE, "Maximum packet size must be no less than the minimum packet size"); 7879ff95228SGleb Smirnoff 7889ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 7899ff95228SGleb Smirnoff errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive"); 7909ff95228SGleb Smirnoff 7919ff95228SGleb Smirnoff if (npackets > 0) { 7929ff95228SGleb Smirnoff snpackets = npackets; 7939ff95228SGleb Smirnoff npackets = 0; 7949ff95228SGleb Smirnoff } else 7959ff95228SGleb Smirnoff snpackets = 1; 7969ff95228SGleb Smirnoff datalen = sweepmin; 7979ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 7989ff95228SGleb Smirnoff } 7999ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 8009ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 80185456935SBill Fenner 8028fae3551SRodney W. Grimes /* 8038fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 8048fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 8058fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 80643470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 80743470e3bSGarrett Wollman * or multicast pings if they wish. 8088fae3551SRodney W. Grimes */ 8094fba6582SMaxim Konovalov 8104fba6582SMaxim Konovalov /* 8114fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8124fba6582SMaxim Konovalov * as well. 8134fba6582SMaxim Konovalov */ 8144fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 81549133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8168fae3551SRodney W. Grimes sizeof(hold)); 81749133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 81849133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 8197bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 82049133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 821261e59bbSMaxim Konovalov if (uid == 0) 82249133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 823e8bd25ceSRobert Watson sizeof(hold)); 82449133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 82549133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 8267bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 82749133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 828e8bd25ceSRobert Watson 82999490edeSWarner Losh if (to->sin_family == AF_INET) { 83099490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 83199490edeSWarner Losh inet_ntoa(to->sin_addr)); 83299490edeSWarner Losh if (source) 83399490edeSWarner Losh (void)printf(" from %s", shostname); 8349ff95228SGleb Smirnoff if (sweepmax) 8359ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8369ff95228SGleb Smirnoff sweepmin, sweepmax); 8379ff95228SGleb Smirnoff else 83899490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8399ff95228SGleb Smirnoff 8409ff95228SGleb Smirnoff } else { 8419ff95228SGleb Smirnoff if (sweepmax) 8429ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8439ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8449ff95228SGleb Smirnoff else 8458fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8469ff95228SGleb Smirnoff } 8478fae3551SRodney W. Grimes 8487d81b35cSBruce Evans /* 849a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 850a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8517d81b35cSBruce Evans */ 852a2a00888SSean Eric Fagan 853f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 85437e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 855a2a00888SSean Eric Fagan 856a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 857a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 858a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 859a2a00888SSean Eric Fagan } 860a2a00888SSean Eric Fagan 861a2a00888SSean Eric Fagan si_sa.sa_handler = status; 86237e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 863624ff938SWarner Losh err(EX_OSERR, "sigaction"); 86437e5b2c6SSean Eric Fagan } 865bf113f1bSBill Fumerola 866bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8677237fd94SBill Fumerola si_sa.sa_handler = stopit; 8687237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 8697237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 870bf113f1bSBill Fumerola } 87137e5b2c6SSean Eric Fagan 872039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 873039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 874039d6aa4SBill Fenner msg.msg_iov = &iov; 875039d6aa4SBill Fenner msg.msg_iovlen = 1; 876039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 877039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 878*67511a4cSAlan Somers msg.msg_controllen = sizeof(ctrl); 879039d6aa4SBill Fenner #endif 880039d6aa4SBill Fenner iov.iov_base = packet; 881e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 882039d6aa4SBill Fenner 88332af342fSRuslan Ermilov if (preload == 0) 88432af342fSRuslan Ermilov pinger(); /* send the first ping */ 88532af342fSRuslan Ermilov else { 88632af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 88732af342fSRuslan Ermilov preload = npackets; 8888fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 8898fae3551SRodney W. Grimes pinger(); 89032af342fSRuslan Ermilov } 8911ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 8928fae3551SRodney W. Grimes 893039d6aa4SBill Fenner if (options & F_FLOOD) { 894039d6aa4SBill Fenner intvl.tv_sec = 0; 8951ad76f1bSAlan Somers intvl.tv_nsec = 10000000; 896039d6aa4SBill Fenner } else { 897526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 8981ad76f1bSAlan Somers intvl.tv_nsec = interval % 1000 * 1000000; 899039d6aa4SBill Fenner } 900039d6aa4SBill Fenner 901261e59bbSMaxim Konovalov almost_done = 0; 9028f975bb3SBruce Evans while (!finish_up) { 9031ad76f1bSAlan Somers struct timespec now, timeout; 904039d6aa4SBill Fenner fd_set rfds; 905d9cacf60SAlan Somers int n; 906d9cacf60SAlan Somers ssize_t cc; 9078fae3551SRodney W. Grimes 9087d81b35cSBruce Evans check_status(); 90949133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 9107e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 911039d6aa4SBill Fenner FD_ZERO(&rfds); 91249133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 9131ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 9141ad76f1bSAlan Somers timespecadd(&last, &intvl, &timeout); 9151ad76f1bSAlan Somers timespecsub(&timeout, &now, &timeout); 916039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9171ad76f1bSAlan Somers timespecclear(&timeout); 9181ad76f1bSAlan Somers n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL); 9195e2cc0f4SStephen McKay if (n < 0) 9205e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 921039d6aa4SBill Fenner if (n == 1) { 9221ad76f1bSAlan Somers struct timespec *tv = NULL; 923039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 924*67511a4cSAlan Somers struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); 925039d6aa4SBill Fenner #endif 926039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 92749133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9288fae3551SRodney W. Grimes if (errno == EINTR) 9298fae3551SRodney W. Grimes continue; 930e345a80dSPhilippe Charnier warn("recvmsg"); 9318fae3551SRodney W. Grimes continue; 9328fae3551SRodney W. Grimes } 933039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 934039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 935039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 93631eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 937fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 938fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 93931eac03bSSean Chittenden tv = &now; 940fa05a94cSJohn Birrell } 941039d6aa4SBill Fenner #endif 94231eac03bSSean Chittenden if (tv == NULL) { 9431ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 94431eac03bSSean Chittenden tv = &now; 945039d6aa4SBill Fenner } 94631eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 94731eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 94831eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9498fae3551SRodney W. Grimes break; 9508fae3551SRodney W. Grimes } 9515e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9529ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 9539ff95228SGleb Smirnoff for (i = 0; i < sweepincr ; ++i) 9549ff95228SGleb Smirnoff *datap++ = i; 9559ff95228SGleb Smirnoff datalen += sweepincr; 9569ff95228SGleb Smirnoff if (datalen > sweepmax) 9579ff95228SGleb Smirnoff break; 9589ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9599ff95228SGleb Smirnoff sntransmitted = 0; 9609ff95228SGleb Smirnoff } 961039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 962039d6aa4SBill Fenner pinger(); 963039d6aa4SBill Fenner else { 964039d6aa4SBill Fenner if (almost_done) 965039d6aa4SBill Fenner break; 966039d6aa4SBill Fenner almost_done = 1; 9671ad76f1bSAlan Somers intvl.tv_nsec = 0; 968039d6aa4SBill Fenner if (nreceived) { 969039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 970039d6aa4SBill Fenner if (!intvl.tv_sec) 971039d6aa4SBill Fenner intvl.tv_sec = 1; 972d6cd1497SGleb Smirnoff } else { 973d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 9741ad76f1bSAlan Somers intvl.tv_nsec = waittime % 1000 * 1000000; 975d6cd1497SGleb Smirnoff } 976039d6aa4SBill Fenner } 9771ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 97825107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 97925107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 98025107197SIan Dowse if (options & F_MISSED) 981ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 982039d6aa4SBill Fenner } 983039d6aa4SBill Fenner } 98425107197SIan Dowse } 9858f975bb3SBruce Evans finish(); 9868fae3551SRodney W. Grimes /* NOTREACHED */ 987f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 9888fae3551SRodney W. Grimes } 9898fae3551SRodney W. Grimes 9908fae3551SRodney W. Grimes /* 9918f975bb3SBruce Evans * stopit -- 9928f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 9938f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 9948f975bb3SBruce Evans * to be called from a signal handler. 995515dd2faSJulian Elischer */ 996515dd2faSJulian Elischer void 997fafb8f11SEd Schouten stopit(int sig __unused) 998515dd2faSJulian Elischer { 999efc8588dSDavid E. O'Brien 1000c8bb99e5SIan Dowse /* 1001c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 1002c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 1003c8bb99e5SIan Dowse */ 1004c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 1005c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 1006515dd2faSJulian Elischer finish_up = 1; 1007515dd2faSJulian Elischer } 1008515dd2faSJulian Elischer 1009515dd2faSJulian Elischer /* 10108fae3551SRodney W. Grimes * pinger -- 10118fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10128fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1013eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10141ad76f1bSAlan Somers * bytes of the data portion are used to hold a UNIX "timespec" struct in 10154fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10168fae3551SRodney W. Grimes */ 101743470e3bSGarrett Wollman static void 101843470e3bSGarrett Wollman pinger(void) 10198fae3551SRodney W. Grimes { 10201ad76f1bSAlan Somers struct timespec now; 102113e3f0b7SMaxim Konovalov struct tv32 tv32; 1022d9cacf60SAlan Somers struct icmp icp; 1023efc8588dSDavid E. O'Brien int cc, i; 10240b2f8b3fSMaxim Konovalov u_char *packet; 10258fae3551SRodney W. Grimes 10260b2f8b3fSMaxim Konovalov packet = outpack; 1027d9cacf60SAlan Somers memcpy(&icp, outpack, ICMP_MINLEN + phdr_len); 1028d9cacf60SAlan Somers icp.icmp_type = icmp_type; 1029d9cacf60SAlan Somers icp.icmp_code = 0; 1030d9cacf60SAlan Somers icp.icmp_cksum = 0; 1031d9cacf60SAlan Somers icp.icmp_seq = htons(ntransmitted); 1032d9cacf60SAlan Somers icp.icmp_id = ident; /* ID */ 10338fae3551SRodney W. Grimes 10345db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10358fae3551SRodney W. Grimes 1036eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 10371ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 10381ad76f1bSAlan Somers /* 10391ad76f1bSAlan Somers * Truncate seconds down to 32 bits in order 10401ad76f1bSAlan Somers * to fit the timestamp within 8 bytes of the 10411ad76f1bSAlan Somers * packet. We're only concerned with 10421ad76f1bSAlan Somers * durations, not absolute times. 10431ad76f1bSAlan Somers */ 10441ad76f1bSAlan Somers tv32.tv32_sec = (uint32_t)htonl(now.tv_sec); 10451ad76f1bSAlan Somers tv32.tv32_nsec = (uint32_t)htonl(now.tv_nsec); 1046eb1543c6SMatthew N. Dodd if (options & F_TIME) 1047d9cacf60SAlan Somers icp.icmp_otime = htonl((now.tv_sec % (24*60*60)) 10481ad76f1bSAlan Somers * 1000 + now.tv_nsec / 1000000); 1049eb1543c6SMatthew N. Dodd if (timing) 105013e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10510fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 105213e3f0b7SMaxim Konovalov sizeof(tv32)); 1053eb1543c6SMatthew N. Dodd } 1054eb1543c6SMatthew N. Dodd 1055d9cacf60SAlan Somers memcpy(outpack, &icp, ICMP_MINLEN + phdr_len); 1056d9cacf60SAlan Somers 10570fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10588fae3551SRodney W. Grimes 10598fae3551SRodney W. Grimes /* compute ICMP checksum here */ 1060d9cacf60SAlan Somers icp.icmp_cksum = in_cksum(outpack, cc); 1061d9cacf60SAlan Somers /* Update icmp_cksum in the raw packet data buffer. */ 1062d9cacf60SAlan Somers memcpy(outpack + offsetof(struct icmp, icmp_cksum), &icp.icmp_cksum, 1063d9cacf60SAlan Somers sizeof(icp.icmp_cksum)); 10648fae3551SRodney W. Grimes 10650b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 1066d9cacf60SAlan Somers struct ip ip; 1067d9cacf60SAlan Somers 10680b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 1069d9cacf60SAlan Somers ip.ip_len = htons(cc); 1070d9cacf60SAlan Somers /* Update ip_len in the raw packet data buffer. */ 1071d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_len), &ip.ip_len, 1072d9cacf60SAlan Somers sizeof(ip.ip_len)); 1073d9cacf60SAlan Somers ip.ip_sum = in_cksum(outpackhdr, cc); 1074d9cacf60SAlan Somers /* Update ip_sum in the raw packet data buffer. */ 1075d9cacf60SAlan Somers memcpy(outpackhdr + offsetof(struct ip, ip_sum), &ip.ip_sum, 1076d9cacf60SAlan Somers sizeof(ip.ip_sum)); 10770b2f8b3fSMaxim Konovalov packet = outpackhdr; 10780b2f8b3fSMaxim Konovalov } 107949133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 10808fae3551SRodney W. Grimes if (i < 0 || i != cc) { 108143470e3bSGarrett Wollman if (i < 0) { 10828f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1083515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1084515dd2faSJulian Elischer return; 1085515dd2faSJulian Elischer } 108643470e3bSGarrett Wollman warn("sendto"); 108743470e3bSGarrett Wollman } else { 108843470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1089416aa49bSPoul-Henning Kamp hostname, i, cc); 10908fae3551SRodney W. Grimes } 1091363d7bbeSJulian Elischer } 1092363d7bbeSJulian Elischer ntransmitted++; 10939ff95228SGleb Smirnoff sntransmitted++; 10948fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 10958fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 10968fae3551SRodney W. Grimes } 10978fae3551SRodney W. Grimes 10988fae3551SRodney W. Grimes /* 10998fae3551SRodney W. Grimes * pr_pack -- 11008fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 11018fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 11028fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 11038fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 11048fae3551SRodney W. Grimes */ 110543470e3bSGarrett Wollman static void 1106d9cacf60SAlan Somers pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, struct timespec *tv) 11078fae3551SRodney W. Grimes { 11089b219646SPeter Wemm struct in_addr ina; 1109d9cacf60SAlan Somers u_char *cp, *dp, l; 1110d9cacf60SAlan Somers struct icmp icp; 1111d9cacf60SAlan Somers struct ip ip; 1112d9cacf60SAlan Somers const u_char *icmp_data_raw; 11138f975bb3SBruce Evans double triptime; 11142c29d74cSAlan Somers int dupflag, hlen, i, j, recv_len; 11152c29d74cSAlan Somers uint16_t seq; 1116efc8588dSDavid E. O'Brien static int old_rrlen; 1117efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 1118d9cacf60SAlan Somers struct ip oip; 1119d9cacf60SAlan Somers u_char oip_header_len; 1120d9cacf60SAlan Somers struct icmp oicmp; 1121d9cacf60SAlan Somers const u_char *oicmp_raw; 1122d9cacf60SAlan Somers 1123d9cacf60SAlan Somers /* 1124d9cacf60SAlan Somers * Get size of IP header of the received packet. The 1125d9cacf60SAlan Somers * information is contained in the lower four bits of the 1126d9cacf60SAlan Somers * first byte. 1127d9cacf60SAlan Somers */ 1128d9cacf60SAlan Somers memcpy(&l, buf, sizeof(l)); 1129d9cacf60SAlan Somers hlen = (l & 0x0f) << 2; 1130d9cacf60SAlan Somers memcpy(&ip, buf, hlen); 11318fae3551SRodney W. Grimes 11328fae3551SRodney W. Grimes /* Check the IP header */ 1133e88178ddSMaxim Konovalov recv_len = cc; 11348fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11358fae3551SRodney W. Grimes if (options & F_VERBOSE) 1136d9cacf60SAlan Somers warn("packet too short (%zd bytes) from %s", cc, 113743470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11388fae3551SRodney W. Grimes return; 11398fae3551SRodney W. Grimes } 11408fae3551SRodney W. Grimes 1141d9cacf60SAlan Somers #ifndef icmp_data 1142d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_ip); 1143d9cacf60SAlan Somers #else 1144d9cacf60SAlan Somers icmp_data_raw = buf + hlen + offsetof(struct icmp, icmp_data); 1145d9cacf60SAlan Somers #endif 1146d9cacf60SAlan Somers 11478fae3551SRodney W. Grimes /* Now the ICMP part */ 11488fae3551SRodney W. Grimes cc -= hlen; 1149d9cacf60SAlan Somers memcpy(&icp, buf + hlen, MIN((ssize_t)sizeof(icp), cc)); 1150d9cacf60SAlan Somers if (icp.icmp_type == icmp_type_rsp) { 1151d9cacf60SAlan Somers if (icp.icmp_id != ident) 11528fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11538fae3551SRodney W. Grimes ++nreceived; 11548f975bb3SBruce Evans triptime = 0.0; 11558fae3551SRodney W. Grimes if (timing) { 11561ad76f1bSAlan Somers struct timespec tv1; 115713e3f0b7SMaxim Konovalov struct tv32 tv32; 1158d9cacf60SAlan Somers const u_char *tp; 1159d9cacf60SAlan Somers 1160d9cacf60SAlan Somers tp = icmp_data_raw + phdr_len; 1161143008a1SMatthew N. Dodd 11627e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 11637e9489e0SHiroki Sato sizeof(tv1)) { 11649d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 116513e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 116613e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 11671ad76f1bSAlan Somers tv1.tv_nsec = ntohl(tv32.tv32_nsec); 11681ad76f1bSAlan Somers timespecsub(tv, &tv1, tv); 1169039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 11701ad76f1bSAlan Somers ((double)tv->tv_nsec) / 1000000.0; 11718fae3551SRodney W. Grimes tsum += triptime; 11723109a910SGarrett Wollman tsumsq += triptime * triptime; 11738fae3551SRodney W. Grimes if (triptime < tmin) 11748fae3551SRodney W. Grimes tmin = triptime; 11758fae3551SRodney W. Grimes if (triptime > tmax) 11768fae3551SRodney W. Grimes tmax = triptime; 117747e9b3eaSMatthew N. Dodd } else 117847e9b3eaSMatthew N. Dodd timing = 0; 11798fae3551SRodney W. Grimes } 11808fae3551SRodney W. Grimes 1181d9cacf60SAlan Somers seq = ntohs(icp.icmp_seq); 11825db89bc7SBill Fenner 11835db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 11848fae3551SRodney W. Grimes ++nrepeats; 11858fae3551SRodney W. Grimes --nreceived; 11868fae3551SRodney W. Grimes dupflag = 1; 11878fae3551SRodney W. Grimes } else { 11885db89bc7SBill Fenner SET(seq % mx_dup_ck); 11898fae3551SRodney W. Grimes dupflag = 0; 11908fae3551SRodney W. Grimes } 11918fae3551SRodney W. Grimes 11928fae3551SRodney W. Grimes if (options & F_QUIET) 11938fae3551SRodney W. Grimes return; 11948fae3551SRodney W. Grimes 1195d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1196d6cd1497SGleb Smirnoff ++nrcvtimeout; 1197d6cd1497SGleb Smirnoff return; 1198d6cd1497SGleb Smirnoff } 1199d6cd1497SGleb Smirnoff 12008fae3551SRodney W. Grimes if (options & F_FLOOD) 12018fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 12028fae3551SRodney W. Grimes else { 1203d9cacf60SAlan Somers (void)printf("%zd bytes from %s: icmp_seq=%u", cc, 1204229e8bf2SAlan Somers pr_addr(from->sin_addr), seq); 1205d9cacf60SAlan Somers (void)printf(" ttl=%d", ip.ip_ttl); 12068fae3551SRodney W. Grimes if (timing) 1207d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 12088fae3551SRodney W. Grimes if (dupflag) 12098fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1210772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1211ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1212143008a1SMatthew N. Dodd if (options & F_MASK) { 1213143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1214143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1215d9cacf60SAlan Somers inet_ntoa(*(struct in_addr *)&(icp.icmp_mask))); 1216143008a1SMatthew N. Dodd } 1217eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1218d9cacf60SAlan Somers (void)printf(" tso=%s", pr_ntime(icp.icmp_otime)); 1219d9cacf60SAlan Somers (void)printf(" tsr=%s", pr_ntime(icp.icmp_rtime)); 1220d9cacf60SAlan Somers (void)printf(" tst=%s", pr_ntime(icp.icmp_ttime)); 1221eb1543c6SMatthew N. Dodd } 1222e88178ddSMaxim Konovalov if (recv_len != send_len) { 1223e88178ddSMaxim Konovalov (void)printf( 1224e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1225e88178ddSMaxim Konovalov recv_len, send_len); 1226e88178ddSMaxim Konovalov } 12278fae3551SRodney W. Grimes /* check the data */ 1228d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + offsetof(struct icmp, 1229d9cacf60SAlan Somers icmp_data) + phdr_len); 12300fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 123147e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 123229dccd6aSMaxim Konovalov i = 0; 123329dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 123429dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 123529dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 123629dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 123729dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 123829dccd6aSMaxim Konovalov } 123929dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12408fae3551SRodney W. Grimes if (*cp != *dp) { 12418fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12428fae3551SRodney W. Grimes i, *dp, *cp); 12431ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 1244d9cacf60SAlan Somers cp = (u_char*)(buf + hlen + 1245d9cacf60SAlan Somers offsetof(struct icmp, icmp_data)); 1246d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1247e88178ddSMaxim Konovalov if ((i % 16) == 8) 1248d32ff037SJohn Birrell (void)printf("\n\t"); 1249e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1250d32ff037SJohn Birrell } 12511ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 12520fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1253d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1254e88178ddSMaxim Konovalov if ((i % 16) == 8) 12558fae3551SRodney W. Grimes (void)printf("\n\t"); 1256e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 12578fae3551SRodney W. Grimes } 12588fae3551SRodney W. Grimes break; 12598fae3551SRodney W. Grimes } 12608fae3551SRodney W. Grimes } 12618fae3551SRodney W. Grimes } 12628fae3551SRodney W. Grimes } else { 1263ef9e6dc7SBill Fenner /* 1264ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1265ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1266ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1267ef9e6dc7SBill Fenner * and ICMP type and ID. 1268f78ac61bSWarner Losh * 1269f78ac61bSWarner Losh * Only print all the error messages if we are running 1270f78ac61bSWarner Losh * as root to avoid leaking information not normally 1271f78ac61bSWarner Losh * available to those not running as root. 1272ef9e6dc7SBill Fenner */ 1273d9cacf60SAlan Somers memcpy(&oip_header_len, icmp_data_raw, sizeof(oip_header_len)); 1274d9cacf60SAlan Somers oip_header_len = (oip_header_len & 0x0f) << 2; 1275d9cacf60SAlan Somers memcpy(&oip, icmp_data_raw, oip_header_len); 1276d9cacf60SAlan Somers oicmp_raw = icmp_data_raw + oip_header_len; 1277d9cacf60SAlan Somers memcpy(&oicmp, oicmp_raw, offsetof(struct icmp, icmp_id) + 1278d9cacf60SAlan Somers sizeof(oicmp.icmp_id)); 1279ef9e6dc7SBill Fenner 1280ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1281ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1282d9cacf60SAlan Somers (oip.ip_dst.s_addr == whereto.sin_addr.s_addr) && 1283d9cacf60SAlan Somers (oip.ip_p == IPPROTO_ICMP) && 1284d9cacf60SAlan Somers (oicmp.icmp_type == ICMP_ECHO) && 1285d9cacf60SAlan Somers (oicmp.icmp_id == ident))) { 1286d9cacf60SAlan Somers (void)printf("%zd bytes from %s: ", cc, 128743470e3bSGarrett Wollman pr_addr(from->sin_addr)); 1288d9cacf60SAlan Somers pr_icmph(&icp, &oip, oicmp_raw); 1289ef9e6dc7SBill Fenner } else 1290ef9e6dc7SBill Fenner return; 12918fae3551SRodney W. Grimes } 12928fae3551SRodney W. Grimes 12938fae3551SRodney W. Grimes /* Display any IP options */ 12948fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 12958fae3551SRodney W. Grimes 12968fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 12978fae3551SRodney W. Grimes switch (*cp) { 12988fae3551SRodney W. Grimes case IPOPT_EOL: 12998fae3551SRodney W. Grimes hlen = 0; 13008fae3551SRodney W. Grimes break; 13018fae3551SRodney W. Grimes case IPOPT_LSRR: 1302cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1303cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1304cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1305301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 13068fae3551SRodney W. Grimes hlen -= 2; 1307301207dfSMaxim Konovalov cp += 2; 13083c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 13093c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 13108fae3551SRodney W. Grimes for (;;) { 1311301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1312301207dfSMaxim Konovalov if (ina.s_addr == 0) 13131ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1314301207dfSMaxim Konovalov else 13151ad0b1beSMaxim Konovalov (void)printf("\t%s", 13161ad0b1beSMaxim Konovalov pr_addr(ina)); 1317301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1318301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1319301207dfSMaxim Konovalov j -= INADDR_LEN; 1320301207dfSMaxim Konovalov if (j < INADDR_LEN) 13218fae3551SRodney W. Grimes break; 13228fae3551SRodney W. Grimes (void)putchar('\n'); 13238fae3551SRodney W. Grimes } 1324301207dfSMaxim Konovalov } else 1325301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 13268fae3551SRodney W. Grimes break; 13278fae3551SRodney W. Grimes case IPOPT_RR: 1328301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1329301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 13308fae3551SRodney W. Grimes hlen -= 2; 1331301207dfSMaxim Konovalov cp += 2; 13328fae3551SRodney W. Grimes if (i > j) 13338fae3551SRodney W. Grimes i = j; 1334301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1335301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1336301207dfSMaxim Konovalov old_rrlen = 0; 13378fae3551SRodney W. Grimes continue; 1338301207dfSMaxim Konovalov } 13398fae3551SRodney W. Grimes if (i == old_rrlen 13408fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 13418fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 13428fae3551SRodney W. Grimes (void)printf("\t(same route)"); 13438fae3551SRodney W. Grimes hlen -= i; 13448fae3551SRodney W. Grimes cp += i; 13458fae3551SRodney W. Grimes break; 13468fae3551SRodney W. Grimes } 13478fae3551SRodney W. Grimes old_rrlen = i; 13488fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 13498fae3551SRodney W. Grimes (void)printf("\nRR: "); 1350301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1351301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 13528fae3551SRodney W. Grimes for (;;) { 1353301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1354301207dfSMaxim Konovalov if (ina.s_addr == 0) 13551ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1356301207dfSMaxim Konovalov else 1357301207dfSMaxim Konovalov (void)printf("\t%s", 1358301207dfSMaxim Konovalov pr_addr(ina)); 1359301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1360301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1361301207dfSMaxim Konovalov i -= INADDR_LEN; 1362301207dfSMaxim Konovalov if (i < INADDR_LEN) 13638fae3551SRodney W. Grimes break; 13648fae3551SRodney W. Grimes (void)putchar('\n'); 13658fae3551SRodney W. Grimes } 1366301207dfSMaxim Konovalov } else 1367301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 13688fae3551SRodney W. Grimes break; 13698fae3551SRodney W. Grimes case IPOPT_NOP: 13708fae3551SRodney W. Grimes (void)printf("\nNOP"); 13718fae3551SRodney W. Grimes break; 13728fae3551SRodney W. Grimes default: 13738fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 13748fae3551SRodney W. Grimes break; 13758fae3551SRodney W. Grimes } 13768fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 13778fae3551SRodney W. Grimes (void)putchar('\n'); 13788fae3551SRodney W. Grimes (void)fflush(stdout); 13798fae3551SRodney W. Grimes } 13808fae3551SRodney W. Grimes } 13818fae3551SRodney W. Grimes 13828fae3551SRodney W. Grimes /* 1383badd8138SSean Eric Fagan * status -- 1384badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1385badd8138SSean Eric Fagan */ 1386badd8138SSean Eric Fagan 138743470e3bSGarrett Wollman static void 1388fafb8f11SEd Schouten status(int sig __unused) 13897d81b35cSBruce Evans { 1390efc8588dSDavid E. O'Brien 139137e5b2c6SSean Eric Fagan siginfo_p = 1; 139237e5b2c6SSean Eric Fagan } 139337e5b2c6SSean Eric Fagan 139443470e3bSGarrett Wollman static void 1395fafb8f11SEd Schouten check_status(void) 1396badd8138SSean Eric Fagan { 1397efc8588dSDavid E. O'Brien 139837e5b2c6SSean Eric Fagan if (siginfo_p) { 139937e5b2c6SSean Eric Fagan siginfo_p = 0; 1400aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 14017d81b35cSBruce Evans nreceived, ntransmitted, 1402aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1403aed98a27SMaxim Konovalov if (nreceived && timing) 1404aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1405aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1406aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 140737e5b2c6SSean Eric Fagan } 1408badd8138SSean Eric Fagan } 1409badd8138SSean Eric Fagan 1410badd8138SSean Eric Fagan /* 14118fae3551SRodney W. Grimes * finish -- 14128fae3551SRodney W. Grimes * Print out statistics, and give up. 14138fae3551SRodney W. Grimes */ 141443470e3bSGarrett Wollman static void 1415fafb8f11SEd Schouten finish(void) 14168fae3551SRodney W. Grimes { 14178fae3551SRodney W. Grimes 14188fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1419a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 14208fae3551SRodney W. Grimes (void)putchar('\n'); 14218fae3551SRodney W. Grimes (void)fflush(stdout); 14228fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 14238fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 14248fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 14258fae3551SRodney W. Grimes if (nrepeats) 14268fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1427ebe70c8fSWarner Losh if (ntransmitted) { 14288fae3551SRodney W. Grimes if (nreceived > ntransmitted) 14298fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 14308fae3551SRodney W. Grimes else 1431aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1432aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1433aa822c39SDima Dorfman ntransmitted); 1434ebe70c8fSWarner Losh } 1435d6cd1497SGleb Smirnoff if (nrcvtimeout) 1436d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 14378fae3551SRodney W. Grimes (void)putchar('\n'); 14383109a910SGarrett Wollman if (nreceived && timing) { 14393109a910SGarrett Wollman double n = nreceived + nrepeats; 14403109a910SGarrett Wollman double avg = tsum / n; 14413109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 14421ad0b1beSMaxim Konovalov (void)printf( 14431ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 14443109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 14453109a910SGarrett Wollman } 1446badd8138SSean Eric Fagan 14476e1173dcSDavid Greenman if (nreceived) 14488fae3551SRodney W. Grimes exit(0); 14496e1173dcSDavid Greenman else 14506e1173dcSDavid Greenman exit(2); 14518fae3551SRodney W. Grimes } 14528fae3551SRodney W. Grimes 14538fae3551SRodney W. Grimes #ifdef notdef 14548fae3551SRodney W. Grimes static char *ttab[] = { 14558fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 14568fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 14578fae3551SRodney W. Grimes "Source Quench", /* IP */ 14588fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 14598fae3551SRodney W. Grimes "Echo", 14608fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 14618fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 14628fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 14638fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 14648fae3551SRodney W. Grimes "Info Request", /* id + sq */ 14658fae3551SRodney W. Grimes "Info Reply" /* " */ 14668fae3551SRodney W. Grimes }; 14678fae3551SRodney W. Grimes #endif 14688fae3551SRodney W. Grimes 14698fae3551SRodney W. Grimes /* 14708fae3551SRodney W. Grimes * pr_icmph -- 14718fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 14728fae3551SRodney W. Grimes */ 147343470e3bSGarrett Wollman static void 1474d9cacf60SAlan Somers pr_icmph(struct icmp *icp, struct ip *oip, const u_char *const oicmp_raw) 14758fae3551SRodney W. Grimes { 1476efc8588dSDavid E. O'Brien 14778fae3551SRodney W. Grimes switch(icp->icmp_type) { 14788fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 14798fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 14808fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 14818fae3551SRodney W. Grimes break; 14828fae3551SRodney W. Grimes case ICMP_UNREACH: 14838fae3551SRodney W. Grimes switch(icp->icmp_code) { 14848fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 14858fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 14868fae3551SRodney W. Grimes break; 14878fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 14888fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 14898fae3551SRodney W. Grimes break; 14908fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 14918fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 14928fae3551SRodney W. Grimes break; 14938fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 14948fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 14958fae3551SRodney W. Grimes break; 14968fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1497ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1498ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 14998fae3551SRodney W. Grimes break; 15008fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 15018fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 15028fae3551SRodney W. Grimes break; 1503ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1504ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1505ef9e6dc7SBill Fenner break; 15068fae3551SRodney W. Grimes default: 15078fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 15088fae3551SRodney W. Grimes icp->icmp_code); 15098fae3551SRodney W. Grimes break; 15108fae3551SRodney W. Grimes } 15118fae3551SRodney W. Grimes /* Print returned IP header information */ 1512d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15138fae3551SRodney W. Grimes break; 15148fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 15158fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 1516d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15178fae3551SRodney W. Grimes break; 15188fae3551SRodney W. Grimes case ICMP_REDIRECT: 15198fae3551SRodney W. Grimes switch(icp->icmp_code) { 15208fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 15218fae3551SRodney W. Grimes (void)printf("Redirect Network"); 15228fae3551SRodney W. Grimes break; 15238fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 15248fae3551SRodney W. Grimes (void)printf("Redirect Host"); 15258fae3551SRodney W. Grimes break; 15268fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15278fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15288fae3551SRodney W. Grimes break; 15298fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15308fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 15318fae3551SRodney W. Grimes break; 15328fae3551SRodney W. Grimes default: 15338fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 15348fae3551SRodney W. Grimes break; 15358fae3551SRodney W. Grimes } 1536ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 1537d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15388fae3551SRodney W. Grimes break; 15398fae3551SRodney W. Grimes case ICMP_ECHO: 15408fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 15418fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15428fae3551SRodney W. Grimes break; 15438fae3551SRodney W. Grimes case ICMP_TIMXCEED: 15448fae3551SRodney W. Grimes switch(icp->icmp_code) { 15458fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 15468fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 15478fae3551SRodney W. Grimes break; 15488fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 15498fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 15508fae3551SRodney W. Grimes break; 15518fae3551SRodney W. Grimes default: 15528fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 15538fae3551SRodney W. Grimes icp->icmp_code); 15548fae3551SRodney W. Grimes break; 15558fae3551SRodney W. Grimes } 1556d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15578fae3551SRodney W. Grimes break; 15588fae3551SRodney W. Grimes case ICMP_PARAMPROB: 15598fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 15608fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 1561d9cacf60SAlan Somers pr_retip(oip, oicmp_raw); 15628fae3551SRodney W. Grimes break; 15638fae3551SRodney W. Grimes case ICMP_TSTAMP: 15648fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 15658fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 15668fae3551SRodney W. Grimes break; 15678fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 15688fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 15698fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 15708fae3551SRodney W. Grimes break; 15718fae3551SRodney W. Grimes case ICMP_IREQ: 15728fae3551SRodney W. Grimes (void)printf("Information Request\n"); 15738fae3551SRodney W. Grimes /* XXX ID + Seq */ 15748fae3551SRodney W. Grimes break; 15758fae3551SRodney W. Grimes case ICMP_IREQREPLY: 15768fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 15778fae3551SRodney W. Grimes /* XXX ID + Seq */ 15788fae3551SRodney W. Grimes break; 15798fae3551SRodney W. Grimes case ICMP_MASKREQ: 15808fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 15818fae3551SRodney W. Grimes break; 15828fae3551SRodney W. Grimes case ICMP_MASKREPLY: 15838fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 15848fae3551SRodney W. Grimes break; 1585ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1586ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1587ef9e6dc7SBill Fenner break; 1588ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1589ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1590ef9e6dc7SBill Fenner break; 15918fae3551SRodney W. Grimes default: 15928fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 15938fae3551SRodney W. Grimes } 15948fae3551SRodney W. Grimes } 15958fae3551SRodney W. Grimes 15968fae3551SRodney W. Grimes /* 15978fae3551SRodney W. Grimes * pr_iph -- 15988fae3551SRodney W. Grimes * Print an IP header with options. 15998fae3551SRodney W. Grimes */ 160043470e3bSGarrett Wollman static void 1601fafb8f11SEd Schouten pr_iph(struct ip *ip) 16028fae3551SRodney W. Grimes { 1603a94c074dSDimitry Andric struct in_addr ina; 16048fae3551SRodney W. Grimes u_char *cp; 1605efc8588dSDavid E. O'Brien int hlen; 16068fae3551SRodney W. Grimes 16078fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 16088fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 16098fae3551SRodney W. Grimes 1610ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 16118fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1612ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1613ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1614d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1615d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1616d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1617ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1618ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1619a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1620a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1621a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1622a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1623ef9e6dc7SBill Fenner /* dump any option bytes */ 16248fae3551SRodney W. Grimes while (hlen-- > 20) { 16258fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16268fae3551SRodney W. Grimes } 16278fae3551SRodney W. Grimes (void)putchar('\n'); 16288fae3551SRodney W. Grimes } 16298fae3551SRodney W. Grimes 16308fae3551SRodney W. Grimes /* 16318fae3551SRodney W. Grimes * pr_addr -- 16328fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 16338fae3551SRodney W. Grimes * a hostname. 16348fae3551SRodney W. Grimes */ 163543470e3bSGarrett Wollman static char * 1636fafb8f11SEd Schouten pr_addr(struct in_addr ina) 16378fae3551SRodney W. Grimes { 16388fae3551SRodney W. Grimes struct hostent *hp; 1639f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 16408fae3551SRodney W. Grimes 164149133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 164243470e3bSGarrett Wollman return inet_ntoa(ina); 164349133c6dSPawel Jakub Dawidek 164449133c6dSPawel Jakub Dawidek hp = cap_gethostbyaddr(capdns, (char *)&ina, 4, AF_INET); 164549133c6dSPawel Jakub Dawidek 164649133c6dSPawel Jakub Dawidek if (hp == NULL) 164749133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 164849133c6dSPawel Jakub Dawidek 1649efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 165043470e3bSGarrett Wollman inet_ntoa(ina)); 16518fae3551SRodney W. Grimes return(buf); 16528fae3551SRodney W. Grimes } 16538fae3551SRodney W. Grimes 16548fae3551SRodney W. Grimes /* 16558fae3551SRodney W. Grimes * pr_retip -- 16568fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 16578fae3551SRodney W. Grimes */ 165843470e3bSGarrett Wollman static void 1659d9cacf60SAlan Somers pr_retip(struct ip *ip, const u_char *cp) 16608fae3551SRodney W. Grimes { 16618fae3551SRodney W. Grimes pr_iph(ip); 16628fae3551SRodney W. Grimes 16638fae3551SRodney W. Grimes if (ip->ip_p == 6) 16648fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 16658fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 16668fae3551SRodney W. Grimes else if (ip->ip_p == 17) 16678fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 16688fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 16698fae3551SRodney W. Grimes } 16708fae3551SRodney W. Grimes 1671eb1543c6SMatthew N. Dodd static char * 1672007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1673eb1543c6SMatthew N. Dodd { 16747898770aSAlan Somers static char buf[11]; 1675007fe4e3SMaxim Konovalov int hour, min, sec; 1676eb1543c6SMatthew N. Dodd 1677007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1678007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1679007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1680007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1681eb1543c6SMatthew N. Dodd 1682007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1683eb1543c6SMatthew N. Dodd 1684eb1543c6SMatthew N. Dodd return (buf); 1685eb1543c6SMatthew N. Dodd } 1686eb1543c6SMatthew N. Dodd 168743470e3bSGarrett Wollman static void 1688fafb8f11SEd Schouten fill(char *bp, char *patp) 16898fae3551SRodney W. Grimes { 16908fae3551SRodney W. Grimes char *cp; 1691efc8588dSDavid E. O'Brien int pat[16]; 16924fba6582SMaxim Konovalov u_int ii, jj, kk; 16938fae3551SRodney W. Grimes 169443470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 169543470e3bSGarrett Wollman if (!isxdigit(*cp)) 169643470e3bSGarrett Wollman errx(EX_USAGE, 169743470e3bSGarrett Wollman "patterns must be specified as hex digits"); 169843470e3bSGarrett Wollman 16998fae3551SRodney W. Grimes } 17008fae3551SRodney W. Grimes ii = sscanf(patp, 17018fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 17028fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 17038fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 17048fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 17058fae3551SRodney W. Grimes 17068fae3551SRodney W. Grimes if (ii > 0) 1707d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 17088fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17098fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17108fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17118fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17128fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17138fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17148fae3551SRodney W. Grimes (void)printf("\n"); 17158fae3551SRodney W. Grimes } 17168fae3551SRodney W. Grimes } 17178fae3551SRodney W. Grimes 171849133c6dSPawel Jakub Dawidek static cap_channel_t * 171949133c6dSPawel Jakub Dawidek capdns_setup(void) 172049133c6dSPawel Jakub Dawidek { 172149133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 1722a3ce7698SAlan Somers #ifdef WITH_CASPER 172349133c6dSPawel Jakub Dawidek const char *types[2]; 172449133c6dSPawel Jakub Dawidek int families[1]; 1725a3ce7698SAlan Somers #endif 172649133c6dSPawel Jakub Dawidek capcas = cap_init(); 1727c501d73cSMariusz Zaborski if (capcas == NULL) 1728c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 172949133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 173049133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 173149133c6dSPawel Jakub Dawidek cap_close(capcas); 173249133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 173349133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 1734a3ce7698SAlan Somers #ifdef WITH_CASPER 1735752d135eSMariusz Zaborski types[0] = "NAME2ADDR"; 1736752d135eSMariusz Zaborski types[1] = "ADDR2NAME"; 173749133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 173849133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 173949133c6dSPawel Jakub Dawidek families[0] = AF_INET; 174049133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 174149133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 1742a3ce7698SAlan Somers #endif 174349133c6dSPawel Jakub Dawidek return (capdnsloc); 174449133c6dSPawel Jakub Dawidek } 174549133c6dSPawel Jakub Dawidek 1746120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 1747120b4a93SRuslan Ermilov #define SECOPT " [-P policy]" 1748120b4a93SRuslan Ermilov #else 1749120b4a93SRuslan Ermilov #define SECOPT "" 1750120b4a93SRuslan Ermilov #endif 175143470e3bSGarrett Wollman static void 1752fafb8f11SEd Schouten usage(void) 17538fae3551SRodney W. Grimes { 175431eac03bSSean Chittenden 1755d6cd1497SGleb Smirnoff (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 175699f13ae1SAlan Somers "usage: ping [-AaDdfHnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]", 1757ee3e1c4cSRuslan Ermilov " [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]", 1758ee3e1c4cSRuslan Ermilov " " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]", 1759d6cd1497SGleb Smirnoff " [-W waittime] [-z tos] host", 176099f13ae1SAlan Somers " ping [-AaDdfHLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]", 1761120b4a93SRuslan Ermilov " [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]", 1762d6cd1497SGleb Smirnoff " [-s packetsize] [-T ttl] [-t timeout] [-W waittime]", 1763d6cd1497SGleb Smirnoff " [-z tos] mcast-group"); 176443470e3bSGarrett Wollman exit(EX_USAGE); 17658fae3551SRodney W. Grimes } 1766