xref: /dflybsd-src/contrib/tcpdump/print-ipnet.c (revision 59c07fbdf8168fa08c76c515186d561b5a92690c)
1411677aeSAaron LI /* \summary: Solaris DLT_IPNET printer */
2411677aeSAaron LI 
327bfbee1SPeter Avalos #ifdef HAVE_CONFIG_H
4*ed775ee7SAntonio Huete Jimenez #include <config.h>
527bfbee1SPeter Avalos #endif
627bfbee1SPeter Avalos 
7*ed775ee7SAntonio Huete Jimenez #include "netdissect-stdinc.h"
827bfbee1SPeter Avalos 
9*ed775ee7SAntonio Huete Jimenez #define ND_LONGJMP_FROM_TCHECK
1027bfbee1SPeter Avalos #include "netdissect.h"
11411677aeSAaron LI #include "extract.h"
12411677aeSAaron LI 
13411677aeSAaron LI 
14411677aeSAaron LI typedef struct ipnet_hdr {
15411677aeSAaron LI 	nd_uint8_t	iph_version;
16411677aeSAaron LI 	nd_uint8_t	iph_family;
17411677aeSAaron LI 	nd_uint16_t	iph_htype;
18411677aeSAaron LI 	nd_uint32_t	iph_pktlen;
19411677aeSAaron LI 	nd_uint32_t	iph_ifindex;
20411677aeSAaron LI 	nd_uint32_t	iph_grifindex;
21411677aeSAaron LI 	nd_uint32_t	iph_zsrc;
22411677aeSAaron LI 	nd_uint32_t	iph_zdst;
23411677aeSAaron LI } ipnet_hdr_t;
24411677aeSAaron LI 
25411677aeSAaron LI #define	IPH_AF_INET	2		/* Matches Solaris's AF_INET */
26411677aeSAaron LI #define	IPH_AF_INET6	26		/* Matches Solaris's AF_INET6 */
2727bfbee1SPeter Avalos 
2827bfbee1SPeter Avalos #ifdef DLT_IPNET
2927bfbee1SPeter Avalos 
30411677aeSAaron LI static const struct tok ipnet_values[] = {
3127bfbee1SPeter Avalos 	{ IPH_AF_INET,		"IPv4" },
3227bfbee1SPeter Avalos 	{ IPH_AF_INET6,		"IPv6" },
3327bfbee1SPeter Avalos 	{ 0,			NULL }
3427bfbee1SPeter Avalos };
3527bfbee1SPeter Avalos 
36*ed775ee7SAntonio Huete Jimenez static void
ipnet_hdr_print(netdissect_options * ndo,const u_char * bp,u_int length)37411677aeSAaron LI ipnet_hdr_print(netdissect_options *ndo, const u_char *bp, u_int length)
3827bfbee1SPeter Avalos {
3927bfbee1SPeter Avalos 	const ipnet_hdr_t *hdr;
4027bfbee1SPeter Avalos 	hdr = (const ipnet_hdr_t *)bp;
4127bfbee1SPeter Avalos 
42*ed775ee7SAntonio Huete Jimenez 	ND_PRINT("%u > %u", GET_BE_U_4(hdr->iph_zsrc),
43*ed775ee7SAntonio Huete Jimenez 		  GET_BE_U_4(hdr->iph_zdst));
4427bfbee1SPeter Avalos 
4527bfbee1SPeter Avalos 	if (!ndo->ndo_qflag) {
46*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", family %s (%u)",
4727bfbee1SPeter Avalos                           tok2str(ipnet_values, "Unknown",
48*ed775ee7SAntonio Huete Jimenez                                   GET_U_1(hdr->iph_family)),
49*ed775ee7SAntonio Huete Jimenez                           GET_U_1(hdr->iph_family));
5027bfbee1SPeter Avalos         } else {
51*ed775ee7SAntonio Huete Jimenez 		ND_PRINT(", %s",
5227bfbee1SPeter Avalos                           tok2str(ipnet_values,
5327bfbee1SPeter Avalos                                   "Unknown Ethertype (0x%04x)",
54*ed775ee7SAntonio Huete Jimenez 				  GET_U_1(hdr->iph_family)));
5527bfbee1SPeter Avalos         }
5627bfbee1SPeter Avalos 
57*ed775ee7SAntonio Huete Jimenez 	ND_PRINT(", length %u: ", length);
5827bfbee1SPeter Avalos }
5927bfbee1SPeter Avalos 
6027bfbee1SPeter Avalos static void
ipnet_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen)61411677aeSAaron LI ipnet_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
6227bfbee1SPeter Avalos {
63411677aeSAaron LI 	const ipnet_hdr_t *hdr;
6427bfbee1SPeter Avalos 
65*ed775ee7SAntonio Huete Jimenez 	ND_TCHECK_LEN(p, sizeof(ipnet_hdr_t));
66*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_ll_hdr_len += sizeof(ipnet_hdr_t);
6727bfbee1SPeter Avalos 
6827bfbee1SPeter Avalos 	if (ndo->ndo_eflag)
6927bfbee1SPeter Avalos 		ipnet_hdr_print(ndo, p, length);
7027bfbee1SPeter Avalos 
7127bfbee1SPeter Avalos 	length -= sizeof(ipnet_hdr_t);
7227bfbee1SPeter Avalos 	caplen -= sizeof(ipnet_hdr_t);
73411677aeSAaron LI 	hdr = (const ipnet_hdr_t *)p;
7427bfbee1SPeter Avalos 	p += sizeof(ipnet_hdr_t);
7527bfbee1SPeter Avalos 
76*ed775ee7SAntonio Huete Jimenez 	switch (GET_U_1(hdr->iph_family)) {
7727bfbee1SPeter Avalos 
7827bfbee1SPeter Avalos 	case IPH_AF_INET:
7927bfbee1SPeter Avalos 	        ip_print(ndo, p, length);
8027bfbee1SPeter Avalos 		break;
8127bfbee1SPeter Avalos 
8227bfbee1SPeter Avalos 	case IPH_AF_INET6:
8327bfbee1SPeter Avalos 		ip6_print(ndo, p, length);
8427bfbee1SPeter Avalos 		break;
8527bfbee1SPeter Avalos 
8627bfbee1SPeter Avalos 	default:
8727bfbee1SPeter Avalos 		if (!ndo->ndo_eflag)
88411677aeSAaron LI 			ipnet_hdr_print(ndo, (const u_char *)hdr,
8927bfbee1SPeter Avalos 					length + sizeof(ipnet_hdr_t));
9027bfbee1SPeter Avalos 
9127bfbee1SPeter Avalos 		if (!ndo->ndo_suppress_default_print)
92411677aeSAaron LI 			ND_DEFAULTPRINT(p, caplen);
9327bfbee1SPeter Avalos 		break;
9427bfbee1SPeter Avalos 	}
9527bfbee1SPeter Avalos }
9627bfbee1SPeter Avalos 
9727bfbee1SPeter Avalos /*
9827bfbee1SPeter Avalos  * This is the top level routine of the printer.  'p' points
9927bfbee1SPeter Avalos  * to the ether header of the packet, 'h->ts' is the timestamp,
10027bfbee1SPeter Avalos  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
10127bfbee1SPeter Avalos  * is the number of bytes actually captured.
10227bfbee1SPeter Avalos  */
103*ed775ee7SAntonio Huete Jimenez void
ipnet_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)104411677aeSAaron LI ipnet_if_print(netdissect_options *ndo,
10527bfbee1SPeter Avalos                const struct pcap_pkthdr *h, const u_char *p)
10627bfbee1SPeter Avalos {
107*ed775ee7SAntonio Huete Jimenez 	ndo->ndo_protocol = "ipnet";
10827bfbee1SPeter Avalos 	ipnet_print(ndo, p, h->len, h->caplen);
10927bfbee1SPeter Avalos }
11027bfbee1SPeter Avalos #endif /* DLT_IPNET */
111