xref: /netbsd-src/external/bsd/tcpdump/dist/print-ipnet.c (revision cc576e1d8e4f4078fd4e81238abca9fca216f6ec)
1 
2 #include <sys/cdefs.h>
3 #ifndef lint
4 __RCSID("$NetBSD: print-ipnet.c,v 1.2 2017/01/24 23:29:14 christos Exp $");
5 #endif
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include <netdissect-stdinc.h>
12 
13 #include "netdissect.h"
14 
15 typedef struct ipnet_hdr {
16 	uint8_t		iph_version;
17 	uint8_t		iph_family;
18 	uint16_t	iph_htype;
19 	uint32_t	iph_pktlen;
20 	uint32_t	iph_ifindex;
21 	uint32_t	iph_grifindex;
22 	uint32_t	iph_zsrc;
23 	uint32_t	iph_zdst;
24 } ipnet_hdr_t;
25 
26 #define	IPH_AF_INET	2		/* Matches Solaris's AF_INET */
27 #define	IPH_AF_INET6	26		/* Matches Solaris's AF_INET6 */
28 
29 #ifdef DLT_IPNET
30 
31 static const struct tok ipnet_values[] = {
32 	{ IPH_AF_INET,		"IPv4" },
33 	{ IPH_AF_INET6,		"IPv6" },
34 	{ 0,			NULL }
35 };
36 
37 static inline void
38 ipnet_hdr_print(netdissect_options *ndo, const u_char *bp, u_int length)
39 {
40 	const ipnet_hdr_t *hdr;
41 	hdr = (const ipnet_hdr_t *)bp;
42 
43 	ND_PRINT((ndo, "%d > %d", hdr->iph_zsrc, hdr->iph_zdst));
44 
45 	if (!ndo->ndo_qflag) {
46 		ND_PRINT((ndo,", family %s (%d)",
47                           tok2str(ipnet_values, "Unknown",
48                                   hdr->iph_family),
49                           hdr->iph_family));
50         } else {
51 		ND_PRINT((ndo,", %s",
52                           tok2str(ipnet_values,
53                                   "Unknown Ethertype (0x%04x)",
54                                   hdr->iph_family)));
55         }
56 
57 	ND_PRINT((ndo, ", length %u: ", length));
58 }
59 
60 static void
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 	if (caplen < sizeof(ipnet_hdr_t)) {
66 		ND_PRINT((ndo, "[|ipnet]"));
67 		return;
68 	}
69 
70 	if (ndo->ndo_eflag)
71 		ipnet_hdr_print(ndo, p, length);
72 
73 	length -= sizeof(ipnet_hdr_t);
74 	caplen -= sizeof(ipnet_hdr_t);
75 	hdr = (const ipnet_hdr_t *)p;
76 	p += sizeof(ipnet_hdr_t);
77 
78 	switch (hdr->iph_family) {
79 
80 	case IPH_AF_INET:
81 	        ip_print(ndo, p, length);
82 		break;
83 
84 	case IPH_AF_INET6:
85 		ip6_print(ndo, p, length);
86 		break;
87 
88 	default:
89 		if (!ndo->ndo_eflag)
90 			ipnet_hdr_print(ndo, (const u_char *)hdr,
91 					length + sizeof(ipnet_hdr_t));
92 
93 		if (!ndo->ndo_suppress_default_print)
94 			ND_DEFAULTPRINT(p, caplen);
95 		break;
96 	}
97 }
98 
99 /*
100  * This is the top level routine of the printer.  'p' points
101  * to the ether header of the packet, 'h->ts' is the timestamp,
102  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
103  * is the number of bytes actually captured.
104  */
105 u_int
106 ipnet_if_print(netdissect_options *ndo,
107                const struct pcap_pkthdr *h, const u_char *p)
108 {
109 	ipnet_print(ndo, p, h->len, h->caplen);
110 
111 	return (sizeof(ipnet_hdr_t));
112 }
113 
114 /*
115  * Local Variables:
116  * c-style: whitesmith
117  * c-basic-offset: 8
118  * End:
119  */
120 
121 #endif /* DLT_IPNET */
122