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