xref: /dragonfly/contrib/tcpdump/print-ipnet.c (revision ed775ee7)
1 /* \summary: Solaris DLT_IPNET printer */
2 
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 
7 #include "netdissect-stdinc.h"
8 
9 #define ND_LONGJMP_FROM_TCHECK
10 #include "netdissect.h"
11 #include "extract.h"
12 
13 
14 typedef struct ipnet_hdr {
15 	nd_uint8_t	iph_version;
16 	nd_uint8_t	iph_family;
17 	nd_uint16_t	iph_htype;
18 	nd_uint32_t	iph_pktlen;
19 	nd_uint32_t	iph_ifindex;
20 	nd_uint32_t	iph_grifindex;
21 	nd_uint32_t	iph_zsrc;
22 	nd_uint32_t	iph_zdst;
23 } ipnet_hdr_t;
24 
25 #define	IPH_AF_INET	2		/* Matches Solaris's AF_INET */
26 #define	IPH_AF_INET6	26		/* Matches Solaris's AF_INET6 */
27 
28 #ifdef DLT_IPNET
29 
30 static const struct tok ipnet_values[] = {
31 	{ IPH_AF_INET,		"IPv4" },
32 	{ IPH_AF_INET6,		"IPv6" },
33 	{ 0,			NULL }
34 };
35 
36 static void
ipnet_hdr_print(netdissect_options * ndo,const u_char * bp,u_int length)37 ipnet_hdr_print(netdissect_options *ndo, const u_char *bp, u_int length)
38 {
39 	const ipnet_hdr_t *hdr;
40 	hdr = (const ipnet_hdr_t *)bp;
41 
42 	ND_PRINT("%u > %u", GET_BE_U_4(hdr->iph_zsrc),
43 		  GET_BE_U_4(hdr->iph_zdst));
44 
45 	if (!ndo->ndo_qflag) {
46 		ND_PRINT(", family %s (%u)",
47                           tok2str(ipnet_values, "Unknown",
48                                   GET_U_1(hdr->iph_family)),
49                           GET_U_1(hdr->iph_family));
50         } else {
51 		ND_PRINT(", %s",
52                           tok2str(ipnet_values,
53                                   "Unknown Ethertype (0x%04x)",
54 				  GET_U_1(hdr->iph_family)));
55         }
56 
57 	ND_PRINT(", length %u: ", length);
58 }
59 
60 static void
ipnet_print(netdissect_options * ndo,const u_char * p,u_int length,u_int caplen)61 ipnet_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
62 {
63 	const ipnet_hdr_t *hdr;
64 
65 	ND_TCHECK_LEN(p, sizeof(ipnet_hdr_t));
66 	ndo->ndo_ll_hdr_len += sizeof(ipnet_hdr_t);
67 
68 	if (ndo->ndo_eflag)
69 		ipnet_hdr_print(ndo, p, length);
70 
71 	length -= sizeof(ipnet_hdr_t);
72 	caplen -= sizeof(ipnet_hdr_t);
73 	hdr = (const ipnet_hdr_t *)p;
74 	p += sizeof(ipnet_hdr_t);
75 
76 	switch (GET_U_1(hdr->iph_family)) {
77 
78 	case IPH_AF_INET:
79 	        ip_print(ndo, p, length);
80 		break;
81 
82 	case IPH_AF_INET6:
83 		ip6_print(ndo, p, length);
84 		break;
85 
86 	default:
87 		if (!ndo->ndo_eflag)
88 			ipnet_hdr_print(ndo, (const u_char *)hdr,
89 					length + sizeof(ipnet_hdr_t));
90 
91 		if (!ndo->ndo_suppress_default_print)
92 			ND_DEFAULTPRINT(p, caplen);
93 		break;
94 	}
95 }
96 
97 /*
98  * This is the top level routine of the printer.  'p' points
99  * to the ether header of the packet, 'h->ts' is the timestamp,
100  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
101  * is the number of bytes actually captured.
102  */
103 void
ipnet_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)104 ipnet_if_print(netdissect_options *ndo,
105                const struct pcap_pkthdr *h, const u_char *p)
106 {
107 	ndo->ndo_protocol = "ipnet";
108 	ipnet_print(ndo, p, h->len, h->caplen);
109 }
110 #endif /* DLT_IPNET */
111