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