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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
222b24ab6bSSebastien Roy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*7d897698SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <fcntl.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/time.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <net/if.h>
377c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
387c478bd9Sstevel@tonic-gate #include <netinet/in.h>
397c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
407c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
417c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
427c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
437c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
4445916cd2Sjpk #include <inet/ip.h>
457c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
467c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
477c478bd9Sstevel@tonic-gate #include <netdb.h>
4845916cd2Sjpk #include <tsol/label.h>
4945916cd2Sjpk #include <sys/tsol/tndb.h>
5045916cd2Sjpk #include <sys/tsol/label_macro.h>
5145916cd2Sjpk 
527c478bd9Sstevel@tonic-gate #include "snoop.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * IPv6 extension header masks.  These are used by the print_ipv6_extensions()
577c478bd9Sstevel@tonic-gate  * function to return information to the caller about which extension headers
587c478bd9Sstevel@tonic-gate  * were processed.  This can be useful if the caller wants to know if the
597c478bd9Sstevel@tonic-gate  * packet is an IPv6 fragment, for example.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate #define	SNOOP_HOPOPTS	0x01U
627c478bd9Sstevel@tonic-gate #define	SNOOP_ROUTING	0x02U
637c478bd9Sstevel@tonic-gate #define	SNOOP_DSTOPTS	0x04U
647c478bd9Sstevel@tonic-gate #define	SNOOP_FRAGMENT	0x08U
657c478bd9Sstevel@tonic-gate #define	SNOOP_AH	0x10U
667c478bd9Sstevel@tonic-gate #define	SNOOP_ESP	0x20U
677c478bd9Sstevel@tonic-gate #define	SNOOP_IPV6	0x40U
687c478bd9Sstevel@tonic-gate 
6945916cd2Sjpk static void prt_routing_hdr(int, const struct ip6_rthdr *);
7045916cd2Sjpk static void prt_fragment_hdr(int, const struct ip6_frag *);
7145916cd2Sjpk static void prt_hbh_options(int, const struct ip6_hbh *);
7245916cd2Sjpk static void prt_dest_options(int, const struct ip6_dest *);
7345916cd2Sjpk static void print_route(const uchar_t *);
7445916cd2Sjpk static void print_ipoptions(const uchar_t *, int);
7545916cd2Sjpk static void print_ripso(const uchar_t *);
7645916cd2Sjpk static void print_cipso(const uchar_t *);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* Keep track of how many nested IP headers we have. */
797c478bd9Sstevel@tonic-gate unsigned int encap_levels;
807c478bd9Sstevel@tonic-gate unsigned int total_encap_levels = 1;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate int
interpret_ip(int flags,const struct ip * ip,int fraglen)8345916cd2Sjpk interpret_ip(int flags, const struct ip *ip, int fraglen)
847c478bd9Sstevel@tonic-gate {
8545916cd2Sjpk 	uchar_t *data;
867c478bd9Sstevel@tonic-gate 	char buff[24];
877c478bd9Sstevel@tonic-gate 	boolean_t isfrag = B_FALSE;
887c478bd9Sstevel@tonic-gate 	boolean_t morefrag;
897c478bd9Sstevel@tonic-gate 	uint16_t fragoffset;
907c478bd9Sstevel@tonic-gate 	int hdrlen;
917c478bd9Sstevel@tonic-gate 	uint16_t iplen, uitmp;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (ip->ip_v == IPV6_VERSION) {
947c478bd9Sstevel@tonic-gate 		iplen = interpret_ipv6(flags, (ip6_t *)ip, fraglen);
957c478bd9Sstevel@tonic-gate 		return (iplen);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	if (encap_levels == 0)
997c478bd9Sstevel@tonic-gate 		total_encap_levels = 0;
1007c478bd9Sstevel@tonic-gate 	encap_levels++;
1017c478bd9Sstevel@tonic-gate 	total_encap_levels++;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	hdrlen = ip->ip_hl * 4;
10445916cd2Sjpk 	data = ((uchar_t *)ip) + hdrlen;
1057c478bd9Sstevel@tonic-gate 	iplen = ntohs(ip->ip_len) - hdrlen;
1067c478bd9Sstevel@tonic-gate 	fraglen -= hdrlen;
1077c478bd9Sstevel@tonic-gate 	if (fraglen > iplen)
1087c478bd9Sstevel@tonic-gate 		fraglen = iplen;
1097c478bd9Sstevel@tonic-gate 	if (fraglen < 0) {
1107c478bd9Sstevel@tonic-gate 		(void) snprintf(get_sum_line(), MAXLINE,
1117c478bd9Sstevel@tonic-gate 		    "IP truncated: header missing %d bytes", -fraglen);
1127c478bd9Sstevel@tonic-gate 		encap_levels--;
1137c478bd9Sstevel@tonic-gate 		return (fraglen + iplen);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * We flag this as a fragment if the more fragments bit is set, or
1177c478bd9Sstevel@tonic-gate 	 * if the fragment offset is non-zero.
1187c478bd9Sstevel@tonic-gate 	 */
1197c478bd9Sstevel@tonic-gate 	morefrag = (ntohs(ip->ip_off) & IP_MF) == 0 ? B_FALSE : B_TRUE;
1207c478bd9Sstevel@tonic-gate 	fragoffset = (ntohs(ip->ip_off) & 0x1FFF) * 8;
1217c478bd9Sstevel@tonic-gate 	if (morefrag || fragoffset != 0)
1227c478bd9Sstevel@tonic-gate 		isfrag = B_TRUE;
1237c478bd9Sstevel@tonic-gate 
1242b24ab6bSSebastien Roy 	src_name = addrtoname(AF_INET, &ip->ip_src);
1252b24ab6bSSebastien Roy 	dst_name = addrtoname(AF_INET, &ip->ip_dst);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
1287c478bd9Sstevel@tonic-gate 		if (isfrag) {
1297c478bd9Sstevel@tonic-gate 			(void) snprintf(get_sum_line(), MAXLINE,
1307c478bd9Sstevel@tonic-gate 			    "%s IP fragment ID=%d Offset=%-4d MF=%d TOS=0x%x "
1317c478bd9Sstevel@tonic-gate 			    "TTL=%d",
1327c478bd9Sstevel@tonic-gate 			    getproto(ip->ip_p),
1337c478bd9Sstevel@tonic-gate 			    ntohs(ip->ip_id),
1347c478bd9Sstevel@tonic-gate 			    fragoffset,
1357c478bd9Sstevel@tonic-gate 			    morefrag,
1367c478bd9Sstevel@tonic-gate 			    ip->ip_tos,
1377c478bd9Sstevel@tonic-gate 			    ip->ip_ttl);
1387c478bd9Sstevel@tonic-gate 		} else {
1397c478bd9Sstevel@tonic-gate 			(void) strlcpy(buff, inet_ntoa(ip->ip_dst),
1407c478bd9Sstevel@tonic-gate 			    sizeof (buff));
1417c478bd9Sstevel@tonic-gate 			uitmp = ntohs(ip->ip_len);
1427c478bd9Sstevel@tonic-gate 			(void) snprintf(get_sum_line(), MAXLINE,
1437c478bd9Sstevel@tonic-gate 			    "IP  D=%s S=%s LEN=%u%s, ID=%d, TOS=0x%x, TTL=%d",
1447c478bd9Sstevel@tonic-gate 			    buff,
1457c478bd9Sstevel@tonic-gate 			    inet_ntoa(ip->ip_src),
1467c478bd9Sstevel@tonic-gate 			    uitmp,
1477c478bd9Sstevel@tonic-gate 			    iplen > fraglen ? "?" : "",
1487c478bd9Sstevel@tonic-gate 			    ntohs(ip->ip_id),
1497c478bd9Sstevel@tonic-gate 			    ip->ip_tos,
1507c478bd9Sstevel@tonic-gate 			    ip->ip_ttl);
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
1557c478bd9Sstevel@tonic-gate 		show_header("IP:   ", "IP Header", iplen);
1567c478bd9Sstevel@tonic-gate 		show_space();
15745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
15845916cd2Sjpk 		    "Version = %d", ip->ip_v);
15945916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
16045916cd2Sjpk 		    "Header length = %d bytes", hdrlen);
16145916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
16245916cd2Sjpk 		    "Type of service = 0x%02x", ip->ip_tos);
16345916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
16445916cd2Sjpk 		    "      xxx. .... = %d (precedence)",
1657c478bd9Sstevel@tonic-gate 		    ip->ip_tos >> 5);
16645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
16745916cd2Sjpk 		    "      %s", getflag(ip->ip_tos, IPTOS_LOWDELAY,
1687c478bd9Sstevel@tonic-gate 		    "low delay", "normal delay"));
16945916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1707c478bd9Sstevel@tonic-gate 		    getflag(ip->ip_tos, IPTOS_THROUGHPUT,
1717c478bd9Sstevel@tonic-gate 		    "high throughput", "normal throughput"));
17245916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1737c478bd9Sstevel@tonic-gate 		    getflag(ip->ip_tos, IPTOS_RELIABILITY,
1747c478bd9Sstevel@tonic-gate 		    "high reliability", "normal reliability"));
17545916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1767c478bd9Sstevel@tonic-gate 		    getflag(ip->ip_tos, IPTOS_ECT,
1777c478bd9Sstevel@tonic-gate 		    "ECN capable transport", "not ECN capable transport"));
17845916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1797c478bd9Sstevel@tonic-gate 		    getflag(ip->ip_tos, IPTOS_CE,
1807c478bd9Sstevel@tonic-gate 		    "ECN congestion experienced",
1817c478bd9Sstevel@tonic-gate 		    "no ECN congestion experienced"));
1827c478bd9Sstevel@tonic-gate 		/* warning: ip_len is signed in netinet/ip.h */
1837c478bd9Sstevel@tonic-gate 		uitmp = ntohs(ip->ip_len);
18445916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
18545916cd2Sjpk 		    "Total length = %u bytes%s", uitmp,
1867c478bd9Sstevel@tonic-gate 		    iplen > fraglen ? " -- truncated" : "");
18745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
18845916cd2Sjpk 		    "Identification = %d", ntohs(ip->ip_id));
1897c478bd9Sstevel@tonic-gate 		/* warning: ip_off is signed in netinet/ip.h */
1907c478bd9Sstevel@tonic-gate 		uitmp = ntohs(ip->ip_off);
19145916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
19245916cd2Sjpk 		    "Flags = 0x%x", uitmp >> 12);
19345916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1947c478bd9Sstevel@tonic-gate 		    getflag(uitmp >> 8, IP_DF >> 8,
1957c478bd9Sstevel@tonic-gate 		    "do not fragment", "may fragment"));
19645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1977c478bd9Sstevel@tonic-gate 		    getflag(uitmp >> 8, IP_MF >> 8,
1987c478bd9Sstevel@tonic-gate 		    "more fragments", "last fragment"));
19945916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
20045916cd2Sjpk 		    "Fragment offset = %u bytes",
2017c478bd9Sstevel@tonic-gate 		    fragoffset);
20245916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
20345916cd2Sjpk 		    "Time to live = %d seconds/hops",
2047c478bd9Sstevel@tonic-gate 		    ip->ip_ttl);
20545916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
20645916cd2Sjpk 		    "Protocol = %d (%s)", ip->ip_p,
2077c478bd9Sstevel@tonic-gate 		    getproto(ip->ip_p));
2087c478bd9Sstevel@tonic-gate 		/*
2097c478bd9Sstevel@tonic-gate 		 * XXX need to compute checksum and print whether it's correct
2107c478bd9Sstevel@tonic-gate 		 */
21145916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
21245916cd2Sjpk 		    "Header checksum = %04x",
2137c478bd9Sstevel@tonic-gate 		    ntohs(ip->ip_sum));
21445916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
21545916cd2Sjpk 		    "Source address = %s, %s",
2167c478bd9Sstevel@tonic-gate 		    inet_ntoa(ip->ip_src), addrtoname(AF_INET, &ip->ip_src));
21745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
21845916cd2Sjpk 		    "Destination address = %s, %s",
2197c478bd9Sstevel@tonic-gate 		    inet_ntoa(ip->ip_dst), addrtoname(AF_INET, &ip->ip_dst));
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		/* Print IP options - if any */
2227c478bd9Sstevel@tonic-gate 
22345916cd2Sjpk 		print_ipoptions((const uchar_t *)(ip + 1),
22445916cd2Sjpk 		    hdrlen - sizeof (struct ip));
2257c478bd9Sstevel@tonic-gate 		show_space();
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * If we are in detail mode, and this is not the first fragment of
2307c478bd9Sstevel@tonic-gate 	 * a fragmented packet, print out a little line stating this.
2317c478bd9Sstevel@tonic-gate 	 * Otherwise, go to the next protocol layer only if this is not a
2327c478bd9Sstevel@tonic-gate 	 * fragment, or we are in detail mode and this is the first fragment
2337c478bd9Sstevel@tonic-gate 	 * of a fragmented packet.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL && fragoffset != 0) {
23645916cd2Sjpk 		(void) snprintf(get_detail_line(0, 0), MAXLINE,
2377c478bd9Sstevel@tonic-gate 		    "%s:  [%d byte(s) of data, continuation of IP ident=%d]",
2387c478bd9Sstevel@tonic-gate 		    getproto(ip->ip_p),
2397c478bd9Sstevel@tonic-gate 		    iplen,
2407c478bd9Sstevel@tonic-gate 		    ntohs(ip->ip_id));
2417c478bd9Sstevel@tonic-gate 	} else if (!isfrag || (flags & F_DTAIL) && isfrag && fragoffset == 0) {
2427c478bd9Sstevel@tonic-gate 		/* go to the next protocol layer */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		if (fraglen > 0) {
2457c478bd9Sstevel@tonic-gate 			switch (ip->ip_p) {
2467c478bd9Sstevel@tonic-gate 			case IPPROTO_IP:
2477c478bd9Sstevel@tonic-gate 				break;
2487c478bd9Sstevel@tonic-gate 			case IPPROTO_ENCAP:
24945916cd2Sjpk 				(void) interpret_ip(flags,
25045916cd2Sjpk 				    /* LINTED: alignment */
25145916cd2Sjpk 				    (const struct ip *)data, fraglen);
2527c478bd9Sstevel@tonic-gate 				break;
2537c478bd9Sstevel@tonic-gate 			case IPPROTO_ICMP:
25445916cd2Sjpk 				(void) interpret_icmp(flags,
25545916cd2Sjpk 				    /* LINTED: alignment */
25645916cd2Sjpk 				    (struct icmp *)data, iplen, fraglen);
2577c478bd9Sstevel@tonic-gate 				break;
2587c478bd9Sstevel@tonic-gate 			case IPPROTO_IGMP:
2597c478bd9Sstevel@tonic-gate 				interpret_igmp(flags, data, iplen, fraglen);
2607c478bd9Sstevel@tonic-gate 				break;
2617c478bd9Sstevel@tonic-gate 			case IPPROTO_GGP:
2627c478bd9Sstevel@tonic-gate 				break;
2637c478bd9Sstevel@tonic-gate 			case IPPROTO_TCP:
26445916cd2Sjpk 				(void) interpret_tcp(flags,
26545916cd2Sjpk 				    (struct tcphdr *)data, iplen, fraglen);
2667c478bd9Sstevel@tonic-gate 				break;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 			case IPPROTO_ESP:
26945916cd2Sjpk 				(void) interpret_esp(flags, data, iplen,
27045916cd2Sjpk 				    fraglen);
2717c478bd9Sstevel@tonic-gate 				break;
2727c478bd9Sstevel@tonic-gate 			case IPPROTO_AH:
27345916cd2Sjpk 				(void) interpret_ah(flags, data, iplen,
27445916cd2Sjpk 				    fraglen);
2757c478bd9Sstevel@tonic-gate 				break;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 			case IPPROTO_OSPF:
2787c478bd9Sstevel@tonic-gate 				interpret_ospf(flags, data, iplen, fraglen);
2797c478bd9Sstevel@tonic-gate 				break;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 			case IPPROTO_EGP:
2827c478bd9Sstevel@tonic-gate 			case IPPROTO_PUP:
2837c478bd9Sstevel@tonic-gate 				break;
2847c478bd9Sstevel@tonic-gate 			case IPPROTO_UDP:
28545916cd2Sjpk 				(void) interpret_udp(flags,
28645916cd2Sjpk 				    (struct udphdr *)data, iplen, fraglen);
2877c478bd9Sstevel@tonic-gate 				break;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 			case IPPROTO_IDP:
2907c478bd9Sstevel@tonic-gate 			case IPPROTO_HELLO:
2917c478bd9Sstevel@tonic-gate 			case IPPROTO_ND:
2927c478bd9Sstevel@tonic-gate 			case IPPROTO_RAW:
2937c478bd9Sstevel@tonic-gate 				break;
2947c478bd9Sstevel@tonic-gate 			case IPPROTO_IPV6:	/* IPV6 encap */
29545916cd2Sjpk 				/* LINTED: alignment */
2967c478bd9Sstevel@tonic-gate 				(void) interpret_ipv6(flags, (ip6_t *)data,
2977c478bd9Sstevel@tonic-gate 				    iplen);
2987c478bd9Sstevel@tonic-gate 				break;
2997c478bd9Sstevel@tonic-gate 			case IPPROTO_SCTP:
30045916cd2Sjpk 				(void) interpret_sctp(flags,
30145916cd2Sjpk 				    (struct sctp_hdr *)data, iplen, fraglen);
3027c478bd9Sstevel@tonic-gate 				break;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	encap_levels--;
3087c478bd9Sstevel@tonic-gate 	return (iplen);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate int
interpret_ipv6(int flags,const ip6_t * ip6h,int fraglen)31245916cd2Sjpk interpret_ipv6(int flags, const ip6_t *ip6h, int fraglen)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	uint8_t *data;
3157c478bd9Sstevel@tonic-gate 	int hdrlen, iplen;
3167c478bd9Sstevel@tonic-gate 	int version, flow, class;
3177c478bd9Sstevel@tonic-gate 	uchar_t proto;
3187c478bd9Sstevel@tonic-gate 	boolean_t isfrag = B_FALSE;
3197c478bd9Sstevel@tonic-gate 	uint8_t extmask;
3207c478bd9Sstevel@tonic-gate 	/*
3217c478bd9Sstevel@tonic-gate 	 * The print_srcname and print_dstname strings are the hostname
3227c478bd9Sstevel@tonic-gate 	 * parts of the verbose IPv6 header output, including the comma
3237c478bd9Sstevel@tonic-gate 	 * and the space after the litteral address strings.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	char print_srcname[MAXHOSTNAMELEN + 2];
3267c478bd9Sstevel@tonic-gate 	char print_dstname[MAXHOSTNAMELEN + 2];
3277c478bd9Sstevel@tonic-gate 	char src_addrstr[INET6_ADDRSTRLEN];
3287c478bd9Sstevel@tonic-gate 	char dst_addrstr[INET6_ADDRSTRLEN];
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	iplen = ntohs(ip6h->ip6_plen);
3317c478bd9Sstevel@tonic-gate 	hdrlen = IPV6_HDR_LEN;
3327c478bd9Sstevel@tonic-gate 	fraglen -= hdrlen;
3337c478bd9Sstevel@tonic-gate 	if (fraglen < 0)
3347c478bd9Sstevel@tonic-gate 		return (fraglen + hdrlen);
3357c478bd9Sstevel@tonic-gate 	data = ((uint8_t *)ip6h) + hdrlen;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	proto = ip6h->ip6_nxt;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	src_name = addrtoname(AF_INET6, &ip6h->ip6_src);
3407c478bd9Sstevel@tonic-gate 	dst_name = addrtoname(AF_INET6, &ip6h->ip6_dst);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Use endian-aware masks to extract traffic class and
3447c478bd9Sstevel@tonic-gate 	 * flowinfo.  Also, flowinfo is now 20 bits and class 8
3457c478bd9Sstevel@tonic-gate 	 * rather than 24 and 4.
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	class = ntohl((ip6h->ip6_vcf & IPV6_FLOWINFO_TCLASS) >> 20);
3487c478bd9Sstevel@tonic-gate 	flow = ntohl(ip6h->ip6_vcf & IPV6_FLOWINFO_FLOWLABEL);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * NOTE: the F_SUM and F_DTAIL flags are mutually exclusive,
3527c478bd9Sstevel@tonic-gate 	 * so the code within the first part of the following if statement
3537c478bd9Sstevel@tonic-gate 	 * will not affect the detailed printing of the packet.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
35645916cd2Sjpk 		(void) snprintf(get_sum_line(), MAXLINE,
35745916cd2Sjpk 		    "IPv6  S=%s D=%s LEN=%d HOPS=%d CLASS=0x%x FLOW=0x%x",
3587c478bd9Sstevel@tonic-gate 		    src_name, dst_name, iplen, ip6h->ip6_hops, class, flow);
3597c478bd9Sstevel@tonic-gate 	} else if (flags & F_DTAIL) {
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, &ip6h->ip6_src, src_addrstr,
3627c478bd9Sstevel@tonic-gate 		    INET6_ADDRSTRLEN);
3637c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, &ip6h->ip6_dst, dst_addrstr,
3647c478bd9Sstevel@tonic-gate 		    INET6_ADDRSTRLEN);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		version = ntohl(ip6h->ip6_vcf) >> 28;
3677c478bd9Sstevel@tonic-gate 
3682b24ab6bSSebastien Roy 		if (strcmp(src_name, src_addrstr) == 0) {
3697c478bd9Sstevel@tonic-gate 			print_srcname[0] = '\0';
3702b24ab6bSSebastien Roy 		} else {
3717c478bd9Sstevel@tonic-gate 			snprintf(print_srcname, sizeof (print_srcname),
3722b24ab6bSSebastien Roy 			    ", %s", src_name);
3732b24ab6bSSebastien Roy 		}
3747c478bd9Sstevel@tonic-gate 
3752b24ab6bSSebastien Roy 		if (strcmp(dst_name, dst_addrstr) == 0) {
3767c478bd9Sstevel@tonic-gate 			print_dstname[0] = '\0';
3772b24ab6bSSebastien Roy 		} else {
3787c478bd9Sstevel@tonic-gate 			snprintf(print_dstname, sizeof (print_dstname),
3792b24ab6bSSebastien Roy 			    ", %s", dst_name);
3802b24ab6bSSebastien Roy 		}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		show_header("IPv6:   ", "IPv6 Header", iplen);
3837c478bd9Sstevel@tonic-gate 		show_space();
3847c478bd9Sstevel@tonic-gate 
38545916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
3867c478bd9Sstevel@tonic-gate 		    "Version = %d", version);
38745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
3887c478bd9Sstevel@tonic-gate 		    "Traffic Class = %d", class);
38945916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
3907c478bd9Sstevel@tonic-gate 		    "Flow label = 0x%x", flow);
39145916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
39245916cd2Sjpk 		    "Payload length = %d", iplen);
39345916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
39445916cd2Sjpk 		    "Next Header = %d (%s)", proto,
3957c478bd9Sstevel@tonic-gate 		    getproto(proto));
39645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
39745916cd2Sjpk 		    "Hop Limit = %d", ip6h->ip6_hops);
39845916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
3997c478bd9Sstevel@tonic-gate 		    "Source address = %s%s", src_addrstr, print_srcname);
40045916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
4017c478bd9Sstevel@tonic-gate 		    "Destination address = %s%s", dst_addrstr, print_dstname);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		show_space();
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	/*
4077c478bd9Sstevel@tonic-gate 	 * Print IPv6 Extension Headers, or skip them in the summary case.
4087c478bd9Sstevel@tonic-gate 	 * Set isfrag to true if one of the extension headers encounterred
4097c478bd9Sstevel@tonic-gate 	 * was a fragment header.
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	if (proto == IPPROTO_HOPOPTS || proto == IPPROTO_DSTOPTS ||
4127c478bd9Sstevel@tonic-gate 	    proto == IPPROTO_ROUTING || proto == IPPROTO_FRAGMENT) {
4137c478bd9Sstevel@tonic-gate 		extmask = print_ipv6_extensions(flags, &data, &proto, &iplen,
4147c478bd9Sstevel@tonic-gate 		    &fraglen);
4157c478bd9Sstevel@tonic-gate 		if ((extmask & SNOOP_FRAGMENT) != 0) {
4167c478bd9Sstevel@tonic-gate 			isfrag = B_TRUE;
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * We only want to print upper layer information if this is not
4227c478bd9Sstevel@tonic-gate 	 * a fragment, or if we're printing in detail.  Note that the
4237c478bd9Sstevel@tonic-gate 	 * proto variable will be set to IPPROTO_NONE if this is a fragment
4247c478bd9Sstevel@tonic-gate 	 * with a non-zero fragment offset.
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	if (!isfrag || flags & F_DTAIL) {
4277c478bd9Sstevel@tonic-gate 		/* go to the next protocol layer */
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		switch (proto) {
4307c478bd9Sstevel@tonic-gate 		case IPPROTO_IP:
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 		case IPPROTO_ENCAP:
43345916cd2Sjpk 			/* LINTED: alignment */
43445916cd2Sjpk 			(void) interpret_ip(flags, (const struct ip *)data,
43545916cd2Sjpk 			    fraglen);
4367c478bd9Sstevel@tonic-gate 			break;
4377c478bd9Sstevel@tonic-gate 		case IPPROTO_ICMPV6:
43845916cd2Sjpk 			/* LINTED: alignment */
43945916cd2Sjpk 			(void) interpret_icmpv6(flags, (icmp6_t *)data, iplen,
4407c478bd9Sstevel@tonic-gate 			    fraglen);
4417c478bd9Sstevel@tonic-gate 			break;
4427c478bd9Sstevel@tonic-gate 		case IPPROTO_IGMP:
4437c478bd9Sstevel@tonic-gate 			interpret_igmp(flags, data, iplen, fraglen);
4447c478bd9Sstevel@tonic-gate 			break;
4457c478bd9Sstevel@tonic-gate 		case IPPROTO_GGP:
4467c478bd9Sstevel@tonic-gate 			break;
4477c478bd9Sstevel@tonic-gate 		case IPPROTO_TCP:
44845916cd2Sjpk 			(void) interpret_tcp(flags, (struct tcphdr *)data,
44945916cd2Sjpk 			    iplen, fraglen);
4507c478bd9Sstevel@tonic-gate 			break;
4517c478bd9Sstevel@tonic-gate 		case IPPROTO_ESP:
45245916cd2Sjpk 			(void) interpret_esp(flags, data, iplen, fraglen);
4537c478bd9Sstevel@tonic-gate 			break;
4547c478bd9Sstevel@tonic-gate 		case IPPROTO_AH:
45545916cd2Sjpk 			(void) interpret_ah(flags, data, iplen, fraglen);
4567c478bd9Sstevel@tonic-gate 			break;
4577c478bd9Sstevel@tonic-gate 		case IPPROTO_EGP:
4587c478bd9Sstevel@tonic-gate 		case IPPROTO_PUP:
4597c478bd9Sstevel@tonic-gate 			break;
4607c478bd9Sstevel@tonic-gate 		case IPPROTO_UDP:
46145916cd2Sjpk 			(void) interpret_udp(flags, (struct udphdr *)data,
46245916cd2Sjpk 			    iplen, fraglen);
4637c478bd9Sstevel@tonic-gate 			break;
4647c478bd9Sstevel@tonic-gate 		case IPPROTO_IDP:
4657c478bd9Sstevel@tonic-gate 		case IPPROTO_HELLO:
4667c478bd9Sstevel@tonic-gate 		case IPPROTO_ND:
4677c478bd9Sstevel@tonic-gate 		case IPPROTO_RAW:
4687c478bd9Sstevel@tonic-gate 			break;
4697c478bd9Sstevel@tonic-gate 		case IPPROTO_IPV6:
47045916cd2Sjpk 			/* LINTED: alignment */
47145916cd2Sjpk 			(void) interpret_ipv6(flags, (const ip6_t *)data,
47245916cd2Sjpk 			    iplen);
4737c478bd9Sstevel@tonic-gate 			break;
4747c478bd9Sstevel@tonic-gate 		case IPPROTO_SCTP:
47545916cd2Sjpk 			(void) interpret_sctp(flags, (struct sctp_hdr *)data,
47645916cd2Sjpk 			    iplen, fraglen);
4777c478bd9Sstevel@tonic-gate 			break;
4787c478bd9Sstevel@tonic-gate 		case IPPROTO_OSPF:
4797c478bd9Sstevel@tonic-gate 			interpret_ospf6(flags, data, iplen, fraglen);
4807c478bd9Sstevel@tonic-gate 			break;
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	return (iplen);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate /*
4887c478bd9Sstevel@tonic-gate  * ip_ext: data including the extension header.
4897c478bd9Sstevel@tonic-gate  * iplen: length of the data remaining in the packet.
4907c478bd9Sstevel@tonic-gate  * Returns a mask of IPv6 extension headers it processed.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate uint8_t
print_ipv6_extensions(int flags,uint8_t ** hdr,uint8_t * next,int * iplen,int * fraglen)4937c478bd9Sstevel@tonic-gate print_ipv6_extensions(int flags, uint8_t **hdr, uint8_t *next, int *iplen,
4947c478bd9Sstevel@tonic-gate     int *fraglen)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	uint8_t *data_ptr;
4977c478bd9Sstevel@tonic-gate 	uchar_t proto = *next;
4987c478bd9Sstevel@tonic-gate 	boolean_t is_extension_header;
4997c478bd9Sstevel@tonic-gate 	struct ip6_hbh *ipv6ext_hbh;
5007c478bd9Sstevel@tonic-gate 	struct ip6_dest *ipv6ext_dest;
5017c478bd9Sstevel@tonic-gate 	struct ip6_rthdr *ipv6ext_rthdr;
5027c478bd9Sstevel@tonic-gate 	struct ip6_frag *ipv6ext_frag;
5037c478bd9Sstevel@tonic-gate 	uint32_t exthdrlen;
5047c478bd9Sstevel@tonic-gate 	uint8_t extmask = 0;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if ((hdr == NULL) || (*hdr == NULL) || (next == NULL) || (iplen == 0))
5077c478bd9Sstevel@tonic-gate 		return (0);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	data_ptr = *hdr;
5107c478bd9Sstevel@tonic-gate 	is_extension_header = B_TRUE;
5117c478bd9Sstevel@tonic-gate 	while (is_extension_header) {
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		/*
5147c478bd9Sstevel@tonic-gate 		 * There must be at least enough data left to read the
5157c478bd9Sstevel@tonic-gate 		 * next header and header length fields from the next
5167c478bd9Sstevel@tonic-gate 		 * header.
5177c478bd9Sstevel@tonic-gate 		 */
5187c478bd9Sstevel@tonic-gate 		if (*fraglen < 2) {
5197c478bd9Sstevel@tonic-gate 			return (extmask);
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 		switch (proto) {
5237c478bd9Sstevel@tonic-gate 		case IPPROTO_HOPOPTS:
5247c478bd9Sstevel@tonic-gate 			ipv6ext_hbh = (struct ip6_hbh *)data_ptr;
5257c478bd9Sstevel@tonic-gate 			exthdrlen = 8 + ipv6ext_hbh->ip6h_len * 8;
5267c478bd9Sstevel@tonic-gate 			if (*fraglen <= exthdrlen) {
5277c478bd9Sstevel@tonic-gate 				return (extmask);
5287c478bd9Sstevel@tonic-gate 			}
5297c478bd9Sstevel@tonic-gate 			prt_hbh_options(flags, ipv6ext_hbh);
5307c478bd9Sstevel@tonic-gate 			extmask |= SNOOP_HOPOPTS;
5317c478bd9Sstevel@tonic-gate 			proto = ipv6ext_hbh->ip6h_nxt;
5327c478bd9Sstevel@tonic-gate 			break;
5337c478bd9Sstevel@tonic-gate 		case IPPROTO_DSTOPTS:
5347c478bd9Sstevel@tonic-gate 			ipv6ext_dest = (struct ip6_dest *)data_ptr;
5357c478bd9Sstevel@tonic-gate 			exthdrlen = 8 + ipv6ext_dest->ip6d_len * 8;
5367c478bd9Sstevel@tonic-gate 			if (*fraglen <= exthdrlen) {
5377c478bd9Sstevel@tonic-gate 				return (extmask);
5387c478bd9Sstevel@tonic-gate 			}
5397c478bd9Sstevel@tonic-gate 			prt_dest_options(flags, ipv6ext_dest);
5407c478bd9Sstevel@tonic-gate 			extmask |= SNOOP_DSTOPTS;
5417c478bd9Sstevel@tonic-gate 			proto = ipv6ext_dest->ip6d_nxt;
5427c478bd9Sstevel@tonic-gate 			break;
5437c478bd9Sstevel@tonic-gate 		case IPPROTO_ROUTING:
5447c478bd9Sstevel@tonic-gate 			ipv6ext_rthdr = (struct ip6_rthdr *)data_ptr;
5457c478bd9Sstevel@tonic-gate 			exthdrlen = 8 + ipv6ext_rthdr->ip6r_len * 8;
5467c478bd9Sstevel@tonic-gate 			if (*fraglen <= exthdrlen) {
5477c478bd9Sstevel@tonic-gate 				return (extmask);
5487c478bd9Sstevel@tonic-gate 			}
5497c478bd9Sstevel@tonic-gate 			prt_routing_hdr(flags, ipv6ext_rthdr);
5507c478bd9Sstevel@tonic-gate 			extmask |= SNOOP_ROUTING;
5517c478bd9Sstevel@tonic-gate 			proto = ipv6ext_rthdr->ip6r_nxt;
5527c478bd9Sstevel@tonic-gate 			break;
5537c478bd9Sstevel@tonic-gate 		case IPPROTO_FRAGMENT:
55445916cd2Sjpk 			/* LINTED: alignment */
5557c478bd9Sstevel@tonic-gate 			ipv6ext_frag = (struct ip6_frag *)data_ptr;
5567c478bd9Sstevel@tonic-gate 			exthdrlen = sizeof (struct ip6_frag);
5577c478bd9Sstevel@tonic-gate 			if (*fraglen <= exthdrlen) {
5587c478bd9Sstevel@tonic-gate 				return (extmask);
5597c478bd9Sstevel@tonic-gate 			}
5607c478bd9Sstevel@tonic-gate 			prt_fragment_hdr(flags, ipv6ext_frag);
5617c478bd9Sstevel@tonic-gate 			extmask |= SNOOP_FRAGMENT;
5627c478bd9Sstevel@tonic-gate 			/*
5637c478bd9Sstevel@tonic-gate 			 * If this is not the first fragment, forget about
5647c478bd9Sstevel@tonic-gate 			 * the rest of the packet, snoop decoding is
5657c478bd9Sstevel@tonic-gate 			 * stateless.
5667c478bd9Sstevel@tonic-gate 			 */
5677c478bd9Sstevel@tonic-gate 			if ((ipv6ext_frag->ip6f_offlg & IP6F_OFF_MASK) != 0)
5687c478bd9Sstevel@tonic-gate 				proto = IPPROTO_NONE;
5697c478bd9Sstevel@tonic-gate 			else
5707c478bd9Sstevel@tonic-gate 				proto = ipv6ext_frag->ip6f_nxt;
5717c478bd9Sstevel@tonic-gate 			break;
5727c478bd9Sstevel@tonic-gate 		default:
5737c478bd9Sstevel@tonic-gate 			is_extension_header = B_FALSE;
5747c478bd9Sstevel@tonic-gate 			break;
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		if (is_extension_header) {
5787c478bd9Sstevel@tonic-gate 			*iplen -= exthdrlen;
5797c478bd9Sstevel@tonic-gate 			*fraglen -= exthdrlen;
5807c478bd9Sstevel@tonic-gate 			data_ptr += exthdrlen;
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	*next = proto;
5857c478bd9Sstevel@tonic-gate 	*hdr = data_ptr;
5867c478bd9Sstevel@tonic-gate 	return (extmask);
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate static void
print_ipoptions(const uchar_t * opt,int optlen)59045916cd2Sjpk print_ipoptions(const uchar_t *opt, int optlen)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	int len;
59345916cd2Sjpk 	int remain;
5947c478bd9Sstevel@tonic-gate 	char *line;
59545916cd2Sjpk 	const char *truncstr;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	if (optlen <= 0) {
59845916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
5997c478bd9Sstevel@tonic-gate 		    "No options");
6007c478bd9Sstevel@tonic-gate 		return;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
60345916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
6047c478bd9Sstevel@tonic-gate 	    "Options: (%d bytes)", optlen);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	while (optlen > 0) {
60745916cd2Sjpk 		line = get_line(0, 0);
60845916cd2Sjpk 		remain = get_line_remain();
6097c478bd9Sstevel@tonic-gate 		len = opt[1];
61045916cd2Sjpk 		truncstr = len > optlen ? "?" : "";
6117c478bd9Sstevel@tonic-gate 		switch (opt[0]) {
6127c478bd9Sstevel@tonic-gate 		case IPOPT_EOL:
61345916cd2Sjpk 			(void) strlcpy(line, "  - End of option list", remain);
6147c478bd9Sstevel@tonic-gate 			return;
6157c478bd9Sstevel@tonic-gate 		case IPOPT_NOP:
61645916cd2Sjpk 			(void) strlcpy(line, "  - No op", remain);
6177c478bd9Sstevel@tonic-gate 			len = 1;
6187c478bd9Sstevel@tonic-gate 			break;
6197c478bd9Sstevel@tonic-gate 		case IPOPT_RR:
62045916cd2Sjpk 			(void) snprintf(line, remain,
62145916cd2Sjpk 			    "  - Record route (%d bytes%s)", len, truncstr);
6227c478bd9Sstevel@tonic-gate 			print_route(opt);
6237c478bd9Sstevel@tonic-gate 			break;
6247c478bd9Sstevel@tonic-gate 		case IPOPT_TS:
62545916cd2Sjpk 			(void) snprintf(line, remain,
62645916cd2Sjpk 			    "  - Time stamp (%d bytes%s)", len, truncstr);
6277c478bd9Sstevel@tonic-gate 			break;
6287c478bd9Sstevel@tonic-gate 		case IPOPT_SECURITY:
62945916cd2Sjpk 			(void) snprintf(line, remain, "  - RIPSO (%d bytes%s)",
63045916cd2Sjpk 			    len, truncstr);
63145916cd2Sjpk 			print_ripso(opt);
63245916cd2Sjpk 			break;
63345916cd2Sjpk 		case IPOPT_COMSEC:
63445916cd2Sjpk 			(void) snprintf(line, remain, "  - CIPSO (%d bytes%s)",
63545916cd2Sjpk 			    len, truncstr);
63645916cd2Sjpk 			print_cipso(opt);
6377c478bd9Sstevel@tonic-gate 			break;
6387c478bd9Sstevel@tonic-gate 		case IPOPT_LSRR:
63945916cd2Sjpk 			(void) snprintf(line, remain,
64045916cd2Sjpk 			    "  - Loose source route (%d bytes%s)", len,
64145916cd2Sjpk 			    truncstr);
6427c478bd9Sstevel@tonic-gate 			print_route(opt);
6437c478bd9Sstevel@tonic-gate 			break;
6447c478bd9Sstevel@tonic-gate 		case IPOPT_SATID:
64545916cd2Sjpk 			(void) snprintf(line, remain,
64645916cd2Sjpk 			    "  - SATNET Stream id (%d bytes%s)",
64745916cd2Sjpk 			    len, truncstr);
6487c478bd9Sstevel@tonic-gate 			break;
6497c478bd9Sstevel@tonic-gate 		case IPOPT_SSRR:
65045916cd2Sjpk 			(void) snprintf(line, remain,
65145916cd2Sjpk 			    "  - Strict source route, (%d bytes%s)", len,
65245916cd2Sjpk 			    truncstr);
6537c478bd9Sstevel@tonic-gate 			print_route(opt);
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 		default:
65645916cd2Sjpk 			(void) snprintf(line, remain,
65745916cd2Sjpk 			    "  - Option %d (unknown - %d bytes%s) %s",
65845916cd2Sjpk 			    opt[0], len, truncstr,
65945916cd2Sjpk 			    tohex((char *)&opt[2], len - 2));
6607c478bd9Sstevel@tonic-gate 			break;
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		if (len <= 0) {
66345916cd2Sjpk 			(void) snprintf(line, remain,
66445916cd2Sjpk 			    "  - Incomplete option len %d", len);
6657c478bd9Sstevel@tonic-gate 			break;
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate 		opt += len;
6687c478bd9Sstevel@tonic-gate 		optlen -= len;
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate static void
print_route(const uchar_t * opt)67345916cd2Sjpk print_route(const uchar_t *opt)
6747c478bd9Sstevel@tonic-gate {
67545916cd2Sjpk 	int len, pointer, remain;
6767c478bd9Sstevel@tonic-gate 	struct in_addr addr;
6777c478bd9Sstevel@tonic-gate 	char *line;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	len = opt[1];
6807c478bd9Sstevel@tonic-gate 	pointer = opt[2];
6817c478bd9Sstevel@tonic-gate 
68245916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
6837c478bd9Sstevel@tonic-gate 	    "    Pointer = %d", pointer);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	pointer -= IPOPT_MINOFF;
6867c478bd9Sstevel@tonic-gate 	opt += (IPOPT_OFFSET + 1);
6877c478bd9Sstevel@tonic-gate 	len -= (IPOPT_OFFSET + 1);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	while (len > 0) {
69045916cd2Sjpk 		line = get_line(0, 0);
69145916cd2Sjpk 		remain = get_line_remain();
6927c478bd9Sstevel@tonic-gate 		memcpy((char *)&addr, opt, sizeof (addr));
6937c478bd9Sstevel@tonic-gate 		if (addr.s_addr == INADDR_ANY)
69445916cd2Sjpk 			(void) strlcpy(line, "      -", remain);
6957c478bd9Sstevel@tonic-gate 		else
69645916cd2Sjpk 			(void) snprintf(line, remain, "      %s",
6977c478bd9Sstevel@tonic-gate 			    addrtoname(AF_INET, &addr));
6987c478bd9Sstevel@tonic-gate 		if (pointer == 0)
69945916cd2Sjpk 			(void) strlcat(line, "  <-- (current)", remain);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		opt += sizeof (addr);
7027c478bd9Sstevel@tonic-gate 		len -= sizeof (addr);
7037c478bd9Sstevel@tonic-gate 		pointer -= sizeof (addr);
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate char *
getproto(int p)70845916cd2Sjpk getproto(int p)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	switch (p) {
7117c478bd9Sstevel@tonic-gate 	case IPPROTO_HOPOPTS:	return ("IPv6-HopOpts");
7127c478bd9Sstevel@tonic-gate 	case IPPROTO_IPV6:	return ("IPv6");
7137c478bd9Sstevel@tonic-gate 	case IPPROTO_ROUTING:	return ("IPv6-Route");
7147c478bd9Sstevel@tonic-gate 	case IPPROTO_FRAGMENT:	return ("IPv6-Frag");
7157c478bd9Sstevel@tonic-gate 	case IPPROTO_RSVP:	return ("RSVP");
7167c478bd9Sstevel@tonic-gate 	case IPPROTO_ENCAP:	return ("IP-in-IP");
7177c478bd9Sstevel@tonic-gate 	case IPPROTO_AH:	return ("AH");
7187c478bd9Sstevel@tonic-gate 	case IPPROTO_ESP:	return ("ESP");
7197c478bd9Sstevel@tonic-gate 	case IPPROTO_ICMP:	return ("ICMP");
7207c478bd9Sstevel@tonic-gate 	case IPPROTO_ICMPV6:	return ("ICMPv6");
7217c478bd9Sstevel@tonic-gate 	case IPPROTO_DSTOPTS:	return ("IPv6-DstOpts");
7227c478bd9Sstevel@tonic-gate 	case IPPROTO_IGMP:	return ("IGMP");
7237c478bd9Sstevel@tonic-gate 	case IPPROTO_GGP:	return ("GGP");
7247c478bd9Sstevel@tonic-gate 	case IPPROTO_TCP:	return ("TCP");
7257c478bd9Sstevel@tonic-gate 	case IPPROTO_EGP:	return ("EGP");
7267c478bd9Sstevel@tonic-gate 	case IPPROTO_PUP:	return ("PUP");
7277c478bd9Sstevel@tonic-gate 	case IPPROTO_UDP:	return ("UDP");
7287c478bd9Sstevel@tonic-gate 	case IPPROTO_IDP:	return ("IDP");
7297c478bd9Sstevel@tonic-gate 	case IPPROTO_HELLO:	return ("HELLO");
7307c478bd9Sstevel@tonic-gate 	case IPPROTO_ND:	return ("ND");
7317c478bd9Sstevel@tonic-gate 	case IPPROTO_EON:	return ("EON");
7327c478bd9Sstevel@tonic-gate 	case IPPROTO_RAW:	return ("RAW");
7337c478bd9Sstevel@tonic-gate 	case IPPROTO_OSPF:	return ("OSPF");
7347c478bd9Sstevel@tonic-gate 	default:		return ("");
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate static void
prt_routing_hdr(int flags,const struct ip6_rthdr * ipv6ext_rthdr)73945916cd2Sjpk prt_routing_hdr(int flags, const struct ip6_rthdr *ipv6ext_rthdr)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	uint8_t nxt_hdr;
7427c478bd9Sstevel@tonic-gate 	uint8_t type;
7437c478bd9Sstevel@tonic-gate 	uint32_t len;
7447c478bd9Sstevel@tonic-gate 	uint8_t segleft;
7457c478bd9Sstevel@tonic-gate 	uint32_t numaddrs;
7467c478bd9Sstevel@tonic-gate 	int i;
7477c478bd9Sstevel@tonic-gate 	struct ip6_rthdr0 *ipv6ext_rthdr0;
7487c478bd9Sstevel@tonic-gate 	struct in6_addr *addrs;
7497c478bd9Sstevel@tonic-gate 	char addr[INET6_ADDRSTRLEN];
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/* in summary mode, we don't do anything. */
7527c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
7537c478bd9Sstevel@tonic-gate 		return;
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	nxt_hdr = ipv6ext_rthdr->ip6r_nxt;
7577c478bd9Sstevel@tonic-gate 	type = ipv6ext_rthdr->ip6r_type;
7587c478bd9Sstevel@tonic-gate 	len = 8 * (ipv6ext_rthdr->ip6r_len + 1);
7597c478bd9Sstevel@tonic-gate 	segleft = ipv6ext_rthdr->ip6r_segleft;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	show_header("IPv6-Route:  ", "IPv6 Routing Header", 0);
7627c478bd9Sstevel@tonic-gate 	show_space();
7637c478bd9Sstevel@tonic-gate 
76445916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
7657c478bd9Sstevel@tonic-gate 	    "Next header = %d (%s)", nxt_hdr, getproto(nxt_hdr));
76645916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
7677c478bd9Sstevel@tonic-gate 	    "Header length = %d", len);
76845916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
7697c478bd9Sstevel@tonic-gate 	    "Routing type = %d", type);
77045916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
7717c478bd9Sstevel@tonic-gate 	    "Segments left = %d", segleft);
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (type == IPV6_RTHDR_TYPE_0) {
7747c478bd9Sstevel@tonic-gate 		/*
7757c478bd9Sstevel@tonic-gate 		 * XXX This loop will print all addresses in the routing header,
7767c478bd9Sstevel@tonic-gate 		 * XXX not just the segments left.
7777c478bd9Sstevel@tonic-gate 		 * XXX (The header length field is twice the number of
7787c478bd9Sstevel@tonic-gate 		 * XXX addresses)
7797c478bd9Sstevel@tonic-gate 		 * XXX At some future time, we may want to change this
7807c478bd9Sstevel@tonic-gate 		 * XXX to differentiate between the hops yet to do
7817c478bd9Sstevel@tonic-gate 		 * XXX and the hops already taken.
7827c478bd9Sstevel@tonic-gate 		 */
78345916cd2Sjpk 		/* LINTED: alignment */
7847c478bd9Sstevel@tonic-gate 		ipv6ext_rthdr0 = (struct ip6_rthdr0 *)ipv6ext_rthdr;
7857c478bd9Sstevel@tonic-gate 		numaddrs = ipv6ext_rthdr0->ip6r0_len / 2;
7867c478bd9Sstevel@tonic-gate 		addrs = (struct in6_addr *)(ipv6ext_rthdr0 + 1);
7877c478bd9Sstevel@tonic-gate 		for (i = 0; i < numaddrs; i++) {
7887c478bd9Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, &addrs[i], addr,
7897c478bd9Sstevel@tonic-gate 			    INET6_ADDRSTRLEN);
79045916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
7917c478bd9Sstevel@tonic-gate 			    "address[%d]=%s", i, addr);
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	show_space();
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate static void
prt_fragment_hdr(int flags,const struct ip6_frag * ipv6ext_frag)79945916cd2Sjpk prt_fragment_hdr(int flags, const struct ip6_frag *ipv6ext_frag)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	boolean_t morefrag;
8027c478bd9Sstevel@tonic-gate 	uint16_t fragoffset;
8037c478bd9Sstevel@tonic-gate 	uint8_t nxt_hdr;
8047c478bd9Sstevel@tonic-gate 	uint32_t fragident;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* extract the various fields from the fragment header */
8077c478bd9Sstevel@tonic-gate 	nxt_hdr = ipv6ext_frag->ip6f_nxt;
8087c478bd9Sstevel@tonic-gate 	morefrag = (ipv6ext_frag->ip6f_offlg & IP6F_MORE_FRAG) == 0
8097c478bd9Sstevel@tonic-gate 	    ? B_FALSE : B_TRUE;
8107c478bd9Sstevel@tonic-gate 	fragoffset = ntohs(ipv6ext_frag->ip6f_offlg & IP6F_OFF_MASK);
8117c478bd9Sstevel@tonic-gate 	fragident = ntohl(ipv6ext_frag->ip6f_ident);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
81445916cd2Sjpk 		(void) snprintf(get_sum_line(), MAXLINE,
8155086f56fSPaul Wernau 		    "IPv6 fragment ID=%u Offset=%-4d MF=%d",
8167c478bd9Sstevel@tonic-gate 		    fragident,
8177c478bd9Sstevel@tonic-gate 		    fragoffset,
8187c478bd9Sstevel@tonic-gate 		    morefrag);
8197c478bd9Sstevel@tonic-gate 	} else { /* F_DTAIL */
8207c478bd9Sstevel@tonic-gate 		show_header("IPv6-Frag:  ", "IPv6 Fragment Header", 0);
8217c478bd9Sstevel@tonic-gate 		show_space();
8227c478bd9Sstevel@tonic-gate 
82345916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
8247c478bd9Sstevel@tonic-gate 		    "Next Header = %d (%s)", nxt_hdr, getproto(nxt_hdr));
82545916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
8267c478bd9Sstevel@tonic-gate 		    "Fragment Offset = %d", fragoffset);
82745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
8287c478bd9Sstevel@tonic-gate 		    "More Fragments Flag = %s", morefrag ? "true" : "false");
82945916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
8305086f56fSPaul Wernau 		    "Identification = %u", fragident);
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		show_space();
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate }
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate static void
print_ip6opt_ls(const uchar_t * data,unsigned int op_len)83745916cd2Sjpk print_ip6opt_ls(const uchar_t *data, unsigned int op_len)
8387c478bd9Sstevel@tonic-gate {
83945916cd2Sjpk 	uint32_t doi;
84045916cd2Sjpk 	uint8_t sotype, solen;
84145916cd2Sjpk 	uint16_t value, value2;
84245916cd2Sjpk 	char *cp;
843