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>
35 #include "netdissect-stdinc.h"
37 #include "netdissect.h"
38 #include "addrtoname.h"
40 #include "netdissect-alloc.h"
42 #include "pcap-missing.h"
49 static const struct printer printers
[] = {
50 { ether_if_print
, DLT_EN10MB
},
52 { ipnet_if_print
, DLT_IPNET
},
54 #ifdef DLT_IEEE802_15_4
55 { ieee802_15_4_if_print
, DLT_IEEE802_15_4
},
57 #ifdef DLT_IEEE802_15_4_NOFCS
58 { ieee802_15_4_if_print
, DLT_IEEE802_15_4_NOFCS
},
60 #ifdef DLT_IEEE802_15_4_TAP
61 { ieee802_15_4_tap_if_print
, DLT_IEEE802_15_4_TAP
},
64 { ppi_if_print
, DLT_PPI
},
66 #ifdef DLT_NETANALYZER
67 { netanalyzer_if_print
, DLT_NETANALYZER
},
69 #ifdef DLT_NETANALYZER_TRANSPARENT
70 { netanalyzer_transparent_if_print
, DLT_NETANALYZER_TRANSPARENT
},
73 { nflog_if_print
, DLT_NFLOG
},
76 { cip_if_print
, DLT_CIP
},
79 { cip_if_print
, DLT_ATM_CLIP
},
82 { ipfc_if_print
, DLT_IP_OVER_FC
},
84 { null_if_print
, DLT_NULL
},
86 { null_if_print
, DLT_LOOP
},
88 #ifdef DLT_APPLE_IP_OVER_IEEE1394
89 { ap1394_if_print
, DLT_APPLE_IP_OVER_IEEE1394
},
91 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
92 { bt_if_print
, DLT_BLUETOOTH_HCI_H4_WITH_PHDR
},
95 { lane_if_print
, DLT_LANE8023
},
97 { arcnet_if_print
, DLT_ARCNET
},
98 #ifdef DLT_ARCNET_LINUX
99 { arcnet_linux_if_print
, DLT_ARCNET_LINUX
},
101 { raw_if_print
, DLT_RAW
},
103 { raw_if_print
, DLT_IPV4
},
106 { raw_if_print
, DLT_IPV6
},
109 { usb_linux_48_byte_if_print
, DLT_USB_LINUX
},
110 #endif /* DLT_USB_LINUX */
111 #ifdef DLT_USB_LINUX_MMAPPED
112 { usb_linux_64_byte_if_print
, DLT_USB_LINUX_MMAPPED
},
113 #endif /* DLT_USB_LINUX_MMAPPED */
114 #ifdef DLT_SYMANTEC_FIREWALL
115 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
118 { chdlc_if_print
, DLT_C_HDLC
},
121 { chdlc_if_print
, DLT_HDLC
},
124 { pppoe_if_print
, DLT_PPP_ETHER
},
126 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
127 { pflog_if_print
, DLT_PFLOG
},
129 { token_if_print
, DLT_IEEE802
},
130 { fddi_if_print
, DLT_FDDI
},
132 { sll_if_print
, DLT_LINUX_SLL
},
134 #ifdef DLT_LINUX_SLL2
135 { sll2_if_print
, DLT_LINUX_SLL2
},
138 { fr_if_print
, DLT_FR
},
141 { fr_if_print
, DLT_FRELAY
},
144 { mfr_if_print
, DLT_MFR
},
146 { atm_if_print
, DLT_ATM_RFC1483
},
148 { sunatm_if_print
, DLT_SUNATM
},
151 { enc_if_print
, DLT_ENC
},
153 { sl_if_print
, DLT_SLIP
},
154 #ifdef DLT_SLIP_BSDOS
155 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
158 { ltalk_if_print
, DLT_LTALK
},
160 #ifdef DLT_JUNIPER_ATM1
161 { juniper_atm1_if_print
, DLT_JUNIPER_ATM1
},
163 #ifdef DLT_JUNIPER_ATM2
164 { juniper_atm2_if_print
, DLT_JUNIPER_ATM2
},
166 #ifdef DLT_JUNIPER_MFR
167 { juniper_mfr_if_print
, DLT_JUNIPER_MFR
},
169 #ifdef DLT_JUNIPER_MLFR
170 { juniper_mlfr_if_print
, DLT_JUNIPER_MLFR
},
172 #ifdef DLT_JUNIPER_MLPPP
173 { juniper_mlppp_if_print
, DLT_JUNIPER_MLPPP
},
175 #ifdef DLT_JUNIPER_PPPOE
176 { juniper_pppoe_if_print
, DLT_JUNIPER_PPPOE
},
178 #ifdef DLT_JUNIPER_PPPOE_ATM
179 { juniper_pppoe_atm_if_print
, DLT_JUNIPER_PPPOE_ATM
},
181 #ifdef DLT_JUNIPER_GGSN
182 { juniper_ggsn_if_print
, DLT_JUNIPER_GGSN
},
184 #ifdef DLT_JUNIPER_ES
185 { juniper_es_if_print
, DLT_JUNIPER_ES
},
187 #ifdef DLT_JUNIPER_MONITOR
188 { juniper_monitor_if_print
, DLT_JUNIPER_MONITOR
},
190 #ifdef DLT_JUNIPER_SERVICES
191 { juniper_services_if_print
, DLT_JUNIPER_SERVICES
},
193 #ifdef DLT_JUNIPER_ETHER
194 { juniper_ether_if_print
, DLT_JUNIPER_ETHER
},
196 #ifdef DLT_JUNIPER_PPP
197 { juniper_ppp_if_print
, DLT_JUNIPER_PPP
},
199 #ifdef DLT_JUNIPER_FRELAY
200 { juniper_frelay_if_print
, DLT_JUNIPER_FRELAY
},
202 #ifdef DLT_JUNIPER_CHDLC
203 { juniper_chdlc_if_print
, DLT_JUNIPER_CHDLC
},
206 { pktap_if_print
, DLT_PKTAP
},
208 #ifdef DLT_IEEE802_11_RADIO
209 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
211 #ifdef DLT_IEEE802_11
212 { ieee802_11_if_print
, DLT_IEEE802_11
},
214 #ifdef DLT_IEEE802_11_RADIO_AVS
215 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
217 #ifdef DLT_PRISM_HEADER
218 { prism_if_print
, DLT_PRISM_HEADER
},
220 { ppp_if_print
, DLT_PPP
},
221 #ifdef DLT_PPP_WITHDIRECTION
222 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
225 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
227 #ifdef DLT_PPP_SERIAL
228 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
230 #ifdef DLT_DSA_TAG_BRCM
231 { brcm_tag_if_print
, DLT_DSA_TAG_BRCM
},
233 #ifdef DLT_DSA_TAG_BRCM_PREPEND
234 { brcm_tag_prepend_if_print
, DLT_DSA_TAG_BRCM_PREPEND
},
239 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
242 static void NORETURN
ndo_error(netdissect_options
*ndo
,
243 status_exit_codes_t status
,
244 FORMAT_STRING(const char *fmt
), ...)
246 static void ndo_warning(netdissect_options
*ndo
,
247 FORMAT_STRING(const char *fmt
), ...)
250 static int ndo_printf(netdissect_options
*ndo
,
251 FORMAT_STRING(const char *fmt
), ...)
255 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
)
258 init_addrtoname(ndo
, localnet
, mask
);
263 lookup_printer(int type
)
265 const struct printer
*p
;
267 for (p
= printers
; p
->f
; ++p
)
271 #if defined(DLT_USER2) && defined(DLT_PKTAP)
273 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
276 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
277 * based OSes or the same value as LINKTYPE_PKTAP as it is on
278 * other OSes, to LINKTYPE_PKTAP, so files written with
279 * this version of libpcap for a DLT_PKTAP capture have a link-
280 * layer header type of LINKTYPE_PKTAP.
282 * However, files written on OS X Mavericks for a DLT_PKTAP
283 * capture have a link-layer header type of LINKTYPE_USER2.
284 * If we don't have a printer for DLT_USER2, and type is
285 * DLT_USER2, we look up the printer for DLT_PKTAP and use
288 if (type
== DLT_USER2
) {
289 for (p
= printers
; p
->f
; ++p
)
290 if (DLT_PKTAP
== p
->type
)
300 has_printer(int type
)
302 return (lookup_printer(type
) != NULL
);
306 get_if_printer(netdissect_options
*ndo
, int type
)
311 printer
= lookup_printer(type
);
312 if (printer
== NULL
) {
313 dltname
= pcap_datalink_val_to_name(type
);
315 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_NO_PRINTER
,
316 "packet printing is not supported for link type %s: use -w",
319 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_NO_PRINTER
,
320 "packet printing is not supported for link type %d: use -w", type
);
326 pretty_print_packet(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
327 const u_char
*sp
, u_int packets_captured
)
330 int invalid_header
= 0;
332 if (ndo
->ndo_packet_number
)
333 ND_PRINT("%5u ", packets_captured
);
335 /* Sanity checks on packet length / capture length */
336 if (h
->caplen
== 0) {
338 ND_PRINT("[Invalid header: caplen==0");
341 if (!invalid_header
) {
343 ND_PRINT("[Invalid header:");
347 } else if (h
->len
< h
->caplen
) {
348 if (!invalid_header
) {
350 ND_PRINT("[Invalid header:");
353 ND_PRINT(" len(%u) < caplen(%u)", h
->len
, h
->caplen
);
355 if (h
->caplen
> MAXIMUM_SNAPLEN
) {
356 if (!invalid_header
) {
358 ND_PRINT("[Invalid header:");
361 ND_PRINT(" caplen(%u) > %u", h
->caplen
, MAXIMUM_SNAPLEN
);
363 if (h
->len
> MAXIMUM_SNAPLEN
) {
364 if (!invalid_header
) {
366 ND_PRINT("[Invalid header:");
369 ND_PRINT(" len(%u) > %u", h
->len
, MAXIMUM_SNAPLEN
);
371 if (invalid_header
) {
378 * capture length != 0,
379 * packet length != 0,
380 * capture length <= MAXIMUM_SNAPLEN,
381 * packet length <= MAXIMUM_SNAPLEN,
382 * packet length >= capture length.
384 * Currently, there is no D-Bus printer, thus no need for
388 ts_print(ndo
, &h
->ts
);
391 * Printers must check that they're not walking off the end of
393 * Rather than pass it all the way down, we set this member
394 * of the netdissect_options structure.
396 ndo
->ndo_snapend
= sp
+ h
->caplen
;
398 hdrlen
= (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
401 * Restore the original snapend, as a printer might have
404 ndo
->ndo_snapend
= sp
+ h
->caplen
;
405 if (ndo
->ndo_Xflag
) {
407 * Print the raw packet data in hex and ASCII.
409 if (ndo
->ndo_Xflag
> 1) {
411 * Include the link-layer header.
413 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
416 * Don't include the link-layer header - and if
417 * we have nothing past the link-layer header,
420 if (h
->caplen
> hdrlen
)
421 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
424 } else if (ndo
->ndo_xflag
) {
426 * Print the raw packet data in hex.
428 if (ndo
->ndo_xflag
> 1) {
430 * Include the link-layer header.
432 hex_print(ndo
, "\n\t", sp
, h
->caplen
);
435 * Don't include the link-layer header - and if
436 * we have nothing past the link-layer header,
439 if (h
->caplen
> hdrlen
)
440 hex_print(ndo
, "\n\t", sp
+ hdrlen
,
443 } else if (ndo
->ndo_Aflag
) {
445 * Print the raw packet data in ASCII.
447 if (ndo
->ndo_Aflag
> 1) {
449 * Include the link-layer header.
451 ascii_print(ndo
, sp
, h
->caplen
);
454 * Don't include the link-layer header - and if
455 * we have nothing past the link-layer header,
458 if (h
->caplen
> hdrlen
)
459 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
468 * By default, print the specified data out in hex and ASCII.
471 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
473 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
478 ndo_error(netdissect_options
*ndo
, status_exit_codes_t status
,
479 const char *fmt
, ...)
483 if (ndo
->program_name
)
484 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
486 (void)vfprintf(stderr
, fmt
, ap
);
491 (void)fputc('\n', stderr
);
500 ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
504 if (ndo
->program_name
)
505 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
506 (void)fprintf(stderr
, "WARNING: ");
508 (void)vfprintf(stderr
, fmt
, ap
);
513 (void)fputc('\n', stderr
);
518 ndo_printf(netdissect_options
*ndo
, const char *fmt
, ...)
524 ret
= vfprintf(stdout
, fmt
, args
);
528 ndo_error(ndo
, S_ERR_ND_WRITE_FILE
,
529 "Unable to write output: %s", pcap_strerror(errno
));
534 ndo_set_function_pointers(netdissect_options
*ndo
)
536 ndo
->ndo_default_print
=ndo_default_print
;
537 ndo
->ndo_printf
=ndo_printf
;
538 ndo
->ndo_error
=ndo_error
;
539 ndo
->ndo_warning
=ndo_warning
;