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.
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
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.
21 * Support for splitting captures into multiple files with a maximum
25 * Seth Webster <swebster@sst.ll.mit.edu>
34 #include "netdissect-stdinc.h"
36 #include "netdissect.h"
37 #include "addrtoname.h"
39 #include "netdissect-alloc.h"
41 #include "pcap-missing.h"
48 static const struct printer printers
[] = {
49 #ifdef DLT_APPLE_IP_OVER_IEEE1394
50 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
52 { arcnet_if_print
, DLT_ARCNET
},
53 #ifdef DLT_ARCNET_LINUX
54 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
56 { atm_if_print
, DLT_ATM_RFC1483
},
57 #ifdef DLT_DSA_TAG_BRCM
58 { brcm_tag_if_print
, DLT_DSA_TAG_BRCM
},
60 #ifdef DLT_DSA_TAG_BRCM_PREPEND
61 { brcm_tag_prepend_if_print
, DLT_DSA_TAG_BRCM_PREPEND
},
63 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
64 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
67 { chdlc_if_print
, DLT_C_HDLC
},
70 { chdlc_if_print
, DLT_HDLC
},
73 { cip_if_print
, DLT_ATM_CLIP
},
76 { cip_if_print
, DLT_CIP
},
78 #ifdef DLT_DSA_TAG_DSA
79 { dsa_if_print
, DLT_DSA_TAG_DSA
},
81 #ifdef DLT_DSA_TAG_EDSA
82 { edsa_if_print
, DLT_DSA_TAG_EDSA
},
85 { enc_if_print
, DLT_ENC
},
87 { ether_if_print
, DLT_EN10MB
},
88 { fddi_if_print
, DLT_FDDI
},
90 { fr_if_print
, DLT_FR
},
93 { fr_if_print
, DLT_FRELAY
},
96 { ieee802_11_if_print
, DLT_IEEE802_11
},
98 #ifdef DLT_IEEE802_11_RADIO_AVS
99 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
101 #ifdef DLT_IEEE802_11_RADIO
102 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
104 #ifdef DLT_IEEE802_15_4
105 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
107 #ifdef DLT_IEEE802_15_4_NOFCS
108 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
110 #ifdef DLT_IEEE802_15_4_TAP
111 { ieee802_15_4_tap_if_print
, DLT_IEEE802_15_4_TAP
},
113 #ifdef DLT_IP_OVER_FC
114 { ipfc_if_print
, DLT_IP_OVER_FC
},
117 { ipnet_if_print
, DLT_IPNET
},
120 { ipoib_if_print
, DLT_IPOIB
},
122 #ifdef DLT_JUNIPER_ATM1
123 { juniper_atm1_if_print
, DLT_JUNIPER_ATM1
},
125 #ifdef DLT_JUNIPER_ATM2
126 { juniper_atm2_if_print
, DLT_JUNIPER_ATM2
},
128 #ifdef DLT_JUNIPER_CHDLC
129 { juniper_chdlc_if_print
, DLT_JUNIPER_CHDLC
},
131 #ifdef DLT_JUNIPER_ES
132 { juniper_es_if_print
, DLT_JUNIPER_ES
},
134 #ifdef DLT_JUNIPER_ETHER
135 { juniper_ether_if_print
, DLT_JUNIPER_ETHER
},
137 #ifdef DLT_JUNIPER_FRELAY
138 { juniper_frelay_if_print
, DLT_JUNIPER_FRELAY
},
140 #ifdef DLT_JUNIPER_GGSN
141 { juniper_ggsn_if_print
, DLT_JUNIPER_GGSN
},
143 #ifdef DLT_JUNIPER_MFR
144 { juniper_mfr_if_print
, DLT_JUNIPER_MFR
},
146 #ifdef DLT_JUNIPER_MLFR
147 { juniper_mlfr_if_print
, DLT_JUNIPER_MLFR
},
149 #ifdef DLT_JUNIPER_MLPPP
150 { juniper_mlppp_if_print
, DLT_JUNIPER_MLPPP
},
152 #ifdef DLT_JUNIPER_MONITOR
153 { juniper_monitor_if_print
, DLT_JUNIPER_MONITOR
},
155 #ifdef DLT_JUNIPER_PPP
156 { juniper_ppp_if_print
, DLT_JUNIPER_PPP
},
158 #ifdef DLT_JUNIPER_PPPOE_ATM
159 { juniper_pppoe_atm_if_print
, DLT_JUNIPER_PPPOE_ATM
},
161 #ifdef DLT_JUNIPER_PPPOE
162 { juniper_pppoe_if_print
, DLT_JUNIPER_PPPOE
},
164 #ifdef DLT_JUNIPER_SERVICES
165 { juniper_services_if_print
, DLT_JUNIPER_SERVICES
},
168 { ltalk_if_print
, DLT_LTALK
},
171 { mfr_if_print
, DLT_MFR
},
173 #ifdef DLT_NETANALYZER
174 { netanalyzer_if_print
, DLT_NETANALYZER
},
176 #ifdef DLT_NETANALYZER_TRANSPARENT
177 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
180 { nflog_if_print
, DLT_NFLOG
},
182 { null_if_print
, DLT_NULL
},
184 { null_if_print
, DLT_LOOP
},
187 { pflog_if_print
, DLT_PFLOG
},
190 { pktap_if_print
, DLT_PKTAP
},
193 { ppi_if_print
, DLT_PPI
},
196 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
198 #ifdef DLT_PPP_SERIAL
199 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
201 { ppp_if_print
, DLT_PPP
},
203 { ppp_if_print
, DLT_PPP_PPPD
},
206 { pppoe_if_print
, DLT_PPP_ETHER
},
208 #ifdef DLT_PRISM_HEADER
209 { prism_if_print
, DLT_PRISM_HEADER
},
211 { raw_if_print
, DLT_RAW
},
213 { raw_if_print
, DLT_IPV4
},
216 { raw_if_print
, DLT_IPV6
},
218 #ifdef DLT_SLIP_BSDOS
219 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
221 { sl_if_print
, DLT_SLIP
},
223 { sll_if_print
, DLT_LINUX_SLL
},
225 #ifdef DLT_LINUX_SLL2
226 { sll2_if_print
, DLT_LINUX_SLL2
},
229 { sunatm_if_print
, DLT_SUNATM
},
231 #ifdef DLT_SYMANTEC_FIREWALL
232 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
234 { token_if_print
, DLT_IEEE802
},
236 { usb_linux_48_byte_if_print
, DLT_USB_LINUX
},
237 #endif /* DLT_USB_LINUX */
238 #ifdef DLT_USB_LINUX_MMAPPED
239 { usb_linux_64_byte_if_print
, DLT_USB_LINUX_MMAPPED
},
240 #endif /* DLT_USB_LINUX_MMAPPED */
242 { vsock_if_print
, DLT_VSOCK
},
247 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
250 static void NORETURN
ndo_error(netdissect_options
*ndo
,
251 status_exit_codes_t status
,
252 FORMAT_STRING(const char *fmt
), ...)
254 static void ndo_warning(netdissect_options
*ndo
,
255 FORMAT_STRING(const char *fmt
), ...)
258 static int ndo_printf(netdissect_options
*ndo
,
259 FORMAT_STRING(const char *fmt
), ...)
263 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
)
265 init_addrtoname(ndo
, localnet
, mask
);
269 lookup_printer(int type
)
271 const struct printer
*p
;
273 for (p
= printers
; p
->f
; ++p
)
277 #if defined(DLT_USER2) && defined(DLT_PKTAP)
279 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
282 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
283 * based OSes or the same value as LINKTYPE_PKTAP as it is on
284 * other OSes, to LINKTYPE_PKTAP, so files written with
285 * this version of libpcap for a DLT_PKTAP capture have a link-
286 * layer header type of LINKTYPE_PKTAP.
288 * However, files written on OS X Mavericks for a DLT_PKTAP
289 * capture have a link-layer header type of LINKTYPE_USER2.
290 * If we don't have a printer for DLT_USER2, and type is
291 * DLT_USER2, we look up the printer for DLT_PKTAP and use
294 if (type
== DLT_USER2
) {
295 for (p
= printers
; p
->f
; ++p
)
296 if (DLT_PKTAP
== p
->type
)
306 has_printer(int type
)
308 return (lookup_printer(type
) != NULL
);
312 get_if_printer(int type
)
316 printer
= lookup_printer(type
);
318 printer
= unsupported_if_print
;
322 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
323 extern int profile_func_level
;
324 static int pretty_print_packet_level
= -1;
328 pretty_print_packet(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
329 const u_char
*sp
, u_int packets_captured
)
332 int invalid_header
= 0;
334 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
335 if (pretty_print_packet_level
== -1)
336 pretty_print_packet_level
= profile_func_level
;
339 if (ndo
->ndo_packet_number
)
340 ND_PRINT("%5u ", packets_captured
);
342 /* Sanity checks on packet length / capture length */
343 if (h
->caplen
== 0) {
345 ND_PRINT("[Invalid header: caplen==0");
348 if (!invalid_header
) {
350 ND_PRINT("[Invalid header:");
354 } else if (h
->len
< h
->caplen
) {
355 if (!invalid_header
) {
357 ND_PRINT("[Invalid header:");
360 ND_PRINT(" len(%u) < caplen(%u)", h
->len
, h
->caplen
);
362 if (h
->caplen
> MAXIMUM_SNAPLEN
) {
363 if (!invalid_header
) {
365 ND_PRINT("[Invalid header:");
368 ND_PRINT(" caplen(%u) > %u", h
->caplen
, MAXIMUM_SNAPLEN
);
370 if (h
->len
> MAXIMUM_SNAPLEN
) {
371 if (!invalid_header
) {
373 ND_PRINT("[Invalid header:");
376 ND_PRINT(" len(%u) > %u", h
->len
, MAXIMUM_SNAPLEN
);
378 if (invalid_header
) {
385 * capture length != 0,
386 * packet length != 0,
387 * capture length <= MAXIMUM_SNAPLEN,
388 * packet length <= MAXIMUM_SNAPLEN,
389 * packet length >= capture length.
391 * Currently, there is no D-Bus printer, thus no need for
396 * The header /usr/include/pcap/pcap.h in OpenBSD declares h->ts as
397 * struct bpf_timeval, not struct timeval. The former comes from
398 * /usr/include/net/bpf.h and uses 32-bit unsigned types instead of
399 * the types used in struct timeval.
401 struct timeval tvbuf
;
402 tvbuf
.tv_sec
= h
->ts
.tv_sec
;
403 tvbuf
.tv_usec
= h
->ts
.tv_usec
;
404 ts_print(ndo
, &tvbuf
);
407 * Printers must check that they're not walking off the end of
409 * Rather than pass it all the way down, we set this member
410 * of the netdissect_options structure.
412 ndo
->ndo_snapend
= sp
+ h
->caplen
;
413 ndo
->ndo_packetp
= sp
;
415 ndo
->ndo_protocol
= "";
416 ndo
->ndo_ll_hdr_len
= 0;
417 switch (setjmp(ndo
->ndo_early_end
)) {
419 /* Print the packet. */
420 (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
423 /* A printer quit because the packet was truncated; report it */
425 /* Print the full packet */
426 ndo
->ndo_ll_hdr_len
= 0;
427 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
428 /* truncation => reassignment */
429 profile_func_level
= pretty_print_packet_level
;
433 hdrlen
= ndo
->ndo_ll_hdr_len
;
436 * Empty the stack of packet information, freeing all pushed buffers;
437 * if we got here by a printer quitting, we need to release anything
438 * that didn't get released because we longjmped out of the code
439 * before it popped the packet information.
441 nd_pop_all_packet_info(ndo
);
444 * Restore the originals snapend and packetp, as a printer
445 * might have changed them.
447 * XXX - nd_pop_all_packet_info() should have restored the
448 * original values, but, just in case....
450 ndo
->ndo_snapend
= sp
+ h
->caplen
;
451 ndo
->ndo_packetp
= sp
;
452 if (ndo
->ndo_Xflag
) {
454 * Print the raw packet data in hex and ASCII.
456 if (ndo
->ndo_Xflag
> 1) {
458 * Include the link-layer header.
460 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
463 * Don't include the link-layer header - and if
464 * we have nothing past the link-layer header,
467 if (h
->caplen
> hdrlen
)
468 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
471 } else if (ndo
->ndo_xflag
) {
473 * Print the raw packet data in hex.
475 if (ndo
->ndo_xflag
> 1) {
477 * Include the link-layer header.
479 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
482 * Don't include the link-layer header - and if
483 * we have nothing past the link-layer header,
486 if (h
->caplen
> hdrlen
)
487 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
490 } else if (ndo
->ndo_Aflag
) {
492 * Print the raw packet data in ASCII.
494 if (ndo
->ndo_Aflag
> 1) {
496 * Include the link-layer header.
498 ascii_print(ndo
, sp
, h
->caplen
);
501 * Don't include the link-layer header - and if
502 * we have nothing past the link-layer header,
505 if (h
->caplen
> hdrlen
)
506 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
515 * By default, print the specified data out in hex and ASCII.
518 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
520 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
525 ndo_error(netdissect_options
*ndo
, status_exit_codes_t status
,
526 const char *fmt
, ...)
530 if (ndo
->program_name
)
531 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
533 (void)vfprintf(stderr
, fmt
, ap
);
538 (void)fputc('\n', stderr
);
547 ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
551 if (ndo
->program_name
)
552 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
553 (void)fprintf(stderr
, "WARNING: ");
555 (void)vfprintf(stderr
, fmt
, ap
);
560 (void)fputc('\n', stderr
);
565 ndo_printf(netdissect_options
*ndo
, const char *fmt
, ...)
571 ret
= vfprintf(stdout
, fmt
, args
);
575 ndo_error(ndo
, S_ERR_ND_WRITE_FILE
,
576 "Unable to write output: %s", pcap_strerror(errno
));
581 ndo_set_function_pointers(netdissect_options
*ndo
)
583 ndo
->ndo_default_print
=ndo_default_print
;
584 ndo
->ndo_printf
=ndo_printf
;
585 ndo
->ndo_error
=ndo_error
;
586 ndo
->ndo_warning
=ndo_warning
;