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>
36 #include "netdissect-stdinc.h"
38 #include "netdissect.h"
39 #include "addrtoname.h"
41 #include "netdissect-alloc.h"
43 #include "pcap-missing.h"
50 static const struct printer printers
[] = {
51 #ifdef DLT_APPLE_IP_OVER_IEEE1394
52 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
54 { arcnet_if_print
, DLT_ARCNET
},
55 #ifdef DLT_ARCNET_LINUX
56 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
58 { atm_if_print
, DLT_ATM_RFC1483
},
59 #ifdef DLT_DSA_TAG_BRCM
60 { brcm_tag_if_print
, DLT_DSA_TAG_BRCM
},
62 #ifdef DLT_DSA_TAG_BRCM_PREPEND
63 { brcm_tag_prepend_if_print
, DLT_DSA_TAG_BRCM_PREPEND
},
65 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
66 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
69 { chdlc_if_print
, DLT_C_HDLC
},
72 { chdlc_if_print
, DLT_HDLC
},
75 { cip_if_print
, DLT_ATM_CLIP
},
78 { cip_if_print
, DLT_CIP
},
80 #ifdef DLT_DSA_TAG_DSA
81 { dsa_if_print
, DLT_DSA_TAG_DSA
},
83 #ifdef DLT_DSA_TAG_EDSA
84 { edsa_if_print
, DLT_DSA_TAG_EDSA
},
87 { enc_if_print
, DLT_ENC
},
89 { ether_if_print
, DLT_EN10MB
},
90 { fddi_if_print
, DLT_FDDI
},
92 { fr_if_print
, DLT_FR
},
95 { fr_if_print
, DLT_FRELAY
},
98 { ieee802_11_if_print
, DLT_IEEE802_11
},
100 #ifdef DLT_IEEE802_11_RADIO_AVS
101 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
103 #ifdef DLT_IEEE802_11_RADIO
104 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
106 #ifdef DLT_IEEE802_15_4
107 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
109 #ifdef DLT_IEEE802_15_4_NOFCS
110 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
112 #ifdef DLT_IEEE802_15_4_TAP
113 { ieee802_15_4_tap_if_print
, DLT_IEEE802_15_4_TAP
},
115 #ifdef DLT_IP_OVER_FC
116 { ipfc_if_print
, DLT_IP_OVER_FC
},
119 { ipnet_if_print
, DLT_IPNET
},
122 { ipoib_if_print
, DLT_IPOIB
},
124 #ifdef DLT_JUNIPER_ATM1
125 { juniper_atm1_if_print
, DLT_JUNIPER_ATM1
},
127 #ifdef DLT_JUNIPER_ATM2
128 { juniper_atm2_if_print
, DLT_JUNIPER_ATM2
},
130 #ifdef DLT_JUNIPER_CHDLC
131 { juniper_chdlc_if_print
, DLT_JUNIPER_CHDLC
},
133 #ifdef DLT_JUNIPER_ES
134 { juniper_es_if_print
, DLT_JUNIPER_ES
},
136 #ifdef DLT_JUNIPER_ETHER
137 { juniper_ether_if_print
, DLT_JUNIPER_ETHER
},
139 #ifdef DLT_JUNIPER_FRELAY
140 { juniper_frelay_if_print
, DLT_JUNIPER_FRELAY
},
142 #ifdef DLT_JUNIPER_GGSN
143 { juniper_ggsn_if_print
, DLT_JUNIPER_GGSN
},
145 #ifdef DLT_JUNIPER_MFR
146 { juniper_mfr_if_print
, DLT_JUNIPER_MFR
},
148 #ifdef DLT_JUNIPER_MLFR
149 { juniper_mlfr_if_print
, DLT_JUNIPER_MLFR
},
151 #ifdef DLT_JUNIPER_MLPPP
152 { juniper_mlppp_if_print
, DLT_JUNIPER_MLPPP
},
154 #ifdef DLT_JUNIPER_MONITOR
155 { juniper_monitor_if_print
, DLT_JUNIPER_MONITOR
},
157 #ifdef DLT_JUNIPER_PPP
158 { juniper_ppp_if_print
, DLT_JUNIPER_PPP
},
160 #ifdef DLT_JUNIPER_PPPOE_ATM
161 { juniper_pppoe_atm_if_print
, DLT_JUNIPER_PPPOE_ATM
},
163 #ifdef DLT_JUNIPER_PPPOE
164 { juniper_pppoe_if_print
, DLT_JUNIPER_PPPOE
},
166 #ifdef DLT_JUNIPER_SERVICES
167 { juniper_services_if_print
, DLT_JUNIPER_SERVICES
},
170 { ltalk_if_print
, DLT_LTALK
},
173 { mfr_if_print
, DLT_MFR
},
175 #ifdef DLT_NETANALYZER
176 { netanalyzer_if_print
, DLT_NETANALYZER
},
178 #ifdef DLT_NETANALYZER_TRANSPARENT
179 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
182 { nflog_if_print
, DLT_NFLOG
},
184 { null_if_print
, DLT_NULL
},
186 { null_if_print
, DLT_LOOP
},
189 { pflog_if_print
, DLT_PFLOG
},
192 { pktap_if_print
, DLT_PKTAP
},
195 { ppi_if_print
, DLT_PPI
},
198 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
200 #ifdef DLT_PPP_SERIAL
201 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
203 { ppp_if_print
, DLT_PPP
},
205 { ppp_if_print
, DLT_PPP_PPPD
},
208 { pppoe_if_print
, DLT_PPP_ETHER
},
210 #ifdef DLT_PRISM_HEADER
211 { prism_if_print
, DLT_PRISM_HEADER
},
213 { raw_if_print
, DLT_RAW
},
215 { raw_if_print
, DLT_IPV4
},
218 { raw_if_print
, DLT_IPV6
},
220 #ifdef DLT_SLIP_BSDOS
221 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
223 { sl_if_print
, DLT_SLIP
},
225 { sll_if_print
, DLT_LINUX_SLL
},
227 #ifdef DLT_LINUX_SLL2
228 { sll2_if_print
, DLT_LINUX_SLL2
},
231 { sunatm_if_print
, DLT_SUNATM
},
233 #ifdef DLT_SYMANTEC_FIREWALL
234 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
236 { token_if_print
, DLT_IEEE802
},
238 { usb_linux_48_byte_if_print
, DLT_USB_LINUX
},
239 #endif /* DLT_USB_LINUX */
240 #ifdef DLT_USB_LINUX_MMAPPED
241 { usb_linux_64_byte_if_print
, DLT_USB_LINUX_MMAPPED
},
242 #endif /* DLT_USB_LINUX_MMAPPED */
244 { vsock_if_print
, DLT_VSOCK
},
249 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
252 static void NORETURN
ndo_error(netdissect_options
*ndo
,
253 status_exit_codes_t status
,
254 FORMAT_STRING(const char *fmt
), ...)
256 static void ndo_warning(netdissect_options
*ndo
,
257 FORMAT_STRING(const char *fmt
), ...)
260 static int ndo_printf(netdissect_options
*ndo
,
261 FORMAT_STRING(const char *fmt
), ...)
265 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
)
267 init_addrtoname(ndo
, localnet
, mask
);
271 lookup_printer(int type
)
273 const struct printer
*p
;
275 for (p
= printers
; p
->f
; ++p
)
279 #if defined(DLT_USER2) && defined(DLT_PKTAP)
281 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
284 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
285 * based OSes or the same value as LINKTYPE_PKTAP as it is on
286 * other OSes, to LINKTYPE_PKTAP, so files written with
287 * this version of libpcap for a DLT_PKTAP capture have a link-
288 * layer header type of LINKTYPE_PKTAP.
290 * However, files written on OS X Mavericks for a DLT_PKTAP
291 * capture have a link-layer header type of LINKTYPE_USER2.
292 * If we don't have a printer for DLT_USER2, and type is
293 * DLT_USER2, we look up the printer for DLT_PKTAP and use
296 if (type
== DLT_USER2
) {
297 for (p
= printers
; p
->f
; ++p
)
298 if (DLT_PKTAP
== p
->type
)
308 has_printer(int type
)
310 return (lookup_printer(type
) != NULL
);
314 get_if_printer(int type
)
318 printer
= lookup_printer(type
);
320 printer
= unsupported_if_print
;
324 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
325 extern int profile_func_level
;
326 static int pretty_print_packet_level
= -1;
330 pretty_print_packet(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
331 const u_char
*sp
, u_int packets_captured
)
334 int invalid_header
= 0;
336 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
337 if (pretty_print_packet_level
== -1)
338 pretty_print_packet_level
= profile_func_level
;
341 if (ndo
->ndo_packet_number
)
342 ND_PRINT("%5u ", packets_captured
);
344 /* Sanity checks on packet length / capture length */
345 if (h
->caplen
== 0) {
347 ND_PRINT("[Invalid header: caplen==0");
350 if (!invalid_header
) {
352 ND_PRINT("[Invalid header:");
356 } else if (h
->len
< h
->caplen
) {
357 if (!invalid_header
) {
359 ND_PRINT("[Invalid header:");
362 ND_PRINT(" len(%u) < caplen(%u)", h
->len
, h
->caplen
);
364 if (h
->caplen
> MAXIMUM_SNAPLEN
) {
365 if (!invalid_header
) {
367 ND_PRINT("[Invalid header:");
370 ND_PRINT(" caplen(%u) > %u", h
->caplen
, MAXIMUM_SNAPLEN
);
372 if (h
->len
> MAXIMUM_SNAPLEN
) {
373 if (!invalid_header
) {
375 ND_PRINT("[Invalid header:");
378 ND_PRINT(" len(%u) > %u", h
->len
, MAXIMUM_SNAPLEN
);
380 if (invalid_header
) {
387 * capture length != 0,
388 * packet length != 0,
389 * capture length <= MAXIMUM_SNAPLEN,
390 * packet length <= MAXIMUM_SNAPLEN,
391 * packet length >= capture length.
393 * Currently, there is no D-Bus printer, thus no need for
398 * The header /usr/include/pcap/pcap.h in OpenBSD declares h->ts as
399 * struct bpf_timeval, not struct timeval. The former comes from
400 * /usr/include/net/bpf.h and uses 32-bit unsigned types instead of
401 * the types used in struct timeval.
403 struct timeval tvbuf
;
404 tvbuf
.tv_sec
= h
->ts
.tv_sec
;
405 tvbuf
.tv_usec
= h
->ts
.tv_usec
;
406 ts_print(ndo
, &tvbuf
);
409 * Printers must check that they're not walking off the end of
411 * Rather than pass it all the way down, we set this member
412 * of the netdissect_options structure.
414 ndo
->ndo_snapend
= sp
+ h
->caplen
;
415 ndo
->ndo_packetp
= sp
;
417 ndo
->ndo_protocol
= "";
418 ndo
->ndo_ll_hdr_len
= 0;
419 switch (setjmp(ndo
->ndo_early_end
)) {
421 /* Print the packet. */
422 (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
425 /* A printer quit because the packet was truncated; report it */
427 /* Print the full packet */
428 ndo
->ndo_ll_hdr_len
= 0;
429 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
430 /* truncation => reassignment */
431 profile_func_level
= pretty_print_packet_level
;
435 hdrlen
= ndo
->ndo_ll_hdr_len
;
438 * Empty the stack of packet information, freeing all pushed buffers;
439 * if we got here by a printer quitting, we need to release anything
440 * that didn't get released because we longjmped out of the code
441 * before it popped the packet information.
443 nd_pop_all_packet_info(ndo
);
446 * Restore the originals snapend and packetp, as a printer
447 * might have changed them.
449 * XXX - nd_pop_all_packet_info() should have restored the
450 * original values, but, just in case....
452 ndo
->ndo_snapend
= sp
+ h
->caplen
;
453 ndo
->ndo_packetp
= sp
;
454 if (ndo
->ndo_Xflag
) {
456 * Print the raw packet data in hex and ASCII.
458 if (ndo
->ndo_Xflag
> 1) {
460 * Include the link-layer header.
462 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
465 * Don't include the link-layer header - and if
466 * we have nothing past the link-layer header,
469 if (h
->caplen
> hdrlen
)
470 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
473 } else if (ndo
->ndo_xflag
) {
475 * Print the raw packet data in hex.
477 if (ndo
->ndo_xflag
> 1) {
479 * Include the link-layer header.
481 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
484 * Don't include the link-layer header - and if
485 * we have nothing past the link-layer header,
488 if (h
->caplen
> hdrlen
)
489 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
492 } else if (ndo
->ndo_Aflag
) {
494 * Print the raw packet data in ASCII.
496 if (ndo
->ndo_Aflag
> 1) {
498 * Include the link-layer header.
500 ascii_print(ndo
, sp
, h
->caplen
);
503 * Don't include the link-layer header - and if
504 * we have nothing past the link-layer header,
507 if (h
->caplen
> hdrlen
)
508 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
517 * By default, print the specified data out in hex and ASCII.
520 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
522 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
527 ndo_error(netdissect_options
*ndo
, status_exit_codes_t status
,
528 const char *fmt
, ...)
532 if (ndo
->program_name
)
533 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
535 (void)vfprintf(stderr
, fmt
, ap
);
540 (void)fputc('\n', stderr
);
549 ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
553 if (ndo
->program_name
)
554 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
555 (void)fprintf(stderr
, "WARNING: ");
557 (void)vfprintf(stderr
, fmt
, ap
);
562 (void)fputc('\n', stderr
);
567 ndo_printf(netdissect_options
*ndo
, const char *fmt
, ...)
573 ret
= vfprintf(stdout
, fmt
, args
);
577 ndo_error(ndo
, S_ERR_ND_WRITE_FILE
,
578 "Unable to write output: %s", pcap_strerror(errno
));
583 ndo_set_function_pointers(netdissect_options
*ndo
)
585 ndo
->ndo_default_print
=ndo_default_print
;
586 ndo
->ndo_printf
=ndo_printf
;
587 ndo
->ndo_error
=ndo_error
;
588 ndo
->ndo_warning
=ndo_warning
;