]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
Turn close_op into cleanup_op; the routine that handles it can also be
[libpcap] / savefile.c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
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 * savefile.c - supports offline use of tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
23 * Modified by Steve McCanne, LBL.
24 *
25 * Used to save the received packet headers, after filtering, to
26 * a file, and then read them later.
27 * The first record in the file contains saved values for the machine
28 * dependent values so we can print the dump file on any architecture.
29 */
30
31 #ifndef lint
32 static const char rcsid[] _U_ =
33 "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.168.2.7 2008-04-14 20:41:52 guy Exp $ (LBL)";
34 #endif
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <errno.h>
41 #include <memory.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "pcap-int.h"
47 #include "pcap/usb.h"
48
49 #ifdef HAVE_OS_PROTO_H
50 #include "os-proto.h"
51 #endif
52
53 /*
54 * Standard libpcap format.
55 */
56 #define TCPDUMP_MAGIC 0xa1b2c3d4
57
58 /*
59 * Alexey Kuznetzov's modified libpcap format.
60 */
61 #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
62
63 /*
64 * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt>
65 * for another modified format.
66 */
67 #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd
68
69 /*
70 * Navtel Communcations' format, with nanosecond timestamps,
71 * as per a request from Dumas Hwang <dumas.hwang@navtelcom.com>.
72 */
73 #define NAVTEL_TCPDUMP_MAGIC 0xa12b3c4d
74
75 /*
76 * Normal libpcap format, except for seconds/nanoseconds timestamps,
77 * as per a request by Ulf Lamping <ulf.lamping@web.de>
78 */
79 #define NSEC_TCPDUMP_MAGIC 0xa1b23c4d
80
81 /*
82 * We use the "receiver-makes-right" approach to byte order,
83 * because time is at a premium when we are writing the file.
84 * In other words, the pcap_file_header and pcap_pkthdr,
85 * records are written in host byte order.
86 * Note that the bytes of packet data are written out in the order in
87 * which they were received, so multi-byte fields in packets are not
88 * written in host byte order, they're written in whatever order the
89 * sending machine put them in.
90 *
91 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
92 * machine (if the file was written in little-end order).
93 */
94 #define SWAPLONG(y) \
95 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
96 #define SWAPSHORT(y) \
97 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
98
99 #define SFERR_TRUNC 1
100 #define SFERR_BADVERSION 2
101 #define SFERR_BADF 3
102 #define SFERR_EOF 4 /* not really an error, just a status */
103
104 /*
105 * Setting O_BINARY on DOS/Windows is a bit tricky
106 */
107 #if defined(WIN32)
108 #define SET_BINMODE(f) _setmode(_fileno(f), _O_BINARY)
109 #elif defined(MSDOS)
110 #if defined(__HIGHC__)
111 #define SET_BINMODE(f) setmode(f, O_BINARY)
112 #else
113 #define SET_BINMODE(f) setmode(fileno(f), O_BINARY)
114 #endif
115 #endif
116
117 /*
118 * We don't write DLT_* values to the capture file header, because
119 * they're not the same on all platforms.
120 *
121 * Unfortunately, the various flavors of BSD have not always used the same
122 * numerical values for the same data types, and various patches to
123 * libpcap for non-BSD OSes have added their own DLT_* codes for link
124 * layer encapsulation types seen on those OSes, and those codes have had,
125 * in some cases, values that were also used, on other platforms, for other
126 * link layer encapsulation types.
127 *
128 * This means that capture files of a type whose numerical DLT_* code
129 * means different things on different BSDs, or with different versions
130 * of libpcap, can't always be read on systems other than those like
131 * the one running on the machine on which the capture was made.
132 *
133 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
134 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
135 * codes to DLT_* codes when reading a savefile header.
136 *
137 * For those DLT_* codes that have, as far as we know, the same values on
138 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
139 * DLT_xxx; that way, captures of those types can still be read by
140 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
141 * captures of those types written by versions of libpcap that map DLT_
142 * values to LINKTYPE_ values can still be read by older versions
143 * of libpcap.
144 *
145 * The other LINKTYPE_* codes are given values starting at 100, in the
146 * hopes that no DLT_* code will be given one of those values.
147 *
148 * In order to ensure that a given LINKTYPE_* code's value will refer to
149 * the same encapsulation type on all platforms, you should not allocate
150 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
151 * The tcpdump developers will allocate a value for you, and will not
152 * subsequently allocate it to anybody else; that value will be added to
153 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
154 * libpcap release will include it.
155 *
156 * You should, if possible, also contribute patches to libpcap and tcpdump
157 * to handle the new encapsulation type, so that they can also be checked
158 * into the tcpdump.org CVS repository and so that they will appear in
159 * future libpcap and tcpdump releases.
160 *
161 * Do *NOT* assume that any values after the largest value in this file
162 * are available; you might not have the most up-to-date version of this
163 * file, and new values after that one might have been assigned. Also,
164 * do *NOT* use any values below 100 - those might already have been
165 * taken by one (or more!) organizations.
166 */
167 #define LINKTYPE_NULL DLT_NULL
168 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
169 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
170 #define LINKTYPE_AX25 DLT_AX25
171 #define LINKTYPE_PRONET DLT_PRONET
172 #define LINKTYPE_CHAOS DLT_CHAOS
173 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
174 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */
175 #define LINKTYPE_SLIP DLT_SLIP
176 #define LINKTYPE_PPP DLT_PPP
177 #define LINKTYPE_FDDI DLT_FDDI
178
179 /*
180 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
181 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
182 * field) at the beginning of the packet.
183 *
184 * This is for use when there is always such a header; the address field
185 * might be 0xff, for regular PPP, or it might be an address field for Cisco
186 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
187 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
188 *
189 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
190 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
191 * captures will be written out with a link type that NetBSD's tcpdump
192 * can read.
193 */
194 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
195
196 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
197
198 #define LINKTYPE_SYMANTEC_FIREWALL 99 /* Symantec Enterprise Firewall */
199
200 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
201 #define LINKTYPE_RAW 101 /* raw IP */
202 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
203 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
204 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
205 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
206 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
207 #define LINKTYPE_FRELAY 107 /* Frame Relay */
208 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
209 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
210
211 /*
212 * These three types are reserved for future use.
213 */
214 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
215 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
216 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
217
218 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
219 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
220 #define LINKTYPE_ECONET 115 /* Acorn Econet */
221
222 /*
223 * Reserved for use with OpenBSD ipfilter.
224 */
225 #define LINKTYPE_IPFILTER 116
226
227 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
228 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
229 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
230 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
231
232 /*
233 * Reserved for Siemens HiPath HDLC.
234 */
235 #define LINKTYPE_HHDLC 121
236
237 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
238 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
239
240 /*
241 * Reserved as per request from Kent Dahlgren <kent@praesum.com>
242 * for private use.
243 */
244 #define LINKTYPE_RIO 124 /* RapidIO */
245 #define LINKTYPE_PCI_EXP 125 /* PCI Express */
246 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */
247
248 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus BSD radio header */
249
250 /*
251 * Reserved for the TZSP encapsulation, as per request from
252 * Chris Waters <chris.waters@networkchemistry.com>
253 * TZSP is a generic encapsulation for any other link type,
254 * which includes a means to include meta-information
255 * with the packet, e.g. signal strength and channel
256 * for 802.11 packets.
257 */
258 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */
259
260 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */
261
262 /*
263 * Juniper-private data link types, as per request from
264 * Hannes Gredler <hannes@juniper.net>. The corresponding
265 * DLT_s are used for passing on chassis-internal
266 * metainformation such as QOS profiles, etc..
267 */
268 #define LINKTYPE_JUNIPER_MLPPP 130
269 #define LINKTYPE_JUNIPER_MLFR 131
270 #define LINKTYPE_JUNIPER_ES 132
271 #define LINKTYPE_JUNIPER_GGSN 133
272 #define LINKTYPE_JUNIPER_MFR 134
273 #define LINKTYPE_JUNIPER_ATM2 135
274 #define LINKTYPE_JUNIPER_SERVICES 136
275 #define LINKTYPE_JUNIPER_ATM1 137
276
277 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138 /* Apple IP-over-IEEE 1394 cooked header */
278
279 #define LINKTYPE_MTP2_WITH_PHDR 139
280 #define LINKTYPE_MTP2 140
281 #define LINKTYPE_MTP3 141
282 #define LINKTYPE_SCCP 142
283
284 #define LINKTYPE_DOCSIS 143 /* DOCSIS MAC frames */
285
286 #define LINKTYPE_LINUX_IRDA 144 /* Linux-IrDA */
287
288 /*
289 * Reserved for IBM SP switch and IBM Next Federation switch.
290 */
291 #define LINKTYPE_IBM_SP 145
292 #define LINKTYPE_IBM_SN 146
293
294 /*
295 * Reserved for private use. If you have some link-layer header type
296 * that you want to use within your organization, with the capture files
297 * using that link-layer header type not ever be sent outside your
298 * organization, you can use these values.
299 *
300 * No libpcap release will use these for any purpose, nor will any
301 * tcpdump release use them, either.
302 *
303 * Do *NOT* use these in capture files that you expect anybody not using
304 * your private versions of capture-file-reading tools to read; in
305 * particular, do *NOT* use them in products, otherwise you may find that
306 * people won't be able to use tcpdump, or snort, or Ethereal, or... to
307 * read capture files from your firewall/intrusion detection/traffic
308 * monitoring/etc. appliance, or whatever product uses that LINKTYPE_ value,
309 * and you may also find that the developers of those applications will
310 * not accept patches to let them read those files.
311 *
312 * Also, do not use them if somebody might send you a capture using them
313 * for *their* private type and tools using them for *your* private type
314 * would have to read them.
315 *
316 * Instead, in those cases, ask "tcpdump-workers@tcpdump.org" for a new DLT_
317 * and LINKTYPE_ value, as per the comment in pcap/bpf.h, and use the type
318 * you're given.
319 */
320 #define LINKTYPE_USER0 147
321 #define LINKTYPE_USER1 148
322 #define LINKTYPE_USER2 149
323 #define LINKTYPE_USER3 150
324 #define LINKTYPE_USER4 151
325 #define LINKTYPE_USER5 152
326 #define LINKTYPE_USER6 153
327 #define LINKTYPE_USER7 154
328 #define LINKTYPE_USER8 155
329 #define LINKTYPE_USER9 156
330 #define LINKTYPE_USER10 157
331 #define LINKTYPE_USER11 158
332 #define LINKTYPE_USER12 159
333 #define LINKTYPE_USER13 160
334 #define LINKTYPE_USER14 161
335 #define LINKTYPE_USER15 162
336
337 /*
338 * For future use with 802.11 captures - defined by AbsoluteValue
339 * Systems to store a number of bits of link-layer information
340 * including radio information:
341 *
342 * http://www.shaftnet.org/~pizza/software/capturefrm.txt
343 *
344 * but could and arguably should also be used by non-AVS Linux
345 * 802.11 drivers; that may happen in the future.
346 */
347 #define LINKTYPE_IEEE802_11_RADIO_AVS 163 /* 802.11 plus AVS radio header */
348
349 /*
350 * Juniper-private data link type, as per request from
351 * Hannes Gredler <hannes@juniper.net>. The corresponding
352 * DLT_s are used for passing on chassis-internal
353 * metainformation such as QOS profiles, etc..
354 */
355 #define LINKTYPE_JUNIPER_MONITOR 164
356
357 /*
358 * Reserved for BACnet MS/TP.
359 */
360 #define LINKTYPE_BACNET_MS_TP 165
361
362 /*
363 * Another PPP variant as per request from Karsten Keil <kkeil@suse.de>.
364 *
365 * This is used in some OSes to allow a kernel socket filter to distinguish
366 * between incoming and outgoing packets, on a socket intended to
367 * supply pppd with outgoing packets so it can do dial-on-demand and
368 * hangup-on-lack-of-demand; incoming packets are filtered out so they
369 * don't cause pppd to hold the connection up (you don't want random
370 * input packets such as port scans, packets from old lost connections,
371 * etc. to force the connection to stay up).
372 *
373 * The first byte of the PPP header (0xff03) is modified to accomodate
374 * the direction - 0x00 = IN, 0x01 = OUT.
375 */
376 #define LINKTYPE_PPP_PPPD 166
377
378 /*
379 * Juniper-private data link type, as per request from
380 * Hannes Gredler <hannes@juniper.net>. The DLT_s are used
381 * for passing on chassis-internal metainformation such as
382 * QOS profiles, cookies, etc..
383 */
384 #define LINKTYPE_JUNIPER_PPPOE 167
385 #define LINKTYPE_JUNIPER_PPPOE_ATM 168
386
387 #define LINKTYPE_GPRS_LLC 169 /* GPRS LLC */
388 #define LINKTYPE_GPF_T 170 /* GPF-T (ITU-T G.7041/Y.1303) */
389 #define LINKTYPE_GPF_F 171 /* GPF-T (ITU-T G.7041/Y.1303) */
390
391 /*
392 * Requested by Oolan Zimmer <oz@gcom.com> for use in Gcom's T1/E1 line
393 * monitoring equipment.
394 */
395 #define LINKTYPE_GCOM_T1E1 172
396 #define LINKTYPE_GCOM_SERIAL 173
397
398 /*
399 * Juniper-private data link type, as per request from
400 * Hannes Gredler <hannes@juniper.net>. The DLT_ is used
401 * for internal communication to Physical Interface Cards (PIC)
402 */
403 #define LINKTYPE_JUNIPER_PIC_PEER 174
404
405 /*
406 * Link types requested by Gregor Maier <gregor@endace.com> of Endace
407 * Measurement Systems. They add an ERF header (see
408 * http://www.endace.com/support/EndaceRecordFormat.pdf) in front of
409 * the link-layer header.
410 */
411 #define LINKTYPE_ERF_ETH 175 /* Ethernet */
412 #define LINKTYPE_ERF_POS 176 /* Packet-over-SONET */
413
414 /*
415 * Requested by Daniele Orlandi <daniele@orlandi.com> for raw LAPD
416 * for vISDN (http://www.orlandi.com/visdn/). Its link-layer header
417 * includes additional information before the LAPD header, so it's
418 * not necessarily a generic LAPD header.
419 */
420 #define LINKTYPE_LINUX_LAPD 177
421
422 /*
423 * Juniper-private data link type, as per request from
424 * Hannes Gredler <hannes@juniper.net>.
425 * The Link Types are used for prepending meta-information
426 * like interface index, interface name
427 * before standard Ethernet, PPP, Frelay & C-HDLC Frames
428 */
429 #define LINKTYPE_JUNIPER_ETHER 178
430 #define LINKTYPE_JUNIPER_PPP 179
431 #define LINKTYPE_JUNIPER_FRELAY 180
432 #define LINKTYPE_JUNIPER_CHDLC 181
433
434 /*
435 * Multi Link Frame Relay (FRF.16)
436 */
437 #define LINKTYPE_MFR 182
438
439 /*
440 * Juniper-private data link type, as per request from
441 * Hannes Gredler <hannes@juniper.net>.
442 * The DLT_ is used for internal communication with a
443 * voice Adapter Card (PIC)
444 */
445 #define LINKTYPE_JUNIPER_VP 183
446
447 /*
448 * Arinc 429 frames.
449 * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
450 * Every frame contains a 32bit A429 label.
451 * More documentation on Arinc 429 can be found at
452 * http://www.condoreng.com/support/downloads/tutorials/ARINCTutorial.pdf
453 */
454 #define LINKTYPE_A429 184
455
456 /*
457 * Arinc 653 Interpartition Communication messages.
458 * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
459 * Please refer to the A653-1 standard for more information.
460 */
461 #define LINKTYPE_A653_ICM 185
462
463 /*
464 * USB packets, beginning with a USB setup header; requested by
465 * Paolo Abeni <paolo.abeni@email.it>.
466 */
467 #define LINKTYPE_USB 186
468
469 /*
470 * Bluetooth HCI UART transport layer (part H:4); requested by
471 * Paolo Abeni.
472 */
473 #define LINKTYPE_BLUETOOTH_HCI_H4 187
474
475 /*
476 * IEEE 802.16 MAC Common Part Sublayer; requested by Maria Cruz
477 * <cruz_petagay@bah.com>.
478 */
479 #define LINKTYPE_IEEE802_16_MAC_CPS 188
480
481 /*
482 * USB packets, beginning with a Linux USB header; requested by
483 * Paolo Abeni <paolo.abeni@email.it>.
484 */
485 #define LINKTYPE_USB_LINUX 189
486
487 /*
488 * Controller Area Network (CAN) v. 2.0B packets.
489 * DLT_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
490 * Used to dump CAN packets coming from a CAN Vector board.
491 * More documentation on the CAN v2.0B frames can be found at
492 * http://www.can-cia.org/downloads/?269
493 */
494 #define LINKTYPE_CAN20B 190
495
496 /*
497 * IEEE 802.15.4, with address fields padded, as is done by Linux
498 * drivers; requested by Juergen Schimmer.
499 */
500 #define LINKTYPE_IEEE802_15_4_LINUX 191
501
502 /*
503 * Per Packet Information encapsulated packets.
504 * LINKTYPE_ requested by Gianluca Varenni <gianluca.varenni@cacetech.com>.
505 */
506 #define LINKTYPE_PPI 192
507
508 /*
509 * Header for 802.16 MAC Common Part Sublayer plus a radiotap radio header;
510 * requested by Charles Clancy.
511 */
512 #define LINKTYPE_IEEE802_16_MAC_CPS_RADIO 193
513
514 /*
515 * Juniper-private data link type, as per request from
516 * Hannes Gredler <hannes@juniper.net>.
517 * The DLT_ is used for internal communication with a
518 * integrated service module (ISM).
519 */
520 #define LINKTYPE_JUNIPER_ISM 194
521
522 /*
523 * IEEE 802.15.4, exactly as it appears in the spec (no padding, no
524 * nothing); requested by Mikko Saarnivala <mikko.saarnivala@sensinode.com>.
525 */
526 #define LINKTYPE_IEEE802_15_4 195
527
528 /*
529 * Various link-layer types, with a pseudo-header, for SITA
530 * (http://www.sita.aero/); requested by Fulko Hew (fulko.hew@gmail.com).
531 */
532 #define LINKTYPE_SITA 196
533
534 /*
535 * Various link-layer types, with a pseudo-header, for Endace DAG cards;
536 * encapsulates Endace ERF records. Requested by Stephen Donnelly
537 * <stephen@endace.com>.
538 */
539 #define LINKTYPE_ERF 197
540
541 /*
542 * Special header prepended to Ethernet packets when capturing from a
543 * u10 Networks board. Requested by Phil Mulholland
544 * <phil@u10networks.com>.
545 */
546 #define LINKTYPE_RAIF1 198
547
548 /*
549 * IPMB packet for IPMI, beginning with the I2C slave address, followed
550 * by the netFn and LUN, etc.. Requested by Chanthy Toeung
551 * <chanthy.toeung@ca.kontron.com>.
552 */
553 #define LINKTYPE_IPMB 199
554
555 /*
556 * Juniper-private data link type, as per request from
557 * Hannes Gredler <hannes@juniper.net>.
558 * The DLT_ is used for capturing data on a secure tunnel interface.
559 */
560 #define LINKTYPE_JUNIPER_ST 200
561
562 /*
563 * Bluetooth HCI UART transport layer (part H:4), with pseudo-header
564 * that includes direction information; requested by Paolo Abeni.
565 */
566 #define LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR 201
567
568 /*
569 * AX.25 packet with a 1-byte KISS header; see
570 *
571 * http://www.ax25.net/kiss.htm
572 *
573 * as per Richard Stearn <richard@rns-stearn.demon.co.uk>.
574 */
575 #define LINKTYPE_AX25_KISS 202
576
577 /*
578 * LAPD packets from an ISDN channel, starting with the address field,
579 * with no pseudo-header.
580 * Requested by Varuna De Silva <varunax@gmail.com>.
581 */
582 #define LINKTYPE_LAPD 203
583
584 /*
585 * Variants of various link-layer headers, with a one-byte direction
586 * pseudo-header prepended - zero means "received by this host",
587 * non-zero (any non-zero value) means "sent by this host" - as per
588 * Will Barker <w.barker@zen.co.uk>.
589 */
590 #define LINKTYPE_PPP_WITH_DIR 204 /* PPP */
591 #define LINKTYPE_C_HDLC_WITH_DIR 205 /* Cisco HDLC */
592 #define LINKTYPE_FRELAY_WITH_DIR 206 /* Frame Relay */
593 #define LINKTYPE_LAPB_WITH_DIR 207 /* LAPB */
594
595 /*
596 * 208 is reserved for an as-yet-unspecified proprietary link-layer
597 * type, as requested by Will Barker.
598 */
599
600 /*
601 * IPMB with a Linux-specific pseudo-header; as requested by Alexey Neyman
602 * <avn@pigeonpoint.com>.
603 */
604 #define LINKTYPE_IPMB_LINUX 209
605
606 /*
607 * FlexRay automotive bus - http://www.flexray.com/ - as requested
608 * by Hannes Kaelber <hannes.kaelber@x2e.de>.
609 */
610 #define LINKTYPE_FLEXRAY 210
611
612 /*
613 * Media Oriented Systems Transport (MOST) bus for multimedia
614 * transport - http://www.mostcooperation.com/ - as requested
615 * by Hannes Kaelber <hannes.kaelber@x2e.de>.
616 */
617 #define LINKTYPE_MOST 211
618
619 /*
620 * Local Interconnect Network (LIN) bus for vehicle networks -
621 * http://www.lin-subbus.org/ - as requested by Hannes Kaelber
622 * <hannes.kaelber@x2e.de>.
623 */
624 #define LINKTYPE_LIN 212
625
626 /*
627 * X2E-private data link type used for serial line capture,
628 * as requested by Hannes Kaelber <hannes.kaelber@x2e.de>.
629 */
630 #define LINKTYPE_X2E_SERIAL 213
631
632 /*
633 * X2E-private data link type used for the Xoraya data logger
634 * family, as requested by Hannes Kaelber <hannes.kaelber@x2e.de>.
635 */
636 #define LINKTYPE_X2E_XORAYA 214
637
638
639 static struct linktype_map {
640 int dlt;
641 int linktype;
642 } map[] = {
643 /*
644 * These DLT_* codes have LINKTYPE_* codes with values identical
645 * to the values of the corresponding DLT_* code.
646 */
647 { DLT_NULL, LINKTYPE_NULL },
648 { DLT_EN10MB, LINKTYPE_ETHERNET },
649 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET },
650 { DLT_AX25, LINKTYPE_AX25 },
651 { DLT_PRONET, LINKTYPE_PRONET },
652 { DLT_CHAOS, LINKTYPE_CHAOS },
653 { DLT_IEEE802, LINKTYPE_TOKEN_RING },
654 { DLT_ARCNET, LINKTYPE_ARCNET },
655 { DLT_SLIP, LINKTYPE_SLIP },
656 { DLT_PPP, LINKTYPE_PPP },
657 { DLT_FDDI, LINKTYPE_FDDI },
658
659 /*
660 * These DLT_* codes have different values on different
661 * platforms; we map them to LINKTYPE_* codes that
662 * have values that should never be equal to any DLT_*
663 * code.
664 */
665 #ifdef DLT_FR
666 /* BSD/OS Frame Relay */
667 { DLT_FR, LINKTYPE_FRELAY },
668 #endif
669
670 { DLT_SYMANTEC_FIREWALL, LINKTYPE_SYMANTEC_FIREWALL },
671 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 },
672 { DLT_RAW, LINKTYPE_RAW },
673 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS },
674 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS },
675
676 /* BSD/OS Cisco HDLC */
677 { DLT_C_HDLC, LINKTYPE_C_HDLC },
678
679 /*
680 * These DLT_* codes are not on all platforms, but, so far,
681 * there don't appear to be any platforms that define
682 * other codes with those values; we map them to
683 * different LINKTYPE_* values anyway, just in case.
684 */
685
686 /* Linux ATM Classical IP */
687 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP },
688
689 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
690 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC },
691
692 /* NetBSD PPP over Ethernet */
693 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER },
694
695 /* IEEE 802.11 wireless */
696 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 },
697
698 /* Frame Relay */
699 { DLT_FRELAY, LINKTYPE_FRELAY },
700
701 /* OpenBSD loopback */
702 { DLT_LOOP, LINKTYPE_LOOP },
703
704 /* Linux cooked socket capture */
705 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL },
706
707 /* Apple LocalTalk hardware */
708 { DLT_LTALK, LINKTYPE_LTALK },
709
710 /* Acorn Econet */
711 { DLT_ECONET, LINKTYPE_ECONET },
712
713 /* OpenBSD DLT_PFLOG */
714 { DLT_PFLOG, LINKTYPE_PFLOG },
715
716 /* For Cisco-internal use */
717 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS },
718
719 /* Prism II monitor-mode header plus 802.11 header */
720 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER },
721
722 /* FreeBSD Aironet driver stuff */
723 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER },
724
725 /* Siemens HiPath HDLC */
726 { DLT_HHDLC, LINKTYPE_HHDLC },
727
728 /* RFC 2625 IP-over-Fibre Channel */
729 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC },
730
731 /* Solaris+SunATM */
732 { DLT_SUNATM, LINKTYPE_SUNATM },
733
734 /* RapidIO */
735 { DLT_RIO, LINKTYPE_RIO },
736
737 /* PCI Express */
738 { DLT_PCI_EXP, LINKTYPE_PCI_EXP },
739
740 /* Xilinx Aurora link layer */
741 { DLT_AURORA, LINKTYPE_AURORA },
742
743 /* 802.11 plus BSD radio header */
744 { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO },
745
746 /* Tazmen Sniffer Protocol */
747 { DLT_TZSP, LINKTYPE_TZSP },
748
749 /* Arcnet with Linux-style link-layer headers */
750 { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX },
751
752 /* Juniper-internal chassis encapsulation */
753 { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP },
754 { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR },
755 { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES },
756 { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN },
757 { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR },
758 { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 },
759 { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES },
760 { DLT_JUNIPER_ATM1, LINKTYPE_JUNIPER_ATM1 },
761
762 /* Apple IP-over-IEEE 1394 cooked header */
763 { DLT_APPLE_IP_OVER_IEEE1394, LINKTYPE_APPLE_IP_OVER_IEEE1394 },
764
765 /* SS7 */
766 { DLT_MTP2_WITH_PHDR, LINKTYPE_MTP2_WITH_PHDR },
767 { DLT_MTP2, LINKTYPE_MTP2 },
768 { DLT_MTP3, LINKTYPE_MTP3 },
769 { DLT_SCCP, LINKTYPE_SCCP },
770
771 /* DOCSIS MAC frames */
772 { DLT_DOCSIS, LINKTYPE_DOCSIS },
773
774 /* IrDA IrLAP packets + Linux-cooked header */
775 { DLT_LINUX_IRDA, LINKTYPE_LINUX_IRDA },
776
777 /* IBM SP and Next Federation switches */
778 { DLT_IBM_SP, LINKTYPE_IBM_SP },
779 { DLT_IBM_SN, LINKTYPE_IBM_SN },
780
781 /* 802.11 plus AVS radio header */
782 { DLT_IEEE802_11_RADIO_AVS, LINKTYPE_IEEE802_11_RADIO_AVS },
783
784 /*
785 * Any platform that defines additional DLT_* codes should:
786 *
787 * request a LINKTYPE_* code and value from tcpdump.org,
788 * as per the above;
789 *
790 * add, in their version of libpcap, an entry to map
791 * those DLT_* codes to the corresponding LINKTYPE_*
792 * code;
793 *
794 * redefine, in their "net/bpf.h", any DLT_* values
795 * that collide with the values used by their additional
796 * DLT_* codes, to remove those collisions (but without
797 * making them collide with any of the LINKTYPE_*
798 * values equal to 50 or above; they should also avoid
799 * defining DLT_* values that collide with those
800 * LINKTYPE_* values, either).
801 */
802
803 /* Juniper-internal chassis encapsulation */
804 { DLT_JUNIPER_MONITOR, LINKTYPE_JUNIPER_MONITOR },
805
806 /* BACnet MS/TP */
807 { DLT_BACNET_MS_TP, LINKTYPE_BACNET_MS_TP },
808
809 /* PPP for pppd, with direction flag in the PPP header */
810 { DLT_PPP_PPPD, LINKTYPE_PPP_PPPD},
811
812 /* Juniper-internal chassis encapsulation */
813 { DLT_JUNIPER_PPPOE, LINKTYPE_JUNIPER_PPPOE },
814 { DLT_JUNIPER_PPPOE_ATM,LINKTYPE_JUNIPER_PPPOE_ATM },
815
816 /* GPRS LLC */
817 { DLT_GPRS_LLC, LINKTYPE_GPRS_LLC },
818
819 /* Transparent Generic Framing Procedure (ITU-T G.7041/Y.1303) */
820 { DLT_GPF_T, LINKTYPE_GPF_T },
821
822 /* Framed Generic Framing Procedure (ITU-T G.7041/Y.1303) */
823 { DLT_GPF_F, LINKTYPE_GPF_F },
824
825 { DLT_GCOM_T1E1, LINKTYPE_GCOM_T1E1 },
826 { DLT_GCOM_SERIAL, LINKTYPE_GCOM_SERIAL },
827
828 /* Juniper-internal chassis encapsulation */
829 { DLT_JUNIPER_PIC_PEER, LINKTYPE_JUNIPER_PIC_PEER },
830
831 /* Endace types */
832 { DLT_ERF_ETH, LINKTYPE_ERF_ETH },
833 { DLT_ERF_POS, LINKTYPE_ERF_POS },
834
835 /* viSDN LAPD */
836 { DLT_LINUX_LAPD, LINKTYPE_LINUX_LAPD },
837
838 /* Juniper meta-information before Ether, PPP, Frame Relay, C-HDLC Frames */
839 { DLT_JUNIPER_ETHER, LINKTYPE_JUNIPER_ETHER },
840 { DLT_JUNIPER_PPP, LINKTYPE_JUNIPER_PPP },
841 { DLT_JUNIPER_FRELAY, LINKTYPE_JUNIPER_FRELAY },
842 { DLT_JUNIPER_CHDLC, LINKTYPE_JUNIPER_CHDLC },
843
844 /* Multi Link Frame Relay (FRF.16) */
845 { DLT_MFR, LINKTYPE_MFR },
846
847 /* Juniper Voice PIC */
848 { DLT_JUNIPER_VP, LINKTYPE_JUNIPER_VP },
849
850 /* Controller Area Network (CAN) v2.0B */
851 { DLT_A429, LINKTYPE_A429 },
852
853 /* Arinc 653 Interpartition Communication messages */
854 { DLT_A653_ICM, LINKTYPE_A653_ICM },
855
856 /* USB */
857 { DLT_USB, LINKTYPE_USB },
858
859 /* Bluetooth HCI UART transport layer */
860 { DLT_BLUETOOTH_HCI_H4, LINKTYPE_BLUETOOTH_HCI_H4 },
861
862 /* IEEE 802.16 MAC Common Part Sublayer */
863 { DLT_IEEE802_16_MAC_CPS, LINKTYPE_IEEE802_16_MAC_CPS },
864
865 /* USB with Linux header */
866 { DLT_USB_LINUX, LINKTYPE_USB_LINUX },
867
868 /* Controller Area Network (CAN) v2.0B */
869 { DLT_CAN20B, LINKTYPE_CAN20B },
870
871 /* IEEE 802.15.4 with address fields padded */
872 { DLT_IEEE802_15_4_LINUX, LINKTYPE_IEEE802_15_4_LINUX },
873
874 /* Per Packet Information encapsulated packets */
875 { DLT_PPI, LINKTYPE_PPI },
876
877 /* IEEE 802.16 MAC Common Part Sublayer plus radiotap header */
878 { DLT_IEEE802_16_MAC_CPS_RADIO, LINKTYPE_IEEE802_16_MAC_CPS_RADIO },
879
880 /* Juniper Voice ISM */
881 { DLT_JUNIPER_ISM, LINKTYPE_JUNIPER_ISM },
882
883 /* IEEE 802.15.4 exactly as it appears in the spec */
884 { DLT_IEEE802_15_4, LINKTYPE_IEEE802_15_4 },
885
886 /* Various link-layer types for SITA */
887 { DLT_SITA, LINKTYPE_SITA },
888
889 /* Various link-layer types for Endace */
890 { DLT_ERF, LINKTYPE_ERF },
891
892 /* Special header for u10 Networks boards */
893 { DLT_RAIF1, LINKTYPE_RAIF1 },
894
895 /* IPMB */
896 { DLT_IPMB, LINKTYPE_IPMB },
897
898 /* Juniper Secure Tunnel */
899 { DLT_JUNIPER_ST, LINKTYPE_JUNIPER_ST },
900
901 /* Bluetooth HCI UART transport layer, with pseudo-header */
902 { DLT_BLUETOOTH_HCI_H4_WITH_PHDR, LINKTYPE_BLUETOOTH_HCI_H4_WITH_PHDR },
903
904 /* AX.25 with KISS header */
905 { DLT_AX25_KISS, LINKTYPE_AX25_KISS },
906
907 /* Raw LAPD, with no pseudo-header */
908 { DLT_LAPD, LINKTYPE_LAPD },
909
910 /* PPP with one-byte pseudo-header giving direction */
911 { DLT_PPP_WITH_DIR, LINKTYPE_PPP_WITH_DIR },
912
913 /* Cisco HDLC with one-byte pseudo-header giving direction */
914 { DLT_C_HDLC_WITH_DIR, LINKTYPE_C_HDLC_WITH_DIR },
915
916 /* Frame Relay with one-byte pseudo-header giving direction */
917 { DLT_FRELAY_WITH_DIR, LINKTYPE_FRELAY_WITH_DIR },
918
919 /* LAPB with one-byte pseudo-header giving direction */
920 { DLT_LAPB_WITH_DIR, LINKTYPE_LAPB_WITH_DIR },
921
922 /* IPMB with Linux pseudo-header */
923 { DLT_IPMB_LINUX, LINKTYPE_IPMB_LINUX },
924
925 /* FlexRay */
926 { DLT_FLEXRAY, LINKTYPE_FLEXRAY },
927
928 /* MOST */
929 { DLT_MOST, LINKTYPE_MOST },
930
931 /* LIN */
932 { DLT_LIN, LINKTYPE_LIN },
933
934 /* X2E-private serial line capture */
935 { DLT_X2E_SERIAL, LINKTYPE_X2E_SERIAL },
936
937 /* X2E-private for Xoraya data logger family */
938 { DLT_X2E_XORAYA, LINKTYPE_X2E_XORAYA },
939
940 { -1, -1 }
941 };
942
943 /*
944 * Mechanism for storing information about a capture in the upper
945 * 6 bits of a linktype value in a capture file.
946 *
947 * LT_LINKTYPE_EXT(x) extracts the additional information.
948 *
949 * The rest of the bits are for a value describing the link-layer
950 * value. LT_LINKTYPE(x) extracts that value.
951 */
952 #define LT_LINKTYPE(x) ((x) & 0x03FFFFFF)
953 #define LT_LINKTYPE_EXT(x) ((x) & 0xFC000000)
954
955 static int
956 dlt_to_linktype(int dlt)
957 {
958 int i;
959
960 for (i = 0; map[i].dlt != -1; i++) {
961 if (map[i].dlt == dlt)
962 return (map[i].linktype);
963 }
964
965 /*
966 * If we don't have a mapping for this DLT_ code, return an
967 * error; that means that the table above needs to have an
968 * entry added.
969 */
970 return (-1);
971 }
972
973 static int
974 linktype_to_dlt(int linktype)
975 {
976 int i;
977
978 for (i = 0; map[i].linktype != -1; i++) {
979 if (map[i].linktype == linktype)
980 return (map[i].dlt);
981 }
982
983 /*
984 * If we don't have an entry for this link type, return
985 * the link type value; it may be a DLT_ value from an
986 * older version of libpcap.
987 */
988 return linktype;
989 }
990
991 static int
992 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
993 {
994 struct pcap_file_header hdr;
995
996 hdr.magic = TCPDUMP_MAGIC;
997 hdr.version_major = PCAP_VERSION_MAJOR;
998 hdr.version_minor = PCAP_VERSION_MINOR;
999
1000 hdr.thiszone = thiszone;
1001 hdr.snaplen = snaplen;
1002 hdr.sigfigs = 0;
1003 hdr.linktype = linktype;
1004
1005 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
1006 return (-1);
1007
1008 return (0);
1009 }
1010
1011 static void
1012 swap_hdr(struct pcap_file_header *hp)
1013 {
1014 hp->version_major = SWAPSHORT(hp->version_major);
1015 hp->version_minor = SWAPSHORT(hp->version_minor);
1016 hp->thiszone = SWAPLONG(hp->thiszone);
1017 hp->sigfigs = SWAPLONG(hp->sigfigs);
1018 hp->snaplen = SWAPLONG(hp->snaplen);
1019 hp->linktype = SWAPLONG(hp->linktype);
1020 }
1021
1022 static int
1023 sf_getnonblock(pcap_t *p, char *errbuf)
1024 {
1025 /*
1026 * This is a savefile, not a live capture file, so never say
1027 * it's in non-blocking mode.
1028 */
1029 return (0);
1030 }
1031
1032 static int
1033 sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
1034 {
1035 /*
1036 * This is a savefile, not a live capture file, so ignore
1037 * requests to put it in non-blocking mode.
1038 */
1039 return (0);
1040 }
1041
1042 static int
1043 sf_stats(pcap_t *p, struct pcap_stat *ps)
1044 {
1045 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1046 "Statistics aren't available from savefiles");
1047 return (-1);
1048 }
1049
1050 #ifdef WIN32
1051 static int
1052 sf_setbuff(pcap_t *p, int dim)
1053 {
1054 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1055 "The kernel buffer size cannot be set while reading from a file");
1056 return (-1);
1057 }
1058
1059 static int
1060 sf_setmode(pcap_t *p, int mode)
1061 {
1062 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1063 "impossible to set mode while reading from a file");
1064 return (-1);
1065 }
1066
1067 static int
1068 sf_setmintocopy(pcap_t *p, int size)
1069 {
1070 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1071 "The mintocopy parameter cannot be set while reading from a file");
1072 return (-1);
1073 }
1074 #endif
1075
1076 static int
1077 sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
1078 {
1079 strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
1080 PCAP_ERRBUF_SIZE);
1081 return (-1);
1082 }
1083
1084 /*
1085 * Set direction flag: Which packets do we accept on a forwarding
1086 * single device? IN, OUT or both?
1087 */
1088 static int
1089 sf_setdirection(pcap_t *p, pcap_direction_t d)
1090 {
1091 snprintf(p->errbuf, sizeof(p->errbuf),
1092 "Setting direction is not supported on savefiles");
1093 return (-1);
1094 }
1095
1096 static void
1097 sf_cleanup(pcap_t *p)
1098 {
1099 if (p->sf.rfile != stdin)
1100 (void)fclose(p->sf.rfile);
1101 if (p->sf.base != NULL)
1102 free(p->sf.base);
1103 }
1104
1105 pcap_t *
1106 pcap_open_offline(const char *fname, char *errbuf)
1107 {
1108 FILE *fp;
1109 pcap_t *p;
1110
1111 if (fname[0] == '-' && fname[1] == '\0')
1112 {
1113 fp = stdin;
1114 #if defined(WIN32) || defined(MSDOS)
1115 /*
1116 * We're reading from the standard input, so put it in binary
1117 * mode, as savefiles are binary files.
1118 */
1119 SET_BINMODE(fp);
1120 #endif
1121 }
1122 else {
1123 #if !defined(WIN32) && !defined(MSDOS)
1124 fp = fopen(fname, "r");
1125 #else
1126 fp = fopen(fname, "rb");
1127 #endif
1128 if (fp == NULL) {
1129 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
1130 pcap_strerror(errno));
1131 return (NULL);
1132 }
1133 }
1134 p = pcap_fopen_offline(fp, errbuf);
1135 if (p == NULL) {
1136 if (fp != stdin)
1137 fclose(fp);
1138 }
1139 return (p);
1140 }
1141
1142 pcap_t *
1143 pcap_fopen_offline(FILE *fp, char *errbuf)
1144 {
1145 register pcap_t *p;
1146 struct pcap_file_header hdr;
1147 size_t amt_read;
1148 bpf_u_int32 magic;
1149 int linklen;
1150
1151 p = (pcap_t *)malloc(sizeof(*p));
1152 if (p == NULL) {
1153 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
1154 return (NULL);
1155 }
1156
1157 memset((char *)p, 0, sizeof(*p));
1158
1159 amt_read = fread((char *)&hdr, 1, sizeof(hdr), fp);
1160 if (amt_read != sizeof(hdr)) {
1161 if (ferror(fp)) {
1162 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1163 "error reading dump file: %s",
1164 pcap_strerror(errno));
1165 } else {
1166 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1167 "truncated dump file; tried to read %lu file header bytes, only got %lu",
1168 (unsigned long)sizeof(hdr),
1169 (unsigned long)amt_read);
1170 }
1171 goto bad;
1172 }
1173 magic = hdr.magic;
1174 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
1175 magic = SWAPLONG(magic);
1176 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
1177 snprintf(errbuf, PCAP_ERRBUF_SIZE,
1178 "bad dump file format");
1179 goto bad;
1180 }
1181 p->sf.swapped = 1;
1182 swap_hdr(&hdr);
1183 }
1184 if (magic == KUZNETZOV_TCPDUMP_MAGIC) {
1185 /*
1186 * XXX - the patch that's in some versions of libpcap
1187 * changes the packet header but not the magic number,
1188 * and some other versions with this magic number have
1189 * some extra debugging information in the packet header;
1190 * we'd have to use some hacks^H^H^H^H^Hheuristics to
1191 * detect those variants.
1192 *
1193 * Ethereal does that, but it does so by trying to read
1194 * the first two packets of the file with each of the
1195 * record header formats. That currently means it seeks
1196 * backwards and retries the reads, which doesn't work
1197 * on pipes. We want to be able to read from a pipe, so
1198 * that strategy won't work; we'd have to buffer some
1199 * data ourselves and read from that buffer in order to
1200 * make that work.
1201 */
1202 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
1203 } else
1204 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
1205 if (hdr.version_major < PCAP_VERSION_MAJOR) {
1206 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
1207 goto bad;
1208 }
1209 p->tzoff = hdr.thiszone;
1210 p->snapshot = hdr.snaplen;
1211 p->linktype = linktype_to_dlt(LT_LINKTYPE(hdr.linktype));
1212 p->linktype_ext = LT_LINKTYPE_EXT(hdr.linktype);
1213 if (magic == KUZNETZOV_TCPDUMP_MAGIC && p->linktype == DLT_EN10MB) {
1214 /*
1215 * This capture might have been done in raw mode or cooked
1216 * mode.
1217 *
1218 * If it was done in cooked mode, p->snapshot was passed
1219 * to recvfrom() as the buffer size, meaning that the
1220 * most packet data that would be copied would be
1221 * p->snapshot. However, a faked Ethernet header would
1222 * then have been added to it, so the most data that would
1223 * be in a packet in the file would be p->snapshot + 14.
1224 *
1225 * We can't easily tell whether the capture was done in
1226 * raw mode or cooked mode, so we'll assume it was
1227 * cooked mode, and add 14 to the snapshot length. That
1228 * means that, for a raw capture, the snapshot length will
1229 * be misleading if you use it to figure out why a capture
1230 * doesn't have all the packet data, but there's not much
1231 * we can do to avoid that.
1232 */
1233 p->snapshot += 14;
1234 }
1235 p->sf.rfile = fp;
1236 #ifndef WIN32
1237 p->bufsize = hdr.snaplen;
1238 #else
1239 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
1240 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr);
1241 #endif
1242
1243 /* Align link header as required for proper data alignment */
1244 /* XXX should handle all types */
1245 switch (p->linktype) {
1246
1247 case DLT_EN10MB:
1248 linklen = 14;
1249 break;
1250
1251 case DLT_FDDI:
1252 linklen = 13 + 8; /* fddi_header + llc */
1253 break;
1254
1255 case DLT_NULL:
1256 default:
1257 linklen = 0;
1258 break;
1259 }
1260
1261 if (p->bufsize < 0)
1262 p->bufsize = BPF_MAXBUFSIZE;
1263 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
1264 if (p->sf.base == NULL) {
1265 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
1266 goto bad;
1267 }
1268 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
1269 p->sf.version_major = hdr.version_major;
1270 p->sf.version_minor = hdr.version_minor;
1271 #ifdef PCAP_FDDIPAD
1272 /* Padding only needed for live capture fcode */
1273 p->fddipad = 0;
1274 #endif
1275
1276 /*
1277 * We interchanged the caplen and len fields at version 2.3,
1278 * in order to match the bpf header layout. But unfortunately
1279 * some files were written with version 2.3 in their headers
1280 * but without the interchanged fields.
1281 *
1282 * In addition, DG/UX tcpdump writes out files with a version
1283 * number of 543.0, and with the caplen and len fields in the
1284 * pre-2.3 order.
1285 */
1286 switch (hdr.version_major) {
1287
1288 case 2:
1289 if (hdr.version_minor < 3)
1290 p->sf.lengths_swapped = SWAPPED;
1291 else if (hdr.version_minor == 3)
1292 p->sf.lengths_swapped = MAYBE_SWAPPED;
1293 else
1294 p->sf.lengths_swapped = NOT_SWAPPED;
1295 break;
1296
1297 case 543:
1298 p->sf.lengths_swapped = SWAPPED;
1299 break;
1300
1301 default:
1302 p->sf.lengths_swapped = NOT_SWAPPED;
1303 break;
1304 }
1305
1306 #if !defined(WIN32) && !defined(MSDOS)
1307 /*
1308 * You can do "select()" and "poll()" on plain files on most
1309 * platforms, and should be able to do so on pipes.
1310 *
1311 * You can't do "select()" on anything other than sockets in
1312 * Windows, so, on Win32 systems, we don't have "selectable_fd".
1313 */
1314 p->selectable_fd = fileno(fp);
1315 #endif
1316
1317 p->read_op = pcap_offline_read;
1318 p->inject_op = sf_inject;
1319 p->setfilter_op = install_bpf_program;
1320 p->setdirection_op = sf_setdirection;
1321 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
1322 p->getnonblock_op = sf_getnonblock;
1323 p->setnonblock_op = sf_setnonblock;
1324 p->stats_op = sf_stats;
1325 #ifdef WIN32
1326 p->setbuff_op = sf_setbuff;
1327 p->setmode_op = sf_setmode;
1328 p->setmintocopy_op = sf_setmintocopy;
1329 #endif
1330 p->cleanup_op = sf_cleanup;
1331 p->activated = 1;
1332
1333 return (p);
1334 bad:
1335 free(p);
1336 return (NULL);
1337 }
1338
1339 /*
1340 * Read sf_readfile and return the next packet. Return the header in hdr
1341 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
1342 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
1343 */
1344 static int
1345 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, u_int buflen)
1346 {
1347 struct pcap_sf_patched_pkthdr sf_hdr;
1348 FILE *fp = p->sf.rfile;
1349 size_t amt_read;
1350 bpf_u_int32 t;
1351
1352 /*
1353 * Read the packet header; the structure we use as a buffer
1354 * is the longer structure for files generated by the patched
1355 * libpcap, but if the file has the magic number for an
1356 * unpatched libpcap we only read as many bytes as the regular
1357 * header has.
1358 */
1359 amt_read = fread(&sf_hdr, 1, p->sf.hdrsize, fp);
1360 if (amt_read != p->sf.hdrsize) {
1361 if (ferror(fp)) {
1362 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1363 "error reading dump file: %s",
1364 pcap_strerror(errno));
1365 return (-1);
1366 } else {
1367 if (amt_read != 0) {
1368 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1369 "truncated dump file; tried to read %lu header bytes, only got %lu",
1370 (unsigned long)p->sf.hdrsize,
1371 (unsigned long)amt_read);
1372 return (-1);
1373 }
1374 /* EOF */
1375 return (1);
1376 }
1377 }
1378
1379 if (p->sf.swapped) {
1380 /* these were written in opposite byte order */
1381 hdr->caplen = SWAPLONG(sf_hdr.caplen);
1382 hdr->len = SWAPLONG(sf_hdr.len);
1383 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
1384 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
1385 } else {
1386 hdr->caplen = sf_hdr.caplen;
1387 hdr->len = sf_hdr.len;
1388 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
1389 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
1390 }
1391 /* Swap the caplen and len fields, if necessary. */
1392 switch (p->sf.lengths_swapped) {
1393
1394 case NOT_SWAPPED:
1395 break;
1396
1397 case MAYBE_SWAPPED:
1398 if (hdr->caplen <= hdr->len) {
1399 /*
1400 * The captured length is <= the actual length,
1401 * so presumably they weren't swapped.
1402 */
1403 break;
1404 }
1405 /* FALLTHROUGH */
1406
1407 case SWAPPED:
1408 t = hdr->caplen;
1409 hdr->caplen = hdr->len;
1410 hdr->len = t;
1411 break;
1412 }
1413
1414 if (hdr->caplen > buflen) {
1415 /*
1416 * This can happen due to Solaris 2.3 systems tripping
1417 * over the BUFMOD problem and not setting the snapshot
1418 * correctly in the savefile header. If the caplen isn't
1419 * grossly wrong, try to salvage.
1420 */
1421 static u_char *tp = NULL;
1422 static size_t tsize = 0;
1423
1424 if (hdr->caplen > 65535) {
1425 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1426 "bogus savefile header");
1427 return (-1);
1428 }
1429
1430 if (tsize < hdr->caplen) {
1431 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
1432 if (tp != NULL)
1433 free((u_char *)tp);
1434 tp = (u_char *)malloc(tsize);
1435 if (tp == NULL) {
1436 tsize = 0;
1437 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1438 "BUFMOD hack malloc");
1439 return (-1);
1440 }
1441 }
1442 amt_read = fread((char *)tp, 1, hdr->caplen, fp);
1443 if (amt_read != hdr->caplen) {
1444 if (ferror(fp)) {
1445 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1446 "error reading dump file: %s",
1447 pcap_strerror(errno));
1448 } else {
1449 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1450 "truncated dump file; tried to read %u captured bytes, only got %lu",
1451 hdr->caplen, (unsigned long)amt_read);
1452 }
1453 return (-1);
1454 }
1455 /*
1456 * We can only keep up to buflen bytes. Since caplen > buflen
1457 * is exactly how we got here, we know we can only keep the
1458 * first buflen bytes and must drop the remainder. Adjust
1459 * caplen accordingly, so we don't get confused later as
1460 * to how many bytes we have to play with.
1461 */
1462 hdr->caplen = buflen;
1463 memcpy((char *)buf, (char *)tp, buflen);
1464
1465 } else {
1466 /* read the packet itself */
1467 amt_read = fread((char *)buf, 1, hdr->caplen, fp);
1468 if (amt_read != hdr->caplen) {
1469 if (ferror(fp)) {
1470 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1471 "error reading dump file: %s",
1472 pcap_strerror(errno));
1473 } else {
1474 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1475 "truncated dump file; tried to read %u captured bytes, only got %lu",
1476 hdr->caplen, (unsigned long)amt_read);
1477 }
1478 return (-1);
1479 }
1480 }
1481
1482 /*
1483 * The DLT_USB_LINUX header is in host byte order when capturing
1484 * (it's supplied directly from a memory-mapped buffer shared
1485 * by the kernel).
1486 *
1487 * When reading a DLT_USB_LINUX capture file, we need to convert
1488 * it from the capturing host's byte order to the reading host's
1489 * byte order.
1490 */
1491 if (p->sf.swapped && p->linktype == DLT_USB_LINUX) {
1492 pcap_usb_header* uhdr = (pcap_usb_header*) buf;
1493 /*
1494 * The URB id is a totally opaque value; do we really need to
1495 * converte it to the reading host's byte order???
1496 */
1497 if (hdr->caplen < 8)
1498 return 0;
1499 uhdr->id = SWAPLL(uhdr->id);
1500 if (hdr->caplen < 14)
1501 return 0;
1502 uhdr->bus_id = SWAPSHORT(uhdr->bus_id);
1503 if (hdr->caplen < 24)
1504 return 0;
1505 uhdr->ts_sec = SWAPLL(uhdr->ts_sec);
1506 if (hdr->caplen < 28)
1507 return 0;
1508 uhdr->ts_usec = SWAPLONG(uhdr->ts_usec);
1509 if (hdr->caplen < 32)
1510 return 0;
1511 uhdr->status = SWAPLONG(uhdr->status);
1512 if (hdr->caplen < 36)
1513 return 0;
1514 uhdr->urb_len = SWAPLONG(uhdr->urb_len);
1515 if (hdr->caplen < 40)
1516 return 0;
1517 uhdr->data_len = SWAPLONG(uhdr->data_len);
1518 }
1519 return (0);
1520 }
1521
1522 /*
1523 * Print out packets stored in the file initialized by sf_read_init().
1524 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
1525 */
1526 int
1527 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
1528 {
1529 struct bpf_insn *fcode;
1530 int status = 0;
1531 int n = 0;
1532
1533 while (status == 0) {
1534 struct pcap_pkthdr h;
1535
1536 /*
1537 * Has "pcap_breakloop()" been called?
1538 * If so, return immediately - if we haven't read any
1539 * packets, clear the flag and return -2 to indicate
1540 * that we were told to break out of the loop, otherwise
1541 * leave the flag set, so that the *next* call will break
1542 * out of the loop without having read any packets, and
1543 * return the number of packets we've processed so far.
1544 */
1545 if (p->break_loop) {
1546 if (n == 0) {
1547 p->break_loop = 0;
1548 return (-2);
1549 } else
1550 return (n);
1551 }
1552
1553 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
1554 if (status) {
1555 if (status == 1)
1556 return (0);
1557 return (status);
1558 }
1559
1560 if ((fcode = p->fcode.bf_insns) == NULL ||
1561 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
1562 (*callback)(user, &h, p->buffer);
1563 if (++n >= cnt && cnt > 0)
1564 break;
1565 }
1566 }
1567 /*XXX this breaks semantics tcpslice expects */
1568 return (n);
1569 }
1570
1571 /*
1572 * Output a packet to the initialized dump file.
1573 */
1574 void
1575 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1576 {
1577 register FILE *f;
1578 struct pcap_sf_pkthdr sf_hdr;
1579
1580 f = (FILE *)user;
1581 sf_hdr.ts.tv_sec = h->ts.tv_sec;
1582 sf_hdr.ts.tv_usec = h->ts.tv_usec;
1583 sf_hdr.caplen = h->caplen;
1584 sf_hdr.len = h->len;
1585 /* XXX we should check the return status */
1586 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
1587 (void)fwrite(sp, h->caplen, 1, f);
1588 }
1589
1590 static pcap_dumper_t *
1591 pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname)
1592 {
1593
1594 #if defined(WIN32) || defined(MSDOS)
1595 /*
1596 * If we're writing to the standard output, put it in binary
1597 * mode, as savefiles are binary files.
1598 *
1599 * Otherwise, we turn off buffering.
1600 * XXX - why? And why not on the standard output?
1601 */
1602 if (f == stdout)
1603 SET_BINMODE(f);
1604 else
1605 setbuf(f, NULL);
1606 #endif
1607 if (sf_write_header(f, linktype, p->tzoff, p->snapshot) == -1) {
1608 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s",
1609 fname, pcap_strerror(errno));
1610 if (f != stdout)
1611 (void)fclose(f);
1612 return (NULL);
1613 }
1614 return ((pcap_dumper_t *)f);
1615 }
1616
1617 /*
1618 * Initialize so that sf_write() will output to the file named 'fname'.
1619 */
1620 pcap_dumper_t *
1621 pcap_dump_open(pcap_t *p, const char *fname)
1622 {
1623 FILE *f;
1624 int linktype;
1625
1626 linktype = dlt_to_linktype(p->linktype);
1627 if (linktype == -1) {
1628 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1629 "%s: link-layer type %d isn't supported in savefiles",
1630 fname, linktype);
1631 return (NULL);
1632 }
1633 linktype |= p->linktype_ext;
1634
1635 if (fname[0] == '-' && fname[1] == '\0') {
1636 f = stdout;
1637 fname = "standard output";
1638 } else {
1639 #if !defined(WIN32) && !defined(MSDOS)
1640 f = fopen(fname, "w");
1641 #else
1642 f = fopen(fname, "wb");
1643 #endif
1644 if (f == NULL) {
1645 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
1646 fname, pcap_strerror(errno));
1647 return (NULL);
1648 }
1649 }
1650 return (pcap_setup_dump(p, linktype, f, fname));
1651 }
1652
1653 /*
1654 * Initialize so that sf_write() will output to the given stream.
1655 */
1656 pcap_dumper_t *
1657 pcap_dump_fopen(pcap_t *p, FILE *f)
1658 {
1659 int linktype;
1660
1661 linktype = dlt_to_linktype(p->linktype);
1662 if (linktype == -1) {
1663 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1664 "stream: link-layer type %d isn't supported in savefiles",
1665 linktype);
1666 return (NULL);
1667 }
1668 linktype |= p->linktype_ext;
1669
1670 return (pcap_setup_dump(p, linktype, f, "stream"));
1671 }
1672
1673 FILE *
1674 pcap_dump_file(pcap_dumper_t *p)
1675 {
1676 return ((FILE *)p);
1677 }
1678
1679 long
1680 pcap_dump_ftell(pcap_dumper_t *p)
1681 {
1682 return (ftell((FILE *)p));
1683 }
1684
1685 int
1686 pcap_dump_flush(pcap_dumper_t *p)
1687 {
1688
1689 if (fflush((FILE *)p) == EOF)
1690 return (-1);
1691 else
1692 return (0);
1693 }
1694
1695 void
1696 pcap_dump_close(pcap_dumper_t *p)
1697 {
1698
1699 #ifdef notyet
1700 if (ferror((FILE *)p))
1701 return-an-error;
1702 /* XXX should check return from fclose() too */
1703 #endif
1704 (void)fclose((FILE *)p);
1705 }