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 { ether_if_print
, DLT_EN10MB
},
53 { ipnet_if_print
, DLT_IPNET
},
55 #ifdef DLT_IEEE802_15_4
56 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
58 #ifdef DLT_IEEE802_15_4_NOFCS
59 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
61 #ifdef DLT_IEEE802_15_4_TAP
62 { ieee802_15_4_tap_if_print
, DLT_IEEE802_15_4_TAP
},
65 { ppi_if_print
, DLT_PPI
},
67 #ifdef DLT_NETANALYZER
68 { netanalyzer_if_print
, DLT_NETANALYZER
},
70 #ifdef DLT_NETANALYZER_TRANSPARENT
71 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
74 { nflog_if_print
, DLT_NFLOG
},
77 { cip_if_print
, DLT_CIP
},
80 { cip_if_print
, DLT_ATM_CLIP
},
83 { ipfc_if_print
, DLT_IP_OVER_FC
},
85 { null_if_print
, DLT_NULL
},
87 { null_if_print
, DLT_LOOP
},
89 #ifdef DLT_APPLE_IP_OVER_IEEE1394
90 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
92 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
93 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
96 { lane_if_print
, DLT_LANE8023
},
98 { arcnet_if_print
, DLT_ARCNET
},
99 #ifdef DLT_ARCNET_LINUX
100 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
102 { raw_if_print
, DLT_RAW
},
104 { raw_if_print
, DLT_IPV4
},
107 { raw_if_print
, DLT_IPV6
},
110 { usb_linux_48_byte_if_print
, DLT_USB_LINUX
},
111 #endif /* DLT_USB_LINUX */
112 #ifdef DLT_USB_LINUX_MMAPPED
113 { usb_linux_64_byte_if_print
, DLT_USB_LINUX_MMAPPED
},
114 #endif /* DLT_USB_LINUX_MMAPPED */
115 #ifdef DLT_SYMANTEC_FIREWALL
116 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
119 { chdlc_if_print
, DLT_C_HDLC
},
122 { chdlc_if_print
, DLT_HDLC
},
125 { pppoe_if_print
, DLT_PPP_ETHER
},
127 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
128 { pflog_if_print
, DLT_PFLOG
},
130 { token_if_print
, DLT_IEEE802
},
131 { fddi_if_print
, DLT_FDDI
},
133 { sll_if_print
, DLT_LINUX_SLL
},
135 #ifdef DLT_LINUX_SLL2
136 { sll2_if_print
, DLT_LINUX_SLL2
},
139 { fr_if_print
, DLT_FR
},
142 { fr_if_print
, DLT_FRELAY
},
145 { mfr_if_print
, DLT_MFR
},
147 { atm_if_print
, DLT_ATM_RFC1483
},
149 { sunatm_if_print
, DLT_SUNATM
},
152 { enc_if_print
, DLT_ENC
},
154 { sl_if_print
, DLT_SLIP
},
155 #ifdef DLT_SLIP_BSDOS
156 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
159 { ltalk_if_print
, DLT_LTALK
},
161 #ifdef DLT_JUNIPER_ATM1
162 { juniper_atm1_if_print
, DLT_JUNIPER_ATM1
},
164 #ifdef DLT_JUNIPER_ATM2
165 { juniper_atm2_if_print
, DLT_JUNIPER_ATM2
},
167 #ifdef DLT_JUNIPER_MFR
168 { juniper_mfr_if_print
, DLT_JUNIPER_MFR
},
170 #ifdef DLT_JUNIPER_MLFR
171 { juniper_mlfr_if_print
, DLT_JUNIPER_MLFR
},
173 #ifdef DLT_JUNIPER_MLPPP
174 { juniper_mlppp_if_print
, DLT_JUNIPER_MLPPP
},
176 #ifdef DLT_JUNIPER_PPPOE
177 { juniper_pppoe_if_print
, DLT_JUNIPER_PPPOE
},
179 #ifdef DLT_JUNIPER_PPPOE_ATM
180 { juniper_pppoe_atm_if_print
, DLT_JUNIPER_PPPOE_ATM
},
182 #ifdef DLT_JUNIPER_GGSN
183 { juniper_ggsn_if_print
, DLT_JUNIPER_GGSN
},
185 #ifdef DLT_JUNIPER_ES
186 { juniper_es_if_print
, DLT_JUNIPER_ES
},
188 #ifdef DLT_JUNIPER_MONITOR
189 { juniper_monitor_if_print
, DLT_JUNIPER_MONITOR
},
191 #ifdef DLT_JUNIPER_SERVICES
192 { juniper_services_if_print
, DLT_JUNIPER_SERVICES
},
194 #ifdef DLT_JUNIPER_ETHER
195 { juniper_ether_if_print
, DLT_JUNIPER_ETHER
},
197 #ifdef DLT_JUNIPER_PPP
198 { juniper_ppp_if_print
, DLT_JUNIPER_PPP
},
200 #ifdef DLT_JUNIPER_FRELAY
201 { juniper_frelay_if_print
, DLT_JUNIPER_FRELAY
},
203 #ifdef DLT_JUNIPER_CHDLC
204 { juniper_chdlc_if_print
, DLT_JUNIPER_CHDLC
},
207 { pktap_if_print
, DLT_PKTAP
},
209 #ifdef DLT_IEEE802_11_RADIO
210 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
212 #ifdef DLT_IEEE802_11
213 { ieee802_11_if_print
, DLT_IEEE802_11
},
215 #ifdef DLT_IEEE802_11_RADIO_AVS
216 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
218 #ifdef DLT_PRISM_HEADER
219 { prism_if_print
, DLT_PRISM_HEADER
},
221 { ppp_if_print
, DLT_PPP
},
222 #ifdef DLT_PPP_WITHDIRECTION
223 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
226 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
228 #ifdef DLT_PPP_SERIAL
229 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
231 #ifdef DLT_DSA_TAG_BRCM
232 { brcm_tag_if_print
, DLT_DSA_TAG_BRCM
},
234 #ifdef DLT_DSA_TAG_BRCM_PREPEND
235 { brcm_tag_prepend_if_print
, DLT_DSA_TAG_BRCM_PREPEND
},
238 { vsock_if_print
, DLT_VSOCK
},
243 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
246 static void NORETURN
ndo_error(netdissect_options
*ndo
,
247 status_exit_codes_t status
,
248 FORMAT_STRING(const char *fmt
), ...)
250 static void ndo_warning(netdissect_options
*ndo
,
251 FORMAT_STRING(const char *fmt
), ...)
254 static int ndo_printf(netdissect_options
*ndo
,
255 FORMAT_STRING(const char *fmt
), ...)
259 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
)
262 init_addrtoname(ndo
, localnet
, mask
);
267 lookup_printer(int type
)
269 const struct printer
*p
;
271 for (p
= printers
; p
->f
; ++p
)
275 #if defined(DLT_USER2) && defined(DLT_PKTAP)
277 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
280 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
281 * based OSes or the same value as LINKTYPE_PKTAP as it is on
282 * other OSes, to LINKTYPE_PKTAP, so files written with
283 * this version of libpcap for a DLT_PKTAP capture have a link-
284 * layer header type of LINKTYPE_PKTAP.
286 * However, files written on OS X Mavericks for a DLT_PKTAP
287 * capture have a link-layer header type of LINKTYPE_USER2.
288 * If we don't have a printer for DLT_USER2, and type is
289 * DLT_USER2, we look up the printer for DLT_PKTAP and use
292 if (type
== DLT_USER2
) {
293 for (p
= printers
; p
->f
; ++p
)
294 if (DLT_PKTAP
== p
->type
)
304 has_printer(int type
)
306 return (lookup_printer(type
) != NULL
);
310 get_if_printer(netdissect_options
*ndo
, int type
)
315 printer
= lookup_printer(type
);
316 if (printer
== NULL
) {
317 dltname
= pcap_datalink_val_to_name(type
);
319 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_NO_PRINTER
,
320 "packet printing is not supported for link type %s: use -w",
323 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_NO_PRINTER
,
324 "packet printing is not supported for link type %d: use -w", type
);
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 if (ndo
->ndo_packet_number
)
337 ND_PRINT("%5u ", packets_captured
);
339 /* Sanity checks on packet length / capture length */
340 if (h
->caplen
== 0) {
342 ND_PRINT("[Invalid header: caplen==0");
345 if (!invalid_header
) {
347 ND_PRINT("[Invalid header:");
351 } else if (h
->len
< h
->caplen
) {
352 if (!invalid_header
) {
354 ND_PRINT("[Invalid header:");
357 ND_PRINT(" len(%u) < caplen(%u)", h
->len
, h
->caplen
);
359 if (h
->caplen
> MAXIMUM_SNAPLEN
) {
360 if (!invalid_header
) {
362 ND_PRINT("[Invalid header:");
365 ND_PRINT(" caplen(%u) > %u", h
->caplen
, MAXIMUM_SNAPLEN
);
367 if (h
->len
> MAXIMUM_SNAPLEN
) {
368 if (!invalid_header
) {
370 ND_PRINT("[Invalid header:");
373 ND_PRINT(" len(%u) > %u", h
->len
, MAXIMUM_SNAPLEN
);
375 if (invalid_header
) {
382 * capture length != 0,
383 * packet length != 0,
384 * capture length <= MAXIMUM_SNAPLEN,
385 * packet length <= MAXIMUM_SNAPLEN,
386 * packet length >= capture length.
388 * Currently, there is no D-Bus printer, thus no need for
392 ts_print(ndo
, &h
->ts
);
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
;
402 ndo
->ndo_protocol
= "";
403 if (setjmp(ndo
->ndo_truncated
) == 0) {
404 /* Print the packet. */
405 hdrlen
= (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
407 /* A printer quit because the packet was truncated; report it */
408 ND_PRINT(" [|%s]", ndo
->ndo_protocol
);
412 * Free all pushed buffers; if we got here by a printer quitting,
413 * we need to release anything that didn't get released because
414 * we longjmped out of the code before it popped a buffer.
416 nd_pop_all_buffers(ndo
);
419 * Restore the original snapend, as a printer might have
422 ndo
->ndo_snapend
= sp
+ h
->caplen
;
423 if (ndo
->ndo_Xflag
) {
425 * Print the raw packet data in hex and ASCII.
427 if (ndo
->ndo_Xflag
> 1) {
429 * Include the link-layer header.
431 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
434 * Don't include the link-layer header - and if
435 * we have nothing past the link-layer header,
438 if (h
->caplen
> hdrlen
)
439 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
442 } else if (ndo
->ndo_xflag
) {
444 * Print the raw packet data in hex.
446 if (ndo
->ndo_xflag
> 1) {
448 * Include the link-layer header.
450 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
453 * Don't include the link-layer header - and if
454 * we have nothing past the link-layer header,
457 if (h
->caplen
> hdrlen
)
458 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
461 } else if (ndo
->ndo_Aflag
) {
463 * Print the raw packet data in ASCII.
465 if (ndo
->ndo_Aflag
> 1) {
467 * Include the link-layer header.
469 ascii_print(ndo
, sp
, h
->caplen
);
472 * Don't include the link-layer header - and if
473 * we have nothing past the link-layer header,
476 if (h
->caplen
> hdrlen
)
477 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
486 * By default, print the specified data out in hex and ASCII.
489 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
491 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
496 ndo_error(netdissect_options
*ndo
, status_exit_codes_t status
,
497 const char *fmt
, ...)
501 if (ndo
->program_name
)
502 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
504 (void)vfprintf(stderr
, fmt
, ap
);
509 (void)fputc('\n', stderr
);
518 ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
522 if (ndo
->program_name
)
523 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
524 (void)fprintf(stderr
, "WARNING: ");
526 (void)vfprintf(stderr
, fmt
, ap
);
531 (void)fputc('\n', stderr
);
536 ndo_printf(netdissect_options
*ndo
, const char *fmt
, ...)
542 ret
= vfprintf(stdout
, fmt
, args
);
546 ndo_error(ndo
, S_ERR_ND_WRITE_FILE
,
547 "Unable to write output: %s", pcap_strerror(errno
));
552 ndo_set_function_pointers(netdissect_options
*ndo
)
554 ndo
->ndo_default_print
=ndo_default_print
;
555 ndo
->ndo_printf
=ndo_printf
;
556 ndo
->ndo_error
=ndo_error
;
557 ndo
->ndo_warning
=ndo_warning
;