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.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/time.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
347c478bd9Sstevel@tonic-gate #include <sys/socket.h>
357c478bd9Sstevel@tonic-gate #include <net/if.h>
367c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
377c478bd9Sstevel@tonic-gate #include <netinet/in.h>
387c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
397c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
407c478bd9Sstevel@tonic-gate #include <netinet/ip_icmp.h>
417c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
427c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
4345916cd2Sjpk #include <inet/ip.h>
447c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
457c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
467c478bd9Sstevel@tonic-gate #include <netdb.h>
4745916cd2Sjpk #include <tsol/label.h>
4845916cd2Sjpk #include <sys/tsol/tndb.h>
4945916cd2Sjpk #include <sys/tsol/label_macro.h>
5045916cd2Sjpk 
517c478bd9Sstevel@tonic-gate #include "snoop.h"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * IPv6 extension header masks.  These are used by the print_ipv6_extensions()
567c478bd9Sstevel@tonic-gate  * function to return information to the caller about which extension headers
577c478bd9Sstevel@tonic-gate  * were processed.  This can be useful if the caller wants to know if the
587c478bd9Sstevel@tonic-gate  * packet is an IPv6 fragment, for example.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate #define	SNOOP_HOPOPTS	0x01U
617c478bd9Sstevel@tonic-gate #define	SNOOP_ROUTING	0x02U
627c478bd9Sstevel@tonic-gate #define	SNOOP_DSTOPTS	0x04U
637c478bd9Sstevel@tonic-gate #define	SNOOP_FRAGMENT	0x08U
647c478bd9Sstevel@tonic-gate #define	SNOOP_AH	0x10U
657c478bd9Sstevel@tonic-gate #define	SNOOP_ESP	0x20U
667c478bd9Sstevel@tonic-gate #define	SNOOP_IPV6	0x40U
677c478bd9Sstevel@tonic-gate 
6845916cd2Sjpk static void prt_routing_hdr(int, const struct ip6_rthdr *);
6945916cd2Sjpk static void prt_fragment_hdr(int, const struct ip6_frag *);
7045916cd2Sjpk static void prt_hbh_options(int, const struct ip6_hbh *);
7145916cd2Sjpk static void prt_dest_options(int, const struct ip6_dest *);
7245916cd2Sjpk static void print_route(const uchar_t *);
7345916cd2Sjpk static void print_ipoptions(const uchar_t *, int);
7445916cd2Sjpk static void print_ripso(const uchar_t *);
7545916cd2Sjpk static void print_cipso(const uchar_t *);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* Keep track of how many nested IP headers we have. */
787c478bd9Sstevel@tonic-gate unsigned int encap_levels;
797c478bd9Sstevel@tonic-gate unsigned int total_encap_levels = 1;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int
8245916cd2Sjpk interpret_ip(int flags, const struct ip *ip, int fraglen)
837c478bd9Sstevel@tonic-gate {
8445916cd2Sjpk 	uchar_t *data;
857c478bd9Sstevel@tonic-gate 	char buff[24];
867c478bd9Sstevel@tonic-gate 	boolean_t isfrag = B_FALSE;
877c478bd9Sstevel@tonic-gate 	boolean_t morefrag;
887c478bd9Sstevel@tonic-gate 	uint16_t fragoffset;
897c478bd9Sstevel@tonic-gate 	int hdrlen;
907c478bd9Sstevel@tonic-gate 	uint16_t iplen, uitmp;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if (ip->ip_v == IPV6_VERSION) {
937c478bd9Sstevel@tonic-gate 		iplen = interpret_ipv6(flags, (ip6_t *)ip, fraglen);
947c478bd9Sstevel@tonic-gate 		return (iplen);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (encap_levels == 0)
987c478bd9Sstevel@tonic-gate 		total_encap_levels = 0;
997c478bd9Sstevel@tonic-gate 	encap_levels++;
1007c478bd9Sstevel@tonic-gate 	total_encap_levels++;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	hdrlen = ip->ip_hl * 4;
10345916cd2Sjpk 	data = ((uchar_t *)ip) + hdrlen;
1047c478bd9Sstevel@tonic-gate 	iplen = ntohs(ip->ip_len) - hdrlen;
1057c478bd9Sstevel@tonic-gate 	fraglen -= hdrlen;
1067c478bd9Sstevel@tonic-gate 	if (fraglen > iplen)
1077c478bd9Sstevel@tonic-gate 		fraglen = iplen;
1087c478bd9Sstevel@tonic-gate 	if (fraglen < 0) {
1097c478bd9Sstevel@tonic-gate 		(void) snprintf(get_sum_line(), MAXLINE,
1107c478bd9Sstevel@tonic-gate 		    "IP truncated: header missing %d bytes", -fraglen);
1117c478bd9Sstevel@tonic-gate 		encap_levels--;
1127c478bd9Sstevel@tonic-gate 		return (fraglen + iplen);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	/*
1157c478bd9Sstevel@tonic-gate 	 * We flag this as a fragment if the more fragments bit is set, or
1167c478bd9Sstevel@tonic-gate 	 * if the fragment offset is non-zero.
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 	morefrag = (ntohs(ip->ip_off) & IP_MF) == 0 ? B_FALSE : B_TRUE;
1197c478bd9Sstevel@tonic-gate 	fragoffset = (ntohs(ip->ip_off) & 0x1FFF) * 8;
1207c478bd9Sstevel@tonic-gate 	if (morefrag || fragoffset != 0)
1217c478bd9Sstevel@tonic-gate 		isfrag = B_TRUE;
1227c478bd9Sstevel@tonic-gate 
1232b24ab6bSSebastien Roy 	src_name = addrtoname(AF_INET, &ip->ip_src);
1242b24ab6bSSebastien Roy 	dst_name = addrtoname(AF_INET, &ip->ip_dst);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
1277c478bd9Sstevel@tonic-gate 		if (isfrag) {
1287c478bd9Sstevel@tonic-gate 			(void) snprintf(get_sum_line(), MAXLINE,
1297c478bd9Sstevel@tonic-gate 			    "%s IP fragment ID=%d Offset=%-4d MF=%d TOS=0x%x "
1307c478bd9Sstevel@tonic-gate 			    "TTL=%d",
1317c478bd9Sstevel@tonic-gate 			    getproto(ip->ip_p),
1327c478bd9Sstevel@tonic-gate 			    ntohs(ip->ip_id),
1337c478bd9Sstevel@tonic-gate 			    fragoffset,
1347c478bd9Sstevel@tonic-gate 			    morefrag,
1357c478bd9Sstevel@tonic-gate 			    ip->ip_tos,
1367c478bd9Sstevel@tonic-gate 			    ip->ip_ttl);
1377c478bd9Sstevel@tonic-gate 		} else {
1387c478bd9Sstevel@tonic-gate 			(void) strlcpy(buff, inet_ntoa(ip->ip_dst),
1397c478bd9Sstevel@tonic-gate 			    sizeof (buff));
1407c478bd9Sstevel@tonic-gate 			uitmp = ntohs(ip->ip_len);
1417c478bd9Sstevel@tonic-gate 			(void) snprintf(get_sum_line(), MAXLINE,
1427c478bd9Sstevel@tonic-gate 			    "IP  D=%s S=%s LEN=%u%s, ID=%d, TOS=0x%x, TTL=%d",
1437c478bd9Sstevel@tonic-gate 			    buff,
1447c478bd9Sstevel@tonic-gate 			    inet_ntoa(ip->ip_src),
1457c478bd9Sstevel@tonic-gate 			    uitmp,
1467c478bd9Sstevel@tonic-gate 			    iplen > fraglen ? "?" : "",
1477c478bd9Sstevel@tonic-gate 			    ntohs(ip->ip_id),
1487c478bd9Sstevel@tonic-gate 			    ip->ip_tos,
1497c478bd9Sstevel@tonic-gate 			    ip->ip_ttl);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL) {
1547c478bd9Sstevel@tonic-gate 		show_header("IP:   ", "IP Header", iplen);
1557c478bd9Sstevel@tonic-gate 		show_space();
15645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
15745916cd2Sjpk 		    "Version = %d", ip->ip_v);
15845916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
15945916cd2Sjpk 		    "Header length = %d bytes", hdrlen);
16045916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
16145916cd2Sjpk 		    "Type of service = 0x%02x", ip->ip_tos);
16245916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
16345916cd2Sjpk 		    "      xxx. .... = %d (precedence)",
1647c478bd9Sstevel@tonic-gate 		    ip->ip_tos >> 5);
16545916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
16645916cd2Sjpk 		    "      %s", getflag(ip->ip_tos, IPTOS_LOWDELAY,
1677c478bd9Sstevel@tonic-gate 		    "low delay", "normal delay"));
16845916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1697c478bd9Sstevel@tonic-gate 		    getflag(ip->ip_tos, IPTOS_THROUGHPUT,
1707c478bd9Sstevel@tonic-gate 		    "high throughput", "normal throughput"));
17145916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1727c478bd9Sstevel@tonic-gate 		    getflag(ip->ip_tos, IPTOS_RELIABILITY,
1737c478bd9Sstevel@tonic-gate 		    "high reliability", "normal reliability"));
17445916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1757c478bd9Sstevel@tonic-gate 		    getflag(ip->ip_tos, IPTOS_ECT,
1767c478bd9Sstevel@tonic-gate 		    "ECN capable transport", "not ECN capable transport"));
17745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1787c478bd9Sstevel@tonic-gate 		    getflag(ip->ip_tos, IPTOS_CE,
1797c478bd9Sstevel@tonic-gate 		    "ECN congestion experienced",
1807c478bd9Sstevel@tonic-gate 		    "no ECN congestion experienced"));
1817c478bd9Sstevel@tonic-gate 		/* warning: ip_len is signed in netinet/ip.h */
1827c478bd9Sstevel@tonic-gate 		uitmp = ntohs(ip->ip_len);
18345916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
18445916cd2Sjpk 		    "Total length = %u bytes%s", uitmp,
1857c478bd9Sstevel@tonic-gate 		    iplen > fraglen ? " -- truncated" : "");
18645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
18745916cd2Sjpk 		    "Identification = %d", ntohs(ip->ip_id));
1887c478bd9Sstevel@tonic-gate 		/* warning: ip_off is signed in netinet/ip.h */
1897c478bd9Sstevel@tonic-gate 		uitmp = ntohs(ip->ip_off);
19045916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
19145916cd2Sjpk 		    "Flags = 0x%x", uitmp >> 12);
19245916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1937c478bd9Sstevel@tonic-gate 		    getflag(uitmp >> 8, IP_DF >> 8,
1947c478bd9Sstevel@tonic-gate 		    "do not fragment", "may fragment"));
19545916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "      %s",
1967c478bd9Sstevel@tonic-gate 		    getflag(uitmp >> 8, IP_MF >> 8,
1977c478bd9Sstevel@tonic-gate 		    "more fragments", "last fragment"));
19845916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
19945916cd2Sjpk 		    "Fragment offset = %u bytes",
2007c478bd9Sstevel@tonic-gate 		    fragoffset);
20145916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
20245916cd2Sjpk 		    "Time to live = %d seconds/hops",
2037c478bd9Sstevel@tonic-gate 		    ip->ip_ttl);
20445916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
20545916cd2Sjpk 		    "Protocol = %d (%s)", ip->ip_p,
2067c478bd9Sstevel@tonic-gate 		    getproto(ip->ip_p));
2077c478bd9Sstevel@tonic-gate 		/*
2087c478bd9Sstevel@tonic-gate 		 * XXX need to compute checksum and print whether it's correct
2097c478bd9Sstevel@tonic-gate 		 */
21045916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
21145916cd2Sjpk 		    "Header checksum = %04x",
2127c478bd9Sstevel@tonic-gate 		    ntohs(ip->ip_sum));
21345916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
21445916cd2Sjpk 		    "Source address = %s, %s",
2157c478bd9Sstevel@tonic-gate 		    inet_ntoa(ip->ip_src), addrtoname(AF_INET, &ip->ip_src));
21645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
21745916cd2Sjpk 		    "Destination address = %s, %s",
2187c478bd9Sstevel@tonic-gate 		    inet_ntoa(ip->ip_dst), addrtoname(AF_INET, &ip->ip_dst));
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		/* Print IP options - if any */
2217c478bd9Sstevel@tonic-gate 
22245916cd2Sjpk 		print_ipoptions((const uchar_t *)(ip + 1),
22345916cd2Sjpk 		    hdrlen - sizeof (struct ip));
2247c478bd9Sstevel@tonic-gate 		show_space();
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/*
2287c478bd9Sstevel@tonic-gate 	 * If we are in detail mode, and this is not the first fragment of
2297c478bd9Sstevel@tonic-gate 	 * a fragmented packet, print out a little line stating this.
2307c478bd9Sstevel@tonic-gate 	 * Otherwise, go to the next protocol layer only if this is not a
2317c478bd9Sstevel@tonic-gate 	 * fragment, or we are in detail mode and this is the first fragment
2327c478bd9Sstevel@tonic-gate 	 * of a fragmented packet.
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	if (flags & F_DTAIL && fragoffset != 0) {
23545916cd2Sjpk 		(void) snprintf(get_detail_line(0, 0), MAXLINE,
2367c478bd9Sstevel@tonic-gate 		    "%s:  [%d byte(s) of data, continuation of IP ident=%d]",
2377c478bd9Sstevel@tonic-gate 		    getproto(ip->ip_p),
2387c478bd9Sstevel@tonic-gate 		    iplen,
2397c478bd9Sstevel@tonic-gate 		    ntohs(ip->ip_id));
2407c478bd9Sstevel@tonic-gate 	} else if (!isfrag || (flags & F_DTAIL) && isfrag && fragoffset == 0) {
2417c478bd9Sstevel@tonic-gate 		/* go to the next protocol layer */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		if (fraglen > 0) {
2447c478bd9Sstevel@tonic-gate 			switch (ip->ip_p) {
2457c478bd9Sstevel@tonic-gate 			case IPPROTO_IP:
2467c478bd9Sstevel@tonic-gate 				break;
2477c478bd9Sstevel@tonic-gate 			case IPPROTO_ENCAP:
24845916cd2Sjpk 				(void) interpret_ip(flags,
24945916cd2Sjpk 				    /* LINTED: alignment */
25045916cd2Sjpk 				    (const struct ip *)data, fraglen);
2517c478bd9Sstevel@tonic-gate 				break;
2527c478bd9Sstevel@tonic-gate 			case IPPROTO_ICMP:
25345916cd2Sjpk 				(void) interpret_icmp(flags,
25445916cd2Sjpk 				    /* LINTED: alignment */
25545916cd2Sjpk 				    (struct icmp *)data, iplen, fraglen);
2567c478bd9Sstevel@tonic-gate 				break;
2577c478bd9Sstevel@tonic-gate 			case IPPROTO_IGMP:
2587c478bd9Sstevel@tonic-gate 				interpret_igmp(flags, data, iplen, fraglen);
2597c478bd9Sstevel@tonic-gate 				break;
2607c478bd9Sstevel@tonic-gate 			case IPPROTO_GGP:
2617c478bd9Sstevel@tonic-gate 				break;
2627c478bd9Sstevel@tonic-gate 			case IPPROTO_TCP:
26345916cd2Sjpk 				(void) interpret_tcp(flags,
26445916cd2Sjpk 				    (struct tcphdr *)data, iplen, fraglen);
2657c478bd9Sstevel@tonic-gate 				break;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 			case IPPROTO_ESP:
26845916cd2Sjpk 				(void) interpret_esp(flags, data, iplen,
26945916cd2Sjpk 				    fraglen);
2707c478bd9Sstevel@tonic-gate 				break;
2717c478bd9Sstevel@tonic-gate 			case IPPROTO_AH:
27245916cd2Sjpk 				(void) interpret_ah(flags, data, iplen,
27345916cd2Sjpk 				    fraglen);
2747c478bd9Sstevel@tonic-gate 				break;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 			case IPPROTO_OSPF:
2777c478bd9Sstevel@tonic-gate 				interpret_ospf(flags, data, iplen, fraglen);
2787c478bd9Sstevel@tonic-gate 				break;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 			case IPPROTO_EGP:
2817c478bd9Sstevel@tonic-gate 			case IPPROTO_PUP:
2827c478bd9Sstevel@tonic-gate 				break;
2837c478bd9Sstevel@tonic-gate 			case IPPROTO_UDP:
28445916cd2Sjpk 				(void) interpret_udp(flags,
28545916cd2Sjpk 				    (struct udphdr *)data, iplen, fraglen);
2867c478bd9Sstevel@tonic-gate 				break;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 			case IPPROTO_IDP:
2897c478bd9Sstevel@tonic-gate 			case IPPROTO_HELLO:
2907c478bd9Sstevel@tonic-gate 			case IPPROTO_ND:
2917c478bd9Sstevel@tonic-gate 			case IPPROTO_RAW:
2927c478bd9Sstevel@tonic-gate 				break;
2937c478bd9Sstevel@tonic-gate 			case IPPROTO_IPV6:	/* IPV6 encap */
29445916cd2Sjpk 				/* LINTED: alignment */
2957c478bd9Sstevel@tonic-gate 				(void) interpret_ipv6(flags, (ip6_t *)data,
2967c478bd9Sstevel@tonic-gate 				    iplen);
2977c478bd9Sstevel@tonic-gate 				break;
2987c478bd9Sstevel@tonic-gate 			case IPPROTO_SCTP:
29945916cd2Sjpk 				(void) interpret_sctp(flags,
30045916cd2Sjpk 				    (struct sctp_hdr *)data, iplen, fraglen);
3017c478bd9Sstevel@tonic-gate 				break;
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	encap_levels--;
3077c478bd9Sstevel@tonic-gate 	return (iplen);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate int
31145916cd2Sjpk interpret_ipv6(int flags, const ip6_t *ip6h, int fraglen)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	uint8_t *data;
3147c478bd9Sstevel@tonic-gate 	int hdrlen, iplen;
3157c478bd9Sstevel@tonic-gate 	int version, flow, class;
3167c478bd9Sstevel@tonic-gate 	uchar_t proto;
3177c478bd9Sstevel@tonic-gate 	boolean_t isfrag = B_FALSE;
3187c478bd9Sstevel@tonic-gate 	uint8_t extmask;
3197c478bd9Sstevel@tonic-gate 	/*
3207c478bd9Sstevel@tonic-gate 	 * The print_srcname and print_dstname strings are the hostname
3217c478bd9Sstevel@tonic-gate 	 * parts of the verbose IPv6 header output, including the comma
3227c478bd9Sstevel@tonic-gate 	 * and the space after the litteral address strings.
3237c478bd9Sstevel@tonic-gate 	 */
3247c478bd9Sstevel@tonic-gate 	char print_srcname[MAXHOSTNAMELEN + 2];
3257c478bd9Sstevel@tonic-gate 	char print_dstname[MAXHOSTNAMELEN + 2];
3267c478bd9Sstevel@tonic-gate 	char src_addrstr[INET6_ADDRSTRLEN];
3277c478bd9Sstevel@tonic-gate 	char dst_addrstr[INET6_ADDRSTRLEN];
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	iplen = ntohs(ip6h->ip6_plen);
3307c478bd9Sstevel@tonic-gate 	hdrlen = IPV6_HDR_LEN;
3317c478bd9Sstevel@tonic-gate 	fraglen -= hdrlen;
3327c478bd9Sstevel@tonic-gate 	if (fraglen < 0)
3337c478bd9Sstevel@tonic-gate 		return (fraglen + hdrlen);
3347c478bd9Sstevel@tonic-gate 	data = ((uint8_t *)ip6h) + hdrlen;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	proto = ip6h->ip6_nxt;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	src_name = addrtoname(AF_INET6, &ip6h->ip6_src);
3397c478bd9Sstevel@tonic-gate 	dst_name = addrtoname(AF_INET6, &ip6h->ip6_dst);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * Use endian-aware masks to extract traffic class and
3437c478bd9Sstevel@tonic-gate 	 * flowinfo.  Also, flowinfo is now 20 bits and class 8
3447c478bd9Sstevel@tonic-gate 	 * rather than 24 and 4.
3457c478bd9Sstevel@tonic-gate 	 */
3467c478bd9Sstevel@tonic-gate 	class = ntohl((ip6h->ip6_vcf & IPV6_FLOWINFO_TCLASS) >> 20);
3477c478bd9Sstevel@tonic-gate 	flow = ntohl(ip6h->ip6_vcf & IPV6_FLOWINFO_FLOWLABEL);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * NOTE: the F_SUM and F_DTAIL flags are mutually exclusive,
3517c478bd9Sstevel@tonic-gate 	 * so the code within the first part of the following if statement
3527c478bd9Sstevel@tonic-gate 	 * will not affect the detailed printing of the packet.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
35545916cd2Sjpk 		(void) snprintf(get_sum_line(), MAXLINE,
35645916cd2Sjpk 		    "IPv6  S=%s D=%s LEN=%d HOPS=%d CLASS=0x%x FLOW=0x%x",
3577c478bd9Sstevel@tonic-gate 		    src_name, dst_name, iplen, ip6h->ip6_hops, class, flow);
3587c478bd9Sstevel@tonic-gate 	} else if (flags & F_DTAIL) {
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, &ip6h->ip6_src, src_addrstr,
3617c478bd9Sstevel@tonic-gate 		    INET6_ADDRSTRLEN);
3627c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, &ip6h->ip6_dst, dst_addrstr,
3637c478bd9Sstevel@tonic-gate 		    INET6_ADDRSTRLEN);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		version = ntohl(ip6h->ip6_vcf) >> 28;
3667c478bd9Sstevel@tonic-gate 
3672b24ab6bSSebastien Roy 		if (strcmp(src_name, src_addrstr) == 0) {
3687c478bd9Sstevel@tonic-gate 			print_srcname[0] = '\0';
3692b24ab6bSSebastien Roy 		} else {
3707c478bd9Sstevel@tonic-gate 			snprintf(print_srcname, sizeof (print_srcname),
3712b24ab6bSSebastien Roy 			    ", %s", src_name);
3722b24ab6bSSebastien Roy 		}
3737c478bd9Sstevel@tonic-gate 
3742b24ab6bSSebastien Roy 		if (strcmp(dst_name, dst_addrstr) == 0) {
3757c478bd9Sstevel@tonic-gate 			print_dstname[0] = '\0';
3762b24ab6bSSebastien Roy 		} else {
3777c478bd9Sstevel@tonic-gate 			snprintf(print_dstname, sizeof (print_dstname),
3782b24ab6bSSebastien Roy 			    ", %s", dst_name);
3792b24ab6bSSebastien Roy 		}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		show_header("IPv6:   ", "IPv6 Header", iplen);
3827c478bd9Sstevel@tonic-gate 		show_space();
3837c478bd9Sstevel@tonic-gate 
38445916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
3857c478bd9Sstevel@tonic-gate 		    "Version = %d", version);
38645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
3877c478bd9Sstevel@tonic-gate 		    "Traffic Class = %d", class);
38845916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
3897c478bd9Sstevel@tonic-gate 		    "Flow label = 0x%x", flow);
39045916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
39145916cd2Sjpk 		    "Payload length = %d", iplen);
39245916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
39345916cd2Sjpk 		    "Next Header = %d (%s)", proto,
3947c478bd9Sstevel@tonic-gate 		    getproto(proto));
39545916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
39645916cd2Sjpk 		    "Hop Limit = %d", ip6h->ip6_hops);
39745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
3987c478bd9Sstevel@tonic-gate 		    "Source address = %s%s", src_addrstr, print_srcname);
39945916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
4007c478bd9Sstevel@tonic-gate 		    "Destination address = %s%s", dst_addrstr, print_dstname);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		show_space();
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Print IPv6 Extension Headers, or skip them in the summary case.
4077c478bd9Sstevel@tonic-gate 	 * Set isfrag to true if one of the extension headers encounterred
4087c478bd9Sstevel@tonic-gate 	 * was a fragment header.
4097c478bd9Sstevel@tonic-gate 	 */
4107c478bd9Sstevel@tonic-gate 	if (proto == IPPROTO_HOPOPTS || proto == IPPROTO_DSTOPTS ||
4117c478bd9Sstevel@tonic-gate 	    proto == IPPROTO_ROUTING || proto == IPPROTO_FRAGMENT) {
4127c478bd9Sstevel@tonic-gate 		extmask = print_ipv6_extensions(flags, &data, &proto, &iplen,
4137c478bd9Sstevel@tonic-gate 		    &fraglen);
4147c478bd9Sstevel@tonic-gate 		if ((extmask & SNOOP_FRAGMENT) != 0) {
4157c478bd9Sstevel@tonic-gate 			isfrag = B_TRUE;
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * We only want to print upper layer information if this is not
4217c478bd9Sstevel@tonic-gate 	 * a fragment, or if we're printing in detail.  Note that the
4227c478bd9Sstevel@tonic-gate 	 * proto variable will be set to IPPROTO_NONE if this is a fragment
4237c478bd9Sstevel@tonic-gate 	 * with a non-zero fragment offset.
4247c478bd9Sstevel@tonic-gate 	 */
4257c478bd9Sstevel@tonic-gate 	if (!isfrag || flags & F_DTAIL) {
4267c478bd9Sstevel@tonic-gate 		/* go to the next protocol layer */
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		switch (proto) {
4297c478bd9Sstevel@tonic-gate 		case IPPROTO_IP:
4307c478bd9Sstevel@tonic-gate 			break;
4317c478bd9Sstevel@tonic-gate 		case IPPROTO_ENCAP:
43245916cd2Sjpk 			/* LINTED: alignment */
43345916cd2Sjpk 			(void) interpret_ip(flags, (const struct ip *)data,
43445916cd2Sjpk 			    fraglen);
4357c478bd9Sstevel@tonic-gate 			break;
4367c478bd9Sstevel@tonic-gate 		case IPPROTO_ICMPV6:
43745916cd2Sjpk 			/* LINTED: alignment */
43845916cd2Sjpk 			(void) interpret_icmpv6(flags, (icmp6_t *)data, iplen,
4397c478bd9Sstevel@tonic-gate 			    fraglen);
4407c478bd9Sstevel@tonic-gate 			break;
4417c478bd9Sstevel@tonic-gate 		case IPPROTO_IGMP:
4427c478bd9Sstevel@tonic-gate 			interpret_igmp(flags, data, iplen, fraglen);
4437c478bd9Sstevel@tonic-gate 			break;
4447c478bd9Sstevel@tonic-gate 		case IPPROTO_GGP:
4457c478bd9Sstevel@tonic-gate 			break;
4467c478bd9Sstevel@tonic-gate 		case IPPROTO_TCP:
44745916cd2Sjpk 			(void) interpret_tcp(flags, (struct tcphdr *)data,
44845916cd2Sjpk 			    iplen, fraglen);
4497c478bd9Sstevel@tonic-gate 			break;
4507c478bd9Sstevel@tonic-gate 		case IPPROTO_ESP:
45145916cd2Sjpk 			(void) interpret_esp(flags, data, iplen, fraglen);
4527c478bd9Sstevel@tonic-gate 			break;
4537c478bd9Sstevel@tonic-gate 		case IPPROTO_AH:
45445916cd2Sjpk 			(void) interpret_ah(flags, data, iplen, fraglen);
4557c478bd9Sstevel@tonic-gate 			break;
4567c478bd9Sstevel@tonic-gate 		case IPPROTO_EGP:
4577c478bd9Sstevel@tonic-gate 		case IPPROTO_PUP:
4587c478bd9Sstevel@tonic-gate 			break;
4597c478bd9Sstevel@tonic-gate 		case IPPROTO_UDP:
46045916cd2Sjpk 			(void) interpret_udp(flags, (struct udphdr *)data,
46145916cd2Sjpk 			    iplen, fraglen);
4627c478bd9Sstevel@tonic-gate 			break;
4637c478bd9Sstevel@tonic-gate 		case IPPROTO_IDP:
4647c478bd9Sstevel@tonic-gate 		case IPPROTO_HELLO:
4657c478bd9Sstevel@tonic-gate 		case IPPROTO_ND:
4667c478bd9Sstevel@tonic-gate 		case IPPROTO_RAW:
4677c478bd9Sstevel@tonic-gate 			break;
4687c478bd9Sstevel@tonic-gate 		case IPPROTO_IPV6:
46945916cd2Sjpk 			/* LINTED: alignment */
47045916cd2Sjpk 			(void) interpret_ipv6(flags, (const ip6_t *)data,
47145916cd2Sjpk 			    iplen);
4727c478bd9Sstevel@tonic-gate 			break;
4737c478bd9Sstevel@tonic-gate 		case IPPROTO_SCTP:
47445916cd2Sjpk 			(void) interpret_sctp(flags, (struct sctp_hdr *)data,
47545916cd2Sjpk 			    iplen, fraglen);
4767c478bd9Sstevel@tonic-gate 			break;
4777c478bd9Sstevel@tonic-gate 		case IPPROTO_OSPF:
4787c478bd9Sstevel@tonic-gate 			interpret_ospf6(flags, data, iplen, fraglen);
4797c478bd9Sstevel@tonic-gate 			break;
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	return (iplen);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * ip_ext: data including the extension header.
4887c478bd9Sstevel@tonic-gate  * iplen: length of the data remaining in the packet.
4897c478bd9Sstevel@tonic-gate  * Returns a mask of IPv6 extension headers it processed.
4907c478bd9Sstevel@tonic-gate  */
4917c478bd9Sstevel@tonic-gate uint8_t
4927c478bd9Sstevel@tonic-gate print_ipv6_extensions(int flags, uint8_t **hdr, uint8_t *next, int *iplen,
4937c478bd9Sstevel@tonic-gate     int *fraglen)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	uint8_t *data_ptr;
4967c478bd9Sstevel@tonic-gate 	uchar_t proto = *next;
4977c478bd9Sstevel@tonic-gate 	boolean_t is_extension_header;
4987c478bd9Sstevel@tonic-gate 	struct ip6_hbh *ipv6ext_hbh;
4997c478bd9Sstevel@tonic-gate 	struct ip6_dest *ipv6ext_dest;
5007c478bd9Sstevel@tonic-gate 	struct ip6_rthdr *ipv6ext_rthdr;
5017c478bd9Sstevel@tonic-gate 	struct ip6_frag *ipv6ext_frag;
5027c478bd9Sstevel@tonic-gate 	uint32_t exthdrlen;
5037c478bd9Sstevel@tonic-gate 	uint8_t extmask = 0;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if ((hdr == NULL) || (*hdr == NULL) || (next == NULL) || (iplen == 0))
5067c478bd9Sstevel@tonic-gate 		return (0);
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	data_ptr = *hdr;
5097c478bd9Sstevel@tonic-gate 	is_extension_header = B_TRUE;
5107c478bd9Sstevel@tonic-gate 	while (is_extension_header) {
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 		/*
5137c478bd9Sstevel@tonic-gate 		 * There must be at least enough data left to read the
5147c478bd9Sstevel@tonic-gate 		 * next header and header length fields from the next
5157c478bd9Sstevel@tonic-gate 		 * header.
5167c478bd9Sstevel@tonic-gate 		 */
5177c478bd9Sstevel@tonic-gate 		if (*fraglen < 2) {
5187c478bd9Sstevel@tonic-gate 			return (extmask);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		switch (proto) {
5227c478bd9Sstevel@tonic-gate 		case IPPROTO_HOPOPTS:
5237c478bd9Sstevel@tonic-gate 			ipv6ext_hbh = (struct ip6_hbh *)data_ptr;
5247c478bd9Sstevel@tonic-gate 			exthdrlen = 8 + ipv6ext_hbh->ip6h_len * 8;
5257c478bd9Sstevel@tonic-gate 			if (*fraglen <= exthdrlen) {
5267c478bd9Sstevel@tonic-gate 				return (extmask);
5277c478bd9Sstevel@tonic-gate 			}
5287c478bd9Sstevel@tonic-gate 			prt_hbh_options(flags, ipv6ext_hbh);
5297c478bd9Sstevel@tonic-gate 			extmask |= SNOOP_HOPOPTS;
5307c478bd9Sstevel@tonic-gate 			proto = ipv6ext_hbh->ip6h_nxt;
5317c478bd9Sstevel@tonic-gate 			break;
5327c478bd9Sstevel@tonic-gate 		case IPPROTO_DSTOPTS:
5337c478bd9Sstevel@tonic-gate 			ipv6ext_dest = (struct ip6_dest *)data_ptr;
5347c478bd9Sstevel@tonic-gate 			exthdrlen = 8 + ipv6ext_dest->ip6d_len * 8;
5357c478bd9Sstevel@tonic-gate 			if (*fraglen <= exthdrlen) {
5367c478bd9Sstevel@tonic-gate 				return (extmask);
5377c478bd9Sstevel@tonic-gate 			}
5387c478bd9Sstevel@tonic-gate 			prt_dest_options(flags, ipv6ext_dest);
5397c478bd9Sstevel@tonic-gate 			extmask |= SNOOP_DSTOPTS;
5407c478bd9Sstevel@tonic-gate 			proto = ipv6ext_dest->ip6d_nxt;
5417c478bd9Sstevel@tonic-gate 			break;
5427c478bd9Sstevel@tonic-gate 		case IPPROTO_ROUTING:
5437c478bd9Sstevel@tonic-gate 			ipv6ext_rthdr = (struct ip6_rthdr *)data_ptr;
5447c478bd9Sstevel@tonic-gate 			exthdrlen = 8 + ipv6ext_rthdr->ip6r_len * 8;
5457c478bd9Sstevel@tonic-gate 			if (*fraglen <= exthdrlen) {
5467c478bd9Sstevel@tonic-gate 				return (extmask);
5477c478bd9Sstevel@tonic-gate 			}
5487c478bd9Sstevel@tonic-gate 			prt_routing_hdr(flags, ipv6ext_rthdr);
5497c478bd9Sstevel@tonic-gate 			extmask |= SNOOP_ROUTING;
5507c478bd9Sstevel@tonic-gate 			proto = ipv6ext_rthdr->ip6r_nxt;
5517c478bd9Sstevel@tonic-gate 			break;
5527c478bd9Sstevel@tonic-gate 		case IPPROTO_FRAGMENT:
55345916cd2Sjpk 			/* LINTED: alignment */
5547c478bd9Sstevel@tonic-gate 			ipv6ext_frag = (struct ip6_frag *)data_ptr;
5557c478bd9Sstevel@tonic-gate 			exthdrlen = sizeof (struct ip6_frag);
5567c478bd9Sstevel@tonic-gate 			if (*fraglen <= exthdrlen) {
5577c478bd9Sstevel@tonic-gate 				return (extmask);
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 			prt_fragment_hdr(flags, ipv6ext_frag);
5607c478bd9Sstevel@tonic-gate 			extmask |= SNOOP_FRAGMENT;
5617c478bd9Sstevel@tonic-gate 			/*
5627c478bd9Sstevel@tonic-gate 			 * If this is not the first fragment, forget about
5637c478bd9Sstevel@tonic-gate 			 * the rest of the packet, snoop decoding is
5647c478bd9Sstevel@tonic-gate 			 * stateless.
5657c478bd9Sstevel@tonic-gate 			 */
5667c478bd9Sstevel@tonic-gate 			if ((ipv6ext_frag->ip6f_offlg & IP6F_OFF_MASK) != 0)
5677c478bd9Sstevel@tonic-gate 				proto = IPPROTO_NONE;
5687c478bd9Sstevel@tonic-gate 			else
5697c478bd9Sstevel@tonic-gate 				proto = ipv6ext_frag->ip6f_nxt;
5707c478bd9Sstevel@tonic-gate 			break;
5717c478bd9Sstevel@tonic-gate 		default:
5727c478bd9Sstevel@tonic-gate 			is_extension_header = B_FALSE;
5737c478bd9Sstevel@tonic-gate 			break;
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		if (is_extension_header) {
5777c478bd9Sstevel@tonic-gate 			*iplen -= exthdrlen;
5787c478bd9Sstevel@tonic-gate 			*fraglen -= exthdrlen;
5797c478bd9Sstevel@tonic-gate 			data_ptr += exthdrlen;
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	*next = proto;
5847c478bd9Sstevel@tonic-gate 	*hdr = data_ptr;
5857c478bd9Sstevel@tonic-gate 	return (extmask);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate static void
58945916cd2Sjpk print_ipoptions(const uchar_t *opt, int optlen)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	int len;
59245916cd2Sjpk 	int remain;
5937c478bd9Sstevel@tonic-gate 	char *line;
59445916cd2Sjpk 	const char *truncstr;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (optlen <= 0) {
59745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
5987c478bd9Sstevel@tonic-gate 		    "No options");
5997c478bd9Sstevel@tonic-gate 		return;
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
60245916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
6037c478bd9Sstevel@tonic-gate 	    "Options: (%d bytes)", optlen);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	while (optlen > 0) {
60645916cd2Sjpk 		line = get_line(0, 0);
60745916cd2Sjpk 		remain = get_line_remain();
6087c478bd9Sstevel@tonic-gate 		len = opt[1];
60945916cd2Sjpk 		truncstr = len > optlen ? "?" : "";
6107c478bd9Sstevel@tonic-gate 		switch (opt[0]) {
6117c478bd9Sstevel@tonic-gate 		case IPOPT_EOL:
61245916cd2Sjpk 			(void) strlcpy(line, "  - End of option list", remain);
6137c478bd9Sstevel@tonic-gate 			return;
6147c478bd9Sstevel@tonic-gate 		case IPOPT_NOP:
61545916cd2Sjpk 			(void) strlcpy(line, "  - No op", remain);
6167c478bd9Sstevel@tonic-gate 			len = 1;
6177c478bd9Sstevel@tonic-gate 			break;
6187c478bd9Sstevel@tonic-gate 		case IPOPT_RR:
61945916cd2Sjpk 			(void) snprintf(line, remain,
62045916cd2Sjpk 			    "  - Record route (%d bytes%s)", len, truncstr);
6217c478bd9Sstevel@tonic-gate 			print_route(opt);
6227c478bd9Sstevel@tonic-gate 			break;
6237c478bd9Sstevel@tonic-gate 		case IPOPT_TS:
62445916cd2Sjpk 			(void) snprintf(line, remain,
62545916cd2Sjpk 			    "  - Time stamp (%d bytes%s)", len, truncstr);
6267c478bd9Sstevel@tonic-gate 			break;
6277c478bd9Sstevel@tonic-gate 		case IPOPT_SECURITY:
62845916cd2Sjpk 			(void) snprintf(line, remain, "  - RIPSO (%d bytes%s)",
62945916cd2Sjpk 			    len, truncstr);
63045916cd2Sjpk 			print_ripso(opt);
63145916cd2Sjpk 			break;
63245916cd2Sjpk 		case IPOPT_COMSEC:
63345916cd2Sjpk 			(void) snprintf(line, remain, "  - CIPSO (%d bytes%s)",
63445916cd2Sjpk 			    len, truncstr);
63545916cd2Sjpk 			print_cipso(opt);
6367c478bd9Sstevel@tonic-gate 			break;
6377c478bd9Sstevel@tonic-gate 		case IPOPT_LSRR:
63845916cd2Sjpk 			(void) snprintf(line, remain,
63945916cd2Sjpk 			    "  - Loose source route (%d bytes%s)", len,
64045916cd2Sjpk 			    truncstr);
6417c478bd9Sstevel@tonic-gate 			print_route(opt);
6427c478bd9Sstevel@tonic-gate 			break;
6437c478bd9Sstevel@tonic-gate 		case IPOPT_SATID:
64445916cd2Sjpk 			(void) snprintf(line, remain,
64545916cd2Sjpk 			    "  - SATNET Stream id (%d bytes%s)",
64645916cd2Sjpk 			    len, truncstr);
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 		case IPOPT_SSRR:
64945916cd2Sjpk 			(void) snprintf(line, remain,
65045916cd2Sjpk 			    "  - Strict source route, (%d bytes%s)", len,
65145916cd2Sjpk 			    truncstr);
6527c478bd9Sstevel@tonic-gate 			print_route(opt);
6537c478bd9Sstevel@tonic-gate 			break;
6547c478bd9Sstevel@tonic-gate 		default:
65545916cd2Sjpk 			(void) snprintf(line, remain,
65645916cd2Sjpk 			    "  - Option %d (unknown - %d bytes%s) %s",
65745916cd2Sjpk 			    opt[0], len, truncstr,
65845916cd2Sjpk 			    tohex((char *)&opt[2], len - 2));
6597c478bd9Sstevel@tonic-gate 			break;
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 		if (len <= 0) {
66245916cd2Sjpk 			(void) snprintf(line, remain,
66345916cd2Sjpk 			    "  - Incomplete option len %d", len);
6647c478bd9Sstevel@tonic-gate 			break;
6657c478bd9Sstevel@tonic-gate 		}
6667c478bd9Sstevel@tonic-gate 		opt += len;
6677c478bd9Sstevel@tonic-gate 		optlen -= len;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate static void
67245916cd2Sjpk print_route(const uchar_t *opt)
6737c478bd9Sstevel@tonic-gate {
67445916cd2Sjpk 	int len, pointer, remain;
6757c478bd9Sstevel@tonic-gate 	struct in_addr addr;
6767c478bd9Sstevel@tonic-gate 	char *line;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	len = opt[1];
6797c478bd9Sstevel@tonic-gate 	pointer = opt[2];
6807c478bd9Sstevel@tonic-gate 
68145916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
6827c478bd9Sstevel@tonic-gate 	    "    Pointer = %d", pointer);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	pointer -= IPOPT_MINOFF;
6857c478bd9Sstevel@tonic-gate 	opt += (IPOPT_OFFSET + 1);
6867c478bd9Sstevel@tonic-gate 	len -= (IPOPT_OFFSET + 1);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	while (len > 0) {
68945916cd2Sjpk 		line = get_line(0, 0);
69045916cd2Sjpk 		remain = get_line_remain();
6917c478bd9Sstevel@tonic-gate 		memcpy((char *)&addr, opt, sizeof (addr));
6927c478bd9Sstevel@tonic-gate 		if (addr.s_addr == INADDR_ANY)
69345916cd2Sjpk 			(void) strlcpy(line, "      -", remain);
6947c478bd9Sstevel@tonic-gate 		else
69545916cd2Sjpk 			(void) snprintf(line, remain, "      %s",
6967c478bd9Sstevel@tonic-gate 			    addrtoname(AF_INET, &addr));
6977c478bd9Sstevel@tonic-gate 		if (pointer == 0)
69845916cd2Sjpk 			(void) strlcat(line, "  <-- (current)", remain);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		opt += sizeof (addr);
7017c478bd9Sstevel@tonic-gate 		len -= sizeof (addr);
7027c478bd9Sstevel@tonic-gate 		pointer -= sizeof (addr);
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate char *
70745916cd2Sjpk getproto(int p)
7087c478bd9Sstevel@tonic-gate {
7097c478bd9Sstevel@tonic-gate 	switch (p) {
7107c478bd9Sstevel@tonic-gate 	case IPPROTO_HOPOPTS:	return ("IPv6-HopOpts");
7117c478bd9Sstevel@tonic-gate 	case IPPROTO_IPV6:	return ("IPv6");
7127c478bd9Sstevel@tonic-gate 	case IPPROTO_ROUTING:	return ("IPv6-Route");
7137c478bd9Sstevel@tonic-gate 	case IPPROTO_FRAGMENT:	return ("IPv6-Frag");
7147c478bd9Sstevel@tonic-gate 	case IPPROTO_RSVP:	return ("RSVP");
7157c478bd9Sstevel@tonic-gate 	case IPPROTO_ENCAP:	return ("IP-in-IP");
7167c478bd9Sstevel@tonic-gate 	case IPPROTO_AH:	return ("AH");
7177c478bd9Sstevel@tonic-gate 	case IPPROTO_ESP:	return ("ESP");
7187c478bd9Sstevel@tonic-gate 	case IPPROTO_ICMP:	return ("ICMP");
7197c478bd9Sstevel@tonic-gate 	case IPPROTO_ICMPV6:	return ("ICMPv6");
7207c478bd9Sstevel@tonic-gate 	case IPPROTO_DSTOPTS:	return ("IPv6-DstOpts");
7217c478bd9Sstevel@tonic-gate 	case IPPROTO_IGMP:	return ("IGMP");
7227c478bd9Sstevel@tonic-gate 	case IPPROTO_GGP:	return ("GGP");
7237c478bd9Sstevel@tonic-gate 	case IPPROTO_TCP:	return ("TCP");
7247c478bd9Sstevel@tonic-gate 	case IPPROTO_EGP:	return ("EGP");
7257c478bd9Sstevel@tonic-gate 	case IPPROTO_PUP:	return ("PUP");
7267c478bd9Sstevel@tonic-gate 	case IPPROTO_UDP:	return ("UDP");
7277c478bd9Sstevel@tonic-gate 	case IPPROTO_IDP:	return ("IDP");
7287c478bd9Sstevel@tonic-gate 	case IPPROTO_HELLO:	return ("HELLO");
7297c478bd9Sstevel@tonic-gate 	case IPPROTO_ND:	return ("ND");
7307c478bd9Sstevel@tonic-gate 	case IPPROTO_EON:	return ("EON");
7317c478bd9Sstevel@tonic-gate 	case IPPROTO_RAW:	return ("RAW");
7327c478bd9Sstevel@tonic-gate 	case IPPROTO_OSPF:	return ("OSPF");
7337c478bd9Sstevel@tonic-gate 	default:		return ("");
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate static void
73845916cd2Sjpk prt_routing_hdr(int flags, const struct ip6_rthdr *ipv6ext_rthdr)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate 	uint8_t nxt_hdr;
7417c478bd9Sstevel@tonic-gate 	uint8_t type;
7427c478bd9Sstevel@tonic-gate 	uint32_t len;
7437c478bd9Sstevel@tonic-gate 	uint8_t segleft;
7447c478bd9Sstevel@tonic-gate 	uint32_t numaddrs;
7457c478bd9Sstevel@tonic-gate 	int i;
7467c478bd9Sstevel@tonic-gate 	struct ip6_rthdr0 *ipv6ext_rthdr0;
7477c478bd9Sstevel@tonic-gate 	struct in6_addr *addrs;
7487c478bd9Sstevel@tonic-gate 	char addr[INET6_ADDRSTRLEN];
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	/* in summary mode, we don't do anything. */
7517c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
7527c478bd9Sstevel@tonic-gate 		return;
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	nxt_hdr = ipv6ext_rthdr->ip6r_nxt;
7567c478bd9Sstevel@tonic-gate 	type = ipv6ext_rthdr->ip6r_type;
7577c478bd9Sstevel@tonic-gate 	len = 8 * (ipv6ext_rthdr->ip6r_len + 1);
7587c478bd9Sstevel@tonic-gate 	segleft = ipv6ext_rthdr->ip6r_segleft;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	show_header("IPv6-Route:  ", "IPv6 Routing Header", 0);
7617c478bd9Sstevel@tonic-gate 	show_space();
7627c478bd9Sstevel@tonic-gate 
76345916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
7647c478bd9Sstevel@tonic-gate 	    "Next header = %d (%s)", nxt_hdr, getproto(nxt_hdr));
76545916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
7667c478bd9Sstevel@tonic-gate 	    "Header length = %d", len);
76745916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
7687c478bd9Sstevel@tonic-gate 	    "Routing type = %d", type);
76945916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
7707c478bd9Sstevel@tonic-gate 	    "Segments left = %d", segleft);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	if (type == IPV6_RTHDR_TYPE_0) {
7737c478bd9Sstevel@tonic-gate 		/*
7747c478bd9Sstevel@tonic-gate 		 * XXX This loop will print all addresses in the routing header,
7757c478bd9Sstevel@tonic-gate 		 * XXX not just the segments left.
7767c478bd9Sstevel@tonic-gate 		 * XXX (The header length field is twice the number of
7777c478bd9Sstevel@tonic-gate 		 * XXX addresses)
7787c478bd9Sstevel@tonic-gate 		 * XXX At some future time, we may want to change this
7797c478bd9Sstevel@tonic-gate 		 * XXX to differentiate between the hops yet to do
7807c478bd9Sstevel@tonic-gate 		 * XXX and the hops already taken.
7817c478bd9Sstevel@tonic-gate 		 */
78245916cd2Sjpk 		/* LINTED: alignment */
7837c478bd9Sstevel@tonic-gate 		ipv6ext_rthdr0 = (struct ip6_rthdr0 *)ipv6ext_rthdr;
7847c478bd9Sstevel@tonic-gate 		numaddrs = ipv6ext_rthdr0->ip6r0_len / 2;
7857c478bd9Sstevel@tonic-gate 		addrs = (struct in6_addr *)(ipv6ext_rthdr0 + 1);
7867c478bd9Sstevel@tonic-gate 		for (i = 0; i < numaddrs; i++) {
7877c478bd9Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, &addrs[i], addr,
7887c478bd9Sstevel@tonic-gate 			    INET6_ADDRSTRLEN);
78945916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
7907c478bd9Sstevel@tonic-gate 			    "address[%d]=%s", i, addr);
7917c478bd9Sstevel@tonic-gate 		}
7927c478bd9Sstevel@tonic-gate 	}
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	show_space();
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate static void
79845916cd2Sjpk prt_fragment_hdr(int flags, const struct ip6_frag *ipv6ext_frag)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	boolean_t morefrag;
8017c478bd9Sstevel@tonic-gate 	uint16_t fragoffset;
8027c478bd9Sstevel@tonic-gate 	uint8_t nxt_hdr;
8037c478bd9Sstevel@tonic-gate 	uint32_t fragident;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	/* extract the various fields from the fragment header */
8067c478bd9Sstevel@tonic-gate 	nxt_hdr = ipv6ext_frag->ip6f_nxt;
8077c478bd9Sstevel@tonic-gate 	morefrag = (ipv6ext_frag->ip6f_offlg & IP6F_MORE_FRAG) == 0
8087c478bd9Sstevel@tonic-gate 	    ? B_FALSE : B_TRUE;
8097c478bd9Sstevel@tonic-gate 	fragoffset = ntohs(ipv6ext_frag->ip6f_offlg & IP6F_OFF_MASK);
8107c478bd9Sstevel@tonic-gate 	fragident = ntohl(ipv6ext_frag->ip6f_ident);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
81345916cd2Sjpk 		(void) snprintf(get_sum_line(), MAXLINE,
814*5086f56fSPaul Wernau 		    "IPv6 fragment ID=%u Offset=%-4d MF=%d",
8157c478bd9Sstevel@tonic-gate 		    fragident,
8167c478bd9Sstevel@tonic-gate 		    fragoffset,
8177c478bd9Sstevel@tonic-gate 		    morefrag);
8187c478bd9Sstevel@tonic-gate 	} else { /* F_DTAIL */
8197c478bd9Sstevel@tonic-gate 		show_header("IPv6-Frag:  ", "IPv6 Fragment Header", 0);
8207c478bd9Sstevel@tonic-gate 		show_space();
8217c478bd9Sstevel@tonic-gate 
82245916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
8237c478bd9Sstevel@tonic-gate 		    "Next Header = %d (%s)", nxt_hdr, getproto(nxt_hdr));
82445916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
8257c478bd9Sstevel@tonic-gate 		    "Fragment Offset = %d", fragoffset);
82645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
8277c478bd9Sstevel@tonic-gate 		    "More Fragments Flag = %s", morefrag ? "true" : "false");
82845916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
829*5086f56fSPaul Wernau 		    "Identification = %u", fragident);
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 		show_space();
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate static void
83645916cd2Sjpk print_ip6opt_ls(const uchar_t *data, unsigned int op_len)
8377c478bd9Sstevel@tonic-gate {
83845916cd2Sjpk 	uint32_t doi;
83945916cd2Sjpk 	uint8_t sotype, solen;
84045916cd2Sjpk 	uint16_t value, value2;
84145916cd2Sjpk 	char *cp;
84245916cd2Sjpk 	int remlen;
84345916cd2Sjpk 	boolean_t printed;
84445916cd2Sjpk 
84545916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
84645916cd2Sjpk 	    "Labeled Security Option len = %u bytes%s", op_len,
84745916cd2Sjpk 	    op_len < sizeof (uint32_t) || (op_len & 1) != 0 ? "?" : "");
84845916cd2Sjpk 	if (op_len < sizeof (uint32_t))
84945916cd2Sjpk 		return;
85045916cd2Sjpk 	GETINT32(doi, data);
85145916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
85245916cd2Sjpk 	    "    DOI = %d (%s)", doi, doi == IP6LS_DOI_V4 ? "IPv4" : "???");
85345916cd2Sjpk 	op_len -= sizeof (uint32_t);
85445916cd2Sjpk 	while (op_len > 0) {
85545916cd2Sjpk 		GETINT8(sotype, data);
85645916cd2Sjpk 		if (op_len < 2) {
85745916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
85845916cd2Sjpk 			    "    truncated %u suboption (no len)", sotype);
85945916cd2Sjpk 			break;
86045916cd2Sjpk 		}
86145916cd2Sjpk 		GETINT8(solen, data);
86245916cd2Sjpk 		if (solen < 2 || solen > op_len) {
86345916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
86445916cd2Sjpk 			    "    bad %u suboption (len 2 <= %u <= %u)",
86545916cd2Sjpk 			    sotype, solen, op_len);
86645916cd2Sjpk 			if (solen < 2)
86745916cd2Sjpk 				solen = 2;
86845916cd2Sjpk 			if (solen > op_len)
86945916cd2Sjpk 				solen = op_len;
87045916cd2Sjpk 		}
87145916cd2Sjpk 		op_len -= solen;
87245916cd2Sjpk 		solen -= 2;
87345916cd2Sjpk 		cp = get_line(0, 0);
87445916cd2Sjpk 		remlen = get_line_remain();
87545916cd2Sjpk 		(void) strlcpy(cp, "    ", remlen);
87645916cd2Sjpk 		cp += 4;
87745916cd2Sjpk 		remlen -= 4;
87845916cd2Sjpk 		printed = B_TRUE;
87945916cd2Sjpk 		switch (sotype) {
88045916cd2Sjpk 		case IP6LS_TT_LEVEL:
88145916cd2Sjpk 			if (solen != 2) {
88245916cd2Sjpk 				printed = B_FALSE;
88345916cd2Sjpk 				break;
88445916cd2Sjpk 			}
88545916cd2Sjpk 			GETINT16(value, data);
88645916cd2Sjpk 			(void) snprintf(cp, remlen, "Level %u", value);
88745916cd2Sjpk 			solen = 0;
88845916cd2Sjpk 			break;
88945916cd2Sjpk 		case IP6LS_TT_VECTOR:
89045916cd2Sjpk 			(void) strlcpy(cp, "Bit-Vector: ", remlen);
89145916cd2Sjpk 			remlen -= strlen(cp);
89245916cd2Sjpk 			cp += strlen(cp);
89345916cd2Sjpk 			while (solen > 1) {
89445916cd2Sjpk 				GETINT16(value, data);
89545916cd2Sjpk 				solen -= 2;
89645916cd2Sjpk 				(void) snprintf(cp, remlen, "%04x", value);
89745916cd2Sjpk 				remlen -= strlen(cp);
89845916cd2Sjpk 				cp += strlen(cp);
89945916cd2Sjpk 			}
90045916cd2Sjpk 			break;
90145916cd2Sjpk 		case IP6LS_TT_ENUM:
90245916cd2Sjpk 			(void) strlcpy(cp, "Enumeration:", remlen);
90345916cd2Sjpk 			remlen -= strlen(cp);
90445916cd2Sjpk 			cp += strlen(cp);
90545916cd2Sjpk 			while (solen > 1) {
90645916cd2Sjpk 				GETINT16(value, data);
90745916cd2Sjpk 				solen -= 2;
90845916cd2Sjpk 				(void) snprintf(cp, remlen, " %u", value);
90945916cd2Sjpk 				remlen -= strlen(cp);
91045916cd2Sjpk 				cp += strlen(cp);
91145916cd2Sjpk 			}
91245916cd2Sjpk 			break;
91345916cd2Sjpk 		case IP6LS_TT_RANGES:
91445916cd2Sjpk 			(void) strlcpy(cp, "Ranges:", remlen);
91545916cd2Sjpk 			remlen -= strlen(cp);
91645916cd2Sjpk 			cp += strlen(cp);
91745916cd2Sjpk 			while (solen > 3) {
91845916cd2Sjpk 				GETINT16(value, data);
91945916cd2Sjpk 				GETINT16(value2, data);
92045916cd2Sjpk 				solen -= 4;
92145916cd2Sjpk 				(void) snprintf(cp, remlen, " %u-%u", value,
92245916cd2Sjpk 				    value2);
92345916cd2Sjpk 				remlen -= strlen(cp);
92445916cd2Sjpk 				cp += strlen(cp);
92545916cd2Sjpk 			}
92645916cd2Sjpk 			break;
92745916cd2Sjpk 		case IP6LS_TT_V4:
92845916cd2Sjpk 			(void) strlcpy(cp, "IPv4 Option", remlen);
92945916cd2Sjpk 			print_ipoptions(data, solen);
93045916cd2Sjpk 			solen = 0;
93145916cd2Sjpk 			break;
93245916cd2Sjpk 		case IP6LS_TT_DEST:
93345916cd2Sjpk 			(void) snprintf(cp, remlen,
93445916cd2Sjpk 			    "Destination-Only Data length %u", solen);
93545916cd2Sjpk 			solen = 0;
93645916cd2Sjpk 			break;
93745916cd2Sjpk 		default:
93845916cd2Sjpk 			(void) snprintf(cp, remlen,
93945916cd2Sjpk 			    "    unknown %u suboption (len %u)", sotype, solen);
94045916cd2Sjpk 			solen = 0;
94145916cd2Sjpk 			break;
94245916cd2Sjpk 		}
94345916cd2Sjpk 		if (solen != 0) {
94445916cd2Sjpk 			if (printed) {
94545916cd2Sjpk 				cp = get_line(0, 0);
94645916cd2Sjpk 				remlen = get_line_remain();
94745916cd2Sjpk 			}
94845916cd2Sjpk 			(void) snprintf(cp, remlen,
94945916cd2Sjpk 			    "    malformed %u suboption (remaining %u)",
95045916cd2Sjpk 			    sotype, solen);
95145916cd2Sjpk 			data += solen;
95245916cd2Sjpk 		}
95345916cd2Sjpk 	}
95445916cd2Sjpk }
95545916cd2Sjpk 
95645916cd2Sjpk static void
95745916cd2Sjpk prt_hbh_options(int flags, const struct ip6_hbh *ipv6ext_hbh)
95845916cd2Sjpk {
95945916cd2Sjpk 	const uint8_t *data, *ndata;
96045916cd2Sjpk 	uint32_t len;
9617c478bd9Sstevel@tonic-gate 	uint8_t op_type;
9627c478bd9Sstevel@tonic-gate 	uint8_t op_len;
9637c478bd9Sstevel@tonic-gate 	uint8_t nxt_hdr;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/* in summary mode, we don't do anything. */
9667c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
9677c478bd9Sstevel@tonic-gate 		return;
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	show_header("IPv6-HopOpts:  ", "IPv6 Hop-by-Hop Options Header", 0);
9717c478bd9Sstevel@tonic-gate 	show_space();
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	/*
9747c478bd9Sstevel@tonic-gate 	 * Store the lengh of this ext hdr in bytes.  The caller has
9757c478bd9Sstevel@tonic-gate 	 * ensured that there is at least len bytes of data left.
9767c478bd9Sstevel@tonic-gate 	 */
9777c478bd9Sstevel@tonic-gate 	len = ipv6ext_hbh->ip6h_len * 8 + 8;
9787c478bd9Sstevel@tonic-gate 
97945916cd2Sjpk 	ndata = (const uint8_t *)ipv6ext_hbh + 2;
9807c478bd9Sstevel@tonic-gate 	len -= 2;
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	nxt_hdr = ipv6ext_hbh->ip6h_nxt;
98345916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
9847c478bd9Sstevel@tonic-gate 	    "Next Header = %u (%s)", nxt_hdr, getproto(nxt_hdr));
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	while (len > 0) {
98745916cd2Sjpk 		data = ndata;
9887c478bd9Sstevel@tonic-gate 		GETINT8(op_type, data);
98945916cd2Sjpk 		/* This is the only one-octet IPv6 option */
99045916cd2Sjpk 		if (op_type == IP6OPT_PAD1) {
99145916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
9927c478bd9Sstevel@tonic-gate 			    "pad1 option ");
9937c478bd9Sstevel@tonic-gate 			len--;
99445916cd2Sjpk 			ndata = data;
99545916cd2Sjpk 			continue;
99645916cd2Sjpk 		}
99745916cd2Sjpk 		GETINT8(op_len, data);
99845916cd2Sjpk 		if (len < 2 || op_len + 2 > len) {
99945916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
100045916cd2Sjpk 			    "Error: option %u truncated (%u + 2 > %u)",
100145916cd2Sjpk 			    op_type, op_len, len);
100245916cd2Sjpk 			op_len = len - 2;
100345916cd2Sjpk 			/*
100445916cd2Sjpk 			 * Continue processing the malformed option so that we
100545916cd2Sjpk 			 * can display as much as possible.
100645916cd2Sjpk 			 */
100745916cd2Sjpk 		}
100845916cd2Sjpk 
100945916cd2Sjpk 		/* advance pointers to the next option */
101045916cd2Sjpk 		len -= op_len + 2;
101145916cd2Sjpk 		ndata = data + op_len;
101245916cd2Sjpk 
101345916cd2Sjpk 		/* process this option */
101445916cd2Sjpk 		switch (op_type) {
10157c478bd9Sstevel@tonic-gate 		case IP6OPT_PADN:
101645916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
10177c478bd9Sstevel@tonic-gate 			    "padN option len = %u", op_len);
10187c478bd9Sstevel@tonic-gate 			break;
10197c478bd9Sstevel@tonic-gate 		case IP6OPT_JUMBO: {
10207c478bd9Sstevel@tonic-gate 			uint32_t payload_len;
10217c478bd9Sstevel@tonic-gate 
102245916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
102345916cd2Sjpk 			    "Jumbo Payload Option len = %u bytes%s", op_len,
102445916cd2Sjpk 			    op_len == sizeof (uint32_t) ? "" : "?");
10257c478bd9Sstevel@tonic-gate 			if (op_len == sizeof (uint32_t)) {
10267c478bd9Sstevel@tonic-gate 				GETINT32(payload_len, data);
102745916cd2Sjpk 				(void) snprintf(get_line(0, 0),
102845916cd2Sjpk 				    get_line_remain(),
10297c478bd9Sstevel@tonic-gate 				    "Jumbo Payload Length = %u bytes",
10307c478bd9Sstevel@tonic-gate 				    payload_len);
10317c478bd9Sstevel@tonic-gate 			}
10327c478bd9Sstevel@tonic-gate 			break;
10337c478bd9Sstevel@tonic-gate 		}
10347c478bd9Sstevel@tonic-gate 		case IP6OPT_ROUTER_ALERT: {
10357c478bd9Sstevel@tonic-gate 			uint16_t value;
10367c478bd9Sstevel@tonic-gate 			const char *label[] = {"MLD", "RSVP", "AN"};
10377c478bd9Sstevel@tonic-gate 
103845916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
103945916cd2Sjpk 			    "Router Alert Option len = %u bytes%s", op_len,
104045916cd2Sjpk 			    op_len == sizeof (uint16_t) ? "" : "?");
10417c478bd9Sstevel@tonic-gate 			if (op_len == sizeof (uint16_t)) {
10427c478bd9Sstevel@tonic-gate 				GETINT16(value, data);
104345916cd2Sjpk 				(void) snprintf(get_line(0, 0),
104445916cd2Sjpk 				    get_line_remain(),
10457c478bd9Sstevel@tonic-gate 				    "Alert Type = %d (%s)", value,
10467c478bd9Sstevel@tonic-gate 				    value < sizeof (label) / sizeof (label[0]) ?
10477c478bd9Sstevel@tonic-gate 				    label[value] : "???");
10487c478bd9Sstevel@tonic-gate 			}
10497c478bd9Sstevel@tonic-gate 			break;
10507c478bd9Sstevel@tonic-gate 		}
105145916cd2Sjpk 		case IP6OPT_LS:
105245916cd2Sjpk 			print_ip6opt_ls(data, op_len);
105345916cd2Sjpk 			break;
10547c478bd9Sstevel@tonic-gate 		default:
105545916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
10567c478bd9Sstevel@tonic-gate 			    "Option type = %u, len = %u", op_type, op_len);
10577c478bd9Sstevel@tonic-gate 			break;
10587c478bd9Sstevel@tonic-gate 		}
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	show_space();
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate static void
106545916cd2Sjpk prt_dest_options(int flags, const struct ip6_dest *ipv6ext_dest)
10667c478bd9Sstevel@tonic-gate {
106745916cd2Sjpk 	const uint8_t *data, *ndata;
106845916cd2Sjpk 	uint32_t len;
10697c478bd9Sstevel@tonic-gate 	uint8_t op_type;
10707c478bd9Sstevel@tonic-gate 	uint32_t op_len;
10717c478bd9Sstevel@tonic-gate 	uint8_t nxt_hdr;
10727c478bd9Sstevel@tonic-gate 	uint8_t value;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	/* in summary mode, we don't do anything. */
10757c478bd9Sstevel@tonic-gate 	if (flags & F_SUM) {
10767c478bd9Sstevel@tonic-gate 		return;
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	show_header("IPv6-DstOpts:  ", "IPv6 Destination Options Header", 0);
10807c478bd9Sstevel@tonic-gate 	show_space();
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	/*
10837c478bd9Sstevel@tonic-gate 	 * Store the length of this ext hdr in bytes.  The caller has
10847c478bd9Sstevel@tonic-gate 	 * ensured that there is at least len bytes of data left.
10857c478bd9Sstevel@tonic-gate 	 */
10867c478bd9Sstevel@tonic-gate 	len = ipv6ext_dest->ip6d_len * 8 + 8;
10877c478bd9Sstevel@tonic-gate 
108845916cd2Sjpk 	ndata = (const uint8_t *)ipv6ext_dest + 2; /* skip hdr/len */
10897c478bd9Sstevel@tonic-gate 	len -= 2;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	nxt_hdr = ipv6ext_dest->ip6d_nxt;
109245916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
10937c478bd9Sstevel@tonic-gate 	    "Next Header = %u (%s)", nxt_hdr, getproto(nxt_hdr));
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	while (len > 0) {
109645916cd2Sjpk 		data = ndata;
10977c478bd9Sstevel@tonic-gate 		GETINT8(op_type, data);
109845916cd2Sjpk 		if (op_type == IP6OPT_PAD1) {
109945916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
11007c478bd9Sstevel@tonic-gate 			    "pad1 option ");
11017c478bd9Sstevel@tonic-gate 			len--;
110245916cd2Sjpk 			ndata = data;
110345916cd2Sjpk 			continue;
110445916cd2Sjpk 		}
110545916cd2Sjpk 		GETINT8(op_len, data);
110645916cd2Sjpk 		if (len < 2 || op_len + 2 > len) {
110745916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
110845916cd2Sjpk 			    "Error: option %u truncated (%u + 2 > %u)",
110945916cd2Sjpk 			    op_type, op_len, len);
111045916cd2Sjpk 			op_len = len - 2;
111145916cd2Sjpk 			/*
111245916cd2Sjpk 			 * Continue processing the malformed option so that we
111345916cd2Sjpk 			 * can display as much as possible.
111445916cd2Sjpk 			 */
111545916cd2Sjpk 		}
111645916cd2Sjpk 
111745916cd2Sjpk 		/* advance pointers to the next option */
111845916cd2Sjpk 		len -= op_len + 2;
111945916cd2Sjpk 		ndata = data + op_len;
112045916cd2Sjpk 
112145916cd2Sjpk 		/* process this option */
112245916cd2Sjpk 		switch (op_type) {
11237c478bd9Sstevel@tonic-gate 		case IP6OPT_PADN:
112445916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
11257c478bd9Sstevel@tonic-gate 			    "padN option len = %u", op_len);
11267c478bd9Sstevel@tonic-gate 			break;
11277c478bd9Sstevel@tonic-gate 		case IP6OPT_TUNNEL_LIMIT:
11287c478bd9Sstevel@tonic-gate 			GETINT8(value, data);
112945916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
11307c478bd9Sstevel@tonic-gate 			    "tunnel encapsulation limit len = %d, value = %d",
11317c478bd9Sstevel@tonic-gate 			    op_len, value);
113245916cd2Sjpk 			break;
113345916cd2Sjpk 		case IP6OPT_LS:
113445916cd2Sjpk 			print_ip6opt_ls(data, op_len);
11357c478bd9Sstevel@tonic-gate 			break;
11367c478bd9Sstevel@tonic-gate 		default:
113745916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
11387c478bd9Sstevel@tonic-gate 			    "Option type = %u, len = %u", op_type, op_len);
113945916cd2Sjpk 			break;
114045916cd2Sjpk 		}
114145916cd2Sjpk 	}
114245916cd2Sjpk 
114345916cd2Sjpk 	show_space();
114445916cd2Sjpk }
114545916cd2Sjpk 
114645916cd2Sjpk #define	ALABEL_MAXLEN	256
114745916cd2Sjpk 
114845916cd2Sjpk static char ascii_label[ALABEL_MAXLEN];
114945916cd2Sjpk static char *plabel = ascii_label;
115045916cd2Sjpk 
115145916cd2Sjpk struct snoop_pair {
115245916cd2Sjpk 	int val;
115345916cd2Sjpk 	const char *name;
115445916cd2Sjpk };
115545916cd2Sjpk 
115645916cd2Sjpk static struct snoop_pair ripso_class_tbl[] = {
115745916cd2Sjpk 	TSOL_CL_TOP_SECRET,	"TOP SECRET",
115845916cd2Sjpk 	TSOL_CL_SECRET,		"SECRET",
115945916cd2Sjpk 	TSOL_CL_CONFIDENTIAL,	"CONFIDENTIAL",
116045916cd2Sjpk 	TSOL_CL_UNCLASSIFIED,	"UNCLASSIFIED",
116145916cd2Sjpk 	-1,			NULL
116245916cd2Sjpk };
116345916cd2Sjpk 
116445916cd2Sjpk static struct snoop_pair ripso_prot_tbl[] = {
116545916cd2Sjpk 	TSOL_PA_GENSER,		"GENSER",
116645916cd2Sjpk 	TSOL_PA_SIOP_ESI,	"SIOP-ESI",
116745916cd2Sjpk 	TSOL_PA_SCI,		"SCI",
116845916cd2Sjpk 	TSOL_PA_NSA,		"NSA",
116945916cd2Sjpk 	TSOL_PA_DOE,		"DOE",
117045916cd2Sjpk 	0x04,			"UNASSIGNED",
117145916cd2Sjpk 	0x02,			"UNASSIGNED",
117245916cd2Sjpk 	-1,			NULL
117345916cd2Sjpk };
117445916cd2Sjpk 
117545916cd2Sjpk static struct snoop_pair *
117645916cd2Sjpk get_pair_byval(struct snoop_pair pairlist[], int val)
117745916cd2Sjpk {
117845916cd2Sjpk 	int i;
117945916cd2Sjpk 
118045916cd2Sjpk 	for (i = 0; pairlist[i].name != NULL; i++)
118145916cd2Sjpk 		if (pairlist[i].val == val)
118245916cd2Sjpk 			return (&pairlist[i]);
118345916cd2Sjpk 	return (NULL);
118445916cd2Sjpk }
118545916cd2Sjpk 
118645916cd2Sjpk static void
118745916cd2Sjpk print_ripso(const uchar_t *opt)
118845916cd2Sjpk {
118945916cd2Sjpk 	struct snoop_pair *ripso_class;
119045916cd2Sjpk 	int i, index, prot_len;
119145916cd2Sjpk 	boolean_t first_prot;
119245916cd2Sjpk 	char line[100], *ptr;
119345916cd2Sjpk 
119445916cd2Sjpk 	prot_len = opt[1] - 3;
119545916cd2Sjpk 	if (prot_len < 0)
119645916cd2Sjpk 		return;
119745916cd2Sjpk 
119845916cd2Sjpk 	show_header("RIPSO:  ", "Revised IP Security Option", 0);
119945916cd2Sjpk 	show_space();
120045916cd2Sjpk 
120145916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
120245916cd2Sjpk 	    "Type = Basic Security Option (%d), Length = %d", opt[0], opt[1]);
120345916cd2Sjpk 
120445916cd2Sjpk 	/*
120545916cd2Sjpk 	 * Display Classification Level
120645916cd2Sjpk 	 */
120745916cd2Sjpk 	ripso_class = get_pair_byval(ripso_class_tbl, (int)opt[2]);
120845916cd2Sjpk 	if (ripso_class != NULL)
120945916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
121045916cd2Sjpk 		    "Classification = Unknown (0x%02x)", opt[2]);
121145916cd2Sjpk 	else
121245916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
121345916cd2Sjpk 		    "Classification = %s (0x%02x)",
121445916cd2Sjpk 		    ripso_class->name, ripso_class->val);
121545916cd2Sjpk 
121645916cd2Sjpk 	/*
121745916cd2Sjpk 	 * Display Protection Authority Flags
121845916cd2Sjpk 	 */
121945916cd2Sjpk 	(void) snprintf(line, sizeof (line), "Protection Authority = ");
122045916cd2Sjpk 	ptr = line;
122145916cd2Sjpk 	first_prot = B_TRUE;
122245916cd2Sjpk 	for (i = 0; i < prot_len; i++) {
122345916cd2Sjpk 		index = 0;
122445916cd2Sjpk 		while (ripso_prot_tbl[index].name != NULL) {
122545916cd2Sjpk 			if (opt[3 + i] & ripso_prot_tbl[index].val) {
122645916cd2Sjpk 				ptr = strchr(ptr, 0);
122745916cd2Sjpk 				if (!first_prot) {
122845916cd2Sjpk 					(void) strlcpy(ptr, ", ",
122945916cd2Sjpk 					    sizeof (line) - (ptr - line));
123045916cd2Sjpk 					ptr = strchr(ptr, 0);
123145916cd2Sjpk 				}
123245916cd2Sjpk 				(void) snprintf(ptr,
123345916cd2Sjpk 				    sizeof (line) - (ptr - line),
123445916cd2Sjpk 				    "%s (0x%02x)",
123545916cd2Sjpk 				    ripso_prot_tbl[index].name,
123645916cd2Sjpk 				    ripso_prot_tbl[index].val);
123745916cd2Sjpk 			}
123845916cd2Sjpk 			index++;
12397c478bd9Sstevel@tonic-gate 		}
124045916cd2Sjpk 		if ((opt[3 + i] & 1) == 0)
12417c478bd9Sstevel@tonic-gate 			break;
124245916cd2Sjpk 	}
124345916cd2Sjpk 	if (!first_prot)
124445916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "%s", line);
124545916cd2Sjpk 	else
124645916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "%sNone",
124745916cd2Sjpk 		    line);
124845916cd2Sjpk }
124945916cd2Sjpk 
125045916cd2Sjpk #define	CIPSO_GENERIC_ARRAY_LEN	200
125145916cd2Sjpk 
125245916cd2Sjpk /*
125345916cd2Sjpk  * Return 1 if CIPSO SL and Categories are all 1's; 0 otherwise.
125445916cd2Sjpk  *
125545916cd2Sjpk  * Note: opt starts with "Tag Type":
125645916cd2Sjpk  *
125745916cd2Sjpk  * |tag_type(1)|tag_length(1)|align(1)|sl(1)|categories(variable)|
125845916cd2Sjpk  *
125945916cd2Sjpk  */
126045916cd2Sjpk static boolean_t
126145916cd2Sjpk cipso_high(const uchar_t *opt)
126245916cd2Sjpk {
126345916cd2Sjpk 	int i;
126445916cd2Sjpk 
126545916cd2Sjpk 	if (((int)opt[1] + 6) < IP_MAX_OPT_LENGTH)
126645916cd2Sjpk 		return (B_FALSE);
126745916cd2Sjpk 	for (i = 0; i < ((int)opt[1] - 3); i++)
126845916cd2Sjpk 		if (opt[3 + i] != 0xff)
126945916cd2Sjpk 			return (B_FALSE);
127045916cd2Sjpk 	return (B_TRUE);
127145916cd2Sjpk }
127245916cd2Sjpk 
127345916cd2Sjpk /*
127445916cd2Sjpk  * Converts CIPSO label to SL.
127545916cd2Sjpk  *
127645916cd2Sjpk  * Note: opt starts with "Tag Type":
127745916cd2Sjpk  *
127845916cd2Sjpk  * |tag_type(1)|tag_length(1)|align(1)|sl(1)|categories(variable)|
127945916cd2Sjpk  *
128045916cd2Sjpk  */
128145916cd2Sjpk static void
128245916cd2Sjpk cipso2sl(const uchar_t *opt, bslabel_t *sl, int *high)
128345916cd2Sjpk {
128445916cd2Sjpk 	int i, taglen;
128545916cd2Sjpk 	uchar_t *q = (uchar_t *)&((_bslabel_impl_t *)sl)->compartments;
128645916cd2Sjpk 
128745916cd2Sjpk 	*high = 0;
128845916cd2Sjpk 	taglen = opt[1];
128945916cd2Sjpk 	memset((caddr_t)sl, 0, sizeof (bslabel_t));
129045916cd2Sjpk 
129145916cd2Sjpk 	if (cipso_high(opt)) {
129245916cd2Sjpk 		BSLHIGH(sl);
129345916cd2Sjpk 		*high = 1;
129445916cd2Sjpk 	} else {
129545916cd2Sjpk 		LCLASS_SET((_bslabel_impl_t *)sl, opt[3]);
129645916cd2Sjpk 		for (i = 0; i < taglen - TSOL_TT1_MIN_LENGTH; i++)
129745916cd2Sjpk 			q[i] = opt[TSOL_TT1_MIN_LENGTH + i];
129845916cd2Sjpk 	}
129945916cd2Sjpk 	SETBLTYPE(sl, SUN_SL_ID);
130045916cd2Sjpk }
130145916cd2Sjpk 
130245916cd2Sjpk static int
130345916cd2Sjpk interpret_cipso_tagtype1(const uchar_t *opt)
130445916cd2Sjpk {
130545916cd2Sjpk 	int i, taglen, ishigh;
130645916cd2Sjpk 	bslabel_t sl;
130745916cd2Sjpk 	char line[CIPSO_GENERIC_ARRAY_LEN], *ptr;
130845916cd2Sjpk 
130945916cd2Sjpk 	taglen = opt[1];
131045916cd2Sjpk 	if (taglen < TSOL_TT1_MIN_LENGTH ||
131145916cd2Sjpk 	    taglen > TSOL_TT1_MAX_LENGTH)
131245916cd2Sjpk 		return (taglen);
131345916cd2Sjpk 
131445916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
131545916cd2Sjpk 	    "Tag Type = %d, Tag Length = %d", opt[0], opt[1]);
131645916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
131745916cd2Sjpk 	    "Sensitivity Level = 0x%02x", opt[3]);
131845916cd2Sjpk 	ptr = line;
131945916cd2Sjpk 	for (i = 0; i < taglen - TSOL_TT1_MIN_LENGTH; i++) {
132045916cd2Sjpk 		(void) snprintf(ptr, sizeof (line) - (ptr - line), "%02x",
132145916cd2Sjpk 		    opt[TSOL_TT1_MIN_LENGTH + i]);
132245916cd2Sjpk 		ptr = strchr(ptr, 0);
132345916cd2Sjpk 	}
132445916cd2Sjpk 	if (i != 0) {
132545916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
132645916cd2Sjpk 		    "Categories = ");
132745916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(), "\t%s",
132845916cd2Sjpk 		    line);
132945916cd2Sjpk 	} else {
133045916cd2Sjpk 		(void) snprintf(get_line(0, 0), get_line_remain(),
133145916cd2Sjpk 		    "Categories = None");
133245916cd2Sjpk 	}
133345916cd2Sjpk 	cipso2sl(opt, &sl, &ishigh);
1334b9dac67bSrica 	if (is_system_labeled()) {
133545916cd2Sjpk 		if (bsltos(&sl, &plabel, ALABEL_MAXLEN,
133645916cd2Sjpk 		    LONG_CLASSIFICATION|LONG_WORDS|VIEW_INTERNAL) < 0) {
133745916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
133845916cd2Sjpk 			    "The Sensitivity Level and Categories can't be "
133945916cd2Sjpk 			    "mapped to a valid SL");
134045916cd2Sjpk 		} else {
134145916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
134245916cd2Sjpk 			    "The Sensitivity Level and Categories are mapped "
134345916cd2Sjpk 			    "to the SL:");
134445916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
134545916cd2Sjpk 			    "\t%s", ascii_label);
134645916cd2Sjpk 		}
134745916cd2Sjpk 	}
134845916cd2Sjpk 	return (taglen);
134945916cd2Sjpk }
135045916cd2Sjpk 
135145916cd2Sjpk /*
135245916cd2Sjpk  * The following struct definition #define's are copied from TS1.x. They are
135345916cd2Sjpk  * not used here (except TTYPE_3_MAX_TOKENS), but included as a reference for
135445916cd2Sjpk  * the tag type 3 packet format.
135545916cd2Sjpk  */
135645916cd2Sjpk #define	TTYPE_3_MAX_TOKENS	7
135745916cd2Sjpk 
135845916cd2Sjpk /*
135945916cd2Sjpk  * Display CIPSO tag type 3 which is defined by MAXSIX.
136045916cd2Sjpk  */
136145916cd2Sjpk static int
136245916cd2Sjpk interpret_cipso_tagtype3(const uchar_t *opt)
136345916cd2Sjpk {
136445916cd2Sjpk 	uchar_t tagtype;
136545916cd2Sjpk 	int index, numtokens, taglen;
136645916cd2Sjpk 	uint16_t mask;
136745916cd2Sjpk 	uint32_t token;
136845916cd2Sjpk 	static const char *name[] = {
136945916cd2Sjpk 		"SL",
137045916cd2Sjpk 		"NCAV",
137145916cd2Sjpk 		"INTEG",
137245916cd2Sjpk 		"SID",
137345916cd2Sjpk 		"undefined",
137445916cd2Sjpk 		"undefined",
137545916cd2Sjpk 		"IL",
137645916cd2Sjpk 		"PRIVS",
137745916cd2Sjpk 		"LUID",
137845916cd2Sjpk 		"PID",
137945916cd2Sjpk 		"IDS",
138045916cd2Sjpk 		"ACL"
138145916cd2Sjpk 	};
138245916cd2Sjpk 
138345916cd2Sjpk 	tagtype = *opt++;
138445916cd2Sjpk 	(void) memcpy(&mask, opt + 3, sizeof (mask));
138545916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
138645916cd2Sjpk 	    "Tag Type = %d (MAXSIX)", tagtype);
138745916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
138845916cd2Sjpk 	    "Generation = 0x%02x%02x%02x, Mask = 0x%04x", opt[0], opt[1],
138945916cd2Sjpk 	    opt[2], mask);
139045916cd2Sjpk 	opt += 3 + sizeof (mask);
139145916cd2Sjpk 
139245916cd2Sjpk 	/*
139345916cd2Sjpk 	 * Display tokens
139445916cd2Sjpk 	 */
139545916cd2Sjpk 	numtokens = 0;
139645916cd2Sjpk 	index = 0;
139745916cd2Sjpk 	while (mask != 0 && numtokens < TTYPE_3_MAX_TOKENS) {
139845916cd2Sjpk 		if (mask & 0x0001) {
139945916cd2Sjpk 			(void) memcpy(&token, opt, sizeof (token));
140045916cd2Sjpk 			opt += sizeof (token);
140145916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
140245916cd2Sjpk 			    "Attribute = %s, Token = 0x%08x",
140345916cd2Sjpk 			    index < sizeof (name) / sizeof (*name) ?
140445916cd2Sjpk 			    name[index] : "unknown", token);
140545916cd2Sjpk 			numtokens++;
14067c478bd9Sstevel@tonic-gate 		}
140745916cd2Sjpk 		mask = mask >> 1;
140845916cd2Sjpk 		index++;
140945916cd2Sjpk 	}
141045916cd2Sjpk 
141145916cd2Sjpk 	taglen = 6 + numtokens * 4;
141245916cd2Sjpk 	return (taglen);
141345916cd2Sjpk }
141445916cd2Sjpk 
141545916cd2Sjpk static void
141645916cd2Sjpk print_cipso(const uchar_t *opt)
141745916cd2Sjpk {
141845916cd2Sjpk 	int optlen, taglen, tagnum;
141945916cd2Sjpk 	uint32_t doi;
142045916cd2Sjpk 	char line[CIPSO_GENERIC_ARRAY_LEN];
142145916cd2Sjpk 	char *oldnest;
142245916cd2Sjpk 
142345916cd2Sjpk 	optlen = opt[1];
142445916cd2Sjpk 	if (optlen < TSOL_CIPSO_MIN_LENGTH || optlen > TSOL_CIPSO_MAX_LENGTH)
142545916cd2Sjpk 		return;
142645916cd2Sjpk 
142745916cd2Sjpk 	oldnest = prot_nest_prefix;
142845916cd2Sjpk 	prot_nest_prefix = prot_prefix;
142945916cd2Sjpk 	show_header("CIPSO:  ", "Common IP Security Option", 0);
143045916cd2Sjpk 	show_space();
143145916cd2Sjpk 
143245916cd2Sjpk 	/*
143345916cd2Sjpk 	 * Display CIPSO Header
143445916cd2Sjpk 	 */
143545916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
143645916cd2Sjpk 	    "Type = CIPSO (%d), Length = %d", opt[0], opt[1]);
143745916cd2Sjpk 	(void) memcpy(&doi, opt + 2, sizeof (doi));
143845916cd2Sjpk 	(void) snprintf(get_line(0, 0), get_line_remain(),
143945916cd2Sjpk 	    "Domain of Interpretation = %u", (unsigned)ntohl(doi));
144045916cd2Sjpk 
144145916cd2Sjpk 	if (opt[1] == TSOL_CIPSO_MIN_LENGTH) {	/* no tags */
144245916cd2Sjpk 		show_space();
144345916cd2Sjpk 		prot_prefix = prot_nest_prefix;
144445916cd2Sjpk 		prot_nest_prefix = oldnest;
144545916cd2Sjpk 		return;
14467c478bd9Sstevel@tonic-gate 	}
144745916cd2Sjpk 	optlen -= TSOL_CIPSO_MIN_LENGTH;
144845916cd2Sjpk 	opt += TSOL_CIPSO_MIN_LENGTH;
14497c478bd9Sstevel@tonic-gate 
145045916cd2Sjpk 	/*
145145916cd2Sjpk 	 * Display Each Tag
145245916cd2Sjpk 	 */
145345916cd2Sjpk 	tagnum = 1;
145445916cd2Sjpk 	while (optlen >= TSOL_TT1_MIN_LENGTH) {
145545916cd2Sjpk 		(void) snprintf(line, sizeof (line), "Tag# %d", tagnum);
145645916cd2Sjpk 		show_header("CIPSO:  ", line, 0);
145745916cd2Sjpk 		/*
145845916cd2Sjpk 		 * We handle tag type 1 and 3 only. Note, tag type 3
145945916cd2Sjpk 		 * is MAXSIX defined.
146045916cd2Sjpk 		 */
146145916cd2Sjpk 		switch (opt[0]) {
146245916cd2Sjpk 		case 1:
146345916cd2Sjpk 			taglen = interpret_cipso_tagtype1(opt);
146445916cd2Sjpk 			break;
146545916cd2Sjpk 		case 3:
146645916cd2Sjpk 			taglen = interpret_cipso_tagtype3(opt);
146745916cd2Sjpk 			break;
146845916cd2Sjpk 		default:
146945916cd2Sjpk 			(void) snprintf(get_line(0, 0), get_line_remain(),
147045916cd2Sjpk 			    "Unknown Tag Type %d", opt[0]);
147145916cd2Sjpk 			show_space();
147245916cd2Sjpk 			prot_prefix = prot_nest_prefix;
147345916cd2Sjpk 			prot_nest_prefix = oldnest;
147445916cd2Sjpk 			return;
147545916cd2Sjpk 		}
147645916cd2Sjpk 
147745916cd2Sjpk 		/*
147845916cd2Sjpk 		 * Move to the next tag
147945916cd2Sjpk 		 */
148045916cd2Sjpk 		if (taglen <= 0)
148145916cd2Sjpk 			break;
148245916cd2Sjpk 		optlen -= taglen;
148345916cd2Sjpk 		opt += taglen;
148445916cd2Sjpk 		tagnum++;
148545916cd2Sjpk 	}
14867c478bd9Sstevel@tonic-gate 	show_space();
148745916cd2Sjpk 	prot_prefix = prot_nest_prefix;
148845916cd2Sjpk 	prot_nest_prefix = oldnest;
14897c478bd9Sstevel@tonic-gate }
1490