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 25*ee67461eSJoseph Mingrone #include <config.h> 263c602fabSXin LI #endif 273c602fabSXin LI 28*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h" 293c602fabSXin LI 30*ee67461eSJoseph Mingrone #define ND_LONGJMP_FROM_TCHECK 313340d773SGleb Smirnoff #include "netdissect.h" 323c602fabSXin LI #include "extract.h" 333c602fabSXin LI 343c602fabSXin LI #ifdef DLT_PKTAP 353c602fabSXin LI 363c602fabSXin LI /* 373c602fabSXin LI * XXX - these are little-endian in the captures I've seen, but Apple 383c602fabSXin LI * no longer make any big-endian machines (Macs use x86, iOS machines 393c602fabSXin LI * use ARM and run it little-endian), so that might be by definition 403c602fabSXin LI * or they might be host-endian. 413c602fabSXin LI * 423c602fabSXin LI * If a big-endian PKTAP file ever shows up, and it comes from a 433c602fabSXin LI * big-endian machine, presumably these are host-endian, and we need 443c602fabSXin LI * to just fetch the fields directly in tcpdump but byte-swap them 453c602fabSXin LI * to host byte order in libpcap. 463c602fabSXin LI */ 473c602fabSXin LI typedef struct pktap_header { 48*ee67461eSJoseph Mingrone nd_uint32_t pkt_len; /* length of pktap header */ 49*ee67461eSJoseph Mingrone nd_uint32_t pkt_rectype; /* type of record */ 50*ee67461eSJoseph Mingrone nd_uint32_t pkt_dlt; /* DLT type of this packet */ 513c602fabSXin LI char pkt_ifname[24]; /* interface name */ 52*ee67461eSJoseph Mingrone nd_uint32_t pkt_flags; 53*ee67461eSJoseph Mingrone nd_uint32_t pkt_pfamily; /* "protocol family" */ 54*ee67461eSJoseph Mingrone nd_uint32_t pkt_llhdrlen; /* link-layer header length? */ 55*ee67461eSJoseph Mingrone nd_uint32_t pkt_lltrlrlen; /* link-layer trailer length? */ 56*ee67461eSJoseph Mingrone nd_uint32_t pkt_pid; /* process ID */ 573c602fabSXin LI char pkt_cmdname[20]; /* command name */ 58*ee67461eSJoseph Mingrone nd_uint32_t pkt_svc_class; /* "service class" */ 59*ee67461eSJoseph Mingrone nd_uint16_t pkt_iftype; /* "interface type" */ 60*ee67461eSJoseph Mingrone nd_uint16_t pkt_ifunit; /* unit number of interface? */ 61*ee67461eSJoseph Mingrone nd_uint32_t pkt_epid; /* "effective process ID" */ 623c602fabSXin LI char pkt_ecmdname[20]; /* "effective command name" */ 633c602fabSXin LI } pktap_header_t; 643c602fabSXin LI 653c602fabSXin LI /* 663c602fabSXin LI * Record types. 673c602fabSXin LI */ 683c602fabSXin LI #define PKT_REC_NONE 0 /* nothing follows the header */ 693c602fabSXin LI #define PKT_REC_PACKET 1 /* a packet follows the header */ 703c602fabSXin LI 71*ee67461eSJoseph Mingrone static void 723c602fabSXin LI pktap_header_print(netdissect_options *ndo, const u_char *bp, u_int length) 733c602fabSXin LI { 743c602fabSXin LI const pktap_header_t *hdr; 753c602fabSXin LI uint32_t dlt, hdrlen; 763340d773SGleb Smirnoff const char *dltname; 773c602fabSXin LI 783c602fabSXin LI hdr = (const pktap_header_t *)bp; 793c602fabSXin LI 80*ee67461eSJoseph Mingrone dlt = GET_LE_U_4(hdr->pkt_dlt); 81*ee67461eSJoseph Mingrone hdrlen = GET_LE_U_4(hdr->pkt_len); 823340d773SGleb Smirnoff dltname = pcap_datalink_val_to_name(dlt); 833c602fabSXin LI if (!ndo->ndo_qflag) { 84*ee67461eSJoseph Mingrone ND_PRINT("DLT %s (%u) len %u", 85*ee67461eSJoseph Mingrone (dltname != NULL ? dltname : "UNKNOWN"), dlt, hdrlen); 863c602fabSXin LI } else { 87*ee67461eSJoseph Mingrone ND_PRINT("%s", (dltname != NULL ? dltname : "UNKNOWN")); 883c602fabSXin LI } 893c602fabSXin LI 90*ee67461eSJoseph Mingrone ND_PRINT(", length %u: ", length); 913c602fabSXin LI } 923c602fabSXin LI 933c602fabSXin LI /* 943c602fabSXin LI * This is the top level routine of the printer. 'p' points 953c602fabSXin LI * to the ether header of the packet, 'h->ts' is the timestamp, 963c602fabSXin LI * 'h->len' is the length of the packet off the wire, and 'h->caplen' 973c602fabSXin LI * is the number of bytes actually captured. 983c602fabSXin LI */ 99*ee67461eSJoseph Mingrone void 1003c602fabSXin LI pktap_if_print(netdissect_options *ndo, 1013c602fabSXin LI const struct pcap_pkthdr *h, const u_char *p) 1023c602fabSXin LI { 1033c602fabSXin LI uint32_t dlt, hdrlen, rectype; 1043c602fabSXin LI u_int caplen = h->caplen; 1053c602fabSXin LI u_int length = h->len; 1063c602fabSXin LI if_printer printer; 1073340d773SGleb Smirnoff const pktap_header_t *hdr; 1080bff6a5aSEd Maste struct pcap_pkthdr nhdr; 1093c602fabSXin LI 110*ee67461eSJoseph Mingrone ndo->ndo_protocol = "pktap"; 111*ee67461eSJoseph Mingrone if (length < sizeof(pktap_header_t)) { 112*ee67461eSJoseph Mingrone ND_PRINT(" (packet too short, %u < %zu)", 113*ee67461eSJoseph Mingrone length, sizeof(pktap_header_t)); 114*ee67461eSJoseph Mingrone goto invalid; 1153c602fabSXin LI } 1163340d773SGleb Smirnoff hdr = (const pktap_header_t *)p; 117*ee67461eSJoseph Mingrone dlt = GET_LE_U_4(hdr->pkt_dlt); 118*ee67461eSJoseph Mingrone hdrlen = GET_LE_U_4(hdr->pkt_len); 1193c602fabSXin LI if (hdrlen < sizeof(pktap_header_t)) { 1203c602fabSXin LI /* 1213c602fabSXin LI * Claimed header length < structure length. 1223c602fabSXin LI * XXX - does this just mean some fields aren't 1233c602fabSXin LI * being supplied, or is it truly an error (i.e., 1243c602fabSXin LI * is the length supplied so that the header can 1253c602fabSXin LI * be expanded in the future)? 1263c602fabSXin LI */ 127*ee67461eSJoseph Mingrone ND_PRINT(" (pkt_len too small, %u < %zu)", 128*ee67461eSJoseph Mingrone hdrlen, sizeof(pktap_header_t)); 129*ee67461eSJoseph Mingrone goto invalid; 1303c602fabSXin LI } 131*ee67461eSJoseph Mingrone if (hdrlen > length) { 132*ee67461eSJoseph Mingrone ND_PRINT(" (pkt_len too big, %u > %u)", 133*ee67461eSJoseph Mingrone hdrlen, length); 134*ee67461eSJoseph Mingrone goto invalid; 1353c602fabSXin LI } 136*ee67461eSJoseph Mingrone ND_TCHECK_LEN(p, hdrlen); 1373c602fabSXin LI 1383c602fabSXin LI if (ndo->ndo_eflag) 1393c602fabSXin LI pktap_header_print(ndo, p, length); 1403c602fabSXin LI 1413c602fabSXin LI length -= hdrlen; 1423c602fabSXin LI caplen -= hdrlen; 1433c602fabSXin LI p += hdrlen; 1443c602fabSXin LI 145*ee67461eSJoseph Mingrone rectype = GET_LE_U_4(hdr->pkt_rectype); 1463c602fabSXin LI switch (rectype) { 1473c602fabSXin LI 1483c602fabSXin LI case PKT_REC_NONE: 149*ee67461eSJoseph Mingrone ND_PRINT("no data"); 1503c602fabSXin LI break; 1513c602fabSXin LI 1523c602fabSXin LI case PKT_REC_PACKET: 153*ee67461eSJoseph Mingrone printer = lookup_printer(dlt); 154*ee67461eSJoseph Mingrone if (printer != NULL) { 1550bff6a5aSEd Maste nhdr = *h; 1560bff6a5aSEd Maste nhdr.caplen = caplen; 1570bff6a5aSEd Maste nhdr.len = length; 158*ee67461eSJoseph Mingrone printer(ndo, &nhdr, p); 159*ee67461eSJoseph Mingrone hdrlen += ndo->ndo_ll_hdr_len; 1603c602fabSXin LI } else { 1613c602fabSXin LI if (!ndo->ndo_eflag) 1623340d773SGleb Smirnoff pktap_header_print(ndo, (const u_char *)hdr, 1633c602fabSXin LI length + hdrlen); 1643c602fabSXin LI 1653c602fabSXin LI if (!ndo->ndo_suppress_default_print) 1663c602fabSXin LI ND_DEFAULTPRINT(p, caplen); 1673c602fabSXin LI } 1683c602fabSXin LI break; 1693c602fabSXin LI } 1703c602fabSXin LI 171*ee67461eSJoseph Mingrone ndo->ndo_ll_hdr_len += hdrlen; 172*ee67461eSJoseph Mingrone return; 173*ee67461eSJoseph Mingrone 174*ee67461eSJoseph Mingrone invalid: 175*ee67461eSJoseph Mingrone nd_print_invalid(ndo); 1763c602fabSXin LI } 1773c602fabSXin LI #endif /* DLT_PKTAP */ 178