1c47fd378Schristos /* 2c47fd378Schristos * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000 3c47fd378Schristos * The Regents of the University of California. All rights reserved. 4c47fd378Schristos * 5c47fd378Schristos * Redistribution and use in source and binary forms, with or without 6c47fd378Schristos * modification, are permitted provided that: (1) source code distributions 7c47fd378Schristos * retain the above copyright notice and this paragraph in its entirety, (2) 8c47fd378Schristos * distributions including binary code include the above copyright notice and 9c47fd378Schristos * this paragraph in its entirety in the documentation or other materials 10c47fd378Schristos * provided with the distribution, and (3) all advertising materials mentioning 11c47fd378Schristos * features or use of this software display the following acknowledgement: 12c47fd378Schristos * ``This product includes software developed by the University of California, 13c47fd378Schristos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14c47fd378Schristos * the University nor the names of its contributors may be used to endorse 15c47fd378Schristos * or promote products derived from this software without specific prior 16c47fd378Schristos * written permission. 17c47fd378Schristos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18c47fd378Schristos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19c47fd378Schristos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20c47fd378Schristos */ 21c47fd378Schristos 22b3a00663Schristos #include <sys/cdefs.h> 23b3a00663Schristos #ifndef lint 24*26ba0b50Schristos __RCSID("$NetBSD: print-pktap.c,v 1.7 2024/09/02 16:15:32 christos Exp $"); 25b3a00663Schristos #endif 26b3a00663Schristos 27dc860a36Sspz /* \summary: Apple's DLT_PKTAP printer */ 28dc860a36Sspz 29c74ad251Schristos #include <config.h> 30c47fd378Schristos 31c74ad251Schristos #include "netdissect-stdinc.h" 32c47fd378Schristos 33c74ad251Schristos #define ND_LONGJMP_FROM_TCHECK 34fdccd7e4Schristos #include "netdissect.h" 35c47fd378Schristos #include "extract.h" 36c47fd378Schristos 37c47fd378Schristos #ifdef DLT_PKTAP 38c47fd378Schristos 39c47fd378Schristos /* 40c47fd378Schristos * XXX - these are little-endian in the captures I've seen, but Apple 41c47fd378Schristos * no longer make any big-endian machines (Macs use x86, iOS machines 42c47fd378Schristos * use ARM and run it little-endian), so that might be by definition 43c47fd378Schristos * or they might be host-endian. 44c47fd378Schristos * 45c47fd378Schristos * If a big-endian PKTAP file ever shows up, and it comes from a 46c47fd378Schristos * big-endian machine, presumably these are host-endian, and we need 47c47fd378Schristos * to just fetch the fields directly in tcpdump but byte-swap them 48c47fd378Schristos * to host byte order in libpcap. 49c47fd378Schristos */ 50c47fd378Schristos typedef struct pktap_header { 51c74ad251Schristos nd_uint32_t pkt_len; /* length of pktap header */ 52c74ad251Schristos nd_uint32_t pkt_rectype; /* type of record */ 53c74ad251Schristos nd_uint32_t pkt_dlt; /* DLT type of this packet */ 54c47fd378Schristos char pkt_ifname[24]; /* interface name */ 55c74ad251Schristos nd_uint32_t pkt_flags; 56c74ad251Schristos nd_uint32_t pkt_pfamily; /* "protocol family" */ 57c74ad251Schristos nd_uint32_t pkt_llhdrlen; /* link-layer header length? */ 58c74ad251Schristos nd_uint32_t pkt_lltrlrlen; /* link-layer trailer length? */ 59c74ad251Schristos nd_uint32_t pkt_pid; /* process ID */ 60c47fd378Schristos char pkt_cmdname[20]; /* command name */ 61c74ad251Schristos nd_uint32_t pkt_svc_class; /* "service class" */ 62c74ad251Schristos nd_uint16_t pkt_iftype; /* "interface type" */ 63c74ad251Schristos nd_uint16_t pkt_ifunit; /* unit number of interface? */ 64c74ad251Schristos nd_uint32_t pkt_epid; /* "effective process ID" */ 65c47fd378Schristos char pkt_ecmdname[20]; /* "effective command name" */ 66c47fd378Schristos } pktap_header_t; 67c47fd378Schristos 68c47fd378Schristos /* 69c47fd378Schristos * Record types. 70c47fd378Schristos */ 71c47fd378Schristos #define PKT_REC_NONE 0 /* nothing follows the header */ 72c47fd378Schristos #define PKT_REC_PACKET 1 /* a packet follows the header */ 73c47fd378Schristos 74c74ad251Schristos static void 75c47fd378Schristos pktap_header_print(netdissect_options *ndo, const u_char *bp, u_int length) 76c47fd378Schristos { 77c47fd378Schristos const pktap_header_t *hdr; 78c47fd378Schristos uint32_t dlt, hdrlen; 79dc860a36Sspz const char *dltname; 80c47fd378Schristos 81c47fd378Schristos hdr = (const pktap_header_t *)bp; 82c47fd378Schristos 83c74ad251Schristos dlt = GET_LE_U_4(hdr->pkt_dlt); 84c74ad251Schristos hdrlen = GET_LE_U_4(hdr->pkt_len); 85dc860a36Sspz dltname = pcap_datalink_val_to_name(dlt); 86c47fd378Schristos if (!ndo->ndo_qflag) { 87c74ad251Schristos ND_PRINT("DLT %s (%u) len %u", 88c74ad251Schristos (dltname != NULL ? dltname : "UNKNOWN"), dlt, hdrlen); 89c47fd378Schristos } else { 90c74ad251Schristos ND_PRINT("%s", (dltname != NULL ? dltname : "UNKNOWN")); 91c47fd378Schristos } 92c47fd378Schristos 93c74ad251Schristos ND_PRINT(", length %u: ", length); 94c47fd378Schristos } 95c47fd378Schristos 96c47fd378Schristos /* 97c47fd378Schristos * This is the top level routine of the printer. 'p' points 98c47fd378Schristos * to the ether header of the packet, 'h->ts' is the timestamp, 99c47fd378Schristos * 'h->len' is the length of the packet off the wire, and 'h->caplen' 100c47fd378Schristos * is the number of bytes actually captured. 101c47fd378Schristos */ 102c74ad251Schristos void 103c47fd378Schristos pktap_if_print(netdissect_options *ndo, 104c47fd378Schristos const struct pcap_pkthdr *h, const u_char *p) 105c47fd378Schristos { 106c47fd378Schristos uint32_t dlt, hdrlen, rectype; 107c47fd378Schristos u_int caplen = h->caplen; 108c47fd378Schristos u_int length = h->len; 109c47fd378Schristos if_printer printer; 110fdccd7e4Schristos const pktap_header_t *hdr; 11172c96ff3Schristos struct pcap_pkthdr nhdr; 112c47fd378Schristos 113c74ad251Schristos ndo->ndo_protocol = "pktap"; 114c74ad251Schristos if (length < sizeof(pktap_header_t)) { 115c74ad251Schristos ND_PRINT(" (packet too short, %u < %zu)", 116c74ad251Schristos length, sizeof(pktap_header_t)); 117c74ad251Schristos goto invalid; 118c47fd378Schristos } 119fdccd7e4Schristos hdr = (const pktap_header_t *)p; 120c74ad251Schristos dlt = GET_LE_U_4(hdr->pkt_dlt); 121c74ad251Schristos hdrlen = GET_LE_U_4(hdr->pkt_len); 122c47fd378Schristos if (hdrlen < sizeof(pktap_header_t)) { 123c47fd378Schristos /* 124c47fd378Schristos * Claimed header length < structure length. 125c47fd378Schristos * XXX - does this just mean some fields aren't 126c47fd378Schristos * being supplied, or is it truly an error (i.e., 127c47fd378Schristos * is the length supplied so that the header can 128c47fd378Schristos * be expanded in the future)? 129c47fd378Schristos */ 130c74ad251Schristos ND_PRINT(" (pkt_len too small, %u < %zu)", 131c74ad251Schristos hdrlen, sizeof(pktap_header_t)); 132c74ad251Schristos goto invalid; 133c47fd378Schristos } 134c74ad251Schristos if (hdrlen > length) { 135c74ad251Schristos ND_PRINT(" (pkt_len too big, %u > %u)", 136c74ad251Schristos hdrlen, length); 137c74ad251Schristos goto invalid; 138c47fd378Schristos } 139c74ad251Schristos ND_TCHECK_LEN(p, hdrlen); 140c47fd378Schristos 141c47fd378Schristos if (ndo->ndo_eflag) 142c47fd378Schristos pktap_header_print(ndo, p, length); 143c47fd378Schristos 144c47fd378Schristos length -= hdrlen; 145c47fd378Schristos caplen -= hdrlen; 146c47fd378Schristos p += hdrlen; 147c47fd378Schristos 148c74ad251Schristos rectype = GET_LE_U_4(hdr->pkt_rectype); 149c47fd378Schristos switch (rectype) { 150c47fd378Schristos 151c47fd378Schristos case PKT_REC_NONE: 152c74ad251Schristos ND_PRINT("no data"); 153c47fd378Schristos break; 154c47fd378Schristos 155c47fd378Schristos case PKT_REC_PACKET: 156c74ad251Schristos printer = lookup_printer(dlt); 157c74ad251Schristos if (printer != NULL) { 15872c96ff3Schristos nhdr = *h; 15972c96ff3Schristos nhdr.caplen = caplen; 16072c96ff3Schristos nhdr.len = length; 161c74ad251Schristos printer(ndo, &nhdr, p); 162c74ad251Schristos hdrlen += ndo->ndo_ll_hdr_len; 163c47fd378Schristos } else { 164c47fd378Schristos if (!ndo->ndo_eflag) 165fdccd7e4Schristos pktap_header_print(ndo, (const u_char *)hdr, 166c47fd378Schristos length + hdrlen); 167c47fd378Schristos 168c47fd378Schristos if (!ndo->ndo_suppress_default_print) 169c47fd378Schristos ND_DEFAULTPRINT(p, caplen); 170c47fd378Schristos } 171c47fd378Schristos break; 172c47fd378Schristos } 173c47fd378Schristos 174c74ad251Schristos ndo->ndo_ll_hdr_len += hdrlen; 175c74ad251Schristos return; 176c74ad251Schristos 177c74ad251Schristos invalid: 178c74ad251Schristos nd_print_invalid(ndo); 179c47fd378Schristos } 180c47fd378Schristos #endif /* DLT_PKTAP */ 181