xref: /freebsd/contrib/tcpdump/print-pktap.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
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 /* \summary: Apple's DLT_PKTAP printer */
23 
24 #include <config.h>
25 
26 #include "netdissect-stdinc.h"
27 
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
30 #include "extract.h"
31 
32 #ifdef DLT_PKTAP
33 
34 /*
35  * XXX - these are little-endian in the captures I've seen, but Apple
36  * no longer make any big-endian machines (Macs use x86, iOS machines
37  * use ARM and run it little-endian), so that might be by definition
38  * or they might be host-endian.
39  *
40  * If a big-endian PKTAP file ever shows up, and it comes from a
41  * big-endian machine, presumably these are host-endian, and we need
42  * to just fetch the fields directly in tcpdump but byte-swap them
43  * to host byte order in libpcap.
44  */
45 typedef struct pktap_header {
46 	nd_uint32_t	pkt_len;	/* length of pktap header */
47 	nd_uint32_t	pkt_rectype;	/* type of record */
48 	nd_uint32_t	pkt_dlt;	/* DLT type of this packet */
49 	char		pkt_ifname[24];	/* interface name */
50 	nd_uint32_t	pkt_flags;
51 	nd_uint32_t	pkt_pfamily;	/* "protocol family" */
52 	nd_uint32_t	pkt_llhdrlen;	/* link-layer header length? */
53 	nd_uint32_t	pkt_lltrlrlen;	/* link-layer trailer length? */
54 	nd_uint32_t	pkt_pid;	/* process ID */
55 	char		pkt_cmdname[20]; /* command name */
56 	nd_uint32_t	pkt_svc_class;	/* "service class" */
57 	nd_uint16_t	pkt_iftype;	/* "interface type" */
58 	nd_uint16_t	pkt_ifunit;	/* unit number of interface? */
59 	nd_uint32_t	pkt_epid;	/* "effective process ID" */
60 	char		pkt_ecmdname[20]; /* "effective command name" */
61 } pktap_header_t;
62 
63 /*
64  * Record types.
65  */
66 #define PKT_REC_NONE	0	/* nothing follows the header */
67 #define PKT_REC_PACKET	1	/* a packet follows the header */
68 
69 static void
pktap_header_print(netdissect_options * ndo,const u_char * bp,u_int length)70 pktap_header_print(netdissect_options *ndo, const u_char *bp, u_int length)
71 {
72 	const pktap_header_t *hdr;
73 	uint32_t dlt, hdrlen;
74 	const char *dltname;
75 
76 	hdr = (const pktap_header_t *)bp;
77 
78 	dlt = GET_LE_U_4(hdr->pkt_dlt);
79 	hdrlen = GET_LE_U_4(hdr->pkt_len);
80 	dltname = pcap_datalink_val_to_name(dlt);
81 	if (!ndo->ndo_qflag) {
82 		ND_PRINT("DLT %s (%u) len %u",
83 			  (dltname != NULL ? dltname : "UNKNOWN"), dlt, hdrlen);
84         } else {
85 		ND_PRINT("%s", (dltname != NULL ? dltname : "UNKNOWN"));
86         }
87 
88 	ND_PRINT(", length %u: ", length);
89 }
90 
91 /*
92  * This is the top level routine of the printer.  'p' points
93  * to the ether header of the packet, 'h->ts' is the timestamp,
94  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
95  * is the number of bytes actually captured.
96  */
97 void
pktap_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)98 pktap_if_print(netdissect_options *ndo,
99                const struct pcap_pkthdr *h, const u_char *p)
100 {
101 	uint32_t dlt, hdrlen, rectype;
102 	u_int caplen = h->caplen;
103 	u_int length = h->len;
104 	if_printer printer;
105 	const pktap_header_t *hdr;
106 	struct pcap_pkthdr nhdr;
107 
108 	ndo->ndo_protocol = "pktap";
109 	if (length < sizeof(pktap_header_t)) {
110 		ND_PRINT(" (packet too short, %u < %zu)",
111 		         length, sizeof(pktap_header_t));
112 		goto invalid;
113 	}
114 	hdr = (const pktap_header_t *)p;
115 	dlt = GET_LE_U_4(hdr->pkt_dlt);
116 	hdrlen = GET_LE_U_4(hdr->pkt_len);
117 	if (hdrlen < sizeof(pktap_header_t)) {
118 		/*
119 		 * Claimed header length < structure length.
120 		 * XXX - does this just mean some fields aren't
121 		 * being supplied, or is it truly an error (i.e.,
122 		 * is the length supplied so that the header can
123 		 * be expanded in the future)?
124 		 */
125 		ND_PRINT(" (pkt_len too small, %u < %zu)",
126 		         hdrlen, sizeof(pktap_header_t));
127 		goto invalid;
128 	}
129 	if (hdrlen > length) {
130 		ND_PRINT(" (pkt_len too big, %u > %u)",
131 		         hdrlen, length);
132 		goto invalid;
133 	}
134 	ND_TCHECK_LEN(p, hdrlen);
135 
136 	if (ndo->ndo_eflag)
137 		pktap_header_print(ndo, p, length);
138 
139 	length -= hdrlen;
140 	caplen -= hdrlen;
141 	p += hdrlen;
142 
143 	rectype = GET_LE_U_4(hdr->pkt_rectype);
144 	switch (rectype) {
145 
146 	case PKT_REC_NONE:
147 		ND_PRINT("no data");
148 		break;
149 
150 	case PKT_REC_PACKET:
151 		printer = lookup_printer(dlt);
152 		if (printer != NULL) {
153 			nhdr = *h;
154 			nhdr.caplen = caplen;
155 			nhdr.len = length;
156 			printer(ndo, &nhdr, p);
157 			hdrlen += ndo->ndo_ll_hdr_len;
158 		} else {
159 			if (!ndo->ndo_eflag)
160 				pktap_header_print(ndo, (const u_char *)hdr,
161 						length + hdrlen);
162 
163 			if (!ndo->ndo_suppress_default_print)
164 				ND_DEFAULTPRINT(p, caplen);
165 		}
166 		break;
167 	}
168 
169 	ndo->ndo_ll_hdr_len += hdrlen;
170 	return;
171 
172 invalid:
173 	nd_print_invalid(ndo);
174 }
175 #endif /* DLT_PKTAP */
176