]> The Tcpdump Group git mirrors - tcpdump/blob - print.c
Merge pull request #750 from Sashan/system-test
[tcpdump] / print.c
1 /*
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.
4 *
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
16 * written permission.
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.
20 *
21 * Support for splitting captures into multiple files with a maximum
22 * file size:
23 *
24 * Copyright (c) 2001
25 * Seth Webster <swebster@sst.ll.mit.edu>
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <setjmp.h>
35
36 #include "netdissect-stdinc.h"
37
38 #include "netdissect.h"
39 #include "addrtoname.h"
40 #include "print.h"
41 #include "netdissect-alloc.h"
42
43 #include "pcap-missing.h"
44
45 struct printer {
46 if_printer f;
47 int type;
48 };
49
50 static const struct printer printers[] = {
51 { ether_if_print, DLT_EN10MB },
52 #ifdef DLT_IPNET
53 { ipnet_if_print, DLT_IPNET },
54 #endif
55 #ifdef DLT_IEEE802_15_4
56 { ieee802_15_4_if_print, DLT_IEEE802_15_4 },
57 #endif
58 #ifdef DLT_IEEE802_15_4_NOFCS
59 { ieee802_15_4_if_print, DLT_IEEE802_15_4_NOFCS },
60 #endif
61 #ifdef DLT_IEEE802_15_4_TAP
62 { ieee802_15_4_tap_if_print, DLT_IEEE802_15_4_TAP },
63 #endif
64 #ifdef DLT_PPI
65 { ppi_if_print, DLT_PPI },
66 #endif
67 #ifdef DLT_NETANALYZER
68 { netanalyzer_if_print, DLT_NETANALYZER },
69 #endif
70 #ifdef DLT_NETANALYZER_TRANSPARENT
71 { netanalyzer_transparent_if_print, DLT_NETANALYZER_TRANSPARENT },
72 #endif
73 #ifdef DLT_NFLOG
74 { nflog_if_print, DLT_NFLOG},
75 #endif
76 #ifdef DLT_CIP
77 { cip_if_print, DLT_CIP },
78 #endif
79 #ifdef DLT_ATM_CLIP
80 { cip_if_print, DLT_ATM_CLIP },
81 #endif
82 #ifdef DLT_IP_OVER_FC
83 { ipfc_if_print, DLT_IP_OVER_FC },
84 #endif
85 { null_if_print, DLT_NULL },
86 #ifdef DLT_LOOP
87 { null_if_print, DLT_LOOP },
88 #endif
89 #ifdef DLT_APPLE_IP_OVER_IEEE1394
90 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
91 #endif
92 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
93 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
94 #endif
95 #ifdef DLT_LANE8023
96 { lane_if_print, DLT_LANE8023 },
97 #endif
98 { arcnet_if_print, DLT_ARCNET },
99 #ifdef DLT_ARCNET_LINUX
100 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
101 #endif
102 { raw_if_print, DLT_RAW },
103 #ifdef DLT_IPV4
104 { raw_if_print, DLT_IPV4 },
105 #endif
106 #ifdef DLT_IPV6
107 { raw_if_print, DLT_IPV6 },
108 #endif
109 #ifdef DLT_USB_LINUX
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 },
117 #endif
118 #ifdef DLT_C_HDLC
119 { chdlc_if_print, DLT_C_HDLC },
120 #endif
121 #ifdef DLT_HDLC
122 { chdlc_if_print, DLT_HDLC },
123 #endif
124 #ifdef DLT_PPP_ETHER
125 { pppoe_if_print, DLT_PPP_ETHER },
126 #endif
127 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
128 { pflog_if_print, DLT_PFLOG },
129 #endif
130 { token_if_print, DLT_IEEE802 },
131 { fddi_if_print, DLT_FDDI },
132 #ifdef DLT_LINUX_SLL
133 { sll_if_print, DLT_LINUX_SLL },
134 #endif
135 #ifdef DLT_LINUX_SLL2
136 { sll2_if_print, DLT_LINUX_SLL2 },
137 #endif
138 #ifdef DLT_FR
139 { fr_if_print, DLT_FR },
140 #endif
141 #ifdef DLT_FRELAY
142 { fr_if_print, DLT_FRELAY },
143 #endif
144 #ifdef DLT_MFR
145 { mfr_if_print, DLT_MFR },
146 #endif
147 { atm_if_print, DLT_ATM_RFC1483 },
148 #ifdef DLT_SUNATM
149 { sunatm_if_print, DLT_SUNATM },
150 #endif
151 #ifdef DLT_ENC
152 { enc_if_print, DLT_ENC },
153 #endif
154 { sl_if_print, DLT_SLIP },
155 #ifdef DLT_SLIP_BSDOS
156 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
157 #endif
158 #ifdef DLT_LTALK
159 { ltalk_if_print, DLT_LTALK },
160 #endif
161 #ifdef DLT_JUNIPER_ATM1
162 { juniper_atm1_if_print, DLT_JUNIPER_ATM1 },
163 #endif
164 #ifdef DLT_JUNIPER_ATM2
165 { juniper_atm2_if_print, DLT_JUNIPER_ATM2 },
166 #endif
167 #ifdef DLT_JUNIPER_MFR
168 { juniper_mfr_if_print, DLT_JUNIPER_MFR },
169 #endif
170 #ifdef DLT_JUNIPER_MLFR
171 { juniper_mlfr_if_print, DLT_JUNIPER_MLFR },
172 #endif
173 #ifdef DLT_JUNIPER_MLPPP
174 { juniper_mlppp_if_print, DLT_JUNIPER_MLPPP },
175 #endif
176 #ifdef DLT_JUNIPER_PPPOE
177 { juniper_pppoe_if_print, DLT_JUNIPER_PPPOE },
178 #endif
179 #ifdef DLT_JUNIPER_PPPOE_ATM
180 { juniper_pppoe_atm_if_print, DLT_JUNIPER_PPPOE_ATM },
181 #endif
182 #ifdef DLT_JUNIPER_GGSN
183 { juniper_ggsn_if_print, DLT_JUNIPER_GGSN },
184 #endif
185 #ifdef DLT_JUNIPER_ES
186 { juniper_es_if_print, DLT_JUNIPER_ES },
187 #endif
188 #ifdef DLT_JUNIPER_MONITOR
189 { juniper_monitor_if_print, DLT_JUNIPER_MONITOR },
190 #endif
191 #ifdef DLT_JUNIPER_SERVICES
192 { juniper_services_if_print, DLT_JUNIPER_SERVICES },
193 #endif
194 #ifdef DLT_JUNIPER_ETHER
195 { juniper_ether_if_print, DLT_JUNIPER_ETHER },
196 #endif
197 #ifdef DLT_JUNIPER_PPP
198 { juniper_ppp_if_print, DLT_JUNIPER_PPP },
199 #endif
200 #ifdef DLT_JUNIPER_FRELAY
201 { juniper_frelay_if_print, DLT_JUNIPER_FRELAY },
202 #endif
203 #ifdef DLT_JUNIPER_CHDLC
204 { juniper_chdlc_if_print, DLT_JUNIPER_CHDLC },
205 #endif
206 #ifdef DLT_PKTAP
207 { pktap_if_print, DLT_PKTAP },
208 #endif
209 #ifdef DLT_IEEE802_11_RADIO
210 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
211 #endif
212 #ifdef DLT_IEEE802_11
213 { ieee802_11_if_print, DLT_IEEE802_11},
214 #endif
215 #ifdef DLT_IEEE802_11_RADIO_AVS
216 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS },
217 #endif
218 #ifdef DLT_PRISM_HEADER
219 { prism_if_print, DLT_PRISM_HEADER },
220 #endif
221 { ppp_if_print, DLT_PPP },
222 #ifdef DLT_PPP_WITHDIRECTION
223 { ppp_if_print, DLT_PPP_WITHDIRECTION },
224 #endif
225 #ifdef DLT_PPP_BSDOS
226 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
227 #endif
228 #ifdef DLT_PPP_SERIAL
229 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
230 #endif
231 #ifdef DLT_DSA_TAG_BRCM
232 { brcm_tag_if_print, DLT_DSA_TAG_BRCM },
233 #endif
234 #ifdef DLT_DSA_TAG_BRCM_PREPEND
235 { brcm_tag_prepend_if_print, DLT_DSA_TAG_BRCM_PREPEND },
236 #endif
237 #ifdef DLT_VSOCK
238 { vsock_if_print, DLT_VSOCK },
239 #endif
240 #ifdef DLT_DSA_TAG_DSA
241 { dsa_if_print, DLT_DSA_TAG_DSA },
242 #endif
243 #ifdef DLT_DSA_TAG_EDSA
244 { edsa_if_print, DLT_DSA_TAG_EDSA },
245 #endif
246 { NULL, 0 },
247 };
248
249 static void ndo_default_print(netdissect_options *ndo, const u_char *bp,
250 u_int length);
251
252 static void NORETURN ndo_error(netdissect_options *ndo,
253 status_exit_codes_t status,
254 FORMAT_STRING(const char *fmt), ...)
255 PRINTFLIKE(3, 4);
256 static void ndo_warning(netdissect_options *ndo,
257 FORMAT_STRING(const char *fmt), ...)
258 PRINTFLIKE(2, 3);
259
260 static int ndo_printf(netdissect_options *ndo,
261 FORMAT_STRING(const char *fmt), ...)
262 PRINTFLIKE(2, 3);
263
264 void
265 init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
266 {
267
268 init_addrtoname(ndo, localnet, mask);
269 init_checksum();
270 }
271
272 if_printer
273 lookup_printer(int type)
274 {
275 const struct printer *p;
276
277 for (p = printers; p->f; ++p)
278 if (type == p->type)
279 return p->f;
280
281 #if defined(DLT_USER2) && defined(DLT_PKTAP)
282 /*
283 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
284 * header.
285 *
286 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
287 * based OSes or the same value as LINKTYPE_PKTAP as it is on
288 * other OSes, to LINKTYPE_PKTAP, so files written with
289 * this version of libpcap for a DLT_PKTAP capture have a link-
290 * layer header type of LINKTYPE_PKTAP.
291 *
292 * However, files written on OS X Mavericks for a DLT_PKTAP
293 * capture have a link-layer header type of LINKTYPE_USER2.
294 * If we don't have a printer for DLT_USER2, and type is
295 * DLT_USER2, we look up the printer for DLT_PKTAP and use
296 * that.
297 */
298 if (type == DLT_USER2) {
299 for (p = printers; p->f; ++p)
300 if (DLT_PKTAP == p->type)
301 return p->f;
302 }
303 #endif
304
305 return NULL;
306 /* NOTREACHED */
307 }
308
309 int
310 has_printer(int type)
311 {
312 return (lookup_printer(type) != NULL);
313 }
314
315 if_printer
316 get_if_printer(netdissect_options *ndo, int type)
317 {
318 const char *dltname;
319 if_printer printer;
320
321 printer = lookup_printer(type);
322 if (printer == NULL) {
323 dltname = pcap_datalink_val_to_name(type);
324 if (dltname != NULL)
325 (*ndo->ndo_error)(ndo, S_ERR_ND_NO_PRINTER,
326 "packet printing is not supported for link type %s: use -w",
327 dltname);
328 else
329 (*ndo->ndo_error)(ndo, S_ERR_ND_NO_PRINTER,
330 "packet printing is not supported for link type %d: use -w", type);
331 }
332 return printer;
333 }
334
335 void
336 pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
337 const u_char *sp, u_int packets_captured)
338 {
339 u_int hdrlen = 0;
340 int invalid_header = 0;
341
342 if (ndo->ndo_packet_number)
343 ND_PRINT("%5u ", packets_captured);
344
345 /* Sanity checks on packet length / capture length */
346 if (h->caplen == 0) {
347 invalid_header = 1;
348 ND_PRINT("[Invalid header: caplen==0");
349 }
350 if (h->len == 0) {
351 if (!invalid_header) {
352 invalid_header = 1;
353 ND_PRINT("[Invalid header:");
354 } else
355 ND_PRINT(",");
356 ND_PRINT(" len==0");
357 } else if (h->len < h->caplen) {
358 if (!invalid_header) {
359 invalid_header = 1;
360 ND_PRINT("[Invalid header:");
361 } else
362 ND_PRINT(",");
363 ND_PRINT(" len(%u) < caplen(%u)", h->len, h->caplen);
364 }
365 if (h->caplen > MAXIMUM_SNAPLEN) {
366 if (!invalid_header) {
367 invalid_header = 1;
368 ND_PRINT("[Invalid header:");
369 } else
370 ND_PRINT(",");
371 ND_PRINT(" caplen(%u) > %u", h->caplen, MAXIMUM_SNAPLEN);
372 }
373 if (h->len > MAXIMUM_SNAPLEN) {
374 if (!invalid_header) {
375 invalid_header = 1;
376 ND_PRINT("[Invalid header:");
377 } else
378 ND_PRINT(",");
379 ND_PRINT(" len(%u) > %u", h->len, MAXIMUM_SNAPLEN);
380 }
381 if (invalid_header) {
382 ND_PRINT("]\n");
383 return;
384 }
385
386 /*
387 * At this point:
388 * capture length != 0,
389 * packet length != 0,
390 * capture length <= MAXIMUM_SNAPLEN,
391 * packet length <= MAXIMUM_SNAPLEN,
392 * packet length >= capture length.
393 *
394 * Currently, there is no D-Bus printer, thus no need for
395 * bigger lengths.
396 */
397
398 ts_print(ndo, &h->ts);
399
400 /*
401 * Printers must check that they're not walking off the end of
402 * the packet.
403 * Rather than pass it all the way down, we set this member
404 * of the netdissect_options structure.
405 */
406 ndo->ndo_snapend = sp + h->caplen;
407
408 ndo->ndo_protocol = "";
409 if (setjmp(ndo->ndo_truncated) == 0) {
410 /* Print the packet. */
411 hdrlen = (ndo->ndo_if_printer)(ndo, h, sp);
412 } else {
413 /* A printer quit because the packet was truncated; report it */
414 ND_PRINT(" [|%s]", ndo->ndo_protocol);
415 }
416
417 /*
418 * Free all pushed buffers; if we got here by a printer quitting,
419 * we need to release anything that didn't get released because
420 * we longjmped out of the code before it popped a buffer.
421 */
422 nd_pop_all_buffers(ndo);
423
424 /*
425 * Restore the original snapend, as a printer might have
426 * changed it.
427 */
428 ndo->ndo_snapend = sp + h->caplen;
429 if (ndo->ndo_Xflag) {
430 /*
431 * Print the raw packet data in hex and ASCII.
432 */
433 if (ndo->ndo_Xflag > 1) {
434 /*
435 * Include the link-layer header.
436 */
437 hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
438 } else {
439 /*
440 * Don't include the link-layer header - and if
441 * we have nothing past the link-layer header,
442 * print nothing.
443 */
444 if (h->caplen > hdrlen)
445 hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
446 h->caplen - hdrlen);
447 }
448 } else if (ndo->ndo_xflag) {
449 /*
450 * Print the raw packet data in hex.
451 */
452 if (ndo->ndo_xflag > 1) {
453 /*
454 * Include the link-layer header.
455 */
456 hex_print(ndo, "\n\t", sp, h->caplen);
457 } else {
458 /*
459 * Don't include the link-layer header - and if
460 * we have nothing past the link-layer header,
461 * print nothing.
462 */
463 if (h->caplen > hdrlen)
464 hex_print(ndo, "\n\t", sp + hdrlen,
465 h->caplen - hdrlen);
466 }
467 } else if (ndo->ndo_Aflag) {
468 /*
469 * Print the raw packet data in ASCII.
470 */
471 if (ndo->ndo_Aflag > 1) {
472 /*
473 * Include the link-layer header.
474 */
475 ascii_print(ndo, sp, h->caplen);
476 } else {
477 /*
478 * Don't include the link-layer header - and if
479 * we have nothing past the link-layer header,
480 * print nothing.
481 */
482 if (h->caplen > hdrlen)
483 ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
484 }
485 }
486
487 ND_PRINT("\n");
488 nd_free_all(ndo);
489 }
490
491 /*
492 * By default, print the specified data out in hex and ASCII.
493 */
494 static void
495 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
496 {
497 hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
498 }
499
500 /* VARARGS */
501 static void
502 ndo_error(netdissect_options *ndo, status_exit_codes_t status,
503 const char *fmt, ...)
504 {
505 va_list ap;
506
507 if (ndo->program_name)
508 (void)fprintf(stderr, "%s: ", ndo->program_name);
509 va_start(ap, fmt);
510 (void)vfprintf(stderr, fmt, ap);
511 va_end(ap);
512 if (*fmt) {
513 fmt += strlen(fmt);
514 if (fmt[-1] != '\n')
515 (void)fputc('\n', stderr);
516 }
517 nd_cleanup();
518 exit(status);
519 /* NOTREACHED */
520 }
521
522 /* VARARGS */
523 static void
524 ndo_warning(netdissect_options *ndo, const char *fmt, ...)
525 {
526 va_list ap;
527
528 if (ndo->program_name)
529 (void)fprintf(stderr, "%s: ", ndo->program_name);
530 (void)fprintf(stderr, "WARNING: ");
531 va_start(ap, fmt);
532 (void)vfprintf(stderr, fmt, ap);
533 va_end(ap);
534 if (*fmt) {
535 fmt += strlen(fmt);
536 if (fmt[-1] != '\n')
537 (void)fputc('\n', stderr);
538 }
539 }
540
541 static int
542 ndo_printf(netdissect_options *ndo, const char *fmt, ...)
543 {
544 va_list args;
545 int ret;
546
547 va_start(args, fmt);
548 ret = vfprintf(stdout, fmt, args);
549 va_end(args);
550
551 if (ret < 0)
552 ndo_error(ndo, S_ERR_ND_WRITE_FILE,
553 "Unable to write output: %s", pcap_strerror(errno));
554 return (ret);
555 }
556
557 void
558 ndo_set_function_pointers(netdissect_options *ndo)
559 {
560 ndo->ndo_default_print=ndo_default_print;
561 ndo->ndo_printf=ndo_printf;
562 ndo->ndo_error=ndo_error;
563 ndo->ndo_warning=ndo_warning;
564 }