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