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