snoop_ip.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <ctype.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.
*/
#define SNOOP_HOPOPTS 0x01U
#define SNOOP_ROUTING 0x02U
#define SNOOP_DSTOPTS 0x04U
#define SNOOP_FRAGMENT 0x08U
#define SNOOP_AH 0x10U
#define SNOOP_ESP 0x20U
#define SNOOP_IPV6 0x40U
extern char *dlc_header;
static void prt_routing_hdr();
static void prt_fragment_hdr();
static void prt_hbh_options();
static void prt_dest_options();
static void print_route();
static void print_ipoptions();
char *getproto();
/* Keep track of how many nested IP headers we have. */
unsigned int encap_levels;
unsigned int total_encap_levels = 1;
int
int flags;
int fraglen;
{
char *data;
char buff[24];
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 (encap_levels == 1) {
} /* Else we already have the src_name and dst_name we want! */
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();
get_line_remain(), " xxx. .... = %d (precedence)",
get_line_remain(), " %s",
"low delay", "normal delay"));
get_line_remain(), " %s",
"high throughput", "normal throughput"));
get_line_remain(), " %s",
"high reliability", "normal reliability"));
get_line_remain(), " %s",
"ECN capable transport", "not ECN capable transport"));
get_line_remain(), " %s",
"ECN congestion experienced",
"no ECN congestion experienced"));
get_line_remain(), " %s",
"do not fragment", "may fragment"));
get_line_remain(), " %s",
"more fragments", "last fragment"));
get_line_remain(), "Fragment offset = %u bytes",
get_line_remain(), "Time to live = %d seconds/hops",
/*
* XXX need to compute checksum and print whether it's correct
*/
get_line_remain(), "Header checksum = %04x",
get_line_remain(), "Source address = %s, %s",
get_line_remain(), "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:
fraglen);
break;
case IPPROTO_ICMP:
break;
case IPPROTO_IGMP:
break;
case IPPROTO_GGP:
break;
case IPPROTO_TCP:
break;
case IPPROTO_ESP:
break;
case IPPROTO_AH:
break;
case IPPROTO_OSPF:
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: /* IPV6 encap */
iplen);
break;
case IPPROTO_SCTP:
break;
}
}
}
encap_levels--;
return (iplen);
}
int
int flags;
int fraglen;
{
/*
* 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.
*/
char src_addrstr[INET6_ADDRSTRLEN];
char dst_addrstr[INET6_ADDRSTRLEN];
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.
*/
"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);
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:
break;
case IPPROTO_ICMPV6:
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:
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)
{
struct ip6_hbh *ipv6ext_hbh;
struct ip6_dest *ipv6ext_dest;
struct ip6_rthdr *ipv6ext_rthdr;
struct ip6_frag *ipv6ext_frag;
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:
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 optlen;
{
int len;
char *line;
if (optlen <= 0) {
"No options");
return;
}
"Options: (%d bytes)", optlen);
while (optlen > 0) {
switch (opt[0]) {
case IPOPT_EOL:
return;
case IPOPT_NOP:
len = 1;
break;
case IPOPT_RR:
len);
break;
case IPOPT_TS:
break;
case IPOPT_SECURITY:
break;
case IPOPT_LSRR:
" - Loose source route (%d bytes)", len);
break;
case IPOPT_SATID:
len);
break;
case IPOPT_SSRR:
" - Strict source route, (%d bytes)", len);
break;
default:
break;
}
if (len <= 0) {
len);
break;
}
}
}
static void
{
char *line;
" Pointer = %d", pointer);
pointer -= IPOPT_MINOFF;
while (len > 0) {
else
if (pointer == 0)
}
}
char *
getproto(p)
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 flags;
struct ip6_rthdr *ipv6ext_rthdr;
{
int i;
struct ip6_rthdr0 *ipv6ext_rthdr0;
char addr[INET6_ADDRSTRLEN];
/* 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.
*/
for (i = 0; i < numaddrs; i++) {
dlc_header, 1),
"address[%d]=%s", i, addr);
}
}
show_space();
}
static void
int flags;
struct ip6_frag *ipv6ext_frag;
{
/* extract the various fields from the fragment header */
(void) sprintf(get_sum_line(),
"IPv6 fragment ID=%d Offset=%-4d MF=%d",
morefrag);
} else { /* F_DTAIL */
show_space();
"Fragment Offset = %d", fragoffset);
"Identification = %d", fragident);
show_space();
}
}
static void
int flags;
struct ip6_hbh *ipv6ext_hbh;
{
/* 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) {
switch (op_type) {
case IP6OPT_PAD1:
dlc_header, 1),
"pad1 option ");
len--;
break;
case IP6OPT_PADN:
dlc_header, 1),
"padN option len = %u", op_len);
break;
case IP6OPT_JUMBO: {
dlc_header, 1),
"Jumbo Payload Option len = %u bytes", op_len);
dlc_header, 1),
"Jumbo Payload Length = %u bytes",
} else {
}
break;
}
case IP6OPT_ROUTER_ALERT: {
"Router Alert Option len = %u bytes", op_len);
"Alert Type = %d (%s)", value,
} else {
}
break;
}
default:
dlc_header, 1),
}
/* check for corrupt length */
dlc_header, 1),
"Incomplete option len = %u, len = %u", op_type,
len);
break;
}
}
show_space();
}
static void
int flags;
struct ip6_dest *ipv6ext_dest;
{
/* 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) {
switch (op_type) {
case IP6OPT_PAD1:
dlc_header, 1),
"pad1 option ");
len--;
break;
case IP6OPT_PADN:
dlc_header, 1),
"padN option len = %u", op_len);
break;
case IP6OPT_TUNNEL_LIMIT:
dlc_header, 1),
"tunnel encapsulation limit len = %d, value = %d",
break;
default:
dlc_header, 1),
}
/* check for corrupt length */
dlc_header, 1),
"Incomplete option len = %u, len = %u", op_type,
len);
break;
}
}
show_space();
}