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