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