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