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> 94261e59bbSMaxim Konovalov #include <signal.h> 95261e59bbSMaxim Konovalov #include <stdio.h> 96261e59bbSMaxim Konovalov #include <stdlib.h> 97261e59bbSMaxim Konovalov #include <string.h> 98261e59bbSMaxim Konovalov #include <sysexits.h> 991ad76f1bSAlan Somers #include <time.h> 100261e59bbSMaxim Konovalov #include <unistd.h> 101261e59bbSMaxim Konovalov 102*ff77ab83SAlan Somers #include "utils.h" 103*ff77ab83SAlan Somers 104301207dfSMaxim Konovalov #define INADDR_LEN ((int)sizeof(in_addr_t)) 10513e3f0b7SMaxim Konovalov #define TIMEVAL_LEN ((int)sizeof(struct tv32)) 106eb1543c6SMatthew N. Dodd #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) 107eb1543c6SMatthew N. Dodd #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) 108c67c1ce8SMatthew N. Dodd #define DEFDATALEN 56 /* default data length */ 1098f975bb3SBruce Evans #define FLOOD_BACKOFF 20000 /* usecs to back off if F_FLOOD mode */ 1108f975bb3SBruce Evans /* runs out of buffer space */ 1114fba6582SMaxim Konovalov #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) 1124fba6582SMaxim Konovalov #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) 113d6cd1497SGleb Smirnoff #define MAXWAIT 10000 /* max ms to wait for response */ 114bf113f1bSBill Fumerola #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ 1150b2f8b3fSMaxim Konovalov #define MAXTOS 255 1168fae3551SRodney W. Grimes 1178fae3551SRodney W. Grimes #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ 1188fae3551SRodney W. Grimes #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ 1198fae3551SRodney W. Grimes #define SET(bit) (A(bit) |= B(bit)) 1208fae3551SRodney W. Grimes #define CLR(bit) (A(bit) &= (~B(bit))) 1218fae3551SRodney W. Grimes #define TST(bit) (A(bit) & B(bit)) 1228fae3551SRodney W. Grimes 12313e3f0b7SMaxim Konovalov struct tv32 { 12413e3f0b7SMaxim Konovalov int32_t tv32_sec; 1251ad76f1bSAlan Somers int32_t tv32_nsec; 12613e3f0b7SMaxim Konovalov }; 12713e3f0b7SMaxim Konovalov 1288fae3551SRodney W. Grimes /* various options */ 1297e9489e0SHiroki Sato static int options; 13085456935SBill Fenner #define F_FLOOD 0x0001 13185456935SBill Fenner #define F_INTERVAL 0x0002 13285456935SBill Fenner #define F_NUMERIC 0x0004 13385456935SBill Fenner #define F_PINGFILLED 0x0008 13485456935SBill Fenner #define F_QUIET 0x0010 13585456935SBill Fenner #define F_RROUTE 0x0020 13685456935SBill Fenner #define F_SO_DEBUG 0x0040 13785456935SBill Fenner #define F_SO_DONTROUTE 0x0080 13885456935SBill Fenner #define F_VERBOSE 0x0100 13985456935SBill Fenner #define F_QUIET2 0x0200 14085456935SBill Fenner #define F_NOLOOP 0x0400 14185456935SBill Fenner #define F_MTTL 0x0800 14285456935SBill Fenner #define F_MIF 0x1000 143772dfa72SDaniel O'Callaghan #define F_AUDIBLE 0x2000 1449a4365d0SYoshinobu Inoue #ifdef IPSEC 1459a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 1469a4365d0SYoshinobu Inoue #define F_POLICY 0x4000 1479a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 1489a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 149211bfbd2SRuslan Ermilov #define F_TTL 0x8000 150ca517ad8SPoul-Henning Kamp #define F_MISSED 0x10000 1518025c44bSDima Dorfman #define F_ONCE 0x20000 1520b2f8b3fSMaxim Konovalov #define F_HDRINCL 0x40000 153143008a1SMatthew N. Dodd #define F_MASK 0x80000 154eb1543c6SMatthew N. Dodd #define F_TIME 0x100000 1559ff95228SGleb Smirnoff #define F_SWEEP 0x200000 156d6cd1497SGleb Smirnoff #define F_WAITTIME 0x400000 1578fae3551SRodney W. Grimes 1588fae3551SRodney W. Grimes /* 1598fae3551SRodney W. Grimes * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum 1608fae3551SRodney W. Grimes * number of received sequence numbers we can keep track of. Change 128 1618fae3551SRodney W. Grimes * to 8192 for complete accuracy... 1628fae3551SRodney W. Grimes */ 1638fae3551SRodney W. Grimes #define MAX_DUP_CHK (8 * 128) 1647e9489e0SHiroki Sato static int mx_dup_ck = MAX_DUP_CHK; 1657e9489e0SHiroki Sato static char rcvd_tbl[MAX_DUP_CHK / 8]; 1668fae3551SRodney W. Grimes 1677e9489e0SHiroki Sato static struct sockaddr_in whereto; /* who to ping */ 1687e9489e0SHiroki Sato static int datalen = DEFDATALEN; 1697e9489e0SHiroki Sato static int maxpayload; 1707e9489e0SHiroki Sato static int ssend; /* send socket file descriptor */ 1717e9489e0SHiroki Sato static int srecv; /* receive socket file descriptor */ 1727e9489e0SHiroki Sato static u_char outpackhdr[IP_MAXPACKET], *outpack; 1737e9489e0SHiroki Sato static char BBELL = '\a'; /* characters written for MISSED and AUDIBLE */ 1747e9489e0SHiroki Sato static char BSPACE = '\b'; /* characters written for flood */ 1757e9489e0SHiroki Sato static char DOT = '.'; 1767e9489e0SHiroki Sato static char *hostname; 1777e9489e0SHiroki Sato static char *shostname; 1787e9489e0SHiroki Sato static int ident; /* process id to identify our packets */ 1797e9489e0SHiroki Sato static int uid; /* cached uid for micro-optimization */ 1807e9489e0SHiroki Sato static u_char icmp_type = ICMP_ECHO; 1817e9489e0SHiroki Sato static u_char icmp_type_rsp = ICMP_ECHOREPLY; 1827e9489e0SHiroki Sato static int phdr_len = 0; 1837e9489e0SHiroki Sato static int send_len; 1848fae3551SRodney W. Grimes 1858fae3551SRodney W. Grimes /* counters */ 1867e9489e0SHiroki Sato static long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ 1877e9489e0SHiroki Sato static long npackets; /* max packets to transmit */ 1887e9489e0SHiroki Sato static long nreceived; /* # of packets we got back */ 1897e9489e0SHiroki Sato static long nrepeats; /* number of duplicates */ 1907e9489e0SHiroki Sato static long ntransmitted; /* sequence # for outbound packets = #sent */ 1917e9489e0SHiroki Sato static long snpackets; /* max packets to transmit in one sweep */ 1927e9489e0SHiroki Sato static long sntransmitted; /* # of packets we sent in this sweep */ 1937e9489e0SHiroki Sato static int sweepmax; /* max value of payload in sweep */ 1947e9489e0SHiroki Sato static int sweepmin = 0; /* start value of payload in sweep */ 1957e9489e0SHiroki Sato static int sweepincr = 1; /* payload increment in sweep */ 1967e9489e0SHiroki Sato static int interval = 1000; /* interval between packets, ms */ 1977e9489e0SHiroki Sato static int waittime = MAXWAIT; /* timeout for each packet */ 1987e9489e0SHiroki Sato static long nrcvtimeout = 0; /* # of packets we got back after waittime */ 1998fae3551SRodney W. Grimes 2008fae3551SRodney W. Grimes /* timing */ 2017e9489e0SHiroki Sato static int timing; /* flag to do timing */ 2027e9489e0SHiroki Sato static double tmin = 999999999.0; /* minimum round trip time */ 2037e9489e0SHiroki Sato static double tmax = 0.0; /* maximum round trip time */ 2047e9489e0SHiroki Sato static double tsum = 0.0; /* sum of all times, for doing average */ 2057e9489e0SHiroki Sato static double tsumsq = 0.0; /* sum of all times squared, for std. dev. */ 2068fae3551SRodney W. Grimes 2077e9489e0SHiroki Sato /* nonzero if we've been told to finish up */ 2087e9489e0SHiroki Sato static volatile sig_atomic_t finish_up; 2097e9489e0SHiroki Sato static volatile sig_atomic_t siginfo_p; 210badd8138SSean Eric Fagan 21149133c6dSPawel Jakub Dawidek static cap_channel_t *capdns; 21249133c6dSPawel Jakub Dawidek 21343470e3bSGarrett Wollman static void fill(char *, char *); 21449133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void); 21543470e3bSGarrett Wollman static void check_status(void); 2168f975bb3SBruce Evans static void finish(void) __dead2; 21743470e3bSGarrett Wollman static void pinger(void); 21843470e3bSGarrett Wollman static char *pr_addr(struct in_addr); 219eb1543c6SMatthew N. Dodd static char *pr_ntime(n_time); 22043470e3bSGarrett Wollman static void pr_icmph(struct icmp *); 22143470e3bSGarrett Wollman static void pr_iph(struct ip *); 2221ad76f1bSAlan Somers static void pr_pack(char *, int, struct sockaddr_in *, struct timespec *); 22343470e3bSGarrett Wollman static void pr_retip(struct ip *); 22443470e3bSGarrett Wollman static void status(int); 2258f975bb3SBruce Evans static void stopit(int); 226e345a80dSPhilippe Charnier static void usage(void) __dead2; 2278fae3551SRodney W. Grimes 22843470e3bSGarrett Wollman int 229fafb8f11SEd Schouten main(int argc, char *const *argv) 2308fae3551SRodney W. Grimes { 23131eac03bSSean Chittenden struct sockaddr_in from, sock_in; 232efc8588dSDavid E. O'Brien struct in_addr ifaddr; 2331ad76f1bSAlan Somers struct timespec last, intvl; 234efc8588dSDavid E. O'Brien struct iovec iov; 2350b2f8b3fSMaxim Konovalov struct ip *ip; 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 264f1284d7aSBill Fenner /* 265f1284d7aSBill Fenner * Do the stuff that we need root priv's for *first*, and 266f1284d7aSBill Fenner * then drop our setuid bit. Save error reporting for 267f1284d7aSBill Fenner * after arg parsing. 26849133c6dSPawel Jakub Dawidek * 26949133c6dSPawel Jakub Dawidek * Historicaly ping was using one socket 's' for sending and for 27049133c6dSPawel Jakub Dawidek * receiving. After capsicum(4) related changes we use two 27149133c6dSPawel Jakub Dawidek * sockets. It was done for special ping use case - when user 27249133c6dSPawel Jakub Dawidek * issue ping on multicast or broadcast address replies come 27349133c6dSPawel Jakub Dawidek * from different addresses, not from the address we 27449133c6dSPawel Jakub Dawidek * connect(2)'ed to, and send socket do not receive those 27549133c6dSPawel Jakub Dawidek * packets. 276f1284d7aSBill Fenner */ 27749133c6dSPawel Jakub Dawidek ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 27849133c6dSPawel Jakub Dawidek ssend_errno = errno; 27949133c6dSPawel Jakub Dawidek srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 28049133c6dSPawel Jakub Dawidek srecv_errno = errno; 281f1284d7aSBill Fenner 2821d1d4a47SEitan Adler if (setuid(getuid()) != 0) 2831d1d4a47SEitan Adler err(EX_NOPERM, "setuid() failed"); 284ee2bf734SWarner Losh uid = getuid(); 285f1284d7aSBill Fenner 286eeb63943SDon Lewis if (ssend < 0) { 287eeb63943SDon Lewis errno = ssend_errno; 288eeb63943SDon Lewis err(EX_OSERR, "ssend socket"); 289eeb63943SDon Lewis } 290eeb63943SDon Lewis 291eeb63943SDon Lewis if (srecv < 0) { 292eeb63943SDon Lewis errno = srecv_errno; 293eeb63943SDon Lewis err(EX_OSERR, "srecv socket"); 294eeb63943SDon Lewis } 295eeb63943SDon Lewis 2960b2f8b3fSMaxim Konovalov alarmtimeout = df = preload = tos = 0; 297badd8138SSean Eric Fagan 2980b2f8b3fSMaxim Konovalov outpack = outpackhdr + sizeof(struct ip); 299211bfbd2SRuslan Ermilov while ((ch = getopt(argc, argv, 300d6cd1497SGleb Smirnoff "Aac:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:" 301211bfbd2SRuslan Ermilov #ifdef IPSEC 3029a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 303211bfbd2SRuslan Ermilov "P:" 3049a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 305211bfbd2SRuslan Ermilov #endif /*IPSEC*/ 306211bfbd2SRuslan Ermilov )) != -1) 3079a4365d0SYoshinobu Inoue { 3088fae3551SRodney W. Grimes switch(ch) { 309ca517ad8SPoul-Henning Kamp case 'A': 310ca517ad8SPoul-Henning Kamp options |= F_MISSED; 311ca517ad8SPoul-Henning Kamp break; 312772dfa72SDaniel O'Callaghan case 'a': 313772dfa72SDaniel O'Callaghan options |= F_AUDIBLE; 314772dfa72SDaniel O'Callaghan break; 3158fae3551SRodney W. Grimes case 'c': 316c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 31765c3a67dSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 31843470e3bSGarrett Wollman errx(EX_USAGE, 31943470e3bSGarrett Wollman "invalid count of packets to transmit: `%s'", 32043470e3bSGarrett Wollman optarg); 321c0a3773aSEugene Grosbein npackets = ltmp; 3228fae3551SRodney W. Grimes break; 3230b2f8b3fSMaxim Konovalov case 'D': 3240b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 3250b2f8b3fSMaxim Konovalov df = 1; 3260b2f8b3fSMaxim Konovalov break; 3278fae3551SRodney W. Grimes case 'd': 3288fae3551SRodney W. Grimes options |= F_SO_DEBUG; 3298fae3551SRodney W. Grimes break; 3308fae3551SRodney W. Grimes case 'f': 331526f06b2SMatthew Dillon if (uid) { 33243470e3bSGarrett Wollman errno = EPERM; 33343470e3bSGarrett Wollman err(EX_NOPERM, "-f flag"); 3348fae3551SRodney W. Grimes } 3358fae3551SRodney W. Grimes options |= F_FLOOD; 3368fae3551SRodney W. Grimes setbuf(stdout, (char *)NULL); 3378fae3551SRodney W. Grimes break; 3389ff95228SGleb Smirnoff case 'G': /* Maximum packet size for ping sweep */ 339c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 340c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 3419ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3429ff95228SGleb Smirnoff optarg); 343c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3449ff95228SGleb Smirnoff errno = EPERM; 3459ff95228SGleb Smirnoff err(EX_NOPERM, 346c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 347c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3489ff95228SGleb Smirnoff } 3499ff95228SGleb Smirnoff options |= F_SWEEP; 350c0a3773aSEugene Grosbein sweepmax = ltmp; 3519ff95228SGleb Smirnoff break; 3529ff95228SGleb Smirnoff case 'g': /* Minimum packet size for ping sweep */ 353c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 354c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp <= 0) 3559ff95228SGleb Smirnoff errx(EX_USAGE, "invalid packet size: `%s'", 3569ff95228SGleb Smirnoff optarg); 357c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3589ff95228SGleb Smirnoff errno = EPERM; 3599ff95228SGleb Smirnoff err(EX_NOPERM, 360c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 361c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3629ff95228SGleb Smirnoff } 3639ff95228SGleb Smirnoff options |= F_SWEEP; 364c0a3773aSEugene Grosbein sweepmin = ltmp; 3659ff95228SGleb Smirnoff break; 3669ff95228SGleb Smirnoff case 'h': /* Packet size increment for ping sweep */ 367c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 368c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp < 1) 3699ff95228SGleb Smirnoff errx(EX_USAGE, "invalid increment size: `%s'", 3709ff95228SGleb Smirnoff optarg); 371c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 3729ff95228SGleb Smirnoff errno = EPERM; 3739ff95228SGleb Smirnoff err(EX_NOPERM, 374c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 375c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 3769ff95228SGleb Smirnoff } 3779ff95228SGleb Smirnoff options |= F_SWEEP; 378c0a3773aSEugene Grosbein sweepincr = ltmp; 3799ff95228SGleb Smirnoff break; 3801f6a4631SRuslan Ermilov case 'I': /* multicast interface */ 3811f6a4631SRuslan Ermilov if (inet_aton(optarg, &ifaddr) == 0) 3821f6a4631SRuslan Ermilov errx(EX_USAGE, 3831f6a4631SRuslan Ermilov "invalid multicast interface: `%s'", 3841f6a4631SRuslan Ermilov optarg); 3851f6a4631SRuslan Ermilov options |= F_MIF; 3861f6a4631SRuslan Ermilov break; 3878fae3551SRodney W. Grimes case 'i': /* wait between sending packets */ 3881ad0b1beSMaxim Konovalov t = strtod(optarg, &ep) * 1000.0; 3891ad0b1beSMaxim Konovalov if (*ep || ep == optarg || t > (double)INT_MAX) 3901ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid timing interval: `%s'", 3911ad0b1beSMaxim Konovalov optarg); 3928fae3551SRodney W. Grimes options |= F_INTERVAL; 393526f06b2SMatthew Dillon interval = (int)t; 394526f06b2SMatthew Dillon if (uid && interval < 1000) { 395526f06b2SMatthew Dillon errno = EPERM; 396526f06b2SMatthew Dillon err(EX_NOPERM, "-i interval too short"); 397526f06b2SMatthew Dillon } 3988fae3551SRodney W. Grimes break; 3991f6a4631SRuslan Ermilov case 'L': 4001f6a4631SRuslan Ermilov options |= F_NOLOOP; 4011f6a4631SRuslan Ermilov loop = 0; 40285456935SBill Fenner break; 4038fae3551SRodney W. Grimes case 'l': 404c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 405c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > INT_MAX || ltmp < 0) 40643470e3bSGarrett Wollman errx(EX_USAGE, 40743470e3bSGarrett Wollman "invalid preload value: `%s'", optarg); 4085e2cc0f4SStephen McKay if (uid) { 409f78ac61bSWarner Losh errno = EPERM; 410f78ac61bSWarner Losh err(EX_NOPERM, "-l flag"); 411f78ac61bSWarner Losh } 412c0a3773aSEugene Grosbein preload = ltmp; 4138fae3551SRodney W. Grimes break; 4141f6a4631SRuslan Ermilov case 'M': 415eb1543c6SMatthew N. Dodd switch(optarg[0]) { 416eb1543c6SMatthew N. Dodd case 'M': 417eb1543c6SMatthew N. Dodd case 'm': 4181f6a4631SRuslan Ermilov options |= F_MASK; 41985456935SBill Fenner break; 420eb1543c6SMatthew N. Dodd case 'T': 421eb1543c6SMatthew N. Dodd case 't': 422eb1543c6SMatthew N. Dodd options |= F_TIME; 423eb1543c6SMatthew N. Dodd break; 424eb1543c6SMatthew N. Dodd default: 425eb1543c6SMatthew N. Dodd errx(EX_USAGE, "invalid message: `%c'", optarg[0]); 426eb1543c6SMatthew N. Dodd break; 427eb1543c6SMatthew N. Dodd } 428eb1543c6SMatthew N. Dodd break; 429211bfbd2SRuslan Ermilov case 'm': /* TTL */ 430c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 431c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTTL || ltmp < 0) 4321ad0b1beSMaxim Konovalov errx(EX_USAGE, "invalid TTL: `%s'", optarg); 433c0a3773aSEugene Grosbein ttl = ltmp; 434211bfbd2SRuslan Ermilov options |= F_TTL; 435211bfbd2SRuslan Ermilov break; 4368fae3551SRodney W. Grimes case 'n': 4378fae3551SRodney W. Grimes options |= F_NUMERIC; 4388fae3551SRodney W. Grimes break; 4398025c44bSDima Dorfman case 'o': 4408025c44bSDima Dorfman options |= F_ONCE; 4418025c44bSDima Dorfman break; 4421f6a4631SRuslan Ermilov #ifdef IPSEC 4431f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC 4441f6a4631SRuslan Ermilov case 'P': 4451f6a4631SRuslan Ermilov options |= F_POLICY; 4461f6a4631SRuslan Ermilov if (!strncmp("in", optarg, 2)) 4471f6a4631SRuslan Ermilov policy_in = strdup(optarg); 4481f6a4631SRuslan Ermilov else if (!strncmp("out", optarg, 3)) 4491f6a4631SRuslan Ermilov policy_out = strdup(optarg); 4501f6a4631SRuslan Ermilov else 4511f6a4631SRuslan Ermilov errx(1, "invalid security policy"); 4521f6a4631SRuslan Ermilov break; 4531f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/ 4541f6a4631SRuslan Ermilov #endif /*IPSEC*/ 4558fae3551SRodney W. Grimes case 'p': /* fill buffer with user pattern */ 4568fae3551SRodney W. Grimes options |= F_PINGFILLED; 457d074d39fSMatthew N. Dodd payload = optarg; 4588fae3551SRodney W. Grimes break; 459ef9e6dc7SBill Fenner case 'Q': 460ef9e6dc7SBill Fenner options |= F_QUIET2; 461ef9e6dc7SBill Fenner break; 4628fae3551SRodney W. Grimes case 'q': 4638fae3551SRodney W. Grimes options |= F_QUIET; 4648fae3551SRodney W. Grimes break; 4658fae3551SRodney W. Grimes case 'R': 4668fae3551SRodney W. Grimes options |= F_RROUTE; 4678fae3551SRodney W. Grimes break; 4688fae3551SRodney W. Grimes case 'r': 4698fae3551SRodney W. Grimes options |= F_SO_DONTROUTE; 4708fae3551SRodney W. Grimes break; 4711f6a4631SRuslan Ermilov case 'S': 4721f6a4631SRuslan Ermilov source = optarg; 4731f6a4631SRuslan Ermilov break; 4748fae3551SRodney W. Grimes case 's': /* size of packet to send */ 475c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 476c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp < 0) 47743470e3bSGarrett Wollman errx(EX_USAGE, "invalid packet size: `%s'", 47843470e3bSGarrett Wollman optarg); 479c0a3773aSEugene Grosbein if (uid != 0 && ltmp > DEFDATALEN) { 480fb7d32c7SMaxim Konovalov errno = EPERM; 481fb7d32c7SMaxim Konovalov err(EX_NOPERM, 482c0a3773aSEugene Grosbein "packet size too large: %ld > %u", 483c0a3773aSEugene Grosbein ltmp, DEFDATALEN); 484fb7d32c7SMaxim Konovalov } 485c0a3773aSEugene Grosbein datalen = ltmp; 4868fae3551SRodney W. Grimes break; 4871f6a4631SRuslan Ermilov case 'T': /* multicast TTL */ 488c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 489c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTTL || ltmp < 0) 4901f6a4631SRuslan Ermilov errx(EX_USAGE, "invalid multicast TTL: `%s'", 4911f6a4631SRuslan Ermilov optarg); 492c0a3773aSEugene Grosbein mttl = ltmp; 4931f6a4631SRuslan Ermilov options |= F_MTTL; 49499490edeSWarner Losh break; 4957237fd94SBill Fumerola case 't': 496bf113f1bSBill Fumerola alarmtimeout = strtoul(optarg, &ep, 0); 497bf113f1bSBill Fumerola if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX)) 4987237fd94SBill Fumerola errx(EX_USAGE, "invalid timeout: `%s'", 4997237fd94SBill Fumerola optarg); 500bf113f1bSBill Fumerola if (alarmtimeout > MAXALARM) 501bf113f1bSBill Fumerola errx(EX_USAGE, "invalid timeout: `%s' > %d", 502bf113f1bSBill Fumerola optarg, MAXALARM); 503bf113f1bSBill Fumerola alarm((int)alarmtimeout); 5047237fd94SBill Fumerola break; 5058fae3551SRodney W. Grimes case 'v': 5068fae3551SRodney W. Grimes options |= F_VERBOSE; 5078fae3551SRodney W. Grimes break; 508d6cd1497SGleb Smirnoff case 'W': /* wait ms for answer */ 509d6cd1497SGleb Smirnoff t = strtod(optarg, &ep); 510d6cd1497SGleb Smirnoff if (*ep || ep == optarg || t > (double)INT_MAX) 511d6cd1497SGleb Smirnoff errx(EX_USAGE, "invalid timing interval: `%s'", 512d6cd1497SGleb Smirnoff optarg); 513d6cd1497SGleb Smirnoff options |= F_WAITTIME; 514d6cd1497SGleb Smirnoff waittime = (int)t; 515d6cd1497SGleb Smirnoff break; 5160b2f8b3fSMaxim Konovalov case 'z': 5170b2f8b3fSMaxim Konovalov options |= F_HDRINCL; 518c0a3773aSEugene Grosbein ltmp = strtol(optarg, &ep, 0); 519c0a3773aSEugene Grosbein if (*ep || ep == optarg || ltmp > MAXTOS || ltmp < 0) 5200b2f8b3fSMaxim Konovalov errx(EX_USAGE, "invalid TOS: `%s'", optarg); 521c0a3773aSEugene Grosbein tos = ltmp; 5220b2f8b3fSMaxim Konovalov break; 5238fae3551SRodney W. Grimes default: 524e345a80dSPhilippe Charnier usage(); 52543470e3bSGarrett Wollman } 52643470e3bSGarrett Wollman } 52743470e3bSGarrett Wollman 52843470e3bSGarrett Wollman if (argc - optind != 1) 529e345a80dSPhilippe Charnier usage(); 53043470e3bSGarrett Wollman target = argv[optind]; 5318fae3551SRodney W. Grimes 532d829c3dfSMatthew N. Dodd switch (options & (F_MASK|F_TIME)) { 533d829c3dfSMatthew N. Dodd case 0: break; 534d829c3dfSMatthew N. Dodd case F_MASK: 535eb1543c6SMatthew N. Dodd icmp_type = ICMP_MASKREQ; 536eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_MASKREPLY; 537d829c3dfSMatthew N. Dodd phdr_len = MASK_LEN; 538eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 539eb1543c6SMatthew N. Dodd (void)printf("ICMP_MASKREQ\n"); 540d829c3dfSMatthew N. Dodd break; 541d829c3dfSMatthew N. Dodd case F_TIME: 542eb1543c6SMatthew N. Dodd icmp_type = ICMP_TSTAMP; 543eb1543c6SMatthew N. Dodd icmp_type_rsp = ICMP_TSTAMPREPLY; 544d829c3dfSMatthew N. Dodd phdr_len = TS_LEN; 545eb1543c6SMatthew N. Dodd if (!(options & F_QUIET)) 546eb1543c6SMatthew N. Dodd (void)printf("ICMP_TSTAMP\n"); 547d829c3dfSMatthew N. Dodd break; 548d829c3dfSMatthew N. Dodd default: 549d829c3dfSMatthew N. Dodd errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive."); 550d829c3dfSMatthew N. Dodd break; 551eb1543c6SMatthew N. Dodd } 5520fe0c0ccSMaxim Konovalov icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len; 553fb7d32c7SMaxim Konovalov if (options & F_RROUTE) 554e88178ddSMaxim Konovalov icmp_len += MAX_IPOPTLEN; 555e88178ddSMaxim Konovalov maxpayload = IP_MAXPACKET - icmp_len; 556fb7d32c7SMaxim Konovalov if (datalen > maxpayload) 5571104dd84SBruce Evans errx(EX_USAGE, "packet size too large: %d > %d", datalen, 558fb7d32c7SMaxim Konovalov maxpayload); 559e88178ddSMaxim Konovalov send_len = icmp_len + datalen; 5600fe0c0ccSMaxim Konovalov datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN]; 561d074d39fSMatthew N. Dodd if (options & F_PINGFILLED) { 562d074d39fSMatthew N. Dodd fill((char *)datap, payload); 563d074d39fSMatthew N. Dodd } 56449133c6dSPawel Jakub Dawidek capdns = capdns_setup(); 56599490edeSWarner Losh if (source) { 56631eac03bSSean Chittenden bzero((char *)&sock_in, sizeof(sock_in)); 56731eac03bSSean Chittenden sock_in.sin_family = AF_INET; 56831eac03bSSean Chittenden if (inet_aton(source, &sock_in.sin_addr) != 0) { 56999490edeSWarner Losh shostname = source; 57099490edeSWarner Losh } else { 571d68e2c04SMariusz Zaborski hp = cap_gethostbyname2(capdns, source, AF_INET); 57299490edeSWarner Losh if (!hp) 57399490edeSWarner Losh errx(EX_NOHOST, "cannot resolve %s: %s", 57499490edeSWarner Losh source, hstrerror(h_errno)); 57599490edeSWarner Losh 57631eac03bSSean Chittenden sock_in.sin_len = sizeof sock_in; 57731eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(sock_in.sin_addr) || 5784fba6582SMaxim Konovalov hp->h_length < 0) 57999490edeSWarner Losh errx(1, "gethostbyname2: illegal address"); 58031eac03bSSean Chittenden memcpy(&sock_in.sin_addr, hp->h_addr_list[0], 58131eac03bSSean Chittenden sizeof(sock_in.sin_addr)); 58299490edeSWarner Losh (void)strncpy(snamebuf, hp->h_name, 58399490edeSWarner Losh sizeof(snamebuf) - 1); 58499490edeSWarner Losh snamebuf[sizeof(snamebuf) - 1] = '\0'; 58599490edeSWarner Losh shostname = snamebuf; 58699490edeSWarner Losh } 58749133c6dSPawel Jakub Dawidek if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) == 58849133c6dSPawel Jakub Dawidek -1) 58999490edeSWarner Losh err(1, "bind"); 59099490edeSWarner Losh } 59199490edeSWarner Losh 592d389e86aSMatt Jacob bzero(&whereto, sizeof(whereto)); 593d389e86aSMatt Jacob to = &whereto; 5948fae3551SRodney W. Grimes to->sin_family = AF_INET; 595d389e86aSMatt Jacob to->sin_len = sizeof *to; 59643470e3bSGarrett Wollman if (inet_aton(target, &to->sin_addr) != 0) { 5978fae3551SRodney W. Grimes hostname = target; 59843470e3bSGarrett Wollman } else { 59949133c6dSPawel Jakub Dawidek hp = cap_gethostbyname2(capdns, target, AF_INET); 60043470e3bSGarrett Wollman if (!hp) 60143470e3bSGarrett Wollman errx(EX_NOHOST, "cannot resolve %s: %s", 60243470e3bSGarrett Wollman target, hstrerror(h_errno)); 60343470e3bSGarrett Wollman 60431eac03bSSean Chittenden if ((unsigned)hp->h_length > sizeof(to->sin_addr)) 6051ffae4a6SWarner Losh errx(1, "gethostbyname2 returned an illegal address"); 60643470e3bSGarrett Wollman memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr); 6078fae3551SRodney W. Grimes (void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1); 608b10d9d5fSWarner Losh hnamebuf[sizeof(hnamebuf) - 1] = '\0'; 6098fae3551SRodney W. Grimes hostname = hnamebuf; 6108fae3551SRodney W. Grimes } 6118fae3551SRodney W. Grimes 61249133c6dSPawel Jakub Dawidek /* From now on we will use only reverse DNS lookups. */ 61349133c6dSPawel Jakub Dawidek if (capdns != NULL) { 61449133c6dSPawel Jakub Dawidek const char *types[1]; 61549133c6dSPawel Jakub Dawidek 616752d135eSMariusz Zaborski types[0] = "ADDR2NAME"; 61749133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdns, types, 1) < 0) 61849133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 61949133c6dSPawel Jakub Dawidek } 62049133c6dSPawel Jakub Dawidek 62149133c6dSPawel Jakub Dawidek if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0) 62249133c6dSPawel Jakub Dawidek err(1, "connect"); 62349133c6dSPawel Jakub Dawidek 62443470e3bSGarrett Wollman if (options & F_FLOOD && options & F_INTERVAL) 62543470e3bSGarrett Wollman errx(EX_USAGE, "-f and -i: incompatible options"); 62643470e3bSGarrett Wollman 62743470e3bSGarrett Wollman if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 62843470e3bSGarrett Wollman errx(EX_USAGE, 62943470e3bSGarrett Wollman "-f flag cannot be used with multicast destination"); 63043470e3bSGarrett Wollman if (options & (F_MIF | F_NOLOOP | F_MTTL) 63143470e3bSGarrett Wollman && !IN_MULTICAST(ntohl(to->sin_addr.s_addr))) 63243470e3bSGarrett Wollman errx(EX_USAGE, 63343470e3bSGarrett Wollman "-I, -L, -T flags cannot be used with unicast destination"); 6348fae3551SRodney W. Grimes 635d829c3dfSMatthew N. Dodd if (datalen >= TIMEVAL_LEN) /* can we time transfer */ 6368fae3551SRodney W. Grimes timing = 1; 63743470e3bSGarrett Wollman 6388fae3551SRodney W. Grimes if (!(options & F_PINGFILLED)) 639d829c3dfSMatthew N. Dodd for (i = TIMEVAL_LEN; i < datalen; ++i) 6408fae3551SRodney W. Grimes *datap++ = i; 6418fae3551SRodney W. Grimes 6428fae3551SRodney W. Grimes ident = getpid() & 0xFFFF; 6438fae3551SRodney W. Grimes 6448fae3551SRodney W. Grimes hold = 1; 64549133c6dSPawel Jakub Dawidek if (options & F_SO_DEBUG) { 64649133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold, 6478fae3551SRodney W. Grimes sizeof(hold)); 64849133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold, 64949133c6dSPawel Jakub Dawidek sizeof(hold)); 65049133c6dSPawel Jakub Dawidek } 6518fae3551SRodney W. Grimes if (options & F_SO_DONTROUTE) 65249133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, 6538fae3551SRodney W. Grimes sizeof(hold)); 6549a4365d0SYoshinobu Inoue #ifdef IPSEC 6559a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC 6569a4365d0SYoshinobu Inoue if (options & F_POLICY) { 6579a4365d0SYoshinobu Inoue char *buf; 6589a4365d0SYoshinobu Inoue if (policy_in != NULL) { 6599a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_in, strlen(policy_in)); 6609a4365d0SYoshinobu Inoue if (buf == NULL) 661ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 66249133c6dSPawel Jakub Dawidek if (setsockopt(srecv, IPPROTO_IP, IP_IPSEC_POLICY, 6639a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6641ad0b1beSMaxim Konovalov err(EX_CONFIG, 6651ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6669a4365d0SYoshinobu Inoue free(buf); 6679a4365d0SYoshinobu Inoue } 6689a4365d0SYoshinobu Inoue 6699a4365d0SYoshinobu Inoue if (policy_out != NULL) { 6709a4365d0SYoshinobu Inoue buf = ipsec_set_policy(policy_out, strlen(policy_out)); 6719a4365d0SYoshinobu Inoue if (buf == NULL) 672ffd40070SKris Kennaway errx(EX_CONFIG, "%s", ipsec_strerror()); 67349133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY, 6749a4365d0SYoshinobu Inoue buf, ipsec_get_policylen(buf)) < 0) 6751ad0b1beSMaxim Konovalov err(EX_CONFIG, 6761ad0b1beSMaxim Konovalov "ipsec policy cannot be configured"); 6779a4365d0SYoshinobu Inoue free(buf); 6789a4365d0SYoshinobu Inoue } 6799a4365d0SYoshinobu Inoue } 6809a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/ 6819a4365d0SYoshinobu Inoue #endif /*IPSEC*/ 6828fae3551SRodney W. Grimes 6830b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 6840b2f8b3fSMaxim Konovalov ip = (struct ip*)outpackhdr; 6850b2f8b3fSMaxim Konovalov if (!(options & (F_TTL | F_MTTL))) { 6860b2f8b3fSMaxim Konovalov mib[0] = CTL_NET; 6870b2f8b3fSMaxim Konovalov mib[1] = PF_INET; 6880b2f8b3fSMaxim Konovalov mib[2] = IPPROTO_IP; 6890b2f8b3fSMaxim Konovalov mib[3] = IPCTL_DEFTTL; 6900b2f8b3fSMaxim Konovalov sz = sizeof(ttl); 6910b2f8b3fSMaxim Konovalov if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1) 6920b2f8b3fSMaxim Konovalov err(1, "sysctl(net.inet.ip.ttl)"); 6930b2f8b3fSMaxim Konovalov } 69449133c6dSPawel Jakub Dawidek setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold)); 6950b2f8b3fSMaxim Konovalov ip->ip_v = IPVERSION; 6960b2f8b3fSMaxim Konovalov ip->ip_hl = sizeof(struct ip) >> 2; 6970b2f8b3fSMaxim Konovalov ip->ip_tos = tos; 6980b2f8b3fSMaxim Konovalov ip->ip_id = 0; 699ffc610b2SAndrey V. Elsukov ip->ip_off = htons(df ? IP_DF : 0); 7000b2f8b3fSMaxim Konovalov ip->ip_ttl = ttl; 7010b2f8b3fSMaxim Konovalov ip->ip_p = IPPROTO_ICMP; 70231eac03bSSean Chittenden ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY; 7030b2f8b3fSMaxim Konovalov ip->ip_dst = to->sin_addr; 7040b2f8b3fSMaxim Konovalov } 70549133c6dSPawel Jakub Dawidek 70649133c6dSPawel Jakub Dawidek /* 70749133c6dSPawel Jakub Dawidek * Here we enter capability mode. Further down access to global 70849133c6dSPawel Jakub Dawidek * namespaces (e.g filesystem) is restricted (see capsicum(4)). 70949133c6dSPawel Jakub Dawidek * We must connect(2) our socket before this point. 71049133c6dSPawel Jakub Dawidek */ 7117bdc3291SMark Johnston caph_cache_catpages(); 71218fcfaa4SMark Johnston if (caph_enter_casper() < 0) 71349133c6dSPawel Jakub Dawidek err(1, "cap_enter"); 71449133c6dSPawel Jakub Dawidek 71549133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT); 7167bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 71749133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv"); 71849133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT); 7197bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 72049133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend"); 72149133c6dSPawel Jakub Dawidek 7228fae3551SRodney W. Grimes /* record route option */ 7238fae3551SRodney W. Grimes if (options & F_RROUTE) { 7248fae3551SRodney W. Grimes #ifdef IP_OPTIONS 725039d6aa4SBill Fenner bzero(rspace, sizeof(rspace)); 7268fae3551SRodney W. Grimes rspace[IPOPT_OPTVAL] = IPOPT_RR; 7278fae3551SRodney W. Grimes rspace[IPOPT_OLEN] = sizeof(rspace) - 1; 7288fae3551SRodney W. Grimes rspace[IPOPT_OFFSET] = IPOPT_MINOFF; 729039d6aa4SBill Fenner rspace[sizeof(rspace) - 1] = IPOPT_EOL; 73049133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace, 73143470e3bSGarrett Wollman sizeof(rspace)) < 0) 73243470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_OPTIONS"); 7338fae3551SRodney W. Grimes #else 73443470e3bSGarrett Wollman errx(EX_UNAVAILABLE, 73543470e3bSGarrett Wollman "record route not available in this implementation"); 7368fae3551SRodney W. Grimes #endif /* IP_OPTIONS */ 7378fae3551SRodney W. Grimes } 7388fae3551SRodney W. Grimes 739211bfbd2SRuslan Ermilov if (options & F_TTL) { 74049133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl, 741211bfbd2SRuslan Ermilov sizeof(ttl)) < 0) { 742211bfbd2SRuslan Ermilov err(EX_OSERR, "setsockopt IP_TTL"); 743211bfbd2SRuslan Ermilov } 744211bfbd2SRuslan Ermilov } 74585456935SBill Fenner if (options & F_NOLOOP) { 74649133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, 74785456935SBill Fenner sizeof(loop)) < 0) { 74843470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP"); 74985456935SBill Fenner } 75085456935SBill Fenner } 75185456935SBill Fenner if (options & F_MTTL) { 75249133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, 753211bfbd2SRuslan Ermilov sizeof(mttl)) < 0) { 75443470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_TTL"); 75585456935SBill Fenner } 75685456935SBill Fenner } 75785456935SBill Fenner if (options & F_MIF) { 75849133c6dSPawel Jakub Dawidek if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr, 75985456935SBill Fenner sizeof(ifaddr)) < 0) { 76043470e3bSGarrett Wollman err(EX_OSERR, "setsockopt IP_MULTICAST_IF"); 76185456935SBill Fenner } 76285456935SBill Fenner } 763039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 76484633ef1SAlan Somers { 76584633ef1SAlan Somers int on = 1; 76684633ef1SAlan Somers int ts_clock = SO_TS_MONOTONIC; 76784633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, 76884633ef1SAlan Somers sizeof(on)) < 0) 769039d6aa4SBill Fenner err(EX_OSERR, "setsockopt SO_TIMESTAMP"); 77084633ef1SAlan Somers if (setsockopt(srecv, SOL_SOCKET, SO_TS_CLOCK, &ts_clock, 77184633ef1SAlan Somers sizeof(ts_clock)) < 0) 77284633ef1SAlan Somers err(EX_OSERR, "setsockopt SO_TS_CLOCK"); 773039d6aa4SBill Fenner } 774039d6aa4SBill Fenner #endif 7759ff95228SGleb Smirnoff if (sweepmax) { 776bb7dfe5eSGleb Smirnoff if (sweepmin > sweepmax) 777bb7dfe5eSGleb Smirnoff errx(EX_USAGE, "Maximum packet size must be no less than the minimum packet size"); 7789ff95228SGleb Smirnoff 7799ff95228SGleb Smirnoff if (datalen != DEFDATALEN) 7809ff95228SGleb Smirnoff errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive"); 7819ff95228SGleb Smirnoff 7829ff95228SGleb Smirnoff if (npackets > 0) { 7839ff95228SGleb Smirnoff snpackets = npackets; 7849ff95228SGleb Smirnoff npackets = 0; 7859ff95228SGleb Smirnoff } else 7869ff95228SGleb Smirnoff snpackets = 1; 7879ff95228SGleb Smirnoff datalen = sweepmin; 7889ff95228SGleb Smirnoff send_len = icmp_len + sweepmin; 7899ff95228SGleb Smirnoff } 7909ff95228SGleb Smirnoff if (options & F_SWEEP && !sweepmax) 7919ff95228SGleb Smirnoff errx(EX_USAGE, "Maximum sweep size must be specified"); 79285456935SBill Fenner 7938fae3551SRodney W. Grimes /* 7948fae3551SRodney W. Grimes * When pinging the broadcast address, you can get a lot of answers. 7958fae3551SRodney W. Grimes * Doing something so evil is useful if you are trying to stress the 7968fae3551SRodney W. Grimes * ethernet, or just want to fill the arp cache to get some stuff for 79743470e3bSGarrett Wollman * /etc/ethers. But beware: RFC 1122 allows hosts to ignore broadcast 79843470e3bSGarrett Wollman * or multicast pings if they wish. 7998fae3551SRodney W. Grimes */ 8004fba6582SMaxim Konovalov 8014fba6582SMaxim Konovalov /* 8024fba6582SMaxim Konovalov * XXX receive buffer needs undetermined space for mbuf overhead 8034fba6582SMaxim Konovalov * as well. 8044fba6582SMaxim Konovalov */ 8054fba6582SMaxim Konovalov hold = IP_MAXPACKET + 128; 80649133c6dSPawel Jakub Dawidek (void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold, 8078fae3551SRodney W. Grimes sizeof(hold)); 80849133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 80949133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_RECV, CAP_EVENT); 8107bdc3291SMark Johnston if (caph_rights_limit(srecv, &rights) < 0) 81149133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit srecv setsockopt"); 812261e59bbSMaxim Konovalov if (uid == 0) 81349133c6dSPawel Jakub Dawidek (void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold, 814e8bd25ceSRobert Watson sizeof(hold)); 81549133c6dSPawel Jakub Dawidek /* CAP_SETSOCKOPT removed */ 81649133c6dSPawel Jakub Dawidek cap_rights_init(&rights, CAP_SEND); 8177bdc3291SMark Johnston if (caph_rights_limit(ssend, &rights) < 0) 81849133c6dSPawel Jakub Dawidek err(1, "cap_rights_limit ssend setsockopt"); 819e8bd25ceSRobert Watson 82099490edeSWarner Losh if (to->sin_family == AF_INET) { 82199490edeSWarner Losh (void)printf("PING %s (%s)", hostname, 82299490edeSWarner Losh inet_ntoa(to->sin_addr)); 82399490edeSWarner Losh if (source) 82499490edeSWarner Losh (void)printf(" from %s", shostname); 8259ff95228SGleb Smirnoff if (sweepmax) 8269ff95228SGleb Smirnoff (void)printf(": (%d ... %d) data bytes\n", 8279ff95228SGleb Smirnoff sweepmin, sweepmax); 8289ff95228SGleb Smirnoff else 82999490edeSWarner Losh (void)printf(": %d data bytes\n", datalen); 8309ff95228SGleb Smirnoff 8319ff95228SGleb Smirnoff } else { 8329ff95228SGleb Smirnoff if (sweepmax) 8339ff95228SGleb Smirnoff (void)printf("PING %s: (%d ... %d) data bytes\n", 8349ff95228SGleb Smirnoff hostname, sweepmin, sweepmax); 8359ff95228SGleb Smirnoff else 8368fae3551SRodney W. Grimes (void)printf("PING %s: %d data bytes\n", hostname, datalen); 8379ff95228SGleb Smirnoff } 8388fae3551SRodney W. Grimes 8397d81b35cSBruce Evans /* 840a2a00888SSean Eric Fagan * Use sigaction() instead of signal() to get unambiguous semantics, 841a2a00888SSean Eric Fagan * in particular with SA_RESTART not set. 8427d81b35cSBruce Evans */ 843a2a00888SSean Eric Fagan 844f6bd468bSPaul Traina sigemptyset(&si_sa.sa_mask); 84537e5b2c6SSean Eric Fagan si_sa.sa_flags = 0; 846a2a00888SSean Eric Fagan 847a2a00888SSean Eric Fagan si_sa.sa_handler = stopit; 848a2a00888SSean Eric Fagan if (sigaction(SIGINT, &si_sa, 0) == -1) { 849a2a00888SSean Eric Fagan err(EX_OSERR, "sigaction SIGINT"); 850a2a00888SSean Eric Fagan } 851a2a00888SSean Eric Fagan 852a2a00888SSean Eric Fagan si_sa.sa_handler = status; 85337e5b2c6SSean Eric Fagan if (sigaction(SIGINFO, &si_sa, 0) == -1) { 854624ff938SWarner Losh err(EX_OSERR, "sigaction"); 85537e5b2c6SSean Eric Fagan } 856bf113f1bSBill Fumerola 857bf113f1bSBill Fumerola if (alarmtimeout > 0) { 8587237fd94SBill Fumerola si_sa.sa_handler = stopit; 8597237fd94SBill Fumerola if (sigaction(SIGALRM, &si_sa, 0) == -1) 8607237fd94SBill Fumerola err(EX_OSERR, "sigaction SIGALRM"); 861bf113f1bSBill Fumerola } 86237e5b2c6SSean Eric Fagan 863039d6aa4SBill Fenner bzero(&msg, sizeof(msg)); 864039d6aa4SBill Fenner msg.msg_name = (caddr_t)&from; 865039d6aa4SBill Fenner msg.msg_iov = &iov; 866039d6aa4SBill Fenner msg.msg_iovlen = 1; 867039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 868039d6aa4SBill Fenner msg.msg_control = (caddr_t)ctrl; 869039d6aa4SBill Fenner #endif 870039d6aa4SBill Fenner iov.iov_base = packet; 871e88178ddSMaxim Konovalov iov.iov_len = IP_MAXPACKET; 872039d6aa4SBill Fenner 87332af342fSRuslan Ermilov if (preload == 0) 87432af342fSRuslan Ermilov pinger(); /* send the first ping */ 87532af342fSRuslan Ermilov else { 87632af342fSRuslan Ermilov if (npackets != 0 && preload > npackets) 87732af342fSRuslan Ermilov preload = npackets; 8788fae3551SRodney W. Grimes while (preload--) /* fire off them quickies */ 8798fae3551SRodney W. Grimes pinger(); 88032af342fSRuslan Ermilov } 8811ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 8828fae3551SRodney W. Grimes 883039d6aa4SBill Fenner if (options & F_FLOOD) { 884039d6aa4SBill Fenner intvl.tv_sec = 0; 8851ad76f1bSAlan Somers intvl.tv_nsec = 10000000; 886039d6aa4SBill Fenner } else { 887526f06b2SMatthew Dillon intvl.tv_sec = interval / 1000; 8881ad76f1bSAlan Somers intvl.tv_nsec = interval % 1000 * 1000000; 889039d6aa4SBill Fenner } 890039d6aa4SBill Fenner 891261e59bbSMaxim Konovalov almost_done = 0; 8928f975bb3SBruce Evans while (!finish_up) { 8931ad76f1bSAlan Somers struct timespec now, timeout; 894039d6aa4SBill Fenner fd_set rfds; 895261e59bbSMaxim Konovalov int cc, n; 8968fae3551SRodney W. Grimes 8977d81b35cSBruce Evans check_status(); 89849133c6dSPawel Jakub Dawidek if ((unsigned)srecv >= FD_SETSIZE) 8997e5bbd68SJacques Vidrine errx(EX_OSERR, "descriptor too large"); 900039d6aa4SBill Fenner FD_ZERO(&rfds); 90149133c6dSPawel Jakub Dawidek FD_SET(srecv, &rfds); 9021ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 9031ad76f1bSAlan Somers timespecadd(&last, &intvl, &timeout); 9041ad76f1bSAlan Somers timespecsub(&timeout, &now, &timeout); 905039d6aa4SBill Fenner if (timeout.tv_sec < 0) 9061ad76f1bSAlan Somers timespecclear(&timeout); 9071ad76f1bSAlan Somers n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL); 9085e2cc0f4SStephen McKay if (n < 0) 9095e2cc0f4SStephen McKay continue; /* Must be EINTR. */ 910039d6aa4SBill Fenner if (n == 1) { 9111ad76f1bSAlan Somers struct timespec *tv = NULL; 912039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 913039d6aa4SBill Fenner struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl; 914039d6aa4SBill Fenner 915039d6aa4SBill Fenner msg.msg_controllen = sizeof(ctrl); 916039d6aa4SBill Fenner #endif 917039d6aa4SBill Fenner msg.msg_namelen = sizeof(from); 91849133c6dSPawel Jakub Dawidek if ((cc = recvmsg(srecv, &msg, 0)) < 0) { 9198fae3551SRodney W. Grimes if (errno == EINTR) 9208fae3551SRodney W. Grimes continue; 921e345a80dSPhilippe Charnier warn("recvmsg"); 9228fae3551SRodney W. Grimes continue; 9238fae3551SRodney W. Grimes } 924039d6aa4SBill Fenner #ifdef SO_TIMESTAMP 925039d6aa4SBill Fenner if (cmsg->cmsg_level == SOL_SOCKET && 926039d6aa4SBill Fenner cmsg->cmsg_type == SCM_TIMESTAMP && 92731eac03bSSean Chittenden cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { 928fa05a94cSJohn Birrell /* Copy to avoid alignment problems: */ 929fa05a94cSJohn Birrell memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); 93031eac03bSSean Chittenden tv = &now; 931fa05a94cSJohn Birrell } 932039d6aa4SBill Fenner #endif 93331eac03bSSean Chittenden if (tv == NULL) { 9341ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 93531eac03bSSean Chittenden tv = &now; 936039d6aa4SBill Fenner } 93731eac03bSSean Chittenden pr_pack((char *)packet, cc, &from, tv); 93831eac03bSSean Chittenden if ((options & F_ONCE && nreceived) || 93931eac03bSSean Chittenden (npackets && nreceived >= npackets)) 9408fae3551SRodney W. Grimes break; 9418fae3551SRodney W. Grimes } 9425e2cc0f4SStephen McKay if (n == 0 || options & F_FLOOD) { 9439ff95228SGleb Smirnoff if (sweepmax && sntransmitted == snpackets) { 9449ff95228SGleb Smirnoff for (i = 0; i < sweepincr ; ++i) 9459ff95228SGleb Smirnoff *datap++ = i; 9469ff95228SGleb Smirnoff datalen += sweepincr; 9479ff95228SGleb Smirnoff if (datalen > sweepmax) 9489ff95228SGleb Smirnoff break; 9499ff95228SGleb Smirnoff send_len = icmp_len + datalen; 9509ff95228SGleb Smirnoff sntransmitted = 0; 9519ff95228SGleb Smirnoff } 952039d6aa4SBill Fenner if (!npackets || ntransmitted < npackets) 953039d6aa4SBill Fenner pinger(); 954039d6aa4SBill Fenner else { 955039d6aa4SBill Fenner if (almost_done) 956039d6aa4SBill Fenner break; 957039d6aa4SBill Fenner almost_done = 1; 9581ad76f1bSAlan Somers intvl.tv_nsec = 0; 959039d6aa4SBill Fenner if (nreceived) { 960039d6aa4SBill Fenner intvl.tv_sec = 2 * tmax / 1000; 961039d6aa4SBill Fenner if (!intvl.tv_sec) 962039d6aa4SBill Fenner intvl.tv_sec = 1; 963d6cd1497SGleb Smirnoff } else { 964d6cd1497SGleb Smirnoff intvl.tv_sec = waittime / 1000; 9651ad76f1bSAlan Somers intvl.tv_nsec = waittime % 1000 * 1000000; 966d6cd1497SGleb Smirnoff } 967039d6aa4SBill Fenner } 9681ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &last); 96925107197SIan Dowse if (ntransmitted - nreceived - 1 > nmissedmax) { 97025107197SIan Dowse nmissedmax = ntransmitted - nreceived - 1; 97125107197SIan Dowse if (options & F_MISSED) 972ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 973039d6aa4SBill Fenner } 974039d6aa4SBill Fenner } 97525107197SIan Dowse } 9768f975bb3SBruce Evans finish(); 9778fae3551SRodney W. Grimes /* NOTREACHED */ 978f78ac61bSWarner Losh exit(0); /* Make the compiler happy */ 9798fae3551SRodney W. Grimes } 9808fae3551SRodney W. Grimes 9818fae3551SRodney W. Grimes /* 9828f975bb3SBruce Evans * stopit -- 9838f975bb3SBruce Evans * Set the global bit that causes the main loop to quit. 9848f975bb3SBruce Evans * Do NOT call finish() from here, since finish() does far too much 9858f975bb3SBruce Evans * to be called from a signal handler. 986515dd2faSJulian Elischer */ 987515dd2faSJulian Elischer void 988fafb8f11SEd Schouten stopit(int sig __unused) 989515dd2faSJulian Elischer { 990efc8588dSDavid E. O'Brien 991c8bb99e5SIan Dowse /* 992c8bb99e5SIan Dowse * When doing reverse DNS lookups, the finish_up flag might not 993c8bb99e5SIan Dowse * be noticed for a while. Just exit if we get a second SIGINT. 994c8bb99e5SIan Dowse */ 995c8bb99e5SIan Dowse if (!(options & F_NUMERIC) && finish_up) 996c8bb99e5SIan Dowse _exit(nreceived ? 0 : 2); 997515dd2faSJulian Elischer finish_up = 1; 998515dd2faSJulian Elischer } 999515dd2faSJulian Elischer 1000515dd2faSJulian Elischer /* 10018fae3551SRodney W. Grimes * pinger -- 10028fae3551SRodney W. Grimes * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet 10038fae3551SRodney W. Grimes * will be added on by the kernel. The ID field is our UNIX process ID, 1004eb1543c6SMatthew N. Dodd * and the sequence number is an ascending integer. The first TIMEVAL_LEN 10051ad76f1bSAlan Somers * bytes of the data portion are used to hold a UNIX "timespec" struct in 10064fba6582SMaxim Konovalov * host byte-order, to compute the round-trip time. 10078fae3551SRodney W. Grimes */ 100843470e3bSGarrett Wollman static void 100943470e3bSGarrett Wollman pinger(void) 10108fae3551SRodney W. Grimes { 10111ad76f1bSAlan Somers struct timespec now; 101213e3f0b7SMaxim Konovalov struct tv32 tv32; 10130b2f8b3fSMaxim Konovalov struct ip *ip; 10143d438ad6SDavid E. O'Brien struct icmp *icp; 1015efc8588dSDavid E. O'Brien int cc, i; 10160b2f8b3fSMaxim Konovalov u_char *packet; 10178fae3551SRodney W. Grimes 10180b2f8b3fSMaxim Konovalov packet = outpack; 10198fae3551SRodney W. Grimes icp = (struct icmp *)outpack; 1020eb1543c6SMatthew N. Dodd icp->icmp_type = icmp_type; 10218fae3551SRodney W. Grimes icp->icmp_code = 0; 10228fae3551SRodney W. Grimes icp->icmp_cksum = 0; 10235db89bc7SBill Fenner icp->icmp_seq = htons(ntransmitted); 10248fae3551SRodney W. Grimes icp->icmp_id = ident; /* ID */ 10258fae3551SRodney W. Grimes 10265db89bc7SBill Fenner CLR(ntransmitted % mx_dup_ck); 10278fae3551SRodney W. Grimes 1028eb1543c6SMatthew N. Dodd if ((options & F_TIME) || timing) { 10291ad76f1bSAlan Somers (void)clock_gettime(CLOCK_MONOTONIC, &now); 10301ad76f1bSAlan Somers /* 10311ad76f1bSAlan Somers * Truncate seconds down to 32 bits in order 10321ad76f1bSAlan Somers * to fit the timestamp within 8 bytes of the 10331ad76f1bSAlan Somers * packet. We're only concerned with 10341ad76f1bSAlan Somers * durations, not absolute times. 10351ad76f1bSAlan Somers */ 10361ad76f1bSAlan Somers tv32.tv32_sec = (uint32_t)htonl(now.tv_sec); 10371ad76f1bSAlan Somers tv32.tv32_nsec = (uint32_t)htonl(now.tv_nsec); 1038eb1543c6SMatthew N. Dodd if (options & F_TIME) 1039eb1543c6SMatthew N. Dodd icp->icmp_otime = htonl((now.tv_sec % (24*60*60)) 10401ad76f1bSAlan Somers * 1000 + now.tv_nsec / 1000000); 1041eb1543c6SMatthew N. Dodd if (timing) 104213e3f0b7SMaxim Konovalov bcopy((void *)&tv32, 10430fe0c0ccSMaxim Konovalov (void *)&outpack[ICMP_MINLEN + phdr_len], 104413e3f0b7SMaxim Konovalov sizeof(tv32)); 1045eb1543c6SMatthew N. Dodd } 1046eb1543c6SMatthew N. Dodd 10470fe0c0ccSMaxim Konovalov cc = ICMP_MINLEN + phdr_len + datalen; 10488fae3551SRodney W. Grimes 10498fae3551SRodney W. Grimes /* compute ICMP checksum here */ 1050d63a9487SAlan Somers icp->icmp_cksum = in_cksum((u_char *)icp, cc); 10518fae3551SRodney W. Grimes 10520b2f8b3fSMaxim Konovalov if (options & F_HDRINCL) { 10530b2f8b3fSMaxim Konovalov cc += sizeof(struct ip); 10540b2f8b3fSMaxim Konovalov ip = (struct ip *)outpackhdr; 1055ffc610b2SAndrey V. Elsukov ip->ip_len = htons(cc); 1056d63a9487SAlan Somers ip->ip_sum = in_cksum(outpackhdr, cc); 10570b2f8b3fSMaxim Konovalov packet = outpackhdr; 10580b2f8b3fSMaxim Konovalov } 105949133c6dSPawel Jakub Dawidek i = send(ssend, (char *)packet, cc, 0); 10608fae3551SRodney W. Grimes if (i < 0 || i != cc) { 106143470e3bSGarrett Wollman if (i < 0) { 10628f975bb3SBruce Evans if (options & F_FLOOD && errno == ENOBUFS) { 1063515dd2faSJulian Elischer usleep(FLOOD_BACKOFF); 1064515dd2faSJulian Elischer return; 1065515dd2faSJulian Elischer } 106643470e3bSGarrett Wollman warn("sendto"); 106743470e3bSGarrett Wollman } else { 106843470e3bSGarrett Wollman warn("%s: partial write: %d of %d bytes", 1069416aa49bSPoul-Henning Kamp hostname, i, cc); 10708fae3551SRodney W. Grimes } 1071363d7bbeSJulian Elischer } 1072363d7bbeSJulian Elischer ntransmitted++; 10739ff95228SGleb Smirnoff sntransmitted++; 10748fae3551SRodney W. Grimes if (!(options & F_QUIET) && options & F_FLOOD) 10758fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &DOT, 1); 10768fae3551SRodney W. Grimes } 10778fae3551SRodney W. Grimes 10788fae3551SRodney W. Grimes /* 10798fae3551SRodney W. Grimes * pr_pack -- 10808fae3551SRodney W. Grimes * Print out the packet, if it came from us. This logic is necessary 10818fae3551SRodney W. Grimes * because ALL readers of the ICMP socket get a copy of ALL ICMP packets 10828fae3551SRodney W. Grimes * which arrive ('tis only fair). This permits multiple copies of this 10838fae3551SRodney W. Grimes * program to be run without having intermingled output (or statistics!). 10848fae3551SRodney W. Grimes */ 108543470e3bSGarrett Wollman static void 10861ad76f1bSAlan Somers pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timespec *tv) 10878fae3551SRodney W. Grimes { 10889b219646SPeter Wemm struct in_addr ina; 10899b219646SPeter Wemm u_char *cp, *dp; 10903d438ad6SDavid E. O'Brien struct icmp *icp; 10918fae3551SRodney W. Grimes struct ip *ip; 10929d2b0ab8SPeter Wemm const void *tp; 10938f975bb3SBruce Evans double triptime; 10942c29d74cSAlan Somers int dupflag, hlen, i, j, recv_len; 10952c29d74cSAlan Somers uint16_t seq; 1096efc8588dSDavid E. O'Brien static int old_rrlen; 1097efc8588dSDavid E. O'Brien static char old_rr[MAX_IPOPTLEN]; 10988fae3551SRodney W. Grimes 10998fae3551SRodney W. Grimes /* Check the IP header */ 11008fae3551SRodney W. Grimes ip = (struct ip *)buf; 11018fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 1102e88178ddSMaxim Konovalov recv_len = cc; 11038fae3551SRodney W. Grimes if (cc < hlen + ICMP_MINLEN) { 11048fae3551SRodney W. Grimes if (options & F_VERBOSE) 110543470e3bSGarrett Wollman warn("packet too short (%d bytes) from %s", cc, 110643470e3bSGarrett Wollman inet_ntoa(from->sin_addr)); 11078fae3551SRodney W. Grimes return; 11088fae3551SRodney W. Grimes } 11098fae3551SRodney W. Grimes 11108fae3551SRodney W. Grimes /* Now the ICMP part */ 11118fae3551SRodney W. Grimes cc -= hlen; 11128fae3551SRodney W. Grimes icp = (struct icmp *)(buf + hlen); 1113eb1543c6SMatthew N. Dodd if (icp->icmp_type == icmp_type_rsp) { 11148fae3551SRodney W. Grimes if (icp->icmp_id != ident) 11158fae3551SRodney W. Grimes return; /* 'Twas not our ECHO */ 11168fae3551SRodney W. Grimes ++nreceived; 11178f975bb3SBruce Evans triptime = 0.0; 11188fae3551SRodney W. Grimes if (timing) { 11191ad76f1bSAlan Somers struct timespec tv1; 112013e3f0b7SMaxim Konovalov struct tv32 tv32; 11218fae3551SRodney W. Grimes #ifndef icmp_data 11229d2b0ab8SPeter Wemm tp = &icp->icmp_ip; 11238fae3551SRodney W. Grimes #else 11249d2b0ab8SPeter Wemm tp = icp->icmp_data; 11258fae3551SRodney W. Grimes #endif 11264eae39bfSStefan Farfeleder tp = (const char *)tp + phdr_len; 1127143008a1SMatthew N. Dodd 11287e9489e0SHiroki Sato if ((size_t)(cc - ICMP_MINLEN - phdr_len) >= 11297e9489e0SHiroki Sato sizeof(tv1)) { 11309d2b0ab8SPeter Wemm /* Copy to avoid alignment problems: */ 113113e3f0b7SMaxim Konovalov memcpy(&tv32, tp, sizeof(tv32)); 113213e3f0b7SMaxim Konovalov tv1.tv_sec = ntohl(tv32.tv32_sec); 11331ad76f1bSAlan Somers tv1.tv_nsec = ntohl(tv32.tv32_nsec); 11341ad76f1bSAlan Somers timespecsub(tv, &tv1, tv); 1135039d6aa4SBill Fenner triptime = ((double)tv->tv_sec) * 1000.0 + 11361ad76f1bSAlan Somers ((double)tv->tv_nsec) / 1000000.0; 11378fae3551SRodney W. Grimes tsum += triptime; 11383109a910SGarrett Wollman tsumsq += triptime * triptime; 11398fae3551SRodney W. Grimes if (triptime < tmin) 11408fae3551SRodney W. Grimes tmin = triptime; 11418fae3551SRodney W. Grimes if (triptime > tmax) 11428fae3551SRodney W. Grimes tmax = triptime; 114347e9b3eaSMatthew N. Dodd } else 114447e9b3eaSMatthew N. Dodd timing = 0; 11458fae3551SRodney W. Grimes } 11468fae3551SRodney W. Grimes 11475db89bc7SBill Fenner seq = ntohs(icp->icmp_seq); 11485db89bc7SBill Fenner 11495db89bc7SBill Fenner if (TST(seq % mx_dup_ck)) { 11508fae3551SRodney W. Grimes ++nrepeats; 11518fae3551SRodney W. Grimes --nreceived; 11528fae3551SRodney W. Grimes dupflag = 1; 11538fae3551SRodney W. Grimes } else { 11545db89bc7SBill Fenner SET(seq % mx_dup_ck); 11558fae3551SRodney W. Grimes dupflag = 0; 11568fae3551SRodney W. Grimes } 11578fae3551SRodney W. Grimes 11588fae3551SRodney W. Grimes if (options & F_QUIET) 11598fae3551SRodney W. Grimes return; 11608fae3551SRodney W. Grimes 1161d6cd1497SGleb Smirnoff if (options & F_WAITTIME && triptime > waittime) { 1162d6cd1497SGleb Smirnoff ++nrcvtimeout; 1163d6cd1497SGleb Smirnoff return; 1164d6cd1497SGleb Smirnoff } 1165d6cd1497SGleb Smirnoff 11668fae3551SRodney W. Grimes if (options & F_FLOOD) 11678fae3551SRodney W. Grimes (void)write(STDOUT_FILENO, &BSPACE, 1); 11688fae3551SRodney W. Grimes else { 11698fae3551SRodney W. Grimes (void)printf("%d bytes from %s: icmp_seq=%u", cc, 11708fae3551SRodney W. Grimes inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr), 11715db89bc7SBill Fenner seq); 11728fae3551SRodney W. Grimes (void)printf(" ttl=%d", ip->ip_ttl); 11738fae3551SRodney W. Grimes if (timing) 1174d410b6f1SDavid Greenman (void)printf(" time=%.3f ms", triptime); 11758fae3551SRodney W. Grimes if (dupflag) 11768fae3551SRodney W. Grimes (void)printf(" (DUP!)"); 1177772dfa72SDaniel O'Callaghan if (options & F_AUDIBLE) 1178ca517ad8SPoul-Henning Kamp (void)write(STDOUT_FILENO, &BBELL, 1); 1179143008a1SMatthew N. Dodd if (options & F_MASK) { 1180143008a1SMatthew N. Dodd /* Just prentend this cast isn't ugly */ 1181143008a1SMatthew N. Dodd (void)printf(" mask=%s", 1182f7fc5c91SMaxim Konovalov inet_ntoa(*(struct in_addr *)&(icp->icmp_mask))); 1183143008a1SMatthew N. Dodd } 1184eb1543c6SMatthew N. Dodd if (options & F_TIME) { 1185eb1543c6SMatthew N. Dodd (void)printf(" tso=%s", pr_ntime(icp->icmp_otime)); 1186eb1543c6SMatthew N. Dodd (void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime)); 1187eb1543c6SMatthew N. Dodd (void)printf(" tst=%s", pr_ntime(icp->icmp_ttime)); 1188eb1543c6SMatthew N. Dodd } 1189e88178ddSMaxim Konovalov if (recv_len != send_len) { 1190e88178ddSMaxim Konovalov (void)printf( 1191e88178ddSMaxim Konovalov "\nwrong total length %d instead of %d", 1192e88178ddSMaxim Konovalov recv_len, send_len); 1193e88178ddSMaxim Konovalov } 11948fae3551SRodney W. Grimes /* check the data */ 1195eb1543c6SMatthew N. Dodd cp = (u_char*)&icp->icmp_data[phdr_len]; 11960fe0c0ccSMaxim Konovalov dp = &outpack[ICMP_MINLEN + phdr_len]; 119747e9b3eaSMatthew N. Dodd cc -= ICMP_MINLEN + phdr_len; 119829dccd6aSMaxim Konovalov i = 0; 119929dccd6aSMaxim Konovalov if (timing) { /* don't check variable timestamp */ 120029dccd6aSMaxim Konovalov cp += TIMEVAL_LEN; 120129dccd6aSMaxim Konovalov dp += TIMEVAL_LEN; 120229dccd6aSMaxim Konovalov cc -= TIMEVAL_LEN; 120329dccd6aSMaxim Konovalov i += TIMEVAL_LEN; 120429dccd6aSMaxim Konovalov } 120529dccd6aSMaxim Konovalov for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) { 12068fae3551SRodney W. Grimes if (*cp != *dp) { 12078fae3551SRodney W. Grimes (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x", 12088fae3551SRodney W. Grimes i, *dp, *cp); 12091ad0b1beSMaxim Konovalov (void)printf("\ncp:"); 12108fae3551SRodney W. Grimes cp = (u_char*)&icp->icmp_data[0]; 1211d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1212e88178ddSMaxim Konovalov if ((i % 16) == 8) 1213d32ff037SJohn Birrell (void)printf("\n\t"); 1214e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 1215d32ff037SJohn Birrell } 12161ad0b1beSMaxim Konovalov (void)printf("\ndp:"); 12170fe0c0ccSMaxim Konovalov cp = &outpack[ICMP_MINLEN]; 1218d32ff037SJohn Birrell for (i = 0; i < datalen; ++i, ++cp) { 1219e88178ddSMaxim Konovalov if ((i % 16) == 8) 12208fae3551SRodney W. Grimes (void)printf("\n\t"); 1221e88178ddSMaxim Konovalov (void)printf("%2x ", *cp); 12228fae3551SRodney W. Grimes } 12238fae3551SRodney W. Grimes break; 12248fae3551SRodney W. Grimes } 12258fae3551SRodney W. Grimes } 12268fae3551SRodney W. Grimes } 12278fae3551SRodney W. Grimes } else { 1228ef9e6dc7SBill Fenner /* 1229ef9e6dc7SBill Fenner * We've got something other than an ECHOREPLY. 1230ef9e6dc7SBill Fenner * See if it's a reply to something that we sent. 1231ef9e6dc7SBill Fenner * We can compare IP destination, protocol, 1232ef9e6dc7SBill Fenner * and ICMP type and ID. 1233f78ac61bSWarner Losh * 1234f78ac61bSWarner Losh * Only print all the error messages if we are running 1235f78ac61bSWarner Losh * as root to avoid leaking information not normally 1236f78ac61bSWarner Losh * available to those not running as root. 1237ef9e6dc7SBill Fenner */ 1238ef9e6dc7SBill Fenner #ifndef icmp_data 1239ef9e6dc7SBill Fenner struct ip *oip = &icp->icmp_ip; 1240ef9e6dc7SBill Fenner #else 1241ef9e6dc7SBill Fenner struct ip *oip = (struct ip *)icp->icmp_data; 1242ef9e6dc7SBill Fenner #endif 1243ef9e6dc7SBill Fenner struct icmp *oicmp = (struct icmp *)(oip + 1); 1244ef9e6dc7SBill Fenner 1245ee2bf734SWarner Losh if (((options & F_VERBOSE) && uid == 0) || 1246ef9e6dc7SBill Fenner (!(options & F_QUIET2) && 1247d389e86aSMatt Jacob (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) && 1248ef9e6dc7SBill Fenner (oip->ip_p == IPPROTO_ICMP) && 1249ef9e6dc7SBill Fenner (oicmp->icmp_type == ICMP_ECHO) && 1250ef9e6dc7SBill Fenner (oicmp->icmp_id == ident))) { 12518fae3551SRodney W. Grimes (void)printf("%d bytes from %s: ", cc, 125243470e3bSGarrett Wollman pr_addr(from->sin_addr)); 12538fae3551SRodney W. Grimes pr_icmph(icp); 1254ef9e6dc7SBill Fenner } else 1255ef9e6dc7SBill Fenner return; 12568fae3551SRodney W. Grimes } 12578fae3551SRodney W. Grimes 12588fae3551SRodney W. Grimes /* Display any IP options */ 12598fae3551SRodney W. Grimes cp = (u_char *)buf + sizeof(struct ip); 12608fae3551SRodney W. Grimes 12618fae3551SRodney W. Grimes for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) 12628fae3551SRodney W. Grimes switch (*cp) { 12638fae3551SRodney W. Grimes case IPOPT_EOL: 12648fae3551SRodney W. Grimes hlen = 0; 12658fae3551SRodney W. Grimes break; 12668fae3551SRodney W. Grimes case IPOPT_LSRR: 1267cb75aca7SMaxim Konovalov case IPOPT_SSRR: 1268cb75aca7SMaxim Konovalov (void)printf(*cp == IPOPT_LSRR ? 1269cb75aca7SMaxim Konovalov "\nLSRR: " : "\nSSRR: "); 1270301207dfSMaxim Konovalov j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1; 12718fae3551SRodney W. Grimes hlen -= 2; 1272301207dfSMaxim Konovalov cp += 2; 12733c721ab3SMaxim Konovalov if (j >= INADDR_LEN && 12743c721ab3SMaxim Konovalov j <= hlen - (int)sizeof(struct ip)) { 12758fae3551SRodney W. Grimes for (;;) { 1276301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1277301207dfSMaxim Konovalov if (ina.s_addr == 0) 12781ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1279301207dfSMaxim Konovalov else 12801ad0b1beSMaxim Konovalov (void)printf("\t%s", 12811ad0b1beSMaxim Konovalov pr_addr(ina)); 1282301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1283301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1284301207dfSMaxim Konovalov j -= INADDR_LEN; 1285301207dfSMaxim Konovalov if (j < INADDR_LEN) 12868fae3551SRodney W. Grimes break; 12878fae3551SRodney W. Grimes (void)putchar('\n'); 12888fae3551SRodney W. Grimes } 1289301207dfSMaxim Konovalov } else 1290301207dfSMaxim Konovalov (void)printf("\t(truncated route)\n"); 12918fae3551SRodney W. Grimes break; 12928fae3551SRodney W. Grimes case IPOPT_RR: 1293301207dfSMaxim Konovalov j = cp[IPOPT_OLEN]; /* get length */ 1294301207dfSMaxim Konovalov i = cp[IPOPT_OFFSET]; /* and pointer */ 12958fae3551SRodney W. Grimes hlen -= 2; 1296301207dfSMaxim Konovalov cp += 2; 12978fae3551SRodney W. Grimes if (i > j) 12988fae3551SRodney W. Grimes i = j; 1299301207dfSMaxim Konovalov i = i - IPOPT_MINOFF + 1; 1300301207dfSMaxim Konovalov if (i < 0 || i > (hlen - (int)sizeof(struct ip))) { 1301301207dfSMaxim Konovalov old_rrlen = 0; 13028fae3551SRodney W. Grimes continue; 1303301207dfSMaxim Konovalov } 13048fae3551SRodney W. Grimes if (i == old_rrlen 13058fae3551SRodney W. Grimes && !bcmp((char *)cp, old_rr, i) 13068fae3551SRodney W. Grimes && !(options & F_FLOOD)) { 13078fae3551SRodney W. Grimes (void)printf("\t(same route)"); 13088fae3551SRodney W. Grimes hlen -= i; 13098fae3551SRodney W. Grimes cp += i; 13108fae3551SRodney W. Grimes break; 13118fae3551SRodney W. Grimes } 13128fae3551SRodney W. Grimes old_rrlen = i; 13138fae3551SRodney W. Grimes bcopy((char *)cp, old_rr, i); 13148fae3551SRodney W. Grimes (void)printf("\nRR: "); 1315301207dfSMaxim Konovalov if (i >= INADDR_LEN && 1316301207dfSMaxim Konovalov i <= hlen - (int)sizeof(struct ip)) { 13178fae3551SRodney W. Grimes for (;;) { 1318301207dfSMaxim Konovalov bcopy(++cp, &ina.s_addr, INADDR_LEN); 1319301207dfSMaxim Konovalov if (ina.s_addr == 0) 13201ad0b1beSMaxim Konovalov (void)printf("\t0.0.0.0"); 1321301207dfSMaxim Konovalov else 1322301207dfSMaxim Konovalov (void)printf("\t%s", 1323301207dfSMaxim Konovalov pr_addr(ina)); 1324301207dfSMaxim Konovalov hlen -= INADDR_LEN; 1325301207dfSMaxim Konovalov cp += INADDR_LEN - 1; 1326301207dfSMaxim Konovalov i -= INADDR_LEN; 1327301207dfSMaxim Konovalov if (i < INADDR_LEN) 13288fae3551SRodney W. Grimes break; 13298fae3551SRodney W. Grimes (void)putchar('\n'); 13308fae3551SRodney W. Grimes } 1331301207dfSMaxim Konovalov } else 1332301207dfSMaxim Konovalov (void)printf("\t(truncated route)"); 13338fae3551SRodney W. Grimes break; 13348fae3551SRodney W. Grimes case IPOPT_NOP: 13358fae3551SRodney W. Grimes (void)printf("\nNOP"); 13368fae3551SRodney W. Grimes break; 13378fae3551SRodney W. Grimes default: 13388fae3551SRodney W. Grimes (void)printf("\nunknown option %x", *cp); 13398fae3551SRodney W. Grimes break; 13408fae3551SRodney W. Grimes } 13418fae3551SRodney W. Grimes if (!(options & F_FLOOD)) { 13428fae3551SRodney W. Grimes (void)putchar('\n'); 13438fae3551SRodney W. Grimes (void)fflush(stdout); 13448fae3551SRodney W. Grimes } 13458fae3551SRodney W. Grimes } 13468fae3551SRodney W. Grimes 13478fae3551SRodney W. Grimes /* 1348badd8138SSean Eric Fagan * status -- 1349badd8138SSean Eric Fagan * Print out statistics when SIGINFO is received. 1350badd8138SSean Eric Fagan */ 1351badd8138SSean Eric Fagan 135243470e3bSGarrett Wollman static void 1353fafb8f11SEd Schouten status(int sig __unused) 13547d81b35cSBruce Evans { 1355efc8588dSDavid E. O'Brien 135637e5b2c6SSean Eric Fagan siginfo_p = 1; 135737e5b2c6SSean Eric Fagan } 135837e5b2c6SSean Eric Fagan 135943470e3bSGarrett Wollman static void 1360fafb8f11SEd Schouten check_status(void) 1361badd8138SSean Eric Fagan { 1362efc8588dSDavid E. O'Brien 136337e5b2c6SSean Eric Fagan if (siginfo_p) { 136437e5b2c6SSean Eric Fagan siginfo_p = 0; 1365aa822c39SDima Dorfman (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", 13667d81b35cSBruce Evans nreceived, ntransmitted, 1367aed98a27SMaxim Konovalov ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); 1368aed98a27SMaxim Konovalov if (nreceived && timing) 1369aed98a27SMaxim Konovalov (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max", 1370aed98a27SMaxim Konovalov tmin, tsum / (nreceived + nrepeats), tmax); 1371aed98a27SMaxim Konovalov (void)fprintf(stderr, "\n"); 137237e5b2c6SSean Eric Fagan } 1373badd8138SSean Eric Fagan } 1374badd8138SSean Eric Fagan 1375badd8138SSean Eric Fagan /* 13768fae3551SRodney W. Grimes * finish -- 13778fae3551SRodney W. Grimes * Print out statistics, and give up. 13788fae3551SRodney W. Grimes */ 137943470e3bSGarrett Wollman static void 1380fafb8f11SEd Schouten finish(void) 13818fae3551SRodney W. Grimes { 13828fae3551SRodney W. Grimes 13838fae3551SRodney W. Grimes (void)signal(SIGINT, SIG_IGN); 1384a2a00888SSean Eric Fagan (void)signal(SIGALRM, SIG_IGN); 13858fae3551SRodney W. Grimes (void)putchar('\n'); 13868fae3551SRodney W. Grimes (void)fflush(stdout); 13878fae3551SRodney W. Grimes (void)printf("--- %s ping statistics ---\n", hostname); 13888fae3551SRodney W. Grimes (void)printf("%ld packets transmitted, ", ntransmitted); 13898fae3551SRodney W. Grimes (void)printf("%ld packets received, ", nreceived); 13908fae3551SRodney W. Grimes if (nrepeats) 13918fae3551SRodney W. Grimes (void)printf("+%ld duplicates, ", nrepeats); 1392ebe70c8fSWarner Losh if (ntransmitted) { 13938fae3551SRodney W. Grimes if (nreceived > ntransmitted) 13948fae3551SRodney W. Grimes (void)printf("-- somebody's printing up packets!"); 13958fae3551SRodney W. Grimes else 1396aa822c39SDima Dorfman (void)printf("%.1f%% packet loss", 1397aa822c39SDima Dorfman ((ntransmitted - nreceived) * 100.0) / 1398aa822c39SDima Dorfman ntransmitted); 1399ebe70c8fSWarner Losh } 1400d6cd1497SGleb Smirnoff if (nrcvtimeout) 1401d6cd1497SGleb Smirnoff (void)printf(", %ld packets out of wait time", nrcvtimeout); 14028fae3551SRodney W. Grimes (void)putchar('\n'); 14033109a910SGarrett Wollman if (nreceived && timing) { 14043109a910SGarrett Wollman double n = nreceived + nrepeats; 14053109a910SGarrett Wollman double avg = tsum / n; 14063109a910SGarrett Wollman double vari = tsumsq / n - avg * avg; 14071ad0b1beSMaxim Konovalov (void)printf( 14081ad0b1beSMaxim Konovalov "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n", 14093109a910SGarrett Wollman tmin, avg, tmax, sqrt(vari)); 14103109a910SGarrett Wollman } 1411badd8138SSean Eric Fagan 14126e1173dcSDavid Greenman if (nreceived) 14138fae3551SRodney W. Grimes exit(0); 14146e1173dcSDavid Greenman else 14156e1173dcSDavid Greenman exit(2); 14168fae3551SRodney W. Grimes } 14178fae3551SRodney W. Grimes 14188fae3551SRodney W. Grimes #ifdef notdef 14198fae3551SRodney W. Grimes static char *ttab[] = { 14208fae3551SRodney W. Grimes "Echo Reply", /* ip + seq + udata */ 14218fae3551SRodney W. Grimes "Dest Unreachable", /* net, host, proto, port, frag, sr + IP */ 14228fae3551SRodney W. Grimes "Source Quench", /* IP */ 14238fae3551SRodney W. Grimes "Redirect", /* redirect type, gateway, + IP */ 14248fae3551SRodney W. Grimes "Echo", 14258fae3551SRodney W. Grimes "Time Exceeded", /* transit, frag reassem + IP */ 14268fae3551SRodney W. Grimes "Parameter Problem", /* pointer + IP */ 14278fae3551SRodney W. Grimes "Timestamp", /* id + seq + three timestamps */ 14288fae3551SRodney W. Grimes "Timestamp Reply", /* " */ 14298fae3551SRodney W. Grimes "Info Request", /* id + sq */ 14308fae3551SRodney W. Grimes "Info Reply" /* " */ 14318fae3551SRodney W. Grimes }; 14328fae3551SRodney W. Grimes #endif 14338fae3551SRodney W. Grimes 14348fae3551SRodney W. Grimes /* 14358fae3551SRodney W. Grimes * pr_icmph -- 14368fae3551SRodney W. Grimes * Print a descriptive string about an ICMP header. 14378fae3551SRodney W. Grimes */ 143843470e3bSGarrett Wollman static void 1439fafb8f11SEd Schouten pr_icmph(struct icmp *icp) 14408fae3551SRodney W. Grimes { 1441efc8588dSDavid E. O'Brien 14428fae3551SRodney W. Grimes switch(icp->icmp_type) { 14438fae3551SRodney W. Grimes case ICMP_ECHOREPLY: 14448fae3551SRodney W. Grimes (void)printf("Echo Reply\n"); 14458fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 14468fae3551SRodney W. Grimes break; 14478fae3551SRodney W. Grimes case ICMP_UNREACH: 14488fae3551SRodney W. Grimes switch(icp->icmp_code) { 14498fae3551SRodney W. Grimes case ICMP_UNREACH_NET: 14508fae3551SRodney W. Grimes (void)printf("Destination Net Unreachable\n"); 14518fae3551SRodney W. Grimes break; 14528fae3551SRodney W. Grimes case ICMP_UNREACH_HOST: 14538fae3551SRodney W. Grimes (void)printf("Destination Host Unreachable\n"); 14548fae3551SRodney W. Grimes break; 14558fae3551SRodney W. Grimes case ICMP_UNREACH_PROTOCOL: 14568fae3551SRodney W. Grimes (void)printf("Destination Protocol Unreachable\n"); 14578fae3551SRodney W. Grimes break; 14588fae3551SRodney W. Grimes case ICMP_UNREACH_PORT: 14598fae3551SRodney W. Grimes (void)printf("Destination Port Unreachable\n"); 14608fae3551SRodney W. Grimes break; 14618fae3551SRodney W. Grimes case ICMP_UNREACH_NEEDFRAG: 1462ef9e6dc7SBill Fenner (void)printf("frag needed and DF set (MTU %d)\n", 1463ff49597eSBill Fenner ntohs(icp->icmp_nextmtu)); 14648fae3551SRodney W. Grimes break; 14658fae3551SRodney W. Grimes case ICMP_UNREACH_SRCFAIL: 14668fae3551SRodney W. Grimes (void)printf("Source Route Failed\n"); 14678fae3551SRodney W. Grimes break; 1468ef9e6dc7SBill Fenner case ICMP_UNREACH_FILTER_PROHIB: 1469ef9e6dc7SBill Fenner (void)printf("Communication prohibited by filter\n"); 1470ef9e6dc7SBill Fenner break; 14718fae3551SRodney W. Grimes default: 14728fae3551SRodney W. Grimes (void)printf("Dest Unreachable, Bad Code: %d\n", 14738fae3551SRodney W. Grimes icp->icmp_code); 14748fae3551SRodney W. Grimes break; 14758fae3551SRodney W. Grimes } 14768fae3551SRodney W. Grimes /* Print returned IP header information */ 14778fae3551SRodney W. Grimes #ifndef icmp_data 14788fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 14798fae3551SRodney W. Grimes #else 14808fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 14818fae3551SRodney W. Grimes #endif 14828fae3551SRodney W. Grimes break; 14838fae3551SRodney W. Grimes case ICMP_SOURCEQUENCH: 14848fae3551SRodney W. Grimes (void)printf("Source Quench\n"); 14858fae3551SRodney W. Grimes #ifndef icmp_data 14868fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 14878fae3551SRodney W. Grimes #else 14888fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 14898fae3551SRodney W. Grimes #endif 14908fae3551SRodney W. Grimes break; 14918fae3551SRodney W. Grimes case ICMP_REDIRECT: 14928fae3551SRodney W. Grimes switch(icp->icmp_code) { 14938fae3551SRodney W. Grimes case ICMP_REDIRECT_NET: 14948fae3551SRodney W. Grimes (void)printf("Redirect Network"); 14958fae3551SRodney W. Grimes break; 14968fae3551SRodney W. Grimes case ICMP_REDIRECT_HOST: 14978fae3551SRodney W. Grimes (void)printf("Redirect Host"); 14988fae3551SRodney W. Grimes break; 14998fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSNET: 15008fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Network"); 15018fae3551SRodney W. Grimes break; 15028fae3551SRodney W. Grimes case ICMP_REDIRECT_TOSHOST: 15038fae3551SRodney W. Grimes (void)printf("Redirect Type of Service and Host"); 15048fae3551SRodney W. Grimes break; 15058fae3551SRodney W. Grimes default: 15068fae3551SRodney W. Grimes (void)printf("Redirect, Bad Code: %d", icp->icmp_code); 15078fae3551SRodney W. Grimes break; 15088fae3551SRodney W. Grimes } 1509ff49597eSBill Fenner (void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr)); 15108fae3551SRodney W. Grimes #ifndef icmp_data 15118fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15128fae3551SRodney W. Grimes #else 15138fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15148fae3551SRodney W. Grimes #endif 15158fae3551SRodney W. Grimes break; 15168fae3551SRodney W. Grimes case ICMP_ECHO: 15178fae3551SRodney W. Grimes (void)printf("Echo Request\n"); 15188fae3551SRodney W. Grimes /* XXX ID + Seq + Data */ 15198fae3551SRodney W. Grimes break; 15208fae3551SRodney W. Grimes case ICMP_TIMXCEED: 15218fae3551SRodney W. Grimes switch(icp->icmp_code) { 15228fae3551SRodney W. Grimes case ICMP_TIMXCEED_INTRANS: 15238fae3551SRodney W. Grimes (void)printf("Time to live exceeded\n"); 15248fae3551SRodney W. Grimes break; 15258fae3551SRodney W. Grimes case ICMP_TIMXCEED_REASS: 15268fae3551SRodney W. Grimes (void)printf("Frag reassembly time exceeded\n"); 15278fae3551SRodney W. Grimes break; 15288fae3551SRodney W. Grimes default: 15298fae3551SRodney W. Grimes (void)printf("Time exceeded, Bad Code: %d\n", 15308fae3551SRodney W. Grimes icp->icmp_code); 15318fae3551SRodney W. Grimes break; 15328fae3551SRodney W. Grimes } 15338fae3551SRodney W. Grimes #ifndef icmp_data 15348fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15358fae3551SRodney W. Grimes #else 15368fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15378fae3551SRodney W. Grimes #endif 15388fae3551SRodney W. Grimes break; 15398fae3551SRodney W. Grimes case ICMP_PARAMPROB: 15408fae3551SRodney W. Grimes (void)printf("Parameter problem: pointer = 0x%02x\n", 15418fae3551SRodney W. Grimes icp->icmp_hun.ih_pptr); 15428fae3551SRodney W. Grimes #ifndef icmp_data 15438fae3551SRodney W. Grimes pr_retip(&icp->icmp_ip); 15448fae3551SRodney W. Grimes #else 15458fae3551SRodney W. Grimes pr_retip((struct ip *)icp->icmp_data); 15468fae3551SRodney W. Grimes #endif 15478fae3551SRodney W. Grimes break; 15488fae3551SRodney W. Grimes case ICMP_TSTAMP: 15498fae3551SRodney W. Grimes (void)printf("Timestamp\n"); 15508fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 15518fae3551SRodney W. Grimes break; 15528fae3551SRodney W. Grimes case ICMP_TSTAMPREPLY: 15538fae3551SRodney W. Grimes (void)printf("Timestamp Reply\n"); 15548fae3551SRodney W. Grimes /* XXX ID + Seq + 3 timestamps */ 15558fae3551SRodney W. Grimes break; 15568fae3551SRodney W. Grimes case ICMP_IREQ: 15578fae3551SRodney W. Grimes (void)printf("Information Request\n"); 15588fae3551SRodney W. Grimes /* XXX ID + Seq */ 15598fae3551SRodney W. Grimes break; 15608fae3551SRodney W. Grimes case ICMP_IREQREPLY: 15618fae3551SRodney W. Grimes (void)printf("Information Reply\n"); 15628fae3551SRodney W. Grimes /* XXX ID + Seq */ 15638fae3551SRodney W. Grimes break; 15648fae3551SRodney W. Grimes case ICMP_MASKREQ: 15658fae3551SRodney W. Grimes (void)printf("Address Mask Request\n"); 15668fae3551SRodney W. Grimes break; 15678fae3551SRodney W. Grimes case ICMP_MASKREPLY: 15688fae3551SRodney W. Grimes (void)printf("Address Mask Reply\n"); 15698fae3551SRodney W. Grimes break; 1570ef9e6dc7SBill Fenner case ICMP_ROUTERADVERT: 1571ef9e6dc7SBill Fenner (void)printf("Router Advertisement\n"); 1572ef9e6dc7SBill Fenner break; 1573ef9e6dc7SBill Fenner case ICMP_ROUTERSOLICIT: 1574ef9e6dc7SBill Fenner (void)printf("Router Solicitation\n"); 1575ef9e6dc7SBill Fenner break; 15768fae3551SRodney W. Grimes default: 15778fae3551SRodney W. Grimes (void)printf("Bad ICMP type: %d\n", icp->icmp_type); 15788fae3551SRodney W. Grimes } 15798fae3551SRodney W. Grimes } 15808fae3551SRodney W. Grimes 15818fae3551SRodney W. Grimes /* 15828fae3551SRodney W. Grimes * pr_iph -- 15838fae3551SRodney W. Grimes * Print an IP header with options. 15848fae3551SRodney W. Grimes */ 158543470e3bSGarrett Wollman static void 1586fafb8f11SEd Schouten pr_iph(struct ip *ip) 15878fae3551SRodney W. Grimes { 1588a94c074dSDimitry Andric struct in_addr ina; 15898fae3551SRodney W. Grimes u_char *cp; 1590efc8588dSDavid E. O'Brien int hlen; 15918fae3551SRodney W. Grimes 15928fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 15938fae3551SRodney W. Grimes cp = (u_char *)ip + 20; /* point to options */ 15948fae3551SRodney W. Grimes 1595ef9e6dc7SBill Fenner (void)printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst\n"); 15968fae3551SRodney W. Grimes (void)printf(" %1x %1x %02x %04x %04x", 1597ef9e6dc7SBill Fenner ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len), 1598ef9e6dc7SBill Fenner ntohs(ip->ip_id)); 1599d32ff037SJohn Birrell (void)printf(" %1lx %04lx", 1600d32ff037SJohn Birrell (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13, 1601d32ff037SJohn Birrell (u_long) ntohl(ip->ip_off) & 0x1fff); 1602ef9e6dc7SBill Fenner (void)printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, 1603ef9e6dc7SBill Fenner ntohs(ip->ip_sum)); 1604a94c074dSDimitry Andric memcpy(&ina, &ip->ip_src.s_addr, sizeof ina); 1605a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1606a94c074dSDimitry Andric memcpy(&ina, &ip->ip_dst.s_addr, sizeof ina); 1607a94c074dSDimitry Andric (void)printf(" %s ", inet_ntoa(ina)); 1608ef9e6dc7SBill Fenner /* dump any option bytes */ 16098fae3551SRodney W. Grimes while (hlen-- > 20) { 16108fae3551SRodney W. Grimes (void)printf("%02x", *cp++); 16118fae3551SRodney W. Grimes } 16128fae3551SRodney W. Grimes (void)putchar('\n'); 16138fae3551SRodney W. Grimes } 16148fae3551SRodney W. Grimes 16158fae3551SRodney W. Grimes /* 16168fae3551SRodney W. Grimes * pr_addr -- 16178fae3551SRodney W. Grimes * Return an ascii host address as a dotted quad and optionally with 16188fae3551SRodney W. Grimes * a hostname. 16198fae3551SRodney W. Grimes */ 162043470e3bSGarrett Wollman static char * 1621fafb8f11SEd Schouten pr_addr(struct in_addr ina) 16228fae3551SRodney W. Grimes { 16238fae3551SRodney W. Grimes struct hostent *hp; 1624f78ac61bSWarner Losh static char buf[16 + 3 + MAXHOSTNAMELEN]; 16258fae3551SRodney W. Grimes 162649133c6dSPawel Jakub Dawidek if (options & F_NUMERIC) 162743470e3bSGarrett Wollman return inet_ntoa(ina); 162849133c6dSPawel Jakub Dawidek 162949133c6dSPawel Jakub Dawidek hp = cap_gethostbyaddr(capdns, (char *)&ina, 4, AF_INET); 163049133c6dSPawel Jakub Dawidek 163149133c6dSPawel Jakub Dawidek if (hp == NULL) 163249133c6dSPawel Jakub Dawidek return inet_ntoa(ina); 163349133c6dSPawel Jakub Dawidek 1634efa38539SPeter Wemm (void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name, 163543470e3bSGarrett Wollman inet_ntoa(ina)); 16368fae3551SRodney W. Grimes return(buf); 16378fae3551SRodney W. Grimes } 16388fae3551SRodney W. Grimes 16398fae3551SRodney W. Grimes /* 16408fae3551SRodney W. Grimes * pr_retip -- 16418fae3551SRodney W. Grimes * Dump some info on a returned (via ICMP) IP packet. 16428fae3551SRodney W. Grimes */ 164343470e3bSGarrett Wollman static void 1644fafb8f11SEd Schouten pr_retip(struct ip *ip) 16458fae3551SRodney W. Grimes { 16468fae3551SRodney W. Grimes u_char *cp; 1647efc8588dSDavid E. O'Brien int hlen; 16488fae3551SRodney W. Grimes 16498fae3551SRodney W. Grimes pr_iph(ip); 16508fae3551SRodney W. Grimes hlen = ip->ip_hl << 2; 16518fae3551SRodney W. Grimes cp = (u_char *)ip + hlen; 16528fae3551SRodney W. Grimes 16538fae3551SRodney W. Grimes if (ip->ip_p == 6) 16548fae3551SRodney W. Grimes (void)printf("TCP: from port %u, to port %u (decimal)\n", 16558fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 16568fae3551SRodney W. Grimes else if (ip->ip_p == 17) 16578fae3551SRodney W. Grimes (void)printf("UDP: from port %u, to port %u (decimal)\n", 16588fae3551SRodney W. Grimes (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3))); 16598fae3551SRodney W. Grimes } 16608fae3551SRodney W. Grimes 1661eb1543c6SMatthew N. Dodd static char * 1662007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp) 1663eb1543c6SMatthew N. Dodd { 1664eb1543c6SMatthew N. Dodd static char buf[10]; 1665007fe4e3SMaxim Konovalov int hour, min, sec; 1666eb1543c6SMatthew N. Dodd 1667007fe4e3SMaxim Konovalov sec = ntohl(timestamp) / 1000; 1668007fe4e3SMaxim Konovalov hour = sec / 60 / 60; 1669007fe4e3SMaxim Konovalov min = (sec % (60 * 60)) / 60; 1670007fe4e3SMaxim Konovalov sec = (sec % (60 * 60)) % 60; 1671eb1543c6SMatthew N. Dodd 1672007fe4e3SMaxim Konovalov (void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec); 1673eb1543c6SMatthew N. Dodd 1674eb1543c6SMatthew N. Dodd return (buf); 1675eb1543c6SMatthew N. Dodd } 1676eb1543c6SMatthew N. Dodd 167743470e3bSGarrett Wollman static void 1678fafb8f11SEd Schouten fill(char *bp, char *patp) 16798fae3551SRodney W. Grimes { 16808fae3551SRodney W. Grimes char *cp; 1681efc8588dSDavid E. O'Brien int pat[16]; 16824fba6582SMaxim Konovalov u_int ii, jj, kk; 16838fae3551SRodney W. Grimes 168443470e3bSGarrett Wollman for (cp = patp; *cp; cp++) { 168543470e3bSGarrett Wollman if (!isxdigit(*cp)) 168643470e3bSGarrett Wollman errx(EX_USAGE, 168743470e3bSGarrett Wollman "patterns must be specified as hex digits"); 168843470e3bSGarrett Wollman 16898fae3551SRodney W. Grimes } 16908fae3551SRodney W. Grimes ii = sscanf(patp, 16918fae3551SRodney W. Grimes "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", 16928fae3551SRodney W. Grimes &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6], 16938fae3551SRodney W. Grimes &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12], 16948fae3551SRodney W. Grimes &pat[13], &pat[14], &pat[15]); 16958fae3551SRodney W. Grimes 16968fae3551SRodney W. Grimes if (ii > 0) 1697d829c3dfSMatthew N. Dodd for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii) 16988fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 16998fae3551SRodney W. Grimes bp[jj + kk] = pat[jj]; 17008fae3551SRodney W. Grimes if (!(options & F_QUIET)) { 17018fae3551SRodney W. Grimes (void)printf("PATTERN: 0x"); 17028fae3551SRodney W. Grimes for (jj = 0; jj < ii; ++jj) 17038fae3551SRodney W. Grimes (void)printf("%02x", bp[jj] & 0xFF); 17048fae3551SRodney W. Grimes (void)printf("\n"); 17058fae3551SRodney W. Grimes } 17068fae3551SRodney W. Grimes } 17078fae3551SRodney W. Grimes 170849133c6dSPawel Jakub Dawidek static cap_channel_t * 170949133c6dSPawel Jakub Dawidek capdns_setup(void) 171049133c6dSPawel Jakub Dawidek { 171149133c6dSPawel Jakub Dawidek cap_channel_t *capcas, *capdnsloc; 171249133c6dSPawel Jakub Dawidek const char *types[2]; 171349133c6dSPawel Jakub Dawidek int families[1]; 171449133c6dSPawel Jakub Dawidek 171549133c6dSPawel Jakub Dawidek capcas = cap_init(); 1716c501d73cSMariusz Zaborski if (capcas == NULL) 1717c501d73cSMariusz Zaborski err(1, "unable to create casper process"); 171849133c6dSPawel Jakub Dawidek capdnsloc = cap_service_open(capcas, "system.dns"); 171949133c6dSPawel Jakub Dawidek /* Casper capability no longer needed. */ 172049133c6dSPawel Jakub Dawidek cap_close(capcas); 172149133c6dSPawel Jakub Dawidek if (capdnsloc == NULL) 172249133c6dSPawel Jakub Dawidek err(1, "unable to open system.dns service"); 1723752d135eSMariusz Zaborski types[0] = "NAME2ADDR"; 1724752d135eSMariusz Zaborski types[1] = "ADDR2NAME"; 172549133c6dSPawel Jakub Dawidek if (cap_dns_type_limit(capdnsloc, types, 2) < 0) 172649133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 172749133c6dSPawel Jakub Dawidek families[0] = AF_INET; 172849133c6dSPawel Jakub Dawidek if (cap_dns_family_limit(capdnsloc, families, 1) < 0) 172949133c6dSPawel Jakub Dawidek err(1, "unable to limit access to system.dns service"); 173049133c6dSPawel Jakub Dawidek 173149133c6dSPawel Jakub Dawidek return (capdnsloc); 173249133c6dSPawel Jakub Dawidek } 173349133c6dSPawel Jakub Dawidek 1734120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC) 1735120b4a93SRuslan Ermilov #define SECOPT " [-P policy]" 1736120b4a93SRuslan Ermilov #else 1737120b4a93SRuslan Ermilov #define SECOPT "" 1738120b4a93SRuslan Ermilov #endif 173943470e3bSGarrett Wollman static void 1740fafb8f11SEd Schouten usage(void) 17418fae3551SRodney W. Grimes { 174231eac03bSSean Chittenden 1743d6cd1497SGleb Smirnoff (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", 1744ee3e1c4cSRuslan Ermilov "usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]", 1745ee3e1c4cSRuslan Ermilov " [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]", 1746ee3e1c4cSRuslan Ermilov " " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]", 1747d6cd1497SGleb Smirnoff " [-W waittime] [-z tos] host", 17481bd10ba2SRuslan Ermilov " ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]", 1749120b4a93SRuslan Ermilov " [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]", 1750d6cd1497SGleb Smirnoff " [-s packetsize] [-T ttl] [-t timeout] [-W waittime]", 1751d6cd1497SGleb Smirnoff " [-z tos] mcast-group"); 175243470e3bSGarrett Wollman exit(EX_USAGE); 17538fae3551SRodney W. Grimes } 1754