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