]> The Tcpdump Group git mirrors - tcpdump/blob - print.c
ARCNET: Update the link-layer dissectors to void functions
[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_NFLOG
73 { nflog_if_print, DLT_NFLOG},
74 #endif
75 #ifdef DLT_CIP
76 { cip_if_print, DLT_CIP },
77 #endif
78 #ifdef DLT_ATM_CLIP
79 { cip_if_print, DLT_ATM_CLIP },
80 #endif
81 #ifdef DLT_IP_OVER_FC
82 { ipfc_if_print, DLT_IP_OVER_FC },
83 #endif
84 #ifdef DLT_LANE8023
85 { lane_if_print, DLT_LANE8023 },
86 #endif
87 #ifdef DLT_IPOIB
88 { ipoib_if_print, DLT_IPOIB },
89 #endif
90 #ifdef DLT_C_HDLC
91 { chdlc_if_print, DLT_C_HDLC },
92 #endif
93 #ifdef DLT_HDLC
94 { chdlc_if_print, DLT_HDLC },
95 #endif
96 #ifdef DLT_PPP_ETHER
97 { pppoe_if_print, DLT_PPP_ETHER },
98 #endif
99 #if defined(DLT_PFLOG) && defined(HAVE_NET_IF_PFLOG_H)
100 { pflog_if_print, DLT_PFLOG },
101 #endif
102 { token_if_print, DLT_IEEE802 },
103 { fddi_if_print, DLT_FDDI },
104 #ifdef DLT_LINUX_SLL
105 { sll_if_print, DLT_LINUX_SLL },
106 #endif
107 #ifdef DLT_LINUX_SLL2
108 { sll2_if_print, DLT_LINUX_SLL2 },
109 #endif
110 #ifdef DLT_FR
111 { fr_if_print, DLT_FR },
112 #endif
113 #ifdef DLT_FRELAY
114 { fr_if_print, DLT_FRELAY },
115 #endif
116 #ifdef DLT_MFR
117 { mfr_if_print, DLT_MFR },
118 #endif
119 { atm_if_print, DLT_ATM_RFC1483 },
120 { sl_if_print, DLT_SLIP },
121 #ifdef DLT_SLIP_BSDOS
122 { sl_bsdos_if_print, DLT_SLIP_BSDOS },
123 #endif
124 #ifdef DLT_LTALK
125 { ltalk_if_print, DLT_LTALK },
126 #endif
127 #ifdef DLT_JUNIPER_ATM1
128 { juniper_atm1_if_print, DLT_JUNIPER_ATM1 },
129 #endif
130 #ifdef DLT_JUNIPER_ATM2
131 { juniper_atm2_if_print, DLT_JUNIPER_ATM2 },
132 #endif
133 #ifdef DLT_JUNIPER_MFR
134 { juniper_mfr_if_print, DLT_JUNIPER_MFR },
135 #endif
136 #ifdef DLT_JUNIPER_MLFR
137 { juniper_mlfr_if_print, DLT_JUNIPER_MLFR },
138 #endif
139 #ifdef DLT_JUNIPER_MLPPP
140 { juniper_mlppp_if_print, DLT_JUNIPER_MLPPP },
141 #endif
142 #ifdef DLT_JUNIPER_PPPOE
143 { juniper_pppoe_if_print, DLT_JUNIPER_PPPOE },
144 #endif
145 #ifdef DLT_JUNIPER_PPPOE_ATM
146 { juniper_pppoe_atm_if_print, DLT_JUNIPER_PPPOE_ATM },
147 #endif
148 #ifdef DLT_JUNIPER_GGSN
149 { juniper_ggsn_if_print, DLT_JUNIPER_GGSN },
150 #endif
151 #ifdef DLT_JUNIPER_ES
152 { juniper_es_if_print, DLT_JUNIPER_ES },
153 #endif
154 #ifdef DLT_JUNIPER_MONITOR
155 { juniper_monitor_if_print, DLT_JUNIPER_MONITOR },
156 #endif
157 #ifdef DLT_JUNIPER_SERVICES
158 { juniper_services_if_print, DLT_JUNIPER_SERVICES },
159 #endif
160 #ifdef DLT_JUNIPER_ETHER
161 { juniper_ether_if_print, DLT_JUNIPER_ETHER },
162 #endif
163 #ifdef DLT_JUNIPER_PPP
164 { juniper_ppp_if_print, DLT_JUNIPER_PPP },
165 #endif
166 #ifdef DLT_JUNIPER_FRELAY
167 { juniper_frelay_if_print, DLT_JUNIPER_FRELAY },
168 #endif
169 #ifdef DLT_JUNIPER_CHDLC
170 { juniper_chdlc_if_print, DLT_JUNIPER_CHDLC },
171 #endif
172 #ifdef DLT_IEEE802_11_RADIO
173 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO },
174 #endif
175 #ifdef DLT_IEEE802_11
176 { ieee802_11_if_print, DLT_IEEE802_11},
177 #endif
178 #ifdef DLT_IEEE802_11_RADIO_AVS
179 { ieee802_11_radio_avs_if_print, DLT_IEEE802_11_RADIO_AVS },
180 #endif
181 #ifdef DLT_PRISM_HEADER
182 { prism_if_print, DLT_PRISM_HEADER },
183 #endif
184 { ppp_if_print, DLT_PPP },
185 #ifdef DLT_PPP_WITHDIRECTION
186 { ppp_if_print, DLT_PPP_WITHDIRECTION },
187 #endif
188 #ifdef DLT_PPP_BSDOS
189 { ppp_bsdos_if_print, DLT_PPP_BSDOS },
190 #endif
191 #ifdef DLT_PPP_SERIAL
192 { ppp_hdlc_if_print, DLT_PPP_SERIAL },
193 #endif
194 #ifdef DLT_DSA_TAG_BRCM
195 { brcm_tag_if_print, DLT_DSA_TAG_BRCM },
196 #endif
197 #ifdef DLT_DSA_TAG_BRCM_PREPEND
198 { brcm_tag_prepend_if_print, DLT_DSA_TAG_BRCM_PREPEND },
199 #endif
200 #ifdef DLT_VSOCK
201 { vsock_if_print, DLT_VSOCK },
202 #endif
203 #ifdef DLT_DSA_TAG_DSA
204 { dsa_if_print, DLT_DSA_TAG_DSA },
205 #endif
206 #ifdef DLT_DSA_TAG_EDSA
207 { edsa_if_print, DLT_DSA_TAG_EDSA },
208 #endif
209 { NULL, 0 },
210 };
211
212 static const struct void_printer void_printers[] = {
213 #ifdef DLT_APPLE_IP_OVER_IEEE1394
214 { ap1394_if_print, DLT_APPLE_IP_OVER_IEEE1394 },
215 #endif
216 { arcnet_if_print, DLT_ARCNET },
217 #ifdef DLT_ARCNET_LINUX
218 { arcnet_linux_if_print, DLT_ARCNET_LINUX },
219 #endif
220 #ifdef DLT_BLUETOOTH_HCI_H4_WITH_PHDR
221 { bt_if_print, DLT_BLUETOOTH_HCI_H4_WITH_PHDR},
222 #endif
223 #ifdef DLT_ENC
224 { enc_if_print, DLT_ENC },
225 #endif
226 #ifdef DLT_IPNET
227 { ipnet_if_print, DLT_IPNET },
228 #endif
229 { null_if_print, DLT_NULL },
230 #ifdef DLT_LOOP
231 { null_if_print, DLT_LOOP },
232 #endif
233 #ifdef DLT_PKTAP
234 { pktap_if_print, DLT_PKTAP },
235 #endif
236 #ifdef DLT_PPI
237 { ppi_if_print, DLT_PPI },
238 #endif
239 { raw_if_print, DLT_RAW },
240 #ifdef DLT_IPV4
241 { raw_if_print, DLT_IPV4 },
242 #endif
243 #ifdef DLT_IPV6
244 { raw_if_print, DLT_IPV6 },
245 #endif
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 const char *dltname;
383 if_printer_t printer;
384
385 printer = lookup_printer(ndo, type);
386 if (printer.printer == NULL) {
387 dltname = pcap_datalink_val_to_name(type);
388 if (dltname != NULL)
389 (*ndo->ndo_error)(ndo, S_ERR_ND_NO_PRINTER,
390 "packet printing is not supported for link type %s: use -w",
391 dltname);
392 else
393 (*ndo->ndo_error)(ndo, S_ERR_ND_NO_PRINTER,
394 "packet printing is not supported for link type %d: use -w", type);
395 }
396 return printer;
397 }
398
399 void
400 pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
401 const u_char *sp, u_int packets_captured)
402 {
403 u_int hdrlen;
404 int invalid_header = 0;
405
406 if (ndo->ndo_packet_number)
407 ND_PRINT("%5u ", packets_captured);
408
409 /* Sanity checks on packet length / capture length */
410 if (h->caplen == 0) {
411 invalid_header = 1;
412 ND_PRINT("[Invalid header: caplen==0");
413 }
414 if (h->len == 0) {
415 if (!invalid_header) {
416 invalid_header = 1;
417 ND_PRINT("[Invalid header:");
418 } else
419 ND_PRINT(",");
420 ND_PRINT(" len==0");
421 } else if (h->len < h->caplen) {
422 if (!invalid_header) {
423 invalid_header = 1;
424 ND_PRINT("[Invalid header:");
425 } else
426 ND_PRINT(",");
427 ND_PRINT(" len(%u) < caplen(%u)", h->len, h->caplen);
428 }
429 if (h->caplen > MAXIMUM_SNAPLEN) {
430 if (!invalid_header) {
431 invalid_header = 1;
432 ND_PRINT("[Invalid header:");
433 } else
434 ND_PRINT(",");
435 ND_PRINT(" caplen(%u) > %u", h->caplen, MAXIMUM_SNAPLEN);
436 }
437 if (h->len > MAXIMUM_SNAPLEN) {
438 if (!invalid_header) {
439 invalid_header = 1;
440 ND_PRINT("[Invalid header:");
441 } else
442 ND_PRINT(",");
443 ND_PRINT(" len(%u) > %u", h->len, MAXIMUM_SNAPLEN);
444 }
445 if (invalid_header) {
446 ND_PRINT("]\n");
447 return;
448 }
449
450 /*
451 * At this point:
452 * capture length != 0,
453 * packet length != 0,
454 * capture length <= MAXIMUM_SNAPLEN,
455 * packet length <= MAXIMUM_SNAPLEN,
456 * packet length >= capture length.
457 *
458 * Currently, there is no D-Bus printer, thus no need for
459 * bigger lengths.
460 */
461
462 ts_print(ndo, &h->ts);
463
464 /*
465 * Printers must check that they're not walking off the end of
466 * the packet.
467 * Rather than pass it all the way down, we set this member
468 * of the netdissect_options structure.
469 */
470 ndo->ndo_snapend = sp + h->caplen;
471
472 ndo->ndo_protocol = "";
473 ndo->ndo_ll_header_length = 0;
474 if (setjmp(ndo->ndo_truncated) == 0) {
475 /* Print the packet. */
476 if (ndo->ndo_void_printer == TRUE) {
477 (ndo->ndo_if_printer.void_printer)(ndo, h, sp);
478 hdrlen = ndo->ndo_ll_header_length;
479 } else
480 hdrlen = (ndo->ndo_if_printer.uint_printer)(ndo, h, sp);
481 } else {
482 /* A printer quit because the packet was truncated; report it */
483 ND_PRINT(" [|%s]", ndo->ndo_protocol);
484 hdrlen = ndo->ndo_ll_header_length;
485 }
486
487 /*
488 * Empty the stack of packet information, freeing all pushed buffers;
489 * if we got here by a printer quitting, we need to release anything
490 * that didn't get released because we longjmped out of the code
491 * before it popped the packet information.
492 */
493 nd_pop_all_packet_info(ndo);
494
495 /*
496 * Restore the original snapend, as a printer might have
497 * changed it.
498 */
499 ndo->ndo_snapend = sp + h->caplen;
500 if (ndo->ndo_Xflag) {
501 /*
502 * Print the raw packet data in hex and ASCII.
503 */
504 if (ndo->ndo_Xflag > 1) {
505 /*
506 * Include the link-layer header.
507 */
508 hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
509 } else {
510 /*
511 * Don't include the link-layer header - and if
512 * we have nothing past the link-layer header,
513 * print nothing.
514 */
515 if (h->caplen > hdrlen)
516 hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
517 h->caplen - hdrlen);
518 }
519 } else if (ndo->ndo_xflag) {
520 /*
521 * Print the raw packet data in hex.
522 */
523 if (ndo->ndo_xflag > 1) {
524 /*
525 * Include the link-layer header.
526 */
527 hex_print(ndo, "\n\t", sp, h->caplen);
528 } else {
529 /*
530 * Don't include the link-layer header - and if
531 * we have nothing past the link-layer header,
532 * print nothing.
533 */
534 if (h->caplen > hdrlen)
535 hex_print(ndo, "\n\t", sp + hdrlen,
536 h->caplen - hdrlen);
537 }
538 } else if (ndo->ndo_Aflag) {
539 /*
540 * Print the raw packet data in ASCII.
541 */
542 if (ndo->ndo_Aflag > 1) {
543 /*
544 * Include the link-layer header.
545 */
546 ascii_print(ndo, sp, h->caplen);
547 } else {
548 /*
549 * Don't include the link-layer header - and if
550 * we have nothing past the link-layer header,
551 * print nothing.
552 */
553 if (h->caplen > hdrlen)
554 ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
555 }
556 }
557
558 ND_PRINT("\n");
559 nd_free_all(ndo);
560 }
561
562 /*
563 * By default, print the specified data out in hex and ASCII.
564 */
565 static void
566 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
567 {
568 hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
569 }
570
571 /* VARARGS */
572 static void
573 ndo_error(netdissect_options *ndo, status_exit_codes_t status,
574 const char *fmt, ...)
575 {
576 va_list ap;
577
578 if (ndo->program_name)
579 (void)fprintf(stderr, "%s: ", ndo->program_name);
580 va_start(ap, fmt);
581 (void)vfprintf(stderr, fmt, ap);
582 va_end(ap);
583 if (*fmt) {
584 fmt += strlen(fmt);
585 if (fmt[-1] != '\n')
586 (void)fputc('\n', stderr);
587 }
588 nd_cleanup();
589 exit(status);
590 /* NOTREACHED */
591 }
592
593 /* VARARGS */
594 static void
595 ndo_warning(netdissect_options *ndo, const char *fmt, ...)
596 {
597 va_list ap;
598
599 if (ndo->program_name)
600 (void)fprintf(stderr, "%s: ", ndo->program_name);
601 (void)fprintf(stderr, "WARNING: ");
602 va_start(ap, fmt);
603 (void)vfprintf(stderr, fmt, ap);
604 va_end(ap);
605 if (*fmt) {
606 fmt += strlen(fmt);
607 if (fmt[-1] != '\n')
608 (void)fputc('\n', stderr);
609 }
610 }
611
612 static int
613 ndo_printf(netdissect_options *ndo, const char *fmt, ...)
614 {
615 va_list args;
616 int ret;
617
618 va_start(args, fmt);
619 ret = vfprintf(stdout, fmt, args);
620 va_end(args);
621
622 if (ret < 0)
623 ndo_error(ndo, S_ERR_ND_WRITE_FILE,
624 "Unable to write output: %s", pcap_strerror(errno));
625 return (ret);
626 }
627
628 void
629 ndo_set_function_pointers(netdissect_options *ndo)
630 {
631 ndo->ndo_default_print=ndo_default_print;
632 ndo->ndo_printf=ndo_printf;
633 ndo->ndo_error=ndo_error;
634 ndo->ndo_warning=ndo_warning;
635 }