xref: /freebsd/sbin/ping/ping.c (revision 49133c6d52243e3666e4eabdc4bf81b26b32ca7c)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1989, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
68fae3551SRodney W. Grimes  * Mike Muuss.
78fae3551SRodney W. Grimes  *
88fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
98fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
108fae3551SRodney W. Grimes  * are met:
118fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
128fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
138fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
148fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
158fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
168fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
178fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
188fae3551SRodney W. Grimes  *    without specific prior written permission.
198fae3551SRodney W. Grimes  *
208fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
218fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
228fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
238fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
248fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
258fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
268fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
278fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
288fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
298fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
308fae3551SRodney W. Grimes  * SUCH DAMAGE.
318fae3551SRodney W. Grimes  */
328fae3551SRodney W. Grimes 
33c69284caSDavid E. O'Brien #if 0
348fae3551SRodney W. Grimes #ifndef lint
3543470e3bSGarrett Wollman static const char copyright[] =
368fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
378fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
388fae3551SRodney W. Grimes #endif /* not lint */
398fae3551SRodney W. Grimes 
408fae3551SRodney W. Grimes #ifndef lint
418fae3551SRodney W. Grimes static char sccsid[] = "@(#)ping.c	8.1 (Berkeley) 6/5/93";
428fae3551SRodney W. Grimes #endif /* not lint */
43c69284caSDavid E. O'Brien #endif
44c69284caSDavid E. O'Brien #include <sys/cdefs.h>
45c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$");
468fae3551SRodney W. Grimes 
478fae3551SRodney W. Grimes /*
488fae3551SRodney W. Grimes  *			P I N G . C
498fae3551SRodney W. Grimes  *
50e345a80dSPhilippe Charnier  * Using the Internet Control Message Protocol (ICMP) "ECHO" facility,
518fae3551SRodney W. Grimes  * measure round-trip-delays and packet loss across network paths.
528fae3551SRodney W. Grimes  *
538fae3551SRodney W. Grimes  * Author -
548fae3551SRodney W. Grimes  *	Mike Muuss
558fae3551SRodney W. Grimes  *	U. S. Army Ballistic Research Laboratory
568fae3551SRodney W. Grimes  *	December, 1983
578fae3551SRodney W. Grimes  *
588fae3551SRodney W. Grimes  * Status -
598fae3551SRodney W. Grimes  *	Public Domain.  Distribution Unlimited.
608fae3551SRodney W. Grimes  * Bugs -
618fae3551SRodney W. Grimes  *	More statistics could always be gathered.
628fae3551SRodney W. Grimes  *	This program has to run SUID to ROOT to access the ICMP socket.
638fae3551SRodney W. Grimes  */
648fae3551SRodney W. Grimes 
6543470e3bSGarrett Wollman #include <sys/param.h>		/* NB: we rely on this for <sys/types.h> */
66*49133c6dSPawel Jakub Dawidek #include <sys/capability.h>
678fae3551SRodney W. Grimes #include <sys/socket.h>
68261e59bbSMaxim Konovalov #include <sys/sysctl.h>
698fae3551SRodney W. Grimes #include <sys/time.h>
70039d6aa4SBill Fenner #include <sys/uio.h>
718fae3551SRodney W. Grimes 
728fae3551SRodney W. Grimes #include <netinet/in.h>
7343470e3bSGarrett Wollman #include <netinet/in_systm.h>
748fae3551SRodney W. Grimes #include <netinet/ip.h>
758fae3551SRodney W. Grimes #include <netinet/ip_icmp.h>
768fae3551SRodney W. Grimes #include <netinet/ip_var.h>
7743470e3bSGarrett Wollman #include <arpa/inet.h>
78*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
79*49133c6dSPawel Jakub Dawidek #include <libcapsicum.h>
80*49133c6dSPawel Jakub Dawidek #include <libcapsicum_dns.h>
81*49133c6dSPawel Jakub Dawidek #include <libcapsicum_service.h>
82*49133c6dSPawel Jakub Dawidek #endif
838fae3551SRodney W. Grimes 
849a4365d0SYoshinobu Inoue #ifdef IPSEC
858409aedfSGeorge V. Neville-Neil #include <netipsec/ipsec.h>
869a4365d0SYoshinobu Inoue #endif /*IPSEC*/
879a4365d0SYoshinobu Inoue 
88261e59bbSMaxim Konovalov #include <ctype.h>
89261e59bbSMaxim Konovalov #include <err.h>
90261e59bbSMaxim Konovalov #include <errno.h>
91261e59bbSMaxim Konovalov #include <math.h>
92261e59bbSMaxim Konovalov #include <netdb.h>
93261e59bbSMaxim Konovalov #include <signal.h>
94261e59bbSMaxim Konovalov #include <stdio.h>
95261e59bbSMaxim Konovalov #include <stdlib.h>
96261e59bbSMaxim Konovalov #include <string.h>
97261e59bbSMaxim Konovalov #include <sysexits.h>
98261e59bbSMaxim Konovalov #include <unistd.h>
99261e59bbSMaxim Konovalov 
100301207dfSMaxim Konovalov #define	INADDR_LEN	((int)sizeof(in_addr_t))
10113e3f0b7SMaxim Konovalov #define	TIMEVAL_LEN	((int)sizeof(struct tv32))
102eb1543c6SMatthew N. Dodd #define	MASK_LEN	(ICMP_MASKLEN - ICMP_MINLEN)
103eb1543c6SMatthew N. Dodd #define	TS_LEN		(ICMP_TSLEN - ICMP_MINLEN)
104c67c1ce8SMatthew N. Dodd #define	DEFDATALEN	56		/* default data length */
1058f975bb3SBruce Evans #define	FLOOD_BACKOFF	20000		/* usecs to back off if F_FLOOD mode */
1068f975bb3SBruce Evans 					/* runs out of buffer space */
1074fba6582SMaxim Konovalov #define	MAXIPLEN	(sizeof(struct ip) + MAX_IPOPTLEN)
1084fba6582SMaxim Konovalov #define	MAXICMPLEN	(ICMP_ADVLENMIN + MAX_IPOPTLEN)
109d6cd1497SGleb Smirnoff #define	MAXWAIT		10000		/* max ms to wait for response */
110bf113f1bSBill Fumerola #define	MAXALARM	(60 * 60)	/* max seconds for alarm timeout */
1110b2f8b3fSMaxim Konovalov #define	MAXTOS		255
1128fae3551SRodney W. Grimes 
1138fae3551SRodney W. Grimes #define	A(bit)		rcvd_tbl[(bit)>>3]	/* identify byte in array */
1148fae3551SRodney W. Grimes #define	B(bit)		(1 << ((bit) & 0x07))	/* identify bit in byte */
1158fae3551SRodney W. Grimes #define	SET(bit)	(A(bit) |= B(bit))
1168fae3551SRodney W. Grimes #define	CLR(bit)	(A(bit) &= (~B(bit)))
1178fae3551SRodney W. Grimes #define	TST(bit)	(A(bit) & B(bit))
1188fae3551SRodney W. Grimes 
11913e3f0b7SMaxim Konovalov struct tv32 {
12013e3f0b7SMaxim Konovalov 	int32_t tv32_sec;
12113e3f0b7SMaxim Konovalov 	int32_t tv32_usec;
12213e3f0b7SMaxim Konovalov };
12313e3f0b7SMaxim Konovalov 
1248fae3551SRodney W. Grimes /* various options */
1258fae3551SRodney W. Grimes int options;
12685456935SBill Fenner #define	F_FLOOD		0x0001
12785456935SBill Fenner #define	F_INTERVAL	0x0002
12885456935SBill Fenner #define	F_NUMERIC	0x0004
12985456935SBill Fenner #define	F_PINGFILLED	0x0008
13085456935SBill Fenner #define	F_QUIET		0x0010
13185456935SBill Fenner #define	F_RROUTE	0x0020
13285456935SBill Fenner #define	F_SO_DEBUG	0x0040
13385456935SBill Fenner #define	F_SO_DONTROUTE	0x0080
13485456935SBill Fenner #define	F_VERBOSE	0x0100
13585456935SBill Fenner #define	F_QUIET2	0x0200
13685456935SBill Fenner #define	F_NOLOOP	0x0400
13785456935SBill Fenner #define	F_MTTL		0x0800
13885456935SBill Fenner #define	F_MIF		0x1000
139772dfa72SDaniel O'Callaghan #define	F_AUDIBLE	0x2000
1409a4365d0SYoshinobu Inoue #ifdef IPSEC
1419a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
1429a4365d0SYoshinobu Inoue #define F_POLICY	0x4000
1439a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
1449a4365d0SYoshinobu Inoue #endif /*IPSEC*/
145211bfbd2SRuslan Ermilov #define	F_TTL		0x8000
146ca517ad8SPoul-Henning Kamp #define	F_MISSED	0x10000
1478025c44bSDima Dorfman #define	F_ONCE		0x20000
1480b2f8b3fSMaxim Konovalov #define	F_HDRINCL	0x40000
149143008a1SMatthew N. Dodd #define	F_MASK		0x80000
150eb1543c6SMatthew N. Dodd #define	F_TIME		0x100000
1519ff95228SGleb Smirnoff #define	F_SWEEP		0x200000
152d6cd1497SGleb Smirnoff #define	F_WAITTIME	0x400000
1538fae3551SRodney W. Grimes 
1548fae3551SRodney W. Grimes /*
1558fae3551SRodney W. Grimes  * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
1568fae3551SRodney W. Grimes  * number of received sequence numbers we can keep track of.  Change 128
1578fae3551SRodney W. Grimes  * to 8192 for complete accuracy...
1588fae3551SRodney W. Grimes  */
1598fae3551SRodney W. Grimes #define	MAX_DUP_CHK	(8 * 128)
1608fae3551SRodney W. Grimes int mx_dup_ck = MAX_DUP_CHK;
1618fae3551SRodney W. Grimes char rcvd_tbl[MAX_DUP_CHK / 8];
1628fae3551SRodney W. Grimes 
163d389e86aSMatt Jacob struct sockaddr_in whereto;	/* who to ping */
1648fae3551SRodney W. Grimes int datalen = DEFDATALEN;
1651104dd84SBruce Evans int maxpayload;
166*49133c6dSPawel Jakub Dawidek int ssend;			/* send socket file descriptor */
167*49133c6dSPawel Jakub Dawidek int srecv;			/* receive socket file descriptor */
1680b2f8b3fSMaxim Konovalov u_char outpackhdr[IP_MAXPACKET], *outpack;
169ca517ad8SPoul-Henning Kamp char BBELL = '\a';		/* characters written for MISSED and AUDIBLE */
170261e59bbSMaxim Konovalov char BSPACE = '\b';		/* characters written for flood */
1718fae3551SRodney W. Grimes char DOT = '.';
1728fae3551SRodney W. Grimes char *hostname;
17399490edeSWarner Losh char *shostname;
1748fae3551SRodney W. Grimes int ident;			/* process id to identify our packets */
175ee2bf734SWarner Losh int uid;			/* cached uid for micro-optimization */
176eb1543c6SMatthew N. Dodd u_char icmp_type = ICMP_ECHO;
177eb1543c6SMatthew N. Dodd u_char icmp_type_rsp = ICMP_ECHOREPLY;
178eb1543c6SMatthew N. Dodd int phdr_len = 0;
179e88178ddSMaxim Konovalov int send_len;
1808fae3551SRodney W. Grimes 
1818fae3551SRodney W. Grimes /* counters */
182261e59bbSMaxim Konovalov long nmissedmax;		/* max value of ntransmitted - nreceived - 1 */
1838fae3551SRodney W. Grimes long npackets;			/* max packets to transmit */
1848fae3551SRodney W. Grimes long nreceived;			/* # of packets we got back */
1858fae3551SRodney W. Grimes long nrepeats;			/* number of duplicates */
1868fae3551SRodney W. Grimes long ntransmitted;		/* sequence # for outbound packets = #sent */
1879ff95228SGleb Smirnoff long snpackets;			/* max packets to transmit in one sweep */
1889ff95228SGleb Smirnoff long snreceived;		/* # of packets we got back in this sweep */
1899ff95228SGleb Smirnoff long sntransmitted;		/* # of packets we sent in this sweep */
1909ff95228SGleb Smirnoff int sweepmax;			/* max value of payload in sweep */
1919ff95228SGleb Smirnoff int sweepmin = 0;		/* start value of payload in sweep */
1929ff95228SGleb Smirnoff int sweepincr = 1;		/* payload increment in sweep */
193526f06b2SMatthew Dillon int interval = 1000;		/* interval between packets, ms */
194d6cd1497SGleb Smirnoff int waittime = MAXWAIT;		/* timeout for each packet */
195d6cd1497SGleb Smirnoff long nrcvtimeout = 0;		/* # of packets we got back after waittime */
1968fae3551SRodney W. Grimes 
1978fae3551SRodney W. Grimes /* timing */
1988fae3551SRodney W. Grimes int timing;			/* flag to do timing */
1998fae3551SRodney W. Grimes double tmin = 999999999.0;	/* minimum round trip time */
2008fae3551SRodney W. Grimes double tmax = 0.0;		/* maximum round trip time */
2018fae3551SRodney W. Grimes double tsum = 0.0;		/* sum of all times, for doing average */
2023109a910SGarrett Wollman double tsumsq = 0.0;		/* sum of all times squared, for std. dev. */
2038fae3551SRodney W. Grimes 
2048f975bb3SBruce Evans volatile sig_atomic_t finish_up;  /* nonzero if we've been told to finish up */
2058f975bb3SBruce Evans volatile sig_atomic_t siginfo_p;
206badd8138SSean Eric Fagan 
207*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
208*49133c6dSPawel Jakub Dawidek static cap_channel_t *capdns;
209*49133c6dSPawel Jakub Dawidek #endif
210*49133c6dSPawel Jakub Dawidek 
21143470e3bSGarrett Wollman static void fill(char *, char *);
21243470e3bSGarrett Wollman static u_short in_cksum(u_short *, int);
213*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
214*49133c6dSPawel Jakub Dawidek static cap_channel_t *capdns_setup(void);
215*49133c6dSPawel Jakub Dawidek #endif
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);
22143470e3bSGarrett Wollman static void pr_icmph(struct icmp *);
22243470e3bSGarrett Wollman static void pr_iph(struct ip *);
223039d6aa4SBill Fenner static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *);
22443470e3bSGarrett Wollman static void pr_retip(struct ip *);
22543470e3bSGarrett Wollman static void status(int);
2268f975bb3SBruce Evans static void stopit(int);
227fafb8f11SEd Schouten static void tvsub(struct timeval *, const struct timeval *);
228e345a80dSPhilippe Charnier static void usage(void) __dead2;
2298fae3551SRodney W. Grimes 
23043470e3bSGarrett Wollman int
231fafb8f11SEd Schouten main(int argc, char *const *argv)
2328fae3551SRodney W. Grimes {
23331eac03bSSean Chittenden 	struct sockaddr_in from, sock_in;
234efc8588dSDavid E. O'Brien 	struct in_addr ifaddr;
235261e59bbSMaxim Konovalov 	struct timeval last, intvl;
236efc8588dSDavid E. O'Brien 	struct iovec iov;
2370b2f8b3fSMaxim Konovalov 	struct ip *ip;
238efc8588dSDavid E. O'Brien 	struct msghdr msg;
239efc8588dSDavid E. O'Brien 	struct sigaction si_sa;
2400b2f8b3fSMaxim Konovalov 	size_t sz;
241e81f5049SOlivier Houchard 	u_char *datap, packet[IP_MAXPACKET] __aligned(4);
242d074d39fSMatthew N. Dodd 	char *ep, *source, *target, *payload;
243261e59bbSMaxim Konovalov 	struct hostent *hp;
244efc8588dSDavid E. O'Brien #ifdef IPSEC_POLICY_IPSEC
245efc8588dSDavid E. O'Brien 	char *policy_in, *policy_out;
246efc8588dSDavid E. O'Brien #endif
247261e59bbSMaxim Konovalov 	struct sockaddr_in *to;
248261e59bbSMaxim Konovalov 	double t;
249efc8588dSDavid E. O'Brien 	u_long alarmtimeout, ultmp;
250*49133c6dSPawel Jakub Dawidek 	int almost_done, ch, df, hold, i, icmp_len, mib[4], preload;
251*49133c6dSPawel Jakub Dawidek 	int ssend_errno, srecv_errno, tos, ttl;
252efc8588dSDavid E. O'Brien 	char ctrl[CMSG_SPACE(sizeof(struct timeval))];
253efc8588dSDavid E. O'Brien 	char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN];
2548fae3551SRodney W. Grimes #ifdef IP_OPTIONS
2554fba6582SMaxim Konovalov 	char rspace[MAX_IPOPTLEN];	/* record route space */
2568fae3551SRodney W. Grimes #endif
257261e59bbSMaxim Konovalov 	unsigned char loop, mttl;
258efc8588dSDavid E. O'Brien 
25931eac03bSSean Chittenden 	payload = source = NULL;
2609a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
261efc8588dSDavid E. O'Brien 	policy_in = policy_out = NULL;
2629a4365d0SYoshinobu Inoue #endif
263*49133c6dSPawel Jakub Dawidek 	cap_rights_t rights;
264*49133c6dSPawel Jakub Dawidek 	bool cansandbox;
2658fae3551SRodney W. Grimes 
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.
270*49133c6dSPawel Jakub Dawidek 	 *
271*49133c6dSPawel Jakub Dawidek 	 * Historicaly ping was using one socket 's' for sending and for
272*49133c6dSPawel Jakub Dawidek 	 * receiving. After capsicum(4) related changes we use two
273*49133c6dSPawel Jakub Dawidek 	 * sockets. It was done for special ping use case - when user
274*49133c6dSPawel Jakub Dawidek 	 * issue ping on multicast or broadcast address replies come
275*49133c6dSPawel Jakub Dawidek 	 * from different addresses, not from the address we
276*49133c6dSPawel Jakub Dawidek 	 * connect(2)'ed to, and send socket do not receive those
277*49133c6dSPawel Jakub Dawidek 	 * packets.
278f1284d7aSBill Fenner 	 */
279*49133c6dSPawel Jakub Dawidek 	ssend = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
280*49133c6dSPawel Jakub Dawidek 	ssend_errno = errno;
281*49133c6dSPawel Jakub Dawidek 	srecv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
282*49133c6dSPawel 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 
2880b2f8b3fSMaxim Konovalov 	alarmtimeout = df = preload = tos = 0;
289badd8138SSean Eric Fagan 
2900b2f8b3fSMaxim Konovalov 	outpack = outpackhdr + sizeof(struct ip);
291211bfbd2SRuslan Ermilov 	while ((ch = getopt(argc, argv,
292d6cd1497SGleb Smirnoff 		"Aac:DdfG:g:h:I:i:Ll:M:m:nop:QqRrS:s:T:t:vW:z:"
293211bfbd2SRuslan Ermilov #ifdef IPSEC
2949a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
295211bfbd2SRuslan Ermilov 		"P:"
2969a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
297211bfbd2SRuslan Ermilov #endif /*IPSEC*/
298211bfbd2SRuslan Ermilov 		)) != -1)
2999a4365d0SYoshinobu Inoue 	{
3008fae3551SRodney W. Grimes 		switch(ch) {
301ca517ad8SPoul-Henning Kamp 		case 'A':
302ca517ad8SPoul-Henning Kamp 			options |= F_MISSED;
303ca517ad8SPoul-Henning Kamp 			break;
304772dfa72SDaniel O'Callaghan 		case 'a':
305772dfa72SDaniel O'Callaghan 			options |= F_AUDIBLE;
306772dfa72SDaniel O'Callaghan 			break;
3078fae3551SRodney W. Grimes 		case 'c':
30843470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
30943470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp)
31043470e3bSGarrett Wollman 				errx(EX_USAGE,
31143470e3bSGarrett Wollman 				    "invalid count of packets to transmit: `%s'",
31243470e3bSGarrett Wollman 				    optarg);
31343470e3bSGarrett Wollman 			npackets = ultmp;
3148fae3551SRodney W. Grimes 			break;
3150b2f8b3fSMaxim Konovalov 		case 'D':
3160b2f8b3fSMaxim Konovalov 			options |= F_HDRINCL;
3170b2f8b3fSMaxim Konovalov 			df = 1;
3180b2f8b3fSMaxim Konovalov 			break;
3198fae3551SRodney W. Grimes 		case 'd':
3208fae3551SRodney W. Grimes 			options |= F_SO_DEBUG;
3218fae3551SRodney W. Grimes 			break;
3228fae3551SRodney W. Grimes 		case 'f':
323526f06b2SMatthew Dillon 			if (uid) {
32443470e3bSGarrett Wollman 				errno = EPERM;
32543470e3bSGarrett Wollman 				err(EX_NOPERM, "-f flag");
3268fae3551SRodney W. Grimes 			}
3278fae3551SRodney W. Grimes 			options |= F_FLOOD;
3288fae3551SRodney W. Grimes 			setbuf(stdout, (char *)NULL);
3298fae3551SRodney W. Grimes 			break;
3309ff95228SGleb Smirnoff 		case 'G': /* Maximum packet size for ping sweep */
3319ff95228SGleb Smirnoff 			ultmp = strtoul(optarg, &ep, 0);
3329ff95228SGleb Smirnoff 			if (*ep || ep == optarg)
3339ff95228SGleb Smirnoff 				errx(EX_USAGE, "invalid packet size: `%s'",
3349ff95228SGleb Smirnoff 				    optarg);
3359ff95228SGleb Smirnoff 			if (uid != 0 && ultmp > DEFDATALEN) {
3369ff95228SGleb Smirnoff 				errno = EPERM;
3379ff95228SGleb Smirnoff 				err(EX_NOPERM,
3389ff95228SGleb Smirnoff 				    "packet size too large: %lu > %u",
3399ff95228SGleb Smirnoff 				    ultmp, DEFDATALEN);
3409ff95228SGleb Smirnoff 			}
3419ff95228SGleb Smirnoff 			options |= F_SWEEP;
3429ff95228SGleb Smirnoff 			sweepmax = ultmp;
3439ff95228SGleb Smirnoff 			break;
3449ff95228SGleb Smirnoff 		case 'g': /* Minimum packet size for ping sweep */
3459ff95228SGleb Smirnoff 			ultmp = strtoul(optarg, &ep, 0);
3469ff95228SGleb Smirnoff 			if (*ep || ep == optarg)
3479ff95228SGleb Smirnoff 				errx(EX_USAGE, "invalid packet size: `%s'",
3489ff95228SGleb Smirnoff 				    optarg);
3499ff95228SGleb Smirnoff 			if (uid != 0 && ultmp > DEFDATALEN) {
3509ff95228SGleb Smirnoff 				errno = EPERM;
3519ff95228SGleb Smirnoff 				err(EX_NOPERM,
3529ff95228SGleb Smirnoff 				    "packet size too large: %lu > %u",
3539ff95228SGleb Smirnoff 				    ultmp, DEFDATALEN);
3549ff95228SGleb Smirnoff 			}
3559ff95228SGleb Smirnoff 			options |= F_SWEEP;
3569ff95228SGleb Smirnoff 			sweepmin = ultmp;
3579ff95228SGleb Smirnoff 			break;
3589ff95228SGleb Smirnoff 		case 'h': /* Packet size increment for ping sweep */
3599ff95228SGleb Smirnoff 			ultmp = strtoul(optarg, &ep, 0);
3609ff95228SGleb Smirnoff 			if (*ep || ep == optarg || ultmp < 1)
3619ff95228SGleb Smirnoff 				errx(EX_USAGE, "invalid increment size: `%s'",
3629ff95228SGleb Smirnoff 				    optarg);
3639ff95228SGleb Smirnoff 			if (uid != 0 && ultmp > DEFDATALEN) {
3649ff95228SGleb Smirnoff 				errno = EPERM;
3659ff95228SGleb Smirnoff 				err(EX_NOPERM,
3669ff95228SGleb Smirnoff 				    "packet size too large: %lu > %u",
3679ff95228SGleb Smirnoff 				    ultmp, DEFDATALEN);
3689ff95228SGleb Smirnoff 			}
3699ff95228SGleb Smirnoff 			options |= F_SWEEP;
3709ff95228SGleb Smirnoff 			sweepincr = ultmp;
3719ff95228SGleb Smirnoff 			break;
3721f6a4631SRuslan Ermilov 		case 'I':		/* multicast interface */
3731f6a4631SRuslan Ermilov 			if (inet_aton(optarg, &ifaddr) == 0)
3741f6a4631SRuslan Ermilov 				errx(EX_USAGE,
3751f6a4631SRuslan Ermilov 				    "invalid multicast interface: `%s'",
3761f6a4631SRuslan Ermilov 				    optarg);
3771f6a4631SRuslan Ermilov 			options |= F_MIF;
3781f6a4631SRuslan Ermilov 			break;
3798fae3551SRodney W. Grimes 		case 'i':		/* wait between sending packets */
3801ad0b1beSMaxim Konovalov 			t = strtod(optarg, &ep) * 1000.0;
3811ad0b1beSMaxim Konovalov 			if (*ep || ep == optarg || t > (double)INT_MAX)
3821ad0b1beSMaxim Konovalov 				errx(EX_USAGE, "invalid timing interval: `%s'",
3831ad0b1beSMaxim Konovalov 				    optarg);
3848fae3551SRodney W. Grimes 			options |= F_INTERVAL;
385526f06b2SMatthew Dillon 			interval = (int)t;
386526f06b2SMatthew Dillon 			if (uid && interval < 1000) {
387526f06b2SMatthew Dillon 				errno = EPERM;
388526f06b2SMatthew Dillon 				err(EX_NOPERM, "-i interval too short");
389526f06b2SMatthew Dillon 			}
3908fae3551SRodney W. Grimes 			break;
3911f6a4631SRuslan Ermilov 		case 'L':
3921f6a4631SRuslan Ermilov 			options |= F_NOLOOP;
3931f6a4631SRuslan Ermilov 			loop = 0;
39485456935SBill Fenner 			break;
3958fae3551SRodney W. Grimes 		case 'l':
39643470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
39743470e3bSGarrett Wollman 			if (*ep || ep == optarg || ultmp > INT_MAX)
39843470e3bSGarrett Wollman 				errx(EX_USAGE,
39943470e3bSGarrett Wollman 				    "invalid preload value: `%s'", optarg);
4005e2cc0f4SStephen McKay 			if (uid) {
401f78ac61bSWarner Losh 				errno = EPERM;
402f78ac61bSWarner Losh 				err(EX_NOPERM, "-l flag");
403f78ac61bSWarner Losh 			}
40443470e3bSGarrett Wollman 			preload = ultmp;
4058fae3551SRodney W. Grimes 			break;
4061f6a4631SRuslan Ermilov 		case 'M':
407eb1543c6SMatthew N. Dodd 			switch(optarg[0]) {
408eb1543c6SMatthew N. Dodd 			case 'M':
409eb1543c6SMatthew N. Dodd 			case 'm':
4101f6a4631SRuslan Ermilov 				options |= F_MASK;
41185456935SBill Fenner 				break;
412eb1543c6SMatthew N. Dodd 			case 'T':
413eb1543c6SMatthew N. Dodd 			case 't':
414eb1543c6SMatthew N. Dodd 				options |= F_TIME;
415eb1543c6SMatthew N. Dodd 				break;
416eb1543c6SMatthew N. Dodd 			default:
417eb1543c6SMatthew N. Dodd 				errx(EX_USAGE, "invalid message: `%c'", optarg[0]);
418eb1543c6SMatthew N. Dodd 				break;
419eb1543c6SMatthew N. Dodd 			}
420eb1543c6SMatthew N. Dodd 			break;
421211bfbd2SRuslan Ermilov 		case 'm':		/* TTL */
422211bfbd2SRuslan Ermilov 			ultmp = strtoul(optarg, &ep, 0);
4239bc1a9ecSMaxim Konovalov 			if (*ep || ep == optarg || ultmp > MAXTTL)
4241ad0b1beSMaxim Konovalov 				errx(EX_USAGE, "invalid TTL: `%s'", optarg);
425211bfbd2SRuslan Ermilov 			ttl = ultmp;
426211bfbd2SRuslan Ermilov 			options |= F_TTL;
427211bfbd2SRuslan Ermilov 			break;
4288fae3551SRodney W. Grimes 		case 'n':
4298fae3551SRodney W. Grimes 			options |= F_NUMERIC;
4308fae3551SRodney W. Grimes 			break;
4318025c44bSDima Dorfman 		case 'o':
4328025c44bSDima Dorfman 			options |= F_ONCE;
4338025c44bSDima Dorfman 			break;
4341f6a4631SRuslan Ermilov #ifdef IPSEC
4351f6a4631SRuslan Ermilov #ifdef IPSEC_POLICY_IPSEC
4361f6a4631SRuslan Ermilov 		case 'P':
4371f6a4631SRuslan Ermilov 			options |= F_POLICY;
4381f6a4631SRuslan Ermilov 			if (!strncmp("in", optarg, 2))
4391f6a4631SRuslan Ermilov 				policy_in = strdup(optarg);
4401f6a4631SRuslan Ermilov 			else if (!strncmp("out", optarg, 3))
4411f6a4631SRuslan Ermilov 				policy_out = strdup(optarg);
4421f6a4631SRuslan Ermilov 			else
4431f6a4631SRuslan Ermilov 				errx(1, "invalid security policy");
4441f6a4631SRuslan Ermilov 			break;
4451f6a4631SRuslan Ermilov #endif /*IPSEC_POLICY_IPSEC*/
4461f6a4631SRuslan Ermilov #endif /*IPSEC*/
4478fae3551SRodney W. Grimes 		case 'p':		/* fill buffer with user pattern */
4488fae3551SRodney W. Grimes 			options |= F_PINGFILLED;
449d074d39fSMatthew N. Dodd 			payload = optarg;
4508fae3551SRodney W. Grimes 			break;
451ef9e6dc7SBill Fenner 		case 'Q':
452ef9e6dc7SBill Fenner 			options |= F_QUIET2;
453ef9e6dc7SBill Fenner 			break;
4548fae3551SRodney W. Grimes 		case 'q':
4558fae3551SRodney W. Grimes 			options |= F_QUIET;
4568fae3551SRodney W. Grimes 			break;
4578fae3551SRodney W. Grimes 		case 'R':
4588fae3551SRodney W. Grimes 			options |= F_RROUTE;
4598fae3551SRodney W. Grimes 			break;
4608fae3551SRodney W. Grimes 		case 'r':
4618fae3551SRodney W. Grimes 			options |= F_SO_DONTROUTE;
4628fae3551SRodney W. Grimes 			break;
4631f6a4631SRuslan Ermilov 		case 'S':
4641f6a4631SRuslan Ermilov 			source = optarg;
4651f6a4631SRuslan Ermilov 			break;
4668fae3551SRodney W. Grimes 		case 's':		/* size of packet to send */
46743470e3bSGarrett Wollman 			ultmp = strtoul(optarg, &ep, 0);
4684fba6582SMaxim Konovalov 			if (*ep || ep == optarg)
46943470e3bSGarrett Wollman 				errx(EX_USAGE, "invalid packet size: `%s'",
47043470e3bSGarrett Wollman 				    optarg);
471fb7d32c7SMaxim Konovalov 			if (uid != 0 && ultmp > DEFDATALEN) {
472fb7d32c7SMaxim Konovalov 				errno = EPERM;
473fb7d32c7SMaxim Konovalov 				err(EX_NOPERM,
474fb7d32c7SMaxim Konovalov 				    "packet size too large: %lu > %u",
475fb7d32c7SMaxim Konovalov 				    ultmp, DEFDATALEN);
476fb7d32c7SMaxim Konovalov 			}
47743470e3bSGarrett Wollman 			datalen = ultmp;
4788fae3551SRodney W. Grimes 			break;
4791f6a4631SRuslan Ermilov 		case 'T':		/* multicast TTL */
4801f6a4631SRuslan Ermilov 			ultmp = strtoul(optarg, &ep, 0);
4811f6a4631SRuslan Ermilov 			if (*ep || ep == optarg || ultmp > MAXTTL)
4821f6a4631SRuslan Ermilov 				errx(EX_USAGE, "invalid multicast TTL: `%s'",
4831f6a4631SRuslan Ermilov 				    optarg);
4841f6a4631SRuslan Ermilov 			mttl = ultmp;
4851f6a4631SRuslan Ermilov 			options |= F_MTTL;
48699490edeSWarner Losh 			break;
4877237fd94SBill Fumerola 		case 't':
488bf113f1bSBill Fumerola 			alarmtimeout = strtoul(optarg, &ep, 0);
489bf113f1bSBill Fumerola 			if ((alarmtimeout < 1) || (alarmtimeout == ULONG_MAX))
4907237fd94SBill Fumerola 				errx(EX_USAGE, "invalid timeout: `%s'",
4917237fd94SBill Fumerola 				    optarg);
492bf113f1bSBill Fumerola 			if (alarmtimeout > MAXALARM)
493bf113f1bSBill Fumerola 				errx(EX_USAGE, "invalid timeout: `%s' > %d",
494bf113f1bSBill Fumerola 				    optarg, MAXALARM);
495bf113f1bSBill Fumerola 			alarm((int)alarmtimeout);
4967237fd94SBill Fumerola 			break;
4978fae3551SRodney W. Grimes 		case 'v':
4988fae3551SRodney W. Grimes 			options |= F_VERBOSE;
4998fae3551SRodney W. Grimes 			break;
500d6cd1497SGleb Smirnoff 		case 'W':		/* wait ms for answer */
501d6cd1497SGleb Smirnoff 			t = strtod(optarg, &ep);
502d6cd1497SGleb Smirnoff 			if (*ep || ep == optarg || t > (double)INT_MAX)
503d6cd1497SGleb Smirnoff 				errx(EX_USAGE, "invalid timing interval: `%s'",
504d6cd1497SGleb Smirnoff 				    optarg);
505d6cd1497SGleb Smirnoff 			options |= F_WAITTIME;
506d6cd1497SGleb Smirnoff 			waittime = (int)t;
507d6cd1497SGleb Smirnoff 			break;
5080b2f8b3fSMaxim Konovalov 		case 'z':
5090b2f8b3fSMaxim Konovalov 			options |= F_HDRINCL;
5100b2f8b3fSMaxim Konovalov 			ultmp = strtoul(optarg, &ep, 0);
5110b2f8b3fSMaxim Konovalov 			if (*ep || ep == optarg || ultmp > MAXTOS)
5120b2f8b3fSMaxim Konovalov 				errx(EX_USAGE, "invalid TOS: `%s'", optarg);
5130b2f8b3fSMaxim Konovalov 			tos = ultmp;
5140b2f8b3fSMaxim Konovalov 			break;
5158fae3551SRodney W. Grimes 		default:
516e345a80dSPhilippe Charnier 			usage();
51743470e3bSGarrett Wollman 		}
51843470e3bSGarrett Wollman 	}
51943470e3bSGarrett Wollman 
52043470e3bSGarrett Wollman 	if (argc - optind != 1)
521e345a80dSPhilippe Charnier 		usage();
52243470e3bSGarrett Wollman 	target = argv[optind];
5238fae3551SRodney W. Grimes 
524d829c3dfSMatthew N. Dodd 	switch (options & (F_MASK|F_TIME)) {
525d829c3dfSMatthew N. Dodd 	case 0: break;
526d829c3dfSMatthew N. Dodd 	case F_MASK:
527eb1543c6SMatthew N. Dodd 		icmp_type = ICMP_MASKREQ;
528eb1543c6SMatthew N. Dodd 		icmp_type_rsp = ICMP_MASKREPLY;
529d829c3dfSMatthew N. Dodd 		phdr_len = MASK_LEN;
530eb1543c6SMatthew N. Dodd 		if (!(options & F_QUIET))
531eb1543c6SMatthew N. Dodd 			(void)printf("ICMP_MASKREQ\n");
532d829c3dfSMatthew N. Dodd 		break;
533d829c3dfSMatthew N. Dodd 	case F_TIME:
534eb1543c6SMatthew N. Dodd 		icmp_type = ICMP_TSTAMP;
535eb1543c6SMatthew N. Dodd 		icmp_type_rsp = ICMP_TSTAMPREPLY;
536d829c3dfSMatthew N. Dodd 		phdr_len = TS_LEN;
537eb1543c6SMatthew N. Dodd 		if (!(options & F_QUIET))
538eb1543c6SMatthew N. Dodd 			(void)printf("ICMP_TSTAMP\n");
539d829c3dfSMatthew N. Dodd 		break;
540d829c3dfSMatthew N. Dodd 	default:
541d829c3dfSMatthew N. Dodd 		errx(EX_USAGE, "ICMP_TSTAMP and ICMP_MASKREQ are exclusive.");
542d829c3dfSMatthew N. Dodd 		break;
543eb1543c6SMatthew N. Dodd 	}
5440fe0c0ccSMaxim Konovalov 	icmp_len = sizeof(struct ip) + ICMP_MINLEN + phdr_len;
545fb7d32c7SMaxim Konovalov 	if (options & F_RROUTE)
546e88178ddSMaxim Konovalov 		icmp_len += MAX_IPOPTLEN;
547e88178ddSMaxim Konovalov 	maxpayload = IP_MAXPACKET - icmp_len;
548fb7d32c7SMaxim Konovalov 	if (datalen > maxpayload)
5491104dd84SBruce Evans 		errx(EX_USAGE, "packet size too large: %d > %d", datalen,
550fb7d32c7SMaxim Konovalov 		    maxpayload);
551e88178ddSMaxim Konovalov 	send_len = icmp_len + datalen;
5520fe0c0ccSMaxim Konovalov 	datap = &outpack[ICMP_MINLEN + phdr_len + TIMEVAL_LEN];
553d074d39fSMatthew N. Dodd 	if (options & F_PINGFILLED) {
554d074d39fSMatthew N. Dodd 		fill((char *)datap, payload);
555d074d39fSMatthew N. Dodd 	}
556*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
557*49133c6dSPawel Jakub Dawidek 	capdns = capdns_setup();
558*49133c6dSPawel Jakub Dawidek #endif
55999490edeSWarner Losh 	if (source) {
56031eac03bSSean Chittenden 		bzero((char *)&sock_in, sizeof(sock_in));
56131eac03bSSean Chittenden 		sock_in.sin_family = AF_INET;
56231eac03bSSean Chittenden 		if (inet_aton(source, &sock_in.sin_addr) != 0) {
56399490edeSWarner Losh 			shostname = source;
56499490edeSWarner Losh 		} else {
565*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
566*49133c6dSPawel Jakub Dawidek 			if (capdns != NULL)
567*49133c6dSPawel Jakub Dawidek 				hp = cap_gethostbyname2(capdns, source,
568*49133c6dSPawel Jakub Dawidek 				    AF_INET);
569*49133c6dSPawel Jakub Dawidek 			else
570*49133c6dSPawel Jakub Dawidek #endif
57199490edeSWarner Losh 				hp = gethostbyname2(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 		}
587*49133c6dSPawel Jakub Dawidek 		if (bind(ssend, (struct sockaddr *)&sock_in, sizeof sock_in) ==
588*49133c6dSPawel 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 {
599*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
600*49133c6dSPawel Jakub Dawidek 		if (capdns != NULL)
601*49133c6dSPawel Jakub Dawidek 			hp = cap_gethostbyname2(capdns, target, AF_INET);
602*49133c6dSPawel Jakub Dawidek 		else
603*49133c6dSPawel Jakub Dawidek #endif
60443470e3bSGarrett Wollman 			hp = gethostbyname2(target, AF_INET);
60543470e3bSGarrett Wollman 		if (!hp)
60643470e3bSGarrett Wollman 			errx(EX_NOHOST, "cannot resolve %s: %s",
60743470e3bSGarrett Wollman 			    target, hstrerror(h_errno));
60843470e3bSGarrett Wollman 
60931eac03bSSean Chittenden 		if ((unsigned)hp->h_length > sizeof(to->sin_addr))
6101ffae4a6SWarner Losh 			errx(1, "gethostbyname2 returned an illegal address");
61143470e3bSGarrett Wollman 		memcpy(&to->sin_addr, hp->h_addr_list[0], sizeof to->sin_addr);
6128fae3551SRodney W. Grimes 		(void)strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
613b10d9d5fSWarner Losh 		hnamebuf[sizeof(hnamebuf) - 1] = '\0';
6148fae3551SRodney W. Grimes 		hostname = hnamebuf;
6158fae3551SRodney W. Grimes 	}
6168fae3551SRodney W. Grimes 
617*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
618*49133c6dSPawel Jakub Dawidek 	/* From now on we will use only reverse DNS lookups. */
619*49133c6dSPawel Jakub Dawidek 	if (capdns != NULL) {
620*49133c6dSPawel Jakub Dawidek 		const char *types[1];
621*49133c6dSPawel Jakub Dawidek 
622*49133c6dSPawel Jakub Dawidek 		types[0] = "ADDR";
623*49133c6dSPawel Jakub Dawidek 		if (cap_dns_type_limit(capdns, types, 1) < 0)
624*49133c6dSPawel Jakub Dawidek 			err(1, "unable to limit access to system.dns service");
625*49133c6dSPawel Jakub Dawidek 	}
626*49133c6dSPawel Jakub Dawidek #endif
627*49133c6dSPawel Jakub Dawidek 
628*49133c6dSPawel Jakub Dawidek 	if (ssend < 0) {
629*49133c6dSPawel Jakub Dawidek 		errno = ssend_errno;
630*49133c6dSPawel Jakub Dawidek 		err(EX_OSERR, "ssend socket");
631*49133c6dSPawel Jakub Dawidek 	}
632*49133c6dSPawel Jakub Dawidek 
633*49133c6dSPawel Jakub Dawidek 	if (srecv < 0) {
634*49133c6dSPawel Jakub Dawidek 		errno = srecv_errno;
635*49133c6dSPawel Jakub Dawidek 		err(EX_OSERR, "srecv socket");
636*49133c6dSPawel Jakub Dawidek 	}
637*49133c6dSPawel Jakub Dawidek 
638*49133c6dSPawel Jakub Dawidek 	if (connect(ssend, (struct sockaddr *)&whereto, sizeof(whereto)) != 0)
639*49133c6dSPawel Jakub Dawidek 		err(1, "connect");
640*49133c6dSPawel Jakub Dawidek 
64143470e3bSGarrett Wollman 	if (options & F_FLOOD && options & F_INTERVAL)
64243470e3bSGarrett Wollman 		errx(EX_USAGE, "-f and -i: incompatible options");
64343470e3bSGarrett Wollman 
64443470e3bSGarrett Wollman 	if (options & F_FLOOD && IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
64543470e3bSGarrett Wollman 		errx(EX_USAGE,
64643470e3bSGarrett Wollman 		    "-f flag cannot be used with multicast destination");
64743470e3bSGarrett Wollman 	if (options & (F_MIF | F_NOLOOP | F_MTTL)
64843470e3bSGarrett Wollman 	    && !IN_MULTICAST(ntohl(to->sin_addr.s_addr)))
64943470e3bSGarrett Wollman 		errx(EX_USAGE,
65043470e3bSGarrett Wollman 		    "-I, -L, -T flags cannot be used with unicast destination");
6518fae3551SRodney W. Grimes 
652d829c3dfSMatthew N. Dodd 	if (datalen >= TIMEVAL_LEN)	/* can we time transfer */
6538fae3551SRodney W. Grimes 		timing = 1;
65443470e3bSGarrett Wollman 
6558fae3551SRodney W. Grimes 	if (!(options & F_PINGFILLED))
656d829c3dfSMatthew N. Dodd 		for (i = TIMEVAL_LEN; i < datalen; ++i)
6578fae3551SRodney W. Grimes 			*datap++ = i;
6588fae3551SRodney W. Grimes 
6598fae3551SRodney W. Grimes 	ident = getpid() & 0xFFFF;
6608fae3551SRodney W. Grimes 
6618fae3551SRodney W. Grimes 	hold = 1;
662*49133c6dSPawel Jakub Dawidek 	if (options & F_SO_DEBUG) {
663*49133c6dSPawel Jakub Dawidek 		(void)setsockopt(ssend, SOL_SOCKET, SO_DEBUG, (char *)&hold,
6648fae3551SRodney W. Grimes 		    sizeof(hold));
665*49133c6dSPawel Jakub Dawidek 		(void)setsockopt(srecv, SOL_SOCKET, SO_DEBUG, (char *)&hold,
666*49133c6dSPawel Jakub Dawidek 		    sizeof(hold));
667*49133c6dSPawel Jakub Dawidek 	}
6688fae3551SRodney W. Grimes 	if (options & F_SO_DONTROUTE)
669*49133c6dSPawel Jakub Dawidek 		(void)setsockopt(ssend, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
6708fae3551SRodney W. Grimes 		    sizeof(hold));
6719a4365d0SYoshinobu Inoue #ifdef IPSEC
6729a4365d0SYoshinobu Inoue #ifdef IPSEC_POLICY_IPSEC
6739a4365d0SYoshinobu Inoue 	if (options & F_POLICY) {
6749a4365d0SYoshinobu Inoue 		char *buf;
6759a4365d0SYoshinobu Inoue 		if (policy_in != NULL) {
6769a4365d0SYoshinobu Inoue 			buf = ipsec_set_policy(policy_in, strlen(policy_in));
6779a4365d0SYoshinobu Inoue 			if (buf == NULL)
678ffd40070SKris Kennaway 				errx(EX_CONFIG, "%s", ipsec_strerror());
679*49133c6dSPawel Jakub Dawidek 			if (setsockopt(srecv, 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 		if (policy_out != NULL) {
6879a4365d0SYoshinobu Inoue 			buf = ipsec_set_policy(policy_out, strlen(policy_out));
6889a4365d0SYoshinobu Inoue 			if (buf == NULL)
689ffd40070SKris Kennaway 				errx(EX_CONFIG, "%s", ipsec_strerror());
690*49133c6dSPawel Jakub Dawidek 			if (setsockopt(ssend, IPPROTO_IP, IP_IPSEC_POLICY,
6919a4365d0SYoshinobu Inoue 					buf, ipsec_get_policylen(buf)) < 0)
6921ad0b1beSMaxim Konovalov 				err(EX_CONFIG,
6931ad0b1beSMaxim Konovalov 				    "ipsec policy cannot be configured");
6949a4365d0SYoshinobu Inoue 			free(buf);
6959a4365d0SYoshinobu Inoue 		}
6969a4365d0SYoshinobu Inoue 	}
6979a4365d0SYoshinobu Inoue #endif /*IPSEC_POLICY_IPSEC*/
6989a4365d0SYoshinobu Inoue #endif /*IPSEC*/
6998fae3551SRodney W. Grimes 
7000b2f8b3fSMaxim Konovalov 	if (options & F_HDRINCL) {
7010b2f8b3fSMaxim Konovalov 		ip = (struct ip*)outpackhdr;
7020b2f8b3fSMaxim Konovalov 		if (!(options & (F_TTL | F_MTTL))) {
7030b2f8b3fSMaxim Konovalov 			mib[0] = CTL_NET;
7040b2f8b3fSMaxim Konovalov 			mib[1] = PF_INET;
7050b2f8b3fSMaxim Konovalov 			mib[2] = IPPROTO_IP;
7060b2f8b3fSMaxim Konovalov 			mib[3] = IPCTL_DEFTTL;
7070b2f8b3fSMaxim Konovalov 			sz = sizeof(ttl);
7080b2f8b3fSMaxim Konovalov 			if (sysctl(mib, 4, &ttl, &sz, NULL, 0) == -1)
7090b2f8b3fSMaxim Konovalov 				err(1, "sysctl(net.inet.ip.ttl)");
7100b2f8b3fSMaxim Konovalov 		}
711*49133c6dSPawel Jakub Dawidek 		setsockopt(ssend, IPPROTO_IP, IP_HDRINCL, &hold, sizeof(hold));
7120b2f8b3fSMaxim Konovalov 		ip->ip_v = IPVERSION;
7130b2f8b3fSMaxim Konovalov 		ip->ip_hl = sizeof(struct ip) >> 2;
7140b2f8b3fSMaxim Konovalov 		ip->ip_tos = tos;
7150b2f8b3fSMaxim Konovalov 		ip->ip_id = 0;
7160b2f8b3fSMaxim Konovalov 		ip->ip_off = df ? IP_DF : 0;
7170b2f8b3fSMaxim Konovalov 		ip->ip_ttl = ttl;
7180b2f8b3fSMaxim Konovalov 		ip->ip_p = IPPROTO_ICMP;
71931eac03bSSean Chittenden 		ip->ip_src.s_addr = source ? sock_in.sin_addr.s_addr : INADDR_ANY;
7200b2f8b3fSMaxim Konovalov 		ip->ip_dst = to->sin_addr;
7210b2f8b3fSMaxim Konovalov         }
722*49133c6dSPawel Jakub Dawidek 
723*49133c6dSPawel Jakub Dawidek 	if (options & F_NUMERIC)
724*49133c6dSPawel Jakub Dawidek 		cansandbox = true;
725*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
726*49133c6dSPawel Jakub Dawidek 	else if (capdns != NULL)
727*49133c6dSPawel Jakub Dawidek 		cansandbox = true;
728*49133c6dSPawel Jakub Dawidek #endif
729*49133c6dSPawel Jakub Dawidek 	else
730*49133c6dSPawel Jakub Dawidek 		cansandbox = false;
731*49133c6dSPawel Jakub Dawidek 
732*49133c6dSPawel Jakub Dawidek 	/*
733*49133c6dSPawel Jakub Dawidek 	 * Here we enter capability mode. Further down access to global
734*49133c6dSPawel Jakub Dawidek 	 * namespaces (e.g filesystem) is restricted (see capsicum(4)).
735*49133c6dSPawel Jakub Dawidek 	 * We must connect(2) our socket before this point.
736*49133c6dSPawel Jakub Dawidek 	 */
737*49133c6dSPawel Jakub Dawidek 	if (cansandbox && cap_enter() < 0 && errno != ENOSYS)
738*49133c6dSPawel Jakub Dawidek 		err(1, "cap_enter");
739*49133c6dSPawel Jakub Dawidek 
740*49133c6dSPawel Jakub Dawidek 	if (cap_sandboxed())
741*49133c6dSPawel Jakub Dawidek 		fprintf(stderr, "capability mode sandbox enabled\n");
742*49133c6dSPawel Jakub Dawidek 
743*49133c6dSPawel Jakub Dawidek 	cap_rights_init(&rights, CAP_RECV, CAP_EVENT, CAP_SETSOCKOPT);
744*49133c6dSPawel Jakub Dawidek 	if (cap_rights_limit(srecv, &rights) < 0 && errno != ENOSYS)
745*49133c6dSPawel Jakub Dawidek 		err(1, "cap_rights_limit srecv");
746*49133c6dSPawel Jakub Dawidek 
747*49133c6dSPawel Jakub Dawidek 	cap_rights_init(&rights, CAP_SEND, CAP_SETSOCKOPT);
748*49133c6dSPawel Jakub Dawidek 	if (cap_rights_limit(ssend, &rights) < 0 && errno != ENOSYS)
749*49133c6dSPawel Jakub Dawidek 		err(1, "cap_rights_limit ssend");
750*49133c6dSPawel Jakub Dawidek 
7518fae3551SRodney W. Grimes 	/* record route option */
7528fae3551SRodney W. Grimes 	if (options & F_RROUTE) {
7538fae3551SRodney W. Grimes #ifdef IP_OPTIONS
754039d6aa4SBill Fenner 		bzero(rspace, sizeof(rspace));
7558fae3551SRodney W. Grimes 		rspace[IPOPT_OPTVAL] = IPOPT_RR;
7568fae3551SRodney W. Grimes 		rspace[IPOPT_OLEN] = sizeof(rspace) - 1;
7578fae3551SRodney W. Grimes 		rspace[IPOPT_OFFSET] = IPOPT_MINOFF;
758039d6aa4SBill Fenner 		rspace[sizeof(rspace) - 1] = IPOPT_EOL;
759*49133c6dSPawel Jakub Dawidek 		if (setsockopt(ssend, IPPROTO_IP, IP_OPTIONS, rspace,
76043470e3bSGarrett Wollman 		    sizeof(rspace)) < 0)
76143470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_OPTIONS");
7628fae3551SRodney W. Grimes #else
76343470e3bSGarrett Wollman 		errx(EX_UNAVAILABLE,
76443470e3bSGarrett Wollman 		    "record route not available in this implementation");
7658fae3551SRodney W. Grimes #endif /* IP_OPTIONS */
7668fae3551SRodney W. Grimes 	}
7678fae3551SRodney W. Grimes 
768211bfbd2SRuslan Ermilov 	if (options & F_TTL) {
769*49133c6dSPawel Jakub Dawidek 		if (setsockopt(ssend, IPPROTO_IP, IP_TTL, &ttl,
770211bfbd2SRuslan Ermilov 		    sizeof(ttl)) < 0) {
771211bfbd2SRuslan Ermilov 			err(EX_OSERR, "setsockopt IP_TTL");
772211bfbd2SRuslan Ermilov 		}
773211bfbd2SRuslan Ermilov 	}
77485456935SBill Fenner 	if (options & F_NOLOOP) {
775*49133c6dSPawel Jakub Dawidek 		if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
77685456935SBill Fenner 		    sizeof(loop)) < 0) {
77743470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_LOOP");
77885456935SBill Fenner 		}
77985456935SBill Fenner 	}
78085456935SBill Fenner 	if (options & F_MTTL) {
781*49133c6dSPawel Jakub Dawidek 		if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_TTL, &mttl,
782211bfbd2SRuslan Ermilov 		    sizeof(mttl)) < 0) {
78343470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_TTL");
78485456935SBill Fenner 		}
78585456935SBill Fenner 	}
78685456935SBill Fenner 	if (options & F_MIF) {
787*49133c6dSPawel Jakub Dawidek 		if (setsockopt(ssend, IPPROTO_IP, IP_MULTICAST_IF, &ifaddr,
78885456935SBill Fenner 		    sizeof(ifaddr)) < 0) {
78943470e3bSGarrett Wollman 			err(EX_OSERR, "setsockopt IP_MULTICAST_IF");
79085456935SBill Fenner 		}
79185456935SBill Fenner 	}
792039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
793039d6aa4SBill Fenner 	{ int on = 1;
794*49133c6dSPawel Jakub Dawidek 	if (setsockopt(srecv, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) < 0)
795039d6aa4SBill Fenner 		err(EX_OSERR, "setsockopt SO_TIMESTAMP");
796039d6aa4SBill Fenner 	}
797039d6aa4SBill Fenner #endif
7989ff95228SGleb Smirnoff 	if (sweepmax) {
7999ff95228SGleb Smirnoff 		if (sweepmin >= sweepmax)
8009ff95228SGleb Smirnoff 			errx(EX_USAGE, "Maximum packet size must be greater than the minimum packet size");
8019ff95228SGleb Smirnoff 
8029ff95228SGleb Smirnoff 		if (datalen != DEFDATALEN)
8039ff95228SGleb Smirnoff 			errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive");
8049ff95228SGleb Smirnoff 
8059ff95228SGleb Smirnoff 		if (npackets > 0) {
8069ff95228SGleb Smirnoff 			snpackets = npackets;
8079ff95228SGleb Smirnoff 			npackets = 0;
8089ff95228SGleb Smirnoff 		} else
8099ff95228SGleb Smirnoff 			snpackets = 1;
8109ff95228SGleb Smirnoff 		datalen = sweepmin;
8119ff95228SGleb Smirnoff 		send_len = icmp_len + sweepmin;
8129ff95228SGleb Smirnoff 	}
8139ff95228SGleb Smirnoff 	if (options & F_SWEEP && !sweepmax)
8149ff95228SGleb Smirnoff 		errx(EX_USAGE, "Maximum sweep size must be specified");
81585456935SBill Fenner 
8168fae3551SRodney W. Grimes 	/*
8178fae3551SRodney W. Grimes 	 * When pinging the broadcast address, you can get a lot of answers.
8188fae3551SRodney W. Grimes 	 * Doing something so evil is useful if you are trying to stress the
8198fae3551SRodney W. Grimes 	 * ethernet, or just want to fill the arp cache to get some stuff for
82043470e3bSGarrett Wollman 	 * /etc/ethers.  But beware: RFC 1122 allows hosts to ignore broadcast
82143470e3bSGarrett Wollman 	 * or multicast pings if they wish.
8228fae3551SRodney W. Grimes 	 */
8234fba6582SMaxim Konovalov 
8244fba6582SMaxim Konovalov 	/*
8254fba6582SMaxim Konovalov 	 * XXX receive buffer needs undetermined space for mbuf overhead
8264fba6582SMaxim Konovalov 	 * as well.
8274fba6582SMaxim Konovalov 	 */
8284fba6582SMaxim Konovalov 	hold = IP_MAXPACKET + 128;
829*49133c6dSPawel Jakub Dawidek 	(void)setsockopt(srecv, SOL_SOCKET, SO_RCVBUF, (char *)&hold,
8308fae3551SRodney W. Grimes 	    sizeof(hold));
831*49133c6dSPawel Jakub Dawidek 	/* CAP_SETSOCKOPT removed */
832*49133c6dSPawel Jakub Dawidek 	cap_rights_init(&rights, CAP_RECV, CAP_EVENT);
833*49133c6dSPawel Jakub Dawidek 	if (cap_rights_limit(srecv, &rights) < 0 && errno != ENOSYS)
834*49133c6dSPawel Jakub Dawidek 		err(1, "cap_rights_limit srecv setsockopt");
835261e59bbSMaxim Konovalov 	if (uid == 0)
836*49133c6dSPawel Jakub Dawidek 		(void)setsockopt(ssend, SOL_SOCKET, SO_SNDBUF, (char *)&hold,
837e8bd25ceSRobert Watson 		    sizeof(hold));
838*49133c6dSPawel Jakub Dawidek 	/* CAP_SETSOCKOPT removed */
839*49133c6dSPawel Jakub Dawidek 	cap_rights_init(&rights, CAP_SEND);
840*49133c6dSPawel Jakub Dawidek 	if (cap_rights_limit(ssend, &rights) < 0 && errno != ENOSYS)
841*49133c6dSPawel Jakub Dawidek 		err(1, "cap_rights_limit ssend setsockopt");
842e8bd25ceSRobert Watson 
84399490edeSWarner Losh 	if (to->sin_family == AF_INET) {
84499490edeSWarner Losh 		(void)printf("PING %s (%s)", hostname,
84599490edeSWarner Losh 		    inet_ntoa(to->sin_addr));
84699490edeSWarner Losh 		if (source)
84799490edeSWarner Losh 			(void)printf(" from %s", shostname);
8489ff95228SGleb Smirnoff 		if (sweepmax)
8499ff95228SGleb Smirnoff 			(void)printf(": (%d ... %d) data bytes\n",
8509ff95228SGleb Smirnoff 			    sweepmin, sweepmax);
8519ff95228SGleb Smirnoff 		else
85299490edeSWarner Losh 			(void)printf(": %d data bytes\n", datalen);
8539ff95228SGleb Smirnoff 
8549ff95228SGleb Smirnoff 	} else {
8559ff95228SGleb Smirnoff 		if (sweepmax)
8569ff95228SGleb Smirnoff 			(void)printf("PING %s: (%d ... %d) data bytes\n",
8579ff95228SGleb Smirnoff 			    hostname, sweepmin, sweepmax);
8589ff95228SGleb Smirnoff 		else
8598fae3551SRodney W. Grimes 			(void)printf("PING %s: %d data bytes\n", hostname, datalen);
8609ff95228SGleb Smirnoff 	}
8618fae3551SRodney W. Grimes 
8627d81b35cSBruce Evans 	/*
863a2a00888SSean Eric Fagan 	 * Use sigaction() instead of signal() to get unambiguous semantics,
864a2a00888SSean Eric Fagan 	 * in particular with SA_RESTART not set.
8657d81b35cSBruce Evans 	 */
866a2a00888SSean Eric Fagan 
867f6bd468bSPaul Traina 	sigemptyset(&si_sa.sa_mask);
86837e5b2c6SSean Eric Fagan 	si_sa.sa_flags = 0;
869a2a00888SSean Eric Fagan 
870a2a00888SSean Eric Fagan 	si_sa.sa_handler = stopit;
871a2a00888SSean Eric Fagan 	if (sigaction(SIGINT, &si_sa, 0) == -1) {
872a2a00888SSean Eric Fagan 		err(EX_OSERR, "sigaction SIGINT");
873a2a00888SSean Eric Fagan 	}
874a2a00888SSean Eric Fagan 
875a2a00888SSean Eric Fagan 	si_sa.sa_handler = status;
87637e5b2c6SSean Eric Fagan 	if (sigaction(SIGINFO, &si_sa, 0) == -1) {
877624ff938SWarner Losh 		err(EX_OSERR, "sigaction");
87837e5b2c6SSean Eric Fagan 	}
879bf113f1bSBill Fumerola 
880bf113f1bSBill Fumerola         if (alarmtimeout > 0) {
8817237fd94SBill Fumerola 		si_sa.sa_handler = stopit;
8827237fd94SBill Fumerola 		if (sigaction(SIGALRM, &si_sa, 0) == -1)
8837237fd94SBill Fumerola 			err(EX_OSERR, "sigaction SIGALRM");
884bf113f1bSBill Fumerola         }
88537e5b2c6SSean Eric Fagan 
886039d6aa4SBill Fenner 	bzero(&msg, sizeof(msg));
887039d6aa4SBill Fenner 	msg.msg_name = (caddr_t)&from;
888039d6aa4SBill Fenner 	msg.msg_iov = &iov;
889039d6aa4SBill Fenner 	msg.msg_iovlen = 1;
890039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
891039d6aa4SBill Fenner 	msg.msg_control = (caddr_t)ctrl;
892039d6aa4SBill Fenner #endif
893039d6aa4SBill Fenner 	iov.iov_base = packet;
894e88178ddSMaxim Konovalov 	iov.iov_len = IP_MAXPACKET;
895039d6aa4SBill Fenner 
89632af342fSRuslan Ermilov 	if (preload == 0)
89732af342fSRuslan Ermilov 		pinger();		/* send the first ping */
89832af342fSRuslan Ermilov 	else {
89932af342fSRuslan Ermilov 		if (npackets != 0 && preload > npackets)
90032af342fSRuslan Ermilov 			preload = npackets;
9018fae3551SRodney W. Grimes 		while (preload--)	/* fire off them quickies */
9028fae3551SRodney W. Grimes 			pinger();
90332af342fSRuslan Ermilov 	}
90432af342fSRuslan Ermilov 	(void)gettimeofday(&last, NULL);
9058fae3551SRodney W. Grimes 
906039d6aa4SBill Fenner 	if (options & F_FLOOD) {
907039d6aa4SBill Fenner 		intvl.tv_sec = 0;
908039d6aa4SBill Fenner 		intvl.tv_usec = 10000;
909039d6aa4SBill Fenner 	} else {
910526f06b2SMatthew Dillon 		intvl.tv_sec = interval / 1000;
911526f06b2SMatthew Dillon 		intvl.tv_usec = interval % 1000 * 1000;
912039d6aa4SBill Fenner 	}
913039d6aa4SBill Fenner 
914261e59bbSMaxim Konovalov 	almost_done = 0;
9158f975bb3SBruce Evans 	while (!finish_up) {
916261e59bbSMaxim Konovalov 		struct timeval now, timeout;
917039d6aa4SBill Fenner 		fd_set rfds;
918261e59bbSMaxim Konovalov 		int cc, n;
9198fae3551SRodney W. Grimes 
9207d81b35cSBruce Evans 		check_status();
921*49133c6dSPawel Jakub Dawidek 		if ((unsigned)srecv >= FD_SETSIZE)
9227e5bbd68SJacques Vidrine 			errx(EX_OSERR, "descriptor too large");
923039d6aa4SBill Fenner 		FD_ZERO(&rfds);
924*49133c6dSPawel Jakub Dawidek 		FD_SET(srecv, &rfds);
925039d6aa4SBill Fenner 		(void)gettimeofday(&now, NULL);
926039d6aa4SBill Fenner 		timeout.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
927039d6aa4SBill Fenner 		timeout.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
928039d6aa4SBill Fenner 		while (timeout.tv_usec < 0) {
929039d6aa4SBill Fenner 			timeout.tv_usec += 1000000;
930039d6aa4SBill Fenner 			timeout.tv_sec--;
9318fae3551SRodney W. Grimes 		}
932e345a80dSPhilippe Charnier 		while (timeout.tv_usec >= 1000000) {
933039d6aa4SBill Fenner 			timeout.tv_usec -= 1000000;
934039d6aa4SBill Fenner 			timeout.tv_sec++;
935039d6aa4SBill Fenner 		}
936039d6aa4SBill Fenner 		if (timeout.tv_sec < 0)
9376959b14dSXin LI 			timerclear(&timeout);
938*49133c6dSPawel Jakub Dawidek 		n = select(srecv + 1, &rfds, NULL, NULL, &timeout);
9395e2cc0f4SStephen McKay 		if (n < 0)
9405e2cc0f4SStephen McKay 			continue;	/* Must be EINTR. */
941039d6aa4SBill Fenner 		if (n == 1) {
94231eac03bSSean Chittenden 			struct timeval *tv = NULL;
943039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
944039d6aa4SBill Fenner 			struct cmsghdr *cmsg = (struct cmsghdr *)&ctrl;
945039d6aa4SBill Fenner 
946039d6aa4SBill Fenner 			msg.msg_controllen = sizeof(ctrl);
947039d6aa4SBill Fenner #endif
948039d6aa4SBill Fenner 			msg.msg_namelen = sizeof(from);
949*49133c6dSPawel Jakub Dawidek 			if ((cc = recvmsg(srecv, &msg, 0)) < 0) {
9508fae3551SRodney W. Grimes 				if (errno == EINTR)
9518fae3551SRodney W. Grimes 					continue;
952e345a80dSPhilippe Charnier 				warn("recvmsg");
9538fae3551SRodney W. Grimes 				continue;
9548fae3551SRodney W. Grimes 			}
955039d6aa4SBill Fenner #ifdef SO_TIMESTAMP
956039d6aa4SBill Fenner 			if (cmsg->cmsg_level == SOL_SOCKET &&
957039d6aa4SBill Fenner 			    cmsg->cmsg_type == SCM_TIMESTAMP &&
95831eac03bSSean Chittenden 			    cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) {
959fa05a94cSJohn Birrell 				/* Copy to avoid alignment problems: */
960fa05a94cSJohn Birrell 				memcpy(&now, CMSG_DATA(cmsg), sizeof(now));
96131eac03bSSean Chittenden 				tv = &now;
962fa05a94cSJohn Birrell 			}
963039d6aa4SBill Fenner #endif
96431eac03bSSean Chittenden 			if (tv == NULL) {
965039d6aa4SBill Fenner 				(void)gettimeofday(&now, NULL);
96631eac03bSSean Chittenden 				tv = &now;
967039d6aa4SBill Fenner 			}
96831eac03bSSean Chittenden 			pr_pack((char *)packet, cc, &from, tv);
96931eac03bSSean Chittenden 			if ((options & F_ONCE && nreceived) ||
97031eac03bSSean Chittenden 			    (npackets && nreceived >= npackets))
9718fae3551SRodney W. Grimes 				break;
9728fae3551SRodney W. Grimes 		}
9735e2cc0f4SStephen McKay 		if (n == 0 || options & F_FLOOD) {
9749ff95228SGleb Smirnoff 			if (sweepmax && sntransmitted == snpackets) {
9759ff95228SGleb Smirnoff 				for (i = 0; i < sweepincr ; ++i)
9769ff95228SGleb Smirnoff 					*datap++ = i;
9779ff95228SGleb Smirnoff 				datalen += sweepincr;
9789ff95228SGleb Smirnoff 				if (datalen > sweepmax)
9799ff95228SGleb Smirnoff 					break;
9809ff95228SGleb Smirnoff 				send_len = icmp_len + datalen;
9819ff95228SGleb Smirnoff 				sntransmitted = 0;
9829ff95228SGleb Smirnoff 			}
983039d6aa4SBill Fenner 			if (!npackets || ntransmitted < npackets)
984039d6aa4SBill Fenner 				pinger();
985039d6aa4SBill Fenner 			else {
986039d6aa4SBill Fenner 				if (almost_done)
987039d6aa4SBill Fenner 					break;
988039d6aa4SBill Fenner 				almost_done = 1;
9895e2cc0f4SStephen McKay 				intvl.tv_usec = 0;
990039d6aa4SBill Fenner 				if (nreceived) {
991039d6aa4SBill Fenner 					intvl.tv_sec = 2 * tmax / 1000;
992039d6aa4SBill Fenner 					if (!intvl.tv_sec)
993039d6aa4SBill Fenner 						intvl.tv_sec = 1;
994d6cd1497SGleb Smirnoff 				} else {
995d6cd1497SGleb Smirnoff 					intvl.tv_sec = waittime / 1000;
996d6cd1497SGleb Smirnoff 					intvl.tv_usec = waittime % 1000 * 1000;
997d6cd1497SGleb Smirnoff 				}
998039d6aa4SBill Fenner 			}
999039d6aa4SBill Fenner 			(void)gettimeofday(&last, NULL);
100025107197SIan Dowse 			if (ntransmitted - nreceived - 1 > nmissedmax) {
100125107197SIan Dowse 				nmissedmax = ntransmitted - nreceived - 1;
100225107197SIan Dowse 				if (options & F_MISSED)
1003ca517ad8SPoul-Henning Kamp 					(void)write(STDOUT_FILENO, &BBELL, 1);
1004039d6aa4SBill Fenner 			}
1005039d6aa4SBill Fenner 		}
100625107197SIan Dowse 	}
10078f975bb3SBruce Evans 	finish();
10088fae3551SRodney W. Grimes 	/* NOTREACHED */
1009f78ac61bSWarner Losh 	exit(0);	/* Make the compiler happy */
10108fae3551SRodney W. Grimes }
10118fae3551SRodney W. Grimes 
10128fae3551SRodney W. Grimes /*
10138f975bb3SBruce Evans  * stopit --
10148f975bb3SBruce Evans  *	Set the global bit that causes the main loop to quit.
10158f975bb3SBruce Evans  * Do NOT call finish() from here, since finish() does far too much
10168f975bb3SBruce Evans  * to be called from a signal handler.
1017515dd2faSJulian Elischer  */
1018515dd2faSJulian Elischer void
1019fafb8f11SEd Schouten stopit(int sig __unused)
1020515dd2faSJulian Elischer {
1021efc8588dSDavid E. O'Brien 
1022c8bb99e5SIan Dowse 	/*
1023c8bb99e5SIan Dowse 	 * When doing reverse DNS lookups, the finish_up flag might not
1024c8bb99e5SIan Dowse 	 * be noticed for a while.  Just exit if we get a second SIGINT.
1025c8bb99e5SIan Dowse 	 */
1026c8bb99e5SIan Dowse 	if (!(options & F_NUMERIC) && finish_up)
1027c8bb99e5SIan Dowse 		_exit(nreceived ? 0 : 2);
1028515dd2faSJulian Elischer 	finish_up = 1;
1029515dd2faSJulian Elischer }
1030515dd2faSJulian Elischer 
1031515dd2faSJulian Elischer /*
10328fae3551SRodney W. Grimes  * pinger --
10338fae3551SRodney W. Grimes  *	Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
10348fae3551SRodney W. Grimes  * will be added on by the kernel.  The ID field is our UNIX process ID,
1035eb1543c6SMatthew N. Dodd  * and the sequence number is an ascending integer.  The first TIMEVAL_LEN
10364fba6582SMaxim Konovalov  * bytes of the data portion are used to hold a UNIX "timeval" struct in
10374fba6582SMaxim Konovalov  * host byte-order, to compute the round-trip time.
10388fae3551SRodney W. Grimes  */
103943470e3bSGarrett Wollman static void
104043470e3bSGarrett Wollman pinger(void)
10418fae3551SRodney W. Grimes {
1042eb1543c6SMatthew N. Dodd 	struct timeval now;
104313e3f0b7SMaxim Konovalov 	struct tv32 tv32;
10440b2f8b3fSMaxim Konovalov 	struct ip *ip;
10453d438ad6SDavid E. O'Brien 	struct icmp *icp;
1046efc8588dSDavid E. O'Brien 	int cc, i;
10470b2f8b3fSMaxim Konovalov 	u_char *packet;
10488fae3551SRodney W. Grimes 
10490b2f8b3fSMaxim Konovalov 	packet = outpack;
10508fae3551SRodney W. Grimes 	icp = (struct icmp *)outpack;
1051eb1543c6SMatthew N. Dodd 	icp->icmp_type = icmp_type;
10528fae3551SRodney W. Grimes 	icp->icmp_code = 0;
10538fae3551SRodney W. Grimes 	icp->icmp_cksum = 0;
10545db89bc7SBill Fenner 	icp->icmp_seq = htons(ntransmitted);
10558fae3551SRodney W. Grimes 	icp->icmp_id = ident;			/* ID */
10568fae3551SRodney W. Grimes 
10575db89bc7SBill Fenner 	CLR(ntransmitted % mx_dup_ck);
10588fae3551SRodney W. Grimes 
1059eb1543c6SMatthew N. Dodd 	if ((options & F_TIME) || timing) {
1060eb1543c6SMatthew N. Dodd 		(void)gettimeofday(&now, NULL);
10618fae3551SRodney W. Grimes 
106213e3f0b7SMaxim Konovalov 		tv32.tv32_sec = htonl(now.tv_sec);
106313e3f0b7SMaxim Konovalov 		tv32.tv32_usec = htonl(now.tv_usec);
1064eb1543c6SMatthew N. Dodd 		if (options & F_TIME)
1065eb1543c6SMatthew N. Dodd 			icp->icmp_otime = htonl((now.tv_sec % (24*60*60))
1066eb1543c6SMatthew N. Dodd 				* 1000 + now.tv_usec / 1000);
1067eb1543c6SMatthew N. Dodd 		if (timing)
106813e3f0b7SMaxim Konovalov 			bcopy((void *)&tv32,
10690fe0c0ccSMaxim Konovalov 			    (void *)&outpack[ICMP_MINLEN + phdr_len],
107013e3f0b7SMaxim Konovalov 			    sizeof(tv32));
1071eb1543c6SMatthew N. Dodd 	}
1072eb1543c6SMatthew N. Dodd 
10730fe0c0ccSMaxim Konovalov 	cc = ICMP_MINLEN + phdr_len + datalen;
10748fae3551SRodney W. Grimes 
10758fae3551SRodney W. Grimes 	/* compute ICMP checksum here */
10768fae3551SRodney W. Grimes 	icp->icmp_cksum = in_cksum((u_short *)icp, cc);
10778fae3551SRodney W. Grimes 
10780b2f8b3fSMaxim Konovalov 	if (options & F_HDRINCL) {
10790b2f8b3fSMaxim Konovalov 		cc += sizeof(struct ip);
10800b2f8b3fSMaxim Konovalov 		ip = (struct ip *)outpackhdr;
10810b2f8b3fSMaxim Konovalov 		ip->ip_len = cc;
10820b2f8b3fSMaxim Konovalov 		ip->ip_sum = in_cksum((u_short *)outpackhdr, cc);
10830b2f8b3fSMaxim Konovalov 		packet = outpackhdr;
10840b2f8b3fSMaxim Konovalov 	}
1085*49133c6dSPawel Jakub Dawidek 	i = send(ssend, (char *)packet, cc, 0);
10868fae3551SRodney W. Grimes 	if (i < 0 || i != cc)  {
108743470e3bSGarrett Wollman 		if (i < 0) {
10888f975bb3SBruce Evans 			if (options & F_FLOOD && errno == ENOBUFS) {
1089515dd2faSJulian Elischer 				usleep(FLOOD_BACKOFF);
1090515dd2faSJulian Elischer 				return;
1091515dd2faSJulian Elischer 			}
109243470e3bSGarrett Wollman 			warn("sendto");
109343470e3bSGarrett Wollman 		} else {
109443470e3bSGarrett Wollman 			warn("%s: partial write: %d of %d bytes",
1095416aa49bSPoul-Henning Kamp 			     hostname, i, cc);
10968fae3551SRodney W. Grimes 		}
1097363d7bbeSJulian Elischer 	}
1098363d7bbeSJulian Elischer 	ntransmitted++;
10999ff95228SGleb Smirnoff 	sntransmitted++;
11008fae3551SRodney W. Grimes 	if (!(options & F_QUIET) && options & F_FLOOD)
11018fae3551SRodney W. Grimes 		(void)write(STDOUT_FILENO, &DOT, 1);
11028fae3551SRodney W. Grimes }
11038fae3551SRodney W. Grimes 
11048fae3551SRodney W. Grimes /*
11058fae3551SRodney W. Grimes  * pr_pack --
11068fae3551SRodney W. Grimes  *	Print out the packet, if it came from us.  This logic is necessary
11078fae3551SRodney W. Grimes  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
11088fae3551SRodney W. Grimes  * which arrive ('tis only fair).  This permits multiple copies of this
11098fae3551SRodney W. Grimes  * program to be run without having intermingled output (or statistics!).
11108fae3551SRodney W. Grimes  */
111143470e3bSGarrett Wollman static void
1112fafb8f11SEd Schouten pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv)
11138fae3551SRodney W. Grimes {
11149b219646SPeter Wemm 	struct in_addr ina;
11159b219646SPeter Wemm 	u_char *cp, *dp;
11163d438ad6SDavid E. O'Brien 	struct icmp *icp;
11178fae3551SRodney W. Grimes 	struct ip *ip;
11189d2b0ab8SPeter Wemm 	const void *tp;
11198f975bb3SBruce Evans 	double triptime;
1120e88178ddSMaxim Konovalov 	int dupflag, hlen, i, j, recv_len, seq;
1121efc8588dSDavid E. O'Brien 	static int old_rrlen;
1122efc8588dSDavid E. O'Brien 	static char old_rr[MAX_IPOPTLEN];
11238fae3551SRodney W. Grimes 
11248fae3551SRodney W. Grimes 	/* Check the IP header */
11258fae3551SRodney W. Grimes 	ip = (struct ip *)buf;
11268fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
1127e88178ddSMaxim Konovalov 	recv_len = cc;
11288fae3551SRodney W. Grimes 	if (cc < hlen + ICMP_MINLEN) {
11298fae3551SRodney W. Grimes 		if (options & F_VERBOSE)
113043470e3bSGarrett Wollman 			warn("packet too short (%d bytes) from %s", cc,
113143470e3bSGarrett Wollman 			     inet_ntoa(from->sin_addr));
11328fae3551SRodney W. Grimes 		return;
11338fae3551SRodney W. Grimes 	}
11348fae3551SRodney W. Grimes 
11358fae3551SRodney W. Grimes 	/* Now the ICMP part */
11368fae3551SRodney W. Grimes 	cc -= hlen;
11378fae3551SRodney W. Grimes 	icp = (struct icmp *)(buf + hlen);
1138eb1543c6SMatthew N. Dodd 	if (icp->icmp_type == icmp_type_rsp) {
11398fae3551SRodney W. Grimes 		if (icp->icmp_id != ident)
11408fae3551SRodney W. Grimes 			return;			/* 'Twas not our ECHO */
11418fae3551SRodney W. Grimes 		++nreceived;
11428f975bb3SBruce Evans 		triptime = 0.0;
11438fae3551SRodney W. Grimes 		if (timing) {
1144d32ff037SJohn Birrell 			struct timeval tv1;
114513e3f0b7SMaxim Konovalov 			struct tv32 tv32;
11468fae3551SRodney W. Grimes #ifndef icmp_data
11479d2b0ab8SPeter Wemm 			tp = &icp->icmp_ip;
11488fae3551SRodney W. Grimes #else
11499d2b0ab8SPeter Wemm 			tp = icp->icmp_data;
11508fae3551SRodney W. Grimes #endif
11514eae39bfSStefan Farfeleder 			tp = (const char *)tp + phdr_len;
1152143008a1SMatthew N. Dodd 
115347e9b3eaSMatthew N. Dodd 			if (cc - ICMP_MINLEN - phdr_len >= sizeof(tv1)) {
11549d2b0ab8SPeter Wemm 				/* Copy to avoid alignment problems: */
115513e3f0b7SMaxim Konovalov 				memcpy(&tv32, tp, sizeof(tv32));
115613e3f0b7SMaxim Konovalov 				tv1.tv_sec = ntohl(tv32.tv32_sec);
115713e3f0b7SMaxim Konovalov 				tv1.tv_usec = ntohl(tv32.tv32_usec);
1158039d6aa4SBill Fenner 				tvsub(tv, &tv1);
1159039d6aa4SBill Fenner  				triptime = ((double)tv->tv_sec) * 1000.0 +
1160039d6aa4SBill Fenner  				    ((double)tv->tv_usec) / 1000.0;
11618fae3551SRodney W. Grimes 				tsum += triptime;
11623109a910SGarrett Wollman 				tsumsq += triptime * triptime;
11638fae3551SRodney W. Grimes 				if (triptime < tmin)
11648fae3551SRodney W. Grimes 					tmin = triptime;
11658fae3551SRodney W. Grimes 				if (triptime > tmax)
11668fae3551SRodney W. Grimes 					tmax = triptime;
116747e9b3eaSMatthew N. Dodd 			} else
116847e9b3eaSMatthew N. Dodd 				timing = 0;
11698fae3551SRodney W. Grimes 		}
11708fae3551SRodney W. Grimes 
11715db89bc7SBill Fenner 		seq = ntohs(icp->icmp_seq);
11725db89bc7SBill Fenner 
11735db89bc7SBill Fenner 		if (TST(seq % mx_dup_ck)) {
11748fae3551SRodney W. Grimes 			++nrepeats;
11758fae3551SRodney W. Grimes 			--nreceived;
11768fae3551SRodney W. Grimes 			dupflag = 1;
11778fae3551SRodney W. Grimes 		} else {
11785db89bc7SBill Fenner 			SET(seq % mx_dup_ck);
11798fae3551SRodney W. Grimes 			dupflag = 0;
11808fae3551SRodney W. Grimes 		}
11818fae3551SRodney W. Grimes 
11828fae3551SRodney W. Grimes 		if (options & F_QUIET)
11838fae3551SRodney W. Grimes 			return;
11848fae3551SRodney W. Grimes 
1185d6cd1497SGleb Smirnoff 		if (options & F_WAITTIME && triptime > waittime) {
1186d6cd1497SGleb Smirnoff 			++nrcvtimeout;
1187d6cd1497SGleb Smirnoff 			return;
1188d6cd1497SGleb Smirnoff 		}
1189d6cd1497SGleb Smirnoff 
11908fae3551SRodney W. Grimes 		if (options & F_FLOOD)
11918fae3551SRodney W. Grimes 			(void)write(STDOUT_FILENO, &BSPACE, 1);
11928fae3551SRodney W. Grimes 		else {
11938fae3551SRodney W. Grimes 			(void)printf("%d bytes from %s: icmp_seq=%u", cc,
11948fae3551SRodney W. Grimes 			   inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
11955db89bc7SBill Fenner 			   seq);
11968fae3551SRodney W. Grimes 			(void)printf(" ttl=%d", ip->ip_ttl);
11978fae3551SRodney W. Grimes 			if (timing)
1198d410b6f1SDavid Greenman 				(void)printf(" time=%.3f ms", triptime);
11998fae3551SRodney W. Grimes 			if (dupflag)
12008fae3551SRodney W. Grimes 				(void)printf(" (DUP!)");
1201772dfa72SDaniel O'Callaghan 			if (options & F_AUDIBLE)
1202ca517ad8SPoul-Henning Kamp 				(void)write(STDOUT_FILENO, &BBELL, 1);
1203143008a1SMatthew N. Dodd 			if (options & F_MASK) {
1204143008a1SMatthew N. Dodd 				/* Just prentend this cast isn't ugly */
1205143008a1SMatthew N. Dodd 				(void)printf(" mask=%s",
1206143008a1SMatthew N. Dodd 					pr_addr(*(struct in_addr *)&(icp->icmp_mask)));
1207143008a1SMatthew N. Dodd 			}
1208eb1543c6SMatthew N. Dodd 			if (options & F_TIME) {
1209eb1543c6SMatthew N. Dodd 				(void)printf(" tso=%s", pr_ntime(icp->icmp_otime));
1210eb1543c6SMatthew N. Dodd 				(void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime));
1211eb1543c6SMatthew N. Dodd 				(void)printf(" tst=%s", pr_ntime(icp->icmp_ttime));
1212eb1543c6SMatthew N. Dodd 			}
1213e88178ddSMaxim Konovalov 			if (recv_len != send_len) {
1214e88178ddSMaxim Konovalov                         	(void)printf(
1215e88178ddSMaxim Konovalov 				     "\nwrong total length %d instead of %d",
1216e88178ddSMaxim Konovalov 				     recv_len, send_len);
1217e88178ddSMaxim Konovalov 			}
12188fae3551SRodney W. Grimes 			/* check the data */
1219eb1543c6SMatthew N. Dodd 			cp = (u_char*)&icp->icmp_data[phdr_len];
12200fe0c0ccSMaxim Konovalov 			dp = &outpack[ICMP_MINLEN + phdr_len];
122147e9b3eaSMatthew N. Dodd 			cc -= ICMP_MINLEN + phdr_len;
122229dccd6aSMaxim Konovalov 			i = 0;
122329dccd6aSMaxim Konovalov 			if (timing) {   /* don't check variable timestamp */
122429dccd6aSMaxim Konovalov 				cp += TIMEVAL_LEN;
122529dccd6aSMaxim Konovalov 				dp += TIMEVAL_LEN;
122629dccd6aSMaxim Konovalov 				cc -= TIMEVAL_LEN;
122729dccd6aSMaxim Konovalov 				i += TIMEVAL_LEN;
122829dccd6aSMaxim Konovalov 			}
122929dccd6aSMaxim Konovalov 			for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) {
12308fae3551SRodney W. Grimes 				if (*cp != *dp) {
12318fae3551SRodney W. Grimes 	(void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
12328fae3551SRodney W. Grimes 	    i, *dp, *cp);
12331ad0b1beSMaxim Konovalov 					(void)printf("\ncp:");
12348fae3551SRodney W. Grimes 					cp = (u_char*)&icp->icmp_data[0];
1235d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
1236e88178ddSMaxim Konovalov 						if ((i % 16) == 8)
1237d32ff037SJohn Birrell 							(void)printf("\n\t");
1238e88178ddSMaxim Konovalov 						(void)printf("%2x ", *cp);
1239d32ff037SJohn Birrell 					}
12401ad0b1beSMaxim Konovalov 					(void)printf("\ndp:");
12410fe0c0ccSMaxim Konovalov 					cp = &outpack[ICMP_MINLEN];
1242d32ff037SJohn Birrell 					for (i = 0; i < datalen; ++i, ++cp) {
1243e88178ddSMaxim Konovalov 						if ((i % 16) == 8)
12448fae3551SRodney W. Grimes 							(void)printf("\n\t");
1245e88178ddSMaxim Konovalov 						(void)printf("%2x ", *cp);
12468fae3551SRodney W. Grimes 					}
12478fae3551SRodney W. Grimes 					break;
12488fae3551SRodney W. Grimes 				}
12498fae3551SRodney W. Grimes 			}
12508fae3551SRodney W. Grimes 		}
12518fae3551SRodney W. Grimes 	} else {
1252ef9e6dc7SBill Fenner 		/*
1253ef9e6dc7SBill Fenner 		 * We've got something other than an ECHOREPLY.
1254ef9e6dc7SBill Fenner 		 * See if it's a reply to something that we sent.
1255ef9e6dc7SBill Fenner 		 * We can compare IP destination, protocol,
1256ef9e6dc7SBill Fenner 		 * and ICMP type and ID.
1257f78ac61bSWarner Losh 		 *
1258f78ac61bSWarner Losh 		 * Only print all the error messages if we are running
1259f78ac61bSWarner Losh 		 * as root to avoid leaking information not normally
1260f78ac61bSWarner Losh 		 * available to those not running as root.
1261ef9e6dc7SBill Fenner 		 */
1262ef9e6dc7SBill Fenner #ifndef icmp_data
1263ef9e6dc7SBill Fenner 		struct ip *oip = &icp->icmp_ip;
1264ef9e6dc7SBill Fenner #else
1265ef9e6dc7SBill Fenner 		struct ip *oip = (struct ip *)icp->icmp_data;
1266ef9e6dc7SBill Fenner #endif
1267ef9e6dc7SBill Fenner 		struct icmp *oicmp = (struct icmp *)(oip + 1);
1268ef9e6dc7SBill Fenner 
1269ee2bf734SWarner Losh 		if (((options & F_VERBOSE) && uid == 0) ||
1270ef9e6dc7SBill Fenner 		    (!(options & F_QUIET2) &&
1271d389e86aSMatt Jacob 		     (oip->ip_dst.s_addr == whereto.sin_addr.s_addr) &&
1272ef9e6dc7SBill Fenner 		     (oip->ip_p == IPPROTO_ICMP) &&
1273ef9e6dc7SBill Fenner 		     (oicmp->icmp_type == ICMP_ECHO) &&
1274ef9e6dc7SBill Fenner 		     (oicmp->icmp_id == ident))) {
12758fae3551SRodney W. Grimes 		    (void)printf("%d bytes from %s: ", cc,
127643470e3bSGarrett Wollman 			pr_addr(from->sin_addr));
12778fae3551SRodney W. Grimes 		    pr_icmph(icp);
1278ef9e6dc7SBill Fenner 		} else
1279ef9e6dc7SBill Fenner 		    return;
12808fae3551SRodney W. Grimes 	}
12818fae3551SRodney W. Grimes 
12828fae3551SRodney W. Grimes 	/* Display any IP options */
12838fae3551SRodney W. Grimes 	cp = (u_char *)buf + sizeof(struct ip);
12848fae3551SRodney W. Grimes 
12858fae3551SRodney W. Grimes 	for (; hlen > (int)sizeof(struct ip); --hlen, ++cp)
12868fae3551SRodney W. Grimes 		switch (*cp) {
12878fae3551SRodney W. Grimes 		case IPOPT_EOL:
12888fae3551SRodney W. Grimes 			hlen = 0;
12898fae3551SRodney W. Grimes 			break;
12908fae3551SRodney W. Grimes 		case IPOPT_LSRR:
1291cb75aca7SMaxim Konovalov 		case IPOPT_SSRR:
1292cb75aca7SMaxim Konovalov 			(void)printf(*cp == IPOPT_LSRR ?
1293cb75aca7SMaxim Konovalov 			    "\nLSRR: " : "\nSSRR: ");
1294301207dfSMaxim Konovalov 			j = cp[IPOPT_OLEN] - IPOPT_MINOFF + 1;
12958fae3551SRodney W. Grimes 			hlen -= 2;
1296301207dfSMaxim Konovalov 			cp += 2;
12973c721ab3SMaxim Konovalov 			if (j >= INADDR_LEN &&
12983c721ab3SMaxim Konovalov 			    j <= hlen - (int)sizeof(struct ip)) {
12998fae3551SRodney W. Grimes 				for (;;) {
1300301207dfSMaxim Konovalov 					bcopy(++cp, &ina.s_addr, INADDR_LEN);
1301301207dfSMaxim Konovalov 					if (ina.s_addr == 0)
13021ad0b1beSMaxim Konovalov 						(void)printf("\t0.0.0.0");
1303301207dfSMaxim Konovalov 					else
13041ad0b1beSMaxim Konovalov 						(void)printf("\t%s",
13051ad0b1beSMaxim Konovalov 						     pr_addr(ina));
1306301207dfSMaxim Konovalov 					hlen -= INADDR_LEN;
1307301207dfSMaxim Konovalov 					cp += INADDR_LEN - 1;
1308301207dfSMaxim Konovalov 					j -= INADDR_LEN;
1309301207dfSMaxim Konovalov 					if (j < INADDR_LEN)
13108fae3551SRodney W. Grimes 						break;
13118fae3551SRodney W. Grimes 					(void)putchar('\n');
13128fae3551SRodney W. Grimes 				}
1313301207dfSMaxim Konovalov 			} else
1314301207dfSMaxim Konovalov 				(void)printf("\t(truncated route)\n");
13158fae3551SRodney W. Grimes 			break;
13168fae3551SRodney W. Grimes 		case IPOPT_RR:
1317301207dfSMaxim Konovalov 			j = cp[IPOPT_OLEN];		/* get length */
1318301207dfSMaxim Konovalov 			i = cp[IPOPT_OFFSET];		/* and pointer */
13198fae3551SRodney W. Grimes 			hlen -= 2;
1320301207dfSMaxim Konovalov 			cp += 2;
13218fae3551SRodney W. Grimes 			if (i > j)
13228fae3551SRodney W. Grimes 				i = j;
1323301207dfSMaxim Konovalov 			i = i - IPOPT_MINOFF + 1;
1324301207dfSMaxim Konovalov 			if (i < 0 || i > (hlen - (int)sizeof(struct ip))) {
1325301207dfSMaxim Konovalov 				old_rrlen = 0;
13268fae3551SRodney W. Grimes 				continue;
1327301207dfSMaxim Konovalov 			}
13288fae3551SRodney W. Grimes 			if (i == old_rrlen
13298fae3551SRodney W. Grimes 			    && !bcmp((char *)cp, old_rr, i)
13308fae3551SRodney W. Grimes 			    && !(options & F_FLOOD)) {
13318fae3551SRodney W. Grimes 				(void)printf("\t(same route)");
13328fae3551SRodney W. Grimes 				hlen -= i;
13338fae3551SRodney W. Grimes 				cp += i;
13348fae3551SRodney W. Grimes 				break;
13358fae3551SRodney W. Grimes 			}
13368fae3551SRodney W. Grimes 			old_rrlen = i;
13378fae3551SRodney W. Grimes 			bcopy((char *)cp, old_rr, i);
13388fae3551SRodney W. Grimes 			(void)printf("\nRR: ");
1339301207dfSMaxim Konovalov 			if (i >= INADDR_LEN &&
1340301207dfSMaxim Konovalov 			    i <= hlen - (int)sizeof(struct ip)) {
13418fae3551SRodney W. Grimes 				for (;;) {
1342301207dfSMaxim Konovalov 					bcopy(++cp, &ina.s_addr, INADDR_LEN);
1343301207dfSMaxim Konovalov 					if (ina.s_addr == 0)
13441ad0b1beSMaxim Konovalov 						(void)printf("\t0.0.0.0");
1345301207dfSMaxim Konovalov 					else
1346301207dfSMaxim Konovalov 						(void)printf("\t%s",
1347301207dfSMaxim Konovalov 						     pr_addr(ina));
1348301207dfSMaxim Konovalov 					hlen -= INADDR_LEN;
1349301207dfSMaxim Konovalov 					cp += INADDR_LEN - 1;
1350301207dfSMaxim Konovalov 					i -= INADDR_LEN;
1351301207dfSMaxim Konovalov 					if (i < INADDR_LEN)
13528fae3551SRodney W. Grimes 						break;
13538fae3551SRodney W. Grimes 					(void)putchar('\n');
13548fae3551SRodney W. Grimes 				}
1355301207dfSMaxim Konovalov 			} else
1356301207dfSMaxim Konovalov 				(void)printf("\t(truncated route)");
13578fae3551SRodney W. Grimes 			break;
13588fae3551SRodney W. Grimes 		case IPOPT_NOP:
13598fae3551SRodney W. Grimes 			(void)printf("\nNOP");
13608fae3551SRodney W. Grimes 			break;
13618fae3551SRodney W. Grimes 		default:
13628fae3551SRodney W. Grimes 			(void)printf("\nunknown option %x", *cp);
13638fae3551SRodney W. Grimes 			break;
13648fae3551SRodney W. Grimes 		}
13658fae3551SRodney W. Grimes 	if (!(options & F_FLOOD)) {
13668fae3551SRodney W. Grimes 		(void)putchar('\n');
13678fae3551SRodney W. Grimes 		(void)fflush(stdout);
13688fae3551SRodney W. Grimes 	}
13698fae3551SRodney W. Grimes }
13708fae3551SRodney W. Grimes 
13718fae3551SRodney W. Grimes /*
13728fae3551SRodney W. Grimes  * in_cksum --
13738fae3551SRodney W. Grimes  *	Checksum routine for Internet Protocol family headers (C Version)
13748fae3551SRodney W. Grimes  */
137543470e3bSGarrett Wollman u_short
1376fafb8f11SEd Schouten in_cksum(u_short *addr, int len)
13778fae3551SRodney W. Grimes {
1378efc8588dSDavid E. O'Brien 	int nleft, sum;
1379efc8588dSDavid E. O'Brien 	u_short *w;
13803ec12efeSPierre Beyssac 	union {
138109333e78SPierre Beyssac 		u_short	us;
138209333e78SPierre Beyssac 		u_char	uc[2];
138309333e78SPierre Beyssac 	} last;
138409333e78SPierre Beyssac 	u_short answer;
13858fae3551SRodney W. Grimes 
1386efc8588dSDavid E. O'Brien 	nleft = len;
1387efc8588dSDavid E. O'Brien 	sum = 0;
1388efc8588dSDavid E. O'Brien 	w = addr;
1389efc8588dSDavid E. O'Brien 
13908fae3551SRodney W. Grimes 	/*
13918fae3551SRodney W. Grimes 	 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
13928fae3551SRodney W. Grimes 	 * sequential 16 bit words to it, and at the end, fold back all the
13938fae3551SRodney W. Grimes 	 * carry bits from the top 16 bits into the lower 16 bits.
13948fae3551SRodney W. Grimes 	 */
13958fae3551SRodney W. Grimes 	while (nleft > 1)  {
13968fae3551SRodney W. Grimes 		sum += *w++;
13978fae3551SRodney W. Grimes 		nleft -= 2;
13988fae3551SRodney W. Grimes 	}
13998fae3551SRodney W. Grimes 
14008fae3551SRodney W. Grimes 	/* mop up an odd byte, if necessary */
14018fae3551SRodney W. Grimes 	if (nleft == 1) {
140209333e78SPierre Beyssac 		last.uc[0] = *(u_char *)w;
140309333e78SPierre Beyssac 		last.uc[1] = 0;
140409333e78SPierre Beyssac 		sum += last.us;
14058fae3551SRodney W. Grimes 	}
14068fae3551SRodney W. Grimes 
14078fae3551SRodney W. Grimes 	/* add back carry outs from top 16 bits to low 16 bits */
14088fae3551SRodney W. Grimes 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
14098fae3551SRodney W. Grimes 	sum += (sum >> 16);			/* add carry */
141009333e78SPierre Beyssac 	answer = ~sum;				/* truncate to 16 bits */
141109333e78SPierre Beyssac 	return(answer);
14128fae3551SRodney W. Grimes }
14138fae3551SRodney W. Grimes 
14148fae3551SRodney W. Grimes /*
14158fae3551SRodney W. Grimes  * tvsub --
14168fae3551SRodney W. Grimes  *	Subtract 2 timeval structs:  out = out - in.  Out is assumed to
14178fae3551SRodney W. Grimes  * be >= in.
14188fae3551SRodney W. Grimes  */
141943470e3bSGarrett Wollman static void
1420fafb8f11SEd Schouten tvsub(struct timeval *out, const struct timeval *in)
14218fae3551SRodney W. Grimes {
1422efc8588dSDavid E. O'Brien 
14238fae3551SRodney W. Grimes 	if ((out->tv_usec -= in->tv_usec) < 0) {
14248fae3551SRodney W. Grimes 		--out->tv_sec;
14258fae3551SRodney W. Grimes 		out->tv_usec += 1000000;
14268fae3551SRodney W. Grimes 	}
14278fae3551SRodney W. Grimes 	out->tv_sec -= in->tv_sec;
14288fae3551SRodney W. Grimes }
14298fae3551SRodney W. Grimes 
14308fae3551SRodney W. Grimes /*
1431badd8138SSean Eric Fagan  * status --
1432badd8138SSean Eric Fagan  *	Print out statistics when SIGINFO is received.
1433badd8138SSean Eric Fagan  */
1434badd8138SSean Eric Fagan 
143543470e3bSGarrett Wollman static void
1436fafb8f11SEd Schouten status(int sig __unused)
14377d81b35cSBruce Evans {
1438efc8588dSDavid E. O'Brien 
143937e5b2c6SSean Eric Fagan 	siginfo_p = 1;
144037e5b2c6SSean Eric Fagan }
144137e5b2c6SSean Eric Fagan 
144243470e3bSGarrett Wollman static void
1443fafb8f11SEd Schouten check_status(void)
1444badd8138SSean Eric Fagan {
1445efc8588dSDavid E. O'Brien 
144637e5b2c6SSean Eric Fagan 	if (siginfo_p) {
144737e5b2c6SSean Eric Fagan 		siginfo_p = 0;
1448aa822c39SDima Dorfman 		(void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)",
14497d81b35cSBruce Evans 		    nreceived, ntransmitted,
1450aed98a27SMaxim Konovalov 		    ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0);
1451aed98a27SMaxim Konovalov 		if (nreceived && timing)
1452aed98a27SMaxim Konovalov 			(void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max",
1453aed98a27SMaxim Konovalov 			    tmin, tsum / (nreceived + nrepeats), tmax);
1454aed98a27SMaxim Konovalov 		(void)fprintf(stderr, "\n");
145537e5b2c6SSean Eric Fagan 	}
1456badd8138SSean Eric Fagan }
1457badd8138SSean Eric Fagan 
1458badd8138SSean Eric Fagan /*
14598fae3551SRodney W. Grimes  * finish --
14608fae3551SRodney W. Grimes  *	Print out statistics, and give up.
14618fae3551SRodney W. Grimes  */
146243470e3bSGarrett Wollman static void
1463fafb8f11SEd Schouten finish(void)
14648fae3551SRodney W. Grimes {
14658fae3551SRodney W. Grimes 
14668fae3551SRodney W. Grimes 	(void)signal(SIGINT, SIG_IGN);
1467a2a00888SSean Eric Fagan 	(void)signal(SIGALRM, SIG_IGN);
14688fae3551SRodney W. Grimes 	(void)putchar('\n');
14698fae3551SRodney W. Grimes 	(void)fflush(stdout);
14708fae3551SRodney W. Grimes 	(void)printf("--- %s ping statistics ---\n", hostname);
14718fae3551SRodney W. Grimes 	(void)printf("%ld packets transmitted, ", ntransmitted);
14728fae3551SRodney W. Grimes 	(void)printf("%ld packets received, ", nreceived);
14738fae3551SRodney W. Grimes 	if (nrepeats)
14748fae3551SRodney W. Grimes 		(void)printf("+%ld duplicates, ", nrepeats);
1475ebe70c8fSWarner Losh 	if (ntransmitted) {
14768fae3551SRodney W. Grimes 		if (nreceived > ntransmitted)
14778fae3551SRodney W. Grimes 			(void)printf("-- somebody's printing up packets!");
14788fae3551SRodney W. Grimes 		else
1479aa822c39SDima Dorfman 			(void)printf("%.1f%% packet loss",
1480aa822c39SDima Dorfman 			    ((ntransmitted - nreceived) * 100.0) /
1481aa822c39SDima Dorfman 			    ntransmitted);
1482ebe70c8fSWarner Losh 	}
1483d6cd1497SGleb Smirnoff 	if (nrcvtimeout)
1484d6cd1497SGleb Smirnoff 		(void)printf(", %ld packets out of wait time", nrcvtimeout);
14858fae3551SRodney W. Grimes 	(void)putchar('\n');
14863109a910SGarrett Wollman 	if (nreceived && timing) {
14873109a910SGarrett Wollman 		double n = nreceived + nrepeats;
14883109a910SGarrett Wollman 		double avg = tsum / n;
14893109a910SGarrett Wollman 		double vari = tsumsq / n - avg * avg;
14901ad0b1beSMaxim Konovalov 		(void)printf(
14911ad0b1beSMaxim Konovalov 		    "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
14923109a910SGarrett Wollman 		    tmin, avg, tmax, sqrt(vari));
14933109a910SGarrett Wollman 	}
1494badd8138SSean Eric Fagan 
14956e1173dcSDavid Greenman 	if (nreceived)
14968fae3551SRodney W. Grimes 		exit(0);
14976e1173dcSDavid Greenman 	else
14986e1173dcSDavid Greenman 		exit(2);
14998fae3551SRodney W. Grimes }
15008fae3551SRodney W. Grimes 
15018fae3551SRodney W. Grimes #ifdef notdef
15028fae3551SRodney W. Grimes static char *ttab[] = {
15038fae3551SRodney W. Grimes 	"Echo Reply",		/* ip + seq + udata */
15048fae3551SRodney W. Grimes 	"Dest Unreachable",	/* net, host, proto, port, frag, sr + IP */
15058fae3551SRodney W. Grimes 	"Source Quench",	/* IP */
15068fae3551SRodney W. Grimes 	"Redirect",		/* redirect type, gateway, + IP  */
15078fae3551SRodney W. Grimes 	"Echo",
15088fae3551SRodney W. Grimes 	"Time Exceeded",	/* transit, frag reassem + IP */
15098fae3551SRodney W. Grimes 	"Parameter Problem",	/* pointer + IP */
15108fae3551SRodney W. Grimes 	"Timestamp",		/* id + seq + three timestamps */
15118fae3551SRodney W. Grimes 	"Timestamp Reply",	/* " */
15128fae3551SRodney W. Grimes 	"Info Request",		/* id + sq */
15138fae3551SRodney W. Grimes 	"Info Reply"		/* " */
15148fae3551SRodney W. Grimes };
15158fae3551SRodney W. Grimes #endif
15168fae3551SRodney W. Grimes 
15178fae3551SRodney W. Grimes /*
15188fae3551SRodney W. Grimes  * pr_icmph --
15198fae3551SRodney W. Grimes  *	Print a descriptive string about an ICMP header.
15208fae3551SRodney W. Grimes  */
152143470e3bSGarrett Wollman static void
1522fafb8f11SEd Schouten pr_icmph(struct icmp *icp)
15238fae3551SRodney W. Grimes {
1524efc8588dSDavid E. O'Brien 
15258fae3551SRodney W. Grimes 	switch(icp->icmp_type) {
15268fae3551SRodney W. Grimes 	case ICMP_ECHOREPLY:
15278fae3551SRodney W. Grimes 		(void)printf("Echo Reply\n");
15288fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
15298fae3551SRodney W. Grimes 		break;
15308fae3551SRodney W. Grimes 	case ICMP_UNREACH:
15318fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
15328fae3551SRodney W. Grimes 		case ICMP_UNREACH_NET:
15338fae3551SRodney W. Grimes 			(void)printf("Destination Net Unreachable\n");
15348fae3551SRodney W. Grimes 			break;
15358fae3551SRodney W. Grimes 		case ICMP_UNREACH_HOST:
15368fae3551SRodney W. Grimes 			(void)printf("Destination Host Unreachable\n");
15378fae3551SRodney W. Grimes 			break;
15388fae3551SRodney W. Grimes 		case ICMP_UNREACH_PROTOCOL:
15398fae3551SRodney W. Grimes 			(void)printf("Destination Protocol Unreachable\n");
15408fae3551SRodney W. Grimes 			break;
15418fae3551SRodney W. Grimes 		case ICMP_UNREACH_PORT:
15428fae3551SRodney W. Grimes 			(void)printf("Destination Port Unreachable\n");
15438fae3551SRodney W. Grimes 			break;
15448fae3551SRodney W. Grimes 		case ICMP_UNREACH_NEEDFRAG:
1545ef9e6dc7SBill Fenner 			(void)printf("frag needed and DF set (MTU %d)\n",
1546ff49597eSBill Fenner 					ntohs(icp->icmp_nextmtu));
15478fae3551SRodney W. Grimes 			break;
15488fae3551SRodney W. Grimes 		case ICMP_UNREACH_SRCFAIL:
15498fae3551SRodney W. Grimes 			(void)printf("Source Route Failed\n");
15508fae3551SRodney W. Grimes 			break;
1551ef9e6dc7SBill Fenner 		case ICMP_UNREACH_FILTER_PROHIB:
1552ef9e6dc7SBill Fenner 			(void)printf("Communication prohibited by filter\n");
1553ef9e6dc7SBill Fenner 			break;
15548fae3551SRodney W. Grimes 		default:
15558fae3551SRodney W. Grimes 			(void)printf("Dest Unreachable, Bad Code: %d\n",
15568fae3551SRodney W. Grimes 			    icp->icmp_code);
15578fae3551SRodney W. Grimes 			break;
15588fae3551SRodney W. Grimes 		}
15598fae3551SRodney W. Grimes 		/* Print returned IP header information */
15608fae3551SRodney W. Grimes #ifndef icmp_data
15618fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
15628fae3551SRodney W. Grimes #else
15638fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
15648fae3551SRodney W. Grimes #endif
15658fae3551SRodney W. Grimes 		break;
15668fae3551SRodney W. Grimes 	case ICMP_SOURCEQUENCH:
15678fae3551SRodney W. Grimes 		(void)printf("Source Quench\n");
15688fae3551SRodney W. Grimes #ifndef icmp_data
15698fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
15708fae3551SRodney W. Grimes #else
15718fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
15728fae3551SRodney W. Grimes #endif
15738fae3551SRodney W. Grimes 		break;
15748fae3551SRodney W. Grimes 	case ICMP_REDIRECT:
15758fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
15768fae3551SRodney W. Grimes 		case ICMP_REDIRECT_NET:
15778fae3551SRodney W. Grimes 			(void)printf("Redirect Network");
15788fae3551SRodney W. Grimes 			break;
15798fae3551SRodney W. Grimes 		case ICMP_REDIRECT_HOST:
15808fae3551SRodney W. Grimes 			(void)printf("Redirect Host");
15818fae3551SRodney W. Grimes 			break;
15828fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSNET:
15838fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Network");
15848fae3551SRodney W. Grimes 			break;
15858fae3551SRodney W. Grimes 		case ICMP_REDIRECT_TOSHOST:
15868fae3551SRodney W. Grimes 			(void)printf("Redirect Type of Service and Host");
15878fae3551SRodney W. Grimes 			break;
15888fae3551SRodney W. Grimes 		default:
15898fae3551SRodney W. Grimes 			(void)printf("Redirect, Bad Code: %d", icp->icmp_code);
15908fae3551SRodney W. Grimes 			break;
15918fae3551SRodney W. Grimes 		}
1592ff49597eSBill Fenner 		(void)printf("(New addr: %s)\n", inet_ntoa(icp->icmp_gwaddr));
15938fae3551SRodney W. Grimes #ifndef icmp_data
15948fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
15958fae3551SRodney W. Grimes #else
15968fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
15978fae3551SRodney W. Grimes #endif
15988fae3551SRodney W. Grimes 		break;
15998fae3551SRodney W. Grimes 	case ICMP_ECHO:
16008fae3551SRodney W. Grimes 		(void)printf("Echo Request\n");
16018fae3551SRodney W. Grimes 		/* XXX ID + Seq + Data */
16028fae3551SRodney W. Grimes 		break;
16038fae3551SRodney W. Grimes 	case ICMP_TIMXCEED:
16048fae3551SRodney W. Grimes 		switch(icp->icmp_code) {
16058fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_INTRANS:
16068fae3551SRodney W. Grimes 			(void)printf("Time to live exceeded\n");
16078fae3551SRodney W. Grimes 			break;
16088fae3551SRodney W. Grimes 		case ICMP_TIMXCEED_REASS:
16098fae3551SRodney W. Grimes 			(void)printf("Frag reassembly time exceeded\n");
16108fae3551SRodney W. Grimes 			break;
16118fae3551SRodney W. Grimes 		default:
16128fae3551SRodney W. Grimes 			(void)printf("Time exceeded, Bad Code: %d\n",
16138fae3551SRodney W. Grimes 			    icp->icmp_code);
16148fae3551SRodney W. Grimes 			break;
16158fae3551SRodney W. Grimes 		}
16168fae3551SRodney W. Grimes #ifndef icmp_data
16178fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
16188fae3551SRodney W. Grimes #else
16198fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
16208fae3551SRodney W. Grimes #endif
16218fae3551SRodney W. Grimes 		break;
16228fae3551SRodney W. Grimes 	case ICMP_PARAMPROB:
16238fae3551SRodney W. Grimes 		(void)printf("Parameter problem: pointer = 0x%02x\n",
16248fae3551SRodney W. Grimes 		    icp->icmp_hun.ih_pptr);
16258fae3551SRodney W. Grimes #ifndef icmp_data
16268fae3551SRodney W. Grimes 		pr_retip(&icp->icmp_ip);
16278fae3551SRodney W. Grimes #else
16288fae3551SRodney W. Grimes 		pr_retip((struct ip *)icp->icmp_data);
16298fae3551SRodney W. Grimes #endif
16308fae3551SRodney W. Grimes 		break;
16318fae3551SRodney W. Grimes 	case ICMP_TSTAMP:
16328fae3551SRodney W. Grimes 		(void)printf("Timestamp\n");
16338fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
16348fae3551SRodney W. Grimes 		break;
16358fae3551SRodney W. Grimes 	case ICMP_TSTAMPREPLY:
16368fae3551SRodney W. Grimes 		(void)printf("Timestamp Reply\n");
16378fae3551SRodney W. Grimes 		/* XXX ID + Seq + 3 timestamps */
16388fae3551SRodney W. Grimes 		break;
16398fae3551SRodney W. Grimes 	case ICMP_IREQ:
16408fae3551SRodney W. Grimes 		(void)printf("Information Request\n");
16418fae3551SRodney W. Grimes 		/* XXX ID + Seq */
16428fae3551SRodney W. Grimes 		break;
16438fae3551SRodney W. Grimes 	case ICMP_IREQREPLY:
16448fae3551SRodney W. Grimes 		(void)printf("Information Reply\n");
16458fae3551SRodney W. Grimes 		/* XXX ID + Seq */
16468fae3551SRodney W. Grimes 		break;
16478fae3551SRodney W. Grimes 	case ICMP_MASKREQ:
16488fae3551SRodney W. Grimes 		(void)printf("Address Mask Request\n");
16498fae3551SRodney W. Grimes 		break;
16508fae3551SRodney W. Grimes 	case ICMP_MASKREPLY:
16518fae3551SRodney W. Grimes 		(void)printf("Address Mask Reply\n");
16528fae3551SRodney W. Grimes 		break;
1653ef9e6dc7SBill Fenner 	case ICMP_ROUTERADVERT:
1654ef9e6dc7SBill Fenner 		(void)printf("Router Advertisement\n");
1655ef9e6dc7SBill Fenner 		break;
1656ef9e6dc7SBill Fenner 	case ICMP_ROUTERSOLICIT:
1657ef9e6dc7SBill Fenner 		(void)printf("Router Solicitation\n");
1658ef9e6dc7SBill Fenner 		break;
16598fae3551SRodney W. Grimes 	default:
16608fae3551SRodney W. Grimes 		(void)printf("Bad ICMP type: %d\n", icp->icmp_type);
16618fae3551SRodney W. Grimes 	}
16628fae3551SRodney W. Grimes }
16638fae3551SRodney W. Grimes 
16648fae3551SRodney W. Grimes /*
16658fae3551SRodney W. Grimes  * pr_iph --
16668fae3551SRodney W. Grimes  *	Print an IP header with options.
16678fae3551SRodney W. Grimes  */
166843470e3bSGarrett Wollman static void
1669fafb8f11SEd Schouten pr_iph(struct ip *ip)
16708fae3551SRodney W. Grimes {
16718fae3551SRodney W. Grimes 	u_char *cp;
1672efc8588dSDavid E. O'Brien 	int hlen;
16738fae3551SRodney W. Grimes 
16748fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
16758fae3551SRodney W. Grimes 	cp = (u_char *)ip + 20;		/* point to options */
16768fae3551SRodney W. Grimes 
1677ef9e6dc7SBill Fenner 	(void)printf("Vr HL TOS  Len   ID Flg  off TTL Pro  cks      Src      Dst\n");
16788fae3551SRodney W. Grimes 	(void)printf(" %1x  %1x  %02x %04x %04x",
1679ef9e6dc7SBill Fenner 	    ip->ip_v, ip->ip_hl, ip->ip_tos, ntohs(ip->ip_len),
1680ef9e6dc7SBill Fenner 	    ntohs(ip->ip_id));
1681d32ff037SJohn Birrell 	(void)printf("   %1lx %04lx",
1682d32ff037SJohn Birrell 	    (u_long) (ntohl(ip->ip_off) & 0xe000) >> 13,
1683d32ff037SJohn Birrell 	    (u_long) ntohl(ip->ip_off) & 0x1fff);
1684ef9e6dc7SBill Fenner 	(void)printf("  %02x  %02x %04x", ip->ip_ttl, ip->ip_p,
1685ef9e6dc7SBill Fenner 							    ntohs(ip->ip_sum));
16868fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
16878fae3551SRodney W. Grimes 	(void)printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1688ef9e6dc7SBill Fenner 	/* dump any option bytes */
16898fae3551SRodney W. Grimes 	while (hlen-- > 20) {
16908fae3551SRodney W. Grimes 		(void)printf("%02x", *cp++);
16918fae3551SRodney W. Grimes 	}
16928fae3551SRodney W. Grimes 	(void)putchar('\n');
16938fae3551SRodney W. Grimes }
16948fae3551SRodney W. Grimes 
16958fae3551SRodney W. Grimes /*
16968fae3551SRodney W. Grimes  * pr_addr --
16978fae3551SRodney W. Grimes  *	Return an ascii host address as a dotted quad and optionally with
16988fae3551SRodney W. Grimes  * a hostname.
16998fae3551SRodney W. Grimes  */
170043470e3bSGarrett Wollman static char *
1701fafb8f11SEd Schouten pr_addr(struct in_addr ina)
17028fae3551SRodney W. Grimes {
17038fae3551SRodney W. Grimes 	struct hostent *hp;
1704f78ac61bSWarner Losh 	static char buf[16 + 3 + MAXHOSTNAMELEN];
17058fae3551SRodney W. Grimes 
1706*49133c6dSPawel Jakub Dawidek 	if (options & F_NUMERIC)
170743470e3bSGarrett Wollman 		return inet_ntoa(ina);
1708*49133c6dSPawel Jakub Dawidek 
1709*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
1710*49133c6dSPawel Jakub Dawidek 	if (capdns != NULL)
1711*49133c6dSPawel Jakub Dawidek 		hp = cap_gethostbyaddr(capdns, (char *)&ina, 4, AF_INET);
17128fae3551SRodney W. Grimes 	else
1713*49133c6dSPawel Jakub Dawidek #endif
1714*49133c6dSPawel Jakub Dawidek 		hp = gethostbyaddr((char *)&ina, 4, AF_INET);
1715*49133c6dSPawel Jakub Dawidek 
1716*49133c6dSPawel Jakub Dawidek 	if (hp == NULL)
1717*49133c6dSPawel Jakub Dawidek 		return inet_ntoa(ina);
1718*49133c6dSPawel Jakub Dawidek 
1719efa38539SPeter Wemm 	(void)snprintf(buf, sizeof(buf), "%s (%s)", hp->h_name,
172043470e3bSGarrett Wollman 	    inet_ntoa(ina));
17218fae3551SRodney W. Grimes 	return(buf);
17228fae3551SRodney W. Grimes }
17238fae3551SRodney W. Grimes 
17248fae3551SRodney W. Grimes /*
17258fae3551SRodney W. Grimes  * pr_retip --
17268fae3551SRodney W. Grimes  *	Dump some info on a returned (via ICMP) IP packet.
17278fae3551SRodney W. Grimes  */
172843470e3bSGarrett Wollman static void
1729fafb8f11SEd Schouten pr_retip(struct ip *ip)
17308fae3551SRodney W. Grimes {
17318fae3551SRodney W. Grimes 	u_char *cp;
1732efc8588dSDavid E. O'Brien 	int hlen;
17338fae3551SRodney W. Grimes 
17348fae3551SRodney W. Grimes 	pr_iph(ip);
17358fae3551SRodney W. Grimes 	hlen = ip->ip_hl << 2;
17368fae3551SRodney W. Grimes 	cp = (u_char *)ip + hlen;
17378fae3551SRodney W. Grimes 
17388fae3551SRodney W. Grimes 	if (ip->ip_p == 6)
17398fae3551SRodney W. Grimes 		(void)printf("TCP: from port %u, to port %u (decimal)\n",
17408fae3551SRodney W. Grimes 		    (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
17418fae3551SRodney W. Grimes 	else if (ip->ip_p == 17)
17428fae3551SRodney W. Grimes 		(void)printf("UDP: from port %u, to port %u (decimal)\n",
17438fae3551SRodney W. Grimes 			(*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
17448fae3551SRodney W. Grimes }
17458fae3551SRodney W. Grimes 
1746eb1543c6SMatthew N. Dodd static char *
1747007fe4e3SMaxim Konovalov pr_ntime(n_time timestamp)
1748eb1543c6SMatthew N. Dodd {
1749eb1543c6SMatthew N. Dodd 	static char buf[10];
1750007fe4e3SMaxim Konovalov 	int hour, min, sec;
1751eb1543c6SMatthew N. Dodd 
1752007fe4e3SMaxim Konovalov 	sec = ntohl(timestamp) / 1000;
1753007fe4e3SMaxim Konovalov 	hour = sec / 60 / 60;
1754007fe4e3SMaxim Konovalov 	min = (sec % (60 * 60)) / 60;
1755007fe4e3SMaxim Konovalov 	sec = (sec % (60 * 60)) % 60;
1756eb1543c6SMatthew N. Dodd 
1757007fe4e3SMaxim Konovalov 	(void)snprintf(buf, sizeof(buf), "%02d:%02d:%02d", hour, min, sec);
1758eb1543c6SMatthew N. Dodd 
1759eb1543c6SMatthew N. Dodd 	return (buf);
1760eb1543c6SMatthew N. Dodd }
1761eb1543c6SMatthew N. Dodd 
176243470e3bSGarrett Wollman static void
1763fafb8f11SEd Schouten fill(char *bp, char *patp)
17648fae3551SRodney W. Grimes {
17658fae3551SRodney W. Grimes 	char *cp;
1766efc8588dSDavid E. O'Brien 	int pat[16];
17674fba6582SMaxim Konovalov 	u_int ii, jj, kk;
17688fae3551SRodney W. Grimes 
176943470e3bSGarrett Wollman 	for (cp = patp; *cp; cp++) {
177043470e3bSGarrett Wollman 		if (!isxdigit(*cp))
177143470e3bSGarrett Wollman 			errx(EX_USAGE,
177243470e3bSGarrett Wollman 			    "patterns must be specified as hex digits");
177343470e3bSGarrett Wollman 
17748fae3551SRodney W. Grimes 	}
17758fae3551SRodney W. Grimes 	ii = sscanf(patp,
17768fae3551SRodney W. Grimes 	    "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
17778fae3551SRodney W. Grimes 	    &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
17788fae3551SRodney W. Grimes 	    &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
17798fae3551SRodney W. Grimes 	    &pat[13], &pat[14], &pat[15]);
17808fae3551SRodney W. Grimes 
17818fae3551SRodney W. Grimes 	if (ii > 0)
1782d829c3dfSMatthew N. Dodd 		for (kk = 0; kk <= maxpayload - (TIMEVAL_LEN + ii); kk += ii)
17838fae3551SRodney W. Grimes 			for (jj = 0; jj < ii; ++jj)
17848fae3551SRodney W. Grimes 				bp[jj + kk] = pat[jj];
17858fae3551SRodney W. Grimes 	if (!(options & F_QUIET)) {
17868fae3551SRodney W. Grimes 		(void)printf("PATTERN: 0x");
17878fae3551SRodney W. Grimes 		for (jj = 0; jj < ii; ++jj)
17888fae3551SRodney W. Grimes 			(void)printf("%02x", bp[jj] & 0xFF);
17898fae3551SRodney W. Grimes 		(void)printf("\n");
17908fae3551SRodney W. Grimes 	}
17918fae3551SRodney W. Grimes }
17928fae3551SRodney W. Grimes 
1793*49133c6dSPawel Jakub Dawidek #ifdef HAVE_LIBCAPSICUM
1794*49133c6dSPawel Jakub Dawidek static cap_channel_t *
1795*49133c6dSPawel Jakub Dawidek capdns_setup(void)
1796*49133c6dSPawel Jakub Dawidek {
1797*49133c6dSPawel Jakub Dawidek 	cap_channel_t *capcas, *capdnsloc;
1798*49133c6dSPawel Jakub Dawidek 	const char *types[2];
1799*49133c6dSPawel Jakub Dawidek 	int families[1];
1800*49133c6dSPawel Jakub Dawidek 
1801*49133c6dSPawel Jakub Dawidek 	capcas = cap_init();
1802*49133c6dSPawel Jakub Dawidek 	if (capcas == NULL) {
1803*49133c6dSPawel Jakub Dawidek 		warn("unable to contact casperd");
1804*49133c6dSPawel Jakub Dawidek 		return (NULL);
1805*49133c6dSPawel Jakub Dawidek 	}
1806*49133c6dSPawel Jakub Dawidek 	capdnsloc = cap_service_open(capcas, "system.dns");
1807*49133c6dSPawel Jakub Dawidek 	/* Casper capability no longer needed. */
1808*49133c6dSPawel Jakub Dawidek 	cap_close(capcas);
1809*49133c6dSPawel Jakub Dawidek 	if (capdnsloc == NULL)
1810*49133c6dSPawel Jakub Dawidek 		err(1, "unable to open system.dns service");
1811*49133c6dSPawel Jakub Dawidek 	types[0] = "NAME";
1812*49133c6dSPawel Jakub Dawidek 	types[1] = "ADDR";
1813*49133c6dSPawel Jakub Dawidek 	if (cap_dns_type_limit(capdnsloc, types, 2) < 0)
1814*49133c6dSPawel Jakub Dawidek 		err(1, "unable to limit access to system.dns service");
1815*49133c6dSPawel Jakub Dawidek 	families[0] = AF_INET;
1816*49133c6dSPawel Jakub Dawidek 	if (cap_dns_family_limit(capdnsloc, families, 1) < 0)
1817*49133c6dSPawel Jakub Dawidek 		err(1, "unable to limit access to system.dns service");
1818*49133c6dSPawel Jakub Dawidek 
1819*49133c6dSPawel Jakub Dawidek 	return (capdnsloc);
1820*49133c6dSPawel Jakub Dawidek }
1821*49133c6dSPawel Jakub Dawidek #endif /* HAVE_LIBCAPSICUM */
1822*49133c6dSPawel Jakub Dawidek 
1823120b4a93SRuslan Ermilov #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
1824120b4a93SRuslan Ermilov #define	SECOPT		" [-P policy]"
1825120b4a93SRuslan Ermilov #else
1826120b4a93SRuslan Ermilov #define	SECOPT		""
1827120b4a93SRuslan Ermilov #endif
182843470e3bSGarrett Wollman static void
1829fafb8f11SEd Schouten usage(void)
18308fae3551SRodney W. Grimes {
183131eac03bSSean Chittenden 
1832d6cd1497SGleb Smirnoff 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
1833ee3e1c4cSRuslan Ermilov "usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize] [-g sweepminsize]",
1834ee3e1c4cSRuslan Ermilov "            [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]",
1835ee3e1c4cSRuslan Ermilov "           " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]",
1836d6cd1497SGleb Smirnoff "            [-W waittime] [-z tos] host",
18371bd10ba2SRuslan Ermilov "       ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]",
1838120b4a93SRuslan Ermilov "            [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]",
1839d6cd1497SGleb Smirnoff "            [-s packetsize] [-T ttl] [-t timeout] [-W waittime]",
1840d6cd1497SGleb Smirnoff "            [-z tos] mcast-group");
184143470e3bSGarrett Wollman 	exit(EX_USAGE);
18428fae3551SRodney W. Grimes }
1843