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