xref: /freebsd/contrib/tcpdump/print-pktap.c (revision 0bff6a5af8cb6d8e5123f8b667df78cac885dbb7)
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 
243c602fabSXin LI #ifdef HAVE_CONFIG_H
253c602fabSXin LI #include "config.h"
263c602fabSXin LI #endif
273c602fabSXin LI 
283340d773SGleb Smirnoff #include <netdissect-stdinc.h>
293c602fabSXin LI 
303340d773SGleb Smirnoff #include "netdissect.h"
313c602fabSXin LI #include "extract.h"
323c602fabSXin LI 
333c602fabSXin LI #ifdef DLT_PKTAP
343c602fabSXin LI 
353c602fabSXin LI /*
363c602fabSXin LI  * XXX - these are little-endian in the captures I've seen, but Apple
373c602fabSXin LI  * no longer make any big-endian machines (Macs use x86, iOS machines
383c602fabSXin LI  * use ARM and run it little-endian), so that might be by definition
393c602fabSXin LI  * or they might be host-endian.
403c602fabSXin LI  *
413c602fabSXin LI  * If a big-endian PKTAP file ever shows up, and it comes from a
423c602fabSXin LI  * big-endian machine, presumably these are host-endian, and we need
433c602fabSXin LI  * to just fetch the fields directly in tcpdump but byte-swap them
443c602fabSXin LI  * to host byte order in libpcap.
453c602fabSXin LI  */
463c602fabSXin LI typedef struct pktap_header {
473c602fabSXin LI 	uint32_t	pkt_len;	/* length of pktap header */
483c602fabSXin LI 	uint32_t	pkt_rectype;	/* type of record */
493c602fabSXin LI 	uint32_t	pkt_dlt;	/* DLT type of this packet */
503c602fabSXin LI 	char		pkt_ifname[24];	/* interface name */
513c602fabSXin LI 	uint32_t	pkt_flags;
523c602fabSXin LI 	uint32_t	pkt_pfamily;	/* "protocol family" */
533c602fabSXin LI 	uint32_t	pkt_llhdrlen;	/* link-layer header length? */
543c602fabSXin LI 	uint32_t	pkt_lltrlrlen;	/* link-layer trailer length? */
553c602fabSXin LI 	uint32_t	pkt_pid;	/* process ID */
563c602fabSXin LI 	char		pkt_cmdname[20]; /* command name */
573c602fabSXin LI 	uint32_t	pkt_svc_class;	/* "service class" */
583c602fabSXin LI 	uint16_t	pkt_iftype;	/* "interface type" */
593c602fabSXin LI 	uint16_t	pkt_ifunit;	/* unit number of interface? */
603c602fabSXin LI 	uint32_t	pkt_epid;	/* "effective process ID" */
613c602fabSXin LI 	char		pkt_ecmdname[20]; /* "effective command name" */
623c602fabSXin LI } pktap_header_t;
633c602fabSXin LI 
643c602fabSXin LI /*
653c602fabSXin LI  * Record types.
663c602fabSXin LI  */
673c602fabSXin LI #define PKT_REC_NONE	0	/* nothing follows the header */
683c602fabSXin LI #define PKT_REC_PACKET	1	/* a packet follows the header */
693c602fabSXin LI 
703c602fabSXin LI static inline void
713c602fabSXin LI pktap_header_print(netdissect_options *ndo, const u_char *bp, u_int length)
723c602fabSXin LI {
733c602fabSXin LI 	const pktap_header_t *hdr;
743c602fabSXin LI 	uint32_t dlt, hdrlen;
753340d773SGleb Smirnoff 	const char *dltname;
763c602fabSXin LI 
773c602fabSXin LI 	hdr = (const pktap_header_t *)bp;
783c602fabSXin LI 
793c602fabSXin LI 	dlt = EXTRACT_LE_32BITS(&hdr->pkt_dlt);
803c602fabSXin LI 	hdrlen = EXTRACT_LE_32BITS(&hdr->pkt_len);
813340d773SGleb Smirnoff 	dltname = pcap_datalink_val_to_name(dlt);
823c602fabSXin LI 	if (!ndo->ndo_qflag) {
833340d773SGleb Smirnoff 		ND_PRINT((ndo,"DLT %s (%d) len %d",
843340d773SGleb Smirnoff 			  (dltname != NULL ? dltname : "UNKNOWN"), dlt, hdrlen));
853c602fabSXin LI         } else {
863340d773SGleb Smirnoff 		ND_PRINT((ndo,"%s", (dltname != NULL ? dltname : "UNKNOWN")));
873c602fabSXin LI         }
883c602fabSXin LI 
893c602fabSXin LI 	ND_PRINT((ndo, ", length %u: ", length));
903c602fabSXin LI }
913c602fabSXin LI 
923c602fabSXin LI /*
933c602fabSXin LI  * This is the top level routine of the printer.  'p' points
943c602fabSXin LI  * to the ether header of the packet, 'h->ts' is the timestamp,
953c602fabSXin LI  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
963c602fabSXin LI  * is the number of bytes actually captured.
973c602fabSXin LI  */
983c602fabSXin LI u_int
993c602fabSXin LI pktap_if_print(netdissect_options *ndo,
1003c602fabSXin LI                const struct pcap_pkthdr *h, const u_char *p)
1013c602fabSXin LI {
1023c602fabSXin LI 	uint32_t dlt, hdrlen, rectype;
1033c602fabSXin LI 	u_int caplen = h->caplen;
1043c602fabSXin LI 	u_int length = h->len;
1053c602fabSXin LI 	if_printer printer;
1063340d773SGleb Smirnoff 	const pktap_header_t *hdr;
107*0bff6a5aSEd Maste 	struct pcap_pkthdr nhdr;
1083c602fabSXin LI 
1093c602fabSXin LI 	if (caplen < sizeof(pktap_header_t) || length < sizeof(pktap_header_t)) {
1103c602fabSXin LI 		ND_PRINT((ndo, "[|pktap]"));
1113c602fabSXin LI 		return (0);
1123c602fabSXin LI 	}
1133340d773SGleb Smirnoff 	hdr = (const pktap_header_t *)p;
1143c602fabSXin LI 	dlt = EXTRACT_LE_32BITS(&hdr->pkt_dlt);
1153c602fabSXin LI 	hdrlen = EXTRACT_LE_32BITS(&hdr->pkt_len);
1163c602fabSXin LI 	if (hdrlen < sizeof(pktap_header_t)) {
1173c602fabSXin LI 		/*
1183c602fabSXin LI 		 * Claimed header length < structure length.
1193c602fabSXin LI 		 * XXX - does this just mean some fields aren't
1203c602fabSXin LI 		 * being supplied, or is it truly an error (i.e.,
1213c602fabSXin LI 		 * is the length supplied so that the header can
1223c602fabSXin LI 		 * be expanded in the future)?
1233c602fabSXin LI 		 */
1243c602fabSXin LI 		ND_PRINT((ndo, "[|pktap]"));
1253c602fabSXin LI 		return (0);
1263c602fabSXin LI 	}
1273c602fabSXin LI 	if (caplen < hdrlen || length < hdrlen) {
1283c602fabSXin LI 		ND_PRINT((ndo, "[|pktap]"));
1293c602fabSXin LI 		return (hdrlen);
1303c602fabSXin LI 	}
1313c602fabSXin LI 
1323c602fabSXin LI 	if (ndo->ndo_eflag)
1333c602fabSXin LI 		pktap_header_print(ndo, p, length);
1343c602fabSXin LI 
1353c602fabSXin LI 	length -= hdrlen;
1363c602fabSXin LI 	caplen -= hdrlen;
1373c602fabSXin LI 	p += hdrlen;
1383c602fabSXin LI 
1393c602fabSXin LI 	rectype = EXTRACT_LE_32BITS(&hdr->pkt_rectype);
1403c602fabSXin LI 	switch (rectype) {
1413c602fabSXin LI 
1423c602fabSXin LI 	case PKT_REC_NONE:
1433c602fabSXin LI 		ND_PRINT((ndo, "no data"));
1443c602fabSXin LI 		break;
1453c602fabSXin LI 
1463c602fabSXin LI 	case PKT_REC_PACKET:
1473c602fabSXin LI 		if ((printer = lookup_printer(dlt)) != NULL) {
148*0bff6a5aSEd Maste 			nhdr = *h;
149*0bff6a5aSEd Maste 			nhdr.caplen = caplen;
150*0bff6a5aSEd Maste 			nhdr.len = length;
151*0bff6a5aSEd Maste 			hdrlen += printer(ndo, &nhdr, p);
1523c602fabSXin LI 		} else {
1533c602fabSXin LI 			if (!ndo->ndo_eflag)
1543340d773SGleb Smirnoff 				pktap_header_print(ndo, (const u_char *)hdr,
1553c602fabSXin LI 						length + hdrlen);
1563c602fabSXin LI 
1573c602fabSXin LI 			if (!ndo->ndo_suppress_default_print)
1583c602fabSXin LI 				ND_DEFAULTPRINT(p, caplen);
1593c602fabSXin LI 		}
1603c602fabSXin LI 		break;
1613c602fabSXin LI 	}
1623c602fabSXin LI 
1633c602fabSXin LI 	return (hdrlen);
1643c602fabSXin LI }
1653c602fabSXin LI 
1663c602fabSXin LI /*
1673c602fabSXin LI  * Local Variables:
1683c602fabSXin LI  * c-style: whitesmith
1693c602fabSXin LI  * c-basic-offset: 8
1703c602fabSXin LI  * End:
1713c602fabSXin LI  */
1723c602fabSXin LI 
1733c602fabSXin LI #endif /* DLT_PKTAP */
174