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