]> The Tcpdump Group git mirrors - libpcap/blob - savefile.c
Put the FDDI padding (if any) into the pcap_t structure on platforms
[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.116 2004-12-15 00:25:09 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 #define LINKTYPE_GPRS_LLC 169 /* GPRS LLC */
353 #define LINKTYPE_GPF_T 170 /* GPF-T (ITU-T G.7041/Y.1303) */
354 #define LINKTYPE_GPF_F 171 /* GPF-T (ITU-T G.7041/Y.1303) */
355
356 static struct linktype_map {
357 int dlt;
358 int linktype;
359 } map[] = {
360 /*
361 * These DLT_* codes have LINKTYPE_* codes with values identical
362 * to the values of the corresponding DLT_* code.
363 */
364 { DLT_NULL, LINKTYPE_NULL },
365 { DLT_EN10MB, LINKTYPE_ETHERNET },
366 { DLT_EN3MB, LINKTYPE_EXP_ETHERNET },
367 { DLT_AX25, LINKTYPE_AX25 },
368 { DLT_PRONET, LINKTYPE_PRONET },
369 { DLT_CHAOS, LINKTYPE_CHAOS },
370 { DLT_IEEE802, LINKTYPE_TOKEN_RING },
371 { DLT_ARCNET, LINKTYPE_ARCNET },
372 { DLT_SLIP, LINKTYPE_SLIP },
373 { DLT_PPP, LINKTYPE_PPP },
374 { DLT_FDDI, LINKTYPE_FDDI },
375
376 /*
377 * These DLT_* codes have different values on different
378 * platforms; we map them to LINKTYPE_* codes that
379 * have values that should never be equal to any DLT_*
380 * code.
381 */
382 #ifdef DLT_FR
383 /* BSD/OS Frame Relay */
384 { DLT_FR, LINKTYPE_FRELAY },
385 #endif
386
387 { DLT_SYMANTEC_FIREWALL, LINKTYPE_SYMANTEC_FIREWALL },
388 { DLT_ATM_RFC1483, LINKTYPE_ATM_RFC1483 },
389 { DLT_RAW, LINKTYPE_RAW },
390 { DLT_SLIP_BSDOS, LINKTYPE_SLIP_BSDOS },
391 { DLT_PPP_BSDOS, LINKTYPE_PPP_BSDOS },
392
393 /* BSD/OS Cisco HDLC */
394 { DLT_C_HDLC, LINKTYPE_C_HDLC },
395
396 /*
397 * These DLT_* codes are not on all platforms, but, so far,
398 * there don't appear to be any platforms that define
399 * other codes with those values; we map them to
400 * different LINKTYPE_* values anyway, just in case.
401 */
402
403 /* Linux ATM Classical IP */
404 { DLT_ATM_CLIP, LINKTYPE_ATM_CLIP },
405
406 /* NetBSD sync/async serial PPP (or Cisco HDLC) */
407 { DLT_PPP_SERIAL, LINKTYPE_PPP_HDLC },
408
409 /* NetBSD PPP over Ethernet */
410 { DLT_PPP_ETHER, LINKTYPE_PPP_ETHER },
411
412 /* IEEE 802.11 wireless */
413 { DLT_IEEE802_11, LINKTYPE_IEEE802_11 },
414
415 /* Frame Relay */
416 { DLT_FRELAY, LINKTYPE_FRELAY },
417
418 /* OpenBSD loopback */
419 { DLT_LOOP, LINKTYPE_LOOP },
420
421 /* Linux cooked socket capture */
422 { DLT_LINUX_SLL, LINKTYPE_LINUX_SLL },
423
424 /* Apple LocalTalk hardware */
425 { DLT_LTALK, LINKTYPE_LTALK },
426
427 /* Acorn Econet */
428 { DLT_ECONET, LINKTYPE_ECONET },
429
430 /* OpenBSD DLT_PFLOG */
431 { DLT_PFLOG, LINKTYPE_PFLOG },
432
433 /* For Cisco-internal use */
434 { DLT_CISCO_IOS, LINKTYPE_CISCO_IOS },
435
436 /* Prism II monitor-mode header plus 802.11 header */
437 { DLT_PRISM_HEADER, LINKTYPE_PRISM_HEADER },
438
439 /* FreeBSD Aironet driver stuff */
440 { DLT_AIRONET_HEADER, LINKTYPE_AIRONET_HEADER },
441
442 /* Siemens HiPath HDLC */
443 { DLT_HHDLC, LINKTYPE_HHDLC },
444
445 /* RFC 2625 IP-over-Fibre Channel */
446 { DLT_IP_OVER_FC, LINKTYPE_IP_OVER_FC },
447
448 /* Solaris+SunATM */
449 { DLT_SUNATM, LINKTYPE_SUNATM },
450
451 /* RapidIO */
452 { DLT_RIO, LINKTYPE_RIO },
453
454 /* PCI Express */
455 { DLT_PCI_EXP, LINKTYPE_PCI_EXP },
456
457 /* Xilinx Aurora link layer */
458 { DLT_AURORA, LINKTYPE_AURORA },
459
460 /* 802.11 plus BSD radio header */
461 { DLT_IEEE802_11_RADIO, LINKTYPE_IEEE802_11_RADIO },
462
463 /* Tazmen Sniffer Protocol */
464 { DLT_TZSP, LINKTYPE_TZSP },
465
466 /* Arcnet with Linux-style link-layer headers */
467 { DLT_ARCNET_LINUX, LINKTYPE_ARCNET_LINUX },
468
469 /* Juniper-internal chassis encapsulation */
470 { DLT_JUNIPER_MLPPP, LINKTYPE_JUNIPER_MLPPP },
471 { DLT_JUNIPER_MLFR, LINKTYPE_JUNIPER_MLFR },
472 { DLT_JUNIPER_ES, LINKTYPE_JUNIPER_ES },
473 { DLT_JUNIPER_GGSN, LINKTYPE_JUNIPER_GGSN },
474 { DLT_JUNIPER_MFR, LINKTYPE_JUNIPER_MFR },
475 { DLT_JUNIPER_ATM2, LINKTYPE_JUNIPER_ATM2 },
476 { DLT_JUNIPER_SERVICES, LINKTYPE_JUNIPER_SERVICES },
477 { DLT_JUNIPER_ATM1, LINKTYPE_JUNIPER_ATM1 },
478
479 /* Apple IP-over-IEEE 1394 cooked header */
480 { DLT_APPLE_IP_OVER_IEEE1394, LINKTYPE_APPLE_IP_OVER_IEEE1394 },
481
482 /* DOCSIS MAC frames */
483 { DLT_DOCSIS, LINKTYPE_DOCSIS },
484
485 /* IrDA IrLAP packets + Linux-cooked header */
486 { DLT_LINUX_IRDA, LINKTYPE_LINUX_IRDA },
487
488 /* IBM SP and Next Federation switches */
489 { DLT_IBM_SP, LINKTYPE_IBM_SP },
490 { DLT_IBM_SN, LINKTYPE_IBM_SN },
491
492 /* 802.11 plus AVS radio header */
493 { DLT_IEEE802_11_RADIO_AVS, LINKTYPE_IEEE802_11_RADIO_AVS },
494
495 /*
496 * Any platform that defines additional DLT_* codes should:
497 *
498 * request a LINKTYPE_* code and value from tcpdump.org,
499 * as per the above;
500 *
501 * add, in their version of libpcap, an entry to map
502 * those DLT_* codes to the corresponding LINKTYPE_*
503 * code;
504 *
505 * redefine, in their "net/bpf.h", any DLT_* values
506 * that collide with the values used by their additional
507 * DLT_* codes, to remove those collisions (but without
508 * making them collide with any of the LINKTYPE_*
509 * values equal to 50 or above; they should also avoid
510 * defining DLT_* values that collide with those
511 * LINKTYPE_* values, either).
512 */
513
514 /* Juniper-internal chassis encapsulation */
515 { DLT_JUNIPER_MONITOR, LINKTYPE_JUNIPER_MONITOR },
516
517 /* BACnet MS/TP */
518 { DLT_BACNET_MS_TP, LINKTYPE_BACNET_MS_TP },
519
520 /* PPP with direction flag in the PPP header */
521 { DLT_PPP_WITHDIRECTION,LINKTYPE_PPP_WITHDIRECTION},
522
523 /* Juniper-internal chassis encapsulation */
524 { DLT_JUNIPER_PPPOE, LINKTYPE_JUNIPER_PPPOE },
525 { DLT_JUNIPER_PPPOE_ATM,LINKTYPE_JUNIPER_PPPOE_ATM },
526
527 /* GPRS LLC */
528 { DLT_GPRS_LLC, LINKTYPE_GPRS_LLC },
529
530 /* Transparent Generic Framing Procedure (ITU-T G.7041/Y.1303) */
531 { DLT_GPF_T, LINKTYPE_GPF_T },
532
533 /* Framed Generic Framing Procedure (ITU-T G.7041/Y.1303) */
534 { DLT_GPF_F, LINKTYPE_GPF_F },
535
536 { -1, -1 }
537 };
538
539 static int
540 dlt_to_linktype(int dlt)
541 {
542 int i;
543
544 for (i = 0; map[i].dlt != -1; i++) {
545 if (map[i].dlt == dlt)
546 return (map[i].linktype);
547 }
548
549 /*
550 * If we don't have a mapping for this DLT_ code, return an
551 * error; that means that the table above needs to have an
552 * entry added.
553 */
554 return (-1);
555 }
556
557 static int
558 linktype_to_dlt(int linktype)
559 {
560 int i;
561
562 for (i = 0; map[i].linktype != -1; i++) {
563 if (map[i].linktype == linktype)
564 return (map[i].dlt);
565 }
566
567 /*
568 * If we don't have an entry for this link type, return
569 * the link type value; it may be a DLT_ value from an
570 * older version of libpcap.
571 */
572 return linktype;
573 }
574
575 static int
576 sf_write_header(FILE *fp, int linktype, int thiszone, int snaplen)
577 {
578 struct pcap_file_header hdr;
579
580 hdr.magic = TCPDUMP_MAGIC;
581 hdr.version_major = PCAP_VERSION_MAJOR;
582 hdr.version_minor = PCAP_VERSION_MINOR;
583
584 hdr.thiszone = thiszone;
585 hdr.snaplen = snaplen;
586 hdr.sigfigs = 0;
587 hdr.linktype = linktype;
588
589 if (fwrite((char *)&hdr, sizeof(hdr), 1, fp) != 1)
590 return (-1);
591
592 return (0);
593 }
594
595 static void
596 swap_hdr(struct pcap_file_header *hp)
597 {
598 hp->version_major = SWAPSHORT(hp->version_major);
599 hp->version_minor = SWAPSHORT(hp->version_minor);
600 hp->thiszone = SWAPLONG(hp->thiszone);
601 hp->sigfigs = SWAPLONG(hp->sigfigs);
602 hp->snaplen = SWAPLONG(hp->snaplen);
603 hp->linktype = SWAPLONG(hp->linktype);
604 }
605
606 static int
607 sf_getnonblock(pcap_t *p, char *errbuf)
608 {
609 /*
610 * This is a savefile, not a live capture file, so never say
611 * it's in non-blocking mode.
612 */
613 return (0);
614 }
615
616 static int
617 sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
618 {
619 /*
620 * This is a savefile, not a live capture file, so ignore
621 * requests to put it in non-blocking mode.
622 */
623 return (0);
624 }
625
626 static int
627 sf_stats(pcap_t *p, struct pcap_stat *ps)
628 {
629 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
630 "Statistics aren't available from savefiles");
631 return (-1);
632 }
633
634 static int
635 sf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
636 {
637 strlcpy(p->errbuf, "Sending packets isn't supported on savefiles",
638 PCAP_ERRBUF_SIZE);
639 return (-1);
640 }
641
642 static void
643 sf_close(pcap_t *p)
644 {
645 if (p->sf.rfile != stdin)
646 (void)fclose(p->sf.rfile);
647 if (p->sf.base != NULL)
648 free(p->sf.base);
649 }
650
651 pcap_t *
652 pcap_open_offline(const char *fname, char *errbuf)
653 {
654 FILE *fp;
655 if (fname[0] == '-' && fname[1] == '\0')
656 fp = stdin;
657 else {
658 #ifndef WIN32
659 fp = fopen(fname, "r");
660 #else
661 fp = fopen(fname, "rb");
662 #endif
663 if (fp == NULL) {
664 snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", fname,
665 pcap_strerror(errno));
666 return NULL;
667 }
668 }
669 return (pcap_fopen_offline(fp, errbuf));
670 }
671
672 pcap_t *
673 pcap_fopen_offline(FILE *fp, char *errbuf)
674 {
675 register pcap_t *p;
676 struct pcap_file_header hdr;
677 bpf_u_int32 magic;
678 int linklen;
679
680 p = (pcap_t *)malloc(sizeof(*p));
681 if (p == NULL) {
682 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
683 return (NULL);
684 }
685
686 memset((char *)p, 0, sizeof(*p));
687
688 if (fread((char *)&hdr, sizeof(hdr), 1, fp) != 1) {
689 snprintf(errbuf, PCAP_ERRBUF_SIZE, "fread: %s",
690 pcap_strerror(errno));
691 goto bad;
692 }
693 magic = hdr.magic;
694 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
695 magic = SWAPLONG(magic);
696 if (magic != TCPDUMP_MAGIC && magic != KUZNETZOV_TCPDUMP_MAGIC) {
697 snprintf(errbuf, PCAP_ERRBUF_SIZE,
698 "bad dump file format");
699 goto bad;
700 }
701 p->sf.swapped = 1;
702 swap_hdr(&hdr);
703 }
704 if (magic == KUZNETZOV_TCPDUMP_MAGIC) {
705 /*
706 * XXX - the patch that's in some versions of libpcap
707 * changes the packet header but not the magic number,
708 * and some other versions with this magic number have
709 * some extra debugging information in the packet header;
710 * we'd have to use some hacks^H^H^H^H^Hheuristics to
711 * detect those variants.
712 *
713 * Ethereal does that, but it does so by trying to read
714 * the first two packets of the file with each of the
715 * record header formats. That currently means it seeks
716 * backwards and retries the reads, which doesn't work
717 * on pipes. We want to be able to read from a pipe, so
718 * that strategy won't work; we'd have to buffer some
719 * data ourselves and read from that buffer in order to
720 * make that work.
721 */
722 p->sf.hdrsize = sizeof(struct pcap_sf_patched_pkthdr);
723 } else
724 p->sf.hdrsize = sizeof(struct pcap_sf_pkthdr);
725 if (hdr.version_major < PCAP_VERSION_MAJOR) {
726 snprintf(errbuf, PCAP_ERRBUF_SIZE, "archaic file format");
727 goto bad;
728 }
729 p->tzoff = hdr.thiszone;
730 p->snapshot = hdr.snaplen;
731 p->linktype = linktype_to_dlt(hdr.linktype);
732 p->sf.rfile = fp;
733 #ifndef WIN32
734 p->bufsize = hdr.snaplen;
735 #else
736 /* Allocate the space for pcap_pkthdr as well. It will be used by pcap_read_ex */
737 p->bufsize = hdr.snaplen+sizeof(struct pcap_pkthdr);
738 #endif
739
740 /* Align link header as required for proper data alignment */
741 /* XXX should handle all types */
742 switch (p->linktype) {
743
744 case DLT_EN10MB:
745 linklen = 14;
746 break;
747
748 case DLT_FDDI:
749 linklen = 13 + 8; /* fddi_header + llc */
750 break;
751
752 case DLT_NULL:
753 default:
754 linklen = 0;
755 break;
756 }
757
758 if (p->bufsize < 0)
759 p->bufsize = BPF_MAXBUFSIZE;
760 p->sf.base = (u_char *)malloc(p->bufsize + BPF_ALIGNMENT);
761 if (p->sf.base == NULL) {
762 strlcpy(errbuf, "out of swap", PCAP_ERRBUF_SIZE);
763 goto bad;
764 }
765 p->buffer = p->sf.base + BPF_ALIGNMENT - (linklen % BPF_ALIGNMENT);
766 p->sf.version_major = hdr.version_major;
767 p->sf.version_minor = hdr.version_minor;
768 #ifdef PCAP_FDDIPAD
769 /* Padding only needed for live capture fcode */
770 p->fddipad = 0;
771 #endif
772
773 /*
774 * We interchanged the caplen and len fields at version 2.3,
775 * in order to match the bpf header layout. But unfortunately
776 * some files were written with version 2.3 in their headers
777 * but without the interchanged fields.
778 *
779 * In addition, DG/UX tcpdump writes out files with a version
780 * number of 543.0, and with the caplen and len fields in the
781 * pre-2.3 order.
782 */
783 switch (hdr.version_major) {
784
785 case 2:
786 if (hdr.version_minor < 3)
787 p->sf.lengths_swapped = SWAPPED;
788 else if (hdr.version_minor == 3)
789 p->sf.lengths_swapped = MAYBE_SWAPPED;
790 else
791 p->sf.lengths_swapped = NOT_SWAPPED;
792 break;
793
794 case 543:
795 p->sf.lengths_swapped = SWAPPED;
796 break;
797
798 default:
799 p->sf.lengths_swapped = NOT_SWAPPED;
800 break;
801 }
802
803 #ifndef WIN32
804 /*
805 * You can do "select()" and "poll()" on plain files on most
806 * platforms, and should be able to do so on pipes.
807 *
808 * You can't do "select()" on anything other than sockets in
809 * Windows, so, on Win32 systems, we don't have "selectable_fd".
810 */
811 p->selectable_fd = fileno(fp);
812 #endif
813
814 p->read_op = pcap_offline_read;
815 p->inject_op = sf_inject;
816 p->setfilter_op = install_bpf_program;
817 p->set_datalink_op = NULL; /* we don't support munging link-layer headers */
818 p->getnonblock_op = sf_getnonblock;
819 p->setnonblock_op = sf_setnonblock;
820 p->stats_op = sf_stats;
821 p->close_op = sf_close;
822
823 #ifdef WIN32
824 /*
825 * If we're reading from the standard input, put it in binary
826 * mode, as savefiles are binary files.
827 */
828 if (fp == stdin)
829 _setmode(_fileno(f), _O_BINARY);
830 #endif
831
832 return (p);
833 bad:
834 if (fp != NULL && fp != stdin)
835 fclose(fp);
836 free(p);
837 return (NULL);
838 }
839
840 /*
841 * Read sf_readfile and return the next packet. Return the header in hdr
842 * and the contents in buf. Return 0 on success, SFERR_EOF if there were
843 * no more packets, and SFERR_TRUNC if a partial packet was encountered.
844 */
845 static int
846 sf_next_packet(pcap_t *p, struct pcap_pkthdr *hdr, u_char *buf, u_int buflen)
847 {
848 struct pcap_sf_patched_pkthdr sf_hdr;
849 FILE *fp = p->sf.rfile;
850 size_t amt_read;
851 bpf_u_int32 t;
852
853 /*
854 * Read the packet header; the structure we use as a buffer
855 * is the longer structure for files generated by the patched
856 * libpcap, but if the file has the magic number for an
857 * unpatched libpcap we only read as many bytes as the regular
858 * header has.
859 */
860 amt_read = fread(&sf_hdr, 1, p->sf.hdrsize, fp);
861 if (amt_read != p->sf.hdrsize) {
862 if (ferror(fp)) {
863 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
864 "error reading dump file: %s",
865 pcap_strerror(errno));
866 return (-1);
867 } else {
868 if (amt_read != 0) {
869 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
870 "truncated dump file; tried to read %d header bytes, only got %lu",
871 p->sf.hdrsize, (unsigned long)amt_read);
872 return (-1);
873 }
874 /* EOF */
875 return (1);
876 }
877 }
878
879 if (p->sf.swapped) {
880 /* these were written in opposite byte order */
881 hdr->caplen = SWAPLONG(sf_hdr.caplen);
882 hdr->len = SWAPLONG(sf_hdr.len);
883 hdr->ts.tv_sec = SWAPLONG(sf_hdr.ts.tv_sec);
884 hdr->ts.tv_usec = SWAPLONG(sf_hdr.ts.tv_usec);
885 } else {
886 hdr->caplen = sf_hdr.caplen;
887 hdr->len = sf_hdr.len;
888 hdr->ts.tv_sec = sf_hdr.ts.tv_sec;
889 hdr->ts.tv_usec = sf_hdr.ts.tv_usec;
890 }
891 /* Swap the caplen and len fields, if necessary. */
892 switch (p->sf.lengths_swapped) {
893
894 case NOT_SWAPPED:
895 break;
896
897 case MAYBE_SWAPPED:
898 if (hdr->caplen <= hdr->len) {
899 /*
900 * The captured length is <= the actual length,
901 * so presumably they weren't swapped.
902 */
903 break;
904 }
905 /* FALLTHROUGH */
906
907 case SWAPPED:
908 t = hdr->caplen;
909 hdr->caplen = hdr->len;
910 hdr->len = t;
911 break;
912 }
913
914 if (hdr->caplen > buflen) {
915 /*
916 * This can happen due to Solaris 2.3 systems tripping
917 * over the BUFMOD problem and not setting the snapshot
918 * correctly in the savefile header. If the caplen isn't
919 * grossly wrong, try to salvage.
920 */
921 static u_char *tp = NULL;
922 static size_t tsize = 0;
923
924 if (hdr->caplen > 65535) {
925 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
926 "bogus savefile header");
927 return (-1);
928 }
929
930 if (tsize < hdr->caplen) {
931 tsize = ((hdr->caplen + 1023) / 1024) * 1024;
932 if (tp != NULL)
933 free((u_char *)tp);
934 tp = (u_char *)malloc(tsize);
935 if (tp == NULL) {
936 tsize = 0;
937 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
938 "BUFMOD hack malloc");
939 return (-1);
940 }
941 }
942 amt_read = fread((char *)tp, 1, hdr->caplen, fp);
943 if (amt_read != hdr->caplen) {
944 if (ferror(fp)) {
945 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
946 "error reading dump file: %s",
947 pcap_strerror(errno));
948 } else {
949 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
950 "truncated dump file; tried to read %u captured bytes, only got %lu",
951 hdr->caplen, (unsigned long)amt_read);
952 }
953 return (-1);
954 }
955 /*
956 * We can only keep up to buflen bytes. Since caplen > buflen
957 * is exactly how we got here, we know we can only keep the
958 * first buflen bytes and must drop the remainder. Adjust
959 * caplen accordingly, so we don't get confused later as
960 * to how many bytes we have to play with.
961 */
962 hdr->caplen = buflen;
963 memcpy((char *)buf, (char *)tp, buflen);
964
965 } else {
966 /* read the packet itself */
967 amt_read = fread((char *)buf, 1, hdr->caplen, fp);
968 if (amt_read != hdr->caplen) {
969 if (ferror(fp)) {
970 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
971 "error reading dump file: %s",
972 pcap_strerror(errno));
973 } else {
974 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
975 "truncated dump file; tried to read %u captured bytes, only got %lu",
976 hdr->caplen, (unsigned long)amt_read);
977 }
978 return (-1);
979 }
980 }
981 return (0);
982 }
983
984 /*
985 * Print out packets stored in the file initialized by sf_read_init().
986 * If cnt > 0, return after 'cnt' packets, otherwise continue until eof.
987 */
988 int
989 pcap_offline_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
990 {
991 struct bpf_insn *fcode;
992 int status = 0;
993 int n = 0;
994
995 while (status == 0) {
996 struct pcap_pkthdr h;
997
998 /*
999 * Has "pcap_breakloop()" been called?
1000 * If so, return immediately - if we haven't read any
1001 * packets, clear the flag and return -2 to indicate
1002 * that we were told to break out of the loop, otherwise
1003 * leave the flag set, so that the *next* call will break
1004 * out of the loop without having read any packets, and
1005 * return the number of packets we've processed so far.
1006 */
1007 if (p->break_loop) {
1008 if (n == 0) {
1009 p->break_loop = 0;
1010 return (-2);
1011 } else
1012 return (n);
1013 }
1014
1015 status = sf_next_packet(p, &h, p->buffer, p->bufsize);
1016 if (status) {
1017 if (status == 1)
1018 return (0);
1019 return (status);
1020 }
1021
1022 if ((fcode = p->fcode.bf_insns) == NULL ||
1023 bpf_filter(fcode, p->buffer, h.len, h.caplen)) {
1024 (*callback)(user, &h, p->buffer);
1025 if (++n >= cnt && cnt > 0)
1026 break;
1027 }
1028 }
1029 /*XXX this breaks semantics tcpslice expects */
1030 return (n);
1031 }
1032
1033 /*
1034 * Output a packet to the initialized dump file.
1035 */
1036 void
1037 pcap_dump(u_char *user, const struct pcap_pkthdr *h, const u_char *sp)
1038 {
1039 register FILE *f;
1040 struct pcap_sf_pkthdr sf_hdr;
1041
1042 f = (FILE *)user;
1043 sf_hdr.ts.tv_sec = h->ts.tv_sec;
1044 sf_hdr.ts.tv_usec = h->ts.tv_usec;
1045 sf_hdr.caplen = h->caplen;
1046 sf_hdr.len = h->len;
1047 /* XXX we should check the return status */
1048 (void)fwrite(&sf_hdr, sizeof(sf_hdr), 1, f);
1049 (void)fwrite((char *)sp, h->caplen, 1, f);
1050 }
1051
1052 static pcap_dumper_t *
1053 pcap_setup_dump(pcap_t *p, int linktype, FILE *f, const char *fname)
1054 {
1055
1056 #ifdef WIN32
1057 /*
1058 * If we're writing to the standard output, put it in binary
1059 * mode, as savefiles are binary files.
1060 *
1061 * Otherwise, we turn off buffering.
1062 * XXX - why? And why not on the standard output?
1063 */
1064 if (f == stdout)
1065 _setmode(_fileno(f), _O_BINARY);
1066 else
1067 setbuf(f, NULL);
1068 #endif
1069 if (sf_write_header(f, linktype, p->tzoff, p->snapshot) == -1) {
1070 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Can't write to %s: %s",
1071 fname, pcap_strerror(errno));
1072 if (f != stdout)
1073 (void)fclose(f);
1074 return (NULL);
1075 }
1076 return ((pcap_dumper_t *)f);
1077 }
1078
1079 /*
1080 * Initialize so that sf_write() will output to the file named 'fname'.
1081 */
1082 pcap_dumper_t *
1083 pcap_dump_open(pcap_t *p, const char *fname)
1084 {
1085 FILE *f;
1086 int linktype;
1087
1088 linktype = dlt_to_linktype(p->linktype);
1089 if (linktype == -1) {
1090 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1091 "%s: link-layer type %d isn't supported in savefiles",
1092 fname, linktype);
1093 return (NULL);
1094 }
1095
1096 if (fname[0] == '-' && fname[1] == '\0') {
1097 f = stdout;
1098 fname = "standard output";
1099 } else {
1100 #ifndef WIN32
1101 f = fopen(fname, "w");
1102 #else
1103 f = fopen(fname, "wb");
1104 #endif
1105 if (f == NULL) {
1106 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
1107 fname, pcap_strerror(errno));
1108 return (NULL);
1109 }
1110 }
1111 return (pcap_setup_dump(p, linktype, f, fname));
1112 }
1113
1114 /*
1115 * Initialize so that sf_write() will output to the given stream.
1116 */
1117 pcap_dumper_t *
1118 pcap_dump_fopen(pcap_t *p, FILE *f)
1119 {
1120 int linktype;
1121
1122 linktype = dlt_to_linktype(p->linktype);
1123 if (linktype == -1) {
1124 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1125 "stream: link-layer type %d isn't supported in savefiles",
1126 linktype);
1127 return (NULL);
1128 }
1129
1130 return (pcap_setup_dump(p, linktype, f, "stream"));
1131 }
1132
1133 FILE *
1134 pcap_dump_file(pcap_dumper_t *p)
1135 {
1136 return ((FILE *)p);
1137 }
1138
1139 int
1140 pcap_dump_flush(pcap_dumper_t *p)
1141 {
1142
1143 if (fflush((FILE *)p) == EOF)
1144 return (-1);
1145 else
1146 return (0);
1147 }
1148
1149 void
1150 pcap_dump_close(pcap_dumper_t *p)
1151 {
1152
1153 #ifdef notyet
1154 if (ferror((FILE *)p))
1155 return-an-error;
1156 /* XXX should check return from fclose() too */
1157 #endif
1158 (void)fclose((FILE *)p);
1159 }