xref: /aosp_15_r20/external/tcpdump/print-pktap.c (revision 05b00f6010a2396e3db2409989fc67270046269f)
1*05b00f60SXin Li /*
2*05b00f60SXin Li  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3*05b00f60SXin Li  *	The Regents of the University of California.  All rights reserved.
4*05b00f60SXin Li  *
5*05b00f60SXin Li  * Redistribution and use in source and binary forms, with or without
6*05b00f60SXin Li  * modification, are permitted provided that: (1) source code distributions
7*05b00f60SXin Li  * retain the above copyright notice and this paragraph in its entirety, (2)
8*05b00f60SXin Li  * distributions including binary code include the above copyright notice and
9*05b00f60SXin Li  * this paragraph in its entirety in the documentation or other materials
10*05b00f60SXin Li  * provided with the distribution, and (3) all advertising materials mentioning
11*05b00f60SXin Li  * features or use of this software display the following acknowledgement:
12*05b00f60SXin Li  * ``This product includes software developed by the University of California,
13*05b00f60SXin Li  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*05b00f60SXin Li  * the University nor the names of its contributors may be used to endorse
15*05b00f60SXin Li  * or promote products derived from this software without specific prior
16*05b00f60SXin Li  * written permission.
17*05b00f60SXin Li  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*05b00f60SXin Li  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*05b00f60SXin Li  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*05b00f60SXin Li  */
21*05b00f60SXin Li 
22*05b00f60SXin Li /* \summary: Apple's DLT_PKTAP printer */
23*05b00f60SXin Li 
24*05b00f60SXin Li #ifdef HAVE_CONFIG_H
25*05b00f60SXin Li #include <config.h>
26*05b00f60SXin Li #endif
27*05b00f60SXin Li 
28*05b00f60SXin Li #include "netdissect-stdinc.h"
29*05b00f60SXin Li 
30*05b00f60SXin Li #define ND_LONGJMP_FROM_TCHECK
31*05b00f60SXin Li #include "netdissect.h"
32*05b00f60SXin Li #include "extract.h"
33*05b00f60SXin Li 
34*05b00f60SXin Li #ifdef DLT_PKTAP
35*05b00f60SXin Li 
36*05b00f60SXin Li /*
37*05b00f60SXin Li  * XXX - these are little-endian in the captures I've seen, but Apple
38*05b00f60SXin Li  * no longer make any big-endian machines (Macs use x86, iOS machines
39*05b00f60SXin Li  * use ARM and run it little-endian), so that might be by definition
40*05b00f60SXin Li  * or they might be host-endian.
41*05b00f60SXin Li  *
42*05b00f60SXin Li  * If a big-endian PKTAP file ever shows up, and it comes from a
43*05b00f60SXin Li  * big-endian machine, presumably these are host-endian, and we need
44*05b00f60SXin Li  * to just fetch the fields directly in tcpdump but byte-swap them
45*05b00f60SXin Li  * to host byte order in libpcap.
46*05b00f60SXin Li  */
47*05b00f60SXin Li typedef struct pktap_header {
48*05b00f60SXin Li 	nd_uint32_t	pkt_len;	/* length of pktap header */
49*05b00f60SXin Li 	nd_uint32_t	pkt_rectype;	/* type of record */
50*05b00f60SXin Li 	nd_uint32_t	pkt_dlt;	/* DLT type of this packet */
51*05b00f60SXin Li 	char		pkt_ifname[24];	/* interface name */
52*05b00f60SXin Li 	nd_uint32_t	pkt_flags;
53*05b00f60SXin Li 	nd_uint32_t	pkt_pfamily;	/* "protocol family" */
54*05b00f60SXin Li 	nd_uint32_t	pkt_llhdrlen;	/* link-layer header length? */
55*05b00f60SXin Li 	nd_uint32_t	pkt_lltrlrlen;	/* link-layer trailer length? */
56*05b00f60SXin Li 	nd_uint32_t	pkt_pid;	/* process ID */
57*05b00f60SXin Li 	char		pkt_cmdname[20]; /* command name */
58*05b00f60SXin Li 	nd_uint32_t	pkt_svc_class;	/* "service class" */
59*05b00f60SXin Li 	nd_uint16_t	pkt_iftype;	/* "interface type" */
60*05b00f60SXin Li 	nd_uint16_t	pkt_ifunit;	/* unit number of interface? */
61*05b00f60SXin Li 	nd_uint32_t	pkt_epid;	/* "effective process ID" */
62*05b00f60SXin Li 	char		pkt_ecmdname[20]; /* "effective command name" */
63*05b00f60SXin Li } pktap_header_t;
64*05b00f60SXin Li 
65*05b00f60SXin Li /*
66*05b00f60SXin Li  * Record types.
67*05b00f60SXin Li  */
68*05b00f60SXin Li #define PKT_REC_NONE	0	/* nothing follows the header */
69*05b00f60SXin Li #define PKT_REC_PACKET	1	/* a packet follows the header */
70*05b00f60SXin Li 
71*05b00f60SXin Li static void
pktap_header_print(netdissect_options * ndo,const u_char * bp,u_int length)72*05b00f60SXin Li pktap_header_print(netdissect_options *ndo, const u_char *bp, u_int length)
73*05b00f60SXin Li {
74*05b00f60SXin Li 	const pktap_header_t *hdr;
75*05b00f60SXin Li 	uint32_t dlt, hdrlen;
76*05b00f60SXin Li 	const char *dltname;
77*05b00f60SXin Li 
78*05b00f60SXin Li 	hdr = (const pktap_header_t *)bp;
79*05b00f60SXin Li 
80*05b00f60SXin Li 	dlt = GET_LE_U_4(hdr->pkt_dlt);
81*05b00f60SXin Li 	hdrlen = GET_LE_U_4(hdr->pkt_len);
82*05b00f60SXin Li 	dltname = pcap_datalink_val_to_name(dlt);
83*05b00f60SXin Li 	if (!ndo->ndo_qflag) {
84*05b00f60SXin Li 		ND_PRINT("DLT %s (%u) len %u",
85*05b00f60SXin Li 			  (dltname != NULL ? dltname : "UNKNOWN"), dlt, hdrlen);
86*05b00f60SXin Li         } else {
87*05b00f60SXin Li 		ND_PRINT("%s", (dltname != NULL ? dltname : "UNKNOWN"));
88*05b00f60SXin Li         }
89*05b00f60SXin Li 
90*05b00f60SXin Li 	ND_PRINT(", length %u: ", length);
91*05b00f60SXin Li }
92*05b00f60SXin Li 
93*05b00f60SXin Li /*
94*05b00f60SXin Li  * This is the top level routine of the printer.  'p' points
95*05b00f60SXin Li  * to the ether header of the packet, 'h->ts' is the timestamp,
96*05b00f60SXin Li  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
97*05b00f60SXin Li  * is the number of bytes actually captured.
98*05b00f60SXin Li  */
99*05b00f60SXin Li void
pktap_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)100*05b00f60SXin Li pktap_if_print(netdissect_options *ndo,
101*05b00f60SXin Li                const struct pcap_pkthdr *h, const u_char *p)
102*05b00f60SXin Li {
103*05b00f60SXin Li 	uint32_t dlt, hdrlen, rectype;
104*05b00f60SXin Li 	u_int caplen = h->caplen;
105*05b00f60SXin Li 	u_int length = h->len;
106*05b00f60SXin Li 	if_printer printer;
107*05b00f60SXin Li 	const pktap_header_t *hdr;
108*05b00f60SXin Li 	struct pcap_pkthdr nhdr;
109*05b00f60SXin Li 
110*05b00f60SXin Li 	ndo->ndo_protocol = "pktap";
111*05b00f60SXin Li 	if (length < sizeof(pktap_header_t)) {
112*05b00f60SXin Li 		ND_PRINT(" (packet too short, %u < %zu)",
113*05b00f60SXin Li 		         length, sizeof(pktap_header_t));
114*05b00f60SXin Li 		goto invalid;
115*05b00f60SXin Li 	}
116*05b00f60SXin Li 	hdr = (const pktap_header_t *)p;
117*05b00f60SXin Li 	dlt = GET_LE_U_4(hdr->pkt_dlt);
118*05b00f60SXin Li 	hdrlen = GET_LE_U_4(hdr->pkt_len);
119*05b00f60SXin Li 	if (hdrlen < sizeof(pktap_header_t)) {
120*05b00f60SXin Li 		/*
121*05b00f60SXin Li 		 * Claimed header length < structure length.
122*05b00f60SXin Li 		 * XXX - does this just mean some fields aren't
123*05b00f60SXin Li 		 * being supplied, or is it truly an error (i.e.,
124*05b00f60SXin Li 		 * is the length supplied so that the header can
125*05b00f60SXin Li 		 * be expanded in the future)?
126*05b00f60SXin Li 		 */
127*05b00f60SXin Li 		ND_PRINT(" (pkt_len too small, %u < %zu)",
128*05b00f60SXin Li 		         hdrlen, sizeof(pktap_header_t));
129*05b00f60SXin Li 		goto invalid;
130*05b00f60SXin Li 	}
131*05b00f60SXin Li 	if (hdrlen > length) {
132*05b00f60SXin Li 		ND_PRINT(" (pkt_len too big, %u > %u)",
133*05b00f60SXin Li 		         hdrlen, length);
134*05b00f60SXin Li 		goto invalid;
135*05b00f60SXin Li 	}
136*05b00f60SXin Li 	ND_TCHECK_LEN(p, hdrlen);
137*05b00f60SXin Li 
138*05b00f60SXin Li 	if (ndo->ndo_eflag)
139*05b00f60SXin Li 		pktap_header_print(ndo, p, length);
140*05b00f60SXin Li 
141*05b00f60SXin Li 	length -= hdrlen;
142*05b00f60SXin Li 	caplen -= hdrlen;
143*05b00f60SXin Li 	p += hdrlen;
144*05b00f60SXin Li 
145*05b00f60SXin Li 	rectype = GET_LE_U_4(hdr->pkt_rectype);
146*05b00f60SXin Li 	switch (rectype) {
147*05b00f60SXin Li 
148*05b00f60SXin Li 	case PKT_REC_NONE:
149*05b00f60SXin Li 		ND_PRINT("no data");
150*05b00f60SXin Li 		break;
151*05b00f60SXin Li 
152*05b00f60SXin Li 	case PKT_REC_PACKET:
153*05b00f60SXin Li 		printer = lookup_printer(dlt);
154*05b00f60SXin Li 		if (printer != NULL) {
155*05b00f60SXin Li 			nhdr = *h;
156*05b00f60SXin Li 			nhdr.caplen = caplen;
157*05b00f60SXin Li 			nhdr.len = length;
158*05b00f60SXin Li 			printer(ndo, &nhdr, p);
159*05b00f60SXin Li 			hdrlen += ndo->ndo_ll_hdr_len;
160*05b00f60SXin Li 		} else {
161*05b00f60SXin Li 			if (!ndo->ndo_eflag)
162*05b00f60SXin Li 				pktap_header_print(ndo, (const u_char *)hdr,
163*05b00f60SXin Li 						length + hdrlen);
164*05b00f60SXin Li 
165*05b00f60SXin Li 			if (!ndo->ndo_suppress_default_print)
166*05b00f60SXin Li 				ND_DEFAULTPRINT(p, caplen);
167*05b00f60SXin Li 		}
168*05b00f60SXin Li 		break;
169*05b00f60SXin Li 	}
170*05b00f60SXin Li 
171*05b00f60SXin Li 	ndo->ndo_ll_hdr_len += hdrlen;
172*05b00f60SXin Li 	return;
173*05b00f60SXin Li 
174*05b00f60SXin Li invalid:
175*05b00f60SXin Li 	nd_print_invalid(ndo);
176*05b00f60SXin Li }
177*05b00f60SXin Li #endif /* DLT_PKTAP */
178