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