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