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