xref: /freebsd/contrib/tcpdump/print-ipnet.c (revision ee67461e56828dd1f8de165947ba83f6d9148a87)
13340d773SGleb Smirnoff /* \summary: Solaris DLT_IPNET printer */
23340d773SGleb Smirnoff 
327df3f5dSRui Paulo #ifdef HAVE_CONFIG_H
4*ee67461eSJoseph Mingrone #include <config.h>
527df3f5dSRui Paulo #endif
627df3f5dSRui Paulo 
7*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
827df3f5dSRui Paulo 
9*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
103340d773SGleb Smirnoff #include "netdissect.h"
1139e421e8SCy Schubert #include "extract.h"
1239e421e8SCy Schubert 
133c602fabSXin LI 
143c602fabSXin LI typedef struct ipnet_hdr {
1539e421e8SCy Schubert 	nd_uint8_t	iph_version;
1639e421e8SCy Schubert 	nd_uint8_t	iph_family;
1739e421e8SCy Schubert 	nd_uint16_t	iph_htype;
1839e421e8SCy Schubert 	nd_uint32_t	iph_pktlen;
1939e421e8SCy Schubert 	nd_uint32_t	iph_ifindex;
2039e421e8SCy Schubert 	nd_uint32_t	iph_grifindex;
2139e421e8SCy Schubert 	nd_uint32_t	iph_zsrc;
2239e421e8SCy Schubert 	nd_uint32_t	iph_zdst;
233c602fabSXin LI } ipnet_hdr_t;
243c602fabSXin LI 
253c602fabSXin LI #define	IPH_AF_INET	2		/* Matches Solaris's AF_INET */
263c602fabSXin LI #define	IPH_AF_INET6	26		/* Matches Solaris's AF_INET6 */
2727df3f5dSRui Paulo 
2827df3f5dSRui Paulo #ifdef DLT_IPNET
2927df3f5dSRui Paulo 
303c602fabSXin LI static const struct tok ipnet_values[] = {
3127df3f5dSRui Paulo 	{ IPH_AF_INET,		"IPv4" },
3227df3f5dSRui Paulo 	{ IPH_AF_INET6,		"IPv6" },
3327df3f5dSRui Paulo 	{ 0,			NULL }
3427df3f5dSRui Paulo };
3527df3f5dSRui Paulo 
36*ee67461eSJoseph Mingrone static void
373c602fabSXin LI ipnet_hdr_print(netdissect_options *ndo, const u_char *bp, u_int length)
3827df3f5dSRui Paulo {
3927df3f5dSRui Paulo 	const ipnet_hdr_t *hdr;
4027df3f5dSRui Paulo 	hdr = (const ipnet_hdr_t *)bp;
4127df3f5dSRui Paulo 
42*ee67461eSJoseph Mingrone 	ND_PRINT("%u > %u", GET_BE_U_4(hdr->iph_zsrc),
43*ee67461eSJoseph Mingrone 		  GET_BE_U_4(hdr->iph_zdst));
4427df3f5dSRui Paulo 
4527df3f5dSRui Paulo 	if (!ndo->ndo_qflag) {
46*ee67461eSJoseph Mingrone 		ND_PRINT(", family %s (%u)",
4727df3f5dSRui Paulo                           tok2str(ipnet_values, "Unknown",
48*ee67461eSJoseph Mingrone                                   GET_U_1(hdr->iph_family)),
49*ee67461eSJoseph Mingrone                           GET_U_1(hdr->iph_family));
5027df3f5dSRui Paulo         } else {
51*ee67461eSJoseph Mingrone 		ND_PRINT(", %s",
5227df3f5dSRui Paulo                           tok2str(ipnet_values,
5327df3f5dSRui Paulo                                   "Unknown Ethertype (0x%04x)",
54*ee67461eSJoseph Mingrone 				  GET_U_1(hdr->iph_family)));
5527df3f5dSRui Paulo         }
5627df3f5dSRui Paulo 
57*ee67461eSJoseph Mingrone 	ND_PRINT(", length %u: ", length);
5827df3f5dSRui Paulo }
5927df3f5dSRui Paulo 
6027df3f5dSRui Paulo static void
613c602fabSXin LI ipnet_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
6227df3f5dSRui Paulo {
633340d773SGleb Smirnoff 	const ipnet_hdr_t *hdr;
6427df3f5dSRui Paulo 
65*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, sizeof(ipnet_hdr_t));
66*ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += sizeof(ipnet_hdr_t);
6727df3f5dSRui Paulo 
6827df3f5dSRui Paulo 	if (ndo->ndo_eflag)
6927df3f5dSRui Paulo 		ipnet_hdr_print(ndo, p, length);
7027df3f5dSRui Paulo 
7127df3f5dSRui Paulo 	length -= sizeof(ipnet_hdr_t);
7227df3f5dSRui Paulo 	caplen -= sizeof(ipnet_hdr_t);
733340d773SGleb Smirnoff 	hdr = (const ipnet_hdr_t *)p;
7427df3f5dSRui Paulo 	p += sizeof(ipnet_hdr_t);
7527df3f5dSRui Paulo 
76*ee67461eSJoseph Mingrone 	switch (GET_U_1(hdr->iph_family)) {
7727df3f5dSRui Paulo 
7827df3f5dSRui Paulo 	case IPH_AF_INET:
7927df3f5dSRui Paulo 	        ip_print(ndo, p, length);
8027df3f5dSRui Paulo 		break;
8127df3f5dSRui Paulo 
8227df3f5dSRui Paulo 	case IPH_AF_INET6:
83cac3dcd5SXin LI 		ip6_print(ndo, p, length);
8427df3f5dSRui Paulo 		break;
8527df3f5dSRui Paulo 
8627df3f5dSRui Paulo 	default:
8727df3f5dSRui Paulo 		if (!ndo->ndo_eflag)
883340d773SGleb Smirnoff 			ipnet_hdr_print(ndo, (const u_char *)hdr,
8927df3f5dSRui Paulo 					length + sizeof(ipnet_hdr_t));
9027df3f5dSRui Paulo 
9127df3f5dSRui Paulo 		if (!ndo->ndo_suppress_default_print)
923c602fabSXin LI 			ND_DEFAULTPRINT(p, caplen);
9327df3f5dSRui Paulo 		break;
9427df3f5dSRui Paulo 	}
9527df3f5dSRui Paulo }
9627df3f5dSRui Paulo 
9727df3f5dSRui Paulo /*
9827df3f5dSRui Paulo  * This is the top level routine of the printer.  'p' points
9927df3f5dSRui Paulo  * to the ether header of the packet, 'h->ts' is the timestamp,
10027df3f5dSRui Paulo  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
10127df3f5dSRui Paulo  * is the number of bytes actually captured.
10227df3f5dSRui Paulo  */
103*ee67461eSJoseph Mingrone void
1043c602fabSXin LI ipnet_if_print(netdissect_options *ndo,
10527df3f5dSRui Paulo                const struct pcap_pkthdr *h, const u_char *p)
10627df3f5dSRui Paulo {
107*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "ipnet";
10827df3f5dSRui Paulo 	ipnet_print(ndo, p, h->len, h->caplen);
10927df3f5dSRui Paulo }
11027df3f5dSRui Paulo #endif /* DLT_IPNET */
111