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