17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
230406ceaaSmeem * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
30*b08923d6SRobert Mustacchi /*
31*b08923d6SRobert Mustacchi * Copyright 2015, Joyent, Inc.
32*b08923d6SRobert Mustacchi */
33*b08923d6SRobert Mustacchi
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
367c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <strings.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #include <sys/time.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/socket.h>
507c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
517c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
527c478bd9Sstevel@tonic-gate #include <sys/file.h>
537c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
547c478bd9Sstevel@tonic-gate #include <net/if.h>
557c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
567c478bd9Sstevel@tonic-gate #include <netinet/in.h>
577c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
587c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
597c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
607c478bd9Sstevel@tonic-gate #include <netdb.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate
630406ceaaSmeem #include <libinetutil.h>
647c478bd9Sstevel@tonic-gate #include "ping.h"
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate * IPv4 source routing option.
697c478bd9Sstevel@tonic-gate * In order to avoid padding for the alignment of IPv4 addresses, ipsr_addrs
707c478bd9Sstevel@tonic-gate * is defined as a 2-D array of uint8_t, instead of 1-D array of struct in_addr.
717c478bd9Sstevel@tonic-gate */
727c478bd9Sstevel@tonic-gate struct ip_sourceroute {
737c478bd9Sstevel@tonic-gate uint8_t ipsr_code;
747c478bd9Sstevel@tonic-gate uint8_t ipsr_len;
757c478bd9Sstevel@tonic-gate uint8_t ipsr_ptr;
767c478bd9Sstevel@tonic-gate /* up to 9 IPv4 addresses */
777c478bd9Sstevel@tonic-gate uint8_t ipsr_addrs[1][sizeof (struct in_addr)];
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate void check_reply(struct addrinfo *, struct msghdr *, int, ushort_t);
817c478bd9Sstevel@tonic-gate extern void find_dstaddr(ushort_t, union any_in_addr *);
827c478bd9Sstevel@tonic-gate extern boolean_t is_a_target(struct addrinfo *, union any_in_addr *);
837c478bd9Sstevel@tonic-gate extern char *pr_name(char *, int);
847c478bd9Sstevel@tonic-gate static void pr_options(uchar_t *, int);
857c478bd9Sstevel@tonic-gate extern char *pr_protocol(int);
867c478bd9Sstevel@tonic-gate static void pr_rropt(uchar_t *, int, boolean_t);
877c478bd9Sstevel@tonic-gate static void pr_tsopt(uchar_t *, int);
887c478bd9Sstevel@tonic-gate static char *pr_type(int);
897c478bd9Sstevel@tonic-gate extern void schedule_sigalrm();
907c478bd9Sstevel@tonic-gate extern void send_scheduled_probe();
917c478bd9Sstevel@tonic-gate extern boolean_t seq_match(ushort_t, int, ushort_t);
927c478bd9Sstevel@tonic-gate extern void sigalrm_handler();
937c478bd9Sstevel@tonic-gate void set_IPv4_options(int, union any_in_addr *, int, struct in_addr *,
947c478bd9Sstevel@tonic-gate struct in_addr *);
957c478bd9Sstevel@tonic-gate extern void tvsub(struct timeval *, struct timeval *);
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate * Set IPv4 options
997c478bd9Sstevel@tonic-gate */
1007c478bd9Sstevel@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)1017c478bd9Sstevel@tonic-gate set_IPv4_options(int sock, union any_in_addr *gw_IP_list, int gw_count,
1027c478bd9Sstevel@tonic-gate struct in_addr *src, struct in_addr *dst)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate int req_size;
1057c478bd9Sstevel@tonic-gate char srr[ROUTE_SIZE + 1];
1067c478bd9Sstevel@tonic-gate char *bufp;
1077c478bd9Sstevel@tonic-gate int optsize = ROUTE_SIZE;
1087c478bd9Sstevel@tonic-gate struct ip_sourceroute *srp;
1097c478bd9Sstevel@tonic-gate struct ip_timestamp *tsp;
1107c478bd9Sstevel@tonic-gate int i;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate if (rr_option || ts_option || gw_count > 0) {
1137c478bd9Sstevel@tonic-gate bzero(srr, sizeof (srr));
1147c478bd9Sstevel@tonic-gate bufp = srr;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if (gw_count > 0) {
1177c478bd9Sstevel@tonic-gate /* 3 = 1 (code) + 1 (len) + 1 (ptr) of src route opt. */
1187c478bd9Sstevel@tonic-gate req_size = 3 + (sizeof (struct in_addr)) * gw_count;
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate if (optsize < req_size) {
1217c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: too many IPv4 gateways\n",
1227c478bd9Sstevel@tonic-gate progname);
1237c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate srp = (struct ip_sourceroute *)bufp;
1277c478bd9Sstevel@tonic-gate srp->ipsr_code = strict ? IPOPT_SSRR : IPOPT_LSRR;
1287c478bd9Sstevel@tonic-gate srp->ipsr_len = req_size;
1297c478bd9Sstevel@tonic-gate srp->ipsr_ptr = IPOPT_MINOFF;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate for (i = 0; i < gw_count; i++) {
1327c478bd9Sstevel@tonic-gate bcopy((char *)&gw_IP_list[i].addr,
1337c478bd9Sstevel@tonic-gate &srp->ipsr_addrs[i],
1347c478bd9Sstevel@tonic-gate sizeof (struct in_addr));
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate optsize -= srp->ipsr_len;
1377c478bd9Sstevel@tonic-gate bufp += srp->ipsr_len;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate /* do we send a timestamp option? */
1407c478bd9Sstevel@tonic-gate if (ts_option) {
1417c478bd9Sstevel@tonic-gate if (optsize < IPOPT_MINOFF) {
1427c478bd9Sstevel@tonic-gate Fprintf(stderr,
1437c478bd9Sstevel@tonic-gate "%s: no room for timestamp option\n",
1447c478bd9Sstevel@tonic-gate progname);
1457c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate /* LINTED */
1487c478bd9Sstevel@tonic-gate tsp = (struct ip_timestamp *)bufp;
1497c478bd9Sstevel@tonic-gate tsp->ipt_code = IPOPT_TS;
1507c478bd9Sstevel@tonic-gate tsp->ipt_len = optsize;
1517c478bd9Sstevel@tonic-gate tsp->ipt_ptr = IPOPT_MINOFF + 1;
1527c478bd9Sstevel@tonic-gate tsp->ipt_flg = ts_flag & 0x0f;
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate if (tsp->ipt_flg > IPOPT_TS_TSANDADDR) {
1557c478bd9Sstevel@tonic-gate req_size = IPOPT_MINOFF +
1567c478bd9Sstevel@tonic-gate 2 * sizeof (struct ipt_ta);
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate * Note: BSD/4.X is broken in their check so we
1597c478bd9Sstevel@tonic-gate * have to bump up this number by at least one.
1607c478bd9Sstevel@tonic-gate */
1617c478bd9Sstevel@tonic-gate req_size++;
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate if (optsize < req_size) {
1647c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: no room for "
1657c478bd9Sstevel@tonic-gate "timestamp option\n", progname);
1667c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate bcopy((char *)dst,
1707c478bd9Sstevel@tonic-gate &tsp->ipt_timestamp.ipt_ta[0].ipt_addr,
1717c478bd9Sstevel@tonic-gate sizeof (struct in_addr));
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate bcopy((char *)src,
1747c478bd9Sstevel@tonic-gate &tsp->ipt_timestamp.ipt_ta[1].ipt_addr,
1757c478bd9Sstevel@tonic-gate sizeof (struct in_addr));
1767c478bd9Sstevel@tonic-gate tsp->ipt_len = req_size;
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate optsize -= tsp->ipt_len;
1807c478bd9Sstevel@tonic-gate bufp += tsp->ipt_len;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate /* do we send a record route option? */
1837c478bd9Sstevel@tonic-gate if (rr_option) {
1847c478bd9Sstevel@tonic-gate if (optsize < IPOPT_MINOFF) {
1857c478bd9Sstevel@tonic-gate Fprintf(stderr,
1867c478bd9Sstevel@tonic-gate "%s: no room for record route option\n",
1877c478bd9Sstevel@tonic-gate progname);
1887c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate * Format of record route option is same as source
1937c478bd9Sstevel@tonic-gate * route option.
1947c478bd9Sstevel@tonic-gate */
1957c478bd9Sstevel@tonic-gate srp = (struct ip_sourceroute *)bufp;
1967c478bd9Sstevel@tonic-gate srp->ipsr_code = IPOPT_RR;
1977c478bd9Sstevel@tonic-gate srp->ipsr_len = optsize;
1987c478bd9Sstevel@tonic-gate srp->ipsr_ptr = IPOPT_MINOFF;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate optsize -= srp->ipsr_len;
2017c478bd9Sstevel@tonic-gate bufp += srp->ipsr_len;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate optsize = bufp - srr;
2047c478bd9Sstevel@tonic-gate /* Round up to 4 byte boundary */
2057c478bd9Sstevel@tonic-gate if (optsize & 0x3)
2067c478bd9Sstevel@tonic-gate optsize = (optsize & ~0x3) + 4;
2077c478bd9Sstevel@tonic-gate if (setsockopt(sock, IPPROTO_IP, IP_OPTIONS, srr, optsize) <
2087c478bd9Sstevel@tonic-gate 0) {
2097c478bd9Sstevel@tonic-gate Fprintf(stderr, "%s: setsockopt IP_OPTIONS %s\n",
2107c478bd9Sstevel@tonic-gate progname, strerror(errno));
2117c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate * Check out the packet to see if it came from us. This logic is necessary
2187c478bd9Sstevel@tonic-gate * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
2197c478bd9Sstevel@tonic-gate * which arrive ('tis only fair). This permits multiple copies of this
2207c478bd9Sstevel@tonic-gate * program to be run without having intermingled output (or statistics!).
2217c478bd9Sstevel@tonic-gate */
2227c478bd9Sstevel@tonic-gate void
check_reply(struct addrinfo * ai_dst,struct msghdr * msg,int cc,ushort_t udp_src_port)2237c478bd9Sstevel@tonic-gate check_reply(struct addrinfo *ai_dst, struct msghdr *msg, int cc,
2247c478bd9Sstevel@tonic-gate ushort_t udp_src_port)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate struct ip *ip;
2277c478bd9Sstevel@tonic-gate struct icmp *icp;
2287c478bd9Sstevel@tonic-gate struct udphdr *up;
2297c478bd9Sstevel@tonic-gate union any_in_addr dst_addr;
2307c478bd9Sstevel@tonic-gate uchar_t *buf;
2317c478bd9Sstevel@tonic-gate int32_t *intp;
2327c478bd9Sstevel@tonic-gate struct sockaddr_in *from;
2337c478bd9Sstevel@tonic-gate struct timeval *tp;
2347c478bd9Sstevel@tonic-gate struct timeval tv;
2357c478bd9Sstevel@tonic-gate int hlen, hlen1;
2367c478bd9Sstevel@tonic-gate int64_t triptime;
2377c478bd9Sstevel@tonic-gate boolean_t valid_reply = _B_FALSE;
238*b08923d6SRobert Mustacchi boolean_t reply_matched_current_target = _B_FALSE; /* Is the source */
239*b08923d6SRobert Mustacchi /* address of this reply same */
240*b08923d6SRobert Mustacchi /* as where we're sending */
241*b08923d6SRobert Mustacchi /* currently? */
2427c478bd9Sstevel@tonic-gate boolean_t last_reply_from_targetaddr = _B_FALSE; /* Is this stats, */
2437c478bd9Sstevel@tonic-gate /* probe all with npackets>0 */
2447c478bd9Sstevel@tonic-gate /* and we received reply for */
2457c478bd9Sstevel@tonic-gate /* the last probe sent to */
2467c478bd9Sstevel@tonic-gate /* targetaddr */
2477c478bd9Sstevel@tonic-gate int cc_left;
2487c478bd9Sstevel@tonic-gate char tmp_buf[INET6_ADDRSTRLEN];
2497c478bd9Sstevel@tonic-gate static char *unreach[] = {
2507c478bd9Sstevel@tonic-gate "Net Unreachable",
2517c478bd9Sstevel@tonic-gate "Host Unreachable",
2527c478bd9Sstevel@tonic-gate "Protocol Unreachable",
2537c478bd9Sstevel@tonic-gate "Port Unreachable",
2547c478bd9Sstevel@tonic-gate "Fragmentation needed and DF set",
2557c478bd9Sstevel@tonic-gate "Source Route Failed",
2567c478bd9Sstevel@tonic-gate /* The following are from RFC1700 */
2577c478bd9Sstevel@tonic-gate "Net Unknown",
2587c478bd9Sstevel@tonic-gate "Host Unknown",
2597c478bd9Sstevel@tonic-gate "Source Host Isolated",
2607c478bd9Sstevel@tonic-gate "Dest Net Prohibited",
2617c478bd9Sstevel@tonic-gate "Dest Host Prohibited",
2627c478bd9Sstevel@tonic-gate "Net Unreachable for TOS",
2637c478bd9Sstevel@tonic-gate "Host Unreachable for TOS",
2647c478bd9Sstevel@tonic-gate "Communication Administratively Prohibited",
2657c478bd9Sstevel@tonic-gate "Host Precedence Violation",
2667c478bd9Sstevel@tonic-gate "Precedence Cutoff in Effect"
2677c478bd9Sstevel@tonic-gate };
2687c478bd9Sstevel@tonic-gate static char *redirect[] = {
2697c478bd9Sstevel@tonic-gate "Net",
2707c478bd9Sstevel@tonic-gate "Host",
2717c478bd9Sstevel@tonic-gate "TOS Net",
2727c478bd9Sstevel@tonic-gate "TOS Host"
2737c478bd9Sstevel@tonic-gate };
2747c478bd9Sstevel@tonic-gate static char *timexceed[] = {
2757c478bd9Sstevel@tonic-gate "Time exceeded in transit",
2767c478bd9Sstevel@tonic-gate "Time exceeded during reassembly"
2777c478bd9Sstevel@tonic-gate };
2787c478bd9Sstevel@tonic-gate boolean_t print_newline = _B_FALSE;
2797c478bd9Sstevel@tonic-gate int i;
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate /* decompose msghdr into useful pieces */
2827c478bd9Sstevel@tonic-gate buf = (uchar_t *)msg->msg_iov->iov_base;
2837c478bd9Sstevel@tonic-gate from = (struct sockaddr_in *)msg->msg_name;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate /* LINTED */
2867c478bd9Sstevel@tonic-gate intp = (int32_t *)buf;
2877c478bd9Sstevel@tonic-gate
288*b08923d6SRobert Mustacchi ping_gettime(msg, &tv);
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate /* LINTED */
2917c478bd9Sstevel@tonic-gate ip = (struct ip *)buf;
2927c478bd9Sstevel@tonic-gate hlen = ip->ip_hl << 2;
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate if ((cc < sizeof (struct ip)) || (cc < hlen + ICMP_MINLEN)) {
2957c478bd9Sstevel@tonic-gate if (verbose) {
2967c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n", cc,
2977c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate return;
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate cc -= hlen;
3037c478bd9Sstevel@tonic-gate /* LINTED */
3047c478bd9Sstevel@tonic-gate icp = (struct icmp *)(buf + hlen);
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate if (ip->ip_p == 0) {
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate * Assume that we are running on a pre-4.3BSD system
3097c478bd9Sstevel@tonic-gate * such as SunOS before 4.0
3107c478bd9Sstevel@tonic-gate */
3117c478bd9Sstevel@tonic-gate /* LINTED */
3127c478bd9Sstevel@tonic-gate icp = (struct icmp *)buf;
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate cc_left = cc - ICMP_MINLEN;
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate switch (icp->icmp_type) {
3177c478bd9Sstevel@tonic-gate case ICMP_UNREACH:
3187c478bd9Sstevel@tonic-gate ip = &icp->icmp_ip;
3197c478bd9Sstevel@tonic-gate hlen1 = ip->ip_hl << 2;
3207c478bd9Sstevel@tonic-gate
3217c478bd9Sstevel@tonic-gate /* check if we have enough of the packet to work on */
3227c478bd9Sstevel@tonic-gate if ((cc_left < sizeof (struct ip)) ||
3237c478bd9Sstevel@tonic-gate (cc_left < hlen1 + sizeof (struct udphdr))) {
3247c478bd9Sstevel@tonic-gate if (verbose) {
3257c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n",
3267c478bd9Sstevel@tonic-gate cc, pr_name((char *)&from->sin_addr,
3277c478bd9Sstevel@tonic-gate AF_INET));
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate return;
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate
3327c478bd9Sstevel@tonic-gate /* get the UDP packet */
3337c478bd9Sstevel@tonic-gate cc_left -= hlen1 + sizeof (struct udphdr);
3347c478bd9Sstevel@tonic-gate /* LINTED */
3357c478bd9Sstevel@tonic-gate up = (struct udphdr *)((uchar_t *)ip + hlen1);
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate /* check to see if this is what we sent */
3387c478bd9Sstevel@tonic-gate if (icp->icmp_code == ICMP_UNREACH_PORT &&
3397c478bd9Sstevel@tonic-gate ip->ip_p == IPPROTO_UDP &&
3407c478bd9Sstevel@tonic-gate udp_src_port == up->uh_sport &&
3417c478bd9Sstevel@tonic-gate use_udp) {
3427c478bd9Sstevel@tonic-gate valid_reply = _B_TRUE;
3437c478bd9Sstevel@tonic-gate } else {
3447c478bd9Sstevel@tonic-gate valid_reply = _B_FALSE;
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate if (valid_reply) {
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate * For this valid reply, if we are still sending to
3507c478bd9Sstevel@tonic-gate * this target IP address, we'd like to do some
3517c478bd9Sstevel@tonic-gate * updates to targetaddr, so hold SIGALRMs.
3527c478bd9Sstevel@tonic-gate */
3537c478bd9Sstevel@tonic-gate (void) sighold(SIGALRM);
3547c478bd9Sstevel@tonic-gate is_alive = _B_TRUE;
3557c478bd9Sstevel@tonic-gate nreceived++;
3567c478bd9Sstevel@tonic-gate reply_matched_current_target =
3577c478bd9Sstevel@tonic-gate seq_match(current_targetaddr->starting_seq_num,
358*b08923d6SRobert Mustacchi current_targetaddr->num_sent,
359*b08923d6SRobert Mustacchi ntohs(up->uh_dport));
3607c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
3617c478bd9Sstevel@tonic-gate current_targetaddr->got_reply = _B_TRUE;
3627c478bd9Sstevel@tonic-gate nreceived_last_target++;
3637c478bd9Sstevel@tonic-gate /*
3647c478bd9Sstevel@tonic-gate * Determine if stats, probe-all, and
3657c478bd9Sstevel@tonic-gate * npackets != 0, and this is the reply for
3667c478bd9Sstevel@tonic-gate * the last probe we sent to current target
3677c478bd9Sstevel@tonic-gate * address.
3687c478bd9Sstevel@tonic-gate */
3697c478bd9Sstevel@tonic-gate if (stats && probe_all && npackets > 0 &&
3707c478bd9Sstevel@tonic-gate ((current_targetaddr->starting_seq_num +
3717c478bd9Sstevel@tonic-gate current_targetaddr->num_probes - 1) %
3727c478bd9Sstevel@tonic-gate (MAX_PORT + 1) == ntohs(up->uh_dport)) &&
3737c478bd9Sstevel@tonic-gate (current_targetaddr->num_probes ==
3747c478bd9Sstevel@tonic-gate current_targetaddr->num_sent))
3757c478bd9Sstevel@tonic-gate last_reply_from_targetaddr = _B_TRUE;
3767c478bd9Sstevel@tonic-gate } else {
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate * If it's just probe_all and we just received
3797c478bd9Sstevel@tonic-gate * a reply from a target address we were
3807c478bd9Sstevel@tonic-gate * probing and had timed out (now we are probing
3817c478bd9Sstevel@tonic-gate * some other target address), we ignore
3827c478bd9Sstevel@tonic-gate * this reply.
3837c478bd9Sstevel@tonic-gate */
3847c478bd9Sstevel@tonic-gate if (probe_all && !stats) {
3857c478bd9Sstevel@tonic-gate valid_reply = _B_FALSE;
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate * Only if it's verbose, we get a
3887c478bd9Sstevel@tonic-gate * message regarding this reply,
3897c478bd9Sstevel@tonic-gate * otherwise we are done here.
3907c478bd9Sstevel@tonic-gate */
3917c478bd9Sstevel@tonic-gate if (!verbose) {
3927c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
3937c478bd9Sstevel@tonic-gate return;
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate
3997c478bd9Sstevel@tonic-gate /* stats mode doesn't print 'alive' messages */
4007c478bd9Sstevel@tonic-gate if (valid_reply && !stats) {
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate * if we are still sending to the same target address,
4037c478bd9Sstevel@tonic-gate * then stop it, because we know it's alive.
4047c478bd9Sstevel@tonic-gate */
4057c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
4067c478bd9Sstevel@tonic-gate (void) alarm(0); /* cancel alarm */
4077c478bd9Sstevel@tonic-gate (void) sigset(SIGALRM, SIG_IGN);
4087c478bd9Sstevel@tonic-gate current_targetaddr->probing_done = _B_TRUE;
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
4117c478bd9Sstevel@tonic-gate
4127c478bd9Sstevel@tonic-gate if (!probe_all) {
4137c478bd9Sstevel@tonic-gate Printf("%s is alive\n", targethost);
4147c478bd9Sstevel@tonic-gate } else {
4157c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET, (void *)&ip->ip_dst,
4167c478bd9Sstevel@tonic-gate tmp_buf, sizeof (tmp_buf));
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate if (nflag) {
4197c478bd9Sstevel@tonic-gate Printf("%s is alive\n", tmp_buf);
4207c478bd9Sstevel@tonic-gate } else {
4217c478bd9Sstevel@tonic-gate Printf("%s (%s) is alive\n",
4227c478bd9Sstevel@tonic-gate targethost, tmp_buf);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate * Let's get things going again, but now
4287c478bd9Sstevel@tonic-gate * ping will start sending to next target IP
4297c478bd9Sstevel@tonic-gate * address.
4307c478bd9Sstevel@tonic-gate */
4317c478bd9Sstevel@tonic-gate send_scheduled_probe();
4327c478bd9Sstevel@tonic-gate (void) sigset(SIGALRM, sigalrm_handler);
4337c478bd9Sstevel@tonic-gate schedule_sigalrm();
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate return;
4367c478bd9Sstevel@tonic-gate } else {
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate * If we are not moving to next targetaddr, let's
4397c478bd9Sstevel@tonic-gate * release the SIGALRM now. We don't want to stall in
4407c478bd9Sstevel@tonic-gate * the middle of probing a targetaddr if the pr_name()
4417c478bd9Sstevel@tonic-gate * call (see below) takes longer.
4427c478bd9Sstevel@tonic-gate */
4437c478bd9Sstevel@tonic-gate if (!last_reply_from_targetaddr)
4447c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
4457c478bd9Sstevel@tonic-gate /* else, we'll release it later */
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate dst_addr.addr = ip->ip_dst;
4497c478bd9Sstevel@tonic-gate if (valid_reply) {
4507c478bd9Sstevel@tonic-gate Printf("%d bytes from %s: ", cc,
4517c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
4527c478bd9Sstevel@tonic-gate Printf("udp_port=%d. ", ntohs(up->uh_dport));
4537c478bd9Sstevel@tonic-gate print_newline = _B_TRUE;
4547c478bd9Sstevel@tonic-gate } else if (is_a_target(ai_dst, &dst_addr) || verbose) {
4557c478bd9Sstevel@tonic-gate if (icp->icmp_code >= A_CNT(unreach)) {
4567c478bd9Sstevel@tonic-gate Printf("ICMP %d Unreachable from gateway %s\n",
4577c478bd9Sstevel@tonic-gate icp->icmp_code,
4587c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
4597c478bd9Sstevel@tonic-gate } else {
4607c478bd9Sstevel@tonic-gate Printf("ICMP %s from gateway %s\n",
4617c478bd9Sstevel@tonic-gate unreach[icp->icmp_code],
4627c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate Printf(" for %s from %s", pr_protocol(ip->ip_p),
4657c478bd9Sstevel@tonic-gate pr_name((char *)&ip->ip_src, AF_INET));
4667c478bd9Sstevel@tonic-gate Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
4677c478bd9Sstevel@tonic-gate if (ip->ip_p == IPPROTO_TCP ||
4687c478bd9Sstevel@tonic-gate ip->ip_p == IPPROTO_UDP) {
4697c478bd9Sstevel@tonic-gate Printf(" port %d ", ntohs(up->uh_dport));
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate print_newline = _B_TRUE;
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate /* if we are timing and the reply has a timeval */
4757c478bd9Sstevel@tonic-gate if (valid_reply && datalen >= sizeof (struct timeval) &&
4767c478bd9Sstevel@tonic-gate cc_left >= sizeof (struct timeval)) {
4777c478bd9Sstevel@tonic-gate /* LINTED */
4787c478bd9Sstevel@tonic-gate tp = (struct timeval *)((char *)up +
4797c478bd9Sstevel@tonic-gate sizeof (struct udphdr));
4807c478bd9Sstevel@tonic-gate (void) tvsub(&tv, tp);
4817c478bd9Sstevel@tonic-gate triptime = (int64_t)tv.tv_sec * MICROSEC + tv.tv_usec;
4827c478bd9Sstevel@tonic-gate Printf("time=" TIMEFORMAT " ms", triptime/1000.0);
4837c478bd9Sstevel@tonic-gate tsum += triptime;
4847c478bd9Sstevel@tonic-gate tsum2 += triptime*triptime;
4857c478bd9Sstevel@tonic-gate if (triptime < tmin)
4867c478bd9Sstevel@tonic-gate tmin = triptime;
4877c478bd9Sstevel@tonic-gate if (triptime > tmax)
4887c478bd9Sstevel@tonic-gate tmax = triptime;
4897c478bd9Sstevel@tonic-gate print_newline = _B_TRUE;
4907c478bd9Sstevel@tonic-gate }
4917c478bd9Sstevel@tonic-gate if (print_newline)
4927c478bd9Sstevel@tonic-gate (void) putchar('\n');
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate * If it's stats, probe-all, npackets > 0, and we received reply
4957c478bd9Sstevel@tonic-gate * for the last probe sent to this target address, then we
4967c478bd9Sstevel@tonic-gate * don't need to wait anymore, let's move on to next target
4977c478bd9Sstevel@tonic-gate * address, now!
4987c478bd9Sstevel@tonic-gate */
4997c478bd9Sstevel@tonic-gate if (last_reply_from_targetaddr) {
5007c478bd9Sstevel@tonic-gate (void) alarm(0); /* cancel alarm */
5017c478bd9Sstevel@tonic-gate current_targetaddr->probing_done = _B_TRUE;
5027c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
5037c478bd9Sstevel@tonic-gate send_scheduled_probe();
5047c478bd9Sstevel@tonic-gate schedule_sigalrm();
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate break;
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate case ICMP_REDIRECT:
5097c478bd9Sstevel@tonic-gate if (cc_left < sizeof (struct ip)) {
5107c478bd9Sstevel@tonic-gate if (verbose) {
5117c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n",
5127c478bd9Sstevel@tonic-gate cc, pr_name((char *)&from->sin_addr,
5137c478bd9Sstevel@tonic-gate AF_INET));
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate return;
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate ip = &icp->icmp_ip;
5197c478bd9Sstevel@tonic-gate dst_addr.addr = ip->ip_dst;
5207c478bd9Sstevel@tonic-gate if (is_a_target(ai_dst, &dst_addr) || verbose) {
5217c478bd9Sstevel@tonic-gate if (icp->icmp_code >= A_CNT(redirect)) {
5227c478bd9Sstevel@tonic-gate Printf("ICMP %d redirect from gateway %s\n",
5237c478bd9Sstevel@tonic-gate icp->icmp_code,
5247c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
5257c478bd9Sstevel@tonic-gate } else {
5267c478bd9Sstevel@tonic-gate Printf("ICMP %s redirect from gateway %s\n",
5277c478bd9Sstevel@tonic-gate redirect[icp->icmp_code],
5287c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate Printf(" to %s",
5317c478bd9Sstevel@tonic-gate pr_name((char *)&icp->icmp_gwaddr, AF_INET));
5327c478bd9Sstevel@tonic-gate Printf(" for %s\n",
5337c478bd9Sstevel@tonic-gate pr_name((char *)&ip->ip_dst, AF_INET));
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate break;
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate case ICMP_ECHOREPLY:
5387c478bd9Sstevel@tonic-gate if (ntohs(icp->icmp_id) == ident) {
5397c478bd9Sstevel@tonic-gate if (!use_udp && !use_icmp_ts)
5407c478bd9Sstevel@tonic-gate valid_reply = _B_TRUE;
5417c478bd9Sstevel@tonic-gate else
5427c478bd9Sstevel@tonic-gate valid_reply = _B_FALSE;
5437c478bd9Sstevel@tonic-gate } else {
5447c478bd9Sstevel@tonic-gate return;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate if (valid_reply) {
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate * For this valid reply, if we are still sending to
5507c478bd9Sstevel@tonic-gate * this target IP address, we'd like to do some
5517c478bd9Sstevel@tonic-gate * updates to targetaddr, so hold SIGALRMs.
5527c478bd9Sstevel@tonic-gate */
5537c478bd9Sstevel@tonic-gate (void) sighold(SIGALRM);
5547c478bd9Sstevel@tonic-gate is_alive = _B_TRUE;
5557c478bd9Sstevel@tonic-gate nreceived++;
5567c478bd9Sstevel@tonic-gate reply_matched_current_target =
5577c478bd9Sstevel@tonic-gate seq_match(current_targetaddr->starting_seq_num,
558*b08923d6SRobert Mustacchi current_targetaddr->num_sent,
559*b08923d6SRobert Mustacchi ntohs(icp->icmp_seq));
5607c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
5617c478bd9Sstevel@tonic-gate current_targetaddr->got_reply = _B_TRUE;
5627c478bd9Sstevel@tonic-gate nreceived_last_target++;
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate * Determine if stats, probe-all, and
5657c478bd9Sstevel@tonic-gate * npackets != 0, and this is the reply for
5667c478bd9Sstevel@tonic-gate * the last probe we sent to current target
5677c478bd9Sstevel@tonic-gate * address.
5687c478bd9Sstevel@tonic-gate */
5697c478bd9Sstevel@tonic-gate if (stats && probe_all && npackets > 0 &&
5707c478bd9Sstevel@tonic-gate ((current_targetaddr->starting_seq_num +
5717c478bd9Sstevel@tonic-gate current_targetaddr->num_probes - 1) %
5727c478bd9Sstevel@tonic-gate (MAX_ICMP_SEQ + 1) ==
5737c478bd9Sstevel@tonic-gate ntohs(icp->icmp_seq)) &&
5747c478bd9Sstevel@tonic-gate (current_targetaddr->num_probes ==
5757c478bd9Sstevel@tonic-gate current_targetaddr->num_sent))
5767c478bd9Sstevel@tonic-gate last_reply_from_targetaddr = _B_TRUE;
5777c478bd9Sstevel@tonic-gate } else {
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate * If it's just probe_all and we just received
5807c478bd9Sstevel@tonic-gate * a reply from a target address we were
5817c478bd9Sstevel@tonic-gate * probing and had timed out (now we are probing
5827c478bd9Sstevel@tonic-gate * some other target address), we ignore
5837c478bd9Sstevel@tonic-gate * this reply.
5847c478bd9Sstevel@tonic-gate */
5857c478bd9Sstevel@tonic-gate if (probe_all && !stats) {
5867c478bd9Sstevel@tonic-gate valid_reply = _B_FALSE;
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate * Only if it's verbose, we get a
5897c478bd9Sstevel@tonic-gate * message regarding this reply,
5907c478bd9Sstevel@tonic-gate * otherwise we are done here.
5917c478bd9Sstevel@tonic-gate */
5927c478bd9Sstevel@tonic-gate if (!verbose) {
5937c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
5947c478bd9Sstevel@tonic-gate return;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate if (!stats && valid_reply) {
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate * if we are still sending to the same target address,
6037c478bd9Sstevel@tonic-gate * then stop it, because we know it's alive.
6047c478bd9Sstevel@tonic-gate */
6057c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
6067c478bd9Sstevel@tonic-gate (void) alarm(0); /* cancel alarm */
6077c478bd9Sstevel@tonic-gate (void) sigset(SIGALRM, SIG_IGN);
6087c478bd9Sstevel@tonic-gate current_targetaddr->probing_done = _B_TRUE;
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
6117c478bd9Sstevel@tonic-gate
6127c478bd9Sstevel@tonic-gate if (!probe_all) {
6137c478bd9Sstevel@tonic-gate Printf("%s is alive\n", targethost);
6147c478bd9Sstevel@tonic-gate } else {
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate * If we are using send_reply, the real
6177c478bd9Sstevel@tonic-gate * target address is not the src address of the
6187c478bd9Sstevel@tonic-gate * replies. Use icmp_seq to find out where this
6197c478bd9Sstevel@tonic-gate * probe was sent to.
6207c478bd9Sstevel@tonic-gate */
6217c478bd9Sstevel@tonic-gate if (send_reply) {
6227c478bd9Sstevel@tonic-gate (void) find_dstaddr(
6237c478bd9Sstevel@tonic-gate ntohs(icp->icmp_seq), &dst_addr);
6247c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET,
6257c478bd9Sstevel@tonic-gate (void *)&dst_addr.addr,
6267c478bd9Sstevel@tonic-gate tmp_buf, sizeof (tmp_buf));
6277c478bd9Sstevel@tonic-gate } else {
6287c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET,
6297c478bd9Sstevel@tonic-gate (void *)&from->sin_addr,
6307c478bd9Sstevel@tonic-gate tmp_buf, sizeof (tmp_buf));
6317c478bd9Sstevel@tonic-gate }
6327c478bd9Sstevel@tonic-gate if (nflag) {
6337c478bd9Sstevel@tonic-gate Printf("%s is alive\n", tmp_buf);
6347c478bd9Sstevel@tonic-gate } else {
6357c478bd9Sstevel@tonic-gate Printf("%s (%s) is alive\n",
6367c478bd9Sstevel@tonic-gate targethost, tmp_buf);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate * Let's get things going again, but now
6427c478bd9Sstevel@tonic-gate * ping will start sending to next target IP
6437c478bd9Sstevel@tonic-gate * address.
6447c478bd9Sstevel@tonic-gate */
6457c478bd9Sstevel@tonic-gate send_scheduled_probe();
6467c478bd9Sstevel@tonic-gate (void) sigset(SIGALRM, sigalrm_handler);
6477c478bd9Sstevel@tonic-gate schedule_sigalrm();
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate return;
6507c478bd9Sstevel@tonic-gate } else {
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate * If we are not moving to next targetaddr, let's
6537c478bd9Sstevel@tonic-gate * release the SIGALRM now. We don't want to stall in
6547c478bd9Sstevel@tonic-gate * the middle of probing a targetaddr if the pr_name()
6557c478bd9Sstevel@tonic-gate * call (see below) takes longer.
6567c478bd9Sstevel@tonic-gate */
6577c478bd9Sstevel@tonic-gate if (!last_reply_from_targetaddr)
6587c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
6597c478bd9Sstevel@tonic-gate /* else, we'll release it later */
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * If we are using send_reply, the real target address is
6637c478bd9Sstevel@tonic-gate * not the src address of the replies. Use icmp_seq to find out
6647c478bd9Sstevel@tonic-gate * where this probe was sent to.
6657c478bd9Sstevel@tonic-gate */
6667c478bd9Sstevel@tonic-gate if (send_reply) {
6677c478bd9Sstevel@tonic-gate (void) find_dstaddr(ntohs(icp->icmp_seq), &dst_addr);
6687c478bd9Sstevel@tonic-gate Printf("%d bytes from %s: ", cc,
6697c478bd9Sstevel@tonic-gate pr_name((char *)&dst_addr.addr, AF_INET));
6707c478bd9Sstevel@tonic-gate } else {
6717c478bd9Sstevel@tonic-gate Printf("%d bytes from %s: ", cc,
6727c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate Printf("icmp_seq=%d. ", ntohs(icp->icmp_seq));
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate if (valid_reply && datalen >= sizeof (struct timeval) &&
6777c478bd9Sstevel@tonic-gate cc_left >= sizeof (struct timeval)) {
6787c478bd9Sstevel@tonic-gate /* LINTED */
6797c478bd9Sstevel@tonic-gate tp = (struct timeval *)&icp->icmp_data[0];
6807c478bd9Sstevel@tonic-gate (void) tvsub(&tv, tp);
6817c478bd9Sstevel@tonic-gate triptime = (int64_t)tv.tv_sec * MICROSEC + tv.tv_usec;
6827c478bd9Sstevel@tonic-gate Printf("time=" TIMEFORMAT " ms", triptime/1000.0);
6837c478bd9Sstevel@tonic-gate tsum += triptime;
6847c478bd9Sstevel@tonic-gate tsum2 += triptime*triptime;
6857c478bd9Sstevel@tonic-gate if (triptime < tmin)
6867c478bd9Sstevel@tonic-gate tmin = triptime;
6877c478bd9Sstevel@tonic-gate if (triptime > tmax)
6887c478bd9Sstevel@tonic-gate tmax = triptime;
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate (void) putchar('\n');
6917c478bd9Sstevel@tonic-gate
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate * If it's stats, probe-all, npackets > 0, and we received reply
6947c478bd9Sstevel@tonic-gate * for the last probe sent to this target address, then we
6957c478bd9Sstevel@tonic-gate * don't need to wait anymore, let's move on to next target
6967c478bd9Sstevel@tonic-gate * address, now!
6977c478bd9Sstevel@tonic-gate */
6987c478bd9Sstevel@tonic-gate if (last_reply_from_targetaddr) {
6997c478bd9Sstevel@tonic-gate (void) alarm(0); /* cancel alarm */
7007c478bd9Sstevel@tonic-gate current_targetaddr->probing_done = _B_TRUE;
7017c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
7027c478bd9Sstevel@tonic-gate send_scheduled_probe();
7037c478bd9Sstevel@tonic-gate schedule_sigalrm();
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate break;
7067c478bd9Sstevel@tonic-gate
7077c478bd9Sstevel@tonic-gate case ICMP_SOURCEQUENCH:
7087c478bd9Sstevel@tonic-gate if (cc_left < sizeof (struct ip)) {
7097c478bd9Sstevel@tonic-gate if (verbose) {
7107c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n",
7117c478bd9Sstevel@tonic-gate cc, pr_name((char *)&from->sin_addr,
7127c478bd9Sstevel@tonic-gate AF_INET));
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate return;
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate ip = &icp->icmp_ip;
7177c478bd9Sstevel@tonic-gate hlen1 = ip->ip_hl << 2;
7187c478bd9Sstevel@tonic-gate dst_addr.addr = ip->ip_dst;
7197c478bd9Sstevel@tonic-gate if (is_a_target(ai_dst, &dst_addr) || verbose) {
7207c478bd9Sstevel@tonic-gate Printf("ICMP Source Quench from %s\n",
7217c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
7227c478bd9Sstevel@tonic-gate Printf(" for %s from %s", pr_protocol(ip->ip_p),
7237c478bd9Sstevel@tonic-gate pr_name((char *)&ip->ip_src, AF_INET));
7247c478bd9Sstevel@tonic-gate Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate * if it's a UDP or TCP packet, we need at least first
7287c478bd9Sstevel@tonic-gate * 4 bytes of it to see the src/dst ports
7297c478bd9Sstevel@tonic-gate */
7307c478bd9Sstevel@tonic-gate if ((ip->ip_p == IPPROTO_TCP ||
7317c478bd9Sstevel@tonic-gate ip->ip_p == IPPROTO_UDP) &&
7327c478bd9Sstevel@tonic-gate (cc_left >= hlen1 + 4)) {
7337c478bd9Sstevel@tonic-gate /* LINTED */
7347c478bd9Sstevel@tonic-gate up = (struct udphdr *)((uchar_t *)ip + hlen1);
7357c478bd9Sstevel@tonic-gate Printf(" port %d", ntohs(up->uh_dport));
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate (void) putchar('\n');
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate break;
7407c478bd9Sstevel@tonic-gate
7417c478bd9Sstevel@tonic-gate case ICMP_PARAMPROB:
7427c478bd9Sstevel@tonic-gate if (cc_left < sizeof (struct ip)) {
7437c478bd9Sstevel@tonic-gate if (verbose) {
7447c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n",
7457c478bd9Sstevel@tonic-gate cc, pr_name((char *)&from->sin_addr,
7467c478bd9Sstevel@tonic-gate AF_INET));
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate return;
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate ip = &icp->icmp_ip;
7517c478bd9Sstevel@tonic-gate hlen1 = ip->ip_hl << 2;
7527c478bd9Sstevel@tonic-gate dst_addr.addr = ip->ip_dst;
7537c478bd9Sstevel@tonic-gate if (is_a_target(ai_dst, &dst_addr) || verbose) {
7547c478bd9Sstevel@tonic-gate switch (icp->icmp_code) {
7557c478bd9Sstevel@tonic-gate case ICMP_PARAMPROB_OPTABSENT:
7567c478bd9Sstevel@tonic-gate Printf("ICMP Missing a Required Option "
7577c478bd9Sstevel@tonic-gate "parameter problem from %s\n",
7587c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
7597c478bd9Sstevel@tonic-gate Printf(" option type = %d", icp->icmp_pptr);
7607c478bd9Sstevel@tonic-gate break;
7617c478bd9Sstevel@tonic-gate case ICMP_PARAMPROB_BADLENGTH:
7627c478bd9Sstevel@tonic-gate Printf("ICMP Bad Length parameter problem "
7637c478bd9Sstevel@tonic-gate "from %s\n",
7647c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
7657c478bd9Sstevel@tonic-gate Printf(" in byte %d", icp->icmp_pptr);
7667c478bd9Sstevel@tonic-gate if (icp->icmp_pptr <= hlen1) {
7677c478bd9Sstevel@tonic-gate Printf(" (value 0x%x)",
7687c478bd9Sstevel@tonic-gate *((char *)ip + icp->icmp_pptr));
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate break;
7717c478bd9Sstevel@tonic-gate case 0:
7727c478bd9Sstevel@tonic-gate default:
7737c478bd9Sstevel@tonic-gate Printf("ICMP Parameter Problem from %s\n",
7747c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
7757c478bd9Sstevel@tonic-gate Printf(" in byte %d", icp->icmp_pptr);
7767c478bd9Sstevel@tonic-gate if (icp->icmp_pptr <= hlen1) {
7777c478bd9Sstevel@tonic-gate Printf(" (value 0x%x)",
7787c478bd9Sstevel@tonic-gate *((char *)ip + icp->icmp_pptr));
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate break;
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate
7837c478bd9Sstevel@tonic-gate Printf(" for %s from %s", pr_protocol(ip->ip_p),
7847c478bd9Sstevel@tonic-gate pr_name((char *)&ip->ip_src, AF_INET));
7857c478bd9Sstevel@tonic-gate Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
7867c478bd9Sstevel@tonic-gate
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate * if it's a UDP or TCP packet, we need at least first
7897c478bd9Sstevel@tonic-gate * 4 bytes of it to see the src/dst ports
7907c478bd9Sstevel@tonic-gate */
7917c478bd9Sstevel@tonic-gate if ((ip->ip_p == IPPROTO_TCP ||
7927c478bd9Sstevel@tonic-gate ip->ip_p == IPPROTO_UDP) &&
7937c478bd9Sstevel@tonic-gate (cc_left >= hlen1 + 4)) {
7947c478bd9Sstevel@tonic-gate /* LINTED */
7957c478bd9Sstevel@tonic-gate up = (struct udphdr *)((uchar_t *)ip + hlen1);
7967c478bd9Sstevel@tonic-gate Printf(" port %d", ntohs(up->uh_dport));
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate (void) putchar('\n');
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate break;
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate case ICMP_TIMXCEED:
8037c478bd9Sstevel@tonic-gate if (cc_left < sizeof (struct ip)) {
8047c478bd9Sstevel@tonic-gate if (verbose) {
8057c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n",
8067c478bd9Sstevel@tonic-gate cc, pr_name((char *)&from->sin_addr,
8077c478bd9Sstevel@tonic-gate AF_INET));
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate return;
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate ip = &icp->icmp_ip;
8127c478bd9Sstevel@tonic-gate hlen1 = ip->ip_hl << 2;
8137c478bd9Sstevel@tonic-gate dst_addr.addr = ip->ip_dst;
8147c478bd9Sstevel@tonic-gate if (is_a_target(ai_dst, &dst_addr) || verbose) {
8157c478bd9Sstevel@tonic-gate if (icp->icmp_code >= A_CNT(timexceed)) {
8167c478bd9Sstevel@tonic-gate Printf("ICMP %d time exceeded from %s\n",
8177c478bd9Sstevel@tonic-gate icp->icmp_code,
8187c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
8197c478bd9Sstevel@tonic-gate } else {
8207c478bd9Sstevel@tonic-gate Printf("ICMP %s from %s\n",
8217c478bd9Sstevel@tonic-gate timexceed[icp->icmp_code],
8227c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate Printf(" for %s from %s", pr_protocol(ip->ip_p),
8257c478bd9Sstevel@tonic-gate pr_name((char *)&ip->ip_src, AF_INET));
8267c478bd9Sstevel@tonic-gate Printf(" to %s", pr_name((char *)&ip->ip_dst, AF_INET));
8277c478bd9Sstevel@tonic-gate if ((ip->ip_p == IPPROTO_TCP ||
8287c478bd9Sstevel@tonic-gate ip->ip_p == IPPROTO_UDP) &&
8297c478bd9Sstevel@tonic-gate (cc_left >= hlen1 + 4)) {
8307c478bd9Sstevel@tonic-gate /* LINTED */
8317c478bd9Sstevel@tonic-gate up = (struct udphdr *)((uchar_t *)ip + hlen1);
8327c478bd9Sstevel@tonic-gate Printf(" port %d", ntohs(up->uh_dport));
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate (void) putchar('\n');
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate break;
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate case ICMP_TSTAMPREPLY:
8397c478bd9Sstevel@tonic-gate /* the packet should have enough space to store timestamps */
8407c478bd9Sstevel@tonic-gate if (cc_left < sizeof (struct id_ts)) {
8417c478bd9Sstevel@tonic-gate if (verbose) {
8427c478bd9Sstevel@tonic-gate Printf("packet too short (%d bytes) from %s\n",
8437c478bd9Sstevel@tonic-gate cc, pr_name((char *)&from->sin_addr,
8447c478bd9Sstevel@tonic-gate AF_INET));
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate return;
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate if (ntohs(icp->icmp_id) == ident) {
8507c478bd9Sstevel@tonic-gate if (use_icmp_ts)
8517c478bd9Sstevel@tonic-gate valid_reply = _B_TRUE;
8527c478bd9Sstevel@tonic-gate else
8537c478bd9Sstevel@tonic-gate valid_reply = _B_FALSE;
8547c478bd9Sstevel@tonic-gate } else {
8557c478bd9Sstevel@tonic-gate return;
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate if (valid_reply) {
8597c478bd9Sstevel@tonic-gate /*
8607c478bd9Sstevel@tonic-gate * For this valid reply, if we are still sending to
8617c478bd9Sstevel@tonic-gate * this target IP address, we'd like to do some
8627c478bd9Sstevel@tonic-gate * updates to targetaddr, so hold SIGALRMs.
8637c478bd9Sstevel@tonic-gate */
8647c478bd9Sstevel@tonic-gate (void) sighold(SIGALRM);
8657c478bd9Sstevel@tonic-gate is_alive = _B_TRUE;
8667c478bd9Sstevel@tonic-gate nreceived++;
8677c478bd9Sstevel@tonic-gate reply_matched_current_target =
8687c478bd9Sstevel@tonic-gate seq_match(current_targetaddr->starting_seq_num,
869*b08923d6SRobert Mustacchi current_targetaddr->num_sent,
870*b08923d6SRobert Mustacchi ntohs(icp->icmp_seq));
8717c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
8727c478bd9Sstevel@tonic-gate current_targetaddr->got_reply = _B_TRUE;
8737c478bd9Sstevel@tonic-gate nreceived_last_target++;
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate * Determine if stats, probe-all, and
8767c478bd9Sstevel@tonic-gate * npackets != 0, and this is the reply for
8777c478bd9Sstevel@tonic-gate * the last probe we sent to current target
8787c478bd9Sstevel@tonic-gate * address.
8797c478bd9Sstevel@tonic-gate */
8807c478bd9Sstevel@tonic-gate if (stats && probe_all && npackets > 0 &&
8817c478bd9Sstevel@tonic-gate ((current_targetaddr->starting_seq_num +
8827c478bd9Sstevel@tonic-gate current_targetaddr->num_probes - 1) %
8837c478bd9Sstevel@tonic-gate (MAX_ICMP_SEQ + 1) ==
8847c478bd9Sstevel@tonic-gate ntohs(icp->icmp_seq)) &&
8857c478bd9Sstevel@tonic-gate (current_targetaddr->num_probes ==
8867c478bd9Sstevel@tonic-gate current_targetaddr->num_sent))
8877c478bd9Sstevel@tonic-gate last_reply_from_targetaddr = _B_TRUE;
8887c478bd9Sstevel@tonic-gate } else {
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate * If it's just probe_all and we just received
8917c478bd9Sstevel@tonic-gate * a reply from a target address we were
8927c478bd9Sstevel@tonic-gate * probing and had timed out (now we are probing
8937c478bd9Sstevel@tonic-gate * some other target address), we ignore
8947c478bd9Sstevel@tonic-gate * this reply.
8957c478bd9Sstevel@tonic-gate */
8967c478bd9Sstevel@tonic-gate if (probe_all && !stats) {
8977c478bd9Sstevel@tonic-gate valid_reply = _B_FALSE;
8987c478bd9Sstevel@tonic-gate /*
8997c478bd9Sstevel@tonic-gate * Only if it's verbose, we get a
9007c478bd9Sstevel@tonic-gate * message regarding this reply,
9017c478bd9Sstevel@tonic-gate * otherwise we are done here.
9027c478bd9Sstevel@tonic-gate */
9037c478bd9Sstevel@tonic-gate if (!verbose) {
9047c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
9057c478bd9Sstevel@tonic-gate return;
9067c478bd9Sstevel@tonic-gate }
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate if (!stats && valid_reply) {
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate * if we are still sending to the same target address,
9147c478bd9Sstevel@tonic-gate * then stop it, because we know it's alive.
9157c478bd9Sstevel@tonic-gate */
9167c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
9177c478bd9Sstevel@tonic-gate (void) alarm(0); /* cancel alarm */
9187c478bd9Sstevel@tonic-gate (void) sigset(SIGALRM, SIG_IGN);
9197c478bd9Sstevel@tonic-gate current_targetaddr->probing_done = _B_TRUE;
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
9227c478bd9Sstevel@tonic-gate
9237c478bd9Sstevel@tonic-gate if (!probe_all) {
9247c478bd9Sstevel@tonic-gate Printf("%s is alive\n", targethost);
9257c478bd9Sstevel@tonic-gate } else {
9267c478bd9Sstevel@tonic-gate /*
9277c478bd9Sstevel@tonic-gate * If we are using send_reply, the real
9287c478bd9Sstevel@tonic-gate * target address is not the src address of the
9297c478bd9Sstevel@tonic-gate * replies. Use icmp_seq to find out where this
9307c478bd9Sstevel@tonic-gate * probe was sent to.
9317c478bd9Sstevel@tonic-gate */
9327c478bd9Sstevel@tonic-gate if (send_reply) {
9337c478bd9Sstevel@tonic-gate (void) find_dstaddr(
9347c478bd9Sstevel@tonic-gate ntohs(icp->icmp_seq), &dst_addr);
9357c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET,
9367c478bd9Sstevel@tonic-gate (void *)&dst_addr.addr,
9377c478bd9Sstevel@tonic-gate tmp_buf, sizeof (tmp_buf));
9387c478bd9Sstevel@tonic-gate } else {
9397c478bd9Sstevel@tonic-gate (void) inet_ntop(AF_INET,
9407c478bd9Sstevel@tonic-gate (void *)&from->sin_addr,
9417c478bd9Sstevel@tonic-gate tmp_buf, sizeof (tmp_buf));
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate if (nflag) {
9447c478bd9Sstevel@tonic-gate Printf("%s is alive\n", tmp_buf);
9457c478bd9Sstevel@tonic-gate } else {
9467c478bd9Sstevel@tonic-gate Printf("%s (%s) is alive\n",
9477c478bd9Sstevel@tonic-gate targethost, tmp_buf);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate if (reply_matched_current_target) {
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate * Let's get things going again, but now
9537c478bd9Sstevel@tonic-gate * ping will start sending to next target IP
9547c478bd9Sstevel@tonic-gate * address.
9557c478bd9Sstevel@tonic-gate */
9567c478bd9Sstevel@tonic-gate send_scheduled_probe();
9577c478bd9Sstevel@tonic-gate (void) sigset(SIGALRM, sigalrm_handler);
9587c478bd9Sstevel@tonic-gate schedule_sigalrm();
9597c478bd9Sstevel@tonic-gate }
9607c478bd9Sstevel@tonic-gate return;
9617c478bd9Sstevel@tonic-gate } else {
9627c478bd9Sstevel@tonic-gate /*
9637c478bd9Sstevel@tonic-gate * If we are not moving to next targetaddr, let's
9647c478bd9Sstevel@tonic-gate * release the SIGALRM now. We don't want to stall in
9657c478bd9Sstevel@tonic-gate * the middle of probing a targetaddr if the pr_name()
9667c478bd9Sstevel@tonic-gate * call (see below) takes longer.
9677c478bd9Sstevel@tonic-gate */
9687c478bd9Sstevel@tonic-gate if (!last_reply_from_targetaddr)
9697c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
9707c478bd9Sstevel@tonic-gate /* else, we'll release it later */
9717c478bd9Sstevel@tonic-gate }
9727c478bd9Sstevel@tonic-gate
9737c478bd9Sstevel@tonic-gate /*
9747c478bd9Sstevel@tonic-gate * If we are using send_reply, the real target address is
9757c478bd9Sstevel@tonic-gate * not the src address of the replies. Use icmp_seq to find out
9767c478bd9Sstevel@tonic-gate * where this probe was sent to.
9777c478bd9Sstevel@tonic-gate */
9787c478bd9Sstevel@tonic-gate if (send_reply) {
9797c478bd9Sstevel@tonic-gate (void) find_dstaddr(ntohs(icp->icmp_seq), &dst_addr);
9807c478bd9Sstevel@tonic-gate Printf("%d bytes from %s: ", cc,
9817c478bd9Sstevel@tonic-gate pr_name((char *)&dst_addr.addr, AF_INET));
9827c478bd9Sstevel@tonic-gate } else {
9837c478bd9Sstevel@tonic-gate Printf("%d bytes from %s: ", cc,
9847c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate Printf("icmp_seq=%d. ", ntohs(icp->icmp_seq));
9877c478bd9Sstevel@tonic-gate Printf("orig = %lu, recv = %lu, xmit = %lu ",
9887c478bd9Sstevel@tonic-gate (ulong_t)ntohl(icp->icmp_otime),
9897c478bd9Sstevel@tonic-gate (ulong_t)ntohl(icp->icmp_rtime),
9907c478bd9Sstevel@tonic-gate (ulong_t)ntohl(icp->icmp_ttime));
9917c478bd9Sstevel@tonic-gate
9927c478bd9Sstevel@tonic-gate if (valid_reply) {
9937c478bd9Sstevel@tonic-gate /*
9947c478bd9Sstevel@tonic-gate * icp->icmp_otime is the time passed since midnight.
9957c478bd9Sstevel@tonic-gate * Therefore we need to adjust tv value, which is
9967c478bd9Sstevel@tonic-gate * the time passed since Jan 1, 1970.
9977c478bd9Sstevel@tonic-gate */
9987c478bd9Sstevel@tonic-gate triptime = (tv.tv_sec % (24LL * 60 * 60)) * MILLISEC +
9997c478bd9Sstevel@tonic-gate (tv.tv_usec / (MICROSEC/MILLISEC));
10007c478bd9Sstevel@tonic-gate triptime -= ntohl(icp->icmp_otime);
10017c478bd9Sstevel@tonic-gate if (triptime < 0)
10027c478bd9Sstevel@tonic-gate triptime += 24LL * 60 * 60 * MILLISEC;
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate Printf("time=%d. ms", (int)triptime);
10057c478bd9Sstevel@tonic-gate triptime *= (MICROSEC/MILLISEC);
10067c478bd9Sstevel@tonic-gate tsum += triptime;
10077c478bd9Sstevel@tonic-gate tsum2 += triptime*triptime;
10087c478bd9Sstevel@tonic-gate if (triptime < tmin)
10097c478bd9Sstevel@tonic-gate tmin = triptime;
10107c478bd9Sstevel@tonic-gate if (triptime > tmax)
10117c478bd9Sstevel@tonic-gate tmax = triptime;
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate (void) putchar('\n');
10147c478bd9Sstevel@tonic-gate /*
10157c478bd9Sstevel@tonic-gate * If it's stats, probe-all, npackets > 0, and we received reply
10167c478bd9Sstevel@tonic-gate * for the last probe sent to this target address, then we
10177c478bd9Sstevel@tonic-gate * don't need to wait anymore, let's move on to next target
10187c478bd9Sstevel@tonic-gate * address, now!
10197c478bd9Sstevel@tonic-gate */
10207c478bd9Sstevel@tonic-gate if (last_reply_from_targetaddr) {
10217c478bd9Sstevel@tonic-gate (void) alarm(0); /* cancel alarm */
10227c478bd9Sstevel@tonic-gate current_targetaddr->probing_done = _B_TRUE;
10237c478bd9Sstevel@tonic-gate (void) sigrelse(SIGALRM);
10247c478bd9Sstevel@tonic-gate send_scheduled_probe();
10257c478bd9Sstevel@tonic-gate schedule_sigalrm();
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate break;
10287c478bd9Sstevel@tonic-gate case ICMP_ROUTERADVERT:
10297c478bd9Sstevel@tonic-gate case ICMP_ROUTERSOLICIT:
10307c478bd9Sstevel@tonic-gate /* Router discovery messages */
10317c478bd9Sstevel@tonic-gate return;
10327c478bd9Sstevel@tonic-gate
10337c478bd9Sstevel@tonic-gate case ICMP_ECHO:
10347c478bd9Sstevel@tonic-gate case ICMP_TSTAMP:
10357c478bd9Sstevel@tonic-gate case ICMP_IREQ:
10367c478bd9Sstevel@tonic-gate case ICMP_MASKREQ:
10377c478bd9Sstevel@tonic-gate /* These were never passed out from the SunOS 4.X kernel. */
10387c478bd9Sstevel@tonic-gate return;
10397c478bd9Sstevel@tonic-gate
10407c478bd9Sstevel@tonic-gate case ICMP_IREQREPLY:
10417c478bd9Sstevel@tonic-gate case ICMP_MASKREPLY:
10427c478bd9Sstevel@tonic-gate /* Replies for information and address mask requests */
10437c478bd9Sstevel@tonic-gate return;
10447c478bd9Sstevel@tonic-gate
10457c478bd9Sstevel@tonic-gate default:
10467c478bd9Sstevel@tonic-gate if (verbose) {
10477c478bd9Sstevel@tonic-gate Printf("%d bytes from %s:\n", cc,
10487c478bd9Sstevel@tonic-gate pr_name((char *)&from->sin_addr, AF_INET));
10497c478bd9Sstevel@tonic-gate Printf("icmp_type=%d (%s) ",
10507c478bd9Sstevel@tonic-gate icp->icmp_type, pr_type(icp->icmp_type));
10517c478bd9Sstevel@tonic-gate Printf("icmp_code=%d\n", icp->icmp_code);
10527c478bd9Sstevel@tonic-gate for (i = 0; i < 12; i++) {
10537c478bd9Sstevel@tonic-gate Printf("x%2.2x: x%8.8x\n",
10547c478bd9Sstevel@tonic-gate i * sizeof (int32_t), *intp++);
10557c478bd9Sstevel@tonic-gate }
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate break;
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate
10607c478bd9Sstevel@tonic-gate buf += sizeof (struct ip);
10617c478bd9Sstevel@tonic-gate hlen -= sizeof (struct ip);
10627c478bd9Sstevel@tonic-gate
10637c478bd9Sstevel@tonic-gate /* if verbose and there exists IP options */
10647c478bd9Sstevel@tonic-gate if (verbose && hlen > 0)
10657c478bd9Sstevel@tonic-gate pr_options((uchar_t *)buf, hlen);
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate
10687c478bd9Sstevel@tonic-gate /*
10697c478bd9Sstevel@tonic-gate * Print out the ip options.
10707c478bd9Sstevel@tonic-gate */
10717c478bd9Sstevel@tonic-gate static void
pr_options(uchar_t * opt,int optlength)10727c478bd9Sstevel@tonic-gate pr_options(uchar_t *opt, int optlength)
10737c478bd9Sstevel@tonic-gate {
10747c478bd9Sstevel@tonic-gate int curlength;
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate Printf(" IP options: ");
10777c478bd9Sstevel@tonic-gate while (optlength > 0) {
10787c478bd9Sstevel@tonic-gate curlength = opt[1];
10797c478bd9Sstevel@tonic-gate switch (*opt) {
10807c478bd9Sstevel@tonic-gate case IPOPT_EOL:
10817c478bd9Sstevel@tonic-gate optlength = 0;
10827c478bd9Sstevel@tonic-gate break;
10837c478bd9Sstevel@tonic-gate
10847c478bd9Sstevel@tonic-gate case IPOPT_NOP:
10857c478bd9Sstevel@tonic-gate opt++;
10867c478bd9Sstevel@tonic-gate optlength--;
10877c478bd9Sstevel@tonic-gate continue;
10887c478bd9Sstevel@tonic-gate
10897c478bd9Sstevel@tonic-gate case IPOPT_RR:
10907c478bd9Sstevel@tonic-gate Printf(" <record route> ");
10917c478bd9Sstevel@tonic-gate pr_rropt(opt, curlength, _B_TRUE);
10927c478bd9Sstevel@tonic-gate break;
10937c478bd9Sstevel@tonic-gate
10947c478bd9Sstevel@tonic-gate case IPOPT_TS:
10957c478bd9Sstevel@tonic-gate Printf(" <time stamp> ");
10967c478bd9Sstevel@tonic-gate pr_tsopt(opt, curlength);
10977c478bd9Sstevel@tonic-gate break;
10987c478bd9Sstevel@tonic-gate
10997c478bd9Sstevel@tonic-gate case IPOPT_SECURITY:
11007c478bd9Sstevel@tonic-gate Printf(" <security>");
11017c478bd9Sstevel@tonic-gate break;
11027c478bd9Sstevel@tonic-gate
11037c478bd9Sstevel@tonic-gate case IPOPT_LSRR:
11047c478bd9Sstevel@tonic-gate Printf(" <loose source route> ");
11057c478bd9Sstevel@tonic-gate pr_rropt(opt, curlength, _B_FALSE);
11067c478bd9Sstevel@tonic-gate break;
11077c478bd9Sstevel@tonic-gate
11087c478bd9Sstevel@tonic-gate case IPOPT_SATID:
11097c478bd9Sstevel@tonic-gate Printf(" <stream id>");
11107c478bd9Sstevel@tonic-gate break;
11117c478bd9Sstevel@tonic-gate
11127c478bd9Sstevel@tonic-gate case IPOPT_SSRR:
11137c478bd9Sstevel@tonic-gate Printf(" <strict source route> ");
11147c478bd9Sstevel@tonic-gate pr_rropt(opt, curlength, _B_FALSE);
11157c478bd9Sstevel@tonic-gate break;
11167c478bd9Sstevel@tonic-gate
11177c478bd9Sstevel@tonic-gate default:
11187c478bd9Sstevel@tonic-gate Printf(" <option %d, len %d>", *opt, curlength);
11197c478bd9Sstevel@tonic-gate break;
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate * Following most options comes a length field
11237c478bd9Sstevel@tonic-gate */
11247c478bd9Sstevel@tonic-gate opt += curlength;
11257c478bd9Sstevel@tonic-gate optlength -= curlength;
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate (void) putchar('\n');
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate * Print out a recorded route option. If rrflag is _B_TRUE, it prints record
11327c478bd9Sstevel@tonic-gate * route option, otherwise LSRR/SSRR.
11337c478bd9Sstevel@tonic-gate */
11347c478bd9Sstevel@tonic-gate static void
pr_rropt(uchar_t * opt,int length,boolean_t rrflag)11357c478bd9Sstevel@tonic-gate pr_rropt(uchar_t *opt, int length, boolean_t rrflag)
11367c478bd9Sstevel@tonic-gate {
11377c478bd9Sstevel@tonic-gate struct ip_sourceroute *rrp;
11387c478bd9Sstevel@tonic-gate int sr_index = 0;
11397c478bd9Sstevel@tonic-gate struct in_addr addr;
11407c478bd9Sstevel@tonic-gate
11417c478bd9Sstevel@tonic-gate rrp = (struct ip_sourceroute *)opt;
11427c478bd9Sstevel@tonic-gate
11437c478bd9Sstevel@tonic-gate /* data starts at offset 3 */
11447c478bd9Sstevel@tonic-gate length -= 3;
11457c478bd9Sstevel@tonic-gate while (length > 0) {
11467c478bd9Sstevel@tonic-gate /*
11477c478bd9Sstevel@tonic-gate * Let's see if we are examining the addr pointed by ipsr_ptr
11487c478bd9Sstevel@tonic-gate */
11497c478bd9Sstevel@tonic-gate if ((rrp->ipsr_ptr == (sr_index + 1) * sizeof (addr)) &&
11507c478bd9Sstevel@tonic-gate rrflag) {
11517c478bd9Sstevel@tonic-gate Printf(" (End of record)");
11527c478bd9Sstevel@tonic-gate break;
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate
11557c478bd9Sstevel@tonic-gate bcopy(&rrp->ipsr_addrs[sr_index], &addr, sizeof (addr));
11567c478bd9Sstevel@tonic-gate Printf("%s", pr_name((char *)&addr, AF_INET));
11577c478bd9Sstevel@tonic-gate
11587c478bd9Sstevel@tonic-gate if (rrp->ipsr_ptr == (sr_index + 1) * sizeof (addr)) {
11597c478bd9Sstevel@tonic-gate Printf("(Current)");
11607c478bd9Sstevel@tonic-gate }
11617c478bd9Sstevel@tonic-gate
11627c478bd9Sstevel@tonic-gate sr_index++;
11637c478bd9Sstevel@tonic-gate
11647c478bd9Sstevel@tonic-gate length -= sizeof (addr);
11657c478bd9Sstevel@tonic-gate if (length > 0)
11667c478bd9Sstevel@tonic-gate Printf(", ");
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate
11707c478bd9Sstevel@tonic-gate /*
11717c478bd9Sstevel@tonic-gate * Print out a timestamp option.
11727c478bd9Sstevel@tonic-gate */
11737c478bd9Sstevel@tonic-gate static void
pr_tsopt(uchar_t * opt,int length)11747c478bd9Sstevel@tonic-gate pr_tsopt(uchar_t *opt, int length)
11757c478bd9Sstevel@tonic-gate {
11767c478bd9Sstevel@tonic-gate boolean_t address_present;
11777c478bd9Sstevel@tonic-gate boolean_t rrflag; /* End at current entry? */
11787c478bd9Sstevel@tonic-gate struct ip_timestamp *tsp;
11797c478bd9Sstevel@tonic-gate int ts_index = 0;
11807c478bd9Sstevel@tonic-gate struct in_addr addr;
11817c478bd9Sstevel@tonic-gate size_t data_len;
11827c478bd9Sstevel@tonic-gate int32_t time;
11837c478bd9Sstevel@tonic-gate
11847c478bd9Sstevel@tonic-gate /* LINTED */
11857c478bd9Sstevel@tonic-gate tsp = (struct ip_timestamp *)opt;
11867c478bd9Sstevel@tonic-gate
11877c478bd9Sstevel@tonic-gate switch (tsp->ipt_flg) {
11887c478bd9Sstevel@tonic-gate case IPOPT_TS_TSONLY:
11897c478bd9Sstevel@tonic-gate address_present = _B_FALSE;
11907c478bd9Sstevel@tonic-gate data_len = sizeof (tsp->ipt_timestamp.ipt_time[0]);
11917c478bd9Sstevel@tonic-gate rrflag = _B_TRUE;
11927c478bd9Sstevel@tonic-gate break;
11937c478bd9Sstevel@tonic-gate case IPOPT_TS_TSANDADDR:
11947c478bd9Sstevel@tonic-gate address_present = _B_TRUE;
11957c478bd9Sstevel@tonic-gate data_len = sizeof (tsp->ipt_timestamp.ipt_ta[0]);
11967c478bd9Sstevel@tonic-gate rrflag = _B_TRUE;
11977c478bd9Sstevel@tonic-gate break;
11987c478bd9Sstevel@tonic-gate case IPOPT_TS_PRESPEC:
11997c478bd9Sstevel@tonic-gate case 3:
12007c478bd9Sstevel@tonic-gate address_present = _B_TRUE;
12017c478bd9Sstevel@tonic-gate data_len = sizeof (tsp->ipt_timestamp.ipt_ta[0]);
12027c478bd9Sstevel@tonic-gate rrflag = _B_FALSE;
12037c478bd9Sstevel@tonic-gate break;
12047c478bd9Sstevel@tonic-gate default:
12057c478bd9Sstevel@tonic-gate Printf("(Bad flag value: 0x%x)", tsp->ipt_flg);
12067c478bd9Sstevel@tonic-gate return;
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate if (tsp->ipt_oflw > 0)
12097c478bd9Sstevel@tonic-gate Printf("(Overflow: %d) ", tsp->ipt_oflw);
12107c478bd9Sstevel@tonic-gate
12117c478bd9Sstevel@tonic-gate /* data starts at offset 4 */
12127c478bd9Sstevel@tonic-gate length -= 4;
12137c478bd9Sstevel@tonic-gate
12147c478bd9Sstevel@tonic-gate while (length > 0) {
12157c478bd9Sstevel@tonic-gate if (length < data_len)
12167c478bd9Sstevel@tonic-gate break;
12177c478bd9Sstevel@tonic-gate
12187c478bd9Sstevel@tonic-gate /* the minimum value of ipt_ptr is 5 */
12197c478bd9Sstevel@tonic-gate if ((tsp->ipt_ptr == ts_index * data_len + 5) && rrflag) {
12207c478bd9Sstevel@tonic-gate Printf(" (End of record)");
12217c478bd9Sstevel@tonic-gate break;
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate if (address_present) {
12247c478bd9Sstevel@tonic-gate bcopy(&tsp->ipt_timestamp.ipt_ta[ts_index].ipt_addr,
12257c478bd9Sstevel@tonic-gate &addr, sizeof (addr));
12267c478bd9Sstevel@tonic-gate Printf("%s: ", pr_name((char *)&addr, AF_INET));
12277c478bd9Sstevel@tonic-gate bcopy(&tsp->ipt_timestamp.ipt_ta[ts_index].ipt_time,
12287c478bd9Sstevel@tonic-gate &time, sizeof (time));
12297c478bd9Sstevel@tonic-gate } else {
12307c478bd9Sstevel@tonic-gate bcopy(&tsp->ipt_timestamp.ipt_time[ts_index],
12317c478bd9Sstevel@tonic-gate &time, sizeof (time));
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate Printf("%d", ntohl(time));
12347c478bd9Sstevel@tonic-gate
12357c478bd9Sstevel@tonic-gate if (tsp->ipt_ptr == ts_index * data_len + 5)
12367c478bd9Sstevel@tonic-gate Printf("(Current)");
12377c478bd9Sstevel@tonic-gate
12387c478bd9Sstevel@tonic-gate ts_index++;
12397c478bd9Sstevel@tonic-gate length -= data_len;
12407c478bd9Sstevel@tonic-gate if (length > 0)
12417c478bd9Sstevel@tonic-gate Printf(", ");
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate }
12447c478bd9Sstevel@tonic-gate
12457c478bd9Sstevel@tonic-gate /*
12467c478bd9Sstevel@tonic-gate * Convert an ICMP "type" field to a printable string.
12477c478bd9Sstevel@tonic-gate */
12487c478bd9Sstevel@tonic-gate static char *
pr_type(int icmp_type)12497c478bd9Sstevel@tonic-gate pr_type(int icmp_type)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate static struct icmptype_table ttab[] = {
12527c478bd9Sstevel@tonic-gate {ICMP_ECHOREPLY, "Echo Reply"},
12537c478bd9Sstevel@tonic-gate {1, "ICMP 1"},
12547c478bd9Sstevel@tonic-gate {2, "ICMP 2"},
12557c478bd9Sstevel@tonic-gate {ICMP_UNREACH, "Dest Unreachable"},
12567c478bd9Sstevel@tonic-gate {ICMP_SOURCEQUENCH, "Source Quench"},
12577c478bd9Sstevel@tonic-gate {ICMP_REDIRECT, "Redirect"},
12587c478bd9Sstevel@tonic-gate {6, "ICMP 6"},
12597c478bd9Sstevel@tonic-gate {7, "ICMP 7"},
12607c478bd9Sstevel@tonic-gate {ICMP_ECHO, "Echo"},
12617c478bd9Sstevel@tonic-gate {ICMP_ROUTERADVERT, "Router Advertisement"},
12627c478bd9Sstevel@tonic-gate {ICMP_ROUTERSOLICIT, "Router Solicitation"},
12637c478bd9Sstevel@tonic-gate {ICMP_TIMXCEED, "Time Exceeded"},
12647c478bd9Sstevel@tonic-gate {ICMP_PARAMPROB, "Parameter Problem"},
12657c478bd9Sstevel@tonic-gate {ICMP_TSTAMP, "Timestamp"},
12667c478bd9Sstevel@tonic-gate {ICMP_TSTAMPREPLY, "Timestamp Reply"},
12677c478bd9Sstevel@tonic-gate {ICMP_IREQ, "Info Request"},
12687c478bd9Sstevel@tonic-gate {ICMP_IREQREPLY, "Info Reply"},
12697c478bd9Sstevel@tonic-gate {ICMP_MASKREQ, "Netmask Request"},
12707c478bd9Sstevel@tonic-gate {ICMP_MASKREPLY, "Netmask Reply"}
12717c478bd9Sstevel@tonic-gate };
12727c478bd9Sstevel@tonic-gate int i;
12737c478bd9Sstevel@tonic-gate
12747c478bd9Sstevel@tonic-gate for (i = 0; i < A_CNT(ttab); i++) {
12757c478bd9Sstevel@tonic-gate if (ttab[i].type == icmp_type)
12767c478bd9Sstevel@tonic-gate return (ttab[i].message);
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate
12797c478bd9Sstevel@tonic-gate return ("OUT-OF-RANGE");
12807c478bd9Sstevel@tonic-gate }
1281