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