xref: /titanic_44/usr/src/cmd/cmd-inet/usr.sbin/ping/ping_aux6.c (revision 0406ceaaee37395222cc0d87df03b940c8946a5c)
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 /*
23*0406ceaaSmeem  * 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 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <strings.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <signal.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/time.h>
467c478bd9Sstevel@tonic-gate #include <sys/param.h>
477c478bd9Sstevel@tonic-gate #include <sys/socket.h>
487c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
497c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
507c478bd9Sstevel@tonic-gate #include <sys/file.h>
517c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
527c478bd9Sstevel@tonic-gate #include <net/if.h>
537c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
547c478bd9Sstevel@tonic-gate #include <netinet/in.h>
557c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
567c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
577c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
587c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
597c478bd9Sstevel@tonic-gate #include <netinet/udp.h>
607c478bd9Sstevel@tonic-gate #include <netdb.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate 
63*0406ceaaSmeem #include <libinetutil.h>
647c478bd9Sstevel@tonic-gate #include "ping.h"
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate void check_reply6(struct addrinfo *, struct msghdr *, int, ushort_t);
677c478bd9Sstevel@tonic-gate extern void find_dstaddr(ushort_t, union any_in_addr *);
687c478bd9Sstevel@tonic-gate static int IPv6_hdrlen(ip6_t *, int, uint8_t *);
697c478bd9Sstevel@tonic-gate extern boolean_t is_a_target(struct addrinfo *, union any_in_addr *);
707c478bd9Sstevel@tonic-gate static void pr_ext_headers(struct msghdr *);
717c478bd9Sstevel@tonic-gate extern char *pr_name(char *, int);
727c478bd9Sstevel@tonic-gate extern char *pr_protocol(int);
737c478bd9Sstevel@tonic-gate static void pr_rthdr(unsigned char *);
747c478bd9Sstevel@tonic-gate static char *pr_type6(uchar_t);
757c478bd9Sstevel@tonic-gate extern void schedule_sigalrm();
767c478bd9Sstevel@tonic-gate extern void send_scheduled_probe();
777c478bd9Sstevel@tonic-gate extern boolean_t seq_match(ushort_t, int, ushort_t);
787c478bd9Sstevel@tonic-gate void set_ancillary_data(struct msghdr *, int, union any_in_addr *, int, uint_t);
797c478bd9Sstevel@tonic-gate extern void sigalrm_handler();
807c478bd9Sstevel@tonic-gate extern void tvsub(struct timeval *, struct timeval *);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Initialize the msghdr for specifying the hoplimit, outgoing interface and
857c478bd9Sstevel@tonic-gate  * routing header.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate void
set_ancillary_data(struct msghdr * msgp,int hoplimit,union any_in_addr * gwIPlist,int gw_cnt,uint_t if_index)887c478bd9Sstevel@tonic-gate set_ancillary_data(struct msghdr *msgp, int hoplimit,
897c478bd9Sstevel@tonic-gate     union any_in_addr *gwIPlist, int gw_cnt, uint_t if_index)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	size_t hoplimit_space;
927c478bd9Sstevel@tonic-gate 	size_t rthdr_space;
937c478bd9Sstevel@tonic-gate 	size_t pktinfo_space;
947c478bd9Sstevel@tonic-gate 	size_t bufspace;
957c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsgp;
967c478bd9Sstevel@tonic-gate 	uchar_t *cmsg_datap;
977c478bd9Sstevel@tonic-gate 	static boolean_t first = _B_TRUE;
987c478bd9Sstevel@tonic-gate 	int i;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (hoplimit == -1 && gw_cnt == 0 && if_index == 0)
1017c478bd9Sstevel@tonic-gate 		return;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * Need to figure out size of buffer needed for ancillary data
1057c478bd9Sstevel@tonic-gate 	 * containing routing header and packet info options.
1067c478bd9Sstevel@tonic-gate 	 *
1077c478bd9Sstevel@tonic-gate 	 * Portable heuristic to compute upper bound on space needed for
1087c478bd9Sstevel@tonic-gate 	 * N ancillary data options. It assumes up to _MAX_ALIGNMENT padding
1097c478bd9Sstevel@tonic-gate 	 * after both header and data as the worst possible upper bound on space
1107c478bd9Sstevel@tonic-gate 	 * consumed by padding.
1117c478bd9Sstevel@tonic-gate 	 * It also adds one extra "sizeof (struct cmsghdr)" for the last option.
1127c478bd9Sstevel@tonic-gate 	 * This is needed because we would like to use CMSG_NXTHDR() while
1137c478bd9Sstevel@tonic-gate 	 * composing the buffer. The CMSG_NXTHDR() macro is designed better for
1147c478bd9Sstevel@tonic-gate 	 * parsing than composing the buffer. It requires the pointer it returns
1157c478bd9Sstevel@tonic-gate 	 * to leave space in buffer for addressing a cmsghdr and we want to make
1167c478bd9Sstevel@tonic-gate 	 * sure it works for us while we skip beyond the last ancillary data
1177c478bd9Sstevel@tonic-gate 	 * option.
1187c478bd9Sstevel@tonic-gate 	 *
1197c478bd9Sstevel@tonic-gate 	 * bufspace[i]  = sizeof(struct cmsghdr) + <pad after header> +
1207c478bd9Sstevel@tonic-gate 	 *		<option[i] content length> + <pad after data>;
1217c478bd9Sstevel@tonic-gate 	 *
1227c478bd9Sstevel@tonic-gate 	 * total_bufspace = bufspace[0] + bufspace[1] + ...
1237c478bd9Sstevel@tonic-gate 	 *		    ... + bufspace[N-1] + sizeof (struct cmsghdr);
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	rthdr_space = 0;
1277c478bd9Sstevel@tonic-gate 	pktinfo_space = 0;
1287c478bd9Sstevel@tonic-gate 	hoplimit_space = 0;
1297c478bd9Sstevel@tonic-gate 	bufspace = 0;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if (hoplimit != -1) {
1327c478bd9Sstevel@tonic-gate 		hoplimit_space = sizeof (int);
1337c478bd9Sstevel@tonic-gate 		bufspace += sizeof (struct cmsghdr) + _MAX_ALIGNMENT +
1347c478bd9Sstevel@tonic-gate 		    hoplimit_space + _MAX_ALIGNMENT;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if (gw_cnt > 0) {
1387c478bd9Sstevel@tonic-gate 		rthdr_space = inet6_rth_space(IPV6_RTHDR_TYPE_0, gw_cnt);
1397c478bd9Sstevel@tonic-gate 		bufspace += sizeof (struct cmsghdr) + _MAX_ALIGNMENT +
1407c478bd9Sstevel@tonic-gate 		    rthdr_space + _MAX_ALIGNMENT;
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (if_index != 0) {
1447c478bd9Sstevel@tonic-gate 		pktinfo_space = sizeof (struct in6_pktinfo);
1457c478bd9Sstevel@tonic-gate 		bufspace += sizeof (struct cmsghdr) + _MAX_ALIGNMENT +
1467c478bd9Sstevel@tonic-gate 		    pktinfo_space + _MAX_ALIGNMENT;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * We need to temporarily set the msgp->msg_controllen to bufspace
1517c478bd9Sstevel@tonic-gate 	 * (we will later trim it to actual length used). This is needed because
1527c478bd9Sstevel@tonic-gate 	 * CMSG_NXTHDR() uses it to check we have not exceeded the bounds.
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	bufspace += sizeof (struct cmsghdr);
1557c478bd9Sstevel@tonic-gate 	msgp->msg_controllen = bufspace;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * This function is called more than once only if -l/-S used,
1597c478bd9Sstevel@tonic-gate 	 * since we need to modify the middle gateway. So, don't alloc
1607c478bd9Sstevel@tonic-gate 	 * new memory, just reuse what msg6 points to.
1617c478bd9Sstevel@tonic-gate 	 */
1627c478bd9Sstevel@tonic-gate 	if (first) {
1637c478bd9Sstevel@tonic-gate 		first = _B_FALSE;
1647c478bd9Sstevel@tonic-gate 		msgp->msg_control = (struct cmsghdr *)malloc(bufspace);
1657c478bd9Sstevel@tonic-gate 		if (msgp->msg_control == NULL) {
1667c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: malloc %s\n",
1677c478bd9Sstevel@tonic-gate 			    progname, strerror(errno));
1687c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 	};
1717c478bd9Sstevel@tonic-gate 	cmsgp = CMSG_FIRSTHDR(msgp);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * Fill ancillary data. First hoplimit, then rthdr and pktinfo.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	/* set hoplimit ancillary data if needed */
1787c478bd9Sstevel@tonic-gate 	if (hoplimit != -1) {
1797c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_level = IPPROTO_IPV6;
1807c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_type = IPV6_HOPLIMIT;
1817c478bd9Sstevel@tonic-gate 		cmsg_datap = CMSG_DATA(cmsgp);
1827c478bd9Sstevel@tonic-gate 		/* LINTED */
1837c478bd9Sstevel@tonic-gate 		*(int *)cmsg_datap = hoplimit;
1847c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_len = cmsg_datap + hoplimit_space -
1857c478bd9Sstevel@tonic-gate 		    (uchar_t *)cmsgp;
1867c478bd9Sstevel@tonic-gate 		cmsgp = CMSG_NXTHDR(msgp, cmsgp);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/* set rthdr ancillary data if needed */
1907c478bd9Sstevel@tonic-gate 	if (gw_cnt > 0) {
1917c478bd9Sstevel@tonic-gate 		struct ip6_rthdr0 *rthdr0p;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_level = IPPROTO_IPV6;
1947c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_type = IPV6_RTHDR;
1957c478bd9Sstevel@tonic-gate 		cmsg_datap = CMSG_DATA(cmsgp);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		/*
1987c478bd9Sstevel@tonic-gate 		 * Initialize rthdr structure
1997c478bd9Sstevel@tonic-gate 		 */
2007c478bd9Sstevel@tonic-gate 		/* LINTED */
2017c478bd9Sstevel@tonic-gate 		rthdr0p = (struct ip6_rthdr0 *)cmsg_datap;
2027c478bd9Sstevel@tonic-gate 		if (inet6_rth_init(rthdr0p, rthdr_space,
2037c478bd9Sstevel@tonic-gate 		    IPV6_RTHDR_TYPE_0, gw_cnt) == NULL) {
2047c478bd9Sstevel@tonic-gate 			Fprintf(stderr, "%s: inet6_rth_init failed\n",
2057c478bd9Sstevel@tonic-gate 			    progname);
2067c478bd9Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		/*
2107c478bd9Sstevel@tonic-gate 		 * Stuff in gateway addresses
2117c478bd9Sstevel@tonic-gate 		 */
2127c478bd9Sstevel@tonic-gate 		for (i = 0; i < gw_cnt; i++) {
2137c478bd9Sstevel@tonic-gate 			if (inet6_rth_add(rthdr0p, &gwIPlist[i].addr6) == -1) {
2147c478bd9Sstevel@tonic-gate 				Fprintf(stderr,
2157c478bd9Sstevel@tonic-gate 				    "%s: inet6_rth_add\n", progname);
2167c478bd9Sstevel@tonic-gate 				exit(EXIT_FAILURE);
2177c478bd9Sstevel@tonic-gate 			}
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_len = cmsg_datap + rthdr_space - (uchar_t *)cmsgp;
2217c478bd9Sstevel@tonic-gate 		cmsgp = CMSG_NXTHDR(msgp, cmsgp);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/* set pktinfo ancillary data if needed */
2257c478bd9Sstevel@tonic-gate 	if (if_index != 0) {
2267c478bd9Sstevel@tonic-gate 		struct in6_pktinfo *pktinfop;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_level = IPPROTO_IPV6;
2297c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_type = IPV6_PKTINFO;
2307c478bd9Sstevel@tonic-gate 		cmsg_datap = CMSG_DATA(cmsgp);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		/* LINTED */
2337c478bd9Sstevel@tonic-gate 		pktinfop = (struct in6_pktinfo *)cmsg_datap;
2347c478bd9Sstevel@tonic-gate 		/*
2357c478bd9Sstevel@tonic-gate 		 * We don't know if pktinfop->ipi6_addr is aligned properly,
2367c478bd9Sstevel@tonic-gate 		 * therefore let's use bcopy, instead of assignment.
2377c478bd9Sstevel@tonic-gate 		 */
2387c478bd9Sstevel@tonic-gate 		(void) bcopy(&in6addr_any, &pktinfop->ipi6_addr,
2397c478bd9Sstevel@tonic-gate 		sizeof (struct in6_addr));
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		/*
2427c478bd9Sstevel@tonic-gate 		 *  We can assume pktinfop->ipi6_ifindex is 32 bit aligned.
2437c478bd9Sstevel@tonic-gate 		 */
2447c478bd9Sstevel@tonic-gate 		pktinfop->ipi6_ifindex = if_index;
2457c478bd9Sstevel@tonic-gate 		cmsgp->cmsg_len = cmsg_datap + pktinfo_space - (uchar_t *)cmsgp;
2467c478bd9Sstevel@tonic-gate 		cmsgp = CMSG_NXTHDR(msgp, cmsgp);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	msgp->msg_controllen = (char *)cmsgp - (char *)msgp->msg_control;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * Check out the packet to see if it came from us.  This logic is necessary
2547c478bd9Sstevel@tonic-gate  * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
2557c478bd9Sstevel@tonic-gate  * which arrive ('tis only fair).  This permits multiple copies of this
2567c478bd9Sstevel@tonic-gate  * program to be run without having intermingled output (or statistics!).
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate void
check_reply6(struct addrinfo * ai_dst,struct msghdr * msg,int cc,ushort_t udp_src_port)2597c478bd9Sstevel@tonic-gate check_reply6(struct addrinfo *ai_dst, struct msghdr *msg, int cc,
2607c478bd9Sstevel@tonic-gate     ushort_t udp_src_port)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	struct icmp6_hdr *icmp6;
2637c478bd9Sstevel@tonic-gate 	ip6_t *ip6h;
2647c478bd9Sstevel@tonic-gate 	nd_redirect_t *nd_rdrct;
2657c478bd9Sstevel@tonic-gate 	struct udphdr *up;
2667c478bd9Sstevel@tonic-gate 	union any_in_addr dst_addr;
2677c478bd9Sstevel@tonic-gate 	uchar_t *buf;
2687c478bd9Sstevel@tonic-gate 	int32_t *intp;
2697c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *from6;
2707c478bd9Sstevel@tonic-gate 	struct timeval tv;
2717c478bd9Sstevel@tonic-gate 	struct timeval *tp;
2727c478bd9Sstevel@tonic-gate 	int64_t triptime;
2737c478bd9Sstevel@tonic-gate 	boolean_t valid_reply = _B_FALSE;
2747c478bd9Sstevel@tonic-gate 	boolean_t reply_matched_current_target;	/* Is the source address of */
2757c478bd9Sstevel@tonic-gate 						/* this reply same as where */
2767c478bd9Sstevel@tonic-gate 						/* we're sending currently? */
2777c478bd9Sstevel@tonic-gate 	boolean_t last_reply_from_targetaddr = _B_FALSE; /* Is this stats, */
2787c478bd9Sstevel@tonic-gate 						/* probe all with npackets>0 */
2797c478bd9Sstevel@tonic-gate 						/* and we received reply for */
2807c478bd9Sstevel@tonic-gate 						/* the last probe sent to */
2817c478bd9Sstevel@tonic-gate 						/* targetaddr */
2827c478bd9Sstevel@tonic-gate 	uint32_t ip6hdr_len;
2837c478bd9Sstevel@tonic-gate 	uint8_t last_hdr;
2847c478bd9Sstevel@tonic-gate 	int cc_left;
2857c478bd9Sstevel@tonic-gate 	int i;
2867c478bd9Sstevel@tonic-gate 	char tmp_buf[INET6_ADDRSTRLEN];
2877c478bd9Sstevel@tonic-gate 	static char *unreach6[] = {
2887c478bd9Sstevel@tonic-gate 	    "No Route to Destination",
2897c478bd9Sstevel@tonic-gate 	    "Communication Administratively Prohibited",
2907c478bd9Sstevel@tonic-gate 	    "Not a Neighbor (obsoleted ICMPv6 code)",
2917c478bd9Sstevel@tonic-gate 	    "Address Unreachable",
2927c478bd9Sstevel@tonic-gate 	    "Port Unreachable"
2937c478bd9Sstevel@tonic-gate 	};
2947c478bd9Sstevel@tonic-gate 	static char *timexceed6[] = {
2957c478bd9Sstevel@tonic-gate 	    "Hop limit exceeded in transit",
2967c478bd9Sstevel@tonic-gate 	    "Fragment reassembly time exceeded"
2977c478bd9Sstevel@tonic-gate 	};
2987c478bd9Sstevel@tonic-gate 	static char *param_prob6[] = {
2997c478bd9Sstevel@tonic-gate 	    "Erroneous header field encountered",
3007c478bd9Sstevel@tonic-gate 	    "Unrecognized next header type encountered",
3017c478bd9Sstevel@tonic-gate 	    "Unrecognized IPv6 option encountered"
3027c478bd9Sstevel@tonic-gate 	};
3037c478bd9Sstevel@tonic-gate 	boolean_t print_newline = _B_FALSE;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/* decompose msghdr into useful pieces */
3067c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)msg->msg_iov->iov_base;
3077c478bd9Sstevel@tonic-gate 	from6 = (struct sockaddr_in6 *)msg->msg_name;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/* LINTED */
3107c478bd9Sstevel@tonic-gate 	intp = (int32_t *)buf;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/* get time now for most accurate time calculation */
3137c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&tv, (struct timezone *)NULL);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/* Ignore packets > 64k or control buffers that don't fit */
3167c478bd9Sstevel@tonic-gate 	if (msg->msg_flags & (MSG_TRUNC|MSG_CTRUNC)) {
3177c478bd9Sstevel@tonic-gate 		if (verbose) {
3187c478bd9Sstevel@tonic-gate 			Printf("Truncated message: msg_flags 0x%x from %s\n",
3197c478bd9Sstevel@tonic-gate 			    msg->msg_flags,
3207c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from6->sin6_addr, AF_INET6));
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 		return;
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate 	if (cc < ICMP6_MINLEN) {
3257c478bd9Sstevel@tonic-gate 		if (verbose) {
3267c478bd9Sstevel@tonic-gate 			Printf("packet too short (%d bytes) from %s\n", cc,
3277c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from6->sin6_addr, AF_INET6));
3287c478bd9Sstevel@tonic-gate 		}
3297c478bd9Sstevel@tonic-gate 		return;
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 	/* LINTED */
3327c478bd9Sstevel@tonic-gate 	icmp6 = (struct icmp6_hdr *)buf;
3337c478bd9Sstevel@tonic-gate 	cc_left = cc - ICMP6_MINLEN;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	switch (icmp6->icmp6_type) {
3367c478bd9Sstevel@tonic-gate 	case ICMP6_DST_UNREACH:
3377c478bd9Sstevel@tonic-gate 		/* LINTED */
3387c478bd9Sstevel@tonic-gate 		ip6h = (ip6_t *)((char *)icmp6 + ICMP6_MINLEN);
3397c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (ip6_t)) {
3407c478bd9Sstevel@tonic-gate 			if (verbose) {
3417c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
3427c478bd9Sstevel@tonic-gate 				    cc,
3437c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
3447c478bd9Sstevel@tonic-gate 					AF_INET6));
3457c478bd9Sstevel@tonic-gate 			}
3467c478bd9Sstevel@tonic-gate 			return;
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * Determine the total length of IPv6 header and extension
3517c478bd9Sstevel@tonic-gate 		 * headers, also the upper layer header (UDP, TCP, ICMP, etc.)
3527c478bd9Sstevel@tonic-gate 		 * following.
3537c478bd9Sstevel@tonic-gate 		 */
3547c478bd9Sstevel@tonic-gate 		ip6hdr_len = IPv6_hdrlen(ip6h, cc_left, &last_hdr);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		cc_left -= ip6hdr_len;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		/* LINTED */
3597c478bd9Sstevel@tonic-gate 		up = (struct udphdr *)((char *)ip6h + ip6hdr_len);
3607c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (struct udphdr)) {
3617c478bd9Sstevel@tonic-gate 			if (verbose) {
3627c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
3637c478bd9Sstevel@tonic-gate 				    cc,
3647c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
3657c478bd9Sstevel@tonic-gate 					AF_INET6));
3667c478bd9Sstevel@tonic-gate 			}
3677c478bd9Sstevel@tonic-gate 			return;
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 		cc_left -= sizeof (struct udphdr);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		/* determine if this is *the* reply */
3727c478bd9Sstevel@tonic-gate 		if (icmp6->icmp6_code == ICMP6_DST_UNREACH_NOPORT &&
3737c478bd9Sstevel@tonic-gate 		    last_hdr == IPPROTO_UDP &&
3747c478bd9Sstevel@tonic-gate 		    udp_src_port == up->uh_sport &&
3757c478bd9Sstevel@tonic-gate 		    use_udp) {
3767c478bd9Sstevel@tonic-gate 			valid_reply = _B_TRUE;
3777c478bd9Sstevel@tonic-gate 		} else {
3787c478bd9Sstevel@tonic-gate 			valid_reply = _B_FALSE;
3797c478bd9Sstevel@tonic-gate 		}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		if (valid_reply) {
3827c478bd9Sstevel@tonic-gate 			/*
3837c478bd9Sstevel@tonic-gate 			 * For this valid reply, if we are still sending to
3847c478bd9Sstevel@tonic-gate 			 * this target IP address, we'd like to do some
3857c478bd9Sstevel@tonic-gate 			 * updates to targetaddr, so hold SIGALRMs.
3867c478bd9Sstevel@tonic-gate 			 */
3877c478bd9Sstevel@tonic-gate 			(void) sighold(SIGALRM);
3887c478bd9Sstevel@tonic-gate 			is_alive = _B_TRUE;
3897c478bd9Sstevel@tonic-gate 			nreceived++;
3907c478bd9Sstevel@tonic-gate 			reply_matched_current_target =
3917c478bd9Sstevel@tonic-gate 			    seq_match(current_targetaddr->starting_seq_num,
3927c478bd9Sstevel@tonic-gate 				current_targetaddr->num_sent,
3937c478bd9Sstevel@tonic-gate 				ntohs(up->uh_dport));
3947c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
3957c478bd9Sstevel@tonic-gate 				current_targetaddr->got_reply = _B_TRUE;
3967c478bd9Sstevel@tonic-gate 				nreceived_last_target++;
3977c478bd9Sstevel@tonic-gate 				/*
3987c478bd9Sstevel@tonic-gate 				 * Determine if stats, probe-all, and
3997c478bd9Sstevel@tonic-gate 				 * npackets != 0, and this is the reply for
4007c478bd9Sstevel@tonic-gate 				 * the last probe we sent to current target
4017c478bd9Sstevel@tonic-gate 				 * address.
4027c478bd9Sstevel@tonic-gate 				 */
4037c478bd9Sstevel@tonic-gate 				if (stats && probe_all && npackets > 0 &&
4047c478bd9Sstevel@tonic-gate 				    ((current_targetaddr->starting_seq_num +
4057c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_probes - 1) %
4067c478bd9Sstevel@tonic-gate 				    (MAX_PORT + 1) == ntohs(up->uh_dport)) &&
4077c478bd9Sstevel@tonic-gate 				    (current_targetaddr->num_probes ==
4087c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_sent))
4097c478bd9Sstevel@tonic-gate 					last_reply_from_targetaddr = _B_TRUE;
4107c478bd9Sstevel@tonic-gate 			} else {
4117c478bd9Sstevel@tonic-gate 				/*
4127c478bd9Sstevel@tonic-gate 				 * If it's just probe_all and we just received
4137c478bd9Sstevel@tonic-gate 				 * a reply from a target address we were
4147c478bd9Sstevel@tonic-gate 				 * probing and had timed out (now we are probing
4157c478bd9Sstevel@tonic-gate 				 * some other target address), we ignore
4167c478bd9Sstevel@tonic-gate 				 * this reply.
4177c478bd9Sstevel@tonic-gate 				 */
4187c478bd9Sstevel@tonic-gate 				if (probe_all && !stats) {
4197c478bd9Sstevel@tonic-gate 					valid_reply = _B_FALSE;
4207c478bd9Sstevel@tonic-gate 					/*
4217c478bd9Sstevel@tonic-gate 					 * Only if it's verbose, we get a
4227c478bd9Sstevel@tonic-gate 					 * message regarding this reply,
4237c478bd9Sstevel@tonic-gate 					 * otherwise we are done here.
4247c478bd9Sstevel@tonic-gate 					 */
4257c478bd9Sstevel@tonic-gate 					if (!verbose) {
4267c478bd9Sstevel@tonic-gate 						(void) sigrelse(SIGALRM);
4277c478bd9Sstevel@tonic-gate 						return;
4287c478bd9Sstevel@tonic-gate 					}
4297c478bd9Sstevel@tonic-gate 				}
4307c478bd9Sstevel@tonic-gate 			}
4317c478bd9Sstevel@tonic-gate 		}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		if (valid_reply && !stats) {
4347c478bd9Sstevel@tonic-gate 			/*
4357c478bd9Sstevel@tonic-gate 			 * if we are still sending to the same target address,
4367c478bd9Sstevel@tonic-gate 			 * then stop it, because we know it's alive.
4377c478bd9Sstevel@tonic-gate 			 */
4387c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
4397c478bd9Sstevel@tonic-gate 				(void) alarm(0);	/* cancel alarm */
4407c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, SIG_IGN);
4417c478bd9Sstevel@tonic-gate 				current_targetaddr->probing_done = _B_TRUE;
4427c478bd9Sstevel@tonic-gate 			}
4437c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 			if (!probe_all) {
4467c478bd9Sstevel@tonic-gate 				Printf("%s is alive\n", targethost);
4477c478bd9Sstevel@tonic-gate 			} else {
4487c478bd9Sstevel@tonic-gate 				(void) inet_ntop(AF_INET6,
4497c478bd9Sstevel@tonic-gate 				    (void *)&ip6h->ip6_dst,
4507c478bd9Sstevel@tonic-gate 				    tmp_buf, sizeof (tmp_buf));
4517c478bd9Sstevel@tonic-gate 				if (nflag) {
4527c478bd9Sstevel@tonic-gate 					Printf("%s is alive\n", tmp_buf);
4537c478bd9Sstevel@tonic-gate 				} else {
4547c478bd9Sstevel@tonic-gate 					Printf("%s (%s) is alive\n",
4557c478bd9Sstevel@tonic-gate 					    targethost, tmp_buf);
4567c478bd9Sstevel@tonic-gate 				}
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
4597c478bd9Sstevel@tonic-gate 				/*
4607c478bd9Sstevel@tonic-gate 				 * Let's get things going again, but now
4617c478bd9Sstevel@tonic-gate 				 * ping will start sending to next target IP
4627c478bd9Sstevel@tonic-gate 				 * address.
4637c478bd9Sstevel@tonic-gate 				 */
4647c478bd9Sstevel@tonic-gate 				send_scheduled_probe();
4657c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, sigalrm_handler);
4667c478bd9Sstevel@tonic-gate 				schedule_sigalrm();
4677c478bd9Sstevel@tonic-gate 			}
4687c478bd9Sstevel@tonic-gate 			return;
4697c478bd9Sstevel@tonic-gate 		} else {
4707c478bd9Sstevel@tonic-gate 			/*
4717c478bd9Sstevel@tonic-gate 			 * If we are not moving to next targetaddr, let's
4727c478bd9Sstevel@tonic-gate 			 * release the SIGALRM now. We don't want to stall in
4737c478bd9Sstevel@tonic-gate 			 * the middle of probing a targetaddr if the pr_name()
4747c478bd9Sstevel@tonic-gate 			 * call (see below) takes longer.
4757c478bd9Sstevel@tonic-gate 			 */
4767c478bd9Sstevel@tonic-gate 			if (!last_reply_from_targetaddr)
4777c478bd9Sstevel@tonic-gate 				(void) sigrelse(SIGALRM);
4787c478bd9Sstevel@tonic-gate 			/* else, we'll release it later */
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		dst_addr.addr6 = ip6h->ip6_dst;
4827c478bd9Sstevel@tonic-gate 		if (valid_reply) {
4837c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
4847c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from6->sin6_addr, AF_INET6));
4857c478bd9Sstevel@tonic-gate 			Printf("udp_port=%d. ", ntohs(up->uh_dport));
4867c478bd9Sstevel@tonic-gate 			print_newline = _B_TRUE;
4877c478bd9Sstevel@tonic-gate 		} else if (is_a_target(ai_dst, &dst_addr)|| verbose) {
4887c478bd9Sstevel@tonic-gate 			if (icmp6->icmp6_code >= A_CNT(unreach6)) {
4897c478bd9Sstevel@tonic-gate 				Printf("ICMPv6 %d Unreachable from gateway "
4907c478bd9Sstevel@tonic-gate 				    "%s\n", icmp6->icmp6_code,
4917c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
4927c478bd9Sstevel@tonic-gate 					AF_INET6));
4937c478bd9Sstevel@tonic-gate 			} else {
4947c478bd9Sstevel@tonic-gate 				Printf("ICMPv6 %s from gateway %s\n",
4957c478bd9Sstevel@tonic-gate 				    unreach6[icmp6->icmp6_code],
4967c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
4977c478bd9Sstevel@tonic-gate 					AF_INET6));
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(last_hdr),
5007c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip6h->ip6_src, AF_INET6));
5017c478bd9Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip6h->ip6_dst,
5027c478bd9Sstevel@tonic-gate 			    AF_INET6));
5037c478bd9Sstevel@tonic-gate 			if (last_hdr == IPPROTO_TCP || last_hdr == IPPROTO_UDP)
5047c478bd9Sstevel@tonic-gate 				Printf(" port %d ", ntohs(up->uh_dport));
5057c478bd9Sstevel@tonic-gate 			print_newline = _B_TRUE;
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * Update and print the stats, if it's a valid reply and
5107c478bd9Sstevel@tonic-gate 		 * contains a timestamp.
5117c478bd9Sstevel@tonic-gate 		 */
5127c478bd9Sstevel@tonic-gate 		if (valid_reply && datalen >= sizeof (struct timeval) &&
5137c478bd9Sstevel@tonic-gate 		    cc_left >= sizeof (struct timeval)) {
5147c478bd9Sstevel@tonic-gate 			/* LINTED */
5157c478bd9Sstevel@tonic-gate 			tp = (struct timeval *)((char *)up +
5167c478bd9Sstevel@tonic-gate 			    sizeof (struct udphdr));
5177c478bd9Sstevel@tonic-gate 			(void) tvsub(&tv, tp);
5187c478bd9Sstevel@tonic-gate 			triptime = (int64_t)tv.tv_sec * MICROSEC + tv.tv_usec;
5197c478bd9Sstevel@tonic-gate 			Printf("time=" TIMEFORMAT " ms", triptime/1000.0);
5207c478bd9Sstevel@tonic-gate 			tsum += triptime;
5217c478bd9Sstevel@tonic-gate 			tsum2 += triptime*triptime;
5227c478bd9Sstevel@tonic-gate 			if (triptime < tmin)
5237c478bd9Sstevel@tonic-gate 				tmin = triptime;
5247c478bd9Sstevel@tonic-gate 			if (triptime > tmax)
5257c478bd9Sstevel@tonic-gate 				tmax = triptime;
5267c478bd9Sstevel@tonic-gate 			print_newline = _B_TRUE;
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 		if (print_newline)
5297c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
5307c478bd9Sstevel@tonic-gate 		/*
5317c478bd9Sstevel@tonic-gate 		 * If it's stats, probe-all, npackets > 0, and we received reply
5327c478bd9Sstevel@tonic-gate 		 * for the last probe sent to this target address, then we
5337c478bd9Sstevel@tonic-gate 		 * don't need to wait anymore, let's move on to next target
5347c478bd9Sstevel@tonic-gate 		 * address, now!
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 		if (last_reply_from_targetaddr) {
5377c478bd9Sstevel@tonic-gate 			(void) alarm(0);	/* cancel alarm */
5387c478bd9Sstevel@tonic-gate 			current_targetaddr->probing_done = _B_TRUE;
5397c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
5407c478bd9Sstevel@tonic-gate 			send_scheduled_probe();
5417c478bd9Sstevel@tonic-gate 			schedule_sigalrm();
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		break;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	case ICMP6_PACKET_TOO_BIG:
5467c478bd9Sstevel@tonic-gate 		/* LINTED */
5477c478bd9Sstevel@tonic-gate 		ip6h = (ip6_t *)((char *)icmp6 + ICMP6_MINLEN);
5487c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (ip6_t)) {
5497c478bd9Sstevel@tonic-gate 			if (verbose) {
5507c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
5517c478bd9Sstevel@tonic-gate 				    cc, pr_name((char *)&from6->sin6_addr,
5527c478bd9Sstevel@tonic-gate 				    AF_INET6));
5537c478bd9Sstevel@tonic-gate 			}
5547c478bd9Sstevel@tonic-gate 			return;
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 		ip6hdr_len = IPv6_hdrlen(ip6h, cc_left, &last_hdr);
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		dst_addr.addr6 = ip6h->ip6_dst;
5597c478bd9Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
5607c478bd9Sstevel@tonic-gate 			Printf("ICMPv6 packet too big from %s\n",
5617c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from6->sin6_addr, AF_INET6));
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(last_hdr),
5647c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip6h->ip6_src, AF_INET6));
5657c478bd9Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip6h->ip6_dst,
5667c478bd9Sstevel@tonic-gate 			    AF_INET6));
5677c478bd9Sstevel@tonic-gate 			if ((last_hdr == IPPROTO_TCP ||
5687c478bd9Sstevel@tonic-gate 			    last_hdr == IPPROTO_UDP) &&
5697c478bd9Sstevel@tonic-gate 			    (cc_left >= (ip6hdr_len + 4))) {
5707c478bd9Sstevel@tonic-gate 				/* LINTED */
5717c478bd9Sstevel@tonic-gate 				up = (struct udphdr *)
5727c478bd9Sstevel@tonic-gate 				    ((char *)ip6h + ip6hdr_len);
5737c478bd9Sstevel@tonic-gate 				Printf(" port %d ", ntohs(up->uh_dport));
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 			Printf(" MTU = %d\n", ntohl(icmp6->icmp6_mtu));
5767c478bd9Sstevel@tonic-gate 		}
5777c478bd9Sstevel@tonic-gate 		break;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	case ICMP6_TIME_EXCEEDED:
5807c478bd9Sstevel@tonic-gate 		/* LINTED */
5817c478bd9Sstevel@tonic-gate 		ip6h = (ip6_t *)((char *)icmp6 + ICMP6_MINLEN);
5827c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (ip6_t)) {
5837c478bd9Sstevel@tonic-gate 			if (verbose) {
5847c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
5857c478bd9Sstevel@tonic-gate 				    cc,
5867c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
5877c478bd9Sstevel@tonic-gate 					AF_INET6));
5887c478bd9Sstevel@tonic-gate 			}
5897c478bd9Sstevel@tonic-gate 			return;
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		ip6hdr_len = IPv6_hdrlen(ip6h, cc_left, &last_hdr);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		dst_addr.addr6 = ip6h->ip6_dst;
5947c478bd9Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
5957c478bd9Sstevel@tonic-gate 			if (icmp6->icmp6_code >= A_CNT(timexceed6)) {
5967c478bd9Sstevel@tonic-gate 				Printf("ICMPv6 %d time exceeded from %s\n",
5977c478bd9Sstevel@tonic-gate 				    icmp6->icmp6_code,
5987c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
5997c478bd9Sstevel@tonic-gate 					AF_INET6));
6007c478bd9Sstevel@tonic-gate 			} else {
6017c478bd9Sstevel@tonic-gate 				Printf("ICMPv6 %s from %s\n",
6027c478bd9Sstevel@tonic-gate 				    timexceed6[icmp6->icmp6_code],
6037c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
6047c478bd9Sstevel@tonic-gate 					AF_INET6));
6057c478bd9Sstevel@tonic-gate 			}
6067c478bd9Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(last_hdr),
6077c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip6h->ip6_src, AF_INET6));
6087c478bd9Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip6h->ip6_dst,
6097c478bd9Sstevel@tonic-gate 			    AF_INET6));
6107c478bd9Sstevel@tonic-gate 			if ((last_hdr == IPPROTO_TCP ||
6117c478bd9Sstevel@tonic-gate 			    last_hdr == IPPROTO_UDP) &&
6127c478bd9Sstevel@tonic-gate 			    (cc_left >= (ip6hdr_len + 4))) {
6137c478bd9Sstevel@tonic-gate 				/* LINTED */
6147c478bd9Sstevel@tonic-gate 				up = (struct udphdr *)
6157c478bd9Sstevel@tonic-gate 				    ((char *)ip6h + ip6hdr_len);
6167c478bd9Sstevel@tonic-gate 				Printf(" port %d", ntohs(up->uh_dport));
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 		break;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	case ICMP6_PARAM_PROB:
6237c478bd9Sstevel@tonic-gate 		/* LINTED */
6247c478bd9Sstevel@tonic-gate 		ip6h = (ip6_t *)((char *)icmp6 + ICMP6_MINLEN);
6257c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (ip6_t)) {
6267c478bd9Sstevel@tonic-gate 			if (verbose) {
6277c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
6287c478bd9Sstevel@tonic-gate 				    cc,
6297c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
6307c478bd9Sstevel@tonic-gate 					AF_INET6));
6317c478bd9Sstevel@tonic-gate 			}
6327c478bd9Sstevel@tonic-gate 			return;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 		ip6hdr_len = IPv6_hdrlen(ip6h, cc_left, &last_hdr);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		dst_addr.addr6 = ip6h->ip6_dst;
6377c478bd9Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
6387c478bd9Sstevel@tonic-gate 			if (icmp6->icmp6_code >= A_CNT(param_prob6)) {
6397c478bd9Sstevel@tonic-gate 				Printf("ICMPv6 %d parameter problem from %s\n",
6407c478bd9Sstevel@tonic-gate 				    icmp6->icmp6_code,
6417c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
6427c478bd9Sstevel@tonic-gate 					AF_INET6));
6437c478bd9Sstevel@tonic-gate 			} else {
6447c478bd9Sstevel@tonic-gate 				Printf("ICMPv6 %s from %s\n",
6457c478bd9Sstevel@tonic-gate 				    param_prob6[icmp6->icmp6_code],
6467c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
6477c478bd9Sstevel@tonic-gate 					AF_INET6));
6487c478bd9Sstevel@tonic-gate 			}
6497c478bd9Sstevel@tonic-gate 			icmp6->icmp6_pptr = ntohl(icmp6->icmp6_pptr);
6507c478bd9Sstevel@tonic-gate 			Printf(" in byte %d", icmp6->icmp6_pptr);
6517c478bd9Sstevel@tonic-gate 			if (icmp6->icmp6_pptr <= ip6hdr_len) {
6527c478bd9Sstevel@tonic-gate 				Printf(" (value 0x%x)",
6537c478bd9Sstevel@tonic-gate 				    *((uchar_t *)ip6h + icmp6->icmp6_pptr));
6547c478bd9Sstevel@tonic-gate 			}
6557c478bd9Sstevel@tonic-gate 			Printf(" for %s from %s", pr_protocol(last_hdr),
6567c478bd9Sstevel@tonic-gate 			    pr_name((char *)&ip6h->ip6_src, AF_INET6));
6577c478bd9Sstevel@tonic-gate 			Printf(" to %s", pr_name((char *)&ip6h->ip6_dst,
6587c478bd9Sstevel@tonic-gate 			    AF_INET6));
6597c478bd9Sstevel@tonic-gate 			if ((last_hdr == IPPROTO_TCP ||
6607c478bd9Sstevel@tonic-gate 			    last_hdr == IPPROTO_UDP) &&
6617c478bd9Sstevel@tonic-gate 			    (cc_left >= (ip6hdr_len + 4))) {
6627c478bd9Sstevel@tonic-gate 				/* LINTED */
6637c478bd9Sstevel@tonic-gate 				up = (struct udphdr *)
6647c478bd9Sstevel@tonic-gate 				    ((char *)ip6h + ip6hdr_len);
6657c478bd9Sstevel@tonic-gate 				Printf(" port %d", ntohs(up->uh_dport));
6667c478bd9Sstevel@tonic-gate 			}
6677c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		break;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	case ICMP6_ECHO_REQUEST:
6727c478bd9Sstevel@tonic-gate 		return;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	case ICMP6_ECHO_REPLY:
6757c478bd9Sstevel@tonic-gate 		if (ntohs(icmp6->icmp6_id) == ident) {
6767c478bd9Sstevel@tonic-gate 			if (!use_udp)
6777c478bd9Sstevel@tonic-gate 				valid_reply = _B_TRUE;
6787c478bd9Sstevel@tonic-gate 			else
6797c478bd9Sstevel@tonic-gate 				valid_reply = _B_FALSE;
6807c478bd9Sstevel@tonic-gate 		} else {
6817c478bd9Sstevel@tonic-gate 			return;
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		if (valid_reply) {
6857c478bd9Sstevel@tonic-gate 			/*
6867c478bd9Sstevel@tonic-gate 			 * For this valid reply, if we are still sending to
6877c478bd9Sstevel@tonic-gate 			 * this target IP address, we'd like to do some
6887c478bd9Sstevel@tonic-gate 			 * updates to targetaddr, so hold SIGALRMs.
6897c478bd9Sstevel@tonic-gate 			 */
6907c478bd9Sstevel@tonic-gate 			(void) sighold(SIGALRM);
6917c478bd9Sstevel@tonic-gate 			is_alive = _B_TRUE;
6927c478bd9Sstevel@tonic-gate 			nreceived++;
6937c478bd9Sstevel@tonic-gate 			reply_matched_current_target =
6947c478bd9Sstevel@tonic-gate 			    seq_match(current_targetaddr->starting_seq_num,
6957c478bd9Sstevel@tonic-gate 				current_targetaddr->num_sent,
6967c478bd9Sstevel@tonic-gate 				ntohs(icmp6->icmp6_seq));
6977c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
6987c478bd9Sstevel@tonic-gate 				current_targetaddr->got_reply = _B_TRUE;
6997c478bd9Sstevel@tonic-gate 				nreceived_last_target++;
7007c478bd9Sstevel@tonic-gate 				/*
7017c478bd9Sstevel@tonic-gate 				 * Determine if stats, probe-all, and
7027c478bd9Sstevel@tonic-gate 				 * npackets != 0, and this is the reply for
7037c478bd9Sstevel@tonic-gate 				 * the last probe we sent to current target
7047c478bd9Sstevel@tonic-gate 				 * address.
7057c478bd9Sstevel@tonic-gate 				 */
7067c478bd9Sstevel@tonic-gate 				if (stats && probe_all && npackets > 0 &&
7077c478bd9Sstevel@tonic-gate 				    ((current_targetaddr->starting_seq_num +
7087c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_probes - 1) %
7097c478bd9Sstevel@tonic-gate 				    (MAX_ICMP_SEQ + 1) ==
7107c478bd9Sstevel@tonic-gate 				    ntohs(icmp6->icmp6_seq)) &&
7117c478bd9Sstevel@tonic-gate 				    (current_targetaddr->num_probes ==
7127c478bd9Sstevel@tonic-gate 				    current_targetaddr->num_sent))
7137c478bd9Sstevel@tonic-gate 					last_reply_from_targetaddr = _B_TRUE;
7147c478bd9Sstevel@tonic-gate 			} else {
7157c478bd9Sstevel@tonic-gate 				/*
7167c478bd9Sstevel@tonic-gate 				 * If it's just probe_all and we just received
7177c478bd9Sstevel@tonic-gate 				 * a reply from a target address we were
7187c478bd9Sstevel@tonic-gate 				 * probing and had timed out (now we are probing
7197c478bd9Sstevel@tonic-gate 				 * some other target address), we ignore
7207c478bd9Sstevel@tonic-gate 				 * this reply.
7217c478bd9Sstevel@tonic-gate 				 */
7227c478bd9Sstevel@tonic-gate 				if (probe_all && !stats) {
7237c478bd9Sstevel@tonic-gate 					valid_reply = _B_FALSE;
7247c478bd9Sstevel@tonic-gate 					/*
7257c478bd9Sstevel@tonic-gate 					 * Only if it's verbose, we get a
7267c478bd9Sstevel@tonic-gate 					 * message regarding this reply,
7277c478bd9Sstevel@tonic-gate 					 * otherwise we are done here.
7287c478bd9Sstevel@tonic-gate 					 */
7297c478bd9Sstevel@tonic-gate 					if (!verbose) {
7307c478bd9Sstevel@tonic-gate 						(void) sigrelse(SIGALRM);
7317c478bd9Sstevel@tonic-gate 						return;
7327c478bd9Sstevel@tonic-gate 					}
7337c478bd9Sstevel@tonic-gate 				}
7347c478bd9Sstevel@tonic-gate 			}
7357c478bd9Sstevel@tonic-gate 		}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		if (!stats && valid_reply) {
7387c478bd9Sstevel@tonic-gate 			/*
7397c478bd9Sstevel@tonic-gate 			 * if we are still sending to the same target address,
7407c478bd9Sstevel@tonic-gate 			 * then stop it, because we know it's alive.
7417c478bd9Sstevel@tonic-gate 			 */
7427c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
7437c478bd9Sstevel@tonic-gate 				(void) alarm(0);	/* cancel alarm */
7447c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, SIG_IGN);
7457c478bd9Sstevel@tonic-gate 				current_targetaddr->probing_done = _B_TRUE;
7467c478bd9Sstevel@tonic-gate 			}
7477c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 			if (!probe_all) {
7507c478bd9Sstevel@tonic-gate 				Printf("%s is alive\n", targethost);
7517c478bd9Sstevel@tonic-gate 			} else {
7527c478bd9Sstevel@tonic-gate 				/*
7537c478bd9Sstevel@tonic-gate 				 * If we are using send_reply, the real
7547c478bd9Sstevel@tonic-gate 				 * target address is not the src address of the
7557c478bd9Sstevel@tonic-gate 				 * replies. Use icmp_seq to find out where this
7567c478bd9Sstevel@tonic-gate 				 * probe was sent to.
7577c478bd9Sstevel@tonic-gate 				 */
7587c478bd9Sstevel@tonic-gate 				if (send_reply) {
7597c478bd9Sstevel@tonic-gate 					(void) find_dstaddr(
7607c478bd9Sstevel@tonic-gate 					    ntohs(icmp6->icmp6_seq), &dst_addr);
7617c478bd9Sstevel@tonic-gate 					(void) inet_ntop(AF_INET6,
7627c478bd9Sstevel@tonic-gate 					    (void *)&dst_addr.addr6,
7637c478bd9Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
7647c478bd9Sstevel@tonic-gate 				} else {
7657c478bd9Sstevel@tonic-gate 					(void) inet_ntop(AF_INET6,
7667c478bd9Sstevel@tonic-gate 					    (void *)&from6->sin6_addr,
7677c478bd9Sstevel@tonic-gate 					    tmp_buf, sizeof (tmp_buf));
7687c478bd9Sstevel@tonic-gate 				}
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 				if (nflag) {
7717c478bd9Sstevel@tonic-gate 					Printf("%s is alive\n", tmp_buf);
7727c478bd9Sstevel@tonic-gate 				} else {
7737c478bd9Sstevel@tonic-gate 					Printf("%s (%s) is alive\n",
7747c478bd9Sstevel@tonic-gate 					    targethost, tmp_buf);
7757c478bd9Sstevel@tonic-gate 				}
7767c478bd9Sstevel@tonic-gate 			}
7777c478bd9Sstevel@tonic-gate 			if (reply_matched_current_target) {
7787c478bd9Sstevel@tonic-gate 				/*
7797c478bd9Sstevel@tonic-gate 				 * Let's get things going again, but now
7807c478bd9Sstevel@tonic-gate 				 * ping will start sending to next target IP
7817c478bd9Sstevel@tonic-gate 				 * address.
7827c478bd9Sstevel@tonic-gate 				 */
7837c478bd9Sstevel@tonic-gate 				send_scheduled_probe();
7847c478bd9Sstevel@tonic-gate 				(void) sigset(SIGALRM, sigalrm_handler);
7857c478bd9Sstevel@tonic-gate 				schedule_sigalrm();
7867c478bd9Sstevel@tonic-gate 			}
7877c478bd9Sstevel@tonic-gate 			return;
7887c478bd9Sstevel@tonic-gate 		} else {
7897c478bd9Sstevel@tonic-gate 			/*
7907c478bd9Sstevel@tonic-gate 			 * If we are not moving to next targetaddr, let's
7917c478bd9Sstevel@tonic-gate 			 * release the SIGALRM now. We don't want to stall in
7927c478bd9Sstevel@tonic-gate 			 * the middle of probing a targetaddr if the pr_name()
7937c478bd9Sstevel@tonic-gate 			 * call (see below) takes longer.
7947c478bd9Sstevel@tonic-gate 			 */
7957c478bd9Sstevel@tonic-gate 			if (!last_reply_from_targetaddr)
7967c478bd9Sstevel@tonic-gate 				(void) sigrelse(SIGALRM);
7977c478bd9Sstevel@tonic-gate 			/* else, we'll release it later */
7987c478bd9Sstevel@tonic-gate 		}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 		/*
8017c478bd9Sstevel@tonic-gate 		 * If we are using send_reply, the real target address is
8027c478bd9Sstevel@tonic-gate 		 * not the src address of the replies. Use icmp_seq to find out
8037c478bd9Sstevel@tonic-gate 		 * where this probe was sent to.
8047c478bd9Sstevel@tonic-gate 		 */
8057c478bd9Sstevel@tonic-gate 		if (send_reply) {
8067c478bd9Sstevel@tonic-gate 			(void) find_dstaddr(ntohs(icmp6->icmp6_seq), &dst_addr);
8077c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
8087c478bd9Sstevel@tonic-gate 			    pr_name((char *)&dst_addr.addr6,  AF_INET6));
8097c478bd9Sstevel@tonic-gate 		} else {
8107c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s: ", cc,
8117c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from6->sin6_addr, AF_INET6));
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		Printf("icmp_seq=%d. ", ntohs(icmp6->icmp6_seq));
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 		if (valid_reply && datalen >= sizeof (struct timeval) &&
8167c478bd9Sstevel@tonic-gate 		    cc_left >= sizeof (struct timeval)) {
8177c478bd9Sstevel@tonic-gate 			/* LINTED */
8187c478bd9Sstevel@tonic-gate 			tp = (struct timeval *)&icmp6->icmp6_data16[2];
8197c478bd9Sstevel@tonic-gate 			(void) tvsub(&tv, tp);
8207c478bd9Sstevel@tonic-gate 			triptime = (int64_t)tv.tv_sec * MICROSEC + tv.tv_usec;
8217c478bd9Sstevel@tonic-gate 			Printf("time=" TIMEFORMAT " ms", triptime/1000.0);
8227c478bd9Sstevel@tonic-gate 			tsum += triptime;
8237c478bd9Sstevel@tonic-gate 			tsum2 += triptime*triptime;
8247c478bd9Sstevel@tonic-gate 			if (triptime < tmin)
8257c478bd9Sstevel@tonic-gate 				tmin = triptime;
8267c478bd9Sstevel@tonic-gate 			if (triptime > tmax)
8277c478bd9Sstevel@tonic-gate 				tmax = triptime;
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
8307c478bd9Sstevel@tonic-gate 		/*
8317c478bd9Sstevel@tonic-gate 		 * If it's stats, probe-all, npackets > 0, and we received reply
8327c478bd9Sstevel@tonic-gate 		 * for the last probe sent to this target address, then we
8337c478bd9Sstevel@tonic-gate 		 * don't need to wait anymore, let's move on to next target
8347c478bd9Sstevel@tonic-gate 		 * address, now!
8357c478bd9Sstevel@tonic-gate 		 */
8367c478bd9Sstevel@tonic-gate 		if (last_reply_from_targetaddr) {
8377c478bd9Sstevel@tonic-gate 			(void) alarm(0);	/* cancel alarm */
8387c478bd9Sstevel@tonic-gate 			current_targetaddr->probing_done = _B_TRUE;
8397c478bd9Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
8407c478bd9Sstevel@tonic-gate 			send_scheduled_probe();
8417c478bd9Sstevel@tonic-gate 			schedule_sigalrm();
8427c478bd9Sstevel@tonic-gate 		}
8437c478bd9Sstevel@tonic-gate 		break;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	case MLD_LISTENER_QUERY:
8467c478bd9Sstevel@tonic-gate 	case MLD_LISTENER_REPORT:
8477c478bd9Sstevel@tonic-gate 	case MLD_LISTENER_REDUCTION:
8487c478bd9Sstevel@tonic-gate 	case ND_ROUTER_SOLICIT:
8497c478bd9Sstevel@tonic-gate 	case ND_ROUTER_ADVERT:
8507c478bd9Sstevel@tonic-gate 	case ND_NEIGHBOR_SOLICIT:
8517c478bd9Sstevel@tonic-gate 	case ND_NEIGHBOR_ADVERT:
8527c478bd9Sstevel@tonic-gate 		return;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	case ND_REDIRECT:
8557c478bd9Sstevel@tonic-gate 		nd_rdrct = (nd_redirect_t *)icmp6;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 		if (cc_left < sizeof (nd_redirect_t) - ICMP6_MINLEN) {
8587c478bd9Sstevel@tonic-gate 			if (verbose) {
8597c478bd9Sstevel@tonic-gate 				Printf("packet too short (%d bytes) from %s\n",
8607c478bd9Sstevel@tonic-gate 				    cc,
8617c478bd9Sstevel@tonic-gate 				    pr_name((char *)&from6->sin6_addr,
8627c478bd9Sstevel@tonic-gate 					AF_INET6));
8637c478bd9Sstevel@tonic-gate 			}
8647c478bd9Sstevel@tonic-gate 			return;
8657c478bd9Sstevel@tonic-gate 		}
8667c478bd9Sstevel@tonic-gate 		dst_addr.addr6 = nd_rdrct->nd_rd_dst;
8677c478bd9Sstevel@tonic-gate 		if (is_a_target(ai_dst, &dst_addr) || verbose) {
8687c478bd9Sstevel@tonic-gate 			Printf("ICMPv6 redirect from gateway %s\n",
8697c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from6->sin6_addr, AF_INET6));
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 			Printf(" to %s",
8727c478bd9Sstevel@tonic-gate 			    pr_name((char *)&nd_rdrct->nd_rd_target, AF_INET6));
8737c478bd9Sstevel@tonic-gate 			Printf(" for %s\n",
8747c478bd9Sstevel@tonic-gate 			    pr_name((char *)&nd_rdrct->nd_rd_dst, AF_INET6));
8757c478bd9Sstevel@tonic-gate 		}
8767c478bd9Sstevel@tonic-gate 		break;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	default:
8797c478bd9Sstevel@tonic-gate 		if (verbose) {
8807c478bd9Sstevel@tonic-gate 			Printf("%d bytes from %s:\n", cc,
8817c478bd9Sstevel@tonic-gate 			    pr_name((char *)&from6->sin6_addr, AF_INET6));
8827c478bd9Sstevel@tonic-gate 			Printf("icmp6_type=%d (%s) ", icmp6->icmp6_type,
8837c478bd9Sstevel@tonic-gate 			    pr_type6(icmp6->icmp6_type));
8847c478bd9Sstevel@tonic-gate 			Printf("icmp6_code=%d\n", icmp6->icmp6_code);
8857c478bd9Sstevel@tonic-gate 			for (i = 0; i < 12; i++) {
8867c478bd9Sstevel@tonic-gate 				Printf("x%2.2x: x%8.8x\n",
8877c478bd9Sstevel@tonic-gate 				    i * sizeof (int32_t), *intp++);
8887c478bd9Sstevel@tonic-gate 			}
8897c478bd9Sstevel@tonic-gate 		}
8907c478bd9Sstevel@tonic-gate 		break;
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * If it's verbose mode and we recv'd ancillary data, print extension
8957c478bd9Sstevel@tonic-gate 	 * headers.
8967c478bd9Sstevel@tonic-gate 	 */
8977c478bd9Sstevel@tonic-gate 	if (verbose && msg->msg_controllen > 0)
8987c478bd9Sstevel@tonic-gate 		pr_ext_headers(msg);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate  * Convert an ICMP6 "type" field to a printable string.
9037c478bd9Sstevel@tonic-gate  */
9047c478bd9Sstevel@tonic-gate static char *
pr_type6(uchar_t icmp6_type)9057c478bd9Sstevel@tonic-gate pr_type6(uchar_t icmp6_type)
9067c478bd9Sstevel@tonic-gate {
9077c478bd9Sstevel@tonic-gate 	static struct icmptype_table ttab6[] = {
9087c478bd9Sstevel@tonic-gate 		{ICMP6_DST_UNREACH,		"Dest Unreachable"},
9097c478bd9Sstevel@tonic-gate 		{ICMP6_PACKET_TOO_BIG,		"Packet Too Big"},
9107c478bd9Sstevel@tonic-gate 		{ICMP6_TIME_EXCEEDED,		"Time Exceeded"},
9117c478bd9Sstevel@tonic-gate 		{ICMP6_PARAM_PROB,		"Parameter Problem"},
9127c478bd9Sstevel@tonic-gate 		{ICMP6_ECHO_REQUEST,		"Echo Request"},
9137c478bd9Sstevel@tonic-gate 		{ICMP6_ECHO_REPLY,		"Echo Reply"},
9147c478bd9Sstevel@tonic-gate 		{MLD_LISTENER_QUERY,		"Multicast Listener Query"},
9157c478bd9Sstevel@tonic-gate 		{MLD_LISTENER_REPORT,		"Multicast Listener Report"},
9167c478bd9Sstevel@tonic-gate 		{MLD_LISTENER_REDUCTION,	"Multicast Listener Done"},
9177c478bd9Sstevel@tonic-gate 		{ND_ROUTER_SOLICIT,		"Router Solicitation"},
9187c478bd9Sstevel@tonic-gate 		{ND_ROUTER_ADVERT,		"Router Advertisement"},
9197c478bd9Sstevel@tonic-gate 		{ND_NEIGHBOR_SOLICIT,		"Neighbor Solicitation"},
9207c478bd9Sstevel@tonic-gate 		{ND_NEIGHBOR_ADVERT,		"Neighbor Advertisement"},
9217c478bd9Sstevel@tonic-gate 		{ND_REDIRECT,			"Redirect Message"},
9227c478bd9Sstevel@tonic-gate 	};
9237c478bd9Sstevel@tonic-gate 	int i;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	for (i = 0; i < A_CNT(ttab6); i++) {
9267c478bd9Sstevel@tonic-gate 		if (ttab6[i].type == icmp6_type)
9277c478bd9Sstevel@tonic-gate 			return (ttab6[i].message);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	return ("OUT-OF-RANGE");
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate /*
9357c478bd9Sstevel@tonic-gate  * Return the length of the IPv6 related headers (including extension headers).
9367c478bd9Sstevel@tonic-gate  * It also sets the *last_hdr_rtrn to the first upper layer protocol header
9377c478bd9Sstevel@tonic-gate  * following IPv6 header and extension headers. If print_flag is _B_TRUE, it
9387c478bd9Sstevel@tonic-gate  * prints extension headers.
9397c478bd9Sstevel@tonic-gate  */
9407c478bd9Sstevel@tonic-gate static int
IPv6_hdrlen(ip6_t * ip6h,int pkt_len,uint8_t * last_hdr_rtrn)9417c478bd9Sstevel@tonic-gate IPv6_hdrlen(ip6_t *ip6h, int pkt_len, uint8_t *last_hdr_rtrn)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	int length;
9447c478bd9Sstevel@tonic-gate 	int exthdrlength;
9457c478bd9Sstevel@tonic-gate 	uint8_t nexthdr;
9467c478bd9Sstevel@tonic-gate 	uint8_t *whereptr;
9477c478bd9Sstevel@tonic-gate 	ip6_hbh_t *hbhhdr;
9487c478bd9Sstevel@tonic-gate 	ip6_dest_t *desthdr;
9497c478bd9Sstevel@tonic-gate 	ip6_rthdr_t *rthdr;
9507c478bd9Sstevel@tonic-gate 	ip6_frag_t *fraghdr;
9517c478bd9Sstevel@tonic-gate 	uint8_t	*endptr;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	length = sizeof (ip6_t);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	whereptr = ((uint8_t *)&ip6h[1]); 	/* point to next hdr */
9567c478bd9Sstevel@tonic-gate 	endptr = ((uint8_t *)ip6h) + pkt_len;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	nexthdr = ip6h->ip6_nxt;
9597c478bd9Sstevel@tonic-gate 	*last_hdr_rtrn = IPPROTO_NONE;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	if (whereptr >= endptr)
9627c478bd9Sstevel@tonic-gate 		return (length);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	while (whereptr < endptr) {
9657c478bd9Sstevel@tonic-gate 		*last_hdr_rtrn = nexthdr;
9667c478bd9Sstevel@tonic-gate 		switch (nexthdr) {
9677c478bd9Sstevel@tonic-gate 		case IPPROTO_HOPOPTS:
9687c478bd9Sstevel@tonic-gate 			hbhhdr = (ip6_hbh_t *)whereptr;
9697c478bd9Sstevel@tonic-gate 			exthdrlength = 8 * (hbhhdr->ip6h_len + 1);
9707c478bd9Sstevel@tonic-gate 			if ((uchar_t *)hbhhdr + exthdrlength > endptr)
9717c478bd9Sstevel@tonic-gate 				return (length);
9727c478bd9Sstevel@tonic-gate 			nexthdr = hbhhdr->ip6h_nxt;
9737c478bd9Sstevel@tonic-gate 			length += exthdrlength;
9747c478bd9Sstevel@tonic-gate 			break;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 		case IPPROTO_DSTOPTS:
9777c478bd9Sstevel@tonic-gate 			desthdr = (ip6_dest_t *)whereptr;
9787c478bd9Sstevel@tonic-gate 			exthdrlength = 8 * (desthdr->ip6d_len + 1);
9797c478bd9Sstevel@tonic-gate 			if ((uchar_t *)desthdr + exthdrlength > endptr)
9807c478bd9Sstevel@tonic-gate 				return (length);
9817c478bd9Sstevel@tonic-gate 			nexthdr = desthdr->ip6d_nxt;
9827c478bd9Sstevel@tonic-gate 			length += exthdrlength;
9837c478bd9Sstevel@tonic-gate 			break;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		case IPPROTO_ROUTING:
9867c478bd9Sstevel@tonic-gate 			rthdr = (ip6_rthdr_t *)whereptr;
9877c478bd9Sstevel@tonic-gate 			exthdrlength = 8 * (rthdr->ip6r_len + 1);
9887c478bd9Sstevel@tonic-gate 			if ((uchar_t *)rthdr + exthdrlength > endptr)
9897c478bd9Sstevel@tonic-gate 				return (length);
9907c478bd9Sstevel@tonic-gate 			nexthdr = rthdr->ip6r_nxt;
9917c478bd9Sstevel@tonic-gate 			length += exthdrlength;
9927c478bd9Sstevel@tonic-gate 			break;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 		case IPPROTO_FRAGMENT:
9957c478bd9Sstevel@tonic-gate 			/* LINTED */
9967c478bd9Sstevel@tonic-gate 			fraghdr = (ip6_frag_t *)whereptr;
9977c478bd9Sstevel@tonic-gate 			if ((uchar_t *)&fraghdr[1] > endptr)
9987c478bd9Sstevel@tonic-gate 				return (length);
9997c478bd9Sstevel@tonic-gate 			nexthdr = fraghdr->ip6f_nxt;
10007c478bd9Sstevel@tonic-gate 			length += sizeof (struct ip6_frag);
10017c478bd9Sstevel@tonic-gate 			break;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 		case IPPROTO_NONE:
10047c478bd9Sstevel@tonic-gate 		default:
10057c478bd9Sstevel@tonic-gate 			return (length);
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 		whereptr = (uint8_t *)ip6h + length;
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 	*last_hdr_rtrn = nexthdr;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	return (length);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate /*
10157c478bd9Sstevel@tonic-gate  * Print extension headers
10167c478bd9Sstevel@tonic-gate  */
10177c478bd9Sstevel@tonic-gate static void
pr_ext_headers(struct msghdr * msg)10187c478bd9Sstevel@tonic-gate pr_ext_headers(struct msghdr *msg)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	Printf("  IPv6 extension headers: ");
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
10257c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
10267c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == IPPROTO_IPV6) {
10277c478bd9Sstevel@tonic-gate 			switch (cmsg->cmsg_type) {
10287c478bd9Sstevel@tonic-gate 			case IPV6_HOPOPTS:
10297c478bd9Sstevel@tonic-gate 				Printf(" <hop-by-hop options>");
10307c478bd9Sstevel@tonic-gate 				break;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 			case IPV6_DSTOPTS:
10337c478bd9Sstevel@tonic-gate 				Printf(" <destination options (after routing"
10347c478bd9Sstevel@tonic-gate 				    "header)>");
10357c478bd9Sstevel@tonic-gate 				break;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 			case IPV6_RTHDRDSTOPTS:
10387c478bd9Sstevel@tonic-gate 				Printf(" <destination options (before routing"
10397c478bd9Sstevel@tonic-gate 				    "header)>");
10407c478bd9Sstevel@tonic-gate 				break;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 			case IPV6_RTHDR:
10437c478bd9Sstevel@tonic-gate 				pr_rthdr((uchar_t *)CMSG_DATA(cmsg));
10447c478bd9Sstevel@tonic-gate 				break;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 			default:
10477c478bd9Sstevel@tonic-gate 				Printf(" <option type %d>", cmsg->cmsg_type);
10487c478bd9Sstevel@tonic-gate 				break;
10497c478bd9Sstevel@tonic-gate 			}
10507c478bd9Sstevel@tonic-gate 		}
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate /*
10567c478bd9Sstevel@tonic-gate  * Print the routing header 0 information
10577c478bd9Sstevel@tonic-gate  */
10587c478bd9Sstevel@tonic-gate static void
pr_rthdr(uchar_t * buf)10597c478bd9Sstevel@tonic-gate pr_rthdr(uchar_t *buf)
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate 	ip6_rthdr_t *rthdr;
10627c478bd9Sstevel@tonic-gate 	ip6_rthdr0_t *rthdr0;
10637c478bd9Sstevel@tonic-gate 	struct in6_addr *gw_addr;
10647c478bd9Sstevel@tonic-gate 	int i, num_addr;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	rthdr = (ip6_rthdr_t *)buf;
10677c478bd9Sstevel@tonic-gate 	Printf(" <type %d routing header, segleft %u> ",
10687c478bd9Sstevel@tonic-gate 	    rthdr->ip6r_type, rthdr->ip6r_segleft);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	if (rthdr->ip6r_type == 0) {
10717c478bd9Sstevel@tonic-gate 		/* LINTED */
10727c478bd9Sstevel@tonic-gate 		rthdr0 = (ip6_rthdr0_t *)buf;
10737c478bd9Sstevel@tonic-gate 		gw_addr = (struct in6_addr *)(rthdr0 + 1);
10747c478bd9Sstevel@tonic-gate 		num_addr = rthdr0->ip6r0_len / 2;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 		for (i = 0; i < num_addr; i++) {
10777c478bd9Sstevel@tonic-gate 			Printf("%s", pr_name((char *)gw_addr, AF_INET6));
10787c478bd9Sstevel@tonic-gate 			if (i == (num_addr - rthdr0->ip6r0_segleft))
10797c478bd9Sstevel@tonic-gate 				Printf("(Current)");
10807c478bd9Sstevel@tonic-gate 			gw_addr++;
10817c478bd9Sstevel@tonic-gate 			if (i != num_addr - 1)
10827c478bd9Sstevel@tonic-gate 				Printf(",  ");
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate }
1086