From Ivan Sy (with minor modifications):
[obnox/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
355         /*
356          * To repeat:
357          *
358          * If you need a new encapsulation type for libpcap files, do
359          * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
360          * add a new encapsulation type by changing an existing entry;
361          * leave the existing entries alone.
362          *
363          * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
364          * for a new DLT_ value, and specifying the purpose of the new value.
365          * When you get the new DLT_ value, use that numerical value in
366          * the "dlt_value" field of "pcap_to_wtap_map[]".
367          */
368
369         /*
370          * The following are entries for libpcap type values that have
371          * different meanings on different OSes.
372          *
373          * We put these *after* the entries for the platform-independent
374          * libpcap type values for those Wiretap encapsulation types, so
375          * that Wireshark chooses the platform-independent libpcap type
376          * value for those encapsulatioin types, not the platform-dependent
377          * one.
378          */
379
380         /*
381          * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
382          * seen that define anything other than DLT_ATM_RFC1483 as 11 are
383          * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
384          * which define it as 100, mapping the kernel's value to 100, in
385          * an attempt to hide the different values used on different
386          * platforms.
387          *
388          * If this is a platform where DLT_FR is defined as 11, we
389          * don't handle 11 at all; otherwise, we handle it as
390          * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
391          * captures from BSD/OS if running on platforms other than BSD/OS,
392          * but
393          *
394          *      1) we don't yet support DLT_FR
395          *
396          * and
397          *
398          *      2) nothing short of a heuristic would let us interpret
399          *         them correctly).
400          */
401 #if defined(DLT_FR) && (DLT_FR == 11)
402         { 11,           WTAP_ENCAP_FRELAY },
403 #else
404         { 11,           WTAP_ENCAP_ATM_RFC1483 },
405 #endif
406
407         /*
408          * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
409          * BSD/OS, and DLT_LOOP on OpenBSD.
410          *
411          * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
412          * (it's just like DLT_NULL, only with the AF_ value in network
413          * rather than host byte order - Wireshark figures out the
414          * byte order from the data, so we don't care what byte order
415          * it's in), so if DLT_LOOP is defined as 12, interpret 12
416          * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
417          * as 12, interpret it as WTAP_ENCAP_RAW_IP.
418          */
419 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
420         { 12,           WTAP_ENCAP_NULL },
421 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
422         /*
423          * Put entry for Cisco HDLC here.
424          * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
425          * start with a 4-byte Cisco HDLC header?
426          */
427 #else
428         { 12,           WTAP_ENCAP_RAW_IP },
429 #endif
430
431         /*
432          * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
433          * don't actually generate it.  I infer that BSD/OS translates
434          * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
435          * libpcap, as the BSD/OS link-layer header is different;
436          * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
437          *
438          * From this, I infer that there's no point in handling 13
439          * as DLT_SLIP_BSDOS.
440          *
441          * 13 is DLT_ATM_RFC1483 on BSD/OS.
442          *
443          * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
444          * of decrypted IPSEC traffic.
445          */
446 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
447         { 13,           WTAP_ENCAP_ATM_RFC1483 },
448 #elif defined(DLT_ENC) && (DLT_ENC == 13)
449         { 13,           WTAP_ENCAP_ENC },
450 #endif
451
452         /*
453          * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
454          * don't actually generate it.  I infer that BSD/OS translates
455          * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
456          * libpcap, as the BSD/OS link-layer header is different;
457          * however, in BSD/OS, DLT_PPP_BSDOS is 16.
458          *
459          * From this, I infer that there's no point in handling 14
460          * as DLT_PPP_BSDOS.
461          *
462          * 14 is DLT_RAW on BSD/OS and OpenBSD.
463          */
464         { 14,           WTAP_ENCAP_RAW_IP },
465
466         /*
467          * 15 is:
468          *
469          *      DLT_SLIP_BSDOS on BSD/OS;
470          *
471          *      DLT_HIPPI on NetBSD;
472          *
473          *      DLT_LANE8023 with Alexey Kuznetzov's patches for
474          *      Linux libpcap;
475          *
476          *      DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
477          *      (and on SuSE 6.3);
478          *
479          * but we don't currently handle any of those.
480          */
481
482         /*
483          * 16 is:
484          *
485          *      DLT_PPP_BSDOS on BSD/OS;
486          *
487          *      DLT_HDLC on NetBSD (Cisco HDLC);
488          *
489          *      DLT_CIP with Alexey Kuznetzov's patches for
490          *      Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
491          *
492          *      DLT_I4L_IP with the ISDN4Linux patches for libpcap
493          *      (and on SuSE 6.3).
494          */
495 #if defined(DLT_CIP) && (DLT_CIP == 16)
496         { 16,           WTAP_ENCAP_LINUX_ATM_CLIP },
497 #endif
498 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
499         { 16,           WTAP_ENCAP_CHDLC },
500 #endif
501
502         /*
503          * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
504          * handle it.
505          * It is also used as the PF (Packet Filter) logging format beginning
506          * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
507          * defined with the value 17.
508          */
509 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
510         { 17,           WTAP_ENCAP_OLD_PFLOG },
511 #endif
512
513         /*
514          * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
515          * DLT_CIP of 16 that the Alexey Kuznetzov patches for
516          * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
517          * I've not found any libpcap that uses it for any other purpose -
518          * hopefully nobody will do so in the future.
519          */
520         { 18,           WTAP_ENCAP_LINUX_ATM_CLIP },
521
522         /*
523          * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
524          * recent versions I've seen of the Linux ATM distribution;
525          * I've not yet found any libpcap that uses it for any other
526          * purpose - hopefully nobody will do so in the future.
527          */
528         { 19,           WTAP_ENCAP_LINUX_ATM_CLIP },
529
530         /*
531          * To repeat:
532          *
533          * If you need a new encapsulation type for libpcap files, do
534          * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
535          * add a new encapsulation type by changing an existing entry;
536          * leave the existing entries alone.
537          *
538          * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
539          * for a new DLT_ value, and specifying the purpose of the new value.
540          * When you get the new DLT_ value, use that numerical value in
541          * the "dlt_value" field of "pcap_to_wtap_map[]".
542          */
543 };
544 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
545
546 int
547 wtap_pcap_encap_to_wtap_encap(int encap)
548 {
549         unsigned int i;
550
551         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
552                 if (pcap_to_wtap_map[i].dlt_value == encap)
553                         return pcap_to_wtap_map[i].wtap_encap_value;
554         }
555         return WTAP_ENCAP_UNKNOWN;
556 }
557
558 int
559 wtap_wtap_encap_to_pcap_encap(int encap)
560 {
561         unsigned int i;
562
563         switch (encap) {
564
565         case WTAP_ENCAP_FDDI:
566         case WTAP_ENCAP_FDDI_BITSWAPPED:
567         case WTAP_ENCAP_NETTL_FDDI:
568                 /*
569                  * Special-case WTAP_ENCAP_FDDI and
570                  * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
571                  * to DLT_FDDI (even though that may mean that the bit
572                  * order in the FDDI MAC addresses is wrong; so it goes
573                  * - libpcap format doesn't record the byte order,
574                  * so that's not fixable).
575                  */
576                 return 10;      /* that's DLT_FDDI */
577
578         case WTAP_ENCAP_FRELAY_WITH_PHDR:
579                 /*
580                  * Do the same with Frame Relay.
581                  */
582                 return 107;
583
584         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
585                 /*
586                  * Map this to DLT_IEEE802_11, for now, even though
587                  * that means the radio information will be lost.
588                  * Once tcpdump support for the BSD radiotap header
589                  * is sufficiently widespread, we should probably
590                  * use that, instead - although we should probably
591                  * ultimately just have WTAP_ENCAP_IEEE_802_11
592                  * as the only Wiretap encapsulation for 802.11,
593                  * and have the pseudo-header include a radiotap-style
594                  * list of attributes.  If we do that, though, we
595                  * should probably bypass the regular Wiretap code
596                  * when writing out packets during a capture, and just
597                  * do the equivalent of a libpcap write (unfortunately,
598                  * libpcap doesn't have an "open dump by file descriptor"
599                  * function, so we can't just use "pcap_dump()"), so
600                  * that we don't spend cycles mapping from libpcap to
601                  * Wiretap and then back to libpcap.  (There are other
602                  * reasons to do that, e.g. to handle AIX libpcap better.)
603                  */
604                 return 105;
605         }
606
607         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
608                 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
609                         return pcap_to_wtap_map[i].dlt_value;
610         }
611         return -1;
612 }
613
614 /*
615  * Various pseudo-headers that appear at the beginning of packet data.
616  *
617  * We represent them as sets of offsets, as they might not be aligned on
618  * an appropriate structure boundary in the buffer, and as that makes them
619  * independent of the way the compiler might align fields.
620  */
621
622 /*
623  * The link-layer header on SunATM packets.
624  */
625 #define SUNATM_FLAGS    0       /* destination and traffic type - 1 byte */
626 #define SUNATM_VPI      1       /* VPI - 1 byte */
627 #define SUNATM_VCI      2       /* VCI - 2 bytes */
628 #define SUNATM_LEN      4       /* length of the header */
629
630 /*
631  * The link-layer header on Nokia IPSO ATM packets.
632  */
633 #define NOKIAATM_FLAGS  0       /* destination - 1 byte */
634 #define NOKIAATM_VPI    1       /* VPI - 1 byte */
635 #define NOKIAATM_VCI    2       /* VCI - 2 bytes */
636 #define NOKIAATM_LEN    4       /* length of the header */
637
638 /*
639  * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
640  * to libpcap.
641  */
642 #define IRDA_SLL_PKTTYPE_OFFSET         0       /* packet type - 2 bytes */
643 /* 12 unused bytes */
644 #define IRDA_SLL_PROTOCOL_OFFSET        14      /* protocol, should be ETH_P_LAPD - 2 bytes */
645 #define IRDA_SLL_LEN                    16      /* length of the header */
646
647 /*
648  * A header containing additional MTP information.
649  */
650 #define MTP2_SENT_OFFSET                0       /* 1 byte */
651 #define MTP2_ANNEX_A_USED_OFFSET        1       /* 1 byte */
652 #define MTP2_LINK_NUMBER_OFFSET         2       /* 2 bytes */
653 #define MTP2_HDR_LEN                    4       /* length of the header */
654
655 /*
656  * A header containing additional SITA WAN information.
657  */
658 #define SITA_FLAGS_OFFSET               0       /* 1 byte */
659 #define SITA_SIGNALS_OFFSET             1       /* 1 byte */
660 #define SITA_ERRORS1_OFFSET             2       /* 1 byte */
661 #define SITA_ERRORS2_OFFSET             3       /* 1 byte */
662 #define SITA_PROTO_OFFSET               4       /* 1 byte */
663 #define SITA_HDR_LEN                    5       /* length of the header */
664
665 /*
666  * The fake link-layer header of LAPD packets.
667  */
668 #ifndef ETH_P_LAPD
669 #define ETH_P_LAPD 0x0030
670 #endif
671
672 #define LAPD_SLL_PKTTYPE_OFFSET         0       /* packet type - 2 bytes */
673 #define LAPD_SLL_HATYPE_OFFSET          2       /* hardware address type - 2 bytes */
674 #define LAPD_SLL_HALEN_OFFSET           4       /* hardware address length - 2 bytes */
675 #define LAPD_SLL_ADDR_OFFSET            6       /* address - 8 bytes */
676 #define LAPD_SLL_PROTOCOL_OFFSET        14      /* protocol, should be ETH_P_LAPD - 2 bytes */
677 #define LAPD_SLL_LEN                    16      /* length of the header */
678
679 /*
680  * I2C link-layer on-disk format
681  */
682 struct i2c_file_hdr {
683     guint8 bus;
684     guint8 flags[4];
685 };
686
687 static gboolean
688 pcap_read_sunatm_pseudoheader(FILE_T fh,
689     union wtap_pseudo_header *pseudo_header, int *err)
690 {
691         guint8  atm_phdr[SUNATM_LEN];
692         int     bytes_read;
693         guint8  vpi;
694         guint16 vci;
695
696         errno = WTAP_ERR_CANT_READ;
697         bytes_read = file_read(atm_phdr, 1, SUNATM_LEN, fh);
698         if (bytes_read != SUNATM_LEN) {
699                 *err = file_error(fh);
700                 if (*err == 0)
701                         *err = WTAP_ERR_SHORT_READ;
702                 return FALSE;
703         }
704
705         vpi = atm_phdr[SUNATM_VPI];
706         vci = pntohs(&atm_phdr[SUNATM_VCI]);
707
708         switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
709
710         case 0x01:      /* LANE */
711                 pseudo_header->atm.aal = AAL_5;
712                 pseudo_header->atm.type = TRAF_LANE;
713                 break;
714
715         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
716                 pseudo_header->atm.aal = AAL_5;
717                 pseudo_header->atm.type = TRAF_LLCMX;
718                 break;
719
720         case 0x05:      /* ILMI */
721                 pseudo_header->atm.aal = AAL_5;
722                 pseudo_header->atm.type = TRAF_ILMI;
723                 break;
724
725         case 0x06:      /* Q.2931 */
726                 pseudo_header->atm.aal = AAL_SIGNALLING;
727                 pseudo_header->atm.type = TRAF_UNKNOWN;
728                 break;
729
730         case 0x03:      /* MARS (RFC 2022) */
731                 pseudo_header->atm.aal = AAL_5;
732                 pseudo_header->atm.type = TRAF_UNKNOWN;
733                 break;
734
735         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
736                 pseudo_header->atm.aal = AAL_5;
737                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
738                 break;
739
740         default:
741                 /*
742                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
743                  * case assume it's AAL_SIGNALLING; we know nothing more
744                  * about it.
745                  *
746                  * XXX - is this necessary?  Or are we guaranteed that
747                  * all signalling traffic has a type of 0x06?
748                  *
749                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
750                  * 0x00 ("raw"), might it be non-AAL5 traffic?
751                  */
752                 if (vpi == 0 && vci == 5)
753                         pseudo_header->atm.aal = AAL_SIGNALLING;
754                 else
755                         pseudo_header->atm.aal = AAL_5;
756                 pseudo_header->atm.type = TRAF_UNKNOWN;
757                 break;
758         }
759         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
760
761         pseudo_header->atm.vpi = vpi;
762         pseudo_header->atm.vci = vci;
763         pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
764
765         /* We don't have this information */
766         pseudo_header->atm.flags = 0;
767         pseudo_header->atm.cells = 0;
768         pseudo_header->atm.aal5t_u2u = 0;
769         pseudo_header->atm.aal5t_len = 0;
770         pseudo_header->atm.aal5t_chksum = 0;
771
772         return TRUE;
773 }
774
775 static gboolean
776 pcap_read_nokiaatm_pseudoheader(FILE_T fh,
777     union wtap_pseudo_header *pseudo_header, int *err)
778 {
779         guint8  atm_phdr[NOKIAATM_LEN];
780         int     bytes_read;
781         guint8  vpi;
782         guint16 vci;
783
784         errno = WTAP_ERR_CANT_READ;
785         bytes_read = file_read(atm_phdr, 1, NOKIAATM_LEN, fh);
786         if (bytes_read != NOKIAATM_LEN) {
787                 *err = file_error(fh);
788                 if (*err == 0)
789                         *err = WTAP_ERR_SHORT_READ;
790                 return FALSE;
791         }
792
793         vpi = atm_phdr[NOKIAATM_VPI];
794         vci = pntohs(&atm_phdr[NOKIAATM_VCI]);
795
796         pseudo_header->atm.vpi = vpi;
797         pseudo_header->atm.vci = vci;
798         pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
799
800         /* We don't have this information */
801         pseudo_header->atm.flags = 0;
802         pseudo_header->atm.cells = 0;
803         pseudo_header->atm.aal5t_u2u = 0;
804         pseudo_header->atm.aal5t_len = 0;
805         pseudo_header->atm.aal5t_chksum = 0;
806
807         return TRUE;
808 }
809
810 static gboolean
811 pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
812     int *err, gchar **err_info)
813 {
814         guint8  irda_phdr[IRDA_SLL_LEN];
815         int     bytes_read;
816
817         errno = WTAP_ERR_CANT_READ;
818         bytes_read = file_read(irda_phdr, 1, IRDA_SLL_LEN, fh);
819         if (bytes_read != IRDA_SLL_LEN) {
820                 *err = file_error(fh);
821                 if (*err == 0)
822                         *err = WTAP_ERR_SHORT_READ;
823                 return FALSE;
824         }
825
826         if (pntohs(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
827                 *err = WTAP_ERR_BAD_RECORD;
828                 if (err_info != NULL)
829                         *err_info = g_strdup("libpcap: IrDA capture has a packet with an invalid sll_protocol field");
830                 return FALSE;
831         }
832
833         pseudo_header->irda.pkttype = pntohs(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
834
835         return TRUE;
836 }
837
838 static gboolean
839 pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
840 {
841         guint8 mtp2_hdr[MTP2_HDR_LEN];
842         int    bytes_read;
843
844         errno = WTAP_ERR_CANT_READ;
845         bytes_read = file_read(mtp2_hdr, 1, MTP2_HDR_LEN, fh);
846         if (bytes_read != MTP2_HDR_LEN) {
847                 *err = file_error(fh);
848                 if (*err == 0)
849                         *err = WTAP_ERR_SHORT_READ;
850                 return FALSE;
851         }
852
853         pseudo_header->mtp2.sent         = mtp2_hdr[MTP2_SENT_OFFSET];
854         pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
855         pseudo_header->mtp2.link_number  = pntohs(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
856
857         return TRUE;
858 }
859
860 static gboolean
861 pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
862     int *err, gchar **err_info)
863 {
864         guint8  lapd_phdr[LAPD_SLL_LEN];
865         int     bytes_read;
866
867         errno = WTAP_ERR_CANT_READ;
868         bytes_read = file_read(lapd_phdr, 1, LAPD_SLL_LEN, fh);
869         if (bytes_read != LAPD_SLL_LEN) {
870                 *err = file_error(fh);
871                 if (*err == 0)
872                         *err = WTAP_ERR_SHORT_READ;
873                 return FALSE;
874         }
875
876         if (pntohs(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
877                 *err = WTAP_ERR_BAD_RECORD;
878                 if (err_info != NULL)
879                         *err_info = g_strdup("libpcap: LAPD capture has a packet with an invalid sll_protocol field");
880                 return FALSE;
881         }
882
883         pseudo_header->lapd.pkttype = pntohs(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
884         pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
885
886         return TRUE;
887 }
888
889 static gboolean
890 pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
891 {
892         guint8  sita_phdr[SITA_HDR_LEN];
893         int     bytes_read;
894
895         errno = WTAP_ERR_CANT_READ;
896         bytes_read = file_read(sita_phdr, 1, SITA_HDR_LEN, fh);
897         if (bytes_read != SITA_HDR_LEN) {
898                 *err = file_error(fh);
899                 if (*err == 0)
900                         *err = WTAP_ERR_SHORT_READ;
901                 return FALSE;
902         }
903
904         pseudo_header->sita.flags   = sita_phdr[SITA_FLAGS_OFFSET];
905         pseudo_header->sita.signals = sita_phdr[SITA_SIGNALS_OFFSET];
906         pseudo_header->sita.errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
907         pseudo_header->sita.errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
908         pseudo_header->sita.proto   = sita_phdr[SITA_PROTO_OFFSET];
909
910         return TRUE;
911 }
912
913 static gboolean
914 pcap_read_linux_usb_pseudoheader(FILE_T fh,
915     union wtap_pseudo_header *pseudo_header, gboolean byte_swapped, int *err)
916 {
917         int     bytes_read;
918
919         errno = WTAP_ERR_CANT_READ;
920         bytes_read = file_read(&pseudo_header->linux_usb, 1,
921             sizeof (struct linux_usb_phdr), fh);
922         if (bytes_read != sizeof (struct linux_usb_phdr)) {
923                 *err = file_error(fh);
924                 if (*err == 0)
925                         *err = WTAP_ERR_SHORT_READ;
926                 return FALSE;
927         }
928
929         if (byte_swapped) {
930                 pseudo_header->linux_usb.id = GUINT64_SWAP_LE_BE(pseudo_header->linux_usb.id);
931                 pseudo_header->linux_usb.bus_id = GUINT16_SWAP_LE_BE(pseudo_header->linux_usb.bus_id);
932                 pseudo_header->linux_usb.ts_sec = GUINT64_SWAP_LE_BE(pseudo_header->linux_usb.ts_sec);
933                 pseudo_header->linux_usb.ts_usec = GUINT32_SWAP_LE_BE(pseudo_header->linux_usb.ts_usec);
934                 pseudo_header->linux_usb.status = GUINT32_SWAP_LE_BE(pseudo_header->linux_usb.status);
935                 pseudo_header->linux_usb.urb_len = GUINT32_SWAP_LE_BE(pseudo_header->linux_usb.urb_len);
936                 pseudo_header->linux_usb.data_len = GUINT32_SWAP_LE_BE(pseudo_header->linux_usb.data_len);
937         }
938
939         return TRUE;
940 }
941
942 static gboolean
943 pcap_read_bt_pseudoheader(FILE_T fh,
944     union wtap_pseudo_header *pseudo_header, int *err)
945 {
946         int     bytes_read;
947         struct libpcap_bt_phdr phdr;
948
949         errno = WTAP_ERR_CANT_READ;
950         bytes_read = file_read(&phdr, 1,
951             sizeof (struct libpcap_bt_phdr), fh);
952         if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
953                 *err = file_error(fh);
954                 if (*err == 0)
955                         *err = WTAP_ERR_SHORT_READ;
956                 return FALSE;
957         }
958         pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
959         return TRUE;
960 }
961
962 static gboolean
963 pcap_read_ppp_pseudoheader(FILE_T fh,
964     union wtap_pseudo_header *pseudo_header, int *err)
965 {
966         int     bytes_read;
967         struct libpcap_ppp_phdr phdr;
968
969         errno = WTAP_ERR_CANT_READ;
970         bytes_read = file_read(&phdr, 1,
971             sizeof (struct libpcap_ppp_phdr), fh);
972         if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
973                 *err = file_error(fh);
974                 if (*err == 0)
975                         *err = WTAP_ERR_SHORT_READ;
976                 return FALSE;
977         }
978         pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
979         return TRUE;
980 }
981
982 static gboolean
983 pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
984                               union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
985 {
986   guint8 erf_hdr[sizeof(struct erf_phdr)];
987   int    bytes_read;
988
989   errno = WTAP_ERR_CANT_READ;
990   bytes_read = file_read(erf_hdr, 1, sizeof(struct erf_phdr), fh);
991   if (bytes_read != sizeof(struct erf_phdr)) {
992     *err = file_error(fh);
993     if (*err == 0)
994       *err = WTAP_ERR_SHORT_READ;
995     return FALSE;
996   }
997   pseudo_header->erf.phdr.ts = pletohll(&erf_hdr[0]); /* timestamp */
998   pseudo_header->erf.phdr.type =  erf_hdr[8];
999   pseudo_header->erf.phdr.flags = erf_hdr[9];
1000   pseudo_header->erf.phdr.rlen = pntohs(&erf_hdr[10]);
1001   pseudo_header->erf.phdr.lctr = pntohs(&erf_hdr[12]);
1002   pseudo_header->erf.phdr.wlen = pntohs(&erf_hdr[14]);
1003
1004   /* The high 32 bits of the timestamp contain the integer number of seconds
1005    * while the lower 32 bits contain the binary fraction of the second.
1006    * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1007   if (whdr) {
1008     guint64 ts = pseudo_header->erf.phdr.ts;
1009     whdr->ts.secs = (guint32) (ts >> 32);
1010     ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1011     ts += (ts & 0x80000000) << 1; /* rounding */
1012     whdr->ts.nsecs = ((guint32) (ts >> 32));
1013     if ( whdr->ts.nsecs >= 1000000000) {
1014       whdr->ts.nsecs -= 1000000000;
1015       whdr->ts.secs += 1;
1016     }
1017   }
1018   return TRUE;
1019 }
1020
1021 /*
1022  * If the type of record given in the pseudo header indicate the presence of an extension
1023  * header then, read all the extension headers
1024  */
1025 static gboolean
1026 pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1027                            int *err, gchar **err_info _U_, guint * psize)
1028 {
1029   int bytes_read = 0;
1030   guint8 erf_exhdr[8];
1031   guint64 erf_exhdr_sw;
1032   int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1033   guint8 type = pseudo_header->erf.phdr.type;
1034   *psize = 0;
1035   if (pseudo_header->erf.phdr.type & 0x80){
1036     do{
1037       errno = WTAP_ERR_CANT_READ;
1038       bytes_read = file_read(erf_exhdr, 1, 8, fh);
1039       if (bytes_read != 8 ) {
1040         *err = file_error(fh);
1041         if (*err == 0)
1042           *err = WTAP_ERR_SHORT_READ;
1043         return FALSE;
1044       }
1045       type = erf_exhdr[0];
1046       erf_exhdr_sw = pntohll((guint64*) &(erf_exhdr[0]));
1047       if (i < max)
1048         memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1049       *psize += 8;
1050       i++;
1051     } while (type & 0x80);
1052   }
1053   return TRUE;
1054 }
1055
1056 /*
1057  * If the type of record given in the pseudo header indicate the precense of a subheader
1058  * then, read this optional subheader
1059  */
1060 static gboolean
1061 pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1062                            int *err, gchar **err_info _U_, guint * psize)
1063 {
1064   guint8 erf_subhdr[sizeof(union erf_subhdr)];
1065   int    bytes_read;
1066
1067   *psize=0;
1068   switch(pseudo_header->erf.phdr.type & 0x7F) {
1069   case ERF_TYPE_MC_HDLC:
1070   case ERF_TYPE_MC_RAW:
1071   case ERF_TYPE_MC_ATM:
1072   case ERF_TYPE_MC_RAW_CHANNEL:
1073   case ERF_TYPE_MC_AAL5:
1074   case ERF_TYPE_MC_AAL2:
1075   case ERF_TYPE_COLOR_MC_HDLC_POS:
1076     /* Extract the Multi Channel header to include it in the pseudo header part */
1077     errno = WTAP_ERR_CANT_READ;
1078     bytes_read = file_read(erf_subhdr, 1, sizeof(erf_mc_header_t), fh);
1079     if (bytes_read != sizeof(erf_mc_header_t) ) {
1080       *err = file_error(fh);
1081       if (*err == 0)
1082         *err = WTAP_ERR_SHORT_READ;
1083       return FALSE;
1084     }
1085     pseudo_header->erf.subhdr.mc_hdr = pntohl(&erf_subhdr[0]);
1086     *psize = sizeof(erf_mc_header_t);
1087     break;
1088   case ERF_TYPE_ETH:
1089   case ERF_TYPE_COLOR_ETH:
1090   case ERF_TYPE_DSM_COLOR_ETH:
1091     /* Extract the Ethernet additional header to include it in the pseudo header part */
1092     errno = WTAP_ERR_CANT_READ;
1093     bytes_read = file_read(erf_subhdr, 1, sizeof(erf_eth_header_t), fh);
1094     if (bytes_read != sizeof(erf_eth_header_t) ) {
1095       *err = file_error(fh);
1096       if (*err == 0)
1097         *err = WTAP_ERR_SHORT_READ;
1098       return FALSE;
1099     }
1100     pseudo_header->erf.subhdr.eth_hdr = pntohs(&erf_subhdr[0]);
1101     *psize = sizeof(erf_eth_header_t);
1102     break;
1103   default:
1104     /* No optional pseudo header for this ERF type */
1105     break;
1106   }
1107   return TRUE;
1108 }
1109
1110 static gboolean
1111 pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
1112 {
1113         struct i2c_file_hdr i2c_hdr;
1114         int    bytes_read;
1115
1116         errno = WTAP_ERR_CANT_READ;
1117         bytes_read = file_read(&i2c_hdr, 1, sizeof (i2c_hdr), fh);
1118         if (bytes_read != sizeof (i2c_hdr)) {
1119                 *err = file_error(fh);
1120                 if (*err == 0)
1121                         *err = WTAP_ERR_SHORT_READ;
1122                 return FALSE;
1123         }
1124
1125         pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
1126         pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
1127         pseudo_header->i2c.flags = pntohl(&i2c_hdr.flags);
1128
1129         return TRUE;
1130 }
1131
1132 int
1133 pcap_process_pseudo_header(FILE_T fh, int file_type, int wtap_encap, gboolean bytes_swapped, guint packet_size,
1134     gboolean check_packet_size, struct wtap_pkthdr *phdr,
1135     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1136 {
1137         int phdr_len = 0;
1138         guint size;
1139
1140         switch (wtap_encap) {
1141
1142         case WTAP_ENCAP_ATM_PDUS:
1143                 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1144                         /*
1145                          * Nokia IPSO ATM.
1146                          */
1147                         if (check_packet_size && packet_size < NOKIAATM_LEN) {
1148                                 /*
1149                                  * Uh-oh, the packet isn't big enough to even
1150                                  * have a pseudo-header.
1151                                  */
1152                                 *err = WTAP_ERR_BAD_RECORD;
1153                                 *err_info = g_strdup_printf("pcap: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1154                                     packet_size);
1155                                 return -1;
1156                         }
1157                         if (!pcap_read_nokiaatm_pseudoheader(fh,
1158                             pseudo_header, err))
1159                                 return -1;      /* Read error */
1160
1161                         phdr_len = NOKIAATM_LEN;
1162                 } else {
1163                         /*
1164                          * SunATM.
1165                          */
1166                         if (check_packet_size && packet_size < SUNATM_LEN) {
1167                                 /*
1168                                  * Uh-oh, the packet isn't big enough to even
1169                                  * have a pseudo-header.
1170                                  */
1171                                 *err = WTAP_ERR_BAD_RECORD;
1172                                 *err_info = g_strdup_printf("pcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1173                                     packet_size);
1174                                 return -1;
1175                         }
1176                         if (!pcap_read_sunatm_pseudoheader(fh,
1177                             pseudo_header, err))
1178                                 return -1;      /* Read error */
1179
1180                         phdr_len = SUNATM_LEN;
1181                 }
1182                 break;
1183
1184         case WTAP_ENCAP_ETHERNET:
1185                 /*
1186                  * We don't know whether there's an FCS in this frame or not.
1187                  */
1188                 pseudo_header->eth.fcs_len = -1;
1189                 break;
1190
1191         case WTAP_ENCAP_IEEE_802_11:
1192         case WTAP_ENCAP_PRISM_HEADER:
1193         case WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP:
1194         case WTAP_ENCAP_IEEE_802_11_WLAN_AVS:
1195                 /*
1196                  * We don't know whether there's an FCS in this frame or not.
1197                  * XXX - are there any OSes where the capture mechanism
1198                  * supplies an FCS?
1199                  */
1200                 pseudo_header->ieee_802_11.fcs_len = -1;
1201                 pseudo_header->ieee_802_11.channel = 0;
1202                 pseudo_header->ieee_802_11.data_rate = 0;
1203                 pseudo_header->ieee_802_11.signal_level = 0;
1204                 break;
1205
1206         case WTAP_ENCAP_IRDA:
1207                 if (check_packet_size && packet_size < IRDA_SLL_LEN) {
1208                         /*
1209                          * Uh-oh, the packet isn't big enough to even
1210                          * have a pseudo-header.
1211                          */
1212                         *err = WTAP_ERR_BAD_RECORD;
1213                         *err_info = g_strdup_printf("pcap: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1214                             packet_size);
1215                         return -1;
1216                 }
1217                 if (!pcap_read_irda_pseudoheader(fh, pseudo_header,
1218                     err, err_info))
1219                         return -1;      /* Read error */
1220
1221                 phdr_len = IRDA_SLL_LEN;
1222                 break;
1223
1224         case WTAP_ENCAP_MTP2_WITH_PHDR:
1225                 if (check_packet_size && packet_size < MTP2_HDR_LEN) {
1226                         /*
1227                          * Uh-oh, the packet isn't big enough to even
1228                          * have a pseudo-header.
1229                          */
1230                         *err = WTAP_ERR_BAD_RECORD;
1231                         *err_info = g_strdup_printf("pcap: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1232                             packet_size);
1233                         return -1;
1234                 }
1235                 if (!pcap_read_mtp2_pseudoheader(fh, pseudo_header,
1236                     err, err_info))
1237                         return -1;      /* Read error */
1238
1239                 phdr_len = MTP2_HDR_LEN;
1240                 break;
1241
1242         case WTAP_ENCAP_LINUX_LAPD:
1243                 if (check_packet_size && packet_size < LAPD_SLL_LEN) {
1244                         /*
1245                          * Uh-oh, the packet isn't big enough to even
1246                          * have a pseudo-header.
1247                          */
1248                         *err = WTAP_ERR_BAD_RECORD;
1249                         *err_info = g_strdup_printf("pcap: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1250                             packet_size);
1251                         return -1;
1252                 }
1253                 if (!pcap_read_lapd_pseudoheader(fh, pseudo_header,
1254                     err, err_info))
1255                         return -1;      /* Read error */
1256
1257                 phdr_len = LAPD_SLL_LEN;
1258                 break;
1259
1260         case WTAP_ENCAP_SITA:
1261                 if (check_packet_size && packet_size < SITA_HDR_LEN) {
1262                         /*
1263                          * Uh-oh, the packet isn't big enough to even
1264                          * have a pseudo-header.
1265                          */
1266                         *err = WTAP_ERR_BAD_RECORD;
1267                         *err_info = g_strdup_printf("pcap: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1268                             packet_size);
1269                         return -1;
1270                 }
1271                 if (!pcap_read_sita_pseudoheader(fh, pseudo_header,
1272                     err, err_info))
1273                         return -1;      /* Read error */
1274
1275                 phdr_len = SITA_HDR_LEN;
1276                 break;
1277
1278         case WTAP_ENCAP_USB_LINUX:
1279         case WTAP_ENCAP_USB_LINUX_MMAPPED:
1280                 if (check_packet_size &&
1281                     packet_size < sizeof (struct linux_usb_phdr)) {
1282                         /*
1283                          * Uh-oh, the packet isn't big enough to even
1284                          * have a pseudo-header.
1285                          */
1286                         *err = WTAP_ERR_BAD_RECORD;
1287                         *err_info = g_strdup_printf("pcap: Linux USB file has a %u-byte packet, too small to have even a Linux USB pseudo-header",
1288                             packet_size);
1289                         return -1;
1290                 }
1291                 if (!pcap_read_linux_usb_pseudoheader(fh,
1292                     pseudo_header, bytes_swapped, err))
1293                         return -1;      /* Read error */
1294
1295                 phdr_len = (int)sizeof (struct linux_usb_phdr);
1296                 break;
1297
1298         case WTAP_ENCAP_BLUETOOTH_H4:
1299                 /* We don't have pseudoheader, so just pretend we received everything. */
1300                 pseudo_header->p2p.sent = FALSE;
1301                 break;
1302
1303         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1304                 if (check_packet_size &&
1305                     packet_size < sizeof (struct libpcap_bt_phdr)) {
1306                         /*
1307                          * Uh-oh, the packet isn't big enough to even
1308                          * have a pseudo-header.
1309                          */
1310                         *err = WTAP_ERR_BAD_RECORD;
1311                         *err_info = g_strdup_printf("pcap: lipcap bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1312                             packet_size);
1313                         return -1;
1314                 }
1315                 if (!pcap_read_bt_pseudoheader(fh,
1316                     pseudo_header, err))
1317                         return -1;      /* Read error */
1318
1319                 phdr_len = (int)sizeof (struct libpcap_bt_phdr);
1320                 break;
1321
1322         case WTAP_ENCAP_PPP_WITH_PHDR:
1323                 if (check_packet_size &&
1324                     packet_size < sizeof (struct libpcap_ppp_phdr)) {
1325                         /*
1326                          * Uh-oh, the packet isn't big enough to even
1327                          * have a pseudo-header.
1328                          */
1329                         *err = WTAP_ERR_BAD_RECORD;
1330                         *err_info = g_strdup_printf("pcap: lipcap ppp file has a %u-byte packet, too small to have even a pseudo-header",
1331                             packet_size);
1332                         return -1;
1333                 }
1334                 if (!pcap_read_ppp_pseudoheader(fh,
1335                     pseudo_header, err))
1336                         return -1;      /* Read error */
1337
1338                 phdr_len = (int)sizeof (struct libpcap_ppp_phdr);
1339                 break;
1340
1341         case WTAP_ENCAP_ERF:
1342                 if (check_packet_size &&
1343                     packet_size < sizeof(struct erf_phdr) ) {
1344                         /*
1345                          * Uh-oh, the packet isn't big enough to even
1346                          * have a pseudo-header.
1347                          */
1348                         *err = WTAP_ERR_BAD_RECORD;
1349                         *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1350                             packet_size);
1351                         return -1;
1352                 }
1353
1354                 if (!pcap_read_erf_pseudoheader(fh, phdr, pseudo_header,
1355                     err, err_info))
1356                         return -1;      /* Read error */
1357
1358                 phdr_len = (int)sizeof(struct erf_phdr);
1359
1360                 /* check the optional Extension header */
1361                 if (!pcap_read_erf_exheader(fh, pseudo_header, err, err_info,
1362                     &size))
1363                         return -1;      /* Read error */
1364
1365                 phdr_len += size;
1366
1367                 /* check the optional Multi Channel header */
1368                 if (!pcap_read_erf_subheader(fh, pseudo_header, err, err_info,
1369                     &size))
1370                         return -1;      /* Read error */
1371
1372                 phdr_len += size;
1373                 break;
1374
1375         case WTAP_ENCAP_I2C:
1376                 if (check_packet_size &&
1377                     packet_size < sizeof (struct i2c_file_hdr)) {
1378                         /*
1379                          * Uh-oh, the packet isn't big enough to even
1380                          * have a pseudo-header.
1381                          */
1382                         *err = WTAP_ERR_BAD_RECORD;
1383                         *err_info = g_strdup_printf("pcap: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1384                             packet_size);
1385                         return -1;
1386                 }
1387                 if (!pcap_read_i2c_pseudoheader(fh, pseudo_header,
1388                     err, err_info))
1389                         return -1;      /* Read error */
1390
1391                 /*
1392                  * Don't count the pseudo-header as part of the packet.
1393                  */
1394                 phdr_len = (int)sizeof (struct i2c_file_hdr);
1395                 break;
1396         }
1397
1398         return phdr_len;
1399 }
1400
1401 int
1402 pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
1403 {
1404         int hdrsize;
1405
1406         switch (encap) {
1407
1408         case WTAP_ENCAP_ATM_PDUS:
1409                 hdrsize = SUNATM_LEN;
1410                 break;
1411
1412         case WTAP_ENCAP_IRDA:
1413                 hdrsize = IRDA_SLL_LEN;
1414                 break;
1415
1416         case WTAP_ENCAP_MTP2_WITH_PHDR:
1417                 hdrsize = MTP2_HDR_LEN;
1418                 break;
1419
1420         case WTAP_ENCAP_LINUX_LAPD:
1421                 hdrsize = LAPD_SLL_LEN;
1422                 break;
1423
1424         case WTAP_ENCAP_SITA:
1425                 hdrsize = SITA_HDR_LEN;
1426                 break;
1427
1428         case WTAP_ENCAP_USB_LINUX:
1429         case WTAP_ENCAP_USB_LINUX_MMAPPED:
1430                 hdrsize = (int)sizeof (struct linux_usb_phdr);
1431                 break;
1432
1433         case WTAP_ENCAP_ERF:
1434                 hdrsize = (int)sizeof (struct erf_phdr);
1435                 if (pseudo_header->erf.phdr.type & 0x80)
1436                         hdrsize += 8;
1437                 switch (pseudo_header->erf.phdr.type & 0x7F) {
1438
1439                 case ERF_TYPE_MC_HDLC:
1440                 case ERF_TYPE_MC_RAW:
1441                 case ERF_TYPE_MC_ATM:
1442                 case ERF_TYPE_MC_RAW_CHANNEL:
1443                 case ERF_TYPE_MC_AAL5:
1444                 case ERF_TYPE_MC_AAL2:
1445                 case ERF_TYPE_COLOR_MC_HDLC_POS:
1446                         hdrsize += (int)sizeof(struct erf_mc_hdr);
1447                         break;
1448
1449                 case ERF_TYPE_ETH:
1450                 case ERF_TYPE_COLOR_ETH:
1451                 case ERF_TYPE_DSM_COLOR_ETH:
1452                         hdrsize += (int)sizeof(struct erf_eth_hdr);
1453                         break;
1454
1455                 default:
1456                         break;
1457                 }
1458                 break;
1459
1460         case WTAP_ENCAP_I2C:
1461                 hdrsize = (int)sizeof (struct i2c_file_hdr);
1462                 break;
1463
1464         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1465                 hdrsize = (int)sizeof (struct libpcap_bt_phdr);
1466                 break;
1467
1468         case WTAP_ENCAP_PPP_WITH_PHDR:
1469                 hdrsize = (int)sizeof (struct libpcap_ppp_phdr);
1470                 break;
1471
1472         default:
1473                 hdrsize = 0;
1474                 break;
1475         }
1476
1477         return hdrsize;
1478 }
1479
1480 gboolean
1481 pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
1482     int *err)
1483 {
1484         guint8 atm_hdr[SUNATM_LEN];
1485         guint8 irda_hdr[IRDA_SLL_LEN];
1486         guint8 lapd_hdr[LAPD_SLL_LEN];
1487         guint8 mtp2_hdr[MTP2_HDR_LEN];
1488         guint8 sita_hdr[SITA_HDR_LEN];
1489         guint8 erf_hdr[ sizeof(struct erf_mc_phdr)];
1490         struct i2c_file_hdr i2c_hdr;
1491         struct libpcap_bt_phdr bt_hdr;
1492         struct libpcap_ppp_phdr ppp_hdr;
1493         size_t nwritten;
1494         size_t size;
1495
1496         switch (encap) {
1497
1498         case WTAP_ENCAP_ATM_PDUS:
1499                 /*
1500                  * Write the ATM header.
1501                  */
1502                 atm_hdr[SUNATM_FLAGS] =
1503                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1504                 switch (pseudo_header->atm.aal) {
1505
1506                 case AAL_SIGNALLING:
1507                         /* Q.2931 */
1508                         atm_hdr[SUNATM_FLAGS] |= 0x06;
1509                         break;
1510
1511                 case AAL_5:
1512                         switch (pseudo_header->atm.type) {
1513
1514                         case TRAF_LANE:
1515                                 /* LANE */
1516                                 atm_hdr[SUNATM_FLAGS] |= 0x01;
1517                                 break;
1518
1519                         case TRAF_LLCMX:
1520                                 /* RFC 1483 LLC multiplexed traffic */
1521                                 atm_hdr[SUNATM_FLAGS] |= 0x02;
1522                                 break;
1523
1524                         case TRAF_ILMI:
1525                                 /* ILMI */
1526                                 atm_hdr[SUNATM_FLAGS] |= 0x05;
1527                                 break;
1528                         }
1529                         break;
1530                 }
1531                 atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
1532                 phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1533                 nwritten = wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr));
1534                 if (nwritten != sizeof(atm_hdr)) {
1535                         if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1536                                 *err = wtap_dump_file_ferror(wdh);
1537                         else
1538                                 *err = WTAP_ERR_SHORT_WRITE;
1539                         return FALSE;
1540                 }
1541                 wdh->bytes_dumped += sizeof(atm_hdr);
1542                 break;
1543
1544         case WTAP_ENCAP_IRDA:
1545                 /*
1546                  * Write the IrDA header.
1547                  */
1548                 memset(irda_hdr, 0, sizeof(irda_hdr));
1549                 phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
1550                     pseudo_header->irda.pkttype);
1551                 phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
1552                 nwritten = wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr));
1553                 if (nwritten != sizeof(irda_hdr)) {
1554                         if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1555                                 *err = wtap_dump_file_ferror(wdh);
1556                         else
1557                                 *err = WTAP_ERR_SHORT_WRITE;
1558                         return FALSE;
1559                 }
1560                 wdh->bytes_dumped += sizeof(irda_hdr);
1561                 break;
1562
1563         case WTAP_ENCAP_MTP2_WITH_PHDR:
1564                 /*
1565                  * Write the MTP2 header.
1566                  */
1567                 memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
1568                 mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
1569                 mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
1570                 phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
1571                     pseudo_header->mtp2.link_number);
1572                 nwritten = wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr));
1573                 if (nwritten != sizeof(mtp2_hdr)) {
1574                         if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1575                                 *err = wtap_dump_file_ferror(wdh);
1576                         else
1577                                 *err = WTAP_ERR_SHORT_WRITE;
1578                         return FALSE;
1579                 }
1580                 wdh->bytes_dumped += sizeof(mtp2_hdr);
1581                 break;
1582
1583         case WTAP_ENCAP_LINUX_LAPD:
1584                 /*
1585                  * Write the LAPD header.
1586                  */
1587                 memset(&lapd_hdr, 0, sizeof(lapd_hdr));
1588                 phtons(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET],
1589                     pseudo_header->lapd.pkttype);
1590                 phtons(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
1591                 lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
1592                     pseudo_header->lapd.we_network?0x01:0x00;
1593                 nwritten = fwrite(&lapd_hdr, 1, sizeof(lapd_hdr), wdh->fh);
1594                 if (nwritten != sizeof(lapd_hdr)) {
1595                         if (nwritten == 0 && ferror(wdh->fh))
1596                                 *err = errno;
1597                         else
1598                                 *err = WTAP_ERR_SHORT_WRITE;
1599                         return FALSE;
1600                 }
1601                 wdh->bytes_dumped += sizeof(lapd_hdr);
1602                 break;
1603
1604         case WTAP_ENCAP_SITA:
1605                 /*
1606                  * Write the SITA header.
1607                  */
1608                 memset(&sita_hdr, 0, sizeof(sita_hdr));
1609                 sita_hdr[SITA_FLAGS_OFFSET]   = pseudo_header->sita.flags;
1610                 sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.signals;
1611                 sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.errors1;
1612                 sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.errors2;
1613                 sita_hdr[SITA_PROTO_OFFSET]   = pseudo_header->sita.proto;
1614                 nwritten = fwrite(&sita_hdr, 1, sizeof(sita_hdr), wdh->fh);
1615                 if (nwritten != sizeof(sita_hdr)) {
1616                         if (nwritten == 0 && ferror(wdh->fh))
1617                                 *err = errno;
1618                         else
1619                                 *err = WTAP_ERR_SHORT_WRITE;
1620                         return FALSE;
1621                 }
1622                 wdh->bytes_dumped += sizeof(sita_hdr);
1623                 break;
1624
1625         case WTAP_ENCAP_USB_LINUX:
1626         case WTAP_ENCAP_USB_LINUX_MMAPPED:
1627                 /*
1628                  * Write out the pseudo-header; it has the same format
1629                  * as the Linux USB header, and that header is supposed
1630                  * to be written in the host byte order of the machine
1631                  * writing the file.
1632                  */
1633                 nwritten = fwrite(&pseudo_header->linux_usb, 1,
1634                     sizeof(pseudo_header->linux_usb), wdh->fh);
1635                 if (nwritten != sizeof(pseudo_header->linux_usb)) {
1636                         if (nwritten == 0 && ferror(wdh->fh))
1637                                 *err = errno;
1638                         else
1639                                 *err = WTAP_ERR_SHORT_WRITE;
1640                         return FALSE;
1641                 }
1642                 wdh->bytes_dumped += sizeof(lapd_hdr);
1643                 break;
1644
1645         case WTAP_ENCAP_ERF:
1646                  /*
1647                  * Write the ERF header.
1648                  */
1649                 memset(&erf_hdr, 0, sizeof(erf_hdr));
1650                 pletonll(&erf_hdr[0], pseudo_header->erf.phdr.ts);
1651                 erf_hdr[8] = pseudo_header->erf.phdr.type;
1652                 erf_hdr[9] = pseudo_header->erf.phdr.flags;
1653                 phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
1654                 phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
1655                 phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
1656                 size = sizeof(struct erf_phdr);
1657
1658                 switch(pseudo_header->erf.phdr.type & 0x7F) {
1659                 case ERF_TYPE_MC_HDLC:
1660                 case ERF_TYPE_MC_RAW:
1661                 case ERF_TYPE_MC_ATM:
1662                 case ERF_TYPE_MC_RAW_CHANNEL:
1663                 case ERF_TYPE_MC_AAL5:
1664                 case ERF_TYPE_MC_AAL2:
1665                 case ERF_TYPE_COLOR_MC_HDLC_POS:
1666                   phtonl(&erf_hdr[16], pseudo_header->erf.subhdr.mc_hdr);
1667                   size += (int)sizeof(struct erf_mc_hdr);
1668                   break;
1669                 case ERF_TYPE_ETH:
1670                 case ERF_TYPE_COLOR_ETH:
1671                 case ERF_TYPE_DSM_COLOR_ETH:
1672                   phtons(&erf_hdr[16], pseudo_header->erf.subhdr.eth_hdr);
1673                   size += (int)sizeof(struct erf_eth_hdr);
1674                   break;
1675                 default:
1676                   break;
1677                 }
1678                 nwritten = wtap_dump_file_write(wdh, erf_hdr, size);
1679                 if (nwritten != (guint) size) {
1680                         if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1681                                 *err = wtap_dump_file_ferror(wdh);
1682                         else
1683                                 *err = WTAP_ERR_SHORT_WRITE;
1684                         return FALSE;
1685                 }
1686                 wdh->bytes_dumped += size;
1687                 break;
1688
1689         case WTAP_ENCAP_I2C:
1690                 /*
1691                  * Write the I2C header.
1692                  */
1693                 memset(&i2c_hdr, 0, sizeof(i2c_hdr));
1694                 i2c_hdr.bus = pseudo_header->i2c.bus |
1695                         (pseudo_header->i2c.is_event ? 0x80 : 0x00);
1696                 phtonl((guint8 *)&i2c_hdr.flags, pseudo_header->i2c.flags);
1697                 nwritten = fwrite(&i2c_hdr, 1, sizeof(i2c_hdr), wdh->fh);
1698                 if (nwritten != sizeof(i2c_hdr)) {
1699                         if (nwritten == 0 && ferror(wdh->fh))
1700                                 *err = errno;
1701                         else
1702                                 *err = WTAP_ERR_SHORT_WRITE;
1703                         return FALSE;
1704                 }
1705                 wdh->bytes_dumped += sizeof(i2c_hdr);
1706                 break;
1707
1708         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1709                 bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
1710                 nwritten = wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr);
1711                 if (nwritten != sizeof bt_hdr) {
1712                         if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1713                                 *err = wtap_dump_file_ferror(wdh);
1714                         else
1715                                 *err = WTAP_ERR_SHORT_WRITE;
1716                         return FALSE;
1717                 }
1718                 wdh->bytes_dumped += sizeof bt_hdr;
1719                 break;
1720
1721         case WTAP_ENCAP_PPP_WITH_PHDR:
1722                 ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
1723                 nwritten = wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr);
1724                 if (nwritten != sizeof ppp_hdr) {
1725                         if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1726                                 *err = wtap_dump_file_ferror(wdh);
1727                         else
1728                                 *err = WTAP_ERR_SHORT_WRITE;
1729                         return FALSE;
1730                 }
1731                 wdh->bytes_dumped += sizeof ppp_hdr;
1732                 break;
1733         }
1734         return TRUE;
1735 }