Addresses https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=3895. Note that this...
[metze/wireshark/wip.git] / wiretap / pcap-common.c
1 /* pcap-common.c
2  * Code common to libpcap and pcap-NG file formats
3  *
4  * $Id$
5  *
6  * Wiretap Library
7  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
8  *
9  * File format support for pcap-ng file format
10  * Copyright (c) 2007 by Ulf Lamping <ulf.lamping@web.de>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include "wtap-int.h"
35 #include "file_wrappers.h"
36 #include "atm.h"
37 #include "erf.h"
38 #include "pcap-encap.h"
39 #include "pcap-common.h"
40
41 /*
42  * Map link-layer types (LINKTYPE_ values) to Wiretap encapsulations.
43  */
44 /*
45  * Either LBL NRG wasn't an adequate central registry (e.g., because of
46  * the slow rate of releases from them), or nobody bothered using them
47  * as a central registry, as many different groups have patched libpcap
48  * (and BPF, on the BSDs) to add new encapsulation types, and have ended
49  * up using the same DLT_ values for different encapsulation types.
50  *
51  * For those numerical encapsulation type values that everybody uses for
52  * the same encapsulation type (which inclues those that some platforms
53  * specify different DLT_ names for but don't appear to use), we map
54  * those values to the appropriate Wiretap values.
55  *
56  * For those numerical encapsulation type values that different libpcap
57  * variants use for different encapsulation types, we check what
58  * <pcap.h> defined to determine how to interpret them, so that we
59  * interpret them the way the libpcap with which we're building
60  * Wireshark/Wiretap interprets them (which, if it doesn't support
61  * them at all, means we don't support them either - any capture files
62  * using them are foreign, and we don't hazard a guess as to which
63  * platform they came from; we could, I guess, choose the most likely
64  * platform).
65  *
66  * Note: if you need a new encapsulation type for libpcap files, do
67  * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
68  * add a new encapsulation type by changing an existing entry;
69  * leave the existing entries alone.
70  *
71  * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking for
72  * a new DLT_ value, and specifying the purpose of the new value.  When
73  * you get the new DLT_ value, use that numerical value in the "dlt_value"
74  * field of "pcap_to_wtap_map[]".
75  */
76
77 static const struct {
78         int     dlt_value;
79         int     wtap_encap_value;
80 } pcap_to_wtap_map[] = {
81         /*
82          * These are the values that are almost certainly the same
83          * in all libpcaps (I've yet to find one where the values
84          * in question are used for some purpose other than the
85          * one below, but...), and that Wiretap and Wireshark
86          * currently support.
87          */
88         { 0,            WTAP_ENCAP_NULL },      /* null encapsulation */
89         { 1,            WTAP_ENCAP_ETHERNET },
90         { 6,            WTAP_ENCAP_TOKEN_RING },        /* IEEE 802 Networks - assume token ring */
91         { 7,            WTAP_ENCAP_ARCNET },
92         { 8,            WTAP_ENCAP_SLIP },
93         { 9,            WTAP_ENCAP_PPP },
94 #ifdef BIT_SWAPPED_MAC_ADDRS
95         { 10,           WTAP_ENCAP_FDDI_BITSWAPPED },
96 #else
97         { 10,           WTAP_ENCAP_FDDI },
98 #endif
99
100         { 32,           WTAP_ENCAP_REDBACK },
101
102         /*
103          * 50 is DLT_PPP_SERIAL in NetBSD; it appears that DLT_PPP
104          * on BSD (at least according to standard tcpdump) has, as
105          * the first octet, an indication of whether the packet was
106          * transmitted or received (rather than having the standard
107          * PPP address value of 0xff), but that DLT_PPP_SERIAL puts
108          * a real live PPP header there, or perhaps a Cisco PPP header
109          * as per section 4.3.1 of RFC 1547 (implementations of this
110          * exist in various BSDs in "sys/net/if_spppsubr.c", and
111          * I think also exist either in standard Linux or in
112          * various Linux patches; the implementations show how to handle
113          * Cisco keepalive packets).
114          *
115          * However, I don't see any obvious place in FreeBSD "if_ppp.c"
116          * where anything other than the standard PPP header would be
117          * passed up.  I see some stuff that sets the first octet
118          * to 0 for incoming and 1 for outgoing packets before applying
119          * a BPF filter to see whether to drop packets whose protocol
120          * field has the 0x8000 bit set, i.e. network control protocols -
121          * those are handed up to userland - but that code puts the
122          * address field back before passing the packet up.
123          *
124          * I also don't see anything immediately obvious that munges
125          * the address field for sync PPP, either.
126          *
127          * Wireshark currently assumes that if the first octet of a
128          * PPP frame is 0xFF, it's the address field and is followed
129          * by a control field and a 2-byte protocol, otherwise the
130          * address and control fields are absent and the frame begins
131          * with a protocol field.  If we ever see a BSD/OS PPP
132          * capture, we'll have to handle it differently, and we may
133          * have to handle standard BSD captures differently if, in fact,
134          * they don't have 0xff 0x03 as the first two bytes - but, as per
135          * the two paragraphs preceding this, it's not clear that
136          * the address field *is* munged into an incoming/outgoing
137          * field when the packet is handed to the BPF device.
138          *
139          * For now, we just map DLT_PPP_SERIAL to WTAP_ENCAP_PPP, as
140          * we treat WTAP_ENCAP_PPP packets as if those beginning with
141          * 0xff have the standard RFC 1662 "PPP in HDLC-like Framing"
142          * 0xff 0x03 address/control header, and DLT_PPP_SERIAL frames
143          * appear to contain that unless they're Cisco frames (if we
144          * ever see a capture with them, we'd need to implement the
145          * RFC 1547 stuff, and the keepalive protocol stuff).
146          *
147          * We may have to distinguish between "PPP where if it doesn't
148          * begin with 0xff there's no HDLC encapsulation and the frame
149          * begins with the protocol field" (which is how we handle
150          * WTAP_ENCAP_PPP now) and "PPP where there's either HDLC
151          * encapsulation or Cisco PPP" (which is what DLT_PPP_SERIAL
152          * is) at some point.
153          *
154          * XXX - NetBSD has DLT_HDLC, which appears to be used for
155          * Cisco HDLC.  Ideally, they should use DLT_PPP_SERIAL
156          * only for real live HDLC-encapsulated PPP, not for Cisco
157          * HDLC.
158          */
159         { 50,           WTAP_ENCAP_PPP },
160
161         /*
162          * Used by NetBSD and OpenBSD pppoe(4).
163          */
164         { 51,           WTAP_ENCAP_PPP_ETHER },
165
166         /*
167          * Apparently used by the Axent Raptor firewall (now Symantec
168          * Enterprise Firewall).
169          * Thanks, Axent, for not reserving that type with tcpdump.org
170          * and not telling anybody about it.
171          */
172         { 99,           WTAP_ENCAP_SYMANTEC },
173
174         /*
175          * These are the values that libpcap 0.5 and later use in
176          * capture file headers, in an attempt to work around the
177          * confusion decried above, and that Wiretap and Wireshark
178          * currently support.
179          */
180         { 100,          WTAP_ENCAP_ATM_RFC1483 },
181         { 101,          WTAP_ENCAP_RAW_IP },
182 #if 0
183         /*
184          * More values used by libpcap 0.5 as DLT_ values and used by the
185          * current CVS version of libpcap in capture file headers.
186          * They are not yet handled in Wireshark.
187          * If we get a capture that contains them, we'll implement them.
188          */
189         { 102,          WTAP_ENCAP_SLIP_BSDOS },
190         { 103,          WTAP_ENCAP_PPP_BSDOS },
191 #endif
192
193         /*
194          * These ones are handled in Wireshark, though.
195          */
196         { 104,          WTAP_ENCAP_CHDLC },     /* Cisco HDLC */
197         { 105,          WTAP_ENCAP_IEEE_802_11 }, /* IEEE 802.11 */
198         { 106,          WTAP_ENCAP_LINUX_ATM_CLIP },
199         { 107,          WTAP_ENCAP_FRELAY },    /* Frame Relay */
200         { 108,          WTAP_ENCAP_NULL },      /* OpenBSD loopback */
201         { 109,          WTAP_ENCAP_ENC },       /* OpenBSD IPSEC enc */
202 #if 0
203         { 110,          WTAP_ENCAP_LANE_802_3 },/* ATM LANE 802.3 */
204         { 111,          WTAP_ENCAP_HIPPI },     /* NetBSD HIPPI */
205 #endif
206         { 112,          WTAP_ENCAP_CHDLC },     /* NetBSD HDLC framing */
207
208         /*
209          * Linux "cooked mode" captures, used by the current CVS version
210          * of libpcap
211          * OR
212          * it could be a packet in Cisco's ERSPAN encapsulation which uses
213          * this number as well (why can't people stick to protocols when it
214          * comes to allocating/using DLT types).
215          */
216         { 113,          WTAP_ENCAP_SLL },       /* Linux cooked capture */
217
218         { 114,          WTAP_ENCAP_LOCALTALK }, /* Localtalk */
219
220         /*
221          * The tcpdump.org version of libpcap uses 117, rather than 17,
222          * for OpenBSD packet filter logging, so as to avoid conflicting
223          * with DLT_LANE8023 in SuSE 6.3 libpcap.
224          */
225         { 117,          WTAP_ENCAP_PFLOG },
226
227         { 118,          WTAP_ENCAP_CISCO_IOS },
228         { 119,          WTAP_ENCAP_IEEE_802_11_PRISM }, /* 802.11 plus Prism monitor mode radio header */
229         { 121,          WTAP_ENCAP_HHDLC },     /* HiPath HDLC */
230         { 122,          WTAP_ENCAP_IP_OVER_FC },   /* RFC 2625 IP-over-FC */
231         { 123,          WTAP_ENCAP_ATM_PDUS },  /* SunATM */
232         { 127,          WTAP_ENCAP_IEEE_802_11_RADIOTAP },  /* 802.11 plus radiotap radio header */
233         { 128,          WTAP_ENCAP_TZSP },      /* Tazmen Sniffer Protocol */
234         { 129,          WTAP_ENCAP_ARCNET_LINUX },
235         { 130,          WTAP_ENCAP_JUNIPER_MLPPP }, /* Juniper MLPPP on ML-, LS-, AS- PICs */
236         { 131,          WTAP_ENCAP_JUNIPER_MLFR }, /* Juniper MLFR (FRF.15) on ML-, LS-, AS- PICs */
237         { 133,          WTAP_ENCAP_JUNIPER_GGSN},
238         /*
239          * Values 132-134, 136 not listed here are reserved for use
240          * in Juniper hardware.
241          */
242         { 135,          WTAP_ENCAP_JUNIPER_ATM2 }, /* various encapsulations captured on the ATM2 PIC */
243         { 137,          WTAP_ENCAP_JUNIPER_ATM1 }, /* various encapsulations captured on the ATM1 PIC */
244
245         { 138,          WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 },
246                                                 /* Apple IP-over-IEEE 1394 */
247
248         { 139,          WTAP_ENCAP_MTP2_WITH_PHDR },
249         { 140,          WTAP_ENCAP_MTP2 },
250         { 141,          WTAP_ENCAP_MTP3 },
251         { 142,          WTAP_ENCAP_SCCP },
252         { 143,          WTAP_ENCAP_DOCSIS },
253         { 144,          WTAP_ENCAP_IRDA },      /* IrDA capture */
254
255         /* Reserved for private use. */
256         { 147,          WTAP_ENCAP_USER0 },
257         { 148,          WTAP_ENCAP_USER1 },
258         { 149,          WTAP_ENCAP_USER2 },
259         { 150,          WTAP_ENCAP_USER3 },
260         { 151,          WTAP_ENCAP_USER4 },
261         { 152,          WTAP_ENCAP_USER5 },
262         { 153,          WTAP_ENCAP_USER6 },
263         { 154,          WTAP_ENCAP_USER7 },
264         { 155,          WTAP_ENCAP_USER8 },
265         { 156,          WTAP_ENCAP_USER9 },
266         { 157,          WTAP_ENCAP_USER10 },
267         { 158,          WTAP_ENCAP_USER11 },
268         { 159,          WTAP_ENCAP_USER12 },
269         { 160,          WTAP_ENCAP_USER13 },
270         { 161,          WTAP_ENCAP_USER14 },
271         { 162,          WTAP_ENCAP_USER15 },
272
273         { 163,          WTAP_ENCAP_IEEE_802_11_AVS },  /* 802.11 plus AVS radio header */
274
275         /*
276          * 164 is reserved for Juniper-private chassis-internal
277          * meta-information such as QoS profiles, etc..
278          */
279
280         { 165,          WTAP_ENCAP_BACNET_MS_TP },
281
282         /*
283          * 166 is reserved for a PPP variant in which the first byte
284          * of the 0xff03 header, the 0xff, is replaced by a direction
285          * byte.  I don't know whether any captures look like that,
286          * but it is used for some Linux IP filtering (ipfilter?).
287          */
288
289         /* Ethernet PPPoE frames captured on a service PIC */
290         { 167,          WTAP_ENCAP_JUNIPER_PPPOE },
291
292         /*
293          * 168 is reserved for more Juniper private-chassis-
294          * internal meta-information.
295          */
296
297         { 169,          WTAP_ENCAP_GPRS_LLC },
298
299         /*
300          * 170 and 171 are reserved for ITU-T G.7041/Y.1303 Generic
301          * Framing Procedure.
302          */
303
304         /* Registered by Gcom, Inc. */
305         { 172,          WTAP_ENCAP_GCOM_TIE1 },
306         { 173,          WTAP_ENCAP_GCOM_SERIAL },
307
308         { 177,          WTAP_ENCAP_LINUX_LAPD },
309
310     /* Ethernet frames prepended with meta-information */
311         { 178,          WTAP_ENCAP_JUNIPER_ETHER },
312         /* PPP frames prepended with meta-information */
313         { 179,          WTAP_ENCAP_JUNIPER_PPP },
314         /* Frame-Relay frames prepended with meta-information */
315         { 180,          WTAP_ENCAP_JUNIPER_FRELAY },
316         /* C-HDLC frames prepended with meta-information */
317         { 181,          WTAP_ENCAP_JUNIPER_CHDLC },
318         /* VOIP Frames prepended with meta-information */
319         { 183,          WTAP_ENCAP_JUNIPER_VP },
320         /* raw USB packets */
321         { 186,          WTAP_ENCAP_USB },
322         /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
323         { 187,          WTAP_ENCAP_BLUETOOTH_H4 },
324         /* IEEE 802.16 MAC Common Part Sublayer */
325         { 188,          WTAP_ENCAP_IEEE802_16_MAC_CPS },
326         /* USB packets with Linux-specified header */
327         { 189,          WTAP_ENCAP_USB_LINUX },
328         /* CAN 2.0b frame */
329         { 190,          WTAP_ENCAP_CAN20B },
330         /* Per-Packet Information header */
331         { 192,          WTAP_ENCAP_PPI },
332         /* IEEE 802.15.4 Wireless PAN */
333         { 195,          WTAP_ENCAP_IEEE802_15_4 },
334         /* SITA File Encapsulation */
335         { 196,          WTAP_ENCAP_SITA },
336         /* Endace Record File Encapsulation */
337         { 197,          WTAP_ENCAP_ERF },
338         /* IPMB */
339         { 199,          WTAP_ENCAP_IPMB },
340         /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
341         { 201,          WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR },
342         /* LAPD frame */
343         { 203,          WTAP_ENCAP_LAPD },
344         /* PPP with pseudoheader */
345         { 204,          WTAP_ENCAP_PPP_WITH_PHDR },
346         /* IPMB/I2C */
347         { 209,          WTAP_ENCAP_I2C },
348         /* FlexRay frame */
349         { 210,          WTAP_ENCAP_FLEXRAY },
350         /* MOST frame */
351         { 211,          WTAP_ENCAP_MOST },
352         /* LIN frame */
353         { 212,          WTAP_ENCAP_LIN },
354         /* X2E Xoraya serial frame */
355         { 213,          WTAP_ENCAP_X2E_SERIAL },
356         /* X2E Xoraya frame */
357         { 214,          WTAP_ENCAP_X2E_XORAYA },
358         /* IEEE 802.15.4 Wireless PAN non-ASK PHY */
359         { 215,          WTAP_ENCAP_IEEE802_15_4_NONASK_PHY },
360         /* USB packets with padded Linux-specified header */
361         { 220,          WTAP_ENCAP_USB_LINUX_MMAPPED },
362         /* Fibre Channel FC-2 frame */
363         { 224,          WTAP_ENCAP_FIBRE_CHANNEL_FC2 },
364         /* Fibre Channel FC-2 frame with Delimiter */
365         { 225,          WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS },
366         /* Solaris IPNET */
367         { 226,          WTAP_ENCAP_IPNET },
368         /* SocketCAN frame */
369         { 227,          WTAP_ENCAP_SOCKETCAN },
370         /* Raw IPv4 */
371         { 228,          WTAP_ENCAP_RAW_IP4 },
372         /* Raw IPv6 */
373         { 229,          WTAP_ENCAP_RAW_IP6 },
374         /* IEEE 802.15.4 Wireless PAN no fcs */
375         { 230,          WTAP_ENCAP_IEEE802_15_4_NOFCS },
376         /* D-BUS */
377         { 231,          WTAP_ENCAP_DBUS },
378         /* DVB-CI (Common Interface) */
379         { 235,          WTAP_ENCAP_DVBCI },
380         /* MUX27010 */
381         { 236,          WTAP_ENCAP_MUX27010 },
382         /* NFLOG */
383         { 239,          WTAP_ENCAP_NFLOG },
384         /* netANALYZER pseudo-header followed by Ethernet with CRC */
385         { 240,          WTAP_ENCAP_NETANALYZER },
386         /* netANALYZER pseudo-header in transparent mode */
387         { 241,          WTAP_ENCAP_NETANALYZER_TRANSPARENT },
388         /* IP-over-Infiniband, as specified by RFC 4391 section 6 */
389         { 242,          WTAP_ENCAP_IP_OVER_IB },
390         /* ISO/IEC 13818-1 MPEG2-TS packets */
391         { 243,          WTAP_ENCAP_MPEG_2_TS },
392         /* NFC LLCP */
393         { 245,          WTAP_ENCAP_NFC_LLCP },
394
395         /*
396          * To repeat:
397          *
398          * If you need a new encapsulation type for libpcap files, do
399          * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
400          * add a new encapsulation type by changing an existing entry;
401          * leave the existing entries alone.
402          *
403          * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
404          * for a new DLT_ value, and specifying the purpose of the new value.
405          * When you get the new DLT_ value, use that numerical value in
406          * the "dlt_value" field of "pcap_to_wtap_map[]".
407          */
408
409         /*
410          * The following are entries for libpcap type values that have
411          * different meanings on different OSes.
412          *
413          * We put these *after* the entries for the platform-independent
414          * libpcap type values for those Wiretap encapsulation types, so
415          * that Wireshark chooses the platform-independent libpcap type
416          * value for those encapsulatioin types, not the platform-dependent
417          * one.
418          */
419
420         /*
421          * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
422          * seen that define anything other than DLT_ATM_RFC1483 as 11 are
423          * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
424          * which define it as 100, mapping the kernel's value to 100, in
425          * an attempt to hide the different values used on different
426          * platforms.
427          *
428          * If this is a platform where DLT_FR is defined as 11, we
429          * don't handle 11 at all; otherwise, we handle it as
430          * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
431          * captures from BSD/OS if running on platforms other than BSD/OS,
432          * but
433          *
434          *      1) we don't yet support DLT_FR
435          *
436          * and
437          *
438          *      2) nothing short of a heuristic would let us interpret
439          *         them correctly).
440          */
441 #if defined(DLT_FR) && (DLT_FR == 11)
442         { 11,           WTAP_ENCAP_FRELAY },
443 #else
444         { 11,           WTAP_ENCAP_ATM_RFC1483 },
445 #endif
446
447         /*
448          * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
449          * BSD/OS, and DLT_LOOP on OpenBSD.
450          *
451          * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
452          * (it's just like DLT_NULL, only with the AF_ value in network
453          * rather than host byte order - Wireshark figures out the
454          * byte order from the data, so we don't care what byte order
455          * it's in), so if DLT_LOOP is defined as 12, interpret 12
456          * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
457          * as 12, interpret it as WTAP_ENCAP_RAW_IP.
458          */
459 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
460         { 12,           WTAP_ENCAP_NULL },
461 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
462         /*
463          * Put entry for Cisco HDLC here.
464          * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
465          * start with a 4-byte Cisco HDLC header?
466          */
467 #else
468         { 12,           WTAP_ENCAP_RAW_IP },
469 #endif
470
471         /*
472          * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
473          * don't actually generate it.  I infer that BSD/OS translates
474          * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
475          * libpcap, as the BSD/OS link-layer header is different;
476          * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
477          *
478          * From this, I infer that there's no point in handling 13
479          * as DLT_SLIP_BSDOS.
480          *
481          * 13 is DLT_ATM_RFC1483 on BSD/OS.
482          *
483          * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
484          * of decrypted IPsec traffic.
485          *
486          * We treat 13 as WTAP_ENCAP_ENC on all systems except those
487          * that define DLT_ATM_RFC1483 as 13 - presumably only
488          * BSD/OS does so - so that, on BSD/OS systems, we still
489          * treate 13 as WTAP_ENCAP_ATM_RFC1483, but, on all other
490          * systems, we can read OpenBSD DLT_ENC captures.
491          */
492 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
493         { 13,           WTAP_ENCAP_ATM_RFC1483 },
494 #else
495         { 13,           WTAP_ENCAP_ENC },
496 #endif
497
498         /*
499          * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
500          * don't actually generate it.  I infer that BSD/OS translates
501          * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
502          * libpcap, as the BSD/OS link-layer header is different;
503          * however, in BSD/OS, DLT_PPP_BSDOS is 16.
504          *
505          * From this, I infer that there's no point in handling 14
506          * as DLT_PPP_BSDOS.
507          *
508          * 14 is DLT_RAW on BSD/OS and OpenBSD.
509          */
510         { 14,           WTAP_ENCAP_RAW_IP },
511
512         /*
513          * 15 is:
514          *
515          *      DLT_SLIP_BSDOS on BSD/OS;
516          *
517          *      DLT_HIPPI on NetBSD;
518          *
519          *      DLT_LANE8023 with Alexey Kuznetzov's patches for
520          *      Linux libpcap;
521          *
522          *      DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
523          *      (and on SuSE 6.3);
524          *
525          * but we don't currently handle any of those.
526          */
527
528         /*
529          * 16 is:
530          *
531          *      DLT_PPP_BSDOS on BSD/OS;
532          *
533          *      DLT_HDLC on NetBSD (Cisco HDLC);
534          *
535          *      DLT_CIP with Alexey Kuznetzov's patches for
536          *      Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
537          *
538          *      DLT_I4L_IP with the ISDN4Linux patches for libpcap
539          *      (and on SuSE 6.3).
540          */
541 #if defined(DLT_CIP) && (DLT_CIP == 16)
542         { 16,           WTAP_ENCAP_LINUX_ATM_CLIP },
543 #endif
544 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
545         { 16,           WTAP_ENCAP_CHDLC },
546 #endif
547
548         /*
549          * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
550          * handle it.
551          * It is also used as the PF (Packet Filter) logging format beginning
552          * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
553          * defined with the value 17.
554          */
555 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
556         { 17,           WTAP_ENCAP_OLD_PFLOG },
557 #endif
558
559         /*
560          * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
561          * DLT_CIP of 16 that the Alexey Kuznetzov patches for
562          * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
563          * I've not found any libpcap that uses it for any other purpose -
564          * hopefully nobody will do so in the future.
565          */
566         { 18,           WTAP_ENCAP_LINUX_ATM_CLIP },
567
568         /*
569          * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
570          * recent versions I've seen of the Linux ATM distribution;
571          * I've not yet found any libpcap that uses it for any other
572          * purpose - hopefully nobody will do so in the future.
573          */
574         { 19,           WTAP_ENCAP_LINUX_ATM_CLIP },
575
576         /*
577          * To repeat:
578          *
579          * If you need a new encapsulation type for libpcap files, do
580          * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
581          * add a new encapsulation type by changing an existing entry;
582          * leave the existing entries alone.
583          *
584          * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
585          * for a new DLT_ value, and specifying the purpose of the new value.
586          * When you get the new DLT_ value, use that numerical value in
587          * the "dlt_value" field of "pcap_to_wtap_map[]".
588          */
589 };
590 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
591
592 int
593 wtap_pcap_encap_to_wtap_encap(int encap)
594 {
595         unsigned int i;
596
597         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
598                 if (pcap_to_wtap_map[i].dlt_value == encap)
599                         return pcap_to_wtap_map[i].wtap_encap_value;
600         }
601         return WTAP_ENCAP_UNKNOWN;
602 }
603
604 int
605 wtap_wtap_encap_to_pcap_encap(int encap)
606 {
607         unsigned int i;
608
609         switch (encap) {
610
611         case WTAP_ENCAP_FDDI:
612         case WTAP_ENCAP_FDDI_BITSWAPPED:
613         case WTAP_ENCAP_NETTL_FDDI:
614                 /*
615                  * Special-case WTAP_ENCAP_FDDI and
616                  * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
617                  * to DLT_FDDI (even though that may mean that the bit
618                  * order in the FDDI MAC addresses is wrong; so it goes
619                  * - libpcap format doesn't record the byte order,
620                  * so that's not fixable).
621                  */
622                 return 10;      /* that's DLT_FDDI */
623
624         case WTAP_ENCAP_FRELAY_WITH_PHDR:
625                 /*
626                  * Do the same with Frame Relay.
627                  */
628                 return 107;
629
630         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
631                 /*
632                  * Map this to DLT_IEEE802_11, for now, even though
633                  * that means the radio information will be lost.
634                  * Once tcpdump support for the BSD radiotap header
635                  * is sufficiently widespread, we should probably
636                  * use that, instead - although we should probably
637                  * ultimately just have WTAP_ENCAP_IEEE_802_11
638                  * as the only Wiretap encapsulation for 802.11,
639                  * and have the pseudo-header include a radiotap-style
640                  * list of attributes.  If we do that, though, we
641                  * should probably bypass the regular Wiretap code
642                  * when writing out packets during a capture, and just
643                  * do the equivalent of a libpcap write (unfortunately,
644                  * libpcap doesn't have an "open dump by file descriptor"
645                  * function, so we can't just use "pcap_dump()"), so
646                  * that we don't spend cycles mapping from libpcap to
647                  * Wiretap and then back to libpcap.  (There are other
648                  * reasons to do that, e.g. to handle AIX libpcap better.)
649                  */
650                 return 105;
651         }
652
653         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
654                 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
655                         return pcap_to_wtap_map[i].dlt_value;
656         }
657         return -1;
658 }
659
660 /*
661  * Various pseudo-headers that appear at the beginning of packet data.
662  *
663  * We represent them as sets of offsets, as they might not be aligned on
664  * an appropriate structure boundary in the buffer, and as that makes them
665  * independent of the way the compiler might align fields.
666  */
667
668 /*
669  * The link-layer header on SunATM packets.
670  */
671 #define SUNATM_FLAGS    0       /* destination and traffic type - 1 byte */
672 #define SUNATM_VPI      1       /* VPI - 1 byte */
673 #define SUNATM_VCI      2       /* VCI - 2 bytes */
674 #define SUNATM_LEN      4       /* length of the header */
675
676 /*
677  * The link-layer header on Nokia IPSO ATM packets.
678  */
679 #define NOKIAATM_FLAGS  0       /* destination - 1 byte */
680 #define NOKIAATM_VPI    1       /* VPI - 1 byte */
681 #define NOKIAATM_VCI    2       /* VCI - 2 bytes */
682 #define NOKIAATM_LEN    4       /* length of the header */
683
684 /*
685  * The link-layer header on Nokia IPSO packets.
686  */
687 #define NOKIA_LEN       4       /* length of the header */
688
689 /*
690  * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
691  * to libpcap.
692  */
693 #define IRDA_SLL_PKTTYPE_OFFSET         0       /* packet type - 2 bytes */
694 /* 12 unused bytes */
695 #define IRDA_SLL_PROTOCOL_OFFSET        14      /* protocol, should be ETH_P_LAPD - 2 bytes */
696 #define IRDA_SLL_LEN                    16      /* length of the header */
697
698 /*
699  * A header containing additional MTP information.
700  */
701 #define MTP2_SENT_OFFSET                0       /* 1 byte */
702 #define MTP2_ANNEX_A_USED_OFFSET        1       /* 1 byte */
703 #define MTP2_LINK_NUMBER_OFFSET         2       /* 2 bytes */
704 #define MTP2_HDR_LEN                    4       /* length of the header */
705
706 /*
707  * A header containing additional SITA WAN information.
708  */
709 #define SITA_FLAGS_OFFSET               0       /* 1 byte */
710 #define SITA_SIGNALS_OFFSET             1       /* 1 byte */
711 #define SITA_ERRORS1_OFFSET             2       /* 1 byte */
712 #define SITA_ERRORS2_OFFSET             3       /* 1 byte */
713 #define SITA_PROTO_OFFSET               4       /* 1 byte */
714 #define SITA_HDR_LEN                    5       /* length of the header */
715
716 /*
717  * The fake link-layer header of LAPD packets.
718  */
719 #ifndef ETH_P_LAPD
720 #define ETH_P_LAPD 0x0030
721 #endif
722
723 #define LAPD_SLL_PKTTYPE_OFFSET         0       /* packet type - 2 bytes */
724 #define LAPD_SLL_HATYPE_OFFSET          2       /* hardware address type - 2 bytes */
725 #define LAPD_SLL_HALEN_OFFSET           4       /* hardware address length - 2 bytes */
726 #define LAPD_SLL_ADDR_OFFSET            6       /* address - 8 bytes */
727 #define LAPD_SLL_PROTOCOL_OFFSET        14      /* protocol, should be ETH_P_LAPD - 2 bytes */
728 #define LAPD_SLL_LEN                    16      /* length of the header */
729
730 /*
731  * The NFC LLCP per-packet header.
732  */
733 #define LLCP_ADAPTER_OFFSET             0
734 #define LLCP_FLAGS_OFFSET               1
735 #define LLCP_HEADER_LEN                 2
736
737 /*
738  * I2C link-layer on-disk format
739  */
740 struct i2c_file_hdr {
741     guint8 bus;
742     guint8 flags[4];
743 };
744
745 static gboolean
746 pcap_read_sunatm_pseudoheader(FILE_T fh,
747     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
748 {
749         guint8  atm_phdr[SUNATM_LEN];
750         int     bytes_read;
751         guint8  vpi;
752         guint16 vci;
753
754         errno = WTAP_ERR_CANT_READ;
755         bytes_read = file_read(atm_phdr, SUNATM_LEN, fh);
756         if (bytes_read != SUNATM_LEN) {
757                 *err = file_error(fh, err_info);
758                 if (*err == 0)
759                         *err = WTAP_ERR_SHORT_READ;
760                 return FALSE;
761         }
762
763         vpi = atm_phdr[SUNATM_VPI];
764         vci = pntohs(&atm_phdr[SUNATM_VCI]);
765
766         switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
767
768         case 0x01:      /* LANE */
769                 pseudo_header->atm.aal = AAL_5;
770                 pseudo_header->atm.type = TRAF_LANE;
771                 break;
772
773         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
774                 pseudo_header->atm.aal = AAL_5;
775                 pseudo_header->atm.type = TRAF_LLCMX;
776                 break;
777
778         case 0x05:      /* ILMI */
779                 pseudo_header->atm.aal = AAL_5;
780                 pseudo_header->atm.type = TRAF_ILMI;
781                 break;
782
783         case 0x06:      /* Q.2931 */
784                 pseudo_header->atm.aal = AAL_SIGNALLING;
785                 pseudo_header->atm.type = TRAF_UNKNOWN;
786                 break;
787
788         case 0x03:      /* MARS (RFC 2022) */
789                 pseudo_header->atm.aal = AAL_5;
790                 pseudo_header->atm.type = TRAF_UNKNOWN;
791                 break;
792
793         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
794                 pseudo_header->atm.aal = AAL_5;
795                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
796                 break;
797
798         default:
799                 /*
800                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
801                  * case assume it's AAL_SIGNALLING; we know nothing more
802                  * about it.
803                  *
804                  * XXX - is this necessary?  Or are we guaranteed that
805                  * all signalling traffic has a type of 0x06?
806                  *
807                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
808                  * 0x00 ("raw"), might it be non-AAL5 traffic?
809                  */
810                 if (vpi == 0 && vci == 5)
811                         pseudo_header->atm.aal = AAL_SIGNALLING;
812                 else
813                         pseudo_header->atm.aal = AAL_5;
814                 pseudo_header->atm.type = TRAF_UNKNOWN;
815                 break;
816         }
817         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
818
819         pseudo_header->atm.vpi = vpi;
820         pseudo_header->atm.vci = vci;
821         pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
822
823         /* We don't have this information */
824         pseudo_header->atm.flags = 0;
825         pseudo_header->atm.cells = 0;
826         pseudo_header->atm.aal5t_u2u = 0;
827         pseudo_header->atm.aal5t_len = 0;
828         pseudo_header->atm.aal5t_chksum = 0;
829
830         return TRUE;
831 }
832
833 static gboolean
834 pcap_read_nokiaatm_pseudoheader(FILE_T fh,
835     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
836 {
837         guint8  atm_phdr[NOKIAATM_LEN];
838         int     bytes_read;
839         guint8  vpi;
840         guint16 vci;
841
842         errno = WTAP_ERR_CANT_READ;
843         bytes_read = file_read(atm_phdr, NOKIAATM_LEN, fh);
844         if (bytes_read != NOKIAATM_LEN) {
845                 *err = file_error(fh, err_info);
846                 if (*err == 0)
847                         *err = WTAP_ERR_SHORT_READ;
848                 return FALSE;
849         }
850
851         vpi = atm_phdr[NOKIAATM_VPI];
852         vci = pntohs(&atm_phdr[NOKIAATM_VCI]);
853
854         pseudo_header->atm.vpi = vpi;
855         pseudo_header->atm.vci = vci;
856         pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
857
858         /* We don't have this information */
859         pseudo_header->atm.flags = 0;
860         pseudo_header->atm.cells = 0;
861         pseudo_header->atm.aal5t_u2u = 0;
862         pseudo_header->atm.aal5t_len = 0;
863         pseudo_header->atm.aal5t_chksum = 0;
864
865         return TRUE;
866 }
867
868 static gboolean
869 pcap_read_nokia_pseudoheader(FILE_T fh,
870     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
871 {
872         guint8  phdr[NOKIA_LEN];
873         int     bytes_read;
874
875         errno = WTAP_ERR_CANT_READ;
876
877         /* backtrack to read the 4 mysterious bytes that aren't considered
878         * part of the packet size
879         */
880         if (file_seek(fh, -NOKIA_LEN, SEEK_CUR, err) == -1)
881         {
882                 *err = file_error(fh, err_info);
883                 if (*err == 0)
884                         *err = WTAP_ERR_SHORT_READ;
885                 return FALSE;
886         }
887
888         bytes_read = file_read(phdr, NOKIA_LEN, fh);
889         if (bytes_read != NOKIA_LEN) {
890                 *err = file_error(fh, err_info);
891                 if (*err == 0)
892                         *err = WTAP_ERR_SHORT_READ;
893                 return FALSE;
894         }
895
896         memcpy(pseudo_header->nokia.stuff, phdr, NOKIA_LEN);
897
898         return TRUE;
899 }
900
901 static gboolean
902 pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
903     int *err, gchar **err_info)
904 {
905         guint8  irda_phdr[IRDA_SLL_LEN];
906         int     bytes_read;
907
908         errno = WTAP_ERR_CANT_READ;
909         bytes_read = file_read(irda_phdr, IRDA_SLL_LEN, fh);
910         if (bytes_read != IRDA_SLL_LEN) {
911                 *err = file_error(fh, err_info);
912                 if (*err == 0)
913                         *err = WTAP_ERR_SHORT_READ;
914                 return FALSE;
915         }
916
917         if (pntohs(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
918                 *err = WTAP_ERR_BAD_FILE;
919                 if (err_info != NULL)
920                         *err_info = g_strdup("libpcap: IrDA capture has a packet with an invalid sll_protocol field");
921                 return FALSE;
922         }
923
924         pseudo_header->irda.pkttype = pntohs(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
925
926         return TRUE;
927 }
928
929 static gboolean
930 pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
931 {
932         guint8 mtp2_hdr[MTP2_HDR_LEN];
933         int    bytes_read;
934
935         errno = WTAP_ERR_CANT_READ;
936         bytes_read = file_read(mtp2_hdr, MTP2_HDR_LEN, fh);
937         if (bytes_read != MTP2_HDR_LEN) {
938                 *err = file_error(fh, err_info);
939                 if (*err == 0)
940                         *err = WTAP_ERR_SHORT_READ;
941                 return FALSE;
942         }
943
944         pseudo_header->mtp2.sent         = mtp2_hdr[MTP2_SENT_OFFSET];
945         pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
946         pseudo_header->mtp2.link_number  = pntohs(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
947
948         return TRUE;
949 }
950
951 static gboolean
952 pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
953     int *err, gchar **err_info)
954 {
955         guint8  lapd_phdr[LAPD_SLL_LEN];
956         int     bytes_read;
957
958         errno = WTAP_ERR_CANT_READ;
959         bytes_read = file_read(lapd_phdr, LAPD_SLL_LEN, fh);
960         if (bytes_read != LAPD_SLL_LEN) {
961                 *err = file_error(fh, err_info);
962                 if (*err == 0)
963                         *err = WTAP_ERR_SHORT_READ;
964                 return FALSE;
965         }
966
967         if (pntohs(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
968                 *err = WTAP_ERR_BAD_FILE;
969                 if (err_info != NULL)
970                         *err_info = g_strdup("libpcap: LAPD capture has a packet with an invalid sll_protocol field");
971                 return FALSE;
972         }
973
974         pseudo_header->lapd.pkttype = pntohs(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
975         pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
976
977         return TRUE;
978 }
979
980 static gboolean
981 pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
982 {
983         guint8  sita_phdr[SITA_HDR_LEN];
984         int     bytes_read;
985
986         errno = WTAP_ERR_CANT_READ;
987         bytes_read = file_read(sita_phdr, SITA_HDR_LEN, fh);
988         if (bytes_read != SITA_HDR_LEN) {
989                 *err = file_error(fh, err_info);
990                 if (*err == 0)
991                         *err = WTAP_ERR_SHORT_READ;
992                 return FALSE;
993         }
994
995         pseudo_header->sita.sita_flags   = sita_phdr[SITA_FLAGS_OFFSET];
996         pseudo_header->sita.sita_signals = sita_phdr[SITA_SIGNALS_OFFSET];
997         pseudo_header->sita.sita_errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
998         pseudo_header->sita.sita_errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
999         pseudo_header->sita.sita_proto   = sita_phdr[SITA_PROTO_OFFSET];
1000
1001         return TRUE;
1002 }
1003
1004 /*
1005  * When not using the memory-mapped interface to capture USB events,
1006  * code that reads those events can use the MON_IOCX_GET ioctl to
1007  * read a 48-byte header consisting of a "struct linux_usb_phdr", as
1008  * defined below, followed immediately by one of:
1009  *
1010  *      8 bytes of a "struct usb_device_setup_hdr", if "setup_flag"
1011  *      in the preceding "struct linux_usb_phdr" is 0;
1012  *
1013  *      in Linux 2.6.30 or later, 8 bytes of a "struct iso_rec", if
1014  *      this is an isochronous transfer;
1015  *
1016  *      8 bytes of junk, otherwise.
1017  *
1018  * In Linux 2.6.31 and later, it can also use the MON_IOCX_GETX ioctl
1019  * to read a 64-byte header; that header consists of the 48 bytes
1020  * above, followed immediately by 16 bytes of a "struct linux_usb_phdr_ext",
1021  * as defined below.
1022  *
1023  * In Linux 2.6.21 and later, there's a memory-mapped interface to
1024  * capture USB events.  In that interface, the events in the memory-mapped
1025  * buffer have a 64-byte header, followed immediately by the data.
1026  * In Linux 2.6.21 through 2.6.30.x, the 64-byte header is the 48-byte
1027  * header described above, followed by 16 bytes of zeroes; in Linux
1028  * 2.6.31 and later, the 64-byte header is the 64-byte header described
1029  * above.
1030  *
1031  * See linux/Documentation/usb/usbmon.txt and libpcap/pcap/usb.h for details.
1032  *
1033  * With WTAP_ENCAP_USB_LINUX, packets have the 48-byte header; with
1034  * WTAP_ENCAP_USB_LINUX_MMAPPED, they have the 64-byte header.  There
1035  * is no indication of whether the header has the "struct iso_rec", or
1036  * whether the last 16 bytes of a 64-byte header are all zeros or are
1037  * a "struct linux_usb_phdr_ext".
1038  */
1039
1040 /*
1041  * URB transfer_type values
1042  */
1043 #define URB_ISOCHRONOUS   0x0
1044 #define URB_INTERRUPT     0x1
1045 #define URB_CONTROL       0x2
1046 #define URB_BULK          0x3
1047
1048 /*
1049  * Information from the URB for Isochronous transfers.
1050  *
1051  * This structure is 8 bytes long.
1052  */
1053 struct iso_rec {
1054     gint32 error_count;
1055     gint32 numdesc;
1056 };
1057
1058 /*
1059  * Header prepended by Linux kernel to each USB event.
1060  *
1061  * (Setup flag is '-', 'D', 'Z', or 0.  Data flag is '<', '>', 'Z', or 0.)
1062  *
1063  * The values are in *host* byte order.
1064  */
1065 struct linux_usb_phdr {
1066     guint64 id;             /* urb id, to link submission and completion events */
1067     guint8 event_type;      /* Submit ('S'), Completed ('C'), Error ('E') */
1068     guint8 transfer_type;   /* ISO (0), Intr, Control, Bulk (3) */
1069     guint8 endpoint_number; /* Endpoint number (0-15) and transfer direction */
1070     guint8 device_address;  /* 0-127 */
1071     guint16 bus_id;
1072     gint8 setup_flag;       /* 0, if the urb setup header is meaningful */
1073     gint8 data_flag;        /* 0, if urb data is present */
1074     gint64 ts_sec;
1075     gint32 ts_usec;
1076     gint32 status;
1077     guint32 urb_len;        /* whole len of urb this event refers to */
1078     guint32 data_len;       /* amount of urb data really present in this event */
1079
1080     /*
1081      * Packet-type-dependent data.
1082      * USB setup information of setup_flag is true.
1083      * Otherwise, some isochronous transfer information.
1084      */
1085     union {
1086         guint8 data[8];
1087         struct iso_rec iso;
1088     } s;
1089
1090     /*
1091      * This data is provided by Linux 2.6.31 and later kernels.
1092      *
1093      * For WTAP_ENCAP_USB_LINUX, it's not in the pseudo-header, so
1094      * the pseudo-header is always 48 bytes long, including the
1095      * packet-type-dependent data.
1096      *
1097      * For WTAP_ENCAP_USB_LINUX_MMAPPED, the pseudo-header is always
1098      * 64 bytes long, with the packet-type-dependent data preceding
1099      * these last 16 bytes.  In pre-2.6.31 kernels, it's zero padding;
1100      * in 2.6.31 and later, it's the following data.
1101      */
1102     gint32 interval;    /* only for Interrupt and Isochronous events */
1103     gint32 start_frame; /* for Isochronous */
1104     guint32 xfer_flags; /* copy of URB's transfer_flags */
1105     guint32 ndesc;      /* actual number of isochronous descriptors */
1106 };
1107
1108 struct linux_usb_isodesc {
1109     gint32 iso_status;
1110     guint32 iso_off;
1111     guint32 iso_len;
1112     guint32 _pad;
1113 };
1114
1115 /*
1116  * USB setup header as defined in USB specification
1117  * See usb_20.pdf, Chapter 9.3 'USB Device Requests' for details.
1118  * http://www.usb.org/developers/docs/usb_20_122909-2.zip
1119  *
1120  * This structure is 8 bytes long.
1121  */
1122 struct usb_device_setup_hdr {
1123     gint8 bmRequestType;
1124     guint8 bRequest;
1125     guint16 wValue;
1126     guint16 wIndex;
1127     guint16 wLength;
1128 };
1129
1130
1131 /*
1132  * Offset of the *end* of a field within a particular structure.
1133  */
1134 #define END_OFFSETOF(basep, fieldp) \
1135         (((char *)(void *)(fieldp)) - ((char *)(void *)(basep)) + \
1136             sizeof(*fieldp))
1137
1138 static void
1139 pcap_process_linux_usb_pseudoheader(guint packet_size, gboolean byte_swapped,
1140     gboolean header_len_64_bytes, guint8 *pd)
1141 {
1142         struct linux_usb_phdr *phdr;
1143         struct linux_usb_isodesc *pisodesc;
1144         gint32 iso_numdesc, i;
1145
1146         if (byte_swapped) {
1147                 /*
1148                  * Greasy hack, but we never directly direference any of
1149                  * the fields in *phdr, we just get offsets of and
1150                  * addresses of its members, so it's safe.
1151                  */
1152                 phdr = (struct linux_usb_phdr *)(void *)pd;
1153
1154                 if (packet_size < END_OFFSETOF(phdr, &phdr->id))
1155                         return;
1156                 PBSWAP64((guint8 *)&phdr->id);
1157                 if (packet_size < END_OFFSETOF(phdr, &phdr->bus_id))
1158                         return;
1159                 PBSWAP16((guint8 *)&phdr->bus_id);
1160                 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_sec))
1161                         return;
1162                 PBSWAP64((guint8 *)&phdr->ts_sec);
1163                 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_usec))
1164                         return;
1165                 PBSWAP32((guint8 *)&phdr->ts_usec);
1166                 if (packet_size < END_OFFSETOF(phdr, &phdr->status))
1167                         return;
1168                 PBSWAP32((guint8 *)&phdr->status);
1169                 if (packet_size < END_OFFSETOF(phdr, &phdr->urb_len))
1170                         return;
1171                 PBSWAP32((guint8 *)&phdr->urb_len);
1172                 if (packet_size < END_OFFSETOF(phdr, &phdr->data_len))
1173                         return;
1174                 PBSWAP32((guint8 *)&phdr->data_len);
1175
1176                 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1177                         if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.error_count))
1178                                 return;
1179                         PBSWAP32((guint8 *)&phdr->s.iso.error_count);
1180
1181                         if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.numdesc))
1182                                 return;
1183                         PBSWAP32((guint8 *)&phdr->s.iso.numdesc);
1184
1185                 }
1186
1187                 if (header_len_64_bytes) {
1188                         /*
1189                          * This is either the "version 1" header, with
1190                          * 16 bytes of additional fields at the end, or
1191                          * a "version 0" header from a memory-mapped
1192                          * capture, with 16 bytes of zeroed-out padding
1193                          * at the end.  Byte swap them as if this were
1194                          * a "version 1" header.
1195                          *
1196                          * Yes, the first argument to END_OFFSETOF() should
1197                          * be phdr, not phdr_ext; we want the offset of
1198                          * the additional fields from the beginning of
1199                          * the packet.
1200                          */
1201                         if (packet_size < END_OFFSETOF(phdr, &phdr->interval))
1202                                 return;
1203                         PBSWAP32((guint8 *)&phdr->interval);
1204                         if (packet_size < END_OFFSETOF(phdr, &phdr->start_frame))
1205                                 return;
1206                         PBSWAP32((guint8 *)&phdr->start_frame);
1207                         if (packet_size < END_OFFSETOF(phdr, &phdr->xfer_flags))
1208                                 return;
1209                         PBSWAP32((guint8 *)&phdr->xfer_flags);
1210                         if (packet_size < END_OFFSETOF(phdr, &phdr->ndesc))
1211                                 return;
1212                         PBSWAP32((guint8 *)&phdr->ndesc);
1213                 }
1214
1215                 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1216                         /* swap the values in struct linux_usb_isodesc */
1217
1218                         /*
1219                          * See previous "Greasy hack" comment.
1220                          */
1221                         if (header_len_64_bytes) {
1222                                 pisodesc = (struct linux_usb_isodesc*)(void *)(pd + 64);
1223                         } else {
1224                                 pisodesc = (struct linux_usb_isodesc*)(void *)(pd + 48);
1225                         }
1226                         iso_numdesc = phdr->s.iso.numdesc;
1227                         for (i = 0; i < iso_numdesc; i++) {
1228                                 /* always check if we have enough data from the
1229                                  * beginnig of the packet (phdr)
1230                                  */
1231                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_status))
1232                                         return;
1233                                 PBSWAP32((guint8 *)&pisodesc->iso_status);
1234                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_off))
1235                                         return;
1236                                 PBSWAP32((guint8 *)&pisodesc->iso_off);
1237                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_len))
1238                                         return;
1239                                 PBSWAP32((guint8 *)&pisodesc->iso_len);
1240                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->_pad))
1241                                         return;
1242                                 PBSWAP32((guint8 *)&pisodesc->_pad);
1243
1244                                 pisodesc++;
1245                         }
1246                 }
1247         }
1248 }
1249
1250 static gboolean
1251 pcap_read_bt_pseudoheader(FILE_T fh,
1252     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1253 {
1254         int     bytes_read;
1255         struct libpcap_bt_phdr phdr;
1256
1257         errno = WTAP_ERR_CANT_READ;
1258         bytes_read = file_read(&phdr,
1259             sizeof (struct libpcap_bt_phdr), fh);
1260         if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
1261                 *err = file_error(fh, err_info);
1262                 if (*err == 0)
1263                         *err = WTAP_ERR_SHORT_READ;
1264                 return FALSE;
1265         }
1266         pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
1267         return TRUE;
1268 }
1269
1270 static gboolean
1271 pcap_read_llcp_pseudoheader(FILE_T fh,
1272     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1273 {
1274         int bytes_read;
1275         guint8 phdr[LLCP_HEADER_LEN];
1276
1277         errno = WTAP_ERR_CANT_READ;
1278         bytes_read = file_read(phdr, LLCP_HEADER_LEN, fh);
1279         if (bytes_read != LLCP_HEADER_LEN) {
1280                 *err = file_error(fh, err_info);
1281                 if (*err == 0)
1282                         *err = WTAP_ERR_SHORT_READ;
1283                 return FALSE;
1284         }
1285         pseudo_header->llcp.adapter = phdr[LLCP_ADAPTER_OFFSET];
1286         pseudo_header->llcp.flags = phdr[LLCP_FLAGS_OFFSET];
1287         return TRUE;
1288 }
1289
1290 static gboolean
1291 pcap_read_ppp_pseudoheader(FILE_T fh,
1292     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1293 {
1294         int     bytes_read;
1295         struct libpcap_ppp_phdr phdr;
1296
1297         errno = WTAP_ERR_CANT_READ;
1298         bytes_read = file_read(&phdr,
1299             sizeof (struct libpcap_ppp_phdr), fh);
1300         if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
1301                 *err = file_error(fh, err_info);
1302                 if (*err == 0)
1303                         *err = WTAP_ERR_SHORT_READ;
1304                 return FALSE;
1305         }
1306         pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
1307         return TRUE;
1308 }
1309
1310 static gboolean
1311 pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
1312                               union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1313 {
1314   guint8 erf_hdr[sizeof(struct erf_phdr)];
1315   int    bytes_read;
1316
1317   errno = WTAP_ERR_CANT_READ;
1318   bytes_read = file_read(erf_hdr, sizeof(struct erf_phdr), fh);
1319   if (bytes_read != sizeof(struct erf_phdr)) {
1320     *err = file_error(fh, err_info);
1321     if (*err == 0)
1322       *err = WTAP_ERR_SHORT_READ;
1323     return FALSE;
1324   }
1325   pseudo_header->erf.phdr.ts = pletohll(&erf_hdr[0]); /* timestamp */
1326   pseudo_header->erf.phdr.type =  erf_hdr[8];
1327   pseudo_header->erf.phdr.flags = erf_hdr[9];
1328   pseudo_header->erf.phdr.rlen = pntohs(&erf_hdr[10]);
1329   pseudo_header->erf.phdr.lctr = pntohs(&erf_hdr[12]);
1330   pseudo_header->erf.phdr.wlen = pntohs(&erf_hdr[14]);
1331
1332   /* The high 32 bits of the timestamp contain the integer number of seconds
1333    * while the lower 32 bits contain the binary fraction of the second.
1334    * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1335   if (whdr) {
1336     guint64 ts = pseudo_header->erf.phdr.ts;
1337     whdr->ts.secs = (guint32) (ts >> 32);
1338     ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1339     ts += (ts & 0x80000000) << 1; /* rounding */
1340     whdr->ts.nsecs = ((guint32) (ts >> 32));
1341     if ( whdr->ts.nsecs >= 1000000000) {
1342       whdr->ts.nsecs -= 1000000000;
1343       whdr->ts.secs += 1;
1344     }
1345   }
1346   return TRUE;
1347 }
1348
1349 /*
1350  * If the type of record given in the pseudo header indicate the presence of an extension
1351  * header then, read all the extension headers
1352  */
1353 static gboolean
1354 pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1355                            int *err, gchar **err_info, guint * psize)
1356 {
1357   int bytes_read = 0;
1358   guint8 erf_exhdr[8];
1359   guint64 erf_exhdr_sw;
1360   int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1361   guint8 type;
1362   *psize = 0;
1363   if (pseudo_header->erf.phdr.type & 0x80){
1364     do{
1365       errno = WTAP_ERR_CANT_READ;
1366       bytes_read = file_read(erf_exhdr, 8, fh);
1367       if (bytes_read != 8 ) {
1368         *err = file_error(fh, err_info);
1369         if (*err == 0)
1370           *err = WTAP_ERR_SHORT_READ;
1371         return FALSE;
1372       }
1373       type = erf_exhdr[0];
1374       erf_exhdr_sw = pntohll(erf_exhdr);
1375       if (i < max)
1376         memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1377       *psize += 8;
1378       i++;
1379     } while (type & 0x80);
1380   }
1381   return TRUE;
1382 }
1383
1384 /*
1385  * If the type of record given in the pseudo header indicate the precense of a subheader
1386  * then, read this optional subheader
1387  */
1388 static gboolean
1389 pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1390                            int *err, gchar **err_info, guint * psize)
1391 {
1392   guint8 erf_subhdr[sizeof(union erf_subhdr)];
1393   int    bytes_read;
1394
1395   *psize=0;
1396   switch(pseudo_header->erf.phdr.type & 0x7F) {
1397   case ERF_TYPE_MC_HDLC:
1398   case ERF_TYPE_MC_RAW:
1399   case ERF_TYPE_MC_ATM:
1400   case ERF_TYPE_MC_RAW_CHANNEL:
1401   case ERF_TYPE_MC_AAL5:
1402   case ERF_TYPE_MC_AAL2:
1403   case ERF_TYPE_COLOR_MC_HDLC_POS:
1404     /* Extract the Multi Channel header to include it in the pseudo header part */
1405     errno = WTAP_ERR_CANT_READ;
1406     bytes_read = file_read(erf_subhdr, sizeof(erf_mc_header_t), fh);
1407     if (bytes_read != sizeof(erf_mc_header_t) ) {
1408       *err = file_error(fh, err_info);
1409       if (*err == 0)
1410         *err = WTAP_ERR_SHORT_READ;
1411       return FALSE;
1412     }
1413     pseudo_header->erf.subhdr.mc_hdr = pntohl(&erf_subhdr[0]);
1414     *psize = sizeof(erf_mc_header_t);
1415     break;
1416   case ERF_TYPE_ETH:
1417   case ERF_TYPE_COLOR_ETH:
1418   case ERF_TYPE_DSM_COLOR_ETH:
1419     /* Extract the Ethernet additional header to include it in the pseudo header part */
1420     errno = WTAP_ERR_CANT_READ;
1421     bytes_read = file_read(erf_subhdr, sizeof(erf_eth_header_t), fh);
1422     if (bytes_read != sizeof(erf_eth_header_t) ) {
1423       *err = file_error(fh, err_info);
1424       if (*err == 0)
1425         *err = WTAP_ERR_SHORT_READ;
1426       return FALSE;
1427     }
1428     pseudo_header->erf.subhdr.eth_hdr = pntohs(&erf_subhdr[0]);
1429     *psize = sizeof(erf_eth_header_t);
1430     break;
1431   default:
1432     /* No optional pseudo header for this ERF type */
1433     break;
1434   }
1435   return TRUE;
1436 }
1437
1438 static gboolean
1439 pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1440 {
1441         struct i2c_file_hdr i2c_hdr;
1442         int    bytes_read;
1443
1444         errno = WTAP_ERR_CANT_READ;
1445         bytes_read = file_read(&i2c_hdr, sizeof (i2c_hdr), fh);
1446         if (bytes_read != sizeof (i2c_hdr)) {
1447                 *err = file_error(fh, err_info);
1448                 if (*err == 0)
1449                         *err = WTAP_ERR_SHORT_READ;
1450                 return FALSE;
1451         }
1452
1453         pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
1454         pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
1455         pseudo_header->i2c.flags = pntohl(&i2c_hdr.flags);
1456
1457         return TRUE;
1458 }
1459
1460 int
1461 pcap_process_pseudo_header(FILE_T fh, int file_type, int wtap_encap,
1462     guint packet_size, gboolean check_packet_size, struct wtap_pkthdr *phdr,
1463     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1464 {
1465         int phdr_len = 0;
1466         guint size;
1467
1468         switch (wtap_encap) {
1469
1470         case WTAP_ENCAP_ATM_PDUS:
1471                 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1472                         /*
1473                          * Nokia IPSO ATM.
1474                          */
1475                         if (check_packet_size && packet_size < NOKIAATM_LEN) {
1476                                 /*
1477                                  * Uh-oh, the packet isn't big enough to even
1478                                  * have a pseudo-header.
1479                                  */
1480                                 *err = WTAP_ERR_BAD_FILE;
1481                                 *err_info = g_strdup_printf("pcap: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1482                                     packet_size);
1483                                 return -1;
1484                         }
1485                         if (!pcap_read_nokiaatm_pseudoheader(fh,
1486                             pseudo_header, err, err_info))
1487                                 return -1;      /* Read error */
1488
1489                         phdr_len = NOKIAATM_LEN;
1490                 } else {
1491                         /*
1492                          * SunATM.
1493                          */
1494                         if (check_packet_size && packet_size < SUNATM_LEN) {
1495                                 /*
1496                                  * Uh-oh, the packet isn't big enough to even
1497                                  * have a pseudo-header.
1498                                  */
1499                                 *err = WTAP_ERR_BAD_FILE;
1500                                 *err_info = g_strdup_printf("pcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1501                                     packet_size);
1502                                 return -1;
1503                         }
1504                         if (!pcap_read_sunatm_pseudoheader(fh,
1505                             pseudo_header, err, err_info))
1506                                 return -1;      /* Read error */
1507
1508                         phdr_len = SUNATM_LEN;
1509                 }
1510                 break;
1511
1512         case WTAP_ENCAP_ETHERNET:
1513                 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1514                         /*
1515                          * Nokia IPSO.  Psuedo header has already been read, but its not considered
1516                          * part of the packet size, so reread it to store the data for later (when saving)
1517                          */
1518                         if (!pcap_read_nokia_pseudoheader(fh, pseudo_header, err, err_info))
1519                                 return -1;      /* Read error */
1520                 }
1521
1522                 /*
1523                  * We don't know whether there's an FCS in this frame or not.
1524                  */
1525                 pseudo_header->eth.fcs_len = -1;
1526                 break;
1527
1528         case WTAP_ENCAP_IEEE_802_11:
1529         case WTAP_ENCAP_IEEE_802_11_PRISM:
1530         case WTAP_ENCAP_IEEE_802_11_RADIOTAP:
1531         case WTAP_ENCAP_IEEE_802_11_AVS:
1532                 /*
1533                  * We don't know whether there's an FCS in this frame or not.
1534                  * XXX - are there any OSes where the capture mechanism
1535                  * supplies an FCS?
1536                  */
1537                 pseudo_header->ieee_802_11.fcs_len = -1;
1538                 pseudo_header->ieee_802_11.decrypted = FALSE;
1539                 pseudo_header->ieee_802_11.channel = 0;
1540                 pseudo_header->ieee_802_11.data_rate = 0;
1541                 pseudo_header->ieee_802_11.signal_level = 0;
1542                 break;
1543
1544         case WTAP_ENCAP_IRDA:
1545                 if (check_packet_size && packet_size < IRDA_SLL_LEN) {
1546                         /*
1547                          * Uh-oh, the packet isn't big enough to even
1548                          * have a pseudo-header.
1549                          */
1550                         *err = WTAP_ERR_BAD_FILE;
1551                         *err_info = g_strdup_printf("pcap: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1552                             packet_size);
1553                         return -1;
1554                 }
1555                 if (!pcap_read_irda_pseudoheader(fh, pseudo_header,
1556                     err, err_info))
1557                         return -1;      /* Read error */
1558
1559                 phdr_len = IRDA_SLL_LEN;
1560                 break;
1561
1562         case WTAP_ENCAP_MTP2_WITH_PHDR:
1563                 if (check_packet_size && packet_size < MTP2_HDR_LEN) {
1564                         /*
1565                          * Uh-oh, the packet isn't big enough to even
1566                          * have a pseudo-header.
1567                          */
1568                         *err = WTAP_ERR_BAD_FILE;
1569                         *err_info = g_strdup_printf("pcap: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1570                             packet_size);
1571                         return -1;
1572                 }
1573                 if (!pcap_read_mtp2_pseudoheader(fh, pseudo_header,
1574                     err, err_info))
1575                         return -1;      /* Read error */
1576
1577                 phdr_len = MTP2_HDR_LEN;
1578                 break;
1579
1580         case WTAP_ENCAP_LINUX_LAPD:
1581                 if (check_packet_size && packet_size < LAPD_SLL_LEN) {
1582                         /*
1583                          * Uh-oh, the packet isn't big enough to even
1584                          * have a pseudo-header.
1585                          */
1586                         *err = WTAP_ERR_BAD_FILE;
1587                         *err_info = g_strdup_printf("pcap: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1588                             packet_size);
1589                         return -1;
1590                 }
1591                 if (!pcap_read_lapd_pseudoheader(fh, pseudo_header,
1592                     err, err_info))
1593                         return -1;      /* Read error */
1594
1595                 phdr_len = LAPD_SLL_LEN;
1596                 break;
1597
1598         case WTAP_ENCAP_SITA:
1599                 if (check_packet_size && packet_size < SITA_HDR_LEN) {
1600                         /*
1601                          * Uh-oh, the packet isn't big enough to even
1602                          * have a pseudo-header.
1603                          */
1604                         *err = WTAP_ERR_BAD_FILE;
1605                         *err_info = g_strdup_printf("pcap: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1606                             packet_size);
1607                         return -1;
1608                 }
1609                 if (!pcap_read_sita_pseudoheader(fh, pseudo_header,
1610                     err, err_info))
1611                         return -1;      /* Read error */
1612
1613                 phdr_len = SITA_HDR_LEN;
1614                 break;
1615
1616         case WTAP_ENCAP_BLUETOOTH_H4:
1617                 /* We don't have pseudoheader, so just pretend we received everything. */
1618                 pseudo_header->p2p.sent = FALSE;
1619                 break;
1620
1621         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1622                 if (check_packet_size &&
1623                     packet_size < sizeof (struct libpcap_bt_phdr)) {
1624                         /*
1625                          * Uh-oh, the packet isn't big enough to even
1626                          * have a pseudo-header.
1627                          */
1628                         *err = WTAP_ERR_BAD_FILE;
1629                         *err_info = g_strdup_printf("pcap: libpcap bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1630                             packet_size);
1631                         return -1;
1632                 }
1633                 if (!pcap_read_bt_pseudoheader(fh,
1634                     pseudo_header, err, err_info))
1635                         return -1;      /* Read error */
1636
1637                 phdr_len = (int)sizeof (struct libpcap_bt_phdr);
1638                 break;
1639
1640         case WTAP_ENCAP_NFC_LLCP:
1641                 if (check_packet_size && packet_size < LLCP_HEADER_LEN) {
1642                         *err = WTAP_ERR_BAD_FILE;
1643                         *err_info = g_strdup_printf("pcap: libpcap llcp file too short");
1644                         return -1;
1645                 }
1646                 if (!pcap_read_llcp_pseudoheader(fh, pseudo_header, err, err_info))
1647                         return -1;      /* Read error */
1648                 phdr_len = LLCP_HEADER_LEN;
1649                 break;
1650
1651         case WTAP_ENCAP_PPP_WITH_PHDR:
1652                 if (check_packet_size &&
1653                     packet_size < sizeof (struct libpcap_ppp_phdr)) {
1654                         /*
1655                          * Uh-oh, the packet isn't big enough to even
1656                          * have a pseudo-header.
1657                          */
1658                         *err = WTAP_ERR_BAD_FILE;
1659                         *err_info = g_strdup_printf("pcap: libpcap ppp file has a %u-byte packet, too small to have even a pseudo-header",
1660                             packet_size);
1661                         return -1;
1662                 }
1663                 if (!pcap_read_ppp_pseudoheader(fh,
1664                     pseudo_header, err, err_info))
1665                         return -1;      /* Read error */
1666
1667                 phdr_len = (int)sizeof (struct libpcap_ppp_phdr);
1668                 break;
1669
1670         case WTAP_ENCAP_ERF:
1671                 if (check_packet_size &&
1672                     packet_size < sizeof(struct erf_phdr) ) {
1673                         /*
1674                          * Uh-oh, the packet isn't big enough to even
1675                          * have a pseudo-header.
1676                          */
1677                         *err = WTAP_ERR_BAD_FILE;
1678                         *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1679                             packet_size);
1680                         return -1;
1681                 }
1682
1683                 if (!pcap_read_erf_pseudoheader(fh, phdr, pseudo_header,
1684                     err, err_info))
1685                         return -1;      /* Read error */
1686
1687                 phdr_len = (int)sizeof(struct erf_phdr);
1688
1689                 /* check the optional Extension header */
1690                 if (!pcap_read_erf_exheader(fh, pseudo_header, err, err_info,
1691                     &size))
1692                         return -1;      /* Read error */
1693
1694                 phdr_len += size;
1695
1696                 /* check the optional Multi Channel header */
1697                 if (!pcap_read_erf_subheader(fh, pseudo_header, err, err_info,
1698                     &size))
1699                         return -1;      /* Read error */
1700
1701                 phdr_len += size;
1702
1703                 if (check_packet_size &&
1704                     packet_size < (guint)phdr_len) {
1705                         /*
1706                          * Uh-oh, the packet isn't big enough for the pseudo-
1707                          * header.
1708                          */
1709                         *err = WTAP_ERR_BAD_FILE;
1710                         *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small for a pseudo-header with ex- and sub-headers (%d)",
1711                             packet_size, phdr_len);
1712                         return -1;
1713                 }
1714                 break;
1715
1716         case WTAP_ENCAP_I2C:
1717                 if (check_packet_size &&
1718                     packet_size < sizeof (struct i2c_file_hdr)) {
1719                         /*
1720                          * Uh-oh, the packet isn't big enough to even
1721                          * have a pseudo-header.
1722                          */
1723                         *err = WTAP_ERR_BAD_FILE;
1724                         *err_info = g_strdup_printf("pcap: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1725                             packet_size);
1726                         return -1;
1727                 }
1728                 if (!pcap_read_i2c_pseudoheader(fh, pseudo_header,
1729                     err, err_info))
1730                         return -1;      /* Read error */
1731
1732                 /*
1733                  * Don't count the pseudo-header as part of the packet.
1734                  */
1735                 phdr_len = (int)sizeof (struct i2c_file_hdr);
1736                 break;
1737         }
1738
1739         return phdr_len;
1740 }
1741
1742 void
1743 pcap_read_post_process(int file_type, int wtap_encap,
1744     union wtap_pseudo_header *pseudo_header,
1745     guint8 *pd, guint packet_size, gboolean bytes_swapped, int fcs_len)
1746 {
1747         switch (wtap_encap) {
1748
1749         case WTAP_ENCAP_ATM_PDUS:
1750                 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1751                         /*
1752                          * Nokia IPSO ATM.
1753                          *
1754                          * Guess the traffic type based on the packet
1755                          * contents.
1756                          */
1757                         atm_guess_traffic_type(pd, packet_size, pseudo_header);
1758                 } else {
1759                         /*
1760                          * SunATM.
1761                          *
1762                          * If this is ATM LANE traffic, try to guess what
1763                          * type of LANE traffic it is based on the packet
1764                          * contents.
1765                          */
1766                         if (pseudo_header->atm.type == TRAF_LANE)
1767                                 atm_guess_lane_type(pd, packet_size,
1768                                     pseudo_header);
1769                 }
1770                 break;
1771
1772         case WTAP_ENCAP_ETHERNET:
1773                 pseudo_header->eth.fcs_len = fcs_len;
1774                 break;
1775
1776         case WTAP_ENCAP_USB_LINUX:
1777                 pcap_process_linux_usb_pseudoheader(packet_size,
1778                     bytes_swapped, FALSE, pd);
1779                 break;
1780
1781         case WTAP_ENCAP_USB_LINUX_MMAPPED:
1782                 pcap_process_linux_usb_pseudoheader(packet_size,
1783                     bytes_swapped, TRUE, pd);
1784                 break;
1785
1786         case WTAP_ENCAP_NETANALYZER:
1787                 /*
1788                  * Not strictly necessary, as the netANALYZER
1789                  * dissector calls the "Ethernet with FCS"
1790                  * dissector, but we might as well set it.
1791                  */
1792                 pseudo_header->eth.fcs_len = 4;
1793                 break;
1794
1795         default:
1796                 break;
1797         }
1798 }
1799
1800 int
1801 pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
1802 {
1803         int hdrsize;
1804
1805         switch (encap) {
1806
1807         case WTAP_ENCAP_ATM_PDUS:
1808                 hdrsize = SUNATM_LEN;
1809                 break;
1810
1811         case WTAP_ENCAP_IRDA:
1812                 hdrsize = IRDA_SLL_LEN;
1813                 break;
1814
1815         case WTAP_ENCAP_MTP2_WITH_PHDR:
1816                 hdrsize = MTP2_HDR_LEN;
1817                 break;
1818
1819         case WTAP_ENCAP_LINUX_LAPD:
1820                 hdrsize = LAPD_SLL_LEN;
1821                 break;
1822
1823         case WTAP_ENCAP_SITA:
1824                 hdrsize = SITA_HDR_LEN;
1825                 break;
1826
1827         case WTAP_ENCAP_ERF:
1828                 hdrsize = (int)sizeof (struct erf_phdr);
1829                 switch (pseudo_header->erf.phdr.type & 0x7F) {
1830
1831                 case ERF_TYPE_MC_HDLC:
1832                 case ERF_TYPE_MC_RAW:
1833                 case ERF_TYPE_MC_ATM:
1834                 case ERF_TYPE_MC_RAW_CHANNEL:
1835                 case ERF_TYPE_MC_AAL5:
1836                 case ERF_TYPE_MC_AAL2:
1837                 case ERF_TYPE_COLOR_MC_HDLC_POS:
1838                         hdrsize += (int)sizeof(struct erf_mc_hdr);
1839                         break;
1840
1841                 case ERF_TYPE_ETH:
1842                 case ERF_TYPE_COLOR_ETH:
1843                 case ERF_TYPE_DSM_COLOR_ETH:
1844                         hdrsize += (int)sizeof(struct erf_eth_hdr);
1845                         break;
1846
1847                 default:
1848                         break;
1849                 }
1850
1851                 /*
1852                  * Add in the lengths of the extension headers.
1853                  */
1854                 if (pseudo_header->erf.phdr.type & 0x80) {
1855                         int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1856                         guint8 erf_exhdr[8];
1857                         guint8 type;
1858
1859                         do {
1860                                 phtonll(erf_exhdr, pseudo_header->erf.ehdr_list[i].ehdr);
1861                                 type = erf_exhdr[0];
1862                                 hdrsize += 8;
1863                                 i++;
1864                         } while (type & 0x80 && i < max);
1865                 }
1866                 break;
1867
1868         case WTAP_ENCAP_I2C:
1869                 hdrsize = (int)sizeof (struct i2c_file_hdr);
1870                 break;
1871
1872         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1873                 hdrsize = (int)sizeof (struct libpcap_bt_phdr);
1874                 break;
1875
1876         case WTAP_ENCAP_PPP_WITH_PHDR:
1877                 hdrsize = (int)sizeof (struct libpcap_ppp_phdr);
1878                 break;
1879
1880         default:
1881                 hdrsize = 0;
1882                 break;
1883         }
1884
1885         return hdrsize;
1886 }
1887
1888 gboolean
1889 pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
1890     int *err)
1891 {
1892         guint8 atm_hdr[SUNATM_LEN];
1893         guint8 irda_hdr[IRDA_SLL_LEN];
1894         guint8 lapd_hdr[LAPD_SLL_LEN];
1895         guint8 mtp2_hdr[MTP2_HDR_LEN];
1896         guint8 sita_hdr[SITA_HDR_LEN];
1897         guint8 erf_hdr[ sizeof(struct erf_mc_phdr)];
1898         struct i2c_file_hdr i2c_hdr;
1899         struct libpcap_bt_phdr bt_hdr;
1900         struct libpcap_ppp_phdr ppp_hdr;
1901         size_t size;
1902
1903         switch (encap) {
1904
1905         case WTAP_ENCAP_ATM_PDUS:
1906                 /*
1907                  * Write the ATM header.
1908                  */
1909                 atm_hdr[SUNATM_FLAGS] =
1910                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1911                 switch (pseudo_header->atm.aal) {
1912
1913                 case AAL_SIGNALLING:
1914                         /* Q.2931 */
1915                         atm_hdr[SUNATM_FLAGS] |= 0x06;
1916                         break;
1917
1918                 case AAL_5:
1919                         switch (pseudo_header->atm.type) {
1920
1921                         case TRAF_LANE:
1922                                 /* LANE */
1923                                 atm_hdr[SUNATM_FLAGS] |= 0x01;
1924                                 break;
1925
1926                         case TRAF_LLCMX:
1927                                 /* RFC 1483 LLC multiplexed traffic */
1928                                 atm_hdr[SUNATM_FLAGS] |= 0x02;
1929                                 break;
1930
1931                         case TRAF_ILMI:
1932                                 /* ILMI */
1933                                 atm_hdr[SUNATM_FLAGS] |= 0x05;
1934                                 break;
1935                         }
1936                         break;
1937                 }
1938                 atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
1939                 phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1940                 if (!wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr), err))
1941                         return FALSE;
1942                 wdh->bytes_dumped += sizeof(atm_hdr);
1943                 break;
1944
1945         case WTAP_ENCAP_IRDA:
1946                 /*
1947                  * Write the IrDA header.
1948                  */
1949                 memset(irda_hdr, 0, sizeof(irda_hdr));
1950                 phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
1951                     pseudo_header->irda.pkttype);
1952                 phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
1953                 if (!wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr), err))
1954                         return FALSE;
1955                 wdh->bytes_dumped += sizeof(irda_hdr);
1956                 break;
1957
1958         case WTAP_ENCAP_MTP2_WITH_PHDR:
1959                 /*
1960                  * Write the MTP2 header.
1961                  */
1962                 memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
1963                 mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
1964                 mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
1965                 phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
1966                     pseudo_header->mtp2.link_number);
1967                 if (!wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr), err))
1968                         return FALSE;
1969                 wdh->bytes_dumped += sizeof(mtp2_hdr);
1970                 break;
1971
1972         case WTAP_ENCAP_LINUX_LAPD:
1973                 /*
1974                  * Write the LAPD header.
1975                  */
1976                 memset(&lapd_hdr, 0, sizeof(lapd_hdr));
1977                 phtons(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET],
1978                     pseudo_header->lapd.pkttype);
1979                 phtons(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
1980                 lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
1981                     pseudo_header->lapd.we_network?0x01:0x00;
1982                 if (!wtap_dump_file_write(wdh, lapd_hdr, sizeof(lapd_hdr), err))
1983                         return FALSE;
1984                 wdh->bytes_dumped += sizeof(lapd_hdr);
1985                 break;
1986
1987         case WTAP_ENCAP_SITA:
1988                 /*
1989                  * Write the SITA header.
1990                  */
1991                 memset(&sita_hdr, 0, sizeof(sita_hdr));
1992                 sita_hdr[SITA_FLAGS_OFFSET]   = pseudo_header->sita.sita_flags;
1993                 sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.sita_signals;
1994                 sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.sita_errors1;
1995                 sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.sita_errors2;
1996                 sita_hdr[SITA_PROTO_OFFSET]   = pseudo_header->sita.sita_proto;
1997                 if (!wtap_dump_file_write(wdh, sita_hdr, sizeof(sita_hdr), err))
1998                         return FALSE;
1999                 wdh->bytes_dumped += sizeof(sita_hdr);
2000                 break;
2001
2002         case WTAP_ENCAP_ERF:
2003                 /*
2004                  * Write the ERF header.
2005                  */
2006                 memset(&erf_hdr, 0, sizeof(erf_hdr));
2007                 phtolell(&erf_hdr[0], pseudo_header->erf.phdr.ts);
2008                 erf_hdr[8] = pseudo_header->erf.phdr.type;
2009                 erf_hdr[9] = pseudo_header->erf.phdr.flags;
2010                 phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
2011                 phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
2012                 phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
2013                 size = sizeof(struct erf_phdr);
2014
2015                 switch(pseudo_header->erf.phdr.type & 0x7F) {
2016                 case ERF_TYPE_MC_HDLC:
2017                 case ERF_TYPE_MC_RAW:
2018                 case ERF_TYPE_MC_ATM:
2019                 case ERF_TYPE_MC_RAW_CHANNEL:
2020                 case ERF_TYPE_MC_AAL5:
2021                 case ERF_TYPE_MC_AAL2:
2022                 case ERF_TYPE_COLOR_MC_HDLC_POS:
2023                   phtonl(&erf_hdr[16], pseudo_header->erf.subhdr.mc_hdr);
2024                   size += (int)sizeof(struct erf_mc_hdr);
2025                   break;
2026                 case ERF_TYPE_ETH:
2027                 case ERF_TYPE_COLOR_ETH:
2028                 case ERF_TYPE_DSM_COLOR_ETH:
2029                   phtons(&erf_hdr[16], pseudo_header->erf.subhdr.eth_hdr);
2030                   size += (int)sizeof(struct erf_eth_hdr);
2031                   break;
2032                 default:
2033                   break;
2034                 }
2035                 if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
2036                         return FALSE;
2037                 wdh->bytes_dumped += size;
2038
2039                 /*
2040                  * Now write out the extension headers.
2041                  */
2042                 if (pseudo_header->erf.phdr.type & 0x80) {
2043                         int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
2044                         guint8 erf_exhdr[8];
2045                         guint8 type;
2046
2047                         do {
2048                                 phtonll(erf_exhdr, pseudo_header->erf.ehdr_list[i].ehdr);
2049                                 type = erf_exhdr[0];
2050                                 if (!wtap_dump_file_write(wdh, erf_exhdr, 8, err))
2051                                         return FALSE;
2052                                 wdh->bytes_dumped += 8;
2053                                 i++;
2054                         } while (type & 0x80 && i < max);
2055                 }
2056                 break;
2057
2058         case WTAP_ENCAP_I2C:
2059                 /*
2060                  * Write the I2C header.
2061                  */
2062                 memset(&i2c_hdr, 0, sizeof(i2c_hdr));
2063                 i2c_hdr.bus = pseudo_header->i2c.bus |
2064                         (pseudo_header->i2c.is_event ? 0x80 : 0x00);
2065                 phtonl((guint8 *)&i2c_hdr.flags, pseudo_header->i2c.flags);
2066                 if (!wtap_dump_file_write(wdh, &i2c_hdr, sizeof(i2c_hdr), err))
2067                         return FALSE;
2068                 wdh->bytes_dumped += sizeof(i2c_hdr);
2069                 break;
2070
2071         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
2072                 bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
2073                 if (!wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr, err))
2074                         return FALSE;
2075                 wdh->bytes_dumped += sizeof bt_hdr;
2076                 break;
2077
2078         case WTAP_ENCAP_PPP_WITH_PHDR:
2079                 ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
2080                 if (!wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr, err))
2081                         return FALSE;
2082                 wdh->bytes_dumped += sizeof ppp_hdr;
2083                 break;
2084         }
2085         return TRUE;
2086 }