/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright 2012 Milan Jurik. All rights reserved.
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <string.h>
#include <netinet/in_systm.h>
#include <netinet/if_ether.h>
#include <netdb.h>
#include "snoop.h"
/*
* IPv6 extension header masks. These are used by the print_ipv6_extensions()
* function to return information to the caller about which extension headers
* were processed. This can be useful if the caller wants to know if the
* packet is an IPv6 fragment, for example.
*/
static void prt_routing_hdr(int, const struct ip6_rthdr *);
static void prt_fragment_hdr(int, const struct ip6_frag *);
static void prt_hbh_options(int, const struct ip6_hbh *);
static void prt_dest_options(int, const struct ip6_dest *);
static void print_route(const uchar_t *);
static void print_ipoptions(const uchar_t *, int);
static void print_ripso(const uchar_t *);
static void print_cipso(const uchar_t *);
/* Keep track of how many nested IP headers we have. */
unsigned int encap_levels;
int
{
int hdrlen;
return (iplen);
}
if (encap_levels == 0)
total_encap_levels = 0;
encap_levels++;
if (fraglen < 0) {
"IP truncated: header missing %d bytes", -fraglen);
encap_levels--;
}
/*
* We flag this as a fragment if the more fragments bit is set, or
* if the fragment offset is non-zero.
*/
if (morefrag || fragoffset != 0)
if (isfrag) {
"%s IP fragment ID=%d Offset=%-4d MF=%d TOS=0x%x "
"TTL=%d",
} else {
sizeof (buff));
"IP D=%s S=%s LEN=%u%s, ID=%d, TOS=0x%x, TTL=%d",
buff,
}
}
show_space();
"Header length = %d bytes", hdrlen);
" xxx. .... = %d (precedence)",
"low delay", "normal delay"));
"high throughput", "normal throughput"));
"high reliability", "normal reliability"));
"ECN capable transport", "not ECN capable transport"));
"ECN congestion experienced",
"no ECN congestion experienced"));
"Total length = %u bytes%s", uitmp,
"do not fragment", "may fragment"));
"more fragments", "last fragment"));
"Fragment offset = %u bytes",
/*
* XXX need to compute checksum and print whether it's correct
*/
"Header checksum = %04x",
"Source address = %s, %s",
"Destination address = %s, %s",
/* Print IP options - if any */
show_space();
}
/*
* If we are in detail mode, and this is not the first fragment of
* a fragmented packet, print out a little line stating this.
* Otherwise, go to the next protocol layer only if this is not a
* fragment, or we are in detail mode and this is the first fragment
* of a fragmented packet.
*/
"%s: [%d byte(s) of data, continuation of IP ident=%d]",
/* go to the next protocol layer */
if (fraglen > 0) {
case IPPROTO_IP:
break;
case IPPROTO_ENCAP:
(void) interpret_ip(flags,
/* LINTED: alignment */
break;
case IPPROTO_ICMP:
(void) interpret_icmp(flags,
/* LINTED: alignment */
break;
case IPPROTO_IGMP:
break;
case IPPROTO_GGP:
break;
case IPPROTO_TCP:
(void) interpret_tcp(flags,
break;
case IPPROTO_ESP:
fraglen);
break;
case IPPROTO_AH:
fraglen);
break;
case IPPROTO_OSPF:
break;
case IPPROTO_EGP:
case IPPROTO_PUP:
break;
case IPPROTO_UDP:
(void) interpret_udp(flags,
break;
case IPPROTO_IDP:
case IPPROTO_HELLO:
case IPPROTO_ND:
case IPPROTO_RAW:
break;
case IPPROTO_IPV6: /* IPV6 encap */
/* LINTED: alignment */
iplen);
break;
case IPPROTO_SCTP:
(void) interpret_sctp(flags,
break;
}
}
}
encap_levels--;
return (iplen);
}
int
{
/*
* The print_srcname and print_dstname strings are the hostname
* parts of the verbose IPv6 header output, including the comma
* and the space after the litteral address strings.
*/
if (fraglen < 0)
/*
* Use endian-aware masks to extract traffic class and
* flowinfo. Also, flowinfo is now 20 bits and class 8
* rather than 24 and 4.
*/
/*
* NOTE: the F_SUM and F_DTAIL flags are mutually exclusive,
* so the code within the first part of the following if statement
* will not affect the detailed printing of the packet.
*/
"IPv6 S=%s D=%s LEN=%d HOPS=%d CLASS=0x%x FLOW=0x%x",
print_srcname[0] = '\0';
} else {
", %s", src_name);
}
print_dstname[0] = '\0';
} else {
", %s", dst_name);
}
show_space();
"Version = %d", version);
"Traffic Class = %d", class);
"Flow label = 0x%x", flow);
"Payload length = %d", iplen);
"Next Header = %d (%s)", proto,
show_space();
}
/*
* Print IPv6 Extension Headers, or skip them in the summary case.
* Set isfrag to true if one of the extension headers encounterred
* was a fragment header.
*/
&fraglen);
if ((extmask & SNOOP_FRAGMENT) != 0) {
}
}
/*
* We only want to print upper layer information if this is not
* a fragment, or if we're printing in detail. Note that the
* proto variable will be set to IPPROTO_NONE if this is a fragment
* with a non-zero fragment offset.
*/
/* go to the next protocol layer */
switch (proto) {
case IPPROTO_IP:
break;
case IPPROTO_ENCAP:
/* LINTED: alignment */
fraglen);
break;
case IPPROTO_ICMPV6:
/* LINTED: alignment */
fraglen);
break;
case IPPROTO_IGMP:
break;
case IPPROTO_GGP:
break;
case IPPROTO_TCP:
break;
case IPPROTO_ESP:
break;
case IPPROTO_AH:
break;
case IPPROTO_EGP:
case IPPROTO_PUP:
break;
case IPPROTO_UDP:
break;
case IPPROTO_IDP:
case IPPROTO_HELLO:
case IPPROTO_ND:
case IPPROTO_RAW:
break;
case IPPROTO_IPV6:
/* LINTED: alignment */
iplen);
break;
case IPPROTO_SCTP:
break;
case IPPROTO_OSPF:
break;
}
}
return (iplen);
}
/*
* ip_ext: data including the extension header.
* iplen: length of the data remaining in the packet.
* Returns a mask of IPv6 extension headers it processed.
*/
int *fraglen)
{
return (0);
while (is_extension_header) {
/*
* There must be at least enough data left to read the
* next header and header length fields from the next
* header.
*/
if (*fraglen < 2) {
return (extmask);
}
switch (proto) {
case IPPROTO_HOPOPTS:
return (extmask);
}
extmask |= SNOOP_HOPOPTS;
break;
case IPPROTO_DSTOPTS:
return (extmask);
}
extmask |= SNOOP_DSTOPTS;
break;
case IPPROTO_ROUTING:
return (extmask);
}
extmask |= SNOOP_ROUTING;
break;
case IPPROTO_FRAGMENT:
/* LINTED: alignment */
return (extmask);
}
/*
* If this is not the first fragment, forget about
* the rest of the packet, snoop decoding is
* stateless.
*/
else
break;
default:
break;
}
if (is_extension_header) {
}
}
return (extmask);
}
static void
{
int len;
int remain;
char *line;
const char *truncstr;
if (optlen <= 0) {
"No options");
return;
}
"Options: (%d bytes)", optlen);
while (optlen > 0) {
remain = get_line_remain();
switch (opt[0]) {
case IPOPT_EOL:
return;
case IPOPT_NOP:
len = 1;
break;
case IPOPT_RR:
break;
case IPOPT_TS:
break;
case IPOPT_SECURITY:
break;
case IPOPT_COMSEC:
break;
case IPOPT_LSRR:
" - Loose source route (%d bytes%s)", len,
truncstr);
break;
case IPOPT_SATID:
" - SATNET Stream id (%d bytes%s)",
break;
case IPOPT_SSRR:
" - Strict source route, (%d bytes%s)", len,
truncstr);
break;
default:
" - Option %d (unknown - %d bytes%s) %s",
break;
}
if (len <= 0) {
" - Incomplete option len %d", len);
break;
}
}
}
static void
{
char *line;
" Pointer = %d", pointer);
pointer -= IPOPT_MINOFF;
while (len > 0) {
remain = get_line_remain();
else
if (pointer == 0)
}
}
char *
getproto(int p)
{
switch (p) {
case IPPROTO_HOPOPTS: return ("IPv6-HopOpts");
case IPPROTO_IPV6: return ("IPv6");
case IPPROTO_ROUTING: return ("IPv6-Route");
case IPPROTO_FRAGMENT: return ("IPv6-Frag");
case IPPROTO_RSVP: return ("RSVP");
case IPPROTO_ENCAP: return ("IP-in-IP");
case IPPROTO_AH: return ("AH");
case IPPROTO_ESP: return ("ESP");
case IPPROTO_ICMP: return ("ICMP");
case IPPROTO_ICMPV6: return ("ICMPv6");
case IPPROTO_DSTOPTS: return ("IPv6-DstOpts");
case IPPROTO_IGMP: return ("IGMP");
case IPPROTO_GGP: return ("GGP");
case IPPROTO_TCP: return ("TCP");
case IPPROTO_EGP: return ("EGP");
case IPPROTO_PUP: return ("PUP");
case IPPROTO_UDP: return ("UDP");
case IPPROTO_IDP: return ("IDP");
case IPPROTO_HELLO: return ("HELLO");
case IPPROTO_ND: return ("ND");
case IPPROTO_EON: return ("EON");
case IPPROTO_RAW: return ("RAW");
case IPPROTO_OSPF: return ("OSPF");
default: return ("");
}
}
static void
{
int i;
/* in summary mode, we don't do anything. */
return;
}
show_space();
"Header length = %d", len);
"Routing type = %d", type);
"Segments left = %d", segleft);
if (type == IPV6_RTHDR_TYPE_0) {
/*
* XXX This loop will print all addresses in the routing header,
* XXX not just the segments left.
* XXX (The header length field is twice the number of
* XXX addresses)
* XXX At some future time, we may want to change this
* XXX to differentiate between the hops yet to do
* XXX and the hops already taken.
*/
/* LINTED: alignment */
for (i = 0; i < numaddrs; i++) {
"address[%d]=%s", i, addr);
}
}
show_space();
}
static void
{
/* extract the various fields from the fragment header */
"IPv6 fragment ID=%u Offset=%-4d MF=%d",
morefrag);
} else { /* F_DTAIL */
show_space();
"Fragment Offset = %d", fragoffset);
"Identification = %u", fragident);
show_space();
}
}
static void
{
char *cp;
int remlen;
"Labeled Security Option len = %u bytes%s", op_len,
return;
while (op_len > 0) {
if (op_len < 2) {
" truncated %u suboption (no len)", sotype);
break;
}
" bad %u suboption (len 2 <= %u <= %u)",
if (solen < 2)
solen = 2;
}
solen -= 2;
remlen = get_line_remain();
cp += 4;
remlen -= 4;
switch (sotype) {
case IP6LS_TT_LEVEL:
if (solen != 2) {
break;
}
solen = 0;
break;
case IP6LS_TT_VECTOR:
while (solen > 1) {
solen -= 2;
}
break;
case IP6LS_TT_ENUM:
while (solen > 1) {
solen -= 2;
}
break;
case IP6LS_TT_RANGES:
while (solen > 3) {
solen -= 4;
value2);
}
break;
case IP6LS_TT_V4:
solen = 0;
break;
case IP6LS_TT_DEST:
"Destination-Only Data length %u", solen);
solen = 0;
break;
default:
solen = 0;
break;
}
if (solen != 0) {
if (printed) {
remlen = get_line_remain();
}
" malformed %u suboption (remaining %u)",
}
}
}
static void
{
/* in summary mode, we don't do anything. */
return;
}
show_space();
/*
* Store the lengh of this ext hdr in bytes. The caller has
* ensured that there is at least len bytes of data left.
*/
len -= 2;
while (len > 0) {
/* This is the only one-octet IPv6 option */
if (op_type == IP6OPT_PAD1) {
"pad1 option ");
len--;
continue;
}
"Error: option %u truncated (%u + 2 > %u)",
/*
* Continue processing the malformed option so that we
* can display as much as possible.
*/
}
/* advance pointers to the next option */
/* process this option */
switch (op_type) {
case IP6OPT_PADN:
"padN option len = %u", op_len);
break;
case IP6OPT_JUMBO: {
"Jumbo Payload Option len = %u bytes%s", op_len,
"Jumbo Payload Length = %u bytes",
}
break;
}
case IP6OPT_ROUTER_ALERT: {
"Router Alert Option len = %u bytes%s", op_len,
"Alert Type = %d (%s)", value,
}
break;
}
case IP6OPT_LS:
break;
default:
break;
}
}
show_space();
}
static void
{
/* in summary mode, we don't do anything. */
return;
}
show_space();
/*
* Store the length of this ext hdr in bytes. The caller has
* ensured that there is at least len bytes of data left.
*/
len -= 2;
while (len > 0) {
if (op_type == IP6OPT_PAD1) {
"pad1 option ");
len--;
continue;
}
"Error: option %u truncated (%u + 2 > %u)",
/*
* Continue processing the malformed option so that we
* can display as much as possible.
*/
}
/* advance pointers to the next option */
/* process this option */
switch (op_type) {
case IP6OPT_PADN:
"padN option len = %u", op_len);
break;
case IP6OPT_TUNNEL_LIMIT:
"tunnel encapsulation limit len = %d, value = %d",
break;
case IP6OPT_LS:
break;
default:
break;
}
}
show_space();
}
struct snoop_pair {
int val;
const char *name;
};
TSOL_CL_TOP_SECRET, "TOP SECRET",
TSOL_CL_SECRET, "SECRET",
TSOL_CL_CONFIDENTIAL, "CONFIDENTIAL",
TSOL_CL_UNCLASSIFIED, "UNCLASSIFIED",
-1, NULL
};
TSOL_PA_GENSER, "GENSER",
TSOL_PA_SIOP_ESI, "SIOP-ESI",
TSOL_PA_SCI, "SCI",
TSOL_PA_NSA, "NSA",
TSOL_PA_DOE, "DOE",
0x04, "UNASSIGNED",
0x02, "UNASSIGNED",
-1, NULL
};
static struct snoop_pair *
{
int i;
return (&pairlist[i]);
return (NULL);
}
static void
{
if (prot_len < 0)
return;
show_space();
/*
* Display Classification Level
*/
if (ripso_class == NULL)
else
"Classification = %s (0x%02x)",
/*
* Display Protection Authority Flags
*/
first_prot = B_TRUE;
for (i = 0; i < prot_len; i++) {
index = 0;
if (!first_prot) {
}
"%s (0x%02x)",
}
index++;
}
break;
}
if (!first_prot)
else
line);
}
/*
* Return 1 if CIPSO SL and Categories are all 1's; 0 otherwise.
*
* Note: opt starts with "Tag Type":
*
* |tag_type(1)|tag_length(1)|align(1)|sl(1)|categories(variable)|
*
*/
static boolean_t
{
int i;
return (B_FALSE);
return (B_FALSE);
return (B_TRUE);
}
/*
* Converts CIPSO label to SL.
*
* Note: opt starts with "Tag Type":
*
* |tag_type(1)|tag_length(1)|align(1)|sl(1)|categories(variable)|
*
*/
static void
{
int i, taglen;
*high = 0;
if (cipso_high(opt)) {
*high = 1;
} else {
for (i = 0; i < taglen - TSOL_TT1_MIN_LENGTH; i++)
q[i] = opt[TSOL_TT1_MIN_LENGTH + i];
}
}
static int
{
if (taglen < TSOL_TT1_MIN_LENGTH ||
return (taglen);
for (i = 0; i < taglen - TSOL_TT1_MIN_LENGTH; i++) {
opt[TSOL_TT1_MIN_LENGTH + i]);
}
if (i != 0) {
"Categories = ");
line);
} else {
"Categories = None");
}
if (is_system_labeled()) {
"The Sensitivity Level and Categories can't be "
"mapped to a valid SL");
} else {
"The Sensitivity Level and Categories are mapped "
"to the SL:");
"\t%s", ascii_label);
}
}
return (taglen);
}
/*
* The following struct definition #define's are copied from TS1.x. They are
* not used here (except TTYPE_3_MAX_TOKENS), but included as a reference for
* the tag type 3 packet format.
*/
/*
* Display CIPSO tag type 3 which is defined by MAXSIX.
*/
static int
{
static const char *name[] = {
"SL",
"NCAV",
"INTEG",
"SID",
"undefined",
"undefined",
"IL",
"PRIVS",
"LUID",
"PID",
"IDS",
"ACL"
};
"Tag Type = %d (MAXSIX)", tagtype);
/*
* Display tokens
*/
numtokens = 0;
index = 0;
if (mask & 0x0001) {
"Attribute = %s, Token = 0x%08x",
numtokens++;
}
index++;
}
return (taglen);
}
static void
{
char *oldnest;
return;
show_space();
/*
* Display CIPSO Header
*/
show_space();
return;
}
/*
* Display Each Tag
*/
tagnum = 1;
while (optlen >= TSOL_TT1_MIN_LENGTH) {
/*
* We handle tag type 1 and 3 only. Note, tag type 3
* is MAXSIX defined.
*/
switch (opt[0]) {
case 1:
break;
case 3:
break;
default:
"Unknown Tag Type %d", opt[0]);
show_space();
return;
}
/*
* Move to the next tag
*/
if (taglen <= 0)
break;
tagnum++;
}
show_space();
}