xref: /netbsd-src/external/bsd/tcpdump/dist/print-ppi.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1 /*
2  * Oracle
3  */
4 
5 #include <sys/cdefs.h>
6 #ifndef lint
7 __RCSID("$NetBSD: print-ppi.c,v 1.6 2024/09/02 16:15:32 christos Exp $");
8 #endif
9 
10 /* \summary: Per-Packet Information (DLT_PPI) printer */
11 
12 /* Specification:
13  * Per-Packet Information Header Specification - Version 1.0.7
14  * https://web.archive.org/web/20160328114748/http://www.cacetech.com/documents/PPI%20Header%20format%201.0.7.pdf
15  */
16 
17 #include <config.h>
18 
19 #include "netdissect-stdinc.h"
20 
21 #include "netdissect.h"
22 #include "extract.h"
23 
24 
25 typedef struct ppi_header {
26 	nd_uint8_t	ppi_ver;	/* Version.  Currently 0 */
27 	nd_uint8_t	ppi_flags;	/* Flags. */
28 	nd_uint16_t	ppi_len;	/* Length of entire message, including
29 					 * this header and TLV payload. */
30 	nd_uint32_t	ppi_dlt;	/* Data Link Type of the captured
31 					 * packet data. */
32 } ppi_header_t;
33 
34 #define	PPI_HDRLEN	8
35 
36 #ifdef DLT_PPI
37 
38 static void
39 ppi_header_print(netdissect_options *ndo, const u_char *bp, u_int length)
40 {
41 	const ppi_header_t *hdr;
42 	uint16_t len;
43 	uint32_t dlt;
44 	const char *dltname;
45 
46 	hdr = (const ppi_header_t *)bp;
47 
48 	len = GET_LE_U_2(hdr->ppi_len);
49 	dlt = GET_LE_U_4(hdr->ppi_dlt);
50 	dltname = pcap_datalink_val_to_name(dlt);
51 
52 	if (!ndo->ndo_qflag) {
53 		ND_PRINT("V.%u DLT %s (%u) len %u", GET_U_1(hdr->ppi_ver),
54 			  (dltname != NULL ? dltname : "UNKNOWN"), dlt,
55                           len);
56         } else {
57 		ND_PRINT("%s", (dltname != NULL ? dltname : "UNKNOWN"));
58         }
59 
60 	ND_PRINT(", length %u: ", length);
61 }
62 
63 /*
64  * This is the top level routine of the printer.  'p' points
65  * to the ether header of the packet, 'h->ts' is the timestamp,
66  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
67  * is the number of bytes actually captured.
68  */
69 void
70 ppi_if_print(netdissect_options *ndo,
71 	     const struct pcap_pkthdr *h, const u_char *p)
72 {
73 	if_printer printer;
74 	const ppi_header_t *hdr;
75 	u_int caplen = h->caplen;
76 	u_int length = h->len;
77 	uint16_t len;
78 	uint32_t dlt;
79 	uint32_t hdrlen;
80 	struct pcap_pkthdr nhdr;
81 
82 	ndo->ndo_protocol = "ppi";
83 	if (caplen < sizeof(ppi_header_t)) {
84 		nd_print_trunc(ndo);
85 		ndo->ndo_ll_hdr_len += caplen;
86 		return;
87 	}
88 
89 	hdr = (const ppi_header_t *)p;
90 	len = GET_LE_U_2(hdr->ppi_len);
91 	if (len < sizeof(ppi_header_t) || len > 65532) {
92 		/* It MUST be between 8 and 65,532 inclusive (spec 3.1.3) */
93 		ND_PRINT(" [length %u < %zu or > 65532]", len,
94 			 sizeof(ppi_header_t));
95 		nd_print_invalid(ndo);
96 		ndo->ndo_ll_hdr_len += caplen;
97 		return;
98 	}
99 	if (caplen < len) {
100 		/*
101 		 * If we don't have the entire PPI header, don't
102 		 * bother.
103 		 */
104 		nd_print_trunc(ndo);
105 		ndo->ndo_ll_hdr_len += caplen;
106 		return;
107 	}
108 	dlt = GET_LE_U_4(hdr->ppi_dlt);
109 
110 	if (ndo->ndo_eflag)
111 		ppi_header_print(ndo, p, length);
112 
113 	length -= len;
114 	caplen -= len;
115 	p += len;
116 
117 	printer = lookup_printer(dlt);
118 	if (printer != NULL) {
119 		nhdr = *h;
120 		nhdr.caplen = caplen;
121 		nhdr.len = length;
122 		printer(ndo, &nhdr, p);
123 		hdrlen = ndo->ndo_ll_hdr_len;
124 	} else {
125 		if (!ndo->ndo_eflag)
126 			ppi_header_print(ndo, (const u_char *)hdr, length + len);
127 
128 		if (!ndo->ndo_suppress_default_print)
129 			ND_DEFAULTPRINT(p, caplen);
130 		hdrlen = 0;
131 	}
132 	ndo->ndo_ll_hdr_len += len + hdrlen;
133 }
134 #endif /* DLT_PPI */
135