xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/ping/ping_aux.c (revision 1254:b12a29236803)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*1254Smeem  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <strings.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <fcntl.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <sys/time.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/socket.h>
480Sstevel@tonic-gate #include <sys/sockio.h>
490Sstevel@tonic-gate #include <sys/stropts.h>
500Sstevel@tonic-gate #include <sys/file.h>
510Sstevel@tonic-gate #include <arpa/inet.h>
520Sstevel@tonic-gate #include <net/if.h>
530Sstevel@tonic-gate #include <netinet/in_systm.h>
540Sstevel@tonic-gate #include <netinet/in.h>
550Sstevel@tonic-gate #include <netinet/ip.h>
560Sstevel@tonic-gate #include <netinet/ip_icmp.h>
570Sstevel@tonic-gate #include <netinet/udp.h>
580Sstevel@tonic-gate #include <netdb.h>
590Sstevel@tonic-gate #include <stdlib.h>
600Sstevel@tonic-gate 
61*1254Smeem #include <libinetutil.h>
620Sstevel@tonic-gate #include "ping.h"
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * IPv4 source routing option.
670Sstevel@tonic-gate  * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
680Sstevel@tonic-gate  * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate struct ip_sourceroute {
710Sstevel@tonic-gate 	uint8_t ipsr_code;
720Sstevel@tonic-gate 	uint8_t ipsr_len;
730Sstevel@tonic-gate 	uint8_t ipsr_ptr;
740Sstevel@tonic-gate 	/* up to 9 IPv4 addresses */
750Sstevel@tonic-gate 	uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
760Sstevel@tonic-gate };
770Sstevel@tonic-gate 
780Sstevel@tonic-gate void check_reply(struct addrinfo *, struct msghdr *, int, ushort_t);
790Sstevel@tonic-gate extern void find_dstaddr(ushort_t, union any_in_addr *);
800Sstevel@tonic-gate extern boolean_t is_a_target(struct addrinfo *, union any_in_addr *);
810Sstevel@tonic-gate extern char *pr_name(char *, int);
820Sstevel@tonic-gate static void pr_options(uchar_t *, int);
830Sstevel@tonic-gate extern char *pr_protocol(int);
840Sstevel@tonic-gate static void pr_rropt(uchar_t *, int, boolean_t);
850Sstevel@tonic-gate static void pr_tsopt(uchar_t *, int);
860Sstevel@tonic-gate static char *pr_type(int);
870Sstevel@tonic-gate extern void schedule_sigalrm();
880Sstevel@tonic-gate extern void send_scheduled_probe();
890Sstevel@tonic-gate extern boolean_t seq_match(ushort_t, int, ushort_t);
900Sstevel@tonic-gate extern void sigalrm_handler();
910Sstevel@tonic-gate void set_IPv4_options(int, union any_in_addr *, int, struct in_addr *,
920Sstevel@tonic-gate     struct in_addr *);
930Sstevel@tonic-gate extern void tvsub(struct timeval *, struct timeval *);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * Set IPv4 options
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate void
set_IPv4_options(int sock,union any_in_addr * gw_IP_list,int gw_count,struct in_addr * src,struct in_addr * dst)990Sstevel@tonic-gate set_IPv4_options(int sock, union any_in_addr *gw_IP_list, int gw_count,
1000Sstevel@tonic-gate     struct in_addr *src, struct in_addr *dst)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	int req_size;
1030Sstevel@tonic-gate 	char srr[ROUTE_SIZE + 1];
1040Sstevel@tonic-gate 	char *bufp;
1050Sstevel@tonic-gate 	int optsize = ROUTE_SIZE;
1060Sstevel@tonic-gate 	struct ip_sourceroute *srp;
1070Sstevel@tonic-gate 	struct ip_timestamp *tsp;
1080Sstevel@tonic-gate 	int i;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	if (rr_option || ts_option || gw_count > 0) {
1110Sstevel@tonic-gate 		bzero(srr, sizeof (srr));
1120Sstevel@tonic-gate 		bufp = srr;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 		if (gw_count > 0) {
1150Sstevel@tonic-gate 			/* 3 = 1 (code) + 1 (len) + 1 (ptr) of src route opt. */
1160Sstevel@tonic-gate 			req_size = 3 + (sizeof (struct in_addr)) * gw_count;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 			if (optsize < req_size) {
1190Sstevel@tonic-gate 				Fprintf(stderr, "%s: too many IPv4 gateways\n",
1200Sstevel@tonic-gate 				    progname);
1210Sstevel@tonic-gate 				exit(EXIT_FAILURE);
1220Sstevel@tonic-gate 			}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 			srp = (struct ip_sourceroute *)bufp;
1250Sstevel@tonic-gate 			srp->ipsr_code = strict ? IPOPT_SSRR : IPOPT_LSRR;
1260Sstevel@tonic-gate 			srp->ipsr_len = req_size;
1270Sstevel@tonic-gate 			srp->ipsr_ptr = IPOPT_MINOFF;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 			for (i = 0; i < gw_count; i++) {
1300Sstevel@tonic-gate 				bcopy((char *)&gw_IP_list[i].addr,
1310Sstevel@tonic-gate 				    &srp->ipsr_addrs[i],
1320Sstevel@tonic-gate 				    sizeof (struct in_addr));
1330Sstevel@tonic-gate 			}
1340Sstevel@tonic-gate 			optsize -= srp->ipsr_len;
1350Sstevel@tonic-gate 			bufp += srp->ipsr_len;
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 		/* do we send a timestamp option? */
1380Sstevel@tonic-gate 		if (ts_option) {
1390Sstevel@tonic-gate 			if (optsize < IPOPT_MINOFF) {
1400Sstevel@tonic-gate 				Fprintf(stderr,
1410Sstevel@tonic-gate 				    "%s: no room for timestamp option\n",
1420Sstevel@tonic-gate 				    progname);
1430Sstevel@tonic-gate 				exit(EXIT_FAILURE);
1440Sstevel@tonic-gate 			}
1450Sstevel@tonic-gate 			/* LINTED */
1460Sstevel@tonic-gate 			tsp = (struct ip_timestamp *)bufp;
1470Sstevel@tonic-gate 			tsp->ipt_code = IPOPT_TS;
1480Sstevel@tonic-gate 			tsp->ipt_len = optsize;
1490Sstevel@tonic-gate 			tsp->ipt_ptr = IPOPT_MINOFF + 1;
1500Sstevel@tonic-gate 			tsp->ipt_flg = ts_flag & 0x0f;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 			if (tsp->ipt_flg > IPOPT_TS_TSANDADDR) {
1530Sstevel@tonic-gate 				req_size = IPOPT_MINOFF +
1540Sstevel@tonic-gate 				    2 * sizeof (struct ipt_ta);
1550Sstevel@tonic-gate 				/*
1560Sstevel@tonic-gate 				 * Note: BSD/4.X is broken in their check so we
1570Sstevel@tonic-gate 				 * have to  bump up this number by at least one.
1580Sstevel@tonic-gate 				 */
1590Sstevel@tonic-gate 				req_size++;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 				if (optsize < req_size) {
1620Sstevel@tonic-gate 					Fprintf(stderr, "%s: no room for "
1630Sstevel@tonic-gate 					    "timestamp option\n", progname);
1640Sstevel@tonic-gate 					exit(EXIT_FAILURE);
1650Sstevel@tonic-gate 				}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 				bcopy((char *)dst,
1680Sstevel@tonic-gate 				    &tsp->ipt_timestamp.ipt_ta[0].ipt_addr,
1690Sstevel@tonic-gate 				    sizeof (struct in_addr));
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 				bcopy((char *)src,
1720Sstevel@tonic-gate 				    &tsp->ipt_timestamp.ipt_ta[1].ipt_addr,
1730Sstevel@tonic-gate 				    sizeof (struct in_addr));
1740Sstevel@tonic-gate 				tsp->ipt_len = req_size;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 			}
1770Sstevel@tonic-gate 			optsize -= tsp->ipt_len;
1780Sstevel@tonic-gate 			bufp += tsp->ipt_len;
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 		/* do we send a record route option? */
1810Sstevel@tonic-gate 		if (rr_option) {
1820Sstevel@tonic-gate 			if (optsize < IPOPT_MINOFF) {
1830Sstevel@tonic-gate 				Fprintf(stderr,
1840Sstevel@tonic-gate 				    "%s: no room for record route option\n",
1850Sstevel@tonic-gate 				    progname);
1860Sstevel@tonic-gate 				exit(EXIT_FAILURE);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 			}
1890Sstevel@tonic-gate 			/*
1900Sstevel@tonic-gate 			 * Format of record route option is same as source
1910Sstevel@tonic-gate 			 * route option.
1920Sstevel@tonic-gate 			 */
1930Sstevel@tonic-gate 			srp = (struct ip_sourceroute *)bufp;
1940Sstevel@tonic-gate 			srp->ipsr_code = IPOPT_RR;
1950Sstevel@tonic-gate 			srp->ipsr_len = optsize;
1960Sstevel@tonic-gate 			srp->ipsr_ptr = IPOPT_MINOFF;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 			optsize -= srp->ipsr_len;
1990Sstevel@tonic-gate 			bufp += srp->ipsr_len;
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 		optsize = bufp - srr;
2020Sstevel@tonic-gate 		/* Round up to 4 byte boundary */
2030Sstevel@tonic-gate 		if (optsize & 0x3)
2040Sstevel@tonic-gate 			optsize = (optsize & ~0x3) + 4;
2050Sstevel@tonic-gate 		if (setsockopt(sock, IPPROTO_IP, IP_OPTIONS, srr, optsize) <
2060Sstevel@tonic-gate 		    0) {
2070Sstevel@tonic-gate 			Fprintf(stderr, "%s: setsockopt IP_OPTIONS %s\n",
2080Sstevel@tonic-gate 			    progname, strerror(errno));
2090Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * Check out the packet to see if it came from us.  This logic is necessary
2160Sstevel@tonic-gate  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
2170Sstevel@tonic-gate  * which arrive ('tis only fair).  This permits multiple copies of this
2180Sstevel@tonic-gate  * program to be run without having intermingled output (or statistics!).
2190Sstevel@tonic-gate  */
2200Sstevel@tonic-gate void
check_reply(struct addrinfo * ai_dst,struct msghdr * msg,int cc,ushort_t udp_src_port)2210Sstevel@tonic-gate check_reply(struct addrinfo *ai_dst, struct msghdr *msg, int cc,
2220Sstevel@tonic-gate     ushort_t udp_src_port)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate 	struct ip *ip;
2250Sstevel@tonic-gate 	struct icmp *icp;
2260Sstevel@tonic-gate 	struct udphdr *up;
2270Sstevel@tonic-gate 	union any_in_addr dst_addr;
2280Sstevel@tonic-gate 	uchar_t *buf;
2290Sstevel@tonic-gate 	int32_t *intp;
2300Sstevel@tonic-gate 	struct sockaddr_in *from;
2310Sstevel@tonic-gate 	struct timeval *tp;
2320Sstevel@tonic-gate 	struct timeval tv;
2330Sstevel@tonic-gate 	int hlen, hlen1;
2340Sstevel@tonic-gate 	int64_t triptime;
2350Sstevel@tonic-gate 	boolean_t valid_reply = _B_FALSE;
2360Sstevel@tonic-gate 	boolean_t reply_matched_current_target;	/* Is the source address of */
2370Sstevel@tonic-gate 						/* this reply same as where */
2380Sstevel@tonic-gate 						/* we're sending currently? */
2390Sstevel@tonic-gate 	boolean_t last_reply_from_targetaddr = _B_FALSE; /* Is this stats, */
2400Sstevel@tonic-gate 						/* probe all with npackets>0 */
2410Sstevel@tonic-gate 						/* and we received reply for */
2420Sstevel@tonic-gate 						/* the last probe sent to */
2430Sstevel@tonic-gate 						/* targetaddr */
2440Sstevel@tonic-gate 	int cc_left;
2450Sstevel@tonic-gate 	char tmp_buf[INET6_ADDRSTRLEN];
2460Sstevel@tonic-gate 	static char *unreach[] = {
2470Sstevel@tonic-gate 	    "Net Unreachable",
2480Sstevel@tonic-gate 	    "Host Unreachable",
2490Sstevel@tonic-gate 	    "Protocol Unreachable",
2500Sstevel@tonic-gate 	    "Port Unreachable",
2510Sstevel@tonic-gate 	    "Fragmentation needed and DF set",
2520Sstevel@tonic-gate 	    "Source Route Failed",
2530Sstevel@tonic-gate 	    /* The following are from RFC1700 */
2540Sstevel@tonic-gate 	    "Net Unknown",
2550Sstevel@tonic-gate 	    "Host Unknown",
2560Sstevel@tonic-gate 	    "Source Host Isolated",
2570Sstevel@tonic-gate 	    "Dest Net Prohibited",
2580Sstevel@tonic-gate 	    "Dest Host Prohibited",
2590Sstevel@tonic-gate 	    "Net Unreachable for TOS",
2600Sstevel@tonic-gate 	    "Host Unreachable for TOS",
2610Sstevel@tonic-gate 	    "Communication Administratively Prohibited",
2620Sstevel@tonic-gate 	    "Host Precedence Violation",
2630Sstevel@tonic-gate 	    "Precedence Cutoff in Effect"
2640Sstevel@tonic-gate 	};
2650Sstevel@tonic-gate 	static char *redirect[] = {
2660Sstevel@tonic-gate 	    "Net",
2670Sstevel@tonic-gate 	    "Host",
2680Sstevel@tonic-gate 	    "TOS Net",
2690Sstevel@tonic-gate 	    "TOS Host"
2700Sstevel@tonic-gate 	};
2710Sstevel@tonic-gate 	static char *timexceed[] = {
2720Sstevel@tonic-gate 	    "Time exceeded in transit",
2730Sstevel@tonic-gate 	    "Time exceeded during reassembly"
2740Sstevel@tonic-gate 	};
2750Sstevel@tonic-gate 	boolean_t print_newline = _B_FALSE;
2760Sstevel@tonic-gate 	int i;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* decompose msghdr into useful pieces */
2790Sstevel@tonic-gate 	buf = (uchar_t *)msg->msg_iov->iov_base;
2800Sstevel@tonic-gate 	from = (struct sockaddr_in *)msg->msg_name;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/* LINTED */
2830Sstevel@tonic-gate 	intp = (int32_t *)buf;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	(void) gettimeofday(&tv, (struct timezone *)NULL);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/* LINTED */
2880Sstevel@tonic-gate 	ip = (struct ip *)buf;
2890Sstevel@tonic-gate 	hlen = ip->ip_hl << 2;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if ((cc < sizeof (struct ip)) || (cc < hlen + ICMP_MINLEN)) {
2920Sstevel@tonic-gate 		if (verbose) {
2930Sstevel@tonic-gate 			Printf("packet too short (%d bytes) from %s\n", cc,
2940Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
2950Sstevel@tonic-gate 		}
2960Sstevel@tonic-gate 		return;
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	cc -= hlen;
3000Sstevel@tonic-gate 	/* LINTED */
3010Sstevel@tonic-gate 	icp = (struct icmp *)(buf + hlen);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (ip->ip_p == 0) {
3040Sstevel@tonic-gate 		/*
3050Sstevel@tonic-gate 		 * Assume that we are running on a pre-4.3BSD system
3060Sstevel@tonic-gate 		 * such as SunOS before 4.0
3070Sstevel@tonic-gate 		 */
3080Sstevel@tonic-gate 		/* LINTED */
3090Sstevel@tonic-gate 		icp = (struct icmp *)buf;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 	cc_left = cc - ICMP_MINLEN;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	switch (icp->icmp_type) {
3140Sstevel@tonic-gate 	case ICMP_UNREACH:
3150Sstevel@tonic-gate 		ip = &icp->icmp_ip;
3160Sstevel@tonic-gate 		hlen1 = ip->ip_hl << 2;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		/* check if we have enough of the packet to work on */
3190Sstevel@tonic-gate 		if ((cc_left < sizeof (struct ip)) ||
3200Sstevel@tonic-gate 		    (cc_left < hlen1 + sizeof (struct udphdr))) {
3210Sstevel@tonic-gate 			if (verbose) {
3220Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
3230Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
3240Sstevel@tonic-gate 				    AF_INET));
3250Sstevel@tonic-gate 			}
3260Sstevel@tonic-gate 			return;
3270Sstevel@tonic-gate 		}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 		/* get the UDP packet */
3300Sstevel@tonic-gate 		cc_left -= hlen1 + sizeof (struct udphdr);
3310Sstevel@tonic-gate 		/* LINTED */
3320Sstevel@tonic-gate 		up = (struct udphdr *)((uchar_t *)ip + hlen1);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		/* check to see if this is what we sent */
3350Sstevel@tonic-gate 		if (icp->icmp_code == ICMP_UNREACH_PORT &&
3360Sstevel@tonic-gate 		    ip->ip_p == IPPROTO_UDP &&
3370Sstevel@tonic-gate 		    udp_src_port == up->uh_sport &&
3380Sstevel@tonic-gate 		    use_udp) {
3390Sstevel@tonic-gate 			valid_reply = _B_TRUE;
3400Sstevel@tonic-gate 		} else {
3410Sstevel@tonic-gate 			valid_reply = _B_FALSE;
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		if (valid_reply) {
3450Sstevel@tonic-gate 			/*
3460Sstevel@tonic-gate 			 * For this valid reply, if we are still sending to
3470Sstevel@tonic-gate 			 * this target IP address, we'd like to do some
3480Sstevel@tonic-gate 			 * updates to targetaddr, so hold SIGALRMs.
3490Sstevel@tonic-gate 			 */
3500Sstevel@tonic-gate 			(void) sighold(SIGALRM);
3510Sstevel@tonic-gate 			is_alive = _B_TRUE;
3520Sstevel@tonic-gate 			nreceived++;
3530Sstevel@tonic-gate 			reply_matched_current_target =
3540Sstevel@tonic-gate 			    seq_match(current_targetaddr->starting_seq_num,
3550Sstevel@tonic-gate 				current_targetaddr->num_sent,
3560Sstevel@tonic-gate 				ntohs(up->uh_dport));
3570Sstevel@tonic-gate 			if (reply_matched_current_target) {
3580Sstevel@tonic-gate 				current_targetaddr->got_reply = _B_TRUE;
3590Sstevel@tonic-gate 				nreceived_last_target++;
3600Sstevel@tonic-gate 				/*
3610Sstevel@tonic-gate 				 * Determine if stats, probe-all, and
3620Sstevel@tonic-gate 				 * npackets != 0, and this is the reply for
3630Sstevel@tonic-gate 				 * the last probe we sent to current target
3640Sstevel@tonic-gate 				 * address.
3650Sstevel@tonic-gate 				 */
3660Sstevel@tonic-gate 				if (stats && probe_all && npackets > 0 &&
3670Sstevel@tonic-gate 				    ((current_targetaddr->starting_seq_num +
3680Sstevel@tonic-gate 				    current_targetaddr->num_probes - 1) %
3690Sstevel@tonic-gate 				    (MAX_PORT + 1) == ntohs(up->uh_dport)) &&
3700Sstevel@tonic-gate 				    (current_targetaddr->num_probes ==
3710Sstevel@tonic-gate 				    current_targetaddr->num_sent))
3720Sstevel@tonic-gate 					last_reply_from_targetaddr = _B_TRUE;
3730Sstevel@tonic-gate 			} else {
3740Sstevel@tonic-gate 				/*
3750Sstevel@tonic-gate 				 * If it's just probe_all and we just received
3760Sstevel@tonic-gate 				 * a reply from a target address we were
3770Sstevel@tonic-gate 				 * probing and had timed out (now we are probing
3780Sstevel@tonic-gate 				 * some other target address), we ignore
3790Sstevel@tonic-gate 				 * this reply.
3800Sstevel@tonic-gate 				 */
3810Sstevel@tonic-gate 				if (probe_all && !stats) {
3820Sstevel@tonic-gate 					valid_reply = _B_FALSE;
3830Sstevel@tonic-gate 					/*
3840Sstevel@tonic-gate 					 * Only if it's verbose, we get a
3850Sstevel@tonic-gate 					 * message regarding this reply,
3860Sstevel@tonic-gate 					 * otherwise we are done here.
3870Sstevel@tonic-gate 					 */
3880Sstevel@tonic-gate 					if (!verbose) {
3890Sstevel@tonic-gate 						(void) sigrelse(SIGALRM);
3900Sstevel@tonic-gate 						return;
3910Sstevel@tonic-gate 					}
3920Sstevel@tonic-gate 				}
3930Sstevel@tonic-gate 			}
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		/* stats mode doesn't print 'alive' messages */
3970Sstevel@tonic-gate 		if (valid_reply && !stats) {
3980Sstevel@tonic-gate 			/*
3990Sstevel@tonic-gate 			 * if we are still sending to the same target address,
4000Sstevel@tonic-gate 			 * then stop it, because we know it's alive.
4010Sstevel@tonic-gate 			 */
4020Sstevel@tonic-gate 			if (reply_matched_current_target) {
4030Sstevel@tonic-gate 				(void) alarm(0);	/* cancel alarm */
4040Sstevel@tonic-gate 				(void) sigset(SIGALRM, SIG_IGN);
4050Sstevel@tonic-gate 				current_targetaddr->probing_done = _B_TRUE;
4060Sstevel@tonic-gate 			}
4070Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 			if (!probe_all) {
4100Sstevel@tonic-gate 				Printf("%s is alive\n", targethost);
4110Sstevel@tonic-gate 			} else {
4120Sstevel@tonic-gate 				(void) inet_ntop(AF_INET, (void *)&ip->ip_dst,
4130Sstevel@tonic-gate 				    tmp_buf, sizeof (tmp_buf));
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 				if (nflag) {
4160Sstevel@tonic-gate 					Printf("%s is alive\n", tmp_buf);
4170Sstevel@tonic-gate 				} else {
4180Sstevel@tonic-gate 					Printf("%s (%s) is alive\n",
4190Sstevel@tonic-gate 					    targethost, tmp_buf);
4200Sstevel@tonic-gate 				}
4210Sstevel@tonic-gate 			}
4220Sstevel@tonic-gate 			if (reply_matched_current_target) {
4230Sstevel@tonic-gate 				/*
4240Sstevel@tonic-gate 				 * Let's get things going again, but now
4250Sstevel@tonic-gate 				 * ping will start sending to next target IP
4260Sstevel@tonic-gate 				 * address.
4270Sstevel@tonic-gate 				 */
4280Sstevel@tonic-gate 				send_scheduled_probe();
4290Sstevel@tonic-gate 				(void) sigset(SIGALRM, sigalrm_handler);
4300Sstevel@tonic-gate 				schedule_sigalrm();
4310Sstevel@tonic-gate 			}
4320Sstevel@tonic-gate 			return;
4330Sstevel@tonic-gate 		} else {
4340Sstevel@tonic-gate 			/*
4350Sstevel@tonic-gate 			 * If we are not moving to next targetaddr, let's
4360Sstevel@tonic-gate 			 * release the SIGALRM now. We don't want to stall in
4370Sstevel@tonic-gate 			 * the middle of probing a targetaddr if the pr_name()
4380Sstevel@tonic-gate 			 * call (see below) takes longer.
4390Sstevel@tonic-gate 			 */
4400Sstevel@tonic-gate 			if (!last_reply_from_targetaddr)
4410Sstevel@tonic-gate 				(void) sigrelse(SIGALRM);
4420Sstevel@tonic-gate 			/* else, we'll release it later */
4430Sstevel@tonic-gate 		}
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
4460Sstevel@tonic-gate 		if (valid_reply) {
4470Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
4480Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
4490Sstevel@tonic-gate 			Printf("udp_port=%d. ", ntohs(up->uh_dport));
4500Sstevel@tonic-gate 			print_newline = _B_TRUE;
4510Sstevel@tonic-gate 		} else if (is_a_target(ai_dst, &dst_addr) || verbose) {
4520Sstevel@tonic-gate 			if (icp->icmp_code >= A_CNT(unreach)) {
4530Sstevel@tonic-gate 				Printf("ICMP %d Unreachable from gateway %s\n",
4540Sstevel@tonic-gate 				    icp->icmp_code,
4550Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
4560Sstevel@tonic-gate 			} else {
4570Sstevel@tonic-gate 				Printf("ICMP %s from gateway %s\n",
4580Sstevel@tonic-gate 				    unreach[icp->icmp_code],
4590Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
4600Sstevel@tonic-gate 			}
4610Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(ip->ip_p),
4620Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_src, AF_INET));
4630Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
4640Sstevel@tonic-gate 			if (ip->ip_p == IPPROTO_TCP ||
4650Sstevel@tonic-gate 			    ip->ip_p == IPPROTO_UDP) {
4660Sstevel@tonic-gate 				Printf(" port %d ", ntohs(up->uh_dport));
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 			print_newline = _B_TRUE;
4690Sstevel@tonic-gate 		}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		/* if we are timing and the reply has a timeval */
4720Sstevel@tonic-gate 		if (valid_reply && datalen >= sizeof (struct timeval) &&
4730Sstevel@tonic-gate 		    cc_left >= sizeof (struct timeval)) {
4740Sstevel@tonic-gate 			/* LINTED */
4750Sstevel@tonic-gate 			tp = (struct timeval *)((char *)up +
4760Sstevel@tonic-gate 			    sizeof (struct udphdr));
4770Sstevel@tonic-gate 			(void) tvsub(&tv, tp);
4780Sstevel@tonic-gate 			triptime = (int64_t)tv.tv_sec * MICROSEC + tv.tv_usec;
4790Sstevel@tonic-gate 			Printf("time=" TIMEFORMAT " ms", triptime/1000.0);
4800Sstevel@tonic-gate 			tsum += triptime;
4810Sstevel@tonic-gate 			tsum2 += triptime*triptime;
4820Sstevel@tonic-gate 			if (triptime < tmin)
4830Sstevel@tonic-gate 				tmin = triptime;
4840Sstevel@tonic-gate 			if (triptime > tmax)
4850Sstevel@tonic-gate 				tmax = triptime;
4860Sstevel@tonic-gate 			print_newline = _B_TRUE;
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 		if (print_newline)
4890Sstevel@tonic-gate 			(void) putchar('\n');
4900Sstevel@tonic-gate 		/*
4910Sstevel@tonic-gate 		 * If it's stats, probe-all, npackets > 0, and we received reply
4920Sstevel@tonic-gate 		 * for the last probe sent to this target address, then we
4930Sstevel@tonic-gate 		 * don't need to wait anymore, let's move on to next target
4940Sstevel@tonic-gate 		 * address, now!
4950Sstevel@tonic-gate 		 */
4960Sstevel@tonic-gate 		if (last_reply_from_targetaddr) {
4970Sstevel@tonic-gate 			(void) alarm(0);	/* cancel alarm */
4980Sstevel@tonic-gate 			current_targetaddr->probing_done = _B_TRUE;
4990Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
5000Sstevel@tonic-gate 			send_scheduled_probe();
5010Sstevel@tonic-gate 			schedule_sigalrm();
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 		break;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	case ICMP_REDIRECT:
5060Sstevel@tonic-gate 		if (cc_left < sizeof (struct ip)) {
5070Sstevel@tonic-gate 			if (verbose) {
5080Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
5090Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
5100Sstevel@tonic-gate 				    AF_INET));
5110Sstevel@tonic-gate 			}
5120Sstevel@tonic-gate 			return;
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 		ip = &icp->icmp_ip;
5160Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
5170Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
5180Sstevel@tonic-gate 			if (icp->icmp_code >= A_CNT(redirect)) {
5190Sstevel@tonic-gate 				Printf("ICMP %d redirect from gateway %s\n",
5200Sstevel@tonic-gate 				    icp->icmp_code,
5210Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
5220Sstevel@tonic-gate 			} else {
5230Sstevel@tonic-gate 				Printf("ICMP %s redirect from gateway %s\n",
5240Sstevel@tonic-gate 				    redirect[icp->icmp_code],
5250Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
5260Sstevel@tonic-gate 			}
5270Sstevel@tonic-gate 			Printf(" to %s",
5280Sstevel@tonic-gate 			    pr_name((char *)&icp->icmp_gwaddr, AF_INET));
5290Sstevel@tonic-gate 			Printf(" for %s\n",
5300Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_dst, AF_INET));
5310Sstevel@tonic-gate 		}
5320Sstevel@tonic-gate 		break;
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	case ICMP_ECHOREPLY:
5350Sstevel@tonic-gate 		if (ntohs(icp->icmp_id) == ident) {
5360Sstevel@tonic-gate 			if (!use_udp && !use_icmp_ts)
5370Sstevel@tonic-gate 				valid_reply = _B_TRUE;
5380Sstevel@tonic-gate 			else
5390Sstevel@tonic-gate 				valid_reply = _B_FALSE;
5400Sstevel@tonic-gate 		} else {
5410Sstevel@tonic-gate 			return;
5420Sstevel@tonic-gate 		}
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 		if (valid_reply) {
5450Sstevel@tonic-gate 			/*
5460Sstevel@tonic-gate 			 * For this valid reply, if we are still sending to
5470Sstevel@tonic-gate 			 * this target IP address, we'd like to do some
5480Sstevel@tonic-gate 			 * updates to targetaddr, so hold SIGALRMs.
5490Sstevel@tonic-gate 			 */
5500Sstevel@tonic-gate 			(void) sighold(SIGALRM);
5510Sstevel@tonic-gate 			is_alive = _B_TRUE;
5520Sstevel@tonic-gate 			nreceived++;
5530Sstevel@tonic-gate 			reply_matched_current_target =
5540Sstevel@tonic-gate 			    seq_match(current_targetaddr->starting_seq_num,
5550Sstevel@tonic-gate 				current_targetaddr->num_sent,
5560Sstevel@tonic-gate 				ntohs(icp->icmp_seq));
5570Sstevel@tonic-gate 			if (reply_matched_current_target) {
5580Sstevel@tonic-gate 				current_targetaddr->got_reply = _B_TRUE;
5590Sstevel@tonic-gate 				nreceived_last_target++;
5600Sstevel@tonic-gate 				/*
5610Sstevel@tonic-gate 				 * Determine if stats, probe-all, and
5620Sstevel@tonic-gate 				 * npackets != 0, and this is the reply for
5630Sstevel@tonic-gate 				 * the last probe we sent to current target
5640Sstevel@tonic-gate 				 * address.
5650Sstevel@tonic-gate 				 */
5660Sstevel@tonic-gate 				if (stats && probe_all && npackets > 0 &&
5670Sstevel@tonic-gate 				    ((current_targetaddr->starting_seq_num +
5680Sstevel@tonic-gate 				    current_targetaddr->num_probes - 1) %
5690Sstevel@tonic-gate 				    (MAX_ICMP_SEQ + 1) ==
5700Sstevel@tonic-gate 				    ntohs(icp->icmp_seq)) &&
5710Sstevel@tonic-gate 				    (current_targetaddr->num_probes ==
5720Sstevel@tonic-gate 				    current_targetaddr->num_sent))
5730Sstevel@tonic-gate 					last_reply_from_targetaddr = _B_TRUE;
5740Sstevel@tonic-gate 			} else {
5750Sstevel@tonic-gate 				/*
5760Sstevel@tonic-gate 				 * If it's just probe_all and we just received
5770Sstevel@tonic-gate 				 * a reply from a target address we were
5780Sstevel@tonic-gate 				 * probing and had timed out (now we are probing
5790Sstevel@tonic-gate 				 * some other target address), we ignore
5800Sstevel@tonic-gate 				 * this reply.
5810Sstevel@tonic-gate 				 */
5820Sstevel@tonic-gate 				if (probe_all && !stats) {
5830Sstevel@tonic-gate 					valid_reply = _B_FALSE;
5840Sstevel@tonic-gate 					/*
5850Sstevel@tonic-gate 					 * Only if it's verbose, we get a
5860Sstevel@tonic-gate 					 * message regarding this reply,
5870Sstevel@tonic-gate 					 * otherwise we are done here.
5880Sstevel@tonic-gate 					 */
5890Sstevel@tonic-gate 					if (!verbose) {
5900Sstevel@tonic-gate 						(void) sigrelse(SIGALRM);
5910Sstevel@tonic-gate 						return;
5920Sstevel@tonic-gate 					}
5930Sstevel@tonic-gate 				}
5940Sstevel@tonic-gate 			}
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		if (!stats && valid_reply) {
5980Sstevel@tonic-gate 			/*
5990Sstevel@tonic-gate 			 * if we are still sending to the same target address,
6000Sstevel@tonic-gate 			 * then stop it, because we know it's alive.
6010Sstevel@tonic-gate 			 */
6020Sstevel@tonic-gate 			if (reply_matched_current_target) {
6030Sstevel@tonic-gate 				(void) alarm(0);	/* cancel alarm */
6040Sstevel@tonic-gate 				(void) sigset(SIGALRM, SIG_IGN);
6050Sstevel@tonic-gate 				current_targetaddr->probing_done = _B_TRUE;
6060Sstevel@tonic-gate 			}
6070Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 			if (!probe_all) {
6100Sstevel@tonic-gate 				Printf("%s is alive\n", targethost);
6110Sstevel@tonic-gate 			} else {
6120Sstevel@tonic-gate 				/*
6130Sstevel@tonic-gate 				 * If we are using send_reply, the real
6140Sstevel@tonic-gate 				 * target address is not the src address of the
6150Sstevel@tonic-gate 				 * replies. Use icmp_seq to find out where this
6160Sstevel@tonic-gate 				 * probe was sent to.
6170Sstevel@tonic-gate 				 */
6180Sstevel@tonic-gate 				if (send_reply) {
6190Sstevel@tonic-gate 					(void) find_dstaddr(
6200Sstevel@tonic-gate 					    ntohs(icp->icmp_seq), &dst_addr);
6210Sstevel@tonic-gate 					(void) inet_ntop(AF_INET,
6220Sstevel@tonic-gate 					    (void *)&dst_addr.addr,
6230Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
6240Sstevel@tonic-gate 				} else {
6250Sstevel@tonic-gate 					(void) inet_ntop(AF_INET,
6260Sstevel@tonic-gate 					    (void *)&from->sin_addr,
6270Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
6280Sstevel@tonic-gate 				}
6290Sstevel@tonic-gate 				if (nflag) {
6300Sstevel@tonic-gate 					Printf("%s is alive\n", tmp_buf);
6310Sstevel@tonic-gate 				} else {
6320Sstevel@tonic-gate 					Printf("%s (%s) is alive\n",
6330Sstevel@tonic-gate 					    targethost, tmp_buf);
6340Sstevel@tonic-gate 				}
6350Sstevel@tonic-gate 			}
6360Sstevel@tonic-gate 			if (reply_matched_current_target) {
6370Sstevel@tonic-gate 				/*
6380Sstevel@tonic-gate 				 * Let's get things going again, but now
6390Sstevel@tonic-gate 				 * ping will start sending to next target IP
6400Sstevel@tonic-gate 				 * address.
6410Sstevel@tonic-gate 				 */
6420Sstevel@tonic-gate 				send_scheduled_probe();
6430Sstevel@tonic-gate 				(void) sigset(SIGALRM, sigalrm_handler);
6440Sstevel@tonic-gate 				schedule_sigalrm();
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 			return;
6470Sstevel@tonic-gate 		} else {
6480Sstevel@tonic-gate 			/*
6490Sstevel@tonic-gate 			 * If we are not moving to next targetaddr, let's
6500Sstevel@tonic-gate 			 * release the SIGALRM now. We don't want to stall in
6510Sstevel@tonic-gate 			 * the middle of probing a targetaddr if the pr_name()
6520Sstevel@tonic-gate 			 * call (see below) takes longer.
6530Sstevel@tonic-gate 			 */
6540Sstevel@tonic-gate 			if (!last_reply_from_targetaddr)
6550Sstevel@tonic-gate 				(void) sigrelse(SIGALRM);
6560Sstevel@tonic-gate 			/* else, we'll release it later */
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		/*
6590Sstevel@tonic-gate 		 * If we are using send_reply, the real target address is
6600Sstevel@tonic-gate 		 * not the src address of the replies. Use icmp_seq to find out
6610Sstevel@tonic-gate 		 * where this probe was sent to.
6620Sstevel@tonic-gate 		 */
6630Sstevel@tonic-gate 		if (send_reply) {
6640Sstevel@tonic-gate 			(void) find_dstaddr(ntohs(icp->icmp_seq), &dst_addr);
6650Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
6660Sstevel@tonic-gate 			    pr_name((char *)&dst_addr.addr,  AF_INET));
6670Sstevel@tonic-gate 		} else {
6680Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
6690Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 		Printf("icmp_seq=%d. ", ntohs(icp->icmp_seq));
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		if (valid_reply && datalen >= sizeof (struct timeval) &&
6740Sstevel@tonic-gate 		    cc_left >= sizeof (struct timeval)) {
6750Sstevel@tonic-gate 			/* LINTED */
6760Sstevel@tonic-gate 			tp = (struct timeval *)&icp->icmp_data[0];
6770Sstevel@tonic-gate 			(void) tvsub(&tv, tp);
6780Sstevel@tonic-gate 			triptime = (int64_t)tv.tv_sec * MICROSEC + tv.tv_usec;
6790Sstevel@tonic-gate 			Printf("time=" TIMEFORMAT " ms", triptime/1000.0);
6800Sstevel@tonic-gate 			tsum += triptime;
6810Sstevel@tonic-gate 			tsum2 += triptime*triptime;
6820Sstevel@tonic-gate 			if (triptime < tmin)
6830Sstevel@tonic-gate 				tmin = triptime;
6840Sstevel@tonic-gate 			if (triptime > tmax)
6850Sstevel@tonic-gate 				tmax = triptime;
6860Sstevel@tonic-gate 		}
6870Sstevel@tonic-gate 		(void) putchar('\n');
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 		/*
6900Sstevel@tonic-gate 		 * If it's stats, probe-all, npackets > 0, and we received reply
6910Sstevel@tonic-gate 		 * for the last probe sent to this target address, then we
6920Sstevel@tonic-gate 		 * don't need to wait anymore, let's move on to next target
6930Sstevel@tonic-gate 		 * address, now!
6940Sstevel@tonic-gate 		 */
6950Sstevel@tonic-gate 		if (last_reply_from_targetaddr) {
6960Sstevel@tonic-gate 			(void) alarm(0);	/* cancel alarm */
6970Sstevel@tonic-gate 			current_targetaddr->probing_done = _B_TRUE;
6980Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
6990Sstevel@tonic-gate 			send_scheduled_probe();
7000Sstevel@tonic-gate 			schedule_sigalrm();
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 		break;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	case ICMP_SOURCEQUENCH:
7050Sstevel@tonic-gate 		if (cc_left < sizeof (struct ip)) {
7060Sstevel@tonic-gate 			if (verbose) {
7070Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
7080Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
7090Sstevel@tonic-gate 				    AF_INET));
7100Sstevel@tonic-gate 			}
7110Sstevel@tonic-gate 			return;
7120Sstevel@tonic-gate 		}
7130Sstevel@tonic-gate 		ip = &icp->icmp_ip;
7140Sstevel@tonic-gate 		hlen1 = ip->ip_hl << 2;
7150Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
7160Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
7170Sstevel@tonic-gate 			Printf("ICMP Source Quench from %s\n",
7180Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
7190Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(ip->ip_p),
7200Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_src, AF_INET));
7210Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 			/*
7240Sstevel@tonic-gate 			 * if it's a UDP or TCP packet, we need at least first
7250Sstevel@tonic-gate 			 * 4 bytes of it to see the src/dst ports
7260Sstevel@tonic-gate 			 */
7270Sstevel@tonic-gate 			if ((ip->ip_p == IPPROTO_TCP ||
7280Sstevel@tonic-gate 			    ip->ip_p == IPPROTO_UDP) &&
7290Sstevel@tonic-gate 			    (cc_left >= hlen1 + 4)) {
7300Sstevel@tonic-gate 				/* LINTED */
7310Sstevel@tonic-gate 				up = (struct udphdr *)((uchar_t *)ip + hlen1);
7320Sstevel@tonic-gate 				Printf(" port %d", ntohs(up->uh_dport));
7330Sstevel@tonic-gate 			}
7340Sstevel@tonic-gate 			(void) putchar('\n');
7350Sstevel@tonic-gate 		}
7360Sstevel@tonic-gate 		break;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	case ICMP_PARAMPROB:
7390Sstevel@tonic-gate 		if (cc_left < sizeof (struct ip)) {
7400Sstevel@tonic-gate 			if (verbose) {
7410Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
7420Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
7430Sstevel@tonic-gate 				    AF_INET));
7440Sstevel@tonic-gate 			}
7450Sstevel@tonic-gate 			return;
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 		ip = &icp->icmp_ip;
7480Sstevel@tonic-gate 		hlen1 = ip->ip_hl << 2;
7490Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
7500Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
7510Sstevel@tonic-gate 			switch (icp->icmp_code) {
7520Sstevel@tonic-gate 			case ICMP_PARAMPROB_OPTABSENT:
7530Sstevel@tonic-gate 				Printf("ICMP Missing a Required Option "
7540Sstevel@tonic-gate 				    "parameter problem from %s\n",
7550Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
7560Sstevel@tonic-gate 				Printf(" option type = %d", icp->icmp_pptr);
7570Sstevel@tonic-gate 				break;
7580Sstevel@tonic-gate 			case ICMP_PARAMPROB_BADLENGTH:
7590Sstevel@tonic-gate 				Printf("ICMP Bad Length parameter problem "
7600Sstevel@tonic-gate 				    "from %s\n",
7610Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
7620Sstevel@tonic-gate 				Printf(" in byte %d", icp->icmp_pptr);
7630Sstevel@tonic-gate 				if (icp->icmp_pptr <= hlen1) {
7640Sstevel@tonic-gate 					Printf(" (value 0x%x)",
7650Sstevel@tonic-gate 					    *((char *)ip + icp->icmp_pptr));
7660Sstevel@tonic-gate 				}
7670Sstevel@tonic-gate 				break;
7680Sstevel@tonic-gate 			case 0:
7690Sstevel@tonic-gate 			default:
7700Sstevel@tonic-gate 				Printf("ICMP Parameter Problem from %s\n",
7710Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
7720Sstevel@tonic-gate 				Printf(" in byte %d", icp->icmp_pptr);
7730Sstevel@tonic-gate 				if (icp->icmp_pptr <= hlen1) {
7740Sstevel@tonic-gate 					Printf(" (value 0x%x)",
7750Sstevel@tonic-gate 					    *((char *)ip + icp->icmp_pptr));
7760Sstevel@tonic-gate 				}
7770Sstevel@tonic-gate 				break;
7780Sstevel@tonic-gate 			}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(ip->ip_p),
7810Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_src, AF_INET));
7820Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 			/*
7850Sstevel@tonic-gate 			 * if it's a UDP or TCP packet, we need at least first
7860Sstevel@tonic-gate 			 * 4 bytes of it to see the src/dst ports
7870Sstevel@tonic-gate 			 */
7880Sstevel@tonic-gate 			if ((ip->ip_p == IPPROTO_TCP ||
7890Sstevel@tonic-gate 			    ip->ip_p == IPPROTO_UDP) &&
7900Sstevel@tonic-gate 			    (cc_left >= hlen1 + 4)) {
7910Sstevel@tonic-gate 				/* LINTED */
7920Sstevel@tonic-gate 				up = (struct udphdr *)((uchar_t *)ip + hlen1);
7930Sstevel@tonic-gate 				Printf(" port %d", ntohs(up->uh_dport));
7940Sstevel@tonic-gate 			}
7950Sstevel@tonic-gate 			(void) putchar('\n');
7960Sstevel@tonic-gate 		}
7970Sstevel@tonic-gate 		break;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	case ICMP_TIMXCEED:
8000Sstevel@tonic-gate 		if (cc_left < sizeof (struct ip)) {
8010Sstevel@tonic-gate 			if (verbose) {
8020Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
8030Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
8040Sstevel@tonic-gate 				    AF_INET));
8050Sstevel@tonic-gate 			}
8060Sstevel@tonic-gate 			return;
8070Sstevel@tonic-gate 		}
8080Sstevel@tonic-gate 		ip = &icp->icmp_ip;
8090Sstevel@tonic-gate 		hlen1 = ip->ip_hl << 2;
8100Sstevel@tonic-gate 		dst_addr.addr = ip->ip_dst;
8110Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
8120Sstevel@tonic-gate 			if (icp->icmp_code >= A_CNT(timexceed)) {
8130Sstevel@tonic-gate 				Printf("ICMP %d time exceeded from %s\n",
8140Sstevel@tonic-gate 				    icp->icmp_code,
8150Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
8160Sstevel@tonic-gate 			} else {
8170Sstevel@tonic-gate 				Printf("ICMP %s from %s\n",
8180Sstevel@tonic-gate 				    timexceed[icp->icmp_code],
8190Sstevel@tonic-gate 				    pr_name((char *)&from->sin_addr, AF_INET));
8200Sstevel@tonic-gate 			}
8210Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(ip->ip_p),
8220Sstevel@tonic-gate 			    pr_name((char *)&ip->ip_src, AF_INET));
8230Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
8240Sstevel@tonic-gate 			if ((ip->ip_p == IPPROTO_TCP ||
8250Sstevel@tonic-gate 			    ip->ip_p == IPPROTO_UDP) &&
8260Sstevel@tonic-gate 			    (cc_left >= hlen1 + 4)) {
8270Sstevel@tonic-gate 				/* LINTED */
8280Sstevel@tonic-gate 				up = (struct udphdr *)((uchar_t *)ip + hlen1);
8290Sstevel@tonic-gate 				Printf(" port %d", ntohs(up->uh_dport));
8300Sstevel@tonic-gate 			}
8310Sstevel@tonic-gate 			(void) putchar('\n');
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 		break;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	case ICMP_TSTAMPREPLY:
8360Sstevel@tonic-gate 		/* the packet should have enough space to store timestamps */
8370Sstevel@tonic-gate 		if (cc_left < sizeof (struct id_ts)) {
8380Sstevel@tonic-gate 			if (verbose) {
8390Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
8400Sstevel@tonic-gate 				    cc, pr_name((char *)&from->sin_addr,
8410Sstevel@tonic-gate 				    AF_INET));
8420Sstevel@tonic-gate 			}
8430Sstevel@tonic-gate 			return;
8440Sstevel@tonic-gate 		}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		if (ntohs(icp->icmp_id) == ident) {
8470Sstevel@tonic-gate 			if (use_icmp_ts)
8480Sstevel@tonic-gate 				valid_reply = _B_TRUE;
8490Sstevel@tonic-gate 			else
8500Sstevel@tonic-gate 				valid_reply = _B_FALSE;
8510Sstevel@tonic-gate 		} else {
8520Sstevel@tonic-gate 			return;
8530Sstevel@tonic-gate 		}
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		if (valid_reply) {
8560Sstevel@tonic-gate 			/*
8570Sstevel@tonic-gate 			 * For this valid reply, if we are still sending to
8580Sstevel@tonic-gate 			 * this target IP address, we'd like to do some
8590Sstevel@tonic-gate 			 * updates to targetaddr, so hold SIGALRMs.
8600Sstevel@tonic-gate 			 */
8610Sstevel@tonic-gate 			(void) sighold(SIGALRM);
8620Sstevel@tonic-gate 			is_alive = _B_TRUE;
8630Sstevel@tonic-gate 			nreceived++;
8640Sstevel@tonic-gate 			reply_matched_current_target =
8650Sstevel@tonic-gate 			    seq_match(current_targetaddr->starting_seq_num,
8660Sstevel@tonic-gate 				current_targetaddr->num_sent,
8670Sstevel@tonic-gate 				ntohs(icp->icmp_seq));
8680Sstevel@tonic-gate 			if (reply_matched_current_target) {
8690Sstevel@tonic-gate 				current_targetaddr->got_reply = _B_TRUE;
8700Sstevel@tonic-gate 				nreceived_last_target++;
8710Sstevel@tonic-gate 				/*
8720Sstevel@tonic-gate 				 * Determine if stats, probe-all, and
8730Sstevel@tonic-gate 				 * npackets != 0, and this is the reply for
8740Sstevel@tonic-gate 				 * the last probe we sent to current target
8750Sstevel@tonic-gate 				 * address.
8760Sstevel@tonic-gate 				 */
8770Sstevel@tonic-gate 				if (stats && probe_all && npackets > 0 &&
8780Sstevel@tonic-gate 				    ((current_targetaddr->starting_seq_num +
8790Sstevel@tonic-gate 				    current_targetaddr->num_probes - 1) %
8800Sstevel@tonic-gate 				    (MAX_ICMP_SEQ + 1) ==
8810Sstevel@tonic-gate 				    ntohs(icp->icmp_seq)) &&
8820Sstevel@tonic-gate 				    (current_targetaddr->num_probes ==
8830Sstevel@tonic-gate 				    current_targetaddr->num_sent))
8840Sstevel@tonic-gate 					last_reply_from_targetaddr = _B_TRUE;
8850Sstevel@tonic-gate 			} else {
8860Sstevel@tonic-gate 				/*
8870Sstevel@tonic-gate 				 * If it's just probe_all and we just received
8880Sstevel@tonic-gate 				 * a reply from a target address we were
8890Sstevel@tonic-gate 				 * probing and had timed out (now we are probing
8900Sstevel@tonic-gate 				 * some other target address), we ignore
8910Sstevel@tonic-gate 				 * this reply.
8920Sstevel@tonic-gate 				 */
8930Sstevel@tonic-gate 				if (probe_all && !stats) {
8940Sstevel@tonic-gate 					valid_reply = _B_FALSE;
8950Sstevel@tonic-gate 					/*
8960Sstevel@tonic-gate 					 * Only if it's verbose, we get a
8970Sstevel@tonic-gate 					 * message regarding this reply,
8980Sstevel@tonic-gate 					 * otherwise we are done here.
8990Sstevel@tonic-gate 					 */
9000Sstevel@tonic-gate 					if (!verbose) {
9010Sstevel@tonic-gate 						(void) sigrelse(SIGALRM);
9020Sstevel@tonic-gate 						return;
9030Sstevel@tonic-gate 					}
9040Sstevel@tonic-gate 				}
9050Sstevel@tonic-gate 			}
9060Sstevel@tonic-gate 		}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 		if (!stats && valid_reply) {
9090Sstevel@tonic-gate 			/*
9100Sstevel@tonic-gate 			 * if we are still sending to the same target address,
9110Sstevel@tonic-gate 			 * then stop it, because we know it's alive.
9120Sstevel@tonic-gate 			 */
9130Sstevel@tonic-gate 			if (reply_matched_current_target) {
9140Sstevel@tonic-gate 				(void) alarm(0);	/* cancel alarm */
9150Sstevel@tonic-gate 				(void) sigset(SIGALRM, SIG_IGN);
9160Sstevel@tonic-gate 				current_targetaddr->probing_done = _B_TRUE;
9170Sstevel@tonic-gate 			}
9180Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 			if (!probe_all) {
9210Sstevel@tonic-gate 				Printf("%s is alive\n", targethost);
9220Sstevel@tonic-gate 			} else {
9230Sstevel@tonic-gate 				/*
9240Sstevel@tonic-gate 				 * If we are using send_reply, the real
9250Sstevel@tonic-gate 				 * target address is not the src address of the
9260Sstevel@tonic-gate 				 * replies. Use icmp_seq to find out where this
9270Sstevel@tonic-gate 				 * probe was sent to.
9280Sstevel@tonic-gate 				 */
9290Sstevel@tonic-gate 				if (send_reply) {
9300Sstevel@tonic-gate 					(void) find_dstaddr(
9310Sstevel@tonic-gate 					    ntohs(icp->icmp_seq), &dst_addr);
9320Sstevel@tonic-gate 					(void) inet_ntop(AF_INET,
9330Sstevel@tonic-gate 					    (void *)&dst_addr.addr,
9340Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
9350Sstevel@tonic-gate 				} else {
9360Sstevel@tonic-gate 					(void) inet_ntop(AF_INET,
9370Sstevel@tonic-gate 					    (void *)&from->sin_addr,
9380Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
9390Sstevel@tonic-gate 				}
9400Sstevel@tonic-gate 				if (nflag) {
9410Sstevel@tonic-gate 					Printf("%s is alive\n", tmp_buf);
9420Sstevel@tonic-gate 				} else {
9430Sstevel@tonic-gate 					Printf("%s (%s) is alive\n",
9440Sstevel@tonic-gate 					    targethost, tmp_buf);
9450Sstevel@tonic-gate 				}
9460Sstevel@tonic-gate 			}
9470Sstevel@tonic-gate 			if (reply_matched_current_target) {
9480Sstevel@tonic-gate 				/*
9490Sstevel@tonic-gate 				 * Let's get things going again, but now
9500Sstevel@tonic-gate 				 * ping will start sending to next target IP
9510Sstevel@tonic-gate 				 * address.
9520Sstevel@tonic-gate 				 */
9530Sstevel@tonic-gate 				send_scheduled_probe();
9540Sstevel@tonic-gate 				(void) sigset(SIGALRM, sigalrm_handler);
9550Sstevel@tonic-gate 				schedule_sigalrm();
9560Sstevel@tonic-gate 			}
9570Sstevel@tonic-gate 			return;
9580Sstevel@tonic-gate 		} else {
9590Sstevel@tonic-gate 			/*
9600Sstevel@tonic-gate 			 * If we are not moving to next targetaddr, let's
9610Sstevel@tonic-gate 			 * release the SIGALRM now. We don't want to stall in
9620Sstevel@tonic-gate 			 * the middle of probing a targetaddr if the pr_name()
9630Sstevel@tonic-gate 			 * call (see below) takes longer.
9640Sstevel@tonic-gate 			 */
9650Sstevel@tonic-gate 			if (!last_reply_from_targetaddr)
9660Sstevel@tonic-gate 				(void) sigrelse(SIGALRM);
9670Sstevel@tonic-gate 			/* else, we'll release it later */
9680Sstevel@tonic-gate 		}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 		/*
9710Sstevel@tonic-gate 		 * If we are using send_reply, the real target address is
9720Sstevel@tonic-gate 		 * not the src address of the replies. Use icmp_seq to find out
9730Sstevel@tonic-gate 		 * where this probe was sent to.
9740Sstevel@tonic-gate 		 */
9750Sstevel@tonic-gate 		if (send_reply) {
9760Sstevel@tonic-gate 			(void) find_dstaddr(ntohs(icp->icmp_seq), &dst_addr);
9770Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
9780Sstevel@tonic-gate 			    pr_name((char *)&dst_addr.addr,  AF_INET));
9790Sstevel@tonic-gate 		} else {
9800Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
9810Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
9820Sstevel@tonic-gate 		}
9830Sstevel@tonic-gate 		Printf("icmp_seq=%d. ", ntohs(icp->icmp_seq));
9840Sstevel@tonic-gate 		Printf("orig = %lu, recv = %lu, xmit = %lu ",
9850Sstevel@tonic-gate 		    (ulong_t)ntohl(icp->icmp_otime),
9860Sstevel@tonic-gate 		    (ulong_t)ntohl(icp->icmp_rtime),
9870Sstevel@tonic-gate 		    (ulong_t)ntohl(icp->icmp_ttime));
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		if (valid_reply) {
9900Sstevel@tonic-gate 			/*
9910Sstevel@tonic-gate 			 * icp->icmp_otime is the time passed since midnight.
9920Sstevel@tonic-gate 			 * Therefore we need to adjust tv value, which is
9930Sstevel@tonic-gate 			 * the time passed since Jan 1, 1970.
9940Sstevel@tonic-gate 			 */
9950Sstevel@tonic-gate 			triptime = (tv.tv_sec % (24LL * 60 * 60)) * MILLISEC +
9960Sstevel@tonic-gate 			    (tv.tv_usec / (MICROSEC/MILLISEC));
9970Sstevel@tonic-gate 			triptime -= ntohl(icp->icmp_otime);
9980Sstevel@tonic-gate 			if (triptime < 0)
9990Sstevel@tonic-gate 				triptime += 24LL * 60 * 60 * MILLISEC;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 			Printf("time=%d. ms", (int)triptime);
10020Sstevel@tonic-gate 			triptime *= (MICROSEC/MILLISEC);
10030Sstevel@tonic-gate 			tsum += triptime;
10040Sstevel@tonic-gate 			tsum2 += triptime*triptime;
10050Sstevel@tonic-gate 			if (triptime < tmin)
10060Sstevel@tonic-gate 				tmin = triptime;
10070Sstevel@tonic-gate 			if (triptime > tmax)
10080Sstevel@tonic-gate 				tmax = triptime;
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 		(void) putchar('\n');
10110Sstevel@tonic-gate 		/*
10120Sstevel@tonic-gate 		 * If it's stats, probe-all, npackets > 0, and we received reply
10130Sstevel@tonic-gate 		 * for the last probe sent to this target address, then we
10140Sstevel@tonic-gate 		 * don't need to wait anymore, let's move on to next target
10150Sstevel@tonic-gate 		 * address, now!
10160Sstevel@tonic-gate 		 */
10170Sstevel@tonic-gate 		if (last_reply_from_targetaddr) {
10180Sstevel@tonic-gate 			(void) alarm(0);	/* cancel alarm */
10190Sstevel@tonic-gate 			current_targetaddr->probing_done = _B_TRUE;
10200Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
10210Sstevel@tonic-gate 			send_scheduled_probe();
10220Sstevel@tonic-gate 			schedule_sigalrm();
10230Sstevel@tonic-gate 		}
10240Sstevel@tonic-gate 		break;
10250Sstevel@tonic-gate 	case ICMP_ROUTERADVERT:
10260Sstevel@tonic-gate 	case ICMP_ROUTERSOLICIT:
10270Sstevel@tonic-gate 		/* Router discovery messages */
10280Sstevel@tonic-gate 		return;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	case ICMP_ECHO:
10310Sstevel@tonic-gate 	case ICMP_TSTAMP:
10320Sstevel@tonic-gate 	case ICMP_IREQ:
10330Sstevel@tonic-gate 	case ICMP_MASKREQ:
10340Sstevel@tonic-gate 		/* These were never passed out from the SunOS 4.X kernel. */
10350Sstevel@tonic-gate 		return;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	case ICMP_IREQREPLY:
10380Sstevel@tonic-gate 	case ICMP_MASKREPLY:
10390Sstevel@tonic-gate 		/* Replies for information and address mask requests */
10400Sstevel@tonic-gate 		return;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	default:
10430Sstevel@tonic-gate 		if (verbose) {
10440Sstevel@tonic-gate 			Printf("%d bytes from %s:\n", cc,
10450Sstevel@tonic-gate 			    pr_name((char *)&from->sin_addr, AF_INET));
10460Sstevel@tonic-gate 			Printf("icmp_type=%d (%s) ",
10470Sstevel@tonic-gate 			    icp->icmp_type, pr_type(icp->icmp_type));
10480Sstevel@tonic-gate 			Printf("icmp_code=%d\n", icp->icmp_code);
10490Sstevel@tonic-gate 			for (i = 0; i < 12; i++) {
10500Sstevel@tonic-gate 				Printf("x%2.2x: x%8.8x\n",
10510Sstevel@tonic-gate 				    i * sizeof (int32_t), *intp++);
10520Sstevel@tonic-gate 			}
10530Sstevel@tonic-gate 		}
10540Sstevel@tonic-gate 		break;
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	buf += sizeof (struct ip);
10580Sstevel@tonic-gate 	hlen -= sizeof (struct ip);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	/* if verbose and there exists IP options */
10610Sstevel@tonic-gate 	if (verbose && hlen > 0)
10620Sstevel@tonic-gate 		pr_options((uchar_t *)buf, hlen);
10630Sstevel@tonic-gate }
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate /*
10660Sstevel@tonic-gate  * Print out the ip options.
10670Sstevel@tonic-gate  */
10680Sstevel@tonic-gate static void
pr_options(uchar_t * opt,int optlength)10690Sstevel@tonic-gate pr_options(uchar_t *opt, int optlength)
10700Sstevel@tonic-gate {
10710Sstevel@tonic-gate 	int curlength;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	Printf("  IP options: ");
10740Sstevel@tonic-gate 	while (optlength > 0) {
10750Sstevel@tonic-gate 		curlength = opt[1];
10760Sstevel@tonic-gate 		switch (*opt) {
10770Sstevel@tonic-gate 		case IPOPT_EOL:
10780Sstevel@tonic-gate 			optlength = 0;
10790Sstevel@tonic-gate 			break;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		case IPOPT_NOP:
10820Sstevel@tonic-gate 			opt++;
10830Sstevel@tonic-gate 			optlength--;
10840Sstevel@tonic-gate 			continue;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		case IPOPT_RR:
10870Sstevel@tonic-gate 			Printf(" <record route> ");
10880Sstevel@tonic-gate 			pr_rropt(opt, curlength, _B_TRUE);
10890Sstevel@tonic-gate 			break;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		case IPOPT_TS:
10920Sstevel@tonic-gate 			Printf(" <time stamp> ");
10930Sstevel@tonic-gate 			pr_tsopt(opt, curlength);
10940Sstevel@tonic-gate 			break;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		case IPOPT_SECURITY:
10970Sstevel@tonic-gate 			Printf(" <security>");
10980Sstevel@tonic-gate 			break;
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 		case IPOPT_LSRR:
11010Sstevel@tonic-gate 			Printf(" <loose source route> ");
11020Sstevel@tonic-gate 			pr_rropt(opt, curlength, _B_FALSE);
11030Sstevel@tonic-gate 			break;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		case IPOPT_SATID:
11060Sstevel@tonic-gate 			Printf(" <stream id>");
11070Sstevel@tonic-gate 			break;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 		case IPOPT_SSRR:
11100Sstevel@tonic-gate 			Printf(" <strict source route> ");
11110Sstevel@tonic-gate 			pr_rropt(opt, curlength, _B_FALSE);
11120Sstevel@tonic-gate 			break;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 		default:
11150Sstevel@tonic-gate 			Printf(" <option %d, len %d>", *opt, curlength);
11160Sstevel@tonic-gate 			break;
11170Sstevel@tonic-gate 		}
11180Sstevel@tonic-gate 		/*
11190Sstevel@tonic-gate 		 * Following most options comes a length field
11200Sstevel@tonic-gate 		 */
11210Sstevel@tonic-gate 		opt += curlength;
11220Sstevel@tonic-gate 		optlength -= curlength;
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 	(void) putchar('\n');
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate /*
11280Sstevel@tonic-gate  * Print out a recorded route option. If rrflag is _B_TRUE, it prints record
11290Sstevel@tonic-gate  * route option, otherwise LSRR/SSRR.
11300Sstevel@tonic-gate  */
11310Sstevel@tonic-gate static void
pr_rropt(uchar_t * opt,int length,boolean_t rrflag)11320Sstevel@tonic-gate pr_rropt(uchar_t *opt, int length, boolean_t rrflag)
11330Sstevel@tonic-gate {
11340Sstevel@tonic-gate 	struct ip_sourceroute *rrp;
11350Sstevel@tonic-gate 	int sr_index = 0;
11360Sstevel@tonic-gate 	struct in_addr addr;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	rrp = (struct ip_sourceroute *)opt;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	/* data starts at offset 3 */
11410Sstevel@tonic-gate 	length -= 3;
11420Sstevel@tonic-gate 	while (length > 0) {
11430Sstevel@tonic-gate 		/*
11440Sstevel@tonic-gate 		 * Let's see if we are examining the addr pointed by ipsr_ptr
11450Sstevel@tonic-gate 		 */
11460Sstevel@tonic-gate 		if ((rrp->ipsr_ptr == (sr_index + 1) * sizeof (addr)) &&
11470Sstevel@tonic-gate 		    rrflag) {
11480Sstevel@tonic-gate 			Printf(" (End of record)");
11490Sstevel@tonic-gate 			break;
11500Sstevel@tonic-gate 		}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		bcopy(&rrp->ipsr_addrs[sr_index], &addr, sizeof (addr));
11530Sstevel@tonic-gate 		Printf("%s", pr_name((char *)&addr, AF_INET));
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		if (rrp->ipsr_ptr == (sr_index + 1) * sizeof (addr)) {
11560Sstevel@tonic-gate 			Printf("(Current)");
11570Sstevel@tonic-gate 		}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		sr_index++;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 		length -= sizeof (addr);
11620Sstevel@tonic-gate 		if (length > 0)
11630Sstevel@tonic-gate 			Printf(", ");
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate /*
11680Sstevel@tonic-gate  * Print out a timestamp option.
11690Sstevel@tonic-gate  */
11700Sstevel@tonic-gate static void
pr_tsopt(uchar_t * opt,int length)11710Sstevel@tonic-gate pr_tsopt(uchar_t *opt, int length)
11720Sstevel@tonic-gate {
11730Sstevel@tonic-gate 	boolean_t address_present;
11740Sstevel@tonic-gate 	boolean_t rrflag;		/* End at current entry? */
11750Sstevel@tonic-gate 	struct ip_timestamp *tsp;
11760Sstevel@tonic-gate 	int ts_index = 0;
11770Sstevel@tonic-gate 	struct in_addr addr;
11780Sstevel@tonic-gate 	size_t data_len;
11790Sstevel@tonic-gate 	int32_t time;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	/* LINTED */
11820Sstevel@tonic-gate 	tsp = (struct ip_timestamp *)opt;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	switch (tsp->ipt_flg) {
11850Sstevel@tonic-gate 	case IPOPT_TS_TSONLY:
11860Sstevel@tonic-gate 		address_present = _B_FALSE;
11870Sstevel@tonic-gate 		data_len = sizeof (tsp->ipt_timestamp.ipt_time[0]);
11880Sstevel@tonic-gate 		rrflag = _B_TRUE;
11890Sstevel@tonic-gate 		break;
11900Sstevel@tonic-gate 	case IPOPT_TS_TSANDADDR:
11910Sstevel@tonic-gate 		address_present = _B_TRUE;
11920Sstevel@tonic-gate 		data_len = sizeof (tsp->ipt_timestamp.ipt_ta[0]);
11930Sstevel@tonic-gate 		rrflag = _B_TRUE;
11940Sstevel@tonic-gate 		break;
11950Sstevel@tonic-gate 	case IPOPT_TS_PRESPEC:
11960Sstevel@tonic-gate 	case 3:
11970Sstevel@tonic-gate 		address_present = _B_TRUE;
11980Sstevel@tonic-gate 		data_len = sizeof (tsp->ipt_timestamp.ipt_ta[0]);
11990Sstevel@tonic-gate 		rrflag = _B_FALSE;
12000Sstevel@tonic-gate 		break;
12010Sstevel@tonic-gate 	default:
12020Sstevel@tonic-gate 		Printf("(Bad flag value: 0x%x)", tsp->ipt_flg);
12030Sstevel@tonic-gate 		return;
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 	if (tsp->ipt_oflw > 0)
12060Sstevel@tonic-gate 		Printf("(Overflow: %d) ", tsp->ipt_oflw);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	/* data starts at offset 4 */
12090Sstevel@tonic-gate 	length -= 4;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	while (length > 0) {
12120Sstevel@tonic-gate 		if (length < data_len)
12130Sstevel@tonic-gate 			break;
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 		/* the minimum value of ipt_ptr is 5 */
12160Sstevel@tonic-gate 		if ((tsp->ipt_ptr == ts_index * data_len + 5) && rrflag) {
12170Sstevel@tonic-gate 			Printf(" (End of record)");
12180Sstevel@tonic-gate 			break;
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 		if (address_present) {
12210Sstevel@tonic-gate 			bcopy(&tsp->ipt_timestamp.ipt_ta[ts_index].ipt_addr,
12220Sstevel@tonic-gate 			    &addr, sizeof (addr));
12230Sstevel@tonic-gate 			Printf("%s: ", pr_name((char *)&addr, AF_INET));
12240Sstevel@tonic-gate 			bcopy(&tsp->ipt_timestamp.ipt_ta[ts_index].ipt_time,
12250Sstevel@tonic-gate 			    &time, sizeof (time));
12260Sstevel@tonic-gate 		} else {
12270Sstevel@tonic-gate 			bcopy(&tsp->ipt_timestamp.ipt_time[ts_index],
12280Sstevel@tonic-gate 			    &time, sizeof (time));
12290Sstevel@tonic-gate 		}
12300Sstevel@tonic-gate 		Printf("%d", ntohl(time));
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		if (tsp->ipt_ptr == ts_index * data_len + 5)
12330Sstevel@tonic-gate 			Printf("(Current)");
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 		ts_index++;
12360Sstevel@tonic-gate 		length -= data_len;
12370Sstevel@tonic-gate 		if (length > 0)
12380Sstevel@tonic-gate 			Printf(", ");
12390Sstevel@tonic-gate 	}
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate /*
12430Sstevel@tonic-gate  * Convert an ICMP "type" field to a printable string.
12440Sstevel@tonic-gate  */
12450Sstevel@tonic-gate static char *
pr_type(int icmp_type)12460Sstevel@tonic-gate pr_type(int icmp_type)
12470Sstevel@tonic-gate {
12480Sstevel@tonic-gate 	static struct icmptype_table ttab[] = {
12490Sstevel@tonic-gate 		{ICMP_ECHOREPLY,	"Echo Reply"},
12500Sstevel@tonic-gate 		{1,			"ICMP 1"},
12510Sstevel@tonic-gate 		{2,			"ICMP 2"},
12520Sstevel@tonic-gate 		{ICMP_UNREACH,		"Dest Unreachable"},
12530Sstevel@tonic-gate 		{ICMP_SOURCEQUENCH,	"Source Quench"},
12540Sstevel@tonic-gate 		{ICMP_REDIRECT,		"Redirect"},
12550Sstevel@tonic-gate 		{6,			"ICMP 6"},
12560Sstevel@tonic-gate 		{7,			"ICMP 7"},
12570Sstevel@tonic-gate 		{ICMP_ECHO,		"Echo"},
12580Sstevel@tonic-gate 		{ICMP_ROUTERADVERT,	"Router Advertisement"},
12590Sstevel@tonic-gate 		{ICMP_ROUTERSOLICIT,	"Router Solicitation"},
12600Sstevel@tonic-gate 		{ICMP_TIMXCEED,		"Time Exceeded"},
12610Sstevel@tonic-gate 		{ICMP_PARAMPROB,	"Parameter Problem"},
12620Sstevel@tonic-gate 		{ICMP_TSTAMP,		"Timestamp"},
12630Sstevel@tonic-gate 		{ICMP_TSTAMPREPLY,	"Timestamp Reply"},
12640Sstevel@tonic-gate 		{ICMP_IREQ,		"Info Request"},
12650Sstevel@tonic-gate 		{ICMP_IREQREPLY,	"Info Reply"},
12660Sstevel@tonic-gate 		{ICMP_MASKREQ,		"Netmask Request"},
12670Sstevel@tonic-gate 		{ICMP_MASKREPLY,	"Netmask Reply"}
12680Sstevel@tonic-gate 	};
12690Sstevel@tonic-gate 	int i;
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 	for (i = 0; i < A_CNT(ttab); i++) {
12720Sstevel@tonic-gate 		if (ttab[i].type == icmp_type)
12730Sstevel@tonic-gate 			return (ttab[i].message);
12740Sstevel@tonic-gate 	}
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	return ("OUT-OF-RANGE");
12770Sstevel@tonic-gate }
1278