]> The Tcpdump Group git mirrors - tcpdump/blob - print.c
44040ecd8550a645cbd784cfe27a405c014fee68
[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 { NULL, 0 },
241 };
242
243 static void ndo_default_print(netdissect_options *ndo, const u_char *bp,
244 u_int length);
245
246 static void NORETURN ndo_error(netdissect_options *ndo,
247 status_exit_codes_t status,
248 FORMAT_STRING(const char *fmt), ...)
249 PRINTFLIKE(3, 4);
250 static void ndo_warning(netdissect_options *ndo,
251 FORMAT_STRING(const char *fmt), ...)
252 PRINTFLIKE(2, 3);
253
254 static int ndo_printf(netdissect_options *ndo,
255 FORMAT_STRING(const char *fmt), ...)
256 PRINTFLIKE(2, 3);
257
258 void
259 init_print(netdissect_options *ndo, uint32_t localnet, uint32_t mask)
260 {
261
262 init_addrtoname(ndo, localnet, mask);
263 init_checksum();
264 }
265
266 if_printer
267 lookup_printer(int type)
268 {
269 const struct printer *p;
270
271 for (p = printers; p->f; ++p)
272 if (type == p->type)
273 return p->f;
274
275 #if defined(DLT_USER2) && defined(DLT_PKTAP)
276 /*
277 * Apple incorrectly chose to use DLT_USER2 for their PKTAP
278 * header.
279 *
280 * We map DLT_PKTAP, whether it's DLT_USER2 as it is on Darwin-
281 * based OSes or the same value as LINKTYPE_PKTAP as it is on
282 * other OSes, to LINKTYPE_PKTAP, so files written with
283 * this version of libpcap for a DLT_PKTAP capture have a link-
284 * layer header type of LINKTYPE_PKTAP.
285 *
286 * However, files written on OS X Mavericks for a DLT_PKTAP
287 * capture have a link-layer header type of LINKTYPE_USER2.
288 * If we don't have a printer for DLT_USER2, and type is
289 * DLT_USER2, we look up the printer for DLT_PKTAP and use
290 * that.
291 */
292 if (type == DLT_USER2) {
293 for (p = printers; p->f; ++p)
294 if (DLT_PKTAP == p->type)
295 return p->f;
296 }
297 #endif
298
299 return NULL;
300 /* NOTREACHED */
301 }
302
303 int
304 has_printer(int type)
305 {
306 return (lookup_printer(type) != NULL);
307 }
308
309 if_printer
310 get_if_printer(netdissect_options *ndo, int type)
311 {
312 const char *dltname;
313 if_printer printer;
314
315 printer = lookup_printer(type);
316 if (printer == NULL) {
317 dltname = pcap_datalink_val_to_name(type);
318 if (dltname != NULL)
319 (*ndo->ndo_error)(ndo, S_ERR_ND_NO_PRINTER,
320 "packet printing is not supported for link type %s: use -w",
321 dltname);
322 else
323 (*ndo->ndo_error)(ndo, S_ERR_ND_NO_PRINTER,
324 "packet printing is not supported for link type %d: use -w", type);
325 }
326 return printer;
327 }
328
329 void
330 pretty_print_packet(netdissect_options *ndo, const struct pcap_pkthdr *h,
331 const u_char *sp, u_int packets_captured)
332 {
333 u_int hdrlen = 0;
334 int invalid_header = 0;
335
336 if (ndo->ndo_packet_number)
337 ND_PRINT("%5u ", packets_captured);
338
339 /* Sanity checks on packet length / capture length */
340 if (h->caplen == 0) {
341 invalid_header = 1;
342 ND_PRINT("[Invalid header: caplen==0");
343 }
344 if (h->len == 0) {
345 if (!invalid_header) {
346 invalid_header = 1;
347 ND_PRINT("[Invalid header:");
348 } else
349 ND_PRINT(",");
350 ND_PRINT(" len==0");
351 } else if (h->len < h->caplen) {
352 if (!invalid_header) {
353 invalid_header = 1;
354 ND_PRINT("[Invalid header:");
355 } else
356 ND_PRINT(",");
357 ND_PRINT(" len(%u) < caplen(%u)", h->len, h->caplen);
358 }
359 if (h->caplen > MAXIMUM_SNAPLEN) {
360 if (!invalid_header) {
361 invalid_header = 1;
362 ND_PRINT("[Invalid header:");
363 } else
364 ND_PRINT(",");
365 ND_PRINT(" caplen(%u) > %u", h->caplen, MAXIMUM_SNAPLEN);
366 }
367 if (h->len > MAXIMUM_SNAPLEN) {
368 if (!invalid_header) {
369 invalid_header = 1;
370 ND_PRINT("[Invalid header:");
371 } else
372 ND_PRINT(",");
373 ND_PRINT(" len(%u) > %u", h->len, MAXIMUM_SNAPLEN);
374 }
375 if (invalid_header) {
376 ND_PRINT("]\n");
377 return;
378 }
379
380 /*
381 * At this point:
382 * capture length != 0,
383 * packet length != 0,
384 * capture length <= MAXIMUM_SNAPLEN,
385 * packet length <= MAXIMUM_SNAPLEN,
386 * packet length >= capture length.
387 *
388 * Currently, there is no D-Bus printer, thus no need for
389 * bigger lengths.
390 */
391
392 ts_print(ndo, &h->ts);
393
394 /*
395 * Printers must check that they're not walking off the end of
396 * the packet.
397 * Rather than pass it all the way down, we set this member
398 * of the netdissect_options structure.
399 */
400 ndo->ndo_snapend = sp + h->caplen;
401
402 ndo->ndo_protocol = "";
403 if (setjmp(ndo->ndo_truncated) == 0) {
404 /* Print the packet. */
405 hdrlen = (ndo->ndo_if_printer)(ndo, h, sp);
406 } else {
407 /* A printer quit because the packet was truncated; report it */
408 ND_PRINT(" [|%s]", ndo->ndo_protocol);
409 }
410
411 /*
412 * Free all pushed buffers; if we got here by a printer quitting,
413 * we need to release anything that didn't get released because
414 * we longjmped out of the code before it popped a buffer.
415 */
416 nd_pop_all_buffers(ndo);
417
418 /*
419 * Restore the original snapend, as a printer might have
420 * changed it.
421 */
422 ndo->ndo_snapend = sp + h->caplen;
423 if (ndo->ndo_Xflag) {
424 /*
425 * Print the raw packet data in hex and ASCII.
426 */
427 if (ndo->ndo_Xflag > 1) {
428 /*
429 * Include the link-layer header.
430 */
431 hex_and_ascii_print(ndo, "\n\t", sp, h->caplen);
432 } else {
433 /*
434 * Don't include the link-layer header - and if
435 * we have nothing past the link-layer header,
436 * print nothing.
437 */
438 if (h->caplen > hdrlen)
439 hex_and_ascii_print(ndo, "\n\t", sp + hdrlen,
440 h->caplen - hdrlen);
441 }
442 } else if (ndo->ndo_xflag) {
443 /*
444 * Print the raw packet data in hex.
445 */
446 if (ndo->ndo_xflag > 1) {
447 /*
448 * Include the link-layer header.
449 */
450 hex_print(ndo, "\n\t", sp, h->caplen);
451 } else {
452 /*
453 * Don't include the link-layer header - and if
454 * we have nothing past the link-layer header,
455 * print nothing.
456 */
457 if (h->caplen > hdrlen)
458 hex_print(ndo, "\n\t", sp + hdrlen,
459 h->caplen - hdrlen);
460 }
461 } else if (ndo->ndo_Aflag) {
462 /*
463 * Print the raw packet data in ASCII.
464 */
465 if (ndo->ndo_Aflag > 1) {
466 /*
467 * Include the link-layer header.
468 */
469 ascii_print(ndo, sp, h->caplen);
470 } else {
471 /*
472 * Don't include the link-layer header - and if
473 * we have nothing past the link-layer header,
474 * print nothing.
475 */
476 if (h->caplen > hdrlen)
477 ascii_print(ndo, sp + hdrlen, h->caplen - hdrlen);
478 }
479 }
480
481 ND_PRINT("\n");
482 nd_free_all(ndo);
483 }
484
485 /*
486 * By default, print the specified data out in hex and ASCII.
487 */
488 static void
489 ndo_default_print(netdissect_options *ndo, const u_char *bp, u_int length)
490 {
491 hex_and_ascii_print(ndo, "\n\t", bp, length); /* pass on lf and indentation string */
492 }
493
494 /* VARARGS */
495 static void
496 ndo_error(netdissect_options *ndo, status_exit_codes_t status,
497 const char *fmt, ...)
498 {
499 va_list ap;
500
501 if (ndo->program_name)
502 (void)fprintf(stderr, "%s: ", ndo->program_name);
503 va_start(ap, fmt);
504 (void)vfprintf(stderr, fmt, ap);
505 va_end(ap);
506 if (*fmt) {
507 fmt += strlen(fmt);
508 if (fmt[-1] != '\n')
509 (void)fputc('\n', stderr);
510 }
511 nd_cleanup();
512 exit(status);
513 /* NOTREACHED */
514 }
515
516 /* VARARGS */
517 static void
518 ndo_warning(netdissect_options *ndo, const char *fmt, ...)
519 {
520 va_list ap;
521
522 if (ndo->program_name)
523 (void)fprintf(stderr, "%s: ", ndo->program_name);
524 (void)fprintf(stderr, "WARNING: ");
525 va_start(ap, fmt);
526 (void)vfprintf(stderr, fmt, ap);
527 va_end(ap);
528 if (*fmt) {
529 fmt += strlen(fmt);
530 if (fmt[-1] != '\n')
531 (void)fputc('\n', stderr);
532 }
533 }
534
535 static int
536 ndo_printf(netdissect_options *ndo, const char *fmt, ...)
537 {
538 va_list args;
539 int ret;
540
541 va_start(args, fmt);
542 ret = vfprintf(stdout, fmt, args);
543 va_end(args);
544
545 if (ret < 0)
546 ndo_error(ndo, S_ERR_ND_WRITE_FILE,
547 "Unable to write output: %s", pcap_strerror(errno));
548 return (ret);
549 }
550
551 void
552 ndo_set_function_pointers(netdissect_options *ndo)
553 {
554 ndo->ndo_default_print=ndo_default_print;
555 ndo->ndo_printf=ndo_printf;
556 ndo->ndo_error=ndo_error;
557 ndo->ndo_warning=ndo_warning;
558 }