]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
From Chris Lightfoot <[email protected]>: add
[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.112 2004-11-07 21:40:48 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
48 #ifdef HAVE_OS_PROTO_H
49 #include "os-proto.h"
50 #endif
51
52 /*
53 * Standard libpcap format.
54 */
55 #define TCPDUMP_MAGIC 0xa1b2c3d4
56
57 /*
58 * Alexey Kuznetzov's modified libpcap format.
59 */
60 #define KUZNETZOV_TCPDUMP_MAGIC 0xa1b2cd34
61
62 /*
63 * Reserved for Francisco Mesquita <francisco.mesquita@radiomovel.pt>
64 * for another modified format.
65 */
66 #define FMESQUITA_TCPDUMP_MAGIC 0xa1b234cd
67
68 /*
69 * We use the "receiver-makes-right" approach to byte order,
70 * because time is at a premium when we are writing the file.
71 * In other words, the pcap_file_header and pcap_pkthdr,
72 * records are written in host byte order.
73 * Note that the bytes of packet data are written out in the order in
74 * which they were received, so multi-byte fields in packets are not
75 * written in host byte order, they're written in whatever order the
76 * sending machine put them in.
77 *
78 * ntoh[ls] aren't sufficient because we might need to swap on a big-endian
79 * machine (if the file was written in little-end order).
80 */
81 #define SWAPLONG(y) \
82 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
83 #define SWAPSHORT(y) \
84 ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
85
86 #define SFERR_TRUNC 1
87 #define SFERR_BADVERSION 2
88 #define SFERR_BADF 3
89 #define SFERR_EOF 4 /* not really an error, just a status */
90
91 /*
92 * We don't write DLT_* values to the capture file header, because
93 * they're not the same on all platforms.
94 *
95 * Unfortunately, the various flavors of BSD have not always used the same
96 * numerical values for the same data types, and various patches to
97 * libpcap for non-BSD OSes have added their own DLT_* codes for link
98 * layer encapsulation types seen on those OSes, and those codes have had,
99 * in some cases, values that were also used, on other platforms, for other
100 * link layer encapsulation types.
101 *
102 * This means that capture files of a type whose numerical DLT_* code
103 * means different things on different BSDs, or with different versions
104 * of libpcap, can't always be read on systems other than those like
105 * the one running on the machine on which the capture was made.
106 *
107 * Instead, we define here a set of LINKTYPE_* codes, and map DLT_* codes
108 * to LINKTYPE_* codes when writing a savefile header, and map LINKTYPE_*
109 * codes to DLT_* codes when reading a savefile header.
110 *
111 * For those DLT_* codes that have, as far as we know, the same values on
112 * all platforms (DLT_NULL through DLT_FDDI), we define LINKTYPE_xxx as
113 * DLT_xxx; that way, captures of those types can still be read by
114 * versions of libpcap that map LINKTYPE_* values to DLT_* values, and
115 * captures of those types written by versions of libpcap that map DLT_
116 * values to LINKTYPE_ values can still be read by older versions
117 * of libpcap.
118 *
119 * The other LINKTYPE_* codes are given values starting at 100, in the
120 * hopes that no DLT_* code will be given one of those values.
121 *
122 * In order to ensure that a given LINKTYPE_* code's value will refer to
123 * the same encapsulation type on all platforms, you should not allocate
124 * a new LINKTYPE_* value without consulting "tcpdump-workers@tcpdump.org".
125 * The tcpdump developers will allocate a value for you, and will not
126 * subsequently allocate it to anybody else; that value will be added to
127 * the "pcap.h" in the tcpdump.org CVS repository, so that a future
128 * libpcap release will include it.
129 *
130 * You should, if possible, also contribute patches to libpcap and tcpdump
131 * to handle the new encapsulation type, so that they can also be checked
132 * into the tcpdump.org CVS repository and so that they will appear in
133 * future libpcap and tcpdump releases.
134 *
135 * Do *NOT* assume that any values after the largest value in this file
136 * are available; you might not have the most up-to-date version of this
137 * file, and new values after that one might have been assigned. Also,
138 * do *NOT* use any values below 100 - those might already have been
139 * taken by one (or more!) organizations.
140 */
141 #define LINKTYPE_NULL DLT_NULL
142 #define LINKTYPE_ETHERNET DLT_EN10MB /* also for 100Mb and up */
143 #define LINKTYPE_EXP_ETHERNET DLT_EN3MB /* 3Mb experimental Ethernet */
144 #define LINKTYPE_AX25 DLT_AX25
145 #define LINKTYPE_PRONET DLT_PRONET
146 #define LINKTYPE_CHAOS DLT_CHAOS
147 #define LINKTYPE_TOKEN_RING DLT_IEEE802 /* DLT_IEEE802 is used for Token Ring */
148 #define LINKTYPE_ARCNET DLT_ARCNET /* BSD-style headers */
149 #define LINKTYPE_SLIP DLT_SLIP
150 #define LINKTYPE_PPP DLT_PPP
151 #define LINKTYPE_FDDI DLT_FDDI
152
153 /*
154 * LINKTYPE_PPP is for use when there might, or might not, be an RFC 1662
155 * PPP in HDLC-like framing header (with 0xff 0x03 before the PPP protocol
156 * field) at the beginning of the packet.
157 *
158 * This is for use when there is always such a header; the address field
159 * might be 0xff, for regular PPP, or it might be an address field for Cisco
160 * point-to-point with HDLC framing as per section 4.3.1 of RFC 1547 ("Cisco
161 * HDLC"). This is, for example, what you get with NetBSD's DLT_PPP_SERIAL.
162 *
163 * We give it the same value as NetBSD's DLT_PPP_SERIAL, in the hopes that
164 * nobody else will choose a DLT_ value of 50, and so that DLT_PPP_SERIAL
165 * captures will be written out with a link type that NetBSD's tcpdump
166 * can read.
167 */
168 #define LINKTYPE_PPP_HDLC 50 /* PPP in HDLC-like framing */
169
170 #define LINKTYPE_PPP_ETHER 51 /* NetBSD PPP-over-Ethernet */
171
172 #define LINKTYPE_SYMANTEC_FIREWALL 99 /* Symantec Enterprise Firewall */
173
174 #define LINKTYPE_ATM_RFC1483 100 /* LLC/SNAP-encapsulated ATM */
175 #define LINKTYPE_RAW 101 /* raw IP */
176 #define LINKTYPE_SLIP_BSDOS 102 /* BSD/OS SLIP BPF header */
177 #define LINKTYPE_PPP_BSDOS 103 /* BSD/OS PPP BPF header */
178 #define LINKTYPE_C_HDLC 104 /* Cisco HDLC */
179 #define LINKTYPE_IEEE802_11 105 /* IEEE 802.11 (wireless) */
180 #define LINKTYPE_ATM_CLIP 106 /* Linux Classical IP over ATM */
181 #define LINKTYPE_FRELAY 107 /* Frame Relay */
182 #define LINKTYPE_LOOP 108 /* OpenBSD loopback */
183 #define LINKTYPE_ENC 109 /* OpenBSD IPSEC enc */
184
185 /*
186 * These three types are reserved for future use.
187 */
188 #define LINKTYPE_LANE8023 110 /* ATM LANE + 802.3 */
189 #define LINKTYPE_HIPPI 111 /* NetBSD HIPPI */
190 #define LINKTYPE_HDLC 112 /* NetBSD HDLC framing */
191
192 #define LINKTYPE_LINUX_SLL 113 /* Linux cooked socket capture */
193 #define LINKTYPE_LTALK 114 /* Apple LocalTalk hardware */
194 #define LINKTYPE_ECONET 115 /* Acorn Econet */
195
196 /*
197 * Reserved for use with OpenBSD ipfilter.
198 */
199 #define LINKTYPE_IPFILTER 116
200
201 #define LINKTYPE_PFLOG 117 /* OpenBSD DLT_PFLOG */
202 #define LINKTYPE_CISCO_IOS 118 /* For Cisco-internal use */
203 #define LINKTYPE_PRISM_HEADER 119 /* 802.11+Prism II monitor mode */
204 #define LINKTYPE_AIRONET_HEADER 120 /* FreeBSD Aironet driver stuff */
205
206 /*
207 * Reserved for Siemens HiPath HDLC.
208 */
209 #define LINKTYPE_HHDLC 121
210
211 #define LINKTYPE_IP_OVER_FC 122 /* RFC 2625 IP-over-Fibre Channel */
212 #define LINKTYPE_SUNATM 123 /* Solaris+SunATM */
213
214 /*
215 * Reserved as per request from Kent Dahlgren <kent@praesum.com>
216 * for private use.
217 */
218 #define LINKTYPE_RIO 124 /* RapidIO */
219 #define LINKTYPE_PCI_EXP 125 /* PCI Express */
220 #define LINKTYPE_AURORA 126 /* Xilinx Aurora link layer */
221
222 #define LINKTYPE_IEEE802_11_RADIO 127 /* 802.11 plus BSD radio header */
223
224 /*
225 * Reserved for the TZSP encapsulation, as per request from
226 * Chris Waters <chris.waters@networkchemistry.com>
227 * TZSP is a generic encapsulation for any other link type,
228 * which includes a means to include meta-information
229 * with the packet, e.g. signal strength and channel
230 * for 802.11 packets.
231 */
232 #define LINKTYPE_TZSP 128 /* Tazmen Sniffer Protocol */
233
234 #define LINKTYPE_ARCNET_LINUX 129 /* Linux-style headers */
235
236 /*
237 * Juniper-private data link types, as per request from
238 * Hannes Gredler <hannes@juniper.net>. The corresponding
239 * DLT_s are used for passing on chassis-internal
240 * metainformation such as QOS profiles, etc..
241 */
242 #define LINKTYPE_JUNIPER_MLPPP 130
243 #define LINKTYPE_JUNIPER_MLFR 131
244 #define LINKTYPE_JUNIPER_ES 132
245 #define LINKTYPE_JUNIPER_GGSN 133
246 #define LINKTYPE_JUNIPER_MFR 134
247 #define LINKTYPE_JUNIPER_ATM2 135
248 #define LINKTYPE_JUNIPER_SERVICES 136
249 #define LINKTYPE_JUNIPER_ATM1 137
250
251 #define LINKTYPE_APPLE_IP_OVER_IEEE1394 138 /* Apple IP-over-IEEE 1394 cooked header */
252
253 #define LINKTYPE_RAWSS7 139 /* see rawss7.h for */
254 #define LINKTYPE_RAWSS7_MTP2 140 /* information on these */
255 #define LINKTYPE_RAWSS7_MTP3 141 /* definitions */
256 #define LINKTYPE_RAWSS7_SCCP 142
257
258 #define LINKTYPE_DOCSIS 143 /* DOCSIS MAC frames */
259
260 #define LINKTYPE_LINUX_IRDA 144 /* Linux-IrDA */
261
262 /*
263 * Reserved for IBM SP switch and IBM Next Federation switch.
264 */
265 #define LINKTYPE_IBM_SP 145
266 #define LINKTYPE_IBM_SN 146
267
268 /*
269 * Reserved for private use. If you have some link-layer header type
270 * that you want to use within your organization, with the capture files
271 * using that link-layer header type not ever be sent outside your
272 * organization, you can use these values.
273 *
274 * No libpcap release will use these for any purpose, nor will any
275 * tcpdump release use them, either.
276 *
277 * Do *NOT* use these in capture files that you expect anybody not using
278 * your private versions of capture-file-reading tools to read; in
279 * particular, do *NOT* use them in products, otherwise you may find that
280 * people won't be able to use tcpdump, or snort, or Ethereal, or... to
281 * read capture files from your firewall/intrusion detection/traffic
282 * monitoring/etc. appliance, or whatever product uses that LINKTYPE_ value,
283 * and you may also find that the developers of those applications will
284 * not accept patches to let them read those files.
285 *
286 * Also, do not use them if somebody might send you a capture using them
287 * for *their* private type and tools using them for *your* private type
288 * would have to read them.
289 *
290 * Instead, in those cases, ask "tcpdump-workers@tcpdump.org" for a new DLT_
291 * and LINKTYPE_ value, as per the comment in pcap-bpf.h, and use the type
292 * you're given.
293 */
294 #define LINKTYPE_USER0 147
295 #define LINKTYPE_USER1 148
296 #define LINKTYPE_USER2 149
297 #define LINKTYPE_USER3 150
298 #define LINKTYPE_USER4 151
299 #define LINKTYPE_USER5 152
300 #define LINKTYPE_USER6 153
301 #define LINKTYPE_USER7 154
302 #define LINKTYPE_USER8 155
303 #define LINKTYPE_USER9 156
304 #define LINKTYPE_USER10 157
305 #define LINKTYPE_USER11 158
306 #define LINKTYPE_USER12 159
307 #define LINKTYPE_USER13 160
308 #define LINKTYPE_USER14 161
309 #define LINKTYPE_USER15 162
310
311 /*
312 * For future use with 802.11 captures - defined by AbsoluteValue
313 * Systems to store a number of bits of link-layer information
314 * including radio information:
315 *
316 * http://www.shaftnet.org/~pizza/software/capturefrm.txt
317 *
318 * but could and arguably should also be used by non-AVS Linux
319 * 802.11 drivers; that may happen in the future.
320 */
321 #define LINKTYPE_IEEE802_11_RADIO_AVS 163 /* 802.11 plus AVS radio header */
322
323 /*
324 * Juniper-private data link type, as per request from
325 * Hannes Gredler <hannes@juniper.net>. The corresponding
326 * DLT_s are used for passing on chassis-internal
327 * metainformation such as QOS profiles, etc..
328 */
329 #define LINKTYPE_JUNIPER_MONITOR 164
330
331 /*
332 * Reserved for BACnet MS/TP.
333 */
334 #define LINKTYPE_BACNET_MS_TP 165
335
336 /*
337 * another PPP variant as per request from Karsten Keil <kkeil@suse.de>
338 * the first byte (0xff) of the PPP header (0xff03) is tweaked to accomodate
339 * the direction 0x00 = IN, 0x01 = OUT
340 */
341 #define LINKTYPE_PPP_WITHDIRECTION 166
342
343 /*
344 * Juniper-private data link type, as per request from
345 * Hannes Gredler <hannes@juniper.net>. The DLT_s are used
346 * for passing on chassis-internal metainformation such as
347 * QOS profiles, cookies, etc..
348 */
349 #define LINKTYPE_JUNIPER_PPPOE 167
350 #define LINKTYPE_JUNIPER_PPPOE_ATM 168
351
352
353 static struct linktype_map {
354 int dlt;
355 int linktype;
356 } map[] = {
357 /*
358 * These DLT_* codes have LINKTYPE_* codes with values identical
359 * to the values of the corresponding DLT_* code.
360 */
361 { DLT_NULL, LINKTYPE_NULL },
362 { DLT_EN10MB, LINKTYPE_ETHERNET },
363 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET },
364 { DLT_AX25, LINKTYPE_AX25 },
365 { DLT_PRONET, LINKTYPE_PRONET },
366 { DLT_CHAOS, LINKTYPE_CHAOS },
367 { DLT_IEEE802, LINKTYPE_TOKEN_RING },
368 { DLT_ARCNET, LINKTYPE_ARCNET },
369 { DLT_SLIP, LINKTYPE_SLIP },
370 { DLT_PPP, LINKTYPE_PPP },
371 { DLT_FDDI, LINKTYPE_FDDI },
372
373 /*
374 * These DLT_* codes have different values on different
375 * platforms; we map them to LINKTYPE_* codes that
376 * have values that should never be equal to any DLT_*
377 * code.
378 */
379 #ifdef DLT_FR
380 /* BSD/OS Frame Relay */
381 { DLT_FR, LINKTYPE_FRELAY },
382 #endif
383
384 { DLT_SYMANTEC_FIREWALL, LINKTYPE_SYMANTEC_FIREWALL },
385 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 },
386 { DLT_RAW, LINKTYPE_RAW },
387 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS },
388 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS },
389
390 /* BSD/OS Cisco HDLC */
391 { DLT_C_HDLC, LINKTYPE_C_HDLC },
392
393 /*
394 * These DLT_* codes are not on all platforms, but, so far,
395 * there don't appear to be any platforms that define
396 * other codes with those values; we map them to
397 * different LINKTYPE_* values anyway, just in case.
398 */
399
400 /* Linux ATM Classical IP */
401 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP },
402
403 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
404 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC },
405
406 /* NetBSD PPP over Ethernet */
407 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER },
408
409 /* IEEE 802.11 wireless */
410 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 },
411
412 /* Frame Relay */
413 { DLT_FRELAY, LINKTYPE_FRELAY },
414
415 /* OpenBSD loopback */
416 { DLT_LOOP, LINKTYPE_LOOP },
417
418 /* Linux cooked socket capture */
419 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL },
420
421 /* Apple LocalTalk hardware */
422 { DLT_LTALK, LINKTYPE_LTALK },
423
424 /* Acorn Econet */
425 { DLT_ECONET, LINKTYPE_ECONET },
426
427 /* OpenBSD DLT_PFLOG */
428 { DLT_PFLOG, LINKTYPE_PFLOG },
429
430 /* For Cisco-internal use */
431 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS },
432
433 /* Prism II monitor-mode header plus 802.11 header */
434 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER },
435
436 /* FreeBSD Aironet driver stuff */
437 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER },
438
439 /* Siemens HiPath HDLC */
440 { DLT_HHDLC, LINKTYPE_HHDLC },
441
442 /* RFC 2625 IP-over-Fibre Channel */
443 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC },
444
445 /* Solaris+SunATM */
446 { DLT_SUNATM, LINKTYPE_SUNATM },
447
448 /* RapidIO */
449 { DLT_RIO, LINKTYPE_RIO },
450
451 /* PCI Express */
452 { DLT_PCI_EXP, LINKTYPE_PCI_EXP },
453
454 /* Xilinx Aurora link layer */
455 { DLT_AURORA, LINKTYPE_AURORA },
456
457 /* 802.11 plus BSD radio header */
458 { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO },
459
460 /* Tazmen Sniffer Protocol */
461 { DLT_TZSP, LINKTYPE_TZSP },
462
463 /* Arcnet with Linux-style link-layer headers */
464 { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX },
465
466 /* Juniper-internal chassis encapsulation */
467 { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP },
468 { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR },
469 { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES },
470 { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN },
471 { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR },
472 { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 },
473 { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES },
474 { DLT_JUNIPER_ATM1, LINKTYPE_JUNIPER_ATM1 },
475
476 /* Apple IP-over-IEEE 1394 cooked header */
477 { DLT_APPLE_IP_OVER_IEEE1394, LINKTYPE_APPLE_IP_OVER_IEEE1394 },
478
479 /* DOCSIS MAC frames */
480 { DLT_DOCSIS, LINKTYPE_DOCSIS },
481
482 /* IrDA IrLAP packets + Linux-cooked header */
483 { DLT_LINUX_IRDA, LINKTYPE_LINUX_IRDA },
484
485 /* IBM SP and Next Federation switches */
486 { DLT_IBM_SP, LINKTYPE_IBM_SP },
487 { DLT_IBM_SN, LINKTYPE_IBM_SN },
488
489 /* 802.11 plus AVS radio header */
490 { DLT_IEEE802_11_RADIO_AVS, LINKTYPE_IEEE802_11_RADIO_AVS },
491
492 /*
493 * Any platform that defines additional DLT_* codes should:
494 *
495 * request a LINKTYPE_* code and value from tcpdump.org,
496 * as per the above;
497 *
498 * add, in their version of libpcap, an entry to map
499 * those DLT_* codes to the corresponding LINKTYPE_*
500 * code;
501 *
502 * redefine, in their "net/bpf.h", any DLT_* values
503 * that collide with the values used by their additional
504 * DLT_* codes, to remove those collisions (but without
505 * making them collide with any of the LINKTYPE_*
506 * values equal to 50 or above; they should also avoid
507 * defining DLT_* values that collide with those
508 * LINKTYPE_* values, either).
509 */
510
511 /* Juniper-internal chassis encapsulation */
512 { DLT_JUNIPER_MONITOR, LINKTYPE_JUNIPER_MONITOR },
513
514 /* BACnet MS/TP */
515 { DLT_BACNET_MS_TP, LINKTYPE_BACNET_MS_TP },
516
517 /* PPP with direction flag in the PPP header */
518 { DLT_PPP_WITHDIRECTION,LINKTYPE_PPP_WITHDIRECTION},
519
520 /* Juniper-internal chassis encapsulation */
521 { DLT_JUNIPER_PPPOE, LINKTYPE_JUNIPER_PPPOE },
522 { DLT_JUNIPER_PPPOE_ATM,LINKTYPE_JUNIPER_PPPOE_ATM },
523
524 { -1, -1 }
525 };
526
527 static int
528 dlt_to_linktype(int dlt)
529 {
530 int i;
531
532 for (i = 0; map[i].dlt != -1; i++) {
533 if (map[i].dlt == dlt)
534 return (map[i].linktype);
535 }
536
537 /*
538 * If we don't have a mapping for this DLT_ code, return an
539 * error; that means that the table above needs to have an
540 * entry added.
541 */
542 return (-1);
543 }
544
545 static int
546 linktype_to_dlt(int linktype)
547 {
548 int i;
549
550 for (i = 0; map[i].linktype != -1; i++) {
551 if (map[i].linktype == linktype)
552 return (map[i].dlt);
553 }
554
555 /*
556 * If we don't have an entry for this link type, return
557 * the link type value; it may be a DLT_ value from an
558 * older version of libpcap.
559 */
560 return linktype;
561 }
562
563 static int
564 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
565 {
566 struct pcap_file_header hdr;
567
568 hdr.magic = TCPDUMP_MAGIC;
569 hdr.version_major = PCAP_VERSION_MAJOR;
570 hdr.version_minor = PCAP_VERSION_MINOR;
571
572 hdr.thiszone = thiszone;
573 hdr.snaplen = snaplen;
574 hdr.sigfigs = 0;
575 hdr.linktype = linktype;
576
577 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
578 return (-1);
579
580 return (0);
581 }
582
583 static void
584 swap_hdr(struct pcap_file_header *hp)
585 {
586 hp->version_major = SWAPSHORT(hp->version_major);
587 hp->version_minor = SWAPSHORT(hp->version_minor);
588 hp->thiszone = SWAPLONG(hp->thiszone);
589 hp->sigfigs = SWAPLONG(hp->sigfigs);
590 hp->snaplen = SWAPLONG(hp->snaplen);
591 hp->linktype = SWAPLONG(hp->linktype);
592 }
593
594 static int
595 sf_getnonblock(pcap_t *p, char *errbuf)
596 {
597 /*
598 * This is a savefile, not a live capture file, so never say
599 * it's in non-blocking mode.
600 */
601 return (0);
602 }
603
604 static int
605 sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
606 {
607 /*
608 * This is a savefile, not a live capture file, so ignore
609 * requests to put it in non-blocking mode.
610 */
611 return (0);
612 }
613
614 static int
615 sf_stats(pcap_t *p, struct pcap_stat *ps)
616 {
617 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
618 "Statistics aren't available from savefiles");
619 return (-1);
620 }
621
622 static int
623 sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
624 {
625 strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
626 PCAP_ERRBUF_SIZE);
627 return (-1);
628 }
629
630 static void
631 sf_close(pcap_t *p)
632 {
633 if (p->sf.rfile != stdin)
634 (void)fclose(p->sf.rfile);
635 if (p->sf.base != NULL)
636 free(p->sf.base);
637 }
638
639 pcap_t *
640 pcap_open_offline(const char *fname, char *errbuf)
641 {
642 FILE *fp;
643 if (fname[0] == '-' && fname[1] == '\0')
644 fp = stdin;
645 else {
646 #ifndef WIN32
647 fp = fopen(fname, "r");
648 #else
649 fp = fopen(fname, "rb");
650 #endif
651 if (fp == NULL) {
652 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
653 pcap_strerror(errno));
654 return NULL;
655 }
656 }
657 return (pcap_fopen_offline(fp, errbuf));
658 }
659
660 pcap_t *
661 pcap_fopen_offline(FILE *fp, char *errbuf)
662 {
663 register pcap_t *p;
664 struct pcap_file_header hdr;
665 bpf_u_int32 magic;
666 int linklen;
667
668 p = (pcap_t *)malloc(sizeof(*p));
669 if (p == NULL) {
670 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
671 return (NULL);
672 }
673
674 memset((char *)p, 0, sizeof(*p));
675
676 if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
677 snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
678 pcap_strerror(errno));
679 goto bad;
680 }
681 magic = hdr.magic;
682 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
683 magic = SWAPLONG(magic);
684 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
685 snprintf(errbuf, PCAP_ERRBUF_SIZE,
686 "bad dump file format");
687 goto bad;
688 }
689 p->sf.swapped = 1;
690 swap_hdr(&hdr);
691 }
692 if (magic == KUZNETZOV_TCPDUMP_MAGIC) {
693 /*
694 * XXX - the patch that's in some versions of libpcap
695 * changes the packet header but not the magic number,
696 * and some other versions with this magic number have
697 * some extra debugging information in the packet header;
698 * we'd have to use some hacks^H^H^H^H^Hheuristics to
699 * detect those variants.
700 *
701 * Ethereal does that, but it does so by trying to read
702 * the first two packets of the file with each of the
703 * record header formats. That currently means it seeks
704 * backwards and retries the reads, which doesn't work
705 * on pipes. We want to be able to read from a pipe, so
706 * that strategy won't work; we'd have to buffer some
707 * data ourselves and read from that buffer in order to
708 * make that work.
709 */
710 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
711 } else
712 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
713 if (hdr.version_major < PCAP_VERSION_MAJOR) {
714 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
715 goto bad;
716 }
717 p->tzoff = hdr.thiszone;
718 p->snapshot = hdr.snaplen;
719 p->linktype = linktype_to_dlt(hdr.linktype);
720 p->sf.rfile = fp;
721 #ifndef WIN32
722 p->bufsize = hdr.snaplen;
723 #else
724 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
725 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr);
726 #endif
727
728 /* Align link header as required for proper data alignment */
729 /* XXX should handle all types */
730 switch (p->linktype) {
731
732 case DLT_EN10MB:
733 linklen = 14;
734 break;
735
736 case DLT_FDDI:
737 linklen = 13 + 8; /* fddi_header + llc */
738 break;
739
740 case DLT_NULL:
741 default:
742 linklen = 0;
743 break;
744 }
745
746 if (p->bufsize < 0)
747 p->bufsize = BPF_MAXBUFSIZE;
748 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
749 if (p->sf.base == NULL) {
750 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
751 goto bad;
752 }
753 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
754 p->sf.version_major = hdr.version_major;
755 p->sf.version_minor = hdr.version_minor;
756 #ifdef PCAP_FDDIPAD
757 /* XXX padding only needed for kernel fcode */
758 pcap_fddipad = 0;
759 #endif
760
761 /*
762 * We interchanged the caplen and len fields at version 2.3,
763 * in order to match the bpf header layout. But unfortunately
764 * some files were written with version 2.3 in their headers
765 * but without the interchanged fields.
766 *
767 * In addition, DG/UX tcpdump writes out files with a version
768 * number of 543.0, and with the caplen and len fields in the
769 * pre-2.3 order.
770 */
771 switch (hdr.version_major) {
772
773 case 2:
774 if (hdr.version_minor < 3)
775 p->sf.lengths_swapped = SWAPPED;
776 else if (hdr.version_minor == 3)
777 p->sf.lengths_swapped = MAYBE_SWAPPED;
778 else
779 p->sf.lengths_swapped = NOT_SWAPPED;
780 break;
781
782 case 543:
783 p->sf.lengths_swapped = SWAPPED;
784 break;
785
786 default:
787 p->sf.lengths_swapped = NOT_SWAPPED;
788 break;
789 }
790
791 #ifndef WIN32
792 /*
793 * You can do "select()" and "poll()" on plain files on most
794 * platforms, and should be able to do so on pipes.
795 *
796 * You can't do "select()" on anything other than sockets in
797 * Windows, so, on Win32 systems, we don't have "selectable_fd".
798 */
799 p->selectable_fd = fileno(fp);
800 #endif
801
802 p->read_op = pcap_offline_read;
803 p->inject_op = sf_inject;
804 p->setfilter_op = install_bpf_program;
805 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
806 p->getnonblock_op = sf_getnonblock;
807 p->setnonblock_op = sf_setnonblock;
808 p->stats_op = sf_stats;
809 p->close_op = sf_close;
810
811 #ifdef WIN32
812 /*
813 * If we're reading from the standard input, put it in binary
814 * mode, as savefiles are binary files.
815 */
816 if (fp == stdin)
817 _setmode(_fileno(f), _O_BINARY);
818 #endif
819
820 return (p);
821 bad:
822 if (fp != NULL && fp != stdin)
823 fclose(fp);
824 free(p);
825 return (NULL);
826 }
827
828 /*
829 * Read sf_readfile and return the next packet. Return the header in hdr
830 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
831 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
832 */
833 static int
834 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, u_int buflen)
835 {
836 struct pcap_sf_patched_pkthdr sf_hdr;
837 FILE *fp = p->sf.rfile;
838 size_t amt_read;
839 bpf_u_int32 t;
840
841 /*
842 * Read the packet header; the structure we use as a buffer
843 * is the longer structure for files generated by the patched
844 * libpcap, but if the file has the magic number for an
845 * unpatched libpcap we only read as many bytes as the regular
846 * header has.
847 */
848 amt_read = fread(&sf_hdr, 1, p->sf.hdrsize, fp);
849 if (amt_read != p->sf.hdrsize) {
850 if (ferror(fp)) {
851 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
852 "error reading dump file: %s",
853 pcap_strerror(errno));
854 return (-1);
855 } else {
856 if (amt_read != 0) {
857 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
858 "truncated dump file; tried to read %d header bytes, only got %lu",
859 p->sf.hdrsize, (unsigned long)amt_read);
860 return (-1);
861 }
862 /* EOF */
863 return (1);
864 }
865 }
866
867 if (p->sf.swapped) {
868 /* these were written in opposite byte order */
869 hdr->caplen = SWAPLONG(sf_hdr.caplen);
870 hdr->len = SWAPLONG(sf_hdr.len);
871 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
872 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
873 } else {
874 hdr->caplen = sf_hdr.caplen;
875 hdr->len = sf_hdr.len;
876 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
877 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
878 }
879 /* Swap the caplen and len fields, if necessary. */
880 switch (p->sf.lengths_swapped) {
881
882 case NOT_SWAPPED:
883 break;
884
885 case MAYBE_SWAPPED:
886 if (hdr->caplen <= hdr->len) {
887 /*
888 * The captured length is <= the actual length,
889 * so presumably they weren't swapped.
890 */
891 break;
892 }
893 /* FALLTHROUGH */
894
895 case SWAPPED:
896 t = hdr->caplen;
897 hdr->caplen = hdr->len;
898 hdr->len = t;
899 break;
900 }
901
902 if (hdr->caplen > buflen) {
903 /*
904 * This can happen due to Solaris 2.3 systems tripping
905 * over the BUFMOD problem and not setting the snapshot
906 * correctly in the savefile header. If the caplen isn't
907 * grossly wrong, try to salvage.
908 */
909 static u_char *tp = NULL;
910 static size_t tsize = 0;
911
912 if (hdr->caplen > 65535) {
913 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
914 "bogus savefile header");
915 return (-1);
916 }
917
918 if (tsize < hdr->caplen) {
919 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
920 if (tp != NULL)
921 free((u_char *)tp);
922 tp = (u_char *)malloc(tsize);
923 if (tp == NULL) {
924 tsize = 0;
925 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
926 "BUFMOD hack malloc");
927 return (-1);
928 }
929 }
930 amt_read = fread((char *)tp, 1, hdr->caplen, fp);
931 if (amt_read != hdr->caplen) {
932 if (ferror(fp)) {
933 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
934 "error reading dump file: %s",
935 pcap_strerror(errno));
936 } else {
937 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
938 "truncated dump file; tried to read %u captured bytes, only got %lu",
939 hdr->caplen, (unsigned long)amt_read);
940 }
941 return (-1);
942 }
943 /*
944 * We can only keep up to buflen bytes. Since caplen > buflen
945 * is exactly how we got here, we know we can only keep the
946 * first buflen bytes and must drop the remainder. Adjust
947 * caplen accordingly, so we don't get confused later as
948 * to how many bytes we have to play with.
949 */
950 hdr->caplen = buflen;
951 memcpy((char *)buf, (char *)tp, buflen);
952
953 } else {
954 /* read the packet itself */
955 amt_read = fread((char *)buf, 1, hdr->caplen, fp);
956 if (amt_read != hdr->caplen) {
957 if (ferror(fp)) {
958 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
959 "error reading dump file: %s",
960 pcap_strerror(errno));
961 } else {
962 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
963 "truncated dump file; tried to read %u captured bytes, only got %lu",
964 hdr->caplen, (unsigned long)amt_read);
965 }
966 return (-1);
967 }
968 }
969 return (0);
970 }
971
972 /*
973 * Print out packets stored in the file initialized by sf_read_init().
974 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
975 */
976 int
977 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
978 {
979 struct bpf_insn *fcode = p->fcode.bf_insns;
980 int status = 0;
981 int n = 0;
982
983 while (status == 0) {
984 struct pcap_pkthdr h;
985
986 /*
987 * Has "pcap_breakloop()" been called?
988 * If so, return immediately - if we haven't read any
989 * packets, clear the flag and return -2 to indicate
990 * that we were told to break out of the loop, otherwise
991 * leave the flag set, so that the *next* call will break
992 * out of the loop without having read any packets, and
993 * return the number of packets we've processed so far.
994 */
995 if (p->break_loop) {
996 if (n == 0) {
997 p->break_loop = 0;
998 return (-2);
999 } else
1000 return (n);
1001 }
1002
1003 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
1004 if (status) {
1005 if (status == 1)
1006 return (0);
1007 return (status);
1008 }
1009
1010 if (fcode == NULL ||
1011 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
1012 (*callback)(user, &h, p->buffer);
1013 if (++n >= cnt && cnt > 0)
1014 break;
1015 }
1016 }
1017 /*XXX this breaks semantics tcpslice expects */
1018 return (n);
1019 }
1020
1021 /*
1022 * Output a packet to the initialized dump file.
1023 */
1024 void
1025 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1026 {
1027 register FILE *f;
1028 struct pcap_sf_pkthdr sf_hdr;
1029
1030 f = (FILE *)user;
1031 sf_hdr.ts.tv_sec = h->ts.tv_sec;
1032 sf_hdr.ts.tv_usec = h->ts.tv_usec;
1033 sf_hdr.caplen = h->caplen;
1034 sf_hdr.len = h->len;
1035 /* XXX we should check the return status */
1036 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
1037 (void)fwrite((char *)sp, h->caplen, 1, f);
1038 }
1039
1040 static pcap_dumper_t *
1041 pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname)
1042 {
1043
1044 #ifdef WIN32
1045 /*
1046 * If we're writing to the standard output, put it in binary
1047 * mode, as savefiles are binary files.
1048 *
1049 * Otherwise, we turn off buffering.
1050 * XXX - why? And why not on the standard output?
1051 */
1052 if (f == stdout)
1053 _setmode(_fileno(f), _O_BINARY);
1054 else
1055 setbuf(f, NULL);
1056 #endif
1057 if (sf_write_header(f, linktype, p->tzoff, p->snapshot) == -1) {
1058 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s",
1059 fname, pcap_strerror(errno));
1060 if (f != stdout)
1061 (void)fclose(f);
1062 return (NULL);
1063 }
1064 return ((pcap_dumper_t *)f);
1065 }
1066
1067 /*
1068 * Initialize so that sf_write() will output to the file named 'fname'.
1069 */
1070 pcap_dumper_t *
1071 pcap_dump_open(pcap_t *p, const char *fname)
1072 {
1073 FILE *f;
1074 int linktype;
1075
1076 linktype = dlt_to_linktype(p->linktype);
1077 if (linktype == -1) {
1078 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1079 "%s: link-layer type %d isn't supported in savefiles",
1080 fname, linktype);
1081 return (NULL);
1082 }
1083
1084 if (fname[0] == '-' && fname[1] == '\0') {
1085 f = stdout;
1086 fname = "standard output";
1087 } else {
1088 #ifndef WIN32
1089 f = fopen(fname, "w");
1090 #else
1091 f = fopen(fname, "wb");
1092 #endif
1093 if (f == NULL) {
1094 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
1095 fname, pcap_strerror(errno));
1096 return (NULL);
1097 }
1098 }
1099 return (pcap_setup_dump(p, linktype, f, fname));
1100 }
1101
1102 /*
1103 * Initialize so that sf_write() will output to the given stream.
1104 */
1105 pcap_dumper_t *
1106 pcap_dump_fopen(pcap_t *p, FILE *f)
1107 {
1108 int linktype;
1109
1110 linktype = dlt_to_linktype(p->linktype);
1111 if (linktype == -1) {
1112 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1113 "stream: link-layer type %d isn't supported in savefiles",
1114 linktype);
1115 return (NULL);
1116 }
1117
1118 return (pcap_setup_dump(p, linktype, f, "stream"));
1119 }
1120
1121 FILE *
1122 pcap_dump_file(pcap_dumper_t *p)
1123 {
1124 return ((FILE *)p);
1125 }
1126
1127 int
1128 pcap_dump_flush(pcap_dumper_t *p)
1129 {
1130
1131 if (fflush((FILE *)p) == EOF)
1132 return (-1);
1133 else
1134 return (0);
1135 }
1136
1137 void
1138 pcap_dump_close(pcap_dumper_t *p)
1139 {
1140
1141 #ifdef notyet
1142 if (ferror((FILE *)p))
1143 return-an-error;
1144 /* XXX should check return from fclose() too */
1145 #endif
1146 (void)fclose((FILE *)p);
1147 }