xref: /freebsd/contrib/tcpdump/print-ipnet.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
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 #include "netdissect.h"
10 #include "extract.h"
11 
12 static const char tstr[] = "[|ipnet]";
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 inline void
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_TCHECK(*hdr);
43 	ND_PRINT((ndo, "%d > %d", EXTRACT_32BITS(hdr->iph_zsrc),
44 		  EXTRACT_32BITS(hdr->iph_zdst)));
45 
46 	if (!ndo->ndo_qflag) {
47 		ND_PRINT((ndo,", family %s (%d)",
48                           tok2str(ipnet_values, "Unknown",
49                                   EXTRACT_8BITS(&hdr->iph_family)),
50                           EXTRACT_8BITS(&hdr->iph_family)));
51         } else {
52 		ND_PRINT((ndo,", %s",
53                           tok2str(ipnet_values,
54                                   "Unknown Ethertype (0x%04x)",
55 				  EXTRACT_8BITS(&hdr->iph_family))));
56         }
57 
58 	ND_PRINT((ndo, ", length %u: ", length));
59 	return;
60 trunc:
61 	ND_PRINT((ndo, " %s", tstr));
62 }
63 
64 static void
65 ipnet_print(netdissect_options *ndo, const u_char *p, u_int length, u_int caplen)
66 {
67 	const ipnet_hdr_t *hdr;
68 
69 	if (caplen < sizeof(ipnet_hdr_t))
70 		goto trunc;
71 
72 	if (ndo->ndo_eflag)
73 		ipnet_hdr_print(ndo, p, length);
74 
75 	length -= sizeof(ipnet_hdr_t);
76 	caplen -= sizeof(ipnet_hdr_t);
77 	hdr = (const ipnet_hdr_t *)p;
78 	p += sizeof(ipnet_hdr_t);
79 
80 	ND_TCHECK2(hdr->iph_family, 1);
81 	switch (EXTRACT_8BITS(&hdr->iph_family)) {
82 
83 	case IPH_AF_INET:
84 	        ip_print(ndo, p, length);
85 		break;
86 
87 	case IPH_AF_INET6:
88 		ip6_print(ndo, p, length);
89 		break;
90 
91 	default:
92 		if (!ndo->ndo_eflag)
93 			ipnet_hdr_print(ndo, (const u_char *)hdr,
94 					length + sizeof(ipnet_hdr_t));
95 
96 		if (!ndo->ndo_suppress_default_print)
97 			ND_DEFAULTPRINT(p, caplen);
98 		break;
99 	}
100 	return;
101 trunc:
102 	ND_PRINT((ndo, " %s", tstr));
103 }
104 
105 /*
106  * This is the top level routine of the printer.  'p' points
107  * to the ether header of the packet, 'h->ts' is the timestamp,
108  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
109  * is the number of bytes actually captured.
110  */
111 u_int
112 ipnet_if_print(netdissect_options *ndo,
113                const struct pcap_pkthdr *h, const u_char *p)
114 {
115 	ipnet_print(ndo, p, h->len, h->caplen);
116 
117 	return (sizeof(ipnet_hdr_t));
118 }
119 
120 /*
121  * Local Variables:
122  * c-style: whitesmith
123  * c-basic-offset: 8
124  * End:
125  */
126 
127 #endif /* DLT_IPNET */
128