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