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 { ipoib_if_print
, DLT_IPOIB
},
113 { usb_linux_48_byte_if_print
, DLT_USB_LINUX
},
114 #endif /* DLT_USB_LINUX */
115 #ifdef DLT_USB_LINUX_MMAPPED
116 { usb_linux_64_byte_if_print
, DLT_USB_LINUX_MMAPPED
},
117 #endif /* DLT_USB_LINUX_MMAPPED */
118 #ifdef DLT_SYMANTEC_FIREWALL
119 { symantec_if_print
, DLT_SYMANTEC_FIREWALL
},
122 { chdlc_if_print
, DLT_C_HDLC
},
125 { chdlc_if_print
, DLT_HDLC
},
128 { pppoe_if_print
, DLT_PPP_ETHER
},
130 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
131 { pflog_if_print
, DLT_PFLOG
},
133 { token_if_print
, DLT_IEEE802
},
134 { fddi_if_print
, DLT_FDDI
},
136 { sll_if_print
, DLT_LINUX_SLL
},
138 #ifdef DLT_LINUX_SLL2
139 { sll2_if_print
, DLT_LINUX_SLL2
},
142 { fr_if_print
, DLT_FR
},
145 { fr_if_print
, DLT_FRELAY
},
148 { mfr_if_print
, DLT_MFR
},
150 { atm_if_print
, DLT_ATM_RFC1483
},
152 { sunatm_if_print
, DLT_SUNATM
},
155 { enc_if_print
, DLT_ENC
},
157 { sl_if_print
, DLT_SLIP
},
158 #ifdef DLT_SLIP_BSDOS
159 { sl_bsdos_if_print
, DLT_SLIP_BSDOS
},
162 { ltalk_if_print
, DLT_LTALK
},
164 #ifdef DLT_JUNIPER_ATM1
165 { juniper_atm1_if_print
, DLT_JUNIPER_ATM1
},
167 #ifdef DLT_JUNIPER_ATM2
168 { juniper_atm2_if_print
, DLT_JUNIPER_ATM2
},
170 #ifdef DLT_JUNIPER_MFR
171 { juniper_mfr_if_print
, DLT_JUNIPER_MFR
},
173 #ifdef DLT_JUNIPER_MLFR
174 { juniper_mlfr_if_print
, DLT_JUNIPER_MLFR
},
176 #ifdef DLT_JUNIPER_MLPPP
177 { juniper_mlppp_if_print
, DLT_JUNIPER_MLPPP
},
179 #ifdef DLT_JUNIPER_PPPOE
180 { juniper_pppoe_if_print
, DLT_JUNIPER_PPPOE
},
182 #ifdef DLT_JUNIPER_PPPOE_ATM
183 { juniper_pppoe_atm_if_print
, DLT_JUNIPER_PPPOE_ATM
},
185 #ifdef DLT_JUNIPER_GGSN
186 { juniper_ggsn_if_print
, DLT_JUNIPER_GGSN
},
188 #ifdef DLT_JUNIPER_ES
189 { juniper_es_if_print
, DLT_JUNIPER_ES
},
191 #ifdef DLT_JUNIPER_MONITOR
192 { juniper_monitor_if_print
, DLT_JUNIPER_MONITOR
},
194 #ifdef DLT_JUNIPER_SERVICES
195 { juniper_services_if_print
, DLT_JUNIPER_SERVICES
},
197 #ifdef DLT_JUNIPER_ETHER
198 { juniper_ether_if_print
, DLT_JUNIPER_ETHER
},
200 #ifdef DLT_JUNIPER_PPP
201 { juniper_ppp_if_print
, DLT_JUNIPER_PPP
},
203 #ifdef DLT_JUNIPER_FRELAY
204 { juniper_frelay_if_print
, DLT_JUNIPER_FRELAY
},
206 #ifdef DLT_JUNIPER_CHDLC
207 { juniper_chdlc_if_print
, DLT_JUNIPER_CHDLC
},
210 { pktap_if_print
, DLT_PKTAP
},
212 #ifdef DLT_IEEE802_11_RADIO
213 { ieee802_11_radio_if_print
, DLT_IEEE802_11_RADIO
},
215 #ifdef DLT_IEEE802_11
216 { ieee802_11_if_print
, DLT_IEEE802_11
},
218 #ifdef DLT_IEEE802_11_RADIO_AVS
219 { ieee802_11_radio_avs_if_print
, DLT_IEEE802_11_RADIO_AVS
},
221 #ifdef DLT_PRISM_HEADER
222 { prism_if_print
, DLT_PRISM_HEADER
},
224 { ppp_if_print
, DLT_PPP
},
225 #ifdef DLT_PPP_WITHDIRECTION
226 { ppp_if_print
, DLT_PPP_WITHDIRECTION
},
229 { ppp_bsdos_if_print
, DLT_PPP_BSDOS
},
231 #ifdef DLT_PPP_SERIAL
232 { ppp_hdlc_if_print
, DLT_PPP_SERIAL
},
234 #ifdef DLT_DSA_TAG_BRCM
235 { brcm_tag_if_print
, DLT_DSA_TAG_BRCM
},
237 #ifdef DLT_DSA_TAG_BRCM_PREPEND
238 { brcm_tag_prepend_if_print
, DLT_DSA_TAG_BRCM_PREPEND
},
241 { vsock_if_print
, DLT_VSOCK
},
243 #ifdef DLT_DSA_TAG_DSA
244 { dsa_if_print
, DLT_DSA_TAG_DSA
},
246 #ifdef DLT_DSA_TAG_EDSA
247 { edsa_if_print
, DLT_DSA_TAG_EDSA
},
252 static void ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
,
255 static void NORETURN
ndo_error(netdissect_options
*ndo
,
256 status_exit_codes_t status
,
257 FORMAT_STRING(const char *fmt
), ...)
259 static void ndo_warning(netdissect_options
*ndo
,
260 FORMAT_STRING(const char *fmt
), ...)
263 static int ndo_printf(netdissect_options
*ndo
,
264 FORMAT_STRING(const char *fmt
), ...)
268 init_print(netdissect_options
*ndo
, uint32_t localnet
, uint32_t mask
)
271 init_addrtoname(ndo
, localnet
, mask
);
276 lookup_printer(int type
)
278 const struct printer
*p
;
280 for (p
= printers
; p
->f
; ++p
)
284 #if defined(DLT_USER2) && defined(DLT_PKTAP)
286 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
289 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
290 * based OSes or the same value as LINKTYPE_PKTAP as it is on
291 * other OSes, to LINKTYPE_PKTAP, so files written with
292 * this version of libpcap for a DLT_PKTAP capture have a link-
293 * layer header type of LINKTYPE_PKTAP.
295 * However, files written on OS X Mavericks for a DLT_PKTAP
296 * capture have a link-layer header type of LINKTYPE_USER2.
297 * If we don't have a printer for DLT_USER2, and type is
298 * DLT_USER2, we look up the printer for DLT_PKTAP and use
301 if (type
== DLT_USER2
) {
302 for (p
= printers
; p
->f
; ++p
)
303 if (DLT_PKTAP
== p
->type
)
313 has_printer(int type
)
315 return (lookup_printer(type
) != NULL
);
319 get_if_printer(netdissect_options
*ndo
, int type
)
324 printer
= lookup_printer(type
);
325 if (printer
== NULL
) {
326 dltname
= pcap_datalink_val_to_name(type
);
328 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_NO_PRINTER
,
329 "packet printing is not supported for link type %s: use -w",
332 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_NO_PRINTER
,
333 "packet printing is not supported for link type %d: use -w", type
);
339 pretty_print_packet(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
340 const u_char
*sp
, u_int packets_captured
)
343 int invalid_header
= 0;
345 if (ndo
->ndo_packet_number
)
346 ND_PRINT("%5u ", packets_captured
);
348 /* Sanity checks on packet length / capture length */
349 if (h
->caplen
== 0) {
351 ND_PRINT("[Invalid header: caplen==0");
354 if (!invalid_header
) {
356 ND_PRINT("[Invalid header:");
360 } else if (h
->len
< h
->caplen
) {
361 if (!invalid_header
) {
363 ND_PRINT("[Invalid header:");
366 ND_PRINT(" len(%u) < caplen(%u)", h
->len
, h
->caplen
);
368 if (h
->caplen
> MAXIMUM_SNAPLEN
) {
369 if (!invalid_header
) {
371 ND_PRINT("[Invalid header:");
374 ND_PRINT(" caplen(%u) > %u", h
->caplen
, MAXIMUM_SNAPLEN
);
376 if (h
->len
> MAXIMUM_SNAPLEN
) {
377 if (!invalid_header
) {
379 ND_PRINT("[Invalid header:");
382 ND_PRINT(" len(%u) > %u", h
->len
, MAXIMUM_SNAPLEN
);
384 if (invalid_header
) {
391 * capture length != 0,
392 * packet length != 0,
393 * capture length <= MAXIMUM_SNAPLEN,
394 * packet length <= MAXIMUM_SNAPLEN,
395 * packet length >= capture length.
397 * Currently, there is no D-Bus printer, thus no need for
401 ts_print(ndo
, &h
->ts
);
404 * Printers must check that they're not walking off the end of
406 * Rather than pass it all the way down, we set this member
407 * of the netdissect_options structure.
409 ndo
->ndo_snapend
= sp
+ h
->caplen
;
411 ndo
->ndo_protocol
= "";
412 if (setjmp(ndo
->ndo_truncated
) == 0) {
413 /* Print the packet. */
414 hdrlen
= (ndo
->ndo_if_printer
)(ndo
, h
, sp
);
416 /* A printer quit because the packet was truncated; report it */
417 ND_PRINT(" [|%s]", ndo
->ndo_protocol
);
421 * Empty the stack of packet information, freeing all pushed buffers;
422 * if we got here by a printer quitting, we need to release anything
423 * that didn't get released because we longjmped out of the code
424 * before it popped the packet information.
426 nd_pop_all_packet_info(ndo
);
429 * Restore the original snapend, as a printer might have
432 ndo
->ndo_snapend
= sp
+ h
->caplen
;
433 if (ndo
->ndo_Xflag
) {
435 * Print the raw packet data in hex and ASCII.
437 if (ndo
->ndo_Xflag
> 1) {
439 * Include the link-layer header.
441 hex_and_ascii_print(ndo
, "\n\t", sp
, h
->caplen
);
444 * Don't include the link-layer header - and if
445 * we have nothing past the link-layer header,
448 if (h
->caplen
> hdrlen
)
449 hex_and_ascii_print(ndo
, "\n\t", sp
+ hdrlen
,
452 } else if (ndo
->ndo_xflag
) {
454 * Print the raw packet data in hex.
456 if (ndo
->ndo_xflag
> 1) {
458 * Include the link-layer header.
460 hex_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_print(ndo
, "\n\t", sp
+ hdrlen
,
471 } else if (ndo
->ndo_Aflag
) {
473 * Print the raw packet data in ASCII.
475 if (ndo
->ndo_Aflag
> 1) {
477 * Include the link-layer header.
479 ascii_print(ndo
, 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 ascii_print(ndo
, sp
+ hdrlen
, h
->caplen
- hdrlen
);
496 * By default, print the specified data out in hex and ASCII.
499 ndo_default_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
501 hex_and_ascii_print(ndo
, "\n\t", bp
, length
); /* pass on lf and indentation string */
506 ndo_error(netdissect_options
*ndo
, status_exit_codes_t status
,
507 const char *fmt
, ...)
511 if (ndo
->program_name
)
512 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
514 (void)vfprintf(stderr
, fmt
, ap
);
519 (void)fputc('\n', stderr
);
528 ndo_warning(netdissect_options
*ndo
, const char *fmt
, ...)
532 if (ndo
->program_name
)
533 (void)fprintf(stderr
, "%s: ", ndo
->program_name
);
534 (void)fprintf(stderr
, "WARNING: ");
536 (void)vfprintf(stderr
, fmt
, ap
);
541 (void)fputc('\n', stderr
);
546 ndo_printf(netdissect_options
*ndo
, const char *fmt
, ...)
552 ret
= vfprintf(stdout
, fmt
, args
);
556 ndo_error(ndo
, S_ERR_ND_WRITE_FILE
,
557 "Unable to write output: %s", pcap_strerror(errno
));
562 ndo_set_function_pointers(netdissect_options
*ndo
)
564 ndo
->ndo_default_print
=ndo_default_print
;
565 ndo
->ndo_printf
=ndo_printf
;
566 ndo
->ndo_error
=ndo_error
;
567 ndo
->ndo_warning
=ndo_warning
;