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