13340d773SGleb Smirnoff /* 23340d773SGleb Smirnoff * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000 33340d773SGleb Smirnoff * The Regents of the University of California. All rights reserved. 43340d773SGleb Smirnoff * 53340d773SGleb Smirnoff * Redistribution and use in source and binary forms, with or without 63340d773SGleb Smirnoff * modification, are permitted provided that: (1) source code distributions 73340d773SGleb Smirnoff * retain the above copyright notice and this paragraph in its entirety, (2) 83340d773SGleb Smirnoff * distributions including binary code include the above copyright notice and 93340d773SGleb Smirnoff * this paragraph in its entirety in the documentation or other materials 103340d773SGleb Smirnoff * provided with the distribution, and (3) all advertising materials mentioning 113340d773SGleb Smirnoff * features or use of this software display the following acknowledgement: 123340d773SGleb Smirnoff * ``This product includes software developed by the University of California, 133340d773SGleb Smirnoff * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 143340d773SGleb Smirnoff * the University nor the names of its contributors may be used to endorse 153340d773SGleb Smirnoff * or promote products derived from this software without specific prior 163340d773SGleb Smirnoff * written permission. 173340d773SGleb Smirnoff * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 183340d773SGleb Smirnoff * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 193340d773SGleb Smirnoff * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 203340d773SGleb Smirnoff * 213340d773SGleb Smirnoff * Support for splitting captures into multiple files with a maximum 223340d773SGleb Smirnoff * file size: 233340d773SGleb Smirnoff * 243340d773SGleb Smirnoff * Copyright (c) 2001 253340d773SGleb Smirnoff * Seth Webster <swebster@sst.ll.mit.edu> 263340d773SGleb Smirnoff */ 273340d773SGleb Smirnoff 283340d773SGleb Smirnoff #ifdef HAVE_CONFIG_H 29ee67461eSJoseph Mingrone #include <config.h> 303340d773SGleb Smirnoff #endif 313340d773SGleb Smirnoff 323340d773SGleb Smirnoff #include <stdlib.h> 333340d773SGleb Smirnoff #include <string.h> 34ee67461eSJoseph Mingrone #include <setjmp.h> 353340d773SGleb Smirnoff 36ee67461eSJoseph Mingrone #include "netdissect-stdinc.h" 373340d773SGleb Smirnoff 383340d773SGleb Smirnoff #include "netdissect.h" 393340d773SGleb Smirnoff #include "addrtoname.h" 403340d773SGleb Smirnoff #include "print.h" 41ee67461eSJoseph Mingrone #include "netdissect-alloc.h" 42ee67461eSJoseph Mingrone 43ee67461eSJoseph Mingrone #include "pcap-missing.h" 443340d773SGleb Smirnoff 453340d773SGleb Smirnoff struct printer { 463340d773SGleb Smirnoff if_printer f; 473340d773SGleb Smirnoff int type; 483340d773SGleb Smirnoff }; 493340d773SGleb Smirnoff 503340d773SGleb Smirnoff static const struct printer printers[] = { 51ee67461eSJoseph Mingrone #ifdef DLT_APPLE_IP_OVER_IEEE1394 52ee67461eSJoseph Mingrone { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 }, 53ee67461eSJoseph Mingrone #endif 54ee67461eSJoseph Mingrone { arcnet_if_print, DLT_ARCNET }, 55ee67461eSJoseph Mingrone #ifdef DLT_ARCNET_LINUX 56ee67461eSJoseph Mingrone { arcnet_linux_if_print, DLT_ARCNET_LINUX }, 57ee67461eSJoseph Mingrone #endif 58ee67461eSJoseph Mingrone { atm_if_print, DLT_ATM_RFC1483 }, 59ee67461eSJoseph Mingrone #ifdef DLT_DSA_TAG_BRCM 60ee67461eSJoseph Mingrone { brcm_tag_if_print, DLT_DSA_TAG_BRCM }, 61ee67461eSJoseph Mingrone #endif 62ee67461eSJoseph Mingrone #ifdef DLT_DSA_TAG_BRCM_PREPEND 63ee67461eSJoseph Mingrone { brcm_tag_prepend_if_print, DLT_DSA_TAG_BRCM_PREPEND }, 64ee67461eSJoseph Mingrone #endif 65ee67461eSJoseph Mingrone #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR 66ee67461eSJoseph Mingrone { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR}, 67ee67461eSJoseph Mingrone #endif 68ee67461eSJoseph Mingrone #ifdef DLT_C_HDLC 69ee67461eSJoseph Mingrone { chdlc_if_print, DLT_C_HDLC }, 70ee67461eSJoseph Mingrone #endif 71ee67461eSJoseph Mingrone #ifdef DLT_HDLC 72ee67461eSJoseph Mingrone { chdlc_if_print, DLT_HDLC }, 73ee67461eSJoseph Mingrone #endif 74ee67461eSJoseph Mingrone #ifdef DLT_ATM_CLIP 75ee67461eSJoseph Mingrone { cip_if_print, DLT_ATM_CLIP }, 76ee67461eSJoseph Mingrone #endif 77ee67461eSJoseph Mingrone #ifdef DLT_CIP 78ee67461eSJoseph Mingrone { cip_if_print, DLT_CIP }, 79ee67461eSJoseph Mingrone #endif 80ee67461eSJoseph Mingrone #ifdef DLT_DSA_TAG_DSA 81ee67461eSJoseph Mingrone { dsa_if_print, DLT_DSA_TAG_DSA }, 82ee67461eSJoseph Mingrone #endif 83ee67461eSJoseph Mingrone #ifdef DLT_DSA_TAG_EDSA 84ee67461eSJoseph Mingrone { edsa_if_print, DLT_DSA_TAG_EDSA }, 85ee67461eSJoseph Mingrone #endif 86ee67461eSJoseph Mingrone #ifdef DLT_ENC 87ee67461eSJoseph Mingrone { enc_if_print, DLT_ENC }, 88ee67461eSJoseph Mingrone #endif 893340d773SGleb Smirnoff { ether_if_print, DLT_EN10MB }, 90ee67461eSJoseph Mingrone { fddi_if_print, DLT_FDDI }, 91ee67461eSJoseph Mingrone #ifdef DLT_FR 92ee67461eSJoseph Mingrone { fr_if_print, DLT_FR }, 93ee67461eSJoseph Mingrone #endif 94ee67461eSJoseph Mingrone #ifdef DLT_FRELAY 95ee67461eSJoseph Mingrone { fr_if_print, DLT_FRELAY }, 96ee67461eSJoseph Mingrone #endif 97ee67461eSJoseph Mingrone #ifdef DLT_IEEE802_11 98ee67461eSJoseph Mingrone { ieee802_11_if_print, DLT_IEEE802_11}, 99ee67461eSJoseph Mingrone #endif 100ee67461eSJoseph Mingrone #ifdef DLT_IEEE802_11_RADIO_AVS 101ee67461eSJoseph Mingrone { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS }, 102ee67461eSJoseph Mingrone #endif 103ee67461eSJoseph Mingrone #ifdef DLT_IEEE802_11_RADIO 104ee67461eSJoseph Mingrone { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO }, 1053340d773SGleb Smirnoff #endif 1063340d773SGleb Smirnoff #ifdef DLT_IEEE802_15_4 1073340d773SGleb Smirnoff { ieee802_15_4_if_print, DLT_IEEE802_15_4 }, 1083340d773SGleb Smirnoff #endif 1093340d773SGleb Smirnoff #ifdef DLT_IEEE802_15_4_NOFCS 1103340d773SGleb Smirnoff { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS }, 1113340d773SGleb Smirnoff #endif 112ee67461eSJoseph Mingrone #ifdef DLT_IEEE802_15_4_TAP 113ee67461eSJoseph Mingrone { ieee802_15_4_tap_if_print, DLT_IEEE802_15_4_TAP }, 114ee67461eSJoseph Mingrone #endif 115ee67461eSJoseph Mingrone #ifdef DLT_IP_OVER_FC 116ee67461eSJoseph Mingrone { ipfc_if_print, DLT_IP_OVER_FC }, 117ee67461eSJoseph Mingrone #endif 118ee67461eSJoseph Mingrone #ifdef DLT_IPNET 119ee67461eSJoseph Mingrone { ipnet_if_print, DLT_IPNET }, 120ee67461eSJoseph Mingrone #endif 121ee67461eSJoseph Mingrone #ifdef DLT_IPOIB 122ee67461eSJoseph Mingrone { ipoib_if_print, DLT_IPOIB }, 123ee67461eSJoseph Mingrone #endif 124ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_ATM1 125ee67461eSJoseph Mingrone { juniper_atm1_if_print, DLT_JUNIPER_ATM1 }, 126ee67461eSJoseph Mingrone #endif 127ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_ATM2 128ee67461eSJoseph Mingrone { juniper_atm2_if_print, DLT_JUNIPER_ATM2 }, 129ee67461eSJoseph Mingrone #endif 130ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_CHDLC 131ee67461eSJoseph Mingrone { juniper_chdlc_if_print, DLT_JUNIPER_CHDLC }, 132ee67461eSJoseph Mingrone #endif 133ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_ES 134ee67461eSJoseph Mingrone { juniper_es_if_print, DLT_JUNIPER_ES }, 135ee67461eSJoseph Mingrone #endif 136ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_ETHER 137ee67461eSJoseph Mingrone { juniper_ether_if_print, DLT_JUNIPER_ETHER }, 138ee67461eSJoseph Mingrone #endif 139ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_FRELAY 140ee67461eSJoseph Mingrone { juniper_frelay_if_print, DLT_JUNIPER_FRELAY }, 141ee67461eSJoseph Mingrone #endif 142ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_GGSN 143ee67461eSJoseph Mingrone { juniper_ggsn_if_print, DLT_JUNIPER_GGSN }, 144ee67461eSJoseph Mingrone #endif 145ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_MFR 146ee67461eSJoseph Mingrone { juniper_mfr_if_print, DLT_JUNIPER_MFR }, 147ee67461eSJoseph Mingrone #endif 148ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_MLFR 149ee67461eSJoseph Mingrone { juniper_mlfr_if_print, DLT_JUNIPER_MLFR }, 150ee67461eSJoseph Mingrone #endif 151ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_MLPPP 152ee67461eSJoseph Mingrone { juniper_mlppp_if_print, DLT_JUNIPER_MLPPP }, 153ee67461eSJoseph Mingrone #endif 154ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_MONITOR 155ee67461eSJoseph Mingrone { juniper_monitor_if_print, DLT_JUNIPER_MONITOR }, 156ee67461eSJoseph Mingrone #endif 157ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_PPP 158ee67461eSJoseph Mingrone { juniper_ppp_if_print, DLT_JUNIPER_PPP }, 159ee67461eSJoseph Mingrone #endif 160ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_PPPOE_ATM 161ee67461eSJoseph Mingrone { juniper_pppoe_atm_if_print, DLT_JUNIPER_PPPOE_ATM }, 162ee67461eSJoseph Mingrone #endif 163ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_PPPOE 164ee67461eSJoseph Mingrone { juniper_pppoe_if_print, DLT_JUNIPER_PPPOE }, 165ee67461eSJoseph Mingrone #endif 166ee67461eSJoseph Mingrone #ifdef DLT_JUNIPER_SERVICES 167ee67461eSJoseph Mingrone { juniper_services_if_print, DLT_JUNIPER_SERVICES }, 168ee67461eSJoseph Mingrone #endif 169ee67461eSJoseph Mingrone #ifdef DLT_LTALK 170ee67461eSJoseph Mingrone { ltalk_if_print, DLT_LTALK }, 171ee67461eSJoseph Mingrone #endif 172ee67461eSJoseph Mingrone #ifdef DLT_MFR 173ee67461eSJoseph Mingrone { mfr_if_print, DLT_MFR }, 1743340d773SGleb Smirnoff #endif 1753340d773SGleb Smirnoff #ifdef DLT_NETANALYZER 1763340d773SGleb Smirnoff { netanalyzer_if_print, DLT_NETANALYZER }, 1773340d773SGleb Smirnoff #endif 1783340d773SGleb Smirnoff #ifdef DLT_NETANALYZER_TRANSPARENT 1793340d773SGleb Smirnoff { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT }, 1803340d773SGleb Smirnoff #endif 181ee67461eSJoseph Mingrone #ifdef DLT_NFLOG 1823340d773SGleb Smirnoff { nflog_if_print, DLT_NFLOG}, 1833340d773SGleb Smirnoff #endif 1843340d773SGleb Smirnoff { null_if_print, DLT_NULL }, 1853340d773SGleb Smirnoff #ifdef DLT_LOOP 1863340d773SGleb Smirnoff { null_if_print, DLT_LOOP }, 1873340d773SGleb Smirnoff #endif 188*171a7bbfSGleb Smirnoff #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H) 189ee67461eSJoseph Mingrone { pflog_if_print, DLT_PFLOG }, 1903340d773SGleb Smirnoff #endif 191ee67461eSJoseph Mingrone #ifdef DLT_PKTAP 192ee67461eSJoseph Mingrone { pktap_if_print, DLT_PKTAP }, 1933340d773SGleb Smirnoff #endif 194ee67461eSJoseph Mingrone #ifdef DLT_PPI 195ee67461eSJoseph Mingrone { ppi_if_print, DLT_PPI }, 1963340d773SGleb Smirnoff #endif 197ee67461eSJoseph Mingrone #ifdef DLT_PPP_BSDOS 198ee67461eSJoseph Mingrone { ppp_bsdos_if_print, DLT_PPP_BSDOS }, 199ee67461eSJoseph Mingrone #endif 200ee67461eSJoseph Mingrone #ifdef DLT_PPP_SERIAL 201ee67461eSJoseph Mingrone { ppp_hdlc_if_print, DLT_PPP_SERIAL }, 202ee67461eSJoseph Mingrone #endif 203ee67461eSJoseph Mingrone { ppp_if_print, DLT_PPP }, 204ee67461eSJoseph Mingrone #ifdef DLT_PPP_PPPD 205ee67461eSJoseph Mingrone { ppp_if_print, DLT_PPP_PPPD }, 206ee67461eSJoseph Mingrone #endif 207ee67461eSJoseph Mingrone #ifdef DLT_PPP_ETHER 208ee67461eSJoseph Mingrone { pppoe_if_print, DLT_PPP_ETHER }, 209ee67461eSJoseph Mingrone #endif 210ee67461eSJoseph Mingrone #ifdef DLT_PRISM_HEADER 211ee67461eSJoseph Mingrone { prism_if_print, DLT_PRISM_HEADER }, 2123340d773SGleb Smirnoff #endif 2133340d773SGleb Smirnoff { raw_if_print, DLT_RAW }, 2143340d773SGleb Smirnoff #ifdef DLT_IPV4 2153340d773SGleb Smirnoff { raw_if_print, DLT_IPV4 }, 2163340d773SGleb Smirnoff #endif 2173340d773SGleb Smirnoff #ifdef DLT_IPV6 2183340d773SGleb Smirnoff { raw_if_print, DLT_IPV6 }, 2193340d773SGleb Smirnoff #endif 2203340d773SGleb Smirnoff #ifdef DLT_SLIP_BSDOS 2213340d773SGleb Smirnoff { sl_bsdos_if_print, DLT_SLIP_BSDOS }, 2223340d773SGleb Smirnoff #endif 223ee67461eSJoseph Mingrone { sl_if_print, DLT_SLIP }, 224ee67461eSJoseph Mingrone #ifdef DLT_LINUX_SLL 225ee67461eSJoseph Mingrone { sll_if_print, DLT_LINUX_SLL }, 2263340d773SGleb Smirnoff #endif 227ee67461eSJoseph Mingrone #ifdef DLT_LINUX_SLL2 228ee67461eSJoseph Mingrone { sll2_if_print, DLT_LINUX_SLL2 }, 2293340d773SGleb Smirnoff #endif 230ee67461eSJoseph Mingrone #ifdef DLT_SUNATM 231ee67461eSJoseph Mingrone { sunatm_if_print, DLT_SUNATM }, 2323340d773SGleb Smirnoff #endif 233ee67461eSJoseph Mingrone #ifdef DLT_SYMANTEC_FIREWALL 234ee67461eSJoseph Mingrone { symantec_if_print, DLT_SYMANTEC_FIREWALL }, 2353340d773SGleb Smirnoff #endif 236ee67461eSJoseph Mingrone { token_if_print, DLT_IEEE802 }, 237ee67461eSJoseph Mingrone #ifdef DLT_USB_LINUX 238ee67461eSJoseph Mingrone { usb_linux_48_byte_if_print, DLT_USB_LINUX}, 239ee67461eSJoseph Mingrone #endif /* DLT_USB_LINUX */ 240ee67461eSJoseph Mingrone #ifdef DLT_USB_LINUX_MMAPPED 241ee67461eSJoseph Mingrone { usb_linux_64_byte_if_print, DLT_USB_LINUX_MMAPPED}, 242ee67461eSJoseph Mingrone #endif /* DLT_USB_LINUX_MMAPPED */ 243ee67461eSJoseph Mingrone #ifdef DLT_VSOCK 244ee67461eSJoseph Mingrone { vsock_if_print, DLT_VSOCK }, 2453340d773SGleb Smirnoff #endif 2463340d773SGleb Smirnoff { NULL, 0 }, 2473340d773SGleb Smirnoff }; 2483340d773SGleb Smirnoff 2493340d773SGleb Smirnoff static void ndo_default_print(netdissect_options *ndo, const u_char *bp, 2503340d773SGleb Smirnoff u_int length); 2513340d773SGleb Smirnoff 252ee67461eSJoseph Mingrone static void NORETURN ndo_error(netdissect_options *ndo, 253ee67461eSJoseph Mingrone status_exit_codes_t status, 2540bff6a5aSEd Maste FORMAT_STRING(const char *fmt), ...) 255ee67461eSJoseph Mingrone PRINTFLIKE(3, 4); 2560bff6a5aSEd Maste static void ndo_warning(netdissect_options *ndo, 2570bff6a5aSEd Maste FORMAT_STRING(const char *fmt), ...) 2580bff6a5aSEd Maste PRINTFLIKE(2, 3); 2593340d773SGleb Smirnoff 2600bff6a5aSEd Maste static int ndo_printf(netdissect_options *ndo, 2610bff6a5aSEd Maste FORMAT_STRING(const char *fmt), ...) 2620bff6a5aSEd Maste PRINTFLIKE(2, 3); 2633340d773SGleb Smirnoff 2643340d773SGleb Smirnoff void 265ee67461eSJoseph Mingrone init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask) 2663340d773SGleb Smirnoff { 2673340d773SGleb Smirnoff 2683340d773SGleb Smirnoff init_addrtoname(ndo, localnet, mask); 2693340d773SGleb Smirnoff init_checksum(); 2703340d773SGleb Smirnoff } 2713340d773SGleb Smirnoff 2723340d773SGleb Smirnoff if_printer 2733340d773SGleb Smirnoff lookup_printer(int type) 2743340d773SGleb Smirnoff { 2753340d773SGleb Smirnoff const struct printer *p; 2763340d773SGleb Smirnoff 2773340d773SGleb Smirnoff for (p = printers; p->f; ++p) 2783340d773SGleb Smirnoff if (type == p->type) 2793340d773SGleb Smirnoff return p->f; 2803340d773SGleb Smirnoff 2813340d773SGleb Smirnoff #if defined(DLT_USER2) && defined(DLT_PKTAP) 2823340d773SGleb Smirnoff /* 2833340d773SGleb Smirnoff * Apple incorrectly chose to use DLT_USER2 for their PKTAP 2843340d773SGleb Smirnoff * header. 2853340d773SGleb Smirnoff * 2863340d773SGleb Smirnoff * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin- 2873340d773SGleb Smirnoff * based OSes or the same value as LINKTYPE_PKTAP as it is on 2883340d773SGleb Smirnoff * other OSes, to LINKTYPE_PKTAP, so files written with 2893340d773SGleb Smirnoff * this version of libpcap for a DLT_PKTAP capture have a link- 2903340d773SGleb Smirnoff * layer header type of LINKTYPE_PKTAP. 2913340d773SGleb Smirnoff * 2923340d773SGleb Smirnoff * However, files written on OS X Mavericks for a DLT_PKTAP 2933340d773SGleb Smirnoff * capture have a link-layer header type of LINKTYPE_USER2. 2943340d773SGleb Smirnoff * If we don't have a printer for DLT_USER2, and type is 2953340d773SGleb Smirnoff * DLT_USER2, we look up the printer for DLT_PKTAP and use 2963340d773SGleb Smirnoff * that. 2973340d773SGleb Smirnoff */ 2983340d773SGleb Smirnoff if (type == DLT_USER2) { 2993340d773SGleb Smirnoff for (p = printers; p->f; ++p) 3003340d773SGleb Smirnoff if (DLT_PKTAP == p->type) 3013340d773SGleb Smirnoff return p->f; 3023340d773SGleb Smirnoff } 3033340d773SGleb Smirnoff #endif 3043340d773SGleb Smirnoff 3053340d773SGleb Smirnoff return NULL; 3063340d773SGleb Smirnoff /* NOTREACHED */ 3073340d773SGleb Smirnoff } 3083340d773SGleb Smirnoff 3093340d773SGleb Smirnoff int 3103340d773SGleb Smirnoff has_printer(int type) 3113340d773SGleb Smirnoff { 3123340d773SGleb Smirnoff return (lookup_printer(type) != NULL); 3133340d773SGleb Smirnoff } 3143340d773SGleb Smirnoff 3153340d773SGleb Smirnoff if_printer 316ee67461eSJoseph Mingrone get_if_printer(int type) 3173340d773SGleb Smirnoff { 3183340d773SGleb Smirnoff if_printer printer; 3193340d773SGleb Smirnoff 3203340d773SGleb Smirnoff printer = lookup_printer(type); 321ee67461eSJoseph Mingrone if (printer == NULL) 322ee67461eSJoseph Mingrone printer = unsupported_if_print; 3233340d773SGleb Smirnoff return printer; 3243340d773SGleb Smirnoff } 3253340d773SGleb Smirnoff 3263340d773SGleb Smirnoff void 3273340d773SGleb Smirnoff pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h, 3283340d773SGleb Smirnoff const u_char *sp, u_int packets_captured) 3293340d773SGleb Smirnoff { 330ee67461eSJoseph Mingrone u_int hdrlen = 0; 331ee67461eSJoseph Mingrone int invalid_header = 0; 3323340d773SGleb Smirnoff 3333340d773SGleb Smirnoff if (ndo->ndo_packet_number) 334ee67461eSJoseph Mingrone ND_PRINT("%5u ", packets_captured); 3353340d773SGleb Smirnoff 336ee67461eSJoseph Mingrone /* Sanity checks on packet length / capture length */ 337ee67461eSJoseph Mingrone if (h->caplen == 0) { 338ee67461eSJoseph Mingrone invalid_header = 1; 339ee67461eSJoseph Mingrone ND_PRINT("[Invalid header: caplen==0"); 340ee67461eSJoseph Mingrone } 341ee67461eSJoseph Mingrone if (h->len == 0) { 342ee67461eSJoseph Mingrone if (!invalid_header) { 343ee67461eSJoseph Mingrone invalid_header = 1; 344ee67461eSJoseph Mingrone ND_PRINT("[Invalid header:"); 345ee67461eSJoseph Mingrone } else 346ee67461eSJoseph Mingrone ND_PRINT(","); 347ee67461eSJoseph Mingrone ND_PRINT(" len==0"); 348ee67461eSJoseph Mingrone } else if (h->len < h->caplen) { 349ee67461eSJoseph Mingrone if (!invalid_header) { 350ee67461eSJoseph Mingrone invalid_header = 1; 351ee67461eSJoseph Mingrone ND_PRINT("[Invalid header:"); 352ee67461eSJoseph Mingrone } else 353ee67461eSJoseph Mingrone ND_PRINT(","); 354ee67461eSJoseph Mingrone ND_PRINT(" len(%u) < caplen(%u)", h->len, h->caplen); 355ee67461eSJoseph Mingrone } 356ee67461eSJoseph Mingrone if (h->caplen > MAXIMUM_SNAPLEN) { 357ee67461eSJoseph Mingrone if (!invalid_header) { 358ee67461eSJoseph Mingrone invalid_header = 1; 359ee67461eSJoseph Mingrone ND_PRINT("[Invalid header:"); 360ee67461eSJoseph Mingrone } else 361ee67461eSJoseph Mingrone ND_PRINT(","); 362ee67461eSJoseph Mingrone ND_PRINT(" caplen(%u) > %u", h->caplen, MAXIMUM_SNAPLEN); 363ee67461eSJoseph Mingrone } 364ee67461eSJoseph Mingrone if (h->len > MAXIMUM_SNAPLEN) { 365ee67461eSJoseph Mingrone if (!invalid_header) { 366ee67461eSJoseph Mingrone invalid_header = 1; 367ee67461eSJoseph Mingrone ND_PRINT("[Invalid header:"); 368ee67461eSJoseph Mingrone } else 369ee67461eSJoseph Mingrone ND_PRINT(","); 370ee67461eSJoseph Mingrone ND_PRINT(" len(%u) > %u", h->len, MAXIMUM_SNAPLEN); 371ee67461eSJoseph Mingrone } 372ee67461eSJoseph Mingrone if (invalid_header) { 373ee67461eSJoseph Mingrone ND_PRINT("]\n"); 374ee67461eSJoseph Mingrone return; 375ee67461eSJoseph Mingrone } 3763340d773SGleb Smirnoff 3773340d773SGleb Smirnoff /* 378ee67461eSJoseph Mingrone * At this point: 379ee67461eSJoseph Mingrone * capture length != 0, 380ee67461eSJoseph Mingrone * packet length != 0, 381ee67461eSJoseph Mingrone * capture length <= MAXIMUM_SNAPLEN, 382ee67461eSJoseph Mingrone * packet length <= MAXIMUM_SNAPLEN, 383ee67461eSJoseph Mingrone * packet length >= capture length. 384ee67461eSJoseph Mingrone * 385ee67461eSJoseph Mingrone * Currently, there is no D-Bus printer, thus no need for 386ee67461eSJoseph Mingrone * bigger lengths. 387ee67461eSJoseph Mingrone */ 388ee67461eSJoseph Mingrone 389ee67461eSJoseph Mingrone /* 390ee67461eSJoseph Mingrone * The header /usr/include/pcap/pcap.h in OpenBSD declares h->ts as 391ee67461eSJoseph Mingrone * struct bpf_timeval, not struct timeval. The former comes from 392ee67461eSJoseph Mingrone * /usr/include/net/bpf.h and uses 32-bit unsigned types instead of 393ee67461eSJoseph Mingrone * the types used in struct timeval. 394ee67461eSJoseph Mingrone */ 395ee67461eSJoseph Mingrone struct timeval tvbuf; 396ee67461eSJoseph Mingrone tvbuf.tv_sec = h->ts.tv_sec; 397ee67461eSJoseph Mingrone tvbuf.tv_usec = h->ts.tv_usec; 398ee67461eSJoseph Mingrone ts_print(ndo, &tvbuf); 399ee67461eSJoseph Mingrone 400ee67461eSJoseph Mingrone /* 401ee67461eSJoseph Mingrone * Printers must check that they're not walking off the end of 402ee67461eSJoseph Mingrone * the packet. 4033340d773SGleb Smirnoff * Rather than pass it all the way down, we set this member 4043340d773SGleb Smirnoff * of the netdissect_options structure. 4053340d773SGleb Smirnoff */ 4063340d773SGleb Smirnoff ndo->ndo_snapend = sp + h->caplen; 407ee67461eSJoseph Mingrone ndo->ndo_packetp = sp; 4083340d773SGleb Smirnoff 409ee67461eSJoseph Mingrone ndo->ndo_protocol = ""; 410ee67461eSJoseph Mingrone ndo->ndo_ll_hdr_len = 0; 411ee67461eSJoseph Mingrone switch (setjmp(ndo->ndo_early_end)) { 412ee67461eSJoseph Mingrone case 0: 413ee67461eSJoseph Mingrone /* Print the packet. */ 414ee67461eSJoseph Mingrone (ndo->ndo_if_printer)(ndo, h, sp); 415ee67461eSJoseph Mingrone break; 416ee67461eSJoseph Mingrone case ND_TRUNCATED: 417ee67461eSJoseph Mingrone /* A printer quit because the packet was truncated; report it */ 418ee67461eSJoseph Mingrone nd_print_trunc(ndo); 419ee67461eSJoseph Mingrone /* Print the full packet */ 420ee67461eSJoseph Mingrone ndo->ndo_ll_hdr_len = 0; 421ee67461eSJoseph Mingrone break; 422ee67461eSJoseph Mingrone } 423ee67461eSJoseph Mingrone hdrlen = ndo->ndo_ll_hdr_len; 424ee67461eSJoseph Mingrone 425ee67461eSJoseph Mingrone /* 426ee67461eSJoseph Mingrone * Empty the stack of packet information, freeing all pushed buffers; 427ee67461eSJoseph Mingrone * if we got here by a printer quitting, we need to release anything 428ee67461eSJoseph Mingrone * that didn't get released because we longjmped out of the code 429ee67461eSJoseph Mingrone * before it popped the packet information. 430ee67461eSJoseph Mingrone */ 431ee67461eSJoseph Mingrone nd_pop_all_packet_info(ndo); 4323340d773SGleb Smirnoff 4333340d773SGleb Smirnoff /* 4343340d773SGleb Smirnoff * Restore the original snapend, as a printer might have 4353340d773SGleb Smirnoff * changed it. 4363340d773SGleb Smirnoff */ 4373340d773SGleb Smirnoff ndo->ndo_snapend = sp + h->caplen; 4383340d773SGleb Smirnoff if (ndo->ndo_Xflag) { 4393340d773SGleb Smirnoff /* 4403340d773SGleb Smirnoff * Print the raw packet data in hex and ASCII. 4413340d773SGleb Smirnoff */ 4423340d773SGleb Smirnoff if (ndo->ndo_Xflag > 1) { 4433340d773SGleb Smirnoff /* 4443340d773SGleb Smirnoff * Include the link-layer header. 4453340d773SGleb Smirnoff */ 4463340d773SGleb Smirnoff hex_and_ascii_print(ndo, "\n\t", sp, h->caplen); 4473340d773SGleb Smirnoff } else { 4483340d773SGleb Smirnoff /* 4493340d773SGleb Smirnoff * Don't include the link-layer header - and if 4503340d773SGleb Smirnoff * we have nothing past the link-layer header, 4513340d773SGleb Smirnoff * print nothing. 4523340d773SGleb Smirnoff */ 4533340d773SGleb Smirnoff if (h->caplen > hdrlen) 4543340d773SGleb Smirnoff hex_and_ascii_print(ndo, "\n\t", sp + hdrlen, 4553340d773SGleb Smirnoff h->caplen - hdrlen); 4563340d773SGleb Smirnoff } 4573340d773SGleb Smirnoff } else if (ndo->ndo_xflag) { 4583340d773SGleb Smirnoff /* 4593340d773SGleb Smirnoff * Print the raw packet data in hex. 4603340d773SGleb Smirnoff */ 4613340d773SGleb Smirnoff if (ndo->ndo_xflag > 1) { 4623340d773SGleb Smirnoff /* 4633340d773SGleb Smirnoff * Include the link-layer header. 4643340d773SGleb Smirnoff */ 4653340d773SGleb Smirnoff hex_print(ndo, "\n\t", sp, h->caplen); 4663340d773SGleb Smirnoff } else { 4673340d773SGleb Smirnoff /* 4683340d773SGleb Smirnoff * Don't include the link-layer header - and if 4693340d773SGleb Smirnoff * we have nothing past the link-layer header, 4703340d773SGleb Smirnoff * print nothing. 4713340d773SGleb Smirnoff */ 4723340d773SGleb Smirnoff if (h->caplen > hdrlen) 4733340d773SGleb Smirnoff hex_print(ndo, "\n\t", sp + hdrlen, 4743340d773SGleb Smirnoff h->caplen - hdrlen); 4753340d773SGleb Smirnoff } 4763340d773SGleb Smirnoff } else if (ndo->ndo_Aflag) { 4773340d773SGleb Smirnoff /* 4783340d773SGleb Smirnoff * Print the raw packet data in ASCII. 4793340d773SGleb Smirnoff */ 4803340d773SGleb Smirnoff if (ndo->ndo_Aflag > 1) { 4813340d773SGleb Smirnoff /* 4823340d773SGleb Smirnoff * Include the link-layer header. 4833340d773SGleb Smirnoff */ 4843340d773SGleb Smirnoff ascii_print(ndo, sp, h->caplen); 4853340d773SGleb Smirnoff } else { 4863340d773SGleb Smirnoff /* 4873340d773SGleb Smirnoff * Don't include the link-layer header - and if 4883340d773SGleb Smirnoff * we have nothing past the link-layer header, 4893340d773SGleb Smirnoff * print nothing. 4903340d773SGleb Smirnoff */ 4913340d773SGleb Smirnoff if (h->caplen > hdrlen) 4923340d773SGleb Smirnoff ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen); 4933340d773SGleb Smirnoff } 4943340d773SGleb Smirnoff } 4953340d773SGleb Smirnoff 496ee67461eSJoseph Mingrone ND_PRINT("\n"); 497ee67461eSJoseph Mingrone nd_free_all(ndo); 4983340d773SGleb Smirnoff } 4993340d773SGleb Smirnoff 5003340d773SGleb Smirnoff /* 5013340d773SGleb Smirnoff * By default, print the specified data out in hex and ASCII. 5023340d773SGleb Smirnoff */ 5033340d773SGleb Smirnoff static void 5043340d773SGleb Smirnoff ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length) 5053340d773SGleb Smirnoff { 5063340d773SGleb Smirnoff hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */ 5073340d773SGleb Smirnoff } 5083340d773SGleb Smirnoff 5093340d773SGleb Smirnoff /* VARARGS */ 5103340d773SGleb Smirnoff static void 511ee67461eSJoseph Mingrone ndo_error(netdissect_options *ndo, status_exit_codes_t status, 512ee67461eSJoseph Mingrone const char *fmt, ...) 5133340d773SGleb Smirnoff { 5143340d773SGleb Smirnoff va_list ap; 5153340d773SGleb Smirnoff 5163340d773SGleb Smirnoff if (ndo->program_name) 5173340d773SGleb Smirnoff (void)fprintf(stderr, "%s: ", ndo->program_name); 5183340d773SGleb Smirnoff va_start(ap, fmt); 5193340d773SGleb Smirnoff (void)vfprintf(stderr, fmt, ap); 5203340d773SGleb Smirnoff va_end(ap); 5213340d773SGleb Smirnoff if (*fmt) { 5223340d773SGleb Smirnoff fmt += strlen(fmt); 5233340d773SGleb Smirnoff if (fmt[-1] != '\n') 5243340d773SGleb Smirnoff (void)fputc('\n', stderr); 5253340d773SGleb Smirnoff } 5263340d773SGleb Smirnoff nd_cleanup(); 527ee67461eSJoseph Mingrone exit(status); 5283340d773SGleb Smirnoff /* NOTREACHED */ 5293340d773SGleb Smirnoff } 5303340d773SGleb Smirnoff 5313340d773SGleb Smirnoff /* VARARGS */ 5323340d773SGleb Smirnoff static void 5333340d773SGleb Smirnoff ndo_warning(netdissect_options *ndo, const char *fmt, ...) 5343340d773SGleb Smirnoff { 5353340d773SGleb Smirnoff va_list ap; 5363340d773SGleb Smirnoff 5373340d773SGleb Smirnoff if (ndo->program_name) 5383340d773SGleb Smirnoff (void)fprintf(stderr, "%s: ", ndo->program_name); 5393340d773SGleb Smirnoff (void)fprintf(stderr, "WARNING: "); 5403340d773SGleb Smirnoff va_start(ap, fmt); 5413340d773SGleb Smirnoff (void)vfprintf(stderr, fmt, ap); 5423340d773SGleb Smirnoff va_end(ap); 5433340d773SGleb Smirnoff if (*fmt) { 5443340d773SGleb Smirnoff fmt += strlen(fmt); 5453340d773SGleb Smirnoff if (fmt[-1] != '\n') 5463340d773SGleb Smirnoff (void)fputc('\n', stderr); 5473340d773SGleb Smirnoff } 5483340d773SGleb Smirnoff } 5493340d773SGleb Smirnoff 5503340d773SGleb Smirnoff static int 5513340d773SGleb Smirnoff ndo_printf(netdissect_options *ndo, const char *fmt, ...) 5523340d773SGleb Smirnoff { 5533340d773SGleb Smirnoff va_list args; 5543340d773SGleb Smirnoff int ret; 5553340d773SGleb Smirnoff 5563340d773SGleb Smirnoff va_start(args, fmt); 5573340d773SGleb Smirnoff ret = vfprintf(stdout, fmt, args); 5583340d773SGleb Smirnoff va_end(args); 5593340d773SGleb Smirnoff 5603340d773SGleb Smirnoff if (ret < 0) 561ee67461eSJoseph Mingrone ndo_error(ndo, S_ERR_ND_WRITE_FILE, 562ee67461eSJoseph Mingrone "Unable to write output: %s", pcap_strerror(errno)); 5633340d773SGleb Smirnoff return (ret); 5643340d773SGleb Smirnoff } 5653340d773SGleb Smirnoff 5663340d773SGleb Smirnoff void 5673340d773SGleb Smirnoff ndo_set_function_pointers(netdissect_options *ndo) 5683340d773SGleb Smirnoff { 5693340d773SGleb Smirnoff ndo->ndo_default_print=ndo_default_print; 5703340d773SGleb Smirnoff ndo->ndo_printf=ndo_printf; 5713340d773SGleb Smirnoff ndo->ndo_error=ndo_error; 5723340d773SGleb Smirnoff ndo->ndo_warning=ndo_warning; 5733340d773SGleb Smirnoff } 574