xref: /freebsd/contrib/tcpdump/print-pktap.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
13c602fabSXin LI /*
23c602fabSXin LI  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
33c602fabSXin LI  *	The Regents of the University of California.  All rights reserved.
43c602fabSXin LI  *
53c602fabSXin LI  * Redistribution and use in source and binary forms, with or without
63c602fabSXin LI  * modification, are permitted provided that: (1) source code distributions
73c602fabSXin LI  * retain the above copyright notice and this paragraph in its entirety, (2)
83c602fabSXin LI  * distributions including binary code include the above copyright notice and
93c602fabSXin LI  * this paragraph in its entirety in the documentation or other materials
103c602fabSXin LI  * provided with the distribution, and (3) all advertising materials mentioning
113c602fabSXin LI  * features or use of this software display the following acknowledgement:
123c602fabSXin LI  * ``This product includes software developed by the University of California,
133c602fabSXin LI  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
143c602fabSXin LI  * the University nor the names of its contributors may be used to endorse
153c602fabSXin LI  * or promote products derived from this software without specific prior
163c602fabSXin LI  * written permission.
173c602fabSXin LI  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
183c602fabSXin LI  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
193c602fabSXin LI  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
203c602fabSXin LI  */
213c602fabSXin LI 
223340d773SGleb Smirnoff /* \summary: Apple's DLT_PKTAP printer */
233340d773SGleb Smirnoff 
24*ee67461eSJoseph Mingrone #include <config.h>
253c602fabSXin LI 
26*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
273c602fabSXin LI 
28*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK
293340d773SGleb Smirnoff #include "netdissect.h"
303c602fabSXin LI #include "extract.h"
313c602fabSXin LI 
323c602fabSXin LI #ifdef DLT_PKTAP
333c602fabSXin LI 
343c602fabSXin LI /*
353c602fabSXin LI  * XXX - these are little-endian in the captures I've seen, but Apple
363c602fabSXin LI  * no longer make any big-endian machines (Macs use x86, iOS machines
373c602fabSXin LI  * use ARM and run it little-endian), so that might be by definition
383c602fabSXin LI  * or they might be host-endian.
393c602fabSXin LI  *
403c602fabSXin LI  * If a big-endian PKTAP file ever shows up, and it comes from a
413c602fabSXin LI  * big-endian machine, presumably these are host-endian, and we need
423c602fabSXin LI  * to just fetch the fields directly in tcpdump but byte-swap them
433c602fabSXin LI  * to host byte order in libpcap.
443c602fabSXin LI  */
453c602fabSXin LI typedef struct pktap_header {
46*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_len;	/* length of pktap header */
47*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_rectype;	/* type of record */
48*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_dlt;	/* DLT type of this packet */
493c602fabSXin LI 	char		pkt_ifname[24];	/* interface name */
50*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_flags;
51*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_pfamily;	/* "protocol family" */
52*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_llhdrlen;	/* link-layer header length? */
53*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_lltrlrlen;	/* link-layer trailer length? */
54*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_pid;	/* process ID */
553c602fabSXin LI 	char		pkt_cmdname[20]; /* command name */
56*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_svc_class;	/* "service class" */
57*ee67461eSJoseph Mingrone 	nd_uint16_t	pkt_iftype;	/* "interface type" */
58*ee67461eSJoseph Mingrone 	nd_uint16_t	pkt_ifunit;	/* unit number of interface? */
59*ee67461eSJoseph Mingrone 	nd_uint32_t	pkt_epid;	/* "effective process ID" */
603c602fabSXin LI 	char		pkt_ecmdname[20]; /* "effective command name" */
613c602fabSXin LI } pktap_header_t;
623c602fabSXin LI 
633c602fabSXin LI /*
643c602fabSXin LI  * Record types.
653c602fabSXin LI  */
663c602fabSXin LI #define PKT_REC_NONE	0	/* nothing follows the header */
673c602fabSXin LI #define PKT_REC_PACKET	1	/* a packet follows the header */
683c602fabSXin LI 
69*ee67461eSJoseph Mingrone static void
pktap_header_print(netdissect_options * ndo,const u_char * bp,u_int length)703c602fabSXin LI pktap_header_print(netdissect_options *ndo, const u_char *bp, u_int length)
713c602fabSXin LI {
723c602fabSXin LI 	const pktap_header_t *hdr;
733c602fabSXin LI 	uint32_t dlt, hdrlen;
743340d773SGleb Smirnoff 	const char *dltname;
753c602fabSXin LI 
763c602fabSXin LI 	hdr = (const pktap_header_t *)bp;
773c602fabSXin LI 
78*ee67461eSJoseph Mingrone 	dlt = GET_LE_U_4(hdr->pkt_dlt);
79*ee67461eSJoseph Mingrone 	hdrlen = GET_LE_U_4(hdr->pkt_len);
803340d773SGleb Smirnoff 	dltname = pcap_datalink_val_to_name(dlt);
813c602fabSXin LI 	if (!ndo->ndo_qflag) {
82*ee67461eSJoseph Mingrone 		ND_PRINT("DLT %s (%u) len %u",
83*ee67461eSJoseph Mingrone 			  (dltname != NULL ? dltname : "UNKNOWN"), dlt, hdrlen);
843c602fabSXin LI         } else {
85*ee67461eSJoseph Mingrone 		ND_PRINT("%s", (dltname != NULL ? dltname : "UNKNOWN"));
863c602fabSXin LI         }
873c602fabSXin LI 
88*ee67461eSJoseph Mingrone 	ND_PRINT(", length %u: ", length);
893c602fabSXin LI }
903c602fabSXin LI 
913c602fabSXin LI /*
923c602fabSXin LI  * This is the top level routine of the printer.  'p' points
933c602fabSXin LI  * to the ether header of the packet, 'h->ts' is the timestamp,
943c602fabSXin LI  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
953c602fabSXin LI  * is the number of bytes actually captured.
963c602fabSXin LI  */
97*ee67461eSJoseph Mingrone void
pktap_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)983c602fabSXin LI pktap_if_print(netdissect_options *ndo,
993c602fabSXin LI                const struct pcap_pkthdr *h, const u_char *p)
1003c602fabSXin LI {
1013c602fabSXin LI 	uint32_t dlt, hdrlen, rectype;
1023c602fabSXin LI 	u_int caplen = h->caplen;
1033c602fabSXin LI 	u_int length = h->len;
1043c602fabSXin LI 	if_printer printer;
1053340d773SGleb Smirnoff 	const pktap_header_t *hdr;
1060bff6a5aSEd Maste 	struct pcap_pkthdr nhdr;
1073c602fabSXin LI 
108*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "pktap";
109*ee67461eSJoseph Mingrone 	if (length < sizeof(pktap_header_t)) {
110*ee67461eSJoseph Mingrone 		ND_PRINT(" (packet too short, %u < %zu)",
111*ee67461eSJoseph Mingrone 		         length, sizeof(pktap_header_t));
112*ee67461eSJoseph Mingrone 		goto invalid;
1133c602fabSXin LI 	}
1143340d773SGleb Smirnoff 	hdr = (const pktap_header_t *)p;
115*ee67461eSJoseph Mingrone 	dlt = GET_LE_U_4(hdr->pkt_dlt);
116*ee67461eSJoseph Mingrone 	hdrlen = GET_LE_U_4(hdr->pkt_len);
1173c602fabSXin LI 	if (hdrlen < sizeof(pktap_header_t)) {
1183c602fabSXin LI 		/*
1193c602fabSXin LI 		 * Claimed header length < structure length.
1203c602fabSXin LI 		 * XXX - does this just mean some fields aren't
1213c602fabSXin LI 		 * being supplied, or is it truly an error (i.e.,
1223c602fabSXin LI 		 * is the length supplied so that the header can
1233c602fabSXin LI 		 * be expanded in the future)?
1243c602fabSXin LI 		 */
125*ee67461eSJoseph Mingrone 		ND_PRINT(" (pkt_len too small, %u < %zu)",
126*ee67461eSJoseph Mingrone 		         hdrlen, sizeof(pktap_header_t));
127*ee67461eSJoseph Mingrone 		goto invalid;
1283c602fabSXin LI 	}
129*ee67461eSJoseph Mingrone 	if (hdrlen > length) {
130*ee67461eSJoseph Mingrone 		ND_PRINT(" (pkt_len too big, %u > %u)",
131*ee67461eSJoseph Mingrone 		         hdrlen, length);
132*ee67461eSJoseph Mingrone 		goto invalid;
1333c602fabSXin LI 	}
134*ee67461eSJoseph Mingrone 	ND_TCHECK_LEN(p, hdrlen);
1353c602fabSXin LI 
1363c602fabSXin LI 	if (ndo->ndo_eflag)
1373c602fabSXin LI 		pktap_header_print(ndo, p, length);
1383c602fabSXin LI 
1393c602fabSXin LI 	length -= hdrlen;
1403c602fabSXin LI 	caplen -= hdrlen;
1413c602fabSXin LI 	p += hdrlen;
1423c602fabSXin LI 
143*ee67461eSJoseph Mingrone 	rectype = GET_LE_U_4(hdr->pkt_rectype);
1443c602fabSXin LI 	switch (rectype) {
1453c602fabSXin LI 
1463c602fabSXin LI 	case PKT_REC_NONE:
147*ee67461eSJoseph Mingrone 		ND_PRINT("no data");
1483c602fabSXin LI 		break;
1493c602fabSXin LI 
1503c602fabSXin LI 	case PKT_REC_PACKET:
151*ee67461eSJoseph Mingrone 		printer = lookup_printer(dlt);
152*ee67461eSJoseph Mingrone 		if (printer != NULL) {
1530bff6a5aSEd Maste 			nhdr = *h;
1540bff6a5aSEd Maste 			nhdr.caplen = caplen;
1550bff6a5aSEd Maste 			nhdr.len = length;
156*ee67461eSJoseph Mingrone 			printer(ndo, &nhdr, p);
157*ee67461eSJoseph Mingrone 			hdrlen += ndo->ndo_ll_hdr_len;
1583c602fabSXin LI 		} else {
1593c602fabSXin LI 			if (!ndo->ndo_eflag)
1603340d773SGleb Smirnoff 				pktap_header_print(ndo, (const u_char *)hdr,
1613c602fabSXin LI 						length + hdrlen);
1623c602fabSXin LI 
1633c602fabSXin LI 			if (!ndo->ndo_suppress_default_print)
1643c602fabSXin LI 				ND_DEFAULTPRINT(p, caplen);
1653c602fabSXin LI 		}
1663c602fabSXin LI 		break;
1673c602fabSXin LI 	}
1683c602fabSXin LI 
169*ee67461eSJoseph Mingrone 	ndo->ndo_ll_hdr_len += hdrlen;
170*ee67461eSJoseph Mingrone 	return;
171*ee67461eSJoseph Mingrone 
172*ee67461eSJoseph Mingrone invalid:
173*ee67461eSJoseph Mingrone 	nd_print_invalid(ndo);
1743c602fabSXin LI }
1753c602fabSXin LI #endif /* DLT_PKTAP */
176