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