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