From Martin Kaiser: dissector for DVB-CI (Common Interface)
[metze/wireshark/wip.git] / wiretap / pcap-common.c
1 /* pcap-common.c
2  * Code common to libpcap and pcap-NG file formats
3  *
4  * $Id$
5  *
6  * Wiretap Library
7  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
8  *
9  * File format support for pcap-ng file format
10  * Copyright (c) 2007 by Ulf Lamping <ulf.lamping@web.de>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include "wtap-int.h"
35 #include "file_wrappers.h"
36 #include "erf.h"
37 #include "pcap-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         /* LAPD frame */
337         { 203,          WTAP_ENCAP_LAPD },
338         /* PPP with pseudoheader */
339         { 204,          WTAP_ENCAP_PPP_WITH_PHDR },
340         /* IPMB/I2C */
341         { 209,          WTAP_ENCAP_I2C },
342         /* FlexRay frame */
343         { 210,          WTAP_ENCAP_FLEXRAY },
344         /* MOST frame */
345         { 211,          WTAP_ENCAP_MOST },
346         /* LIN frame */
347         { 212,          WTAP_ENCAP_LIN },
348         /* X2E Xoraya serial frame */
349         { 213,          WTAP_ENCAP_X2E_SERIAL },
350         /* X2E Xoraya frame */
351         { 214,          WTAP_ENCAP_X2E_XORAYA },
352         /* IEEE 802.15.4 Wireless PAN non-ASK PHY */
353         { 215,          WTAP_ENCAP_IEEE802_15_4_NONASK_PHY },
354         /* USB packets with padded Linux-specified header */
355         { 220,          WTAP_ENCAP_USB_LINUX_MMAPPED },
356         /* Fibre Channel FC-2 frame */
357         { 224,          WTAP_ENCAP_FIBRE_CHANNEL_FC2 },
358         /* Fibre Channel FC-2 frame with Delimiter */
359         { 225,          WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS },
360         /* Solaris IPNET */
361         { 226,          WTAP_ENCAP_IPNET },
362         /* SocketCAN frame */
363         { 227,          WTAP_ENCAP_SOCKETCAN },
364         /* Raw IPv4 */
365         { 228,          WTAP_ENCAP_RAW_IP4 },
366         /* Raw IPv6 */
367         { 229,          WTAP_ENCAP_RAW_IP6 },
368         /* IEEE 802.15.4 Wireless PAN no fcs */
369         { 230,          WTAP_ENCAP_IEEE802_15_4_NOFCS },
370         /* DVB-CI (Common Interface) */
371         { 235,          WTAP_ENCAP_DVBCI },
372
373         /*
374          * To repeat:
375          *
376          * If you need a new encapsulation type for libpcap files, do
377          * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
378          * add a new encapsulation type by changing an existing entry;
379          * leave the existing entries alone.
380          *
381          * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
382          * for a new DLT_ value, and specifying the purpose of the new value.
383          * When you get the new DLT_ value, use that numerical value in
384          * the "dlt_value" field of "pcap_to_wtap_map[]".
385          */
386
387         /*
388          * The following are entries for libpcap type values that have
389          * different meanings on different OSes.
390          *
391          * We put these *after* the entries for the platform-independent
392          * libpcap type values for those Wiretap encapsulation types, so
393          * that Wireshark chooses the platform-independent libpcap type
394          * value for those encapsulatioin types, not the platform-dependent
395          * one.
396          */
397
398         /*
399          * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
400          * seen that define anything other than DLT_ATM_RFC1483 as 11 are
401          * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
402          * which define it as 100, mapping the kernel's value to 100, in
403          * an attempt to hide the different values used on different
404          * platforms.
405          *
406          * If this is a platform where DLT_FR is defined as 11, we
407          * don't handle 11 at all; otherwise, we handle it as
408          * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
409          * captures from BSD/OS if running on platforms other than BSD/OS,
410          * but
411          *
412          *      1) we don't yet support DLT_FR
413          *
414          * and
415          *
416          *      2) nothing short of a heuristic would let us interpret
417          *         them correctly).
418          */
419 #if defined(DLT_FR) && (DLT_FR == 11)
420         { 11,           WTAP_ENCAP_FRELAY },
421 #else
422         { 11,           WTAP_ENCAP_ATM_RFC1483 },
423 #endif
424
425         /*
426          * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
427          * BSD/OS, and DLT_LOOP on OpenBSD.
428          *
429          * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
430          * (it's just like DLT_NULL, only with the AF_ value in network
431          * rather than host byte order - Wireshark figures out the
432          * byte order from the data, so we don't care what byte order
433          * it's in), so if DLT_LOOP is defined as 12, interpret 12
434          * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
435          * as 12, interpret it as WTAP_ENCAP_RAW_IP.
436          */
437 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
438         { 12,           WTAP_ENCAP_NULL },
439 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
440         /*
441          * Put entry for Cisco HDLC here.
442          * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
443          * start with a 4-byte Cisco HDLC header?
444          */
445 #else
446         { 12,           WTAP_ENCAP_RAW_IP },
447 #endif
448
449         /*
450          * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
451          * don't actually generate it.  I infer that BSD/OS translates
452          * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
453          * libpcap, as the BSD/OS link-layer header is different;
454          * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
455          *
456          * From this, I infer that there's no point in handling 13
457          * as DLT_SLIP_BSDOS.
458          *
459          * 13 is DLT_ATM_RFC1483 on BSD/OS.
460          *
461          * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
462          * of decrypted IPsec traffic.
463          *
464          * We treat 13 as WTAP_ENCAP_ENC on all systems except those
465          * that define DLT_ATM_RFC1483 as 13 - presumably only
466          * BSD/OS does so - so that, on BSD/OS systems, we still
467          * treate 13 as WTAP_ENCAP_ATM_RFC1483, but, on all other
468          * systems, we can read OpenBSD DLT_ENC captures.
469          */
470 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
471         { 13,           WTAP_ENCAP_ATM_RFC1483 },
472 #else
473         { 13,           WTAP_ENCAP_ENC },
474 #endif
475
476         /*
477          * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
478          * don't actually generate it.  I infer that BSD/OS translates
479          * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
480          * libpcap, as the BSD/OS link-layer header is different;
481          * however, in BSD/OS, DLT_PPP_BSDOS is 16.
482          *
483          * From this, I infer that there's no point in handling 14
484          * as DLT_PPP_BSDOS.
485          *
486          * 14 is DLT_RAW on BSD/OS and OpenBSD.
487          */
488         { 14,           WTAP_ENCAP_RAW_IP },
489
490         /*
491          * 15 is:
492          *
493          *      DLT_SLIP_BSDOS on BSD/OS;
494          *
495          *      DLT_HIPPI on NetBSD;
496          *
497          *      DLT_LANE8023 with Alexey Kuznetzov's patches for
498          *      Linux libpcap;
499          *
500          *      DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
501          *      (and on SuSE 6.3);
502          *
503          * but we don't currently handle any of those.
504          */
505
506         /*
507          * 16 is:
508          *
509          *      DLT_PPP_BSDOS on BSD/OS;
510          *
511          *      DLT_HDLC on NetBSD (Cisco HDLC);
512          *
513          *      DLT_CIP with Alexey Kuznetzov's patches for
514          *      Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
515          *
516          *      DLT_I4L_IP with the ISDN4Linux patches for libpcap
517          *      (and on SuSE 6.3).
518          */
519 #if defined(DLT_CIP) && (DLT_CIP == 16)
520         { 16,           WTAP_ENCAP_LINUX_ATM_CLIP },
521 #endif
522 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
523         { 16,           WTAP_ENCAP_CHDLC },
524 #endif
525
526         /*
527          * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
528          * handle it.
529          * It is also used as the PF (Packet Filter) logging format beginning
530          * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
531          * defined with the value 17.
532          */
533 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
534         { 17,           WTAP_ENCAP_OLD_PFLOG },
535 #endif
536
537         /*
538          * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
539          * DLT_CIP of 16 that the Alexey Kuznetzov patches for
540          * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
541          * I've not found any libpcap that uses it for any other purpose -
542          * hopefully nobody will do so in the future.
543          */
544         { 18,           WTAP_ENCAP_LINUX_ATM_CLIP },
545
546         /*
547          * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
548          * recent versions I've seen of the Linux ATM distribution;
549          * I've not yet found any libpcap that uses it for any other
550          * purpose - hopefully nobody will do so in the future.
551          */
552         { 19,           WTAP_ENCAP_LINUX_ATM_CLIP },
553
554         /*
555          * To repeat:
556          *
557          * If you need a new encapsulation type for libpcap files, do
558          * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
559          * add a new encapsulation type by changing an existing entry;
560          * leave the existing entries alone.
561          *
562          * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
563          * for a new DLT_ value, and specifying the purpose of the new value.
564          * When you get the new DLT_ value, use that numerical value in
565          * the "dlt_value" field of "pcap_to_wtap_map[]".
566          */
567 };
568 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
569
570 int
571 wtap_pcap_encap_to_wtap_encap(int encap)
572 {
573         unsigned int i;
574
575         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
576                 if (pcap_to_wtap_map[i].dlt_value == encap)
577                         return pcap_to_wtap_map[i].wtap_encap_value;
578         }
579         return WTAP_ENCAP_UNKNOWN;
580 }
581
582 int
583 wtap_wtap_encap_to_pcap_encap(int encap)
584 {
585         unsigned int i;
586
587         switch (encap) {
588
589         case WTAP_ENCAP_FDDI:
590         case WTAP_ENCAP_FDDI_BITSWAPPED:
591         case WTAP_ENCAP_NETTL_FDDI:
592                 /*
593                  * Special-case WTAP_ENCAP_FDDI and
594                  * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
595                  * to DLT_FDDI (even though that may mean that the bit
596                  * order in the FDDI MAC addresses is wrong; so it goes
597                  * - libpcap format doesn't record the byte order,
598                  * so that's not fixable).
599                  */
600                 return 10;      /* that's DLT_FDDI */
601
602         case WTAP_ENCAP_FRELAY_WITH_PHDR:
603                 /*
604                  * Do the same with Frame Relay.
605                  */
606                 return 107;
607
608         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
609                 /*
610                  * Map this to DLT_IEEE802_11, for now, even though
611                  * that means the radio information will be lost.
612                  * Once tcpdump support for the BSD radiotap header
613                  * is sufficiently widespread, we should probably
614                  * use that, instead - although we should probably
615                  * ultimately just have WTAP_ENCAP_IEEE_802_11
616                  * as the only Wiretap encapsulation for 802.11,
617                  * and have the pseudo-header include a radiotap-style
618                  * list of attributes.  If we do that, though, we
619                  * should probably bypass the regular Wiretap code
620                  * when writing out packets during a capture, and just
621                  * do the equivalent of a libpcap write (unfortunately,
622                  * libpcap doesn't have an "open dump by file descriptor"
623                  * function, so we can't just use "pcap_dump()"), so
624                  * that we don't spend cycles mapping from libpcap to
625                  * Wiretap and then back to libpcap.  (There are other
626                  * reasons to do that, e.g. to handle AIX libpcap better.)
627                  */
628                 return 105;
629         }
630
631         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
632                 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
633                         return pcap_to_wtap_map[i].dlt_value;
634         }
635         return -1;
636 }
637
638 /*
639  * Various pseudo-headers that appear at the beginning of packet data.
640  *
641  * We represent them as sets of offsets, as they might not be aligned on
642  * an appropriate structure boundary in the buffer, and as that makes them
643  * independent of the way the compiler might align fields.
644  */
645
646 /*
647  * The link-layer header on SunATM packets.
648  */
649 #define SUNATM_FLAGS    0       /* destination and traffic type - 1 byte */
650 #define SUNATM_VPI      1       /* VPI - 1 byte */
651 #define SUNATM_VCI      2       /* VCI - 2 bytes */
652 #define SUNATM_LEN      4       /* length of the header */
653
654 /*
655  * The link-layer header on Nokia IPSO ATM packets.
656  */
657 #define NOKIAATM_FLAGS  0       /* destination - 1 byte */
658 #define NOKIAATM_VPI    1       /* VPI - 1 byte */
659 #define NOKIAATM_VCI    2       /* VCI - 2 bytes */
660 #define NOKIAATM_LEN    4       /* length of the header */
661
662 /*
663  * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
664  * to libpcap.
665  */
666 #define IRDA_SLL_PKTTYPE_OFFSET         0       /* packet type - 2 bytes */
667 /* 12 unused bytes */
668 #define IRDA_SLL_PROTOCOL_OFFSET        14      /* protocol, should be ETH_P_LAPD - 2 bytes */
669 #define IRDA_SLL_LEN                    16      /* length of the header */
670
671 /*
672  * A header containing additional MTP information.
673  */
674 #define MTP2_SENT_OFFSET                0       /* 1 byte */
675 #define MTP2_ANNEX_A_USED_OFFSET        1       /* 1 byte */
676 #define MTP2_LINK_NUMBER_OFFSET         2       /* 2 bytes */
677 #define MTP2_HDR_LEN                    4       /* length of the header */
678
679 /*
680  * A header containing additional SITA WAN information.
681  */
682 #define SITA_FLAGS_OFFSET               0       /* 1 byte */
683 #define SITA_SIGNALS_OFFSET             1       /* 1 byte */
684 #define SITA_ERRORS1_OFFSET             2       /* 1 byte */
685 #define SITA_ERRORS2_OFFSET             3       /* 1 byte */
686 #define SITA_PROTO_OFFSET               4       /* 1 byte */
687 #define SITA_HDR_LEN                    5       /* length of the header */
688
689 /*
690  * The fake link-layer header of LAPD packets.
691  */
692 #ifndef ETH_P_LAPD
693 #define ETH_P_LAPD 0x0030
694 #endif
695
696 #define LAPD_SLL_PKTTYPE_OFFSET         0       /* packet type - 2 bytes */
697 #define LAPD_SLL_HATYPE_OFFSET          2       /* hardware address type - 2 bytes */
698 #define LAPD_SLL_HALEN_OFFSET           4       /* hardware address length - 2 bytes */
699 #define LAPD_SLL_ADDR_OFFSET            6       /* address - 8 bytes */
700 #define LAPD_SLL_PROTOCOL_OFFSET        14      /* protocol, should be ETH_P_LAPD - 2 bytes */
701 #define LAPD_SLL_LEN                    16      /* length of the header */
702
703 /*
704  * I2C link-layer on-disk format
705  */
706 struct i2c_file_hdr {
707     guint8 bus;
708     guint8 flags[4];
709 };
710
711 static gboolean
712 pcap_read_sunatm_pseudoheader(FILE_T fh,
713     union wtap_pseudo_header *pseudo_header, int *err)
714 {
715         guint8  atm_phdr[SUNATM_LEN];
716         int     bytes_read;
717         guint8  vpi;
718         guint16 vci;
719
720         errno = WTAP_ERR_CANT_READ;
721         bytes_read = file_read(atm_phdr, 1, SUNATM_LEN, fh);
722         if (bytes_read != SUNATM_LEN) {
723                 *err = file_error(fh);
724                 if (*err == 0)
725                         *err = WTAP_ERR_SHORT_READ;
726                 return FALSE;
727         }
728
729         vpi = atm_phdr[SUNATM_VPI];
730         vci = pntohs(&atm_phdr[SUNATM_VCI]);
731
732         switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
733
734         case 0x01:      /* LANE */
735                 pseudo_header->atm.aal = AAL_5;
736                 pseudo_header->atm.type = TRAF_LANE;
737                 break;
738
739         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
740                 pseudo_header->atm.aal = AAL_5;
741                 pseudo_header->atm.type = TRAF_LLCMX;
742                 break;
743
744         case 0x05:      /* ILMI */
745                 pseudo_header->atm.aal = AAL_5;
746                 pseudo_header->atm.type = TRAF_ILMI;
747                 break;
748
749         case 0x06:      /* Q.2931 */
750                 pseudo_header->atm.aal = AAL_SIGNALLING;
751                 pseudo_header->atm.type = TRAF_UNKNOWN;
752                 break;
753
754         case 0x03:      /* MARS (RFC 2022) */
755                 pseudo_header->atm.aal = AAL_5;
756                 pseudo_header->atm.type = TRAF_UNKNOWN;
757                 break;
758
759         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
760                 pseudo_header->atm.aal = AAL_5;
761                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
762                 break;
763
764         default:
765                 /*
766                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
767                  * case assume it's AAL_SIGNALLING; we know nothing more
768                  * about it.
769                  *
770                  * XXX - is this necessary?  Or are we guaranteed that
771                  * all signalling traffic has a type of 0x06?
772                  *
773                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
774                  * 0x00 ("raw"), might it be non-AAL5 traffic?
775                  */
776                 if (vpi == 0 && vci == 5)
777                         pseudo_header->atm.aal = AAL_SIGNALLING;
778                 else
779                         pseudo_header->atm.aal = AAL_5;
780                 pseudo_header->atm.type = TRAF_UNKNOWN;
781                 break;
782         }
783         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
784
785         pseudo_header->atm.vpi = vpi;
786         pseudo_header->atm.vci = vci;
787         pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
788
789         /* We don't have this information */
790         pseudo_header->atm.flags = 0;
791         pseudo_header->atm.cells = 0;
792         pseudo_header->atm.aal5t_u2u = 0;
793         pseudo_header->atm.aal5t_len = 0;
794         pseudo_header->atm.aal5t_chksum = 0;
795
796         return TRUE;
797 }
798
799 static gboolean
800 pcap_read_nokiaatm_pseudoheader(FILE_T fh,
801     union wtap_pseudo_header *pseudo_header, int *err)
802 {
803         guint8  atm_phdr[NOKIAATM_LEN];
804         int     bytes_read;
805         guint8  vpi;
806         guint16 vci;
807
808         errno = WTAP_ERR_CANT_READ;
809         bytes_read = file_read(atm_phdr, 1, NOKIAATM_LEN, fh);
810         if (bytes_read != NOKIAATM_LEN) {
811                 *err = file_error(fh);
812                 if (*err == 0)
813                         *err = WTAP_ERR_SHORT_READ;
814                 return FALSE;
815         }
816
817         vpi = atm_phdr[NOKIAATM_VPI];
818         vci = pntohs(&atm_phdr[NOKIAATM_VCI]);
819
820         pseudo_header->atm.vpi = vpi;
821         pseudo_header->atm.vci = vci;
822         pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
823
824         /* We don't have this information */
825         pseudo_header->atm.flags = 0;
826         pseudo_header->atm.cells = 0;
827         pseudo_header->atm.aal5t_u2u = 0;
828         pseudo_header->atm.aal5t_len = 0;
829         pseudo_header->atm.aal5t_chksum = 0;
830
831         return TRUE;
832 }
833
834 static gboolean
835 pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
836     int *err, gchar **err_info)
837 {
838         guint8  irda_phdr[IRDA_SLL_LEN];
839         int     bytes_read;
840
841         errno = WTAP_ERR_CANT_READ;
842         bytes_read = file_read(irda_phdr, 1, IRDA_SLL_LEN, fh);
843         if (bytes_read != IRDA_SLL_LEN) {
844                 *err = file_error(fh);
845                 if (*err == 0)
846                         *err = WTAP_ERR_SHORT_READ;
847                 return FALSE;
848         }
849
850         if (pntohs(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
851                 *err = WTAP_ERR_BAD_RECORD;
852                 if (err_info != NULL)
853                         *err_info = g_strdup("libpcap: IrDA capture has a packet with an invalid sll_protocol field");
854                 return FALSE;
855         }
856
857         pseudo_header->irda.pkttype = pntohs(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
858
859         return TRUE;
860 }
861
862 static gboolean
863 pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
864 {
865         guint8 mtp2_hdr[MTP2_HDR_LEN];
866         int    bytes_read;
867
868         errno = WTAP_ERR_CANT_READ;
869         bytes_read = file_read(mtp2_hdr, 1, MTP2_HDR_LEN, fh);
870         if (bytes_read != MTP2_HDR_LEN) {
871                 *err = file_error(fh);
872                 if (*err == 0)
873                         *err = WTAP_ERR_SHORT_READ;
874                 return FALSE;
875         }
876
877         pseudo_header->mtp2.sent         = mtp2_hdr[MTP2_SENT_OFFSET];
878         pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
879         pseudo_header->mtp2.link_number  = pntohs(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
880
881         return TRUE;
882 }
883
884 static gboolean
885 pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
886     int *err, gchar **err_info)
887 {
888         guint8  lapd_phdr[LAPD_SLL_LEN];
889         int     bytes_read;
890
891         errno = WTAP_ERR_CANT_READ;
892         bytes_read = file_read(lapd_phdr, 1, LAPD_SLL_LEN, fh);
893         if (bytes_read != LAPD_SLL_LEN) {
894                 *err = file_error(fh);
895                 if (*err == 0)
896                         *err = WTAP_ERR_SHORT_READ;
897                 return FALSE;
898         }
899
900         if (pntohs(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
901                 *err = WTAP_ERR_BAD_RECORD;
902                 if (err_info != NULL)
903                         *err_info = g_strdup("libpcap: LAPD capture has a packet with an invalid sll_protocol field");
904                 return FALSE;
905         }
906
907         pseudo_header->lapd.pkttype = pntohs(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
908         pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
909
910         return TRUE;
911 }
912
913 static gboolean
914 pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
915 {
916         guint8  sita_phdr[SITA_HDR_LEN];
917         int     bytes_read;
918
919         errno = WTAP_ERR_CANT_READ;
920         bytes_read = file_read(sita_phdr, 1, SITA_HDR_LEN, fh);
921         if (bytes_read != SITA_HDR_LEN) {
922                 *err = file_error(fh);
923                 if (*err == 0)
924                         *err = WTAP_ERR_SHORT_READ;
925                 return FALSE;
926         }
927
928         pseudo_header->sita.flags   = sita_phdr[SITA_FLAGS_OFFSET];
929         pseudo_header->sita.signals = sita_phdr[SITA_SIGNALS_OFFSET];
930         pseudo_header->sita.errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
931         pseudo_header->sita.errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
932         pseudo_header->sita.proto   = sita_phdr[SITA_PROTO_OFFSET];
933
934         return TRUE;
935 }
936
937 /*
938  * When not using the memory-mapped interface to capture USB events,
939  * code that reads those events can use the MON_IOCX_GET ioctl to
940  * read a 48-byte header consisting of a "struct linux_usb_phdr", as
941  * defined below, followed immediately by one of:
942  *
943  *      8 bytes of a "struct usb_device_setup_hdr", if "setup_flag"
944  *      in the preceding "struct linux_usb_phdr" is 0;
945  *
946  *      in Linux 2.6.30 or later, 8 bytes of a "struct iso_rec", if
947  *      this is an isochronous transfer;
948  *
949  *      8 bytes of junk, otherwise.
950  *
951  * In Linux 2.6.31 and later, it can also use the MON_IOCX_GETX ioctl
952  * to read a 64-byte header; that header consists of the 48 bytes
953  * above, followed immediately by 16 bytes of a "struct linux_usb_phdr_ext",
954  * as defined below.
955  *
956  * In Linux 2.6.21 and later, there's a memory-mapped interface to
957  * capture USB events.  In that interface, the events in the memory-mapped
958  * buffer have a 64-byte header, followed immediately by the data.
959  * In Linux 2.6.21 through 2.6.30.x, the 64-byte header is the 48-byte
960  * header described above, followed by 16 bytes of zeroes; in Linux
961  * 2.6.31 and later, the 64-byte header is the 64-byte header described
962  * above.
963  *
964  * See linux/Documentation/usb/usbmon.txt and libpcap/pcap/usb.h for details.
965  *
966  * With WTAP_ENCAP_USB_LINUX, packets have the 48-byte header; with
967  * WTAP_ENCAP_USB_LINUX_MMAPPED, they have the 64-byte header.  There
968  * is no indication of whether the header has the "struct iso_rec", or
969  * whether the last 16 bytes of a 64-byte header are all zeros or are
970  * a "struct linux_usb_phdr_ext".
971  */
972
973 /*
974  * URB transfer_type values
975  */
976 #define URB_ISOCHRONOUS   0x0
977 #define URB_INTERRUPT     0x1
978 #define URB_CONTROL       0x2
979 #define URB_BULK          0x3
980
981 /*
982  * Information from the URB for Isochronous transfers.
983  *
984  * This structure is 8 bytes long.
985  */
986 struct iso_rec {
987     gint32 error_count;
988     gint32 numdesc;
989 };
990
991 /*
992  * Header prepended by Linux kernel to each USB event.
993  *
994  * (Setup flag is '-', 'D', 'Z', or 0.  Data flag is '<', '>', 'Z', or 0.)
995  *
996  * The values are in *host* byte order.
997  */
998 struct linux_usb_phdr {
999     guint64 id;             /* urb id, to link submission and completion events */
1000     guint8 event_type;      /* Submit ('S'), Completed ('C'), Error ('E') */
1001     guint8 transfer_type;   /* ISO (0), Intr, Control, Bulk (3) */
1002     guint8 endpoint_number; /* Endpoint number (0-15) and transfer direction */
1003     guint8 device_address;  /* 0-127 */
1004     guint16 bus_id;
1005     gint8 setup_flag;       /* 0, if the urb setup header is meaningful */
1006     gint8 data_flag;        /* 0, if urb data is present */
1007     gint64 ts_sec;
1008     gint32 ts_usec;
1009     gint32 status;
1010     guint32 urb_len;        /* whole len of urb this event refers to */
1011     guint32 data_len;       /* amount of urb data really present in this event */
1012
1013     /*
1014      * Packet-type-dependent data.
1015      * USB setup information of setup_flag is true.
1016      * Otherwise, some isochronous transfer information.
1017      */
1018     union {
1019         guint8 data[8];
1020         struct iso_rec iso;
1021     } s;
1022
1023     /*
1024      * This data is provided by Linux 2.6.31 and later kernels.
1025      *
1026      * For WTAP_ENCAP_USB_LINUX, it's not in the pseudo-header, so
1027      * the pseudo-header is always 48 bytes long, including the
1028      * packet-type-dependent data.
1029      *
1030      * For WTAP_ENCAP_USB_LINUX_MMAPPED, the pseudo-header is always
1031      * 64 bytes long, with the packet-type-dependent data preceding
1032      * these last 16 bytes.  In pre-2.6.31 kernels, it's zero padding;
1033      * in 2.6.31 and later, it's the following data.
1034      */
1035     gint32 interval;    /* only for Interrupt and Isochronous events */
1036     gint32 start_frame; /* for Isochronous */
1037     guint32 xfer_flags; /* copy of URB's transfer_flags */
1038     guint32 ndesc;      /* actual number of isochronous descriptors */
1039 };
1040
1041 struct linux_usb_isodesc {
1042     gint32 iso_status;
1043     guint32 iso_off;
1044     guint32 iso_len;
1045     guint32 _pad;
1046 };
1047
1048 /*
1049  * USB setup header as defined in USB specification
1050  * See usb_20.pdf, Chapter 9.3 'USB Device Requests' for details.
1051  * http://www.usb.org/developers/docs/usb_20_122909-2.zip
1052  *
1053  * This structure is 8 bytes long.
1054  */
1055 struct usb_device_setup_hdr {
1056     gint8 bmRequestType;
1057     guint8 bRequest;
1058     guint16 wValue;
1059     guint16 wIndex;
1060     guint16 wLength;
1061 };
1062
1063
1064 /*
1065  * Offset of the *end* of a field within a particular structure.
1066  */
1067 #define END_OFFSETOF(basep, fieldp) \
1068         (((char *)(void *)(fieldp)) - ((char *)(void *)(basep)) + \
1069             sizeof(*fieldp))
1070
1071 static void
1072 pcap_process_linux_usb_pseudoheader(guint packet_size, gboolean byte_swapped,
1073     gboolean header_len_64_bytes, guint8 *pd)
1074 {
1075         struct linux_usb_phdr *phdr;
1076         struct linux_usb_isodesc *pisodesc;
1077         gint32 iso_numdesc, i;
1078
1079         if (byte_swapped) {
1080                 phdr = (struct linux_usb_phdr *)pd;
1081
1082                 if (packet_size < END_OFFSETOF(phdr, &phdr->id))
1083                         return;
1084                 PBSWAP64((guint8 *)&phdr->id);
1085                 if (packet_size < END_OFFSETOF(phdr, &phdr->bus_id))
1086                         return;
1087                 PBSWAP16((guint8 *)&phdr->bus_id);
1088                 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_sec))
1089                         return;
1090                 PBSWAP64((guint8 *)&phdr->ts_sec);
1091                 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_usec))
1092                         return;
1093                 PBSWAP32((guint8 *)&phdr->ts_usec);
1094                 if (packet_size < END_OFFSETOF(phdr, &phdr->status))
1095                         return;
1096                 PBSWAP32((guint8 *)&phdr->status);
1097                 if (packet_size < END_OFFSETOF(phdr, &phdr->urb_len))
1098                         return;
1099                 PBSWAP32((guint8 *)&phdr->urb_len);
1100                 if (packet_size < END_OFFSETOF(phdr, &phdr->data_len))
1101                         return;
1102                 PBSWAP32((guint8 *)&phdr->data_len);
1103
1104                 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1105                         if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.error_count))
1106                                 return;
1107                         PBSWAP32((guint8 *)&phdr->s.iso.error_count);
1108
1109                         if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.numdesc))
1110                                 return;
1111                         PBSWAP32((guint8 *)&phdr->s.iso.numdesc);
1112
1113                 }
1114
1115                 if (header_len_64_bytes) {
1116                         /*
1117                          * This is either the "version 1" header, with
1118                          * 16 bytes of additional fields at the end, or
1119                          * a "version 0" header from a memory-mapped
1120                          * capture, with 16 bytes of zeroed-out padding
1121                          * at the end.  Byte swap them as if this were
1122                          * a "version 1" header.
1123                          *
1124                          * Yes, the first argument to END_OFFSETOF() should
1125                          * be phdr, not phdr_ext; we want the offset of
1126                          * the additional fields from the beginning of
1127                          * the packet.
1128                          */
1129                         if (packet_size < END_OFFSETOF(phdr, &phdr->interval))
1130                                 return;
1131                         PBSWAP32((guint8 *)&phdr->interval);
1132                         if (packet_size < END_OFFSETOF(phdr, &phdr->start_frame))
1133                                 return;
1134                         PBSWAP32((guint8 *)&phdr->start_frame);
1135                         if (packet_size < END_OFFSETOF(phdr, &phdr->xfer_flags))
1136                                 return;
1137                         PBSWAP32((guint8 *)&phdr->xfer_flags);
1138                         if (packet_size < END_OFFSETOF(phdr, &phdr->ndesc))
1139                                 return;
1140                         PBSWAP32((guint8 *)&phdr->ndesc);
1141                 }
1142
1143                 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1144                         /* swap the values in struct linux_usb_isodesc */
1145
1146                         if (header_len_64_bytes) {
1147                                 pisodesc = (struct linux_usb_isodesc*)(pd + 64);
1148                         } else {
1149                                 pisodesc = (struct linux_usb_isodesc*)(pd + 48);
1150                         }
1151                         iso_numdesc = phdr->s.iso.numdesc;
1152                         for (i = 0; i < iso_numdesc; i++) {
1153                                 /* always check if we have enough data from the
1154                                  * beginnig of the packet (phdr)
1155                                  */
1156                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_status))
1157                                         return;
1158                                 PBSWAP32((guint8 *)&pisodesc->iso_status);
1159                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_off))
1160                                         return;
1161                                 PBSWAP32((guint8 *)&pisodesc->iso_off);
1162                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_len))
1163                                         return;
1164                                 PBSWAP32((guint8 *)&pisodesc->iso_len);
1165                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->_pad))
1166                                         return;
1167                                 PBSWAP32((guint8 *)&pisodesc->_pad);
1168
1169                                 pisodesc++;
1170                         }
1171                 }
1172         }
1173 }
1174
1175 static gboolean
1176 pcap_read_bt_pseudoheader(FILE_T fh,
1177     union wtap_pseudo_header *pseudo_header, int *err)
1178 {
1179         int     bytes_read;
1180         struct libpcap_bt_phdr phdr;
1181
1182         errno = WTAP_ERR_CANT_READ;
1183         bytes_read = file_read(&phdr, 1,
1184             sizeof (struct libpcap_bt_phdr), fh);
1185         if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
1186                 *err = file_error(fh);
1187                 if (*err == 0)
1188                         *err = WTAP_ERR_SHORT_READ;
1189                 return FALSE;
1190         }
1191         pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
1192         return TRUE;
1193 }
1194
1195 static gboolean
1196 pcap_read_ppp_pseudoheader(FILE_T fh,
1197     union wtap_pseudo_header *pseudo_header, int *err)
1198 {
1199         int     bytes_read;
1200         struct libpcap_ppp_phdr phdr;
1201
1202         errno = WTAP_ERR_CANT_READ;
1203         bytes_read = file_read(&phdr, 1,
1204             sizeof (struct libpcap_ppp_phdr), fh);
1205         if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
1206                 *err = file_error(fh);
1207                 if (*err == 0)
1208                         *err = WTAP_ERR_SHORT_READ;
1209                 return FALSE;
1210         }
1211         pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
1212         return TRUE;
1213 }
1214
1215 static gboolean
1216 pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
1217                               union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
1218 {
1219   guint8 erf_hdr[sizeof(struct erf_phdr)];
1220   int    bytes_read;
1221
1222   errno = WTAP_ERR_CANT_READ;
1223   bytes_read = file_read(erf_hdr, 1, sizeof(struct erf_phdr), fh);
1224   if (bytes_read != sizeof(struct erf_phdr)) {
1225     *err = file_error(fh);
1226     if (*err == 0)
1227       *err = WTAP_ERR_SHORT_READ;
1228     return FALSE;
1229   }
1230   pseudo_header->erf.phdr.ts = pletohll(&erf_hdr[0]); /* timestamp */
1231   pseudo_header->erf.phdr.type =  erf_hdr[8];
1232   pseudo_header->erf.phdr.flags = erf_hdr[9];
1233   pseudo_header->erf.phdr.rlen = pntohs(&erf_hdr[10]);
1234   pseudo_header->erf.phdr.lctr = pntohs(&erf_hdr[12]);
1235   pseudo_header->erf.phdr.wlen = pntohs(&erf_hdr[14]);
1236
1237   /* The high 32 bits of the timestamp contain the integer number of seconds
1238    * while the lower 32 bits contain the binary fraction of the second.
1239    * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1240   if (whdr) {
1241     guint64 ts = pseudo_header->erf.phdr.ts;
1242     whdr->ts.secs = (guint32) (ts >> 32);
1243     ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1244     ts += (ts & 0x80000000) << 1; /* rounding */
1245     whdr->ts.nsecs = ((guint32) (ts >> 32));
1246     if ( whdr->ts.nsecs >= 1000000000) {
1247       whdr->ts.nsecs -= 1000000000;
1248       whdr->ts.secs += 1;
1249     }
1250   }
1251   return TRUE;
1252 }
1253
1254 /*
1255  * If the type of record given in the pseudo header indicate the presence of an extension
1256  * header then, read all the extension headers
1257  */
1258 static gboolean
1259 pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1260                            int *err, gchar **err_info _U_, guint * psize)
1261 {
1262   int bytes_read = 0;
1263   guint8 erf_exhdr[8];
1264   guint64 erf_exhdr_sw;
1265   int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1266   guint8 type;
1267   *psize = 0;
1268   if (pseudo_header->erf.phdr.type & 0x80){
1269     do{
1270       errno = WTAP_ERR_CANT_READ;
1271       bytes_read = file_read(erf_exhdr, 1, 8, fh);
1272       if (bytes_read != 8 ) {
1273         *err = file_error(fh);
1274         if (*err == 0)
1275           *err = WTAP_ERR_SHORT_READ;
1276         return FALSE;
1277       }
1278       type = erf_exhdr[0];
1279       erf_exhdr_sw = pntohll((guint64*) &(erf_exhdr[0]));
1280       if (i < max)
1281         memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1282       *psize += 8;
1283       i++;
1284     } while (type & 0x80);
1285   }
1286   return TRUE;
1287 }
1288
1289 /*
1290  * If the type of record given in the pseudo header indicate the precense of a subheader
1291  * then, read this optional subheader
1292  */
1293 static gboolean
1294 pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1295                            int *err, gchar **err_info _U_, guint * psize)
1296 {
1297   guint8 erf_subhdr[sizeof(union erf_subhdr)];
1298   int    bytes_read;
1299
1300   *psize=0;
1301   switch(pseudo_header->erf.phdr.type & 0x7F) {
1302   case ERF_TYPE_MC_HDLC:
1303   case ERF_TYPE_MC_RAW:
1304   case ERF_TYPE_MC_ATM:
1305   case ERF_TYPE_MC_RAW_CHANNEL:
1306   case ERF_TYPE_MC_AAL5:
1307   case ERF_TYPE_MC_AAL2:
1308   case ERF_TYPE_COLOR_MC_HDLC_POS:
1309     /* Extract the Multi Channel header to include it in the pseudo header part */
1310     errno = WTAP_ERR_CANT_READ;
1311     bytes_read = file_read(erf_subhdr, 1, sizeof(erf_mc_header_t), fh);
1312     if (bytes_read != sizeof(erf_mc_header_t) ) {
1313       *err = file_error(fh);
1314       if (*err == 0)
1315         *err = WTAP_ERR_SHORT_READ;
1316       return FALSE;
1317     }
1318     pseudo_header->erf.subhdr.mc_hdr = pntohl(&erf_subhdr[0]);
1319     *psize = sizeof(erf_mc_header_t);
1320     break;
1321   case ERF_TYPE_ETH:
1322   case ERF_TYPE_COLOR_ETH:
1323   case ERF_TYPE_DSM_COLOR_ETH:
1324     /* Extract the Ethernet additional header to include it in the pseudo header part */
1325     errno = WTAP_ERR_CANT_READ;
1326     bytes_read = file_read(erf_subhdr, 1, sizeof(erf_eth_header_t), fh);
1327     if (bytes_read != sizeof(erf_eth_header_t) ) {
1328       *err = file_error(fh);
1329       if (*err == 0)
1330         *err = WTAP_ERR_SHORT_READ;
1331       return FALSE;
1332     }
1333     pseudo_header->erf.subhdr.eth_hdr = pntohs(&erf_subhdr[0]);
1334     *psize = sizeof(erf_eth_header_t);
1335     break;
1336   default:
1337     /* No optional pseudo header for this ERF type */
1338     break;
1339   }
1340   return TRUE;
1341 }
1342
1343 static gboolean
1344 pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
1345 {
1346         struct i2c_file_hdr i2c_hdr;
1347         int    bytes_read;
1348
1349         errno = WTAP_ERR_CANT_READ;
1350         bytes_read = file_read(&i2c_hdr, 1, sizeof (i2c_hdr), fh);
1351         if (bytes_read != sizeof (i2c_hdr)) {
1352                 *err = file_error(fh);
1353                 if (*err == 0)
1354                         *err = WTAP_ERR_SHORT_READ;
1355                 return FALSE;
1356         }
1357
1358         pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
1359         pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
1360         pseudo_header->i2c.flags = pntohl(&i2c_hdr.flags);
1361
1362         return TRUE;
1363 }
1364
1365 int
1366 pcap_process_pseudo_header(FILE_T fh, int file_type, int wtap_encap,
1367     guint packet_size, gboolean check_packet_size, struct wtap_pkthdr *phdr,
1368     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1369 {
1370         int phdr_len = 0;
1371         guint size;
1372
1373         switch (wtap_encap) {
1374
1375         case WTAP_ENCAP_ATM_PDUS:
1376                 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1377                         /*
1378                          * Nokia IPSO ATM.
1379                          */
1380                         if (check_packet_size && packet_size < NOKIAATM_LEN) {
1381                                 /*
1382                                  * Uh-oh, the packet isn't big enough to even
1383                                  * have a pseudo-header.
1384                                  */
1385                                 *err = WTAP_ERR_BAD_RECORD;
1386                                 *err_info = g_strdup_printf("pcap: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1387                                     packet_size);
1388                                 return -1;
1389                         }
1390                         if (!pcap_read_nokiaatm_pseudoheader(fh,
1391                             pseudo_header, err))
1392                                 return -1;      /* Read error */
1393
1394                         phdr_len = NOKIAATM_LEN;
1395                 } else {
1396                         /*
1397                          * SunATM.
1398                          */
1399                         if (check_packet_size && packet_size < SUNATM_LEN) {
1400                                 /*
1401                                  * Uh-oh, the packet isn't big enough to even
1402                                  * have a pseudo-header.
1403                                  */
1404                                 *err = WTAP_ERR_BAD_RECORD;
1405                                 *err_info = g_strdup_printf("pcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1406                                     packet_size);
1407                                 return -1;
1408                         }
1409                         if (!pcap_read_sunatm_pseudoheader(fh,
1410                             pseudo_header, err))
1411                                 return -1;      /* Read error */
1412
1413                         phdr_len = SUNATM_LEN;
1414                 }
1415                 break;
1416
1417         case WTAP_ENCAP_ETHERNET:
1418                 /*
1419                  * We don't know whether there's an FCS in this frame or not.
1420                  */
1421                 pseudo_header->eth.fcs_len = -1;
1422                 break;
1423
1424         case WTAP_ENCAP_IEEE_802_11:
1425         case WTAP_ENCAP_PRISM_HEADER:
1426         case WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP:
1427         case WTAP_ENCAP_IEEE_802_11_WLAN_AVS:
1428                 /*
1429                  * We don't know whether there's an FCS in this frame or not.
1430                  * XXX - are there any OSes where the capture mechanism
1431                  * supplies an FCS?
1432                  */
1433                 pseudo_header->ieee_802_11.fcs_len = -1;
1434                 pseudo_header->ieee_802_11.channel = 0;
1435                 pseudo_header->ieee_802_11.data_rate = 0;
1436                 pseudo_header->ieee_802_11.signal_level = 0;
1437                 break;
1438
1439         case WTAP_ENCAP_IRDA:
1440                 if (check_packet_size && packet_size < IRDA_SLL_LEN) {
1441                         /*
1442                          * Uh-oh, the packet isn't big enough to even
1443                          * have a pseudo-header.
1444                          */
1445                         *err = WTAP_ERR_BAD_RECORD;
1446                         *err_info = g_strdup_printf("pcap: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1447                             packet_size);
1448                         return -1;
1449                 }
1450                 if (!pcap_read_irda_pseudoheader(fh, pseudo_header,
1451                     err, err_info))
1452                         return -1;      /* Read error */
1453
1454                 phdr_len = IRDA_SLL_LEN;
1455                 break;
1456
1457         case WTAP_ENCAP_MTP2_WITH_PHDR:
1458                 if (check_packet_size && packet_size < MTP2_HDR_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: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1465                             packet_size);
1466                         return -1;
1467                 }
1468                 if (!pcap_read_mtp2_pseudoheader(fh, pseudo_header,
1469                     err, err_info))
1470                         return -1;      /* Read error */
1471
1472                 phdr_len = MTP2_HDR_LEN;
1473                 break;
1474
1475         case WTAP_ENCAP_LINUX_LAPD:
1476                 if (check_packet_size && packet_size < LAPD_SLL_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: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1483                             packet_size);
1484                         return -1;
1485                 }
1486                 if (!pcap_read_lapd_pseudoheader(fh, pseudo_header,
1487                     err, err_info))
1488                         return -1;      /* Read error */
1489
1490                 phdr_len = LAPD_SLL_LEN;
1491                 break;
1492
1493         case WTAP_ENCAP_SITA:
1494                 if (check_packet_size && packet_size < SITA_HDR_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: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1501                             packet_size);
1502                         return -1;
1503                 }
1504                 if (!pcap_read_sita_pseudoheader(fh, pseudo_header,
1505                     err, err_info))
1506                         return -1;      /* Read error */
1507
1508                 phdr_len = SITA_HDR_LEN;
1509                 break;
1510
1511         case WTAP_ENCAP_BLUETOOTH_H4:
1512                 /* We don't have pseudoheader, so just pretend we received everything. */
1513                 pseudo_header->p2p.sent = FALSE;
1514                 break;
1515
1516         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1517                 if (check_packet_size &&
1518                     packet_size < sizeof (struct libpcap_bt_phdr)) {
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: libpcap bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1525                             packet_size);
1526                         return -1;
1527                 }
1528                 if (!pcap_read_bt_pseudoheader(fh,
1529                     pseudo_header, err))
1530                         return -1;      /* Read error */
1531
1532                 phdr_len = (int)sizeof (struct libpcap_bt_phdr);
1533                 break;
1534
1535         case WTAP_ENCAP_PPP_WITH_PHDR:
1536                 if (check_packet_size &&
1537                     packet_size < sizeof (struct libpcap_ppp_phdr)) {
1538                         /*
1539                          * Uh-oh, the packet isn't big enough to even
1540                          * have a pseudo-header.
1541                          */
1542                         *err = WTAP_ERR_BAD_RECORD;
1543                         *err_info = g_strdup_printf("pcap: libpcap ppp file has a %u-byte packet, too small to have even a pseudo-header",
1544                             packet_size);
1545                         return -1;
1546                 }
1547                 if (!pcap_read_ppp_pseudoheader(fh,
1548                     pseudo_header, err))
1549                         return -1;      /* Read error */
1550
1551                 phdr_len = (int)sizeof (struct libpcap_ppp_phdr);
1552                 break;
1553
1554         case WTAP_ENCAP_ERF:
1555                 if (check_packet_size &&
1556                     packet_size < sizeof(struct erf_phdr) ) {
1557                         /*
1558                          * Uh-oh, the packet isn't big enough to even
1559                          * have a pseudo-header.
1560                          */
1561                         *err = WTAP_ERR_BAD_RECORD;
1562                         *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1563                             packet_size);
1564                         return -1;
1565                 }
1566
1567                 if (!pcap_read_erf_pseudoheader(fh, phdr, pseudo_header,
1568                     err, err_info))
1569                         return -1;      /* Read error */
1570
1571                 phdr_len = (int)sizeof(struct erf_phdr);
1572
1573                 /* check the optional Extension header */
1574                 if (!pcap_read_erf_exheader(fh, pseudo_header, err, err_info,
1575                     &size))
1576                         return -1;      /* Read error */
1577
1578                 phdr_len += size;
1579
1580                 /* check the optional Multi Channel header */
1581                 if (!pcap_read_erf_subheader(fh, pseudo_header, err, err_info,
1582                     &size))
1583                         return -1;      /* Read error */
1584
1585                 phdr_len += size;
1586                 break;
1587
1588         case WTAP_ENCAP_I2C:
1589                 if (check_packet_size &&
1590                     packet_size < sizeof (struct i2c_file_hdr)) {
1591                         /*
1592                          * Uh-oh, the packet isn't big enough to even
1593                          * have a pseudo-header.
1594                          */
1595                         *err = WTAP_ERR_BAD_RECORD;
1596                         *err_info = g_strdup_printf("pcap: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1597                             packet_size);
1598                         return -1;
1599                 }
1600                 if (!pcap_read_i2c_pseudoheader(fh, pseudo_header,
1601                     err, err_info))
1602                         return -1;      /* Read error */
1603
1604                 /*
1605                  * Don't count the pseudo-header as part of the packet.
1606                  */
1607                 phdr_len = (int)sizeof (struct i2c_file_hdr);
1608                 break;
1609         }
1610
1611         return phdr_len;
1612 }
1613
1614 void
1615 pcap_read_post_process(int wtap_encap, guint packet_size,
1616     gboolean bytes_swapped, guchar *pd)
1617 {
1618         switch (wtap_encap) {
1619
1620         case WTAP_ENCAP_USB_LINUX:
1621                 pcap_process_linux_usb_pseudoheader(packet_size,
1622                     bytes_swapped, FALSE, pd);
1623                 break;
1624
1625         case WTAP_ENCAP_USB_LINUX_MMAPPED:
1626                 pcap_process_linux_usb_pseudoheader(packet_size,
1627                     bytes_swapped, TRUE, pd);
1628                 break;
1629
1630         default:
1631                 break;
1632         }
1633 }
1634
1635 int
1636 pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
1637 {
1638         int hdrsize;
1639
1640         switch (encap) {
1641
1642         case WTAP_ENCAP_ATM_PDUS:
1643                 hdrsize = SUNATM_LEN;
1644                 break;
1645
1646         case WTAP_ENCAP_IRDA:
1647                 hdrsize = IRDA_SLL_LEN;
1648                 break;
1649
1650         case WTAP_ENCAP_MTP2_WITH_PHDR:
1651                 hdrsize = MTP2_HDR_LEN;
1652                 break;
1653
1654         case WTAP_ENCAP_LINUX_LAPD:
1655                 hdrsize = LAPD_SLL_LEN;
1656                 break;
1657
1658         case WTAP_ENCAP_SITA:
1659                 hdrsize = SITA_HDR_LEN;
1660                 break;
1661
1662         case WTAP_ENCAP_ERF:
1663                 hdrsize = (int)sizeof (struct erf_phdr);
1664                 if (pseudo_header->erf.phdr.type & 0x80)
1665                         hdrsize += 8;
1666                 switch (pseudo_header->erf.phdr.type & 0x7F) {
1667
1668                 case ERF_TYPE_MC_HDLC:
1669                 case ERF_TYPE_MC_RAW:
1670                 case ERF_TYPE_MC_ATM:
1671                 case ERF_TYPE_MC_RAW_CHANNEL:
1672                 case ERF_TYPE_MC_AAL5:
1673                 case ERF_TYPE_MC_AAL2:
1674                 case ERF_TYPE_COLOR_MC_HDLC_POS:
1675                         hdrsize += (int)sizeof(struct erf_mc_hdr);
1676                         break;
1677
1678                 case ERF_TYPE_ETH:
1679                 case ERF_TYPE_COLOR_ETH:
1680                 case ERF_TYPE_DSM_COLOR_ETH:
1681                         hdrsize += (int)sizeof(struct erf_eth_hdr);
1682                         break;
1683
1684                 default:
1685                         break;
1686                 }
1687                 break;
1688
1689         case WTAP_ENCAP_I2C:
1690                 hdrsize = (int)sizeof (struct i2c_file_hdr);
1691                 break;
1692
1693         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1694                 hdrsize = (int)sizeof (struct libpcap_bt_phdr);
1695                 break;
1696
1697         case WTAP_ENCAP_PPP_WITH_PHDR:
1698                 hdrsize = (int)sizeof (struct libpcap_ppp_phdr);
1699                 break;
1700
1701         default:
1702                 hdrsize = 0;
1703                 break;
1704         }
1705
1706         return hdrsize;
1707 }
1708
1709 gboolean
1710 pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
1711     int *err)
1712 {
1713         guint8 atm_hdr[SUNATM_LEN];
1714         guint8 irda_hdr[IRDA_SLL_LEN];
1715         guint8 lapd_hdr[LAPD_SLL_LEN];
1716         guint8 mtp2_hdr[MTP2_HDR_LEN];
1717         guint8 sita_hdr[SITA_HDR_LEN];
1718         guint8 erf_hdr[ sizeof(struct erf_mc_phdr)];
1719         struct i2c_file_hdr i2c_hdr;
1720         struct libpcap_bt_phdr bt_hdr;
1721         struct libpcap_ppp_phdr ppp_hdr;
1722         size_t size;
1723
1724         switch (encap) {
1725
1726         case WTAP_ENCAP_ATM_PDUS:
1727                 /*
1728                  * Write the ATM header.
1729                  */
1730                 atm_hdr[SUNATM_FLAGS] =
1731                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1732                 switch (pseudo_header->atm.aal) {
1733
1734                 case AAL_SIGNALLING:
1735                         /* Q.2931 */
1736                         atm_hdr[SUNATM_FLAGS] |= 0x06;
1737                         break;
1738
1739                 case AAL_5:
1740                         switch (pseudo_header->atm.type) {
1741
1742                         case TRAF_LANE:
1743                                 /* LANE */
1744                                 atm_hdr[SUNATM_FLAGS] |= 0x01;
1745                                 break;
1746
1747                         case TRAF_LLCMX:
1748                                 /* RFC 1483 LLC multiplexed traffic */
1749                                 atm_hdr[SUNATM_FLAGS] |= 0x02;
1750                                 break;
1751
1752                         case TRAF_ILMI:
1753                                 /* ILMI */
1754                                 atm_hdr[SUNATM_FLAGS] |= 0x05;
1755                                 break;
1756                         }
1757                         break;
1758                 }
1759                 atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
1760                 phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1761                 if (!wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr), err))
1762                         return FALSE;
1763                 wdh->bytes_dumped += sizeof(atm_hdr);
1764                 break;
1765
1766         case WTAP_ENCAP_IRDA:
1767                 /*
1768                  * Write the IrDA header.
1769                  */
1770                 memset(irda_hdr, 0, sizeof(irda_hdr));
1771                 phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
1772                     pseudo_header->irda.pkttype);
1773                 phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
1774                 if (!wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr), err))
1775                         return FALSE;
1776                 wdh->bytes_dumped += sizeof(irda_hdr);
1777                 break;
1778
1779         case WTAP_ENCAP_MTP2_WITH_PHDR:
1780                 /*
1781                  * Write the MTP2 header.
1782                  */
1783                 memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
1784                 mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
1785                 mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
1786                 phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
1787                     pseudo_header->mtp2.link_number);
1788                 if (!wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr), err))
1789                         return FALSE;
1790                 wdh->bytes_dumped += sizeof(mtp2_hdr);
1791                 break;
1792
1793         case WTAP_ENCAP_LINUX_LAPD:
1794                 /*
1795                  * Write the LAPD header.
1796                  */
1797                 memset(&lapd_hdr, 0, sizeof(lapd_hdr));
1798                 phtons(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET],
1799                     pseudo_header->lapd.pkttype);
1800                 phtons(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
1801                 lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
1802                     pseudo_header->lapd.we_network?0x01:0x00;
1803                 if (!wtap_dump_file_write(wdh, lapd_hdr, sizeof(lapd_hdr), err))
1804                         return FALSE;
1805                 wdh->bytes_dumped += sizeof(lapd_hdr);
1806                 break;
1807
1808         case WTAP_ENCAP_SITA:
1809                 /*
1810                  * Write the SITA header.
1811                  */
1812                 memset(&sita_hdr, 0, sizeof(sita_hdr));
1813                 sita_hdr[SITA_FLAGS_OFFSET]   = pseudo_header->sita.flags;
1814                 sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.signals;
1815                 sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.errors1;
1816                 sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.errors2;
1817                 sita_hdr[SITA_PROTO_OFFSET]   = pseudo_header->sita.proto;
1818                 if (!wtap_dump_file_write(wdh, sita_hdr, sizeof(sita_hdr), err))
1819                         return FALSE;
1820                 wdh->bytes_dumped += sizeof(sita_hdr);
1821                 break;
1822
1823         case WTAP_ENCAP_ERF:
1824                  /*
1825                  * Write the ERF header.
1826                  */
1827                 memset(&erf_hdr, 0, sizeof(erf_hdr));
1828                 pletonll(&erf_hdr[0], pseudo_header->erf.phdr.ts);
1829                 erf_hdr[8] = pseudo_header->erf.phdr.type;
1830                 erf_hdr[9] = pseudo_header->erf.phdr.flags;
1831                 phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
1832                 phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
1833                 phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
1834                 size = sizeof(struct erf_phdr);
1835
1836                 switch(pseudo_header->erf.phdr.type & 0x7F) {
1837                 case ERF_TYPE_MC_HDLC:
1838                 case ERF_TYPE_MC_RAW:
1839                 case ERF_TYPE_MC_ATM:
1840                 case ERF_TYPE_MC_RAW_CHANNEL:
1841                 case ERF_TYPE_MC_AAL5:
1842                 case ERF_TYPE_MC_AAL2:
1843                 case ERF_TYPE_COLOR_MC_HDLC_POS:
1844                   phtonl(&erf_hdr[16], pseudo_header->erf.subhdr.mc_hdr);
1845                   size += (int)sizeof(struct erf_mc_hdr);
1846                   break;
1847                 case ERF_TYPE_ETH:
1848                 case ERF_TYPE_COLOR_ETH:
1849                 case ERF_TYPE_DSM_COLOR_ETH:
1850                   phtons(&erf_hdr[16], pseudo_header->erf.subhdr.eth_hdr);
1851                   size += (int)sizeof(struct erf_eth_hdr);
1852                   break;
1853                 default:
1854                   break;
1855                 }
1856                 if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
1857                         return FALSE;
1858                 wdh->bytes_dumped += size;
1859                 break;
1860
1861         case WTAP_ENCAP_I2C:
1862                 /*
1863                  * Write the I2C header.
1864                  */
1865                 memset(&i2c_hdr, 0, sizeof(i2c_hdr));
1866                 i2c_hdr.bus = pseudo_header->i2c.bus |
1867                         (pseudo_header->i2c.is_event ? 0x80 : 0x00);
1868                 phtonl((guint8 *)&i2c_hdr.flags, pseudo_header->i2c.flags);
1869                 if (!wtap_dump_file_write(wdh, &i2c_hdr, sizeof(i2c_hdr), err))
1870                         return FALSE;
1871                 wdh->bytes_dumped += sizeof(i2c_hdr);
1872                 break;
1873
1874         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1875                 bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
1876                 if (!wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr, err))
1877                         return FALSE;
1878                 wdh->bytes_dumped += sizeof bt_hdr;
1879                 break;
1880
1881         case WTAP_ENCAP_PPP_WITH_PHDR:
1882                 ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
1883                 if (!wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr, err))
1884                         return FALSE;
1885                 wdh->bytes_dumped += sizeof ppp_hdr;
1886                 break;
1887         }
1888         return TRUE;
1889 }