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
},
195 #ifdef DLT_PPP_SERIAL
196 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
198 { ppp_if_print
, DLT_PPP
},
200 { ppp_if_print
, DLT_PPP_PPPD
},
203 { pppoe_if_print
, DLT_PPP_ETHER
},
205 #ifdef DLT_PRISM_HEADER
206 { prism_if_print
, DLT_PRISM_HEADER
},
208 { raw_if_print
, DLT_RAW
},
210 { raw_if_print
, DLT_IPV4
},
213 { raw_if_print
, DLT_IPV6
},
215 #ifdef DLT_SLIP_BSDOS
216 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
218 { sl_if_print
, DLT_SLIP
},
220 { sll_if_print
, DLT_LINUX_SLL
},
222 #ifdef DLT_LINUX_SLL2
223 { sll2_if_print
, DLT_LINUX_SLL2
},
226 { sunatm_if_print
, DLT_SUNATM
},
228 #ifdef DLT_SYMANTEC_FIREWALL
229 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
231 { token_if_print
, DLT_IEEE802
},
233 { usb_linux_48_byte_if_print
, DLT_USB_LINUX
},
234 #endif /* DLT_USB_LINUX */
235 #ifdef DLT_USB_LINUX_MMAPPED
236 { usb_linux_64_byte_if_print
, DLT_USB_LINUX_MMAPPED
},
237 #endif /* DLT_USB_LINUX_MMAPPED */
239 { vsock_if_print
, DLT_VSOCK
},
245 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
)
247 init_addrtoname(ndo
, localnet
, mask
);
251 lookup_printer(int type
)
253 const struct printer
*p
;
255 for (p
= printers
; p
->f
; ++p
)
259 #if defined(DLT_USER2) && defined(DLT_PKTAP)
261 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
264 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
265 * based OSes or the same value as LINKTYPE_PKTAP as it is on
266 * other OSes, to LINKTYPE_PKTAP, so files written with
267 * this version of libpcap for a DLT_PKTAP capture have a link-
268 * layer header type of LINKTYPE_PKTAP.
270 * However, files written on OS X Mavericks for a DLT_PKTAP
271 * capture have a link-layer header type of LINKTYPE_USER2.
272 * If we don't have a printer for DLT_USER2, and type is
273 * DLT_USER2, we look up the printer for DLT_PKTAP and use
276 if (type
== DLT_USER2
) {
277 for (p
= printers
; p
->f
; ++p
)
278 if (DLT_PKTAP
== p
->type
)
288 has_printer(int type
)
290 return (lookup_printer(type
) != NULL
);
294 get_if_printer(int type
)
298 printer
= lookup_printer(type
);
300 printer
= unsupported_if_print
;
304 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
305 extern int profile_func_level
;
306 static int pretty_print_packet_level
= -1;
310 pretty_print_packet(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
311 const u_char
*sp
, u_int packets_captured
)
314 int invalid_header
= 0;
316 if (ndo
->ndo_print_sampling
&& packets_captured
% ndo
->ndo_print_sampling
!= 0)
319 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
320 if (pretty_print_packet_level
== -1)
321 pretty_print_packet_level
= profile_func_level
;
324 if (ndo
->ndo_packet_number
)
325 ND_PRINT("%5u ", packets_captured
);
327 if (ndo
->ndo_lengths
)
328 ND_PRINT("caplen %u len %u ", h
->caplen
, h
->len
);
330 /* Sanity checks on packet length / capture length */
331 if (h
->caplen
== 0) {
333 ND_PRINT("[Invalid header: caplen==0");
336 if (!invalid_header
) {
338 ND_PRINT("[Invalid header:");
342 } else if (h
->len
< h
->caplen
) {
343 if (!invalid_header
) {
345 ND_PRINT("[Invalid header:");
348 ND_PRINT(" len(%u) < caplen(%u)", h
->len
, h
->caplen
);
350 if (h
->caplen
> MAXIMUM_SNAPLEN
) {
351 if (!invalid_header
) {
353 ND_PRINT("[Invalid header:");
356 ND_PRINT(" caplen(%u) > %u", h
->caplen
, MAXIMUM_SNAPLEN
);
358 if (h
->len
> MAXIMUM_SNAPLEN
) {
359 if (!invalid_header
) {
361 ND_PRINT("[Invalid header:");
364 ND_PRINT(" len(%u) > %u", h
->len
, MAXIMUM_SNAPLEN
);
366 if (invalid_header
) {
373 * capture length != 0,
374 * packet length != 0,
375 * capture length <= MAXIMUM_SNAPLEN,
376 * packet length <= MAXIMUM_SNAPLEN,
377 * packet length >= capture length.
379 * Currently, there is no D-Bus printer, thus no need for
384 * The header /usr/include/pcap/pcap.h in OpenBSD declares h->ts as
385 * struct bpf_timeval, not struct timeval. The former comes from
386 * /usr/include/net/bpf.h and uses 32-bit unsigned types instead of
387 * the types used in struct timeval.
389 struct timeval tvbuf
;
390 tvbuf
.tv_sec
= h
->ts
.tv_sec
;
391 tvbuf
.tv_usec
= h
->ts
.tv_usec
;
392 ts_print(ndo
, &tvbuf
);
395 * Printers must check that they're not walking off the end of
397 * Rather than pass it all the way down, we set this member
398 * of the netdissect_options structure.
400 ndo
->ndo_snapend
= sp
+ h
->caplen
;
401 ndo
->ndo_packetp
= sp
;
403 ndo
->ndo_protocol
= "";
404 ndo
->ndo_ll_hdr_len
= 0;
405 switch (setjmp(ndo
->ndo_early_end
)) {
407 /* Print the packet. */
408 (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
411 /* A printer quit because the packet was truncated; report it */
413 /* Print the full packet */
414 ndo
->ndo_ll_hdr_len
= 0;
415 #ifdef ENABLE_INSTRUMENT_FUNCTIONS
416 /* truncation => reassignment */
417 profile_func_level
= pretty_print_packet_level
;
421 hdrlen
= ndo
->ndo_ll_hdr_len
;
424 * Empty the stack of packet information, freeing all pushed buffers;
425 * if we got here by a printer quitting, we need to release anything
426 * that didn't get released because we longjmped out of the code
427 * before it popped the packet information.
429 nd_pop_all_packet_info(ndo
);
432 * Restore the originals snapend and packetp, as a printer
433 * might have changed them.
435 * XXX - nd_pop_all_packet_info() should have restored the
436 * original values, but, just in case....
438 ndo
->ndo_snapend
= sp
+ h
->caplen
;
439 ndo
->ndo_packetp
= sp
;
440 if (ndo
->ndo_Xflag
) {
442 * Print the raw packet data in hex and ASCII.
444 if (ndo
->ndo_Xflag
> 1) {
446 * Include the link-layer header.
448 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
451 * Don't include the link-layer header - and if
452 * we have nothing past the link-layer header,
455 if (h
->caplen
> hdrlen
)
456 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
459 } else if (ndo
->ndo_xflag
) {
461 * Print the raw packet data in hex.
463 if (ndo
->ndo_xflag
> 1) {
465 * Include the link-layer header.
467 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
470 * Don't include the link-layer header - and if
471 * we have nothing past the link-layer header,
474 if (h
->caplen
> hdrlen
)
475 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
478 } else if (ndo
->ndo_Aflag
) {
480 * Print the raw packet data in ASCII.
482 if (ndo
->ndo_Aflag
> 1) {
484 * Include the link-layer header.
486 ascii_print(ndo
, sp
, h
->caplen
);
489 * Don't include the link-layer header - and if
490 * we have nothing past the link-layer header,
493 if (h
->caplen
> hdrlen
)
494 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
503 * By default, print the specified data out in hex and ASCII.
506 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
508 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
512 static void NORETURN
PRINTFLIKE(3, 4)
513 ndo_error(netdissect_options
*ndo
, status_exit_codes_t status
,
514 FORMAT_STRING(const char *fmt
), ...)
518 if (ndo
->program_name
)
519 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
521 (void)vfprintf(stderr
, fmt
, ap
);
526 (void)fputc('\n', stderr
);
534 static void PRINTFLIKE(2, 3)
535 ndo_warning(netdissect_options
*ndo
, FORMAT_STRING(const char *fmt
), ...)
539 if (ndo
->program_name
)
540 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
541 (void)fprintf(stderr
, "WARNING: ");
543 (void)vfprintf(stderr
, fmt
, ap
);
548 (void)fputc('\n', stderr
);
553 static int PRINTFLIKE(2, 3)
554 ndo_printf(netdissect_options
*ndo
, FORMAT_STRING(const char *fmt
), ...)
560 ret
= vfprintf(stdout
, fmt
, args
);
564 ndo_error(ndo
, S_ERR_ND_WRITE_FILE
,
565 "Unable to write output: %s", pcap_strerror(errno
));
570 ndo_set_function_pointers(netdissect_options
*ndo
)
572 ndo
->ndo_default_print
=ndo_default_print
;
573 ndo
->ndo_printf
=ndo_printf
;
574 ndo
->ndo_error
=ndo_error
;
575 ndo
->ndo_warning
=ndo_warning
;