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