1892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes/*
2892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes *	The Regents of the University of California.  All rights reserved.
4892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes *
5892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * Redistribution and use in source and binary forms, with or without
6892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * modification, are permitted provided that: (1) source code distributions
7892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * retain the above copyright notice and this paragraph in its entirety, (2)
8892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * distributions including binary code include the above copyright notice and
9892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * this paragraph in its entirety in the documentation or other materials
10892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * provided with the distribution, and (3) all advertising materials mentioning
11892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * features or use of this software display the following acknowledgement:
12892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * ``This product includes software developed by the University of California,
13892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * the University nor the names of its contributors may be used to endorse
15892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * or promote products derived from this software without specific prior
16892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * written permission.
17892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes */
21892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
22892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#define NETDISSECT_REWORKED
23892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#ifdef HAVE_CONFIG_H
24892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#include "config.h"
25892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#endif
26892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
27892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#include <tcpdump-stdinc.h>
28892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
29892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#include "interface.h"
30892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#include "extract.h"
31892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
32892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#ifdef DLT_PKTAP
33892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
34892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes/*
35892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * XXX - these are little-endian in the captures I've seen, but Apple
36892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * no longer make any big-endian machines (Macs use x86, iOS machines
37892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * use ARM and run it little-endian), so that might be by definition
38892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * or they might be host-endian.
39892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes *
40892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * If a big-endian PKTAP file ever shows up, and it comes from a
41892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * big-endian machine, presumably these are host-endian, and we need
42892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * to just fetch the fields directly in tcpdump but byte-swap them
43892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * to host byte order in libpcap.
44892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes */
45892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughestypedef struct pktap_header {
46892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_len;	/* length of pktap header */
47892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_rectype;	/* type of record */
48892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_dlt;	/* DLT type of this packet */
49892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	char		pkt_ifname[24];	/* interface name */
50892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_flags;
51892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_pfamily;	/* "protocol family" */
52892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_llhdrlen;	/* link-layer header length? */
53892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_lltrlrlen;	/* link-layer trailer length? */
54892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_pid;	/* process ID */
55892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	char		pkt_cmdname[20]; /* command name */
56892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_svc_class;	/* "service class" */
57892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint16_t	pkt_iftype;	/* "interface type" */
58892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint16_t	pkt_ifunit;	/* unit number of interface? */
59892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t	pkt_epid;	/* "effective process ID" */
60892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	char		pkt_ecmdname[20]; /* "effective command name" */
61892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes} pktap_header_t;
62892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
63892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes/*
64892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * Record types.
65892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes */
66892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#define PKT_REC_NONE	0	/* nothing follows the header */
67892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#define PKT_REC_PACKET	1	/* a packet follows the header */
68892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
69892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughesstatic inline void
70892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughespktap_header_print(netdissect_options *ndo, const u_char *bp, u_int length)
71892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes{
72892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	const pktap_header_t *hdr;
73892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t dlt, hdrlen;
74892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
75892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	hdr = (const pktap_header_t *)bp;
76892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
77892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	dlt = EXTRACT_LE_32BITS(&hdr->pkt_dlt);
78892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	hdrlen = EXTRACT_LE_32BITS(&hdr->pkt_len);
79892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	if (!ndo->ndo_qflag) {
80892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		ND_PRINT((ndo,", DLT %s (%d) len %d",
81892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes			  pcap_datalink_val_to_name(dlt), dlt, hdrlen));
82892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes        } else {
83892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		ND_PRINT((ndo,", %s", pcap_datalink_val_to_name(dlt)));
84892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes        }
85892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
86892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	ND_PRINT((ndo, ", length %u: ", length));
87892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes}
88892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
89892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes/*
90892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * This is the top level routine of the printer.  'p' points
91892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * to the ether header of the packet, 'h->ts' is the timestamp,
92892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * 'h->len' is the length of the packet off the wire, and 'h->caplen'
93892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * is the number of bytes actually captured.
94892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes */
95892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughesu_int
96892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughespktap_if_print(netdissect_options *ndo,
97892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes               const struct pcap_pkthdr *h, const u_char *p)
98892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes{
99892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	uint32_t dlt, hdrlen, rectype;
100892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	u_int caplen = h->caplen;
101892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	u_int length = h->len;
102892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	if_ndo_printer ndo_printer;
103892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes        if_printer printer;
104892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	pktap_header_t *hdr;
105892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
106892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	if (caplen < sizeof(pktap_header_t) || length < sizeof(pktap_header_t)) {
107892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		ND_PRINT((ndo, "[|pktap]"));
108892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		return (0);
109892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	}
110892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	hdr = (pktap_header_t *)p;
111892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	dlt = EXTRACT_LE_32BITS(&hdr->pkt_dlt);
112892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	hdrlen = EXTRACT_LE_32BITS(&hdr->pkt_len);
113892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	if (hdrlen < sizeof(pktap_header_t)) {
114892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		/*
115892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		 * Claimed header length < structure length.
116892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		 * XXX - does this just mean some fields aren't
117892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		 * being supplied, or is it truly an error (i.e.,
118892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		 * is the length supplied so that the header can
119892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		 * be expanded in the future)?
120892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		 */
121892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		ND_PRINT((ndo, "[|pktap]"));
122892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		return (0);
123892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	}
124892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	if (caplen < hdrlen || length < hdrlen) {
125892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		ND_PRINT((ndo, "[|pktap]"));
126892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		return (hdrlen);
127892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	}
128892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
129892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	if (ndo->ndo_eflag)
130892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		pktap_header_print(ndo, p, length);
131892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
132892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	length -= hdrlen;
133892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	caplen -= hdrlen;
134892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	p += hdrlen;
135892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
136892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	rectype = EXTRACT_LE_32BITS(&hdr->pkt_rectype);
137892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	switch (rectype) {
138892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
139892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	case PKT_REC_NONE:
140892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		ND_PRINT((ndo, "no data"));
141892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		break;
142892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
143892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	case PKT_REC_PACKET:
144892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		if ((printer = lookup_printer(dlt)) != NULL) {
145892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes			printer(h, p);
146892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		} else if ((ndo_printer = lookup_ndo_printer(dlt)) != NULL) {
147892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes			ndo_printer(ndo, h, p);
148892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		} else {
149892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes			if (!ndo->ndo_eflag)
150892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes				pktap_header_print(ndo, (u_char *)hdr,
151892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes						length + hdrlen);
152892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
153892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes			if (!ndo->ndo_suppress_default_print)
154892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes				ND_DEFAULTPRINT(p, caplen);
155892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		}
156892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes		break;
157892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	}
158892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
159892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes	return (hdrlen);
160892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes}
161892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
162892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes/*
163892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * Local Variables:
164892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * c-style: whitesmith
165892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * c-basic-offset: 8
166892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes * End:
167892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes */
168892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes
169892a68bdf2f50b40781212e4d7ee7369c8165953Elliott Hughes#endif /* DLT_PKTAP */
170