wiretap: start using <wsutil/pint.h>
[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
421         /*
422          * To repeat:
423          *
424          * If you need a new encapsulation type for libpcap files, do
425          * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
426          * add a new encapsulation type by changing an existing entry;
427          * leave the existing entries alone.
428          *
429          * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
430          * for a new DLT_ value, and specifying the purpose of the new value.
431          * When you get the new DLT_ value, use that numerical value in
432          * the "linktype_value" field of "pcap_to_wtap_map[]".
433          */
434
435         /*
436          * The following are entries for libpcap type values that have
437          * different meanings on different OSes.  I.e., these are DLT_
438          * values that are different on different OSes, and that have
439          * a separate LINKTYPE_ value assigned to them.
440          *
441          * We put these *after* the entries for the LINKTYPE_ values for
442          * those Wiretap encapsulation types, so that, when writing a
443          * pcap or pcap-ng file, Wireshark writes the LINKTYPE_ value,
444          * not the OS's DLT_ value, as the file's link-layer header type
445          * for pcap or the interface's link-layer header type.
446          */
447
448         /*
449          * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
450          * seen that define anything other than DLT_ATM_RFC1483 as 11 are
451          * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
452          * which define it as 100, mapping the kernel's value to 100, in
453          * an attempt to hide the different values used on different
454          * platforms.
455          *
456          * If this is a platform where DLT_FR is defined as 11, we
457          * don't handle 11 at all; otherwise, we handle it as
458          * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
459          * captures from BSD/OS if running on platforms other than BSD/OS,
460          * but
461          *
462          *      1) we don't yet support DLT_FR
463          *
464          * and
465          *
466          *      2) nothing short of a heuristic would let us interpret
467          *         them correctly).
468          */
469 #if defined(DLT_FR) && (DLT_FR == 11)
470         { 11,           WTAP_ENCAP_FRELAY },
471 #else
472         { 11,           WTAP_ENCAP_ATM_RFC1483 },
473 #endif
474
475         /*
476          * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
477          * BSD/OS, and DLT_LOOP on OpenBSD.
478          *
479          * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
480          * (it's just like DLT_NULL, only with the AF_ value in network
481          * rather than host byte order - Wireshark figures out the
482          * byte order from the data, so we don't care what byte order
483          * it's in), so if DLT_LOOP is defined as 12, interpret 12
484          * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
485          * as 12, interpret it as WTAP_ENCAP_RAW_IP.
486          */
487 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
488         { 12,           WTAP_ENCAP_NULL },
489 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
490         /*
491          * Put entry for Cisco HDLC here.
492          * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
493          * start with a 4-byte Cisco HDLC header?
494          */
495 #else
496         { 12,           WTAP_ENCAP_RAW_IP },
497 #endif
498
499         /*
500          * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
501          * don't actually generate it.  I infer that BSD/OS translates
502          * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
503          * libpcap, as the BSD/OS link-layer header is different;
504          * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
505          *
506          * From this, I infer that there's no point in handling 13
507          * as DLT_SLIP_BSDOS.
508          *
509          * 13 is DLT_ATM_RFC1483 on BSD/OS.
510          *
511          * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
512          * of decrypted IPsec traffic.
513          *
514          * We treat 13 as WTAP_ENCAP_ENC on all systems except those
515          * that define DLT_ATM_RFC1483 as 13 - presumably only
516          * BSD/OS does so - so that, on BSD/OS systems, we still
517          * treate 13 as WTAP_ENCAP_ATM_RFC1483, but, on all other
518          * systems, we can read OpenBSD DLT_ENC captures.
519          */
520 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
521         { 13,           WTAP_ENCAP_ATM_RFC1483 },
522 #else
523         { 13,           WTAP_ENCAP_ENC },
524 #endif
525
526         /*
527          * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
528          * don't actually generate it.  I infer that BSD/OS translates
529          * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
530          * libpcap, as the BSD/OS link-layer header is different;
531          * however, in BSD/OS, DLT_PPP_BSDOS is 16.
532          *
533          * From this, I infer that there's no point in handling 14
534          * as DLT_PPP_BSDOS.
535          *
536          * 14 is DLT_RAW on BSD/OS and OpenBSD.
537          */
538         { 14,           WTAP_ENCAP_RAW_IP },
539
540         /*
541          * 15 is:
542          *
543          *      DLT_SLIP_BSDOS on BSD/OS;
544          *
545          *      DLT_HIPPI on NetBSD;
546          *
547          *      DLT_LANE8023 with Alexey Kuznetzov's patches for
548          *      Linux libpcap;
549          *
550          *      DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
551          *      (and on SuSE 6.3);
552          *
553          * but we don't currently handle any of those.
554          */
555
556         /*
557          * 16 is:
558          *
559          *      DLT_PPP_BSDOS on BSD/OS;
560          *
561          *      DLT_HDLC on NetBSD (Cisco HDLC);
562          *
563          *      DLT_CIP with Alexey Kuznetzov's patches for
564          *      Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
565          *
566          *      DLT_I4L_IP with the ISDN4Linux patches for libpcap
567          *      (and on SuSE 6.3).
568          */
569 #if defined(DLT_CIP) && (DLT_CIP == 16)
570         { 16,           WTAP_ENCAP_LINUX_ATM_CLIP },
571 #endif
572 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
573         { 16,           WTAP_ENCAP_CHDLC },
574 #endif
575
576         /*
577          * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
578          * handle it.
579          * It is also used as the PF (Packet Filter) logging format beginning
580          * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
581          * defined with the value 17.
582          */
583 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
584         { 17,           WTAP_ENCAP_OLD_PFLOG },
585 #endif
586
587         /*
588          * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
589          * DLT_CIP of 16 that the Alexey Kuznetzov patches for
590          * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
591          * I've not found any libpcap that uses it for any other purpose -
592          * hopefully nobody will do so in the future.
593          */
594         { 18,           WTAP_ENCAP_LINUX_ATM_CLIP },
595
596         /*
597          * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
598          * recent versions I've seen of the Linux ATM distribution;
599          * I've not yet found any libpcap that uses it for any other
600          * purpose - hopefully nobody will do so in the future.
601          */
602         { 19,           WTAP_ENCAP_LINUX_ATM_CLIP },
603
604         /*
605          * To repeat:
606          *
607          * If you need a new encapsulation type for libpcap files, do
608          * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
609          * add a new encapsulation type by changing an existing entry;
610          * leave the existing entries alone.
611          *
612          * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
613          * for a new DLT_ value, and specifying the purpose of the new value.
614          * When you get the new DLT_ value, use that numerical value in
615          * the "linktype_value" field of "pcap_to_wtap_map[]".
616          */
617 };
618 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
619
620 int
621 wtap_pcap_encap_to_wtap_encap(int encap)
622 {
623         unsigned int i;
624
625         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
626                 if (pcap_to_wtap_map[i].linktype_value == encap)
627                         return pcap_to_wtap_map[i].wtap_encap_value;
628         }
629         return WTAP_ENCAP_UNKNOWN;
630 }
631
632 int
633 wtap_wtap_encap_to_pcap_encap(int encap)
634 {
635         unsigned int i;
636
637         switch (encap) {
638
639         case WTAP_ENCAP_FDDI:
640         case WTAP_ENCAP_FDDI_BITSWAPPED:
641                 /*
642                  * Special-case WTAP_ENCAP_FDDI and
643                  * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
644                  * to DLT_FDDI (even though that may mean that the bit
645                  * order in the FDDI MAC addresses is wrong; so it goes
646                  * - libpcap format doesn't record the byte order,
647                  * so that's not fixable).
648                  *
649                  * The pcap_to_wtap_map[] table will only have an
650                  * entry for one of the above, which is why we have
651                  * to special-case them.
652                  */
653                 return 10;      /* that's DLT_FDDI */
654
655         case WTAP_ENCAP_NETTL_FDDI:
656                 /*
657                  * This will discard the nettl information, as that's
658                  * in the pseudo-header.
659                  *
660                  * XXX - what about Ethernet and Token Ring?
661                  */
662                 return 10;      /* that's DLT_FDDI */
663
664         case WTAP_ENCAP_FRELAY_WITH_PHDR:
665                 /*
666                  * This will discard the pseudo-header information.
667                  */
668                 return 107;
669
670         case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
671                 /*
672                  * Map this to DLT_IEEE802_11, for now, even though
673                  * that means the radio information will be lost.
674                  * We should try to map those values to radiotap
675                  * values and write this out as a radiotap file,
676                  * if possible.
677                  */
678                 return 105;
679         }
680
681         for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
682                 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
683                         return pcap_to_wtap_map[i].linktype_value;
684         }
685         return -1;
686 }
687
688 gboolean
689 wtap_encap_requires_phdr(int encap) {
690         if (
691                 (encap == WTAP_ENCAP_ATM_PDUS) ||
692                 (encap == WTAP_ENCAP_IRDA) ||
693                 (encap == WTAP_ENCAP_MTP2_WITH_PHDR) ||
694                 (encap == WTAP_ENCAP_LINUX_LAPD) ||
695                 (encap == WTAP_ENCAP_SITA) ||
696                 (encap == WTAP_ENCAP_ERF) ||
697                 (encap == WTAP_ENCAP_I2C) ||
698                 (encap == WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR) ||
699                 (encap == WTAP_ENCAP_PPP_WITH_PHDR)
700         ) {
701                 return TRUE;
702         }
703         return FALSE;
704 }
705
706
707 /*
708  * Various pseudo-headers that appear at the beginning of packet data.
709  *
710  * We represent them as sets of offsets, as they might not be aligned on
711  * an appropriate structure boundary in the buffer, and as that makes them
712  * independent of the way the compiler might align fields.
713  */
714
715 /*
716  * The link-layer header on SunATM packets.
717  */
718 #define SUNATM_FLAGS    0       /* destination and traffic type - 1 byte */
719 #define SUNATM_VPI      1       /* VPI - 1 byte */
720 #define SUNATM_VCI      2       /* VCI - 2 bytes */
721 #define SUNATM_LEN      4       /* length of the header */
722
723 /*
724  * The link-layer header on Nokia IPSO ATM packets.
725  */
726 #define NOKIAATM_FLAGS  0       /* destination - 1 byte */
727 #define NOKIAATM_VPI    1       /* VPI - 1 byte */
728 #define NOKIAATM_VCI    2       /* VCI - 2 bytes */
729 #define NOKIAATM_LEN    4       /* length of the header */
730
731 /*
732  * The link-layer header on Nokia IPSO packets.
733  */
734 #define NOKIA_LEN       4       /* length of the header */
735
736 /*
737  * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
738  * to libpcap.
739  */
740 #define IRDA_SLL_PKTTYPE_OFFSET         0       /* packet type - 2 bytes */
741 /* 12 unused bytes */
742 #define IRDA_SLL_PROTOCOL_OFFSET        14      /* protocol, should be ETH_P_LAPD - 2 bytes */
743 #define IRDA_SLL_LEN                    16      /* length of the header */
744
745 /*
746  * A header containing additional MTP information.
747  */
748 #define MTP2_SENT_OFFSET                0       /* 1 byte */
749 #define MTP2_ANNEX_A_USED_OFFSET        1       /* 1 byte */
750 #define MTP2_LINK_NUMBER_OFFSET         2       /* 2 bytes */
751 #define MTP2_HDR_LEN                    4       /* length of the header */
752
753 /*
754  * A header containing additional SITA WAN information.
755  */
756 #define SITA_FLAGS_OFFSET               0       /* 1 byte */
757 #define SITA_SIGNALS_OFFSET             1       /* 1 byte */
758 #define SITA_ERRORS1_OFFSET             2       /* 1 byte */
759 #define SITA_ERRORS2_OFFSET             3       /* 1 byte */
760 #define SITA_PROTO_OFFSET               4       /* 1 byte */
761 #define SITA_HDR_LEN                    5       /* length of the header */
762
763 /*
764  * The fake link-layer header of LAPD packets.
765  */
766 #ifndef ETH_P_LAPD
767 #define ETH_P_LAPD 0x0030
768 #endif
769
770 #define LAPD_SLL_PKTTYPE_OFFSET         0       /* packet type - 2 bytes */
771 #define LAPD_SLL_HATYPE_OFFSET          2       /* hardware address type - 2 bytes */
772 #define LAPD_SLL_HALEN_OFFSET           4       /* hardware address length - 2 bytes */
773 #define LAPD_SLL_ADDR_OFFSET            6       /* address - 8 bytes */
774 #define LAPD_SLL_PROTOCOL_OFFSET        14      /* protocol, should be ETH_P_LAPD - 2 bytes */
775 #define LAPD_SLL_LEN                    16      /* length of the header */
776
777 /*
778  * The NFC LLCP per-packet header.
779  */
780 #define LLCP_ADAPTER_OFFSET             0
781 #define LLCP_FLAGS_OFFSET               1
782 #define LLCP_HEADER_LEN                 2
783
784 /*
785  * I2C link-layer on-disk format
786  */
787 struct i2c_file_hdr {
788     guint8 bus;
789     guint8 flags[4];
790 };
791
792 static gboolean
793 pcap_read_sunatm_pseudoheader(FILE_T fh,
794     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
795 {
796         guint8  atm_phdr[SUNATM_LEN];
797         int     bytes_read;
798         guint8  vpi;
799         guint16 vci;
800
801         errno = WTAP_ERR_CANT_READ;
802         bytes_read = file_read(atm_phdr, SUNATM_LEN, fh);
803         if (bytes_read != SUNATM_LEN) {
804                 *err = file_error(fh, err_info);
805                 if (*err == 0)
806                         *err = WTAP_ERR_SHORT_READ;
807                 return FALSE;
808         }
809
810         vpi = atm_phdr[SUNATM_VPI];
811         vci = pntoh16(&atm_phdr[SUNATM_VCI]);
812
813         switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
814
815         case 0x01:      /* LANE */
816                 pseudo_header->atm.aal = AAL_5;
817                 pseudo_header->atm.type = TRAF_LANE;
818                 break;
819
820         case 0x02:      /* RFC 1483 LLC multiplexed traffic */
821                 pseudo_header->atm.aal = AAL_5;
822                 pseudo_header->atm.type = TRAF_LLCMX;
823                 break;
824
825         case 0x05:      /* ILMI */
826                 pseudo_header->atm.aal = AAL_5;
827                 pseudo_header->atm.type = TRAF_ILMI;
828                 break;
829
830         case 0x06:      /* Q.2931 */
831                 pseudo_header->atm.aal = AAL_SIGNALLING;
832                 pseudo_header->atm.type = TRAF_UNKNOWN;
833                 break;
834
835         case 0x03:      /* MARS (RFC 2022) */
836                 pseudo_header->atm.aal = AAL_5;
837                 pseudo_header->atm.type = TRAF_UNKNOWN;
838                 break;
839
840         case 0x04:      /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
841                 pseudo_header->atm.aal = AAL_5;
842                 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
843                 break;
844
845         default:
846                 /*
847                  * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
848                  * case assume it's AAL_SIGNALLING; we know nothing more
849                  * about it.
850                  *
851                  * XXX - is this necessary?  Or are we guaranteed that
852                  * all signalling traffic has a type of 0x06?
853                  *
854                  * XXX - is this guaranteed to be AAL5?  Or, if the type is
855                  * 0x00 ("raw"), might it be non-AAL5 traffic?
856                  */
857                 if (vpi == 0 && vci == 5)
858                         pseudo_header->atm.aal = AAL_SIGNALLING;
859                 else
860                         pseudo_header->atm.aal = AAL_5;
861                 pseudo_header->atm.type = TRAF_UNKNOWN;
862                 break;
863         }
864         pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
865
866         pseudo_header->atm.vpi = vpi;
867         pseudo_header->atm.vci = vci;
868         pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
869
870         /* We don't have this information */
871         pseudo_header->atm.flags = 0;
872         pseudo_header->atm.cells = 0;
873         pseudo_header->atm.aal5t_u2u = 0;
874         pseudo_header->atm.aal5t_len = 0;
875         pseudo_header->atm.aal5t_chksum = 0;
876
877         return TRUE;
878 }
879
880 static gboolean
881 pcap_read_nokiaatm_pseudoheader(FILE_T fh,
882     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
883 {
884         guint8  atm_phdr[NOKIAATM_LEN];
885         int     bytes_read;
886         guint8  vpi;
887         guint16 vci;
888
889         errno = WTAP_ERR_CANT_READ;
890         bytes_read = file_read(atm_phdr, NOKIAATM_LEN, fh);
891         if (bytes_read != NOKIAATM_LEN) {
892                 *err = file_error(fh, err_info);
893                 if (*err == 0)
894                         *err = WTAP_ERR_SHORT_READ;
895                 return FALSE;
896         }
897
898         vpi = atm_phdr[NOKIAATM_VPI];
899         vci = pntoh16(&atm_phdr[NOKIAATM_VCI]);
900
901         pseudo_header->atm.vpi = vpi;
902         pseudo_header->atm.vci = vci;
903         pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
904
905         /* We don't have this information */
906         pseudo_header->atm.flags = 0;
907         pseudo_header->atm.cells = 0;
908         pseudo_header->atm.aal5t_u2u = 0;
909         pseudo_header->atm.aal5t_len = 0;
910         pseudo_header->atm.aal5t_chksum = 0;
911
912         return TRUE;
913 }
914
915 static gboolean
916 pcap_read_nokia_pseudoheader(FILE_T fh,
917     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
918 {
919         guint8  phdr[NOKIA_LEN];
920         int     bytes_read;
921
922         errno = WTAP_ERR_CANT_READ;
923
924         /* backtrack to read the 4 mysterious bytes that aren't considered
925         * part of the packet size
926         */
927         if (file_seek(fh, -NOKIA_LEN, SEEK_CUR, err) == -1)
928         {
929                 *err = file_error(fh, err_info);
930                 if (*err == 0)
931                         *err = WTAP_ERR_SHORT_READ;
932                 return FALSE;
933         }
934
935         bytes_read = file_read(phdr, NOKIA_LEN, fh);
936         if (bytes_read != NOKIA_LEN) {
937                 *err = file_error(fh, err_info);
938                 if (*err == 0)
939                         *err = WTAP_ERR_SHORT_READ;
940                 return FALSE;
941         }
942
943         memcpy(pseudo_header->nokia.stuff, phdr, NOKIA_LEN);
944
945         return TRUE;
946 }
947
948 static gboolean
949 pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
950     int *err, gchar **err_info)
951 {
952         guint8  irda_phdr[IRDA_SLL_LEN];
953         int     bytes_read;
954
955         errno = WTAP_ERR_CANT_READ;
956         bytes_read = file_read(irda_phdr, IRDA_SLL_LEN, fh);
957         if (bytes_read != IRDA_SLL_LEN) {
958                 *err = file_error(fh, err_info);
959                 if (*err == 0)
960                         *err = WTAP_ERR_SHORT_READ;
961                 return FALSE;
962         }
963
964         if (pntoh16(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
965                 *err = WTAP_ERR_BAD_FILE;
966                 if (err_info != NULL)
967                         *err_info = g_strdup("libpcap: IrDA capture has a packet with an invalid sll_protocol field");
968                 return FALSE;
969         }
970
971         pseudo_header->irda.pkttype = pntoh16(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
972
973         return TRUE;
974 }
975
976 static gboolean
977 pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
978 {
979         guint8 mtp2_hdr[MTP2_HDR_LEN];
980         int    bytes_read;
981
982         errno = WTAP_ERR_CANT_READ;
983         bytes_read = file_read(mtp2_hdr, MTP2_HDR_LEN, fh);
984         if (bytes_read != MTP2_HDR_LEN) {
985                 *err = file_error(fh, err_info);
986                 if (*err == 0)
987                         *err = WTAP_ERR_SHORT_READ;
988                 return FALSE;
989         }
990
991         pseudo_header->mtp2.sent         = mtp2_hdr[MTP2_SENT_OFFSET];
992         pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
993         pseudo_header->mtp2.link_number  = pntoh16(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
994
995         return TRUE;
996 }
997
998 static gboolean
999 pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1000     int *err, gchar **err_info)
1001 {
1002         guint8  lapd_phdr[LAPD_SLL_LEN];
1003         int     bytes_read;
1004
1005         errno = WTAP_ERR_CANT_READ;
1006         bytes_read = file_read(lapd_phdr, LAPD_SLL_LEN, fh);
1007         if (bytes_read != LAPD_SLL_LEN) {
1008                 *err = file_error(fh, err_info);
1009                 if (*err == 0)
1010                         *err = WTAP_ERR_SHORT_READ;
1011                 return FALSE;
1012         }
1013
1014         if (pntoh16(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
1015                 *err = WTAP_ERR_BAD_FILE;
1016                 if (err_info != NULL)
1017                         *err_info = g_strdup("libpcap: LAPD capture has a packet with an invalid sll_protocol field");
1018                 return FALSE;
1019         }
1020
1021         pseudo_header->lapd.pkttype = pntoh16(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
1022         pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
1023
1024         return TRUE;
1025 }
1026
1027 static gboolean
1028 pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1029 {
1030         guint8  sita_phdr[SITA_HDR_LEN];
1031         int     bytes_read;
1032
1033         errno = WTAP_ERR_CANT_READ;
1034         bytes_read = file_read(sita_phdr, SITA_HDR_LEN, fh);
1035         if (bytes_read != SITA_HDR_LEN) {
1036                 *err = file_error(fh, err_info);
1037                 if (*err == 0)
1038                         *err = WTAP_ERR_SHORT_READ;
1039                 return FALSE;
1040         }
1041
1042         pseudo_header->sita.sita_flags   = sita_phdr[SITA_FLAGS_OFFSET];
1043         pseudo_header->sita.sita_signals = sita_phdr[SITA_SIGNALS_OFFSET];
1044         pseudo_header->sita.sita_errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
1045         pseudo_header->sita.sita_errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
1046         pseudo_header->sita.sita_proto   = sita_phdr[SITA_PROTO_OFFSET];
1047
1048         return TRUE;
1049 }
1050
1051 /*
1052  * When not using the memory-mapped interface to capture USB events,
1053  * code that reads those events can use the MON_IOCX_GET ioctl to
1054  * read a 48-byte header consisting of a "struct linux_usb_phdr", as
1055  * defined below, followed immediately by one of:
1056  *
1057  *      8 bytes of a "struct usb_device_setup_hdr", if "setup_flag"
1058  *      in the preceding "struct linux_usb_phdr" is 0;
1059  *
1060  *      in Linux 2.6.30 or later, 8 bytes of a "struct iso_rec", if
1061  *      this is an isochronous transfer;
1062  *
1063  *      8 bytes of junk, otherwise.
1064  *
1065  * In Linux 2.6.31 and later, it can also use the MON_IOCX_GETX ioctl
1066  * to read a 64-byte header; that header consists of the 48 bytes
1067  * above, followed immediately by 16 bytes of a "struct linux_usb_phdr_ext",
1068  * as defined below.
1069  *
1070  * In Linux 2.6.21 and later, there's a memory-mapped interface to
1071  * capture USB events.  In that interface, the events in the memory-mapped
1072  * buffer have a 64-byte header, followed immediately by the data.
1073  * In Linux 2.6.21 through 2.6.30.x, the 64-byte header is the 48-byte
1074  * header described above, followed by 16 bytes of zeroes; in Linux
1075  * 2.6.31 and later, the 64-byte header is the 64-byte header described
1076  * above.
1077  *
1078  * See linux/Documentation/usb/usbmon.txt and libpcap/pcap/usb.h for details.
1079  *
1080  * With WTAP_ENCAP_USB_LINUX, packets have the 48-byte header; with
1081  * WTAP_ENCAP_USB_LINUX_MMAPPED, they have the 64-byte header.  There
1082  * is no indication of whether the header has the "struct iso_rec", or
1083  * whether the last 16 bytes of a 64-byte header are all zeros or are
1084  * a "struct linux_usb_phdr_ext".
1085  */
1086
1087 /*
1088  * URB transfer_type values
1089  */
1090 #define URB_ISOCHRONOUS   0x0
1091 #define URB_INTERRUPT     0x1
1092 #define URB_CONTROL       0x2
1093 #define URB_BULK          0x3
1094
1095 /*
1096  * Information from the URB for Isochronous transfers.
1097  *
1098  * This structure is 8 bytes long.
1099  */
1100 struct iso_rec {
1101     gint32 error_count;
1102     gint32 numdesc;
1103 };
1104
1105 /*
1106  * Header prepended by Linux kernel to each USB event.
1107  *
1108  * (Setup flag is '-', 'D', 'Z', or 0.  Data flag is '<', '>', 'Z', or 0.)
1109  *
1110  * The values are in *host* byte order.
1111  */
1112 struct linux_usb_phdr {
1113     guint64 id;             /* urb id, to link submission and completion events */
1114     guint8 event_type;      /* Submit ('S'), Completed ('C'), Error ('E') */
1115     guint8 transfer_type;   /* ISO (0), Intr, Control, Bulk (3) */
1116     guint8 endpoint_number; /* Endpoint number (0-15) and transfer direction */
1117     guint8 device_address;  /* 0-127 */
1118     guint16 bus_id;
1119     gint8 setup_flag;       /* 0, if the urb setup header is meaningful */
1120     gint8 data_flag;        /* 0, if urb data is present */
1121     gint64 ts_sec;
1122     gint32 ts_usec;
1123     gint32 status;
1124     guint32 urb_len;        /* whole len of urb this event refers to */
1125     guint32 data_len;       /* amount of urb data really present in this event */
1126
1127     /*
1128      * Packet-type-dependent data.
1129      * USB setup information of setup_flag is true.
1130      * Otherwise, some isochronous transfer information.
1131      */
1132     union {
1133         guint8 data[8];
1134         struct iso_rec iso;
1135     } s;
1136
1137     /*
1138      * This data is provided by Linux 2.6.31 and later kernels.
1139      *
1140      * For WTAP_ENCAP_USB_LINUX, it's not in the pseudo-header, so
1141      * the pseudo-header is always 48 bytes long, including the
1142      * packet-type-dependent data.
1143      *
1144      * For WTAP_ENCAP_USB_LINUX_MMAPPED, the pseudo-header is always
1145      * 64 bytes long, with the packet-type-dependent data preceding
1146      * these last 16 bytes.  In pre-2.6.31 kernels, it's zero padding;
1147      * in 2.6.31 and later, it's the following data.
1148      */
1149     gint32 interval;    /* only for Interrupt and Isochronous events */
1150     gint32 start_frame; /* for Isochronous */
1151     guint32 xfer_flags; /* copy of URB's transfer_flags */
1152     guint32 ndesc;      /* actual number of isochronous descriptors */
1153 };
1154
1155 struct linux_usb_isodesc {
1156     gint32 iso_status;
1157     guint32 iso_off;
1158     guint32 iso_len;
1159     guint32 _pad;
1160 };
1161
1162 /*
1163  * USB setup header as defined in USB specification
1164  * See usb_20.pdf, Chapter 9.3 'USB Device Requests' for details.
1165  * http://www.usb.org/developers/docs/usb_20_122909-2.zip
1166  *
1167  * This structure is 8 bytes long.
1168  */
1169 struct usb_device_setup_hdr {
1170     gint8 bmRequestType;
1171     guint8 bRequest;
1172     guint16 wValue;
1173     guint16 wIndex;
1174     guint16 wLength;
1175 };
1176
1177
1178 /*
1179  * Offset of the *end* of a field within a particular structure.
1180  */
1181 #define END_OFFSETOF(basep, fieldp) \
1182         (((char *)(void *)(fieldp)) - ((char *)(void *)(basep)) + \
1183             sizeof(*fieldp))
1184
1185 static void
1186 pcap_process_linux_usb_pseudoheader(guint packet_size, gboolean byte_swapped,
1187     gboolean header_len_64_bytes, guint8 *pd)
1188 {
1189         struct linux_usb_phdr *phdr;
1190         struct linux_usb_isodesc *pisodesc;
1191         gint32 iso_numdesc, i;
1192
1193         if (byte_swapped) {
1194                 /*
1195                  * Greasy hack, but we never directly direference any of
1196                  * the fields in *phdr, we just get offsets of and
1197                  * addresses of its members, so it's safe.
1198                  */
1199                 phdr = (struct linux_usb_phdr *)(void *)pd;
1200
1201                 if (packet_size < END_OFFSETOF(phdr, &phdr->id))
1202                         return;
1203                 PBSWAP64((guint8 *)&phdr->id);
1204                 if (packet_size < END_OFFSETOF(phdr, &phdr->bus_id))
1205                         return;
1206                 PBSWAP16((guint8 *)&phdr->bus_id);
1207                 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_sec))
1208                         return;
1209                 PBSWAP64((guint8 *)&phdr->ts_sec);
1210                 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_usec))
1211                         return;
1212                 PBSWAP32((guint8 *)&phdr->ts_usec);
1213                 if (packet_size < END_OFFSETOF(phdr, &phdr->status))
1214                         return;
1215                 PBSWAP32((guint8 *)&phdr->status);
1216                 if (packet_size < END_OFFSETOF(phdr, &phdr->urb_len))
1217                         return;
1218                 PBSWAP32((guint8 *)&phdr->urb_len);
1219                 if (packet_size < END_OFFSETOF(phdr, &phdr->data_len))
1220                         return;
1221                 PBSWAP32((guint8 *)&phdr->data_len);
1222
1223                 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1224                         if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.error_count))
1225                                 return;
1226                         PBSWAP32((guint8 *)&phdr->s.iso.error_count);
1227
1228                         if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.numdesc))
1229                                 return;
1230                         PBSWAP32((guint8 *)&phdr->s.iso.numdesc);
1231
1232                 }
1233
1234                 if (header_len_64_bytes) {
1235                         /*
1236                          * This is either the "version 1" header, with
1237                          * 16 bytes of additional fields at the end, or
1238                          * a "version 0" header from a memory-mapped
1239                          * capture, with 16 bytes of zeroed-out padding
1240                          * at the end.  Byte swap them as if this were
1241                          * a "version 1" header.
1242                          *
1243                          * Yes, the first argument to END_OFFSETOF() should
1244                          * be phdr, not phdr_ext; we want the offset of
1245                          * the additional fields from the beginning of
1246                          * the packet.
1247                          */
1248                         if (packet_size < END_OFFSETOF(phdr, &phdr->interval))
1249                                 return;
1250                         PBSWAP32((guint8 *)&phdr->interval);
1251                         if (packet_size < END_OFFSETOF(phdr, &phdr->start_frame))
1252                                 return;
1253                         PBSWAP32((guint8 *)&phdr->start_frame);
1254                         if (packet_size < END_OFFSETOF(phdr, &phdr->xfer_flags))
1255                                 return;
1256                         PBSWAP32((guint8 *)&phdr->xfer_flags);
1257                         if (packet_size < END_OFFSETOF(phdr, &phdr->ndesc))
1258                                 return;
1259                         PBSWAP32((guint8 *)&phdr->ndesc);
1260                 }
1261
1262                 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1263                         /* swap the values in struct linux_usb_isodesc */
1264
1265                         /*
1266                          * See previous "Greasy hack" comment.
1267                          */
1268                         if (header_len_64_bytes) {
1269                                 pisodesc = (struct linux_usb_isodesc*)(void *)(pd + 64);
1270                         } else {
1271                                 pisodesc = (struct linux_usb_isodesc*)(void *)(pd + 48);
1272                         }
1273                         iso_numdesc = phdr->s.iso.numdesc;
1274                         for (i = 0; i < iso_numdesc; i++) {
1275                                 /* always check if we have enough data from the
1276                                  * beginnig of the packet (phdr)
1277                                  */
1278                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_status))
1279                                         return;
1280                                 PBSWAP32((guint8 *)&pisodesc->iso_status);
1281                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_off))
1282                                         return;
1283                                 PBSWAP32((guint8 *)&pisodesc->iso_off);
1284                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_len))
1285                                         return;
1286                                 PBSWAP32((guint8 *)&pisodesc->iso_len);
1287                                 if (packet_size < END_OFFSETOF(phdr, &pisodesc->_pad))
1288                                         return;
1289                                 PBSWAP32((guint8 *)&pisodesc->_pad);
1290
1291                                 pisodesc++;
1292                         }
1293                 }
1294         }
1295 }
1296
1297 static gboolean
1298 pcap_read_bt_pseudoheader(FILE_T fh,
1299     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1300 {
1301         int     bytes_read;
1302         struct libpcap_bt_phdr phdr;
1303
1304         errno = WTAP_ERR_CANT_READ;
1305         bytes_read = file_read(&phdr,
1306             sizeof (struct libpcap_bt_phdr), fh);
1307         if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
1308                 *err = file_error(fh, err_info);
1309                 if (*err == 0)
1310                         *err = WTAP_ERR_SHORT_READ;
1311                 return FALSE;
1312         }
1313         pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
1314         return TRUE;
1315 }
1316
1317 static gboolean
1318 pcap_read_llcp_pseudoheader(FILE_T fh,
1319     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1320 {
1321         int bytes_read;
1322         guint8 phdr[LLCP_HEADER_LEN];
1323
1324         errno = WTAP_ERR_CANT_READ;
1325         bytes_read = file_read(phdr, LLCP_HEADER_LEN, fh);
1326         if (bytes_read != LLCP_HEADER_LEN) {
1327                 *err = file_error(fh, err_info);
1328                 if (*err == 0)
1329                         *err = WTAP_ERR_SHORT_READ;
1330                 return FALSE;
1331         }
1332         pseudo_header->llcp.adapter = phdr[LLCP_ADAPTER_OFFSET];
1333         pseudo_header->llcp.flags = phdr[LLCP_FLAGS_OFFSET];
1334         return TRUE;
1335 }
1336
1337 static gboolean
1338 pcap_read_ppp_pseudoheader(FILE_T fh,
1339     union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1340 {
1341         int     bytes_read;
1342         struct libpcap_ppp_phdr phdr;
1343
1344         errno = WTAP_ERR_CANT_READ;
1345         bytes_read = file_read(&phdr,
1346             sizeof (struct libpcap_ppp_phdr), fh);
1347         if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
1348                 *err = file_error(fh, err_info);
1349                 if (*err == 0)
1350                         *err = WTAP_ERR_SHORT_READ;
1351                 return FALSE;
1352         }
1353         pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
1354         return TRUE;
1355 }
1356
1357 static gboolean
1358 pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
1359                               union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1360 {
1361   guint8 erf_hdr[sizeof(struct erf_phdr)];
1362   int    bytes_read;
1363
1364   errno = WTAP_ERR_CANT_READ;
1365   bytes_read = file_read(erf_hdr, sizeof(struct erf_phdr), fh);
1366   if (bytes_read != sizeof(struct erf_phdr)) {
1367     *err = file_error(fh, err_info);
1368     if (*err == 0)
1369       *err = WTAP_ERR_SHORT_READ;
1370     return FALSE;
1371   }
1372   pseudo_header->erf.phdr.ts = pletoh64(&erf_hdr[0]); /* timestamp */
1373   pseudo_header->erf.phdr.type =  erf_hdr[8];
1374   pseudo_header->erf.phdr.flags = erf_hdr[9];
1375   pseudo_header->erf.phdr.rlen = pntoh16(&erf_hdr[10]);
1376   pseudo_header->erf.phdr.lctr = pntoh16(&erf_hdr[12]);
1377   pseudo_header->erf.phdr.wlen = pntoh16(&erf_hdr[14]);
1378
1379   /* The high 32 bits of the timestamp contain the integer number of seconds
1380    * while the lower 32 bits contain the binary fraction of the second.
1381    * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1382   if (whdr) {
1383     guint64 ts = pseudo_header->erf.phdr.ts;
1384     whdr->ts.secs = (guint32) (ts >> 32);
1385     ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1386     ts += (ts & 0x80000000) << 1; /* rounding */
1387     whdr->ts.nsecs = ((guint32) (ts >> 32));
1388     if ( whdr->ts.nsecs >= 1000000000) {
1389       whdr->ts.nsecs -= 1000000000;
1390       whdr->ts.secs += 1;
1391     }
1392   }
1393   return TRUE;
1394 }
1395
1396 /*
1397  * If the type of record given in the pseudo header indicate the presence of an extension
1398  * header then, read all the extension headers
1399  */
1400 static gboolean
1401 pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1402                            int *err, gchar **err_info, guint * psize)
1403 {
1404   int bytes_read = 0;
1405   guint8 erf_exhdr[8];
1406   guint64 erf_exhdr_sw;
1407   int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1408   guint8 type;
1409   *psize = 0;
1410   if (pseudo_header->erf.phdr.type & 0x80){
1411     do{
1412       errno = WTAP_ERR_CANT_READ;
1413       bytes_read = file_read(erf_exhdr, 8, fh);
1414       if (bytes_read != 8 ) {
1415         *err = file_error(fh, err_info);
1416         if (*err == 0)
1417           *err = WTAP_ERR_SHORT_READ;
1418         return FALSE;
1419       }
1420       type = erf_exhdr[0];
1421       erf_exhdr_sw = pntoh64(erf_exhdr);
1422       if (i < max)
1423         memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1424       *psize += 8;
1425       i++;
1426     } while (type & 0x80);
1427   }
1428   return TRUE;
1429 }
1430
1431 /*
1432  * If the type of record given in the pseudo header indicate the precense of a subheader
1433  * then, read this optional subheader
1434  */
1435 static gboolean
1436 pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1437                            int *err, gchar **err_info, guint * psize)
1438 {
1439   guint8 erf_subhdr[sizeof(union erf_subhdr)];
1440   int    bytes_read;
1441
1442   *psize=0;
1443   switch(pseudo_header->erf.phdr.type & 0x7F) {
1444   case ERF_TYPE_MC_HDLC:
1445   case ERF_TYPE_MC_RAW:
1446   case ERF_TYPE_MC_ATM:
1447   case ERF_TYPE_MC_RAW_CHANNEL:
1448   case ERF_TYPE_MC_AAL5:
1449   case ERF_TYPE_MC_AAL2:
1450   case ERF_TYPE_COLOR_MC_HDLC_POS:
1451     /* Extract the Multi Channel header to include it in the pseudo header part */
1452     errno = WTAP_ERR_CANT_READ;
1453     bytes_read = file_read(erf_subhdr, sizeof(erf_mc_header_t), fh);
1454     if (bytes_read != sizeof(erf_mc_header_t) ) {
1455       *err = file_error(fh, err_info);
1456       if (*err == 0)
1457         *err = WTAP_ERR_SHORT_READ;
1458       return FALSE;
1459     }
1460     pseudo_header->erf.subhdr.mc_hdr = pntoh32(&erf_subhdr[0]);
1461     *psize = sizeof(erf_mc_header_t);
1462     break;
1463   case ERF_TYPE_ETH:
1464   case ERF_TYPE_COLOR_ETH:
1465   case ERF_TYPE_DSM_COLOR_ETH:
1466     /* Extract the Ethernet additional header to include it in the pseudo header part */
1467     errno = WTAP_ERR_CANT_READ;
1468     bytes_read = file_read(erf_subhdr, sizeof(erf_eth_header_t), fh);
1469     if (bytes_read != sizeof(erf_eth_header_t) ) {
1470       *err = file_error(fh, err_info);
1471       if (*err == 0)
1472         *err = WTAP_ERR_SHORT_READ;
1473       return FALSE;
1474     }
1475     pseudo_header->erf.subhdr.eth_hdr = pntoh16(&erf_subhdr[0]);
1476     *psize = sizeof(erf_eth_header_t);
1477     break;
1478   default:
1479     /* No optional pseudo header for this ERF type */
1480     break;
1481   }
1482   return TRUE;
1483 }
1484
1485 static gboolean
1486 pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1487 {
1488         struct i2c_file_hdr i2c_hdr;
1489         int    bytes_read;
1490
1491         errno = WTAP_ERR_CANT_READ;
1492         bytes_read = file_read(&i2c_hdr, sizeof (i2c_hdr), fh);
1493         if (bytes_read != sizeof (i2c_hdr)) {
1494                 *err = file_error(fh, err_info);
1495                 if (*err == 0)
1496                         *err = WTAP_ERR_SHORT_READ;
1497                 return FALSE;
1498         }
1499
1500         pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
1501         pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
1502         pseudo_header->i2c.flags = pntoh32(&i2c_hdr.flags);
1503
1504         return TRUE;
1505 }
1506
1507 int
1508 pcap_process_pseudo_header(FILE_T fh, int file_type, int wtap_encap,
1509     guint packet_size, gboolean check_packet_size,
1510     struct wtap_pkthdr *phdr, int *err, gchar **err_info)
1511 {
1512         int phdr_len = 0;
1513         guint size;
1514
1515         switch (wtap_encap) {
1516
1517         case WTAP_ENCAP_ATM_PDUS:
1518                 if (file_type == WTAP_FILE_TYPE_SUBTYPE_PCAP_NOKIA) {
1519                         /*
1520                          * Nokia IPSO ATM.
1521                          */
1522                         if (check_packet_size && packet_size < NOKIAATM_LEN) {
1523                                 /*
1524                                  * Uh-oh, the packet isn't big enough to even
1525                                  * have a pseudo-header.
1526                                  */
1527                                 *err = WTAP_ERR_BAD_FILE;
1528                                 *err_info = g_strdup_printf("pcap: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1529                                     packet_size);
1530                                 return -1;
1531                         }
1532                         if (!pcap_read_nokiaatm_pseudoheader(fh,
1533                             &phdr->pseudo_header, err, err_info))
1534                                 return -1;      /* Read error */
1535
1536                         phdr_len = NOKIAATM_LEN;
1537                 } else {
1538                         /*
1539                          * SunATM.
1540                          */
1541                         if (check_packet_size && packet_size < SUNATM_LEN) {
1542                                 /*
1543                                  * Uh-oh, the packet isn't big enough to even
1544                                  * have a pseudo-header.
1545                                  */
1546                                 *err = WTAP_ERR_BAD_FILE;
1547                                 *err_info = g_strdup_printf("pcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1548                                     packet_size);
1549                                 return -1;
1550                         }
1551                         if (!pcap_read_sunatm_pseudoheader(fh,
1552                             &phdr->pseudo_header, err, err_info))
1553                                 return -1;      /* Read error */
1554
1555                         phdr_len = SUNATM_LEN;
1556                 }
1557                 break;
1558
1559         case WTAP_ENCAP_ETHERNET:
1560                 if (file_type == WTAP_FILE_TYPE_SUBTYPE_PCAP_NOKIA) {
1561                         /*
1562                          * Nokia IPSO.  Psuedo header has already been read, but it's not considered
1563                          * part of the packet size, so reread it to store the data for later (when saving)
1564                          */
1565                         if (!pcap_read_nokia_pseudoheader(fh, &phdr->pseudo_header, err, err_info))
1566                                 return -1;      /* Read error */
1567                 }
1568
1569                 /*
1570                  * We don't know whether there's an FCS in this frame or not.
1571                  */
1572                 phdr->pseudo_header.eth.fcs_len = -1;
1573                 break;
1574
1575         case WTAP_ENCAP_IEEE_802_11:
1576         case WTAP_ENCAP_IEEE_802_11_PRISM:
1577         case WTAP_ENCAP_IEEE_802_11_RADIOTAP:
1578         case WTAP_ENCAP_IEEE_802_11_AVS:
1579                 /*
1580                  * We don't know whether there's an FCS in this frame or not.
1581                  * XXX - are there any OSes where the capture mechanism
1582                  * supplies an FCS?
1583                  */
1584                 phdr->pseudo_header.ieee_802_11.fcs_len = -1;
1585                 phdr->pseudo_header.ieee_802_11.decrypted = FALSE;
1586                 phdr->pseudo_header.ieee_802_11.channel = 0;
1587                 phdr->pseudo_header.ieee_802_11.data_rate = 0;
1588                 phdr->pseudo_header.ieee_802_11.signal_level = 0;
1589                 break;
1590
1591         case WTAP_ENCAP_IRDA:
1592                 if (check_packet_size && packet_size < IRDA_SLL_LEN) {
1593                         /*
1594                          * Uh-oh, the packet isn't big enough to even
1595                          * have a pseudo-header.
1596                          */
1597                         *err = WTAP_ERR_BAD_FILE;
1598                         *err_info = g_strdup_printf("pcap: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1599                             packet_size);
1600                         return -1;
1601                 }
1602                 if (!pcap_read_irda_pseudoheader(fh, &phdr->pseudo_header,
1603                     err, err_info))
1604                         return -1;      /* Read error */
1605
1606                 phdr_len = IRDA_SLL_LEN;
1607                 break;
1608
1609         case WTAP_ENCAP_MTP2_WITH_PHDR:
1610                 if (check_packet_size && packet_size < MTP2_HDR_LEN) {
1611                         /*
1612                          * Uh-oh, the packet isn't big enough to even
1613                          * have a pseudo-header.
1614                          */
1615                         *err = WTAP_ERR_BAD_FILE;
1616                         *err_info = g_strdup_printf("pcap: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1617                             packet_size);
1618                         return -1;
1619                 }
1620                 if (!pcap_read_mtp2_pseudoheader(fh, &phdr->pseudo_header,
1621                     err, err_info))
1622                         return -1;      /* Read error */
1623
1624                 phdr_len = MTP2_HDR_LEN;
1625                 break;
1626
1627         case WTAP_ENCAP_LINUX_LAPD:
1628                 if (check_packet_size && packet_size < LAPD_SLL_LEN) {
1629                         /*
1630                          * Uh-oh, the packet isn't big enough to even
1631                          * have a pseudo-header.
1632                          */
1633                         *err = WTAP_ERR_BAD_FILE;
1634                         *err_info = g_strdup_printf("pcap: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1635                             packet_size);
1636                         return -1;
1637                 }
1638                 if (!pcap_read_lapd_pseudoheader(fh, &phdr->pseudo_header,
1639                     err, err_info))
1640                         return -1;      /* Read error */
1641
1642                 phdr_len = LAPD_SLL_LEN;
1643                 break;
1644
1645         case WTAP_ENCAP_SITA:
1646                 if (check_packet_size && packet_size < SITA_HDR_LEN) {
1647                         /*
1648                          * Uh-oh, the packet isn't big enough to even
1649                          * have a pseudo-header.
1650                          */
1651                         *err = WTAP_ERR_BAD_FILE;
1652                         *err_info = g_strdup_printf("pcap: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1653                             packet_size);
1654                         return -1;
1655                 }
1656                 if (!pcap_read_sita_pseudoheader(fh, &phdr->pseudo_header,
1657                     err, err_info))
1658                         return -1;      /* Read error */
1659
1660                 phdr_len = SITA_HDR_LEN;
1661                 break;
1662
1663         case WTAP_ENCAP_BLUETOOTH_H4:
1664                 /* We don't have pseudoheader, so just pretend we received everything. */
1665                 phdr->pseudo_header.p2p.sent = FALSE;
1666                 break;
1667
1668         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1669                 if (check_packet_size &&
1670                     packet_size < sizeof (struct libpcap_bt_phdr)) {
1671                         /*
1672                          * Uh-oh, the packet isn't big enough to even
1673                          * have a pseudo-header.
1674                          */
1675                         *err = WTAP_ERR_BAD_FILE;
1676                         *err_info = g_strdup_printf("pcap: libpcap bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1677                             packet_size);
1678                         return -1;
1679                 }
1680                 if (!pcap_read_bt_pseudoheader(fh,
1681                     &phdr->pseudo_header, err, err_info))
1682                         return -1;      /* Read error */
1683
1684                 phdr_len = (int)sizeof (struct libpcap_bt_phdr);
1685                 break;
1686
1687         case WTAP_ENCAP_NFC_LLCP:
1688                 if (check_packet_size && packet_size < LLCP_HEADER_LEN) {
1689                         *err = WTAP_ERR_BAD_FILE;
1690                         *err_info = g_strdup_printf("pcap: libpcap llcp file too short");
1691                         return -1;
1692                 }
1693                 if (!pcap_read_llcp_pseudoheader(fh, &phdr->pseudo_header, err, err_info))
1694                         return -1;      /* Read error */
1695                 phdr_len = LLCP_HEADER_LEN;
1696                 break;
1697
1698         case WTAP_ENCAP_PPP_WITH_PHDR:
1699                 if (check_packet_size &&
1700                     packet_size < sizeof (struct libpcap_ppp_phdr)) {
1701                         /*
1702                          * Uh-oh, the packet isn't big enough to even
1703                          * have a pseudo-header.
1704                          */
1705                         *err = WTAP_ERR_BAD_FILE;
1706                         *err_info = g_strdup_printf("pcap: libpcap ppp file has a %u-byte packet, too small to have even a pseudo-header",
1707                             packet_size);
1708                         return -1;
1709                 }
1710                 if (!pcap_read_ppp_pseudoheader(fh,
1711                     &phdr->pseudo_header, err, err_info))
1712                         return -1;      /* Read error */
1713
1714                 phdr_len = (int)sizeof (struct libpcap_ppp_phdr);
1715                 break;
1716
1717         case WTAP_ENCAP_ERF:
1718                 if (check_packet_size &&
1719                     packet_size < sizeof(struct erf_phdr) ) {
1720                         /*
1721                          * Uh-oh, the packet isn't big enough to even
1722                          * have a pseudo-header.
1723                          */
1724                         *err = WTAP_ERR_BAD_FILE;
1725                         *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1726                             packet_size);
1727                         return -1;
1728                 }
1729
1730                 if (!pcap_read_erf_pseudoheader(fh, phdr, &phdr->pseudo_header,
1731                     err, err_info))
1732                         return -1;      /* Read error */
1733
1734                 phdr_len = (int)sizeof(struct erf_phdr);
1735
1736                 /* check the optional Extension header */
1737                 if (!pcap_read_erf_exheader(fh, &phdr->pseudo_header, err, err_info,
1738                     &size))
1739                         return -1;      /* Read error */
1740
1741                 phdr_len += size;
1742
1743                 /* check the optional Multi Channel header */
1744                 if (!pcap_read_erf_subheader(fh, &phdr->pseudo_header, err, err_info,
1745                     &size))
1746                         return -1;      /* Read error */
1747
1748                 phdr_len += size;
1749
1750                 if (check_packet_size &&
1751                     packet_size < (guint)phdr_len) {
1752                         /*
1753                          * Uh-oh, the packet isn't big enough for the pseudo-
1754                          * header.
1755                          */
1756                         *err = WTAP_ERR_BAD_FILE;
1757                         *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)",
1758                             packet_size, phdr_len);
1759                         return -1;
1760                 }
1761                 break;
1762
1763         case WTAP_ENCAP_I2C:
1764                 if (check_packet_size &&
1765                     packet_size < sizeof (struct i2c_file_hdr)) {
1766                         /*
1767                          * Uh-oh, the packet isn't big enough to even
1768                          * have a pseudo-header.
1769                          */
1770                         *err = WTAP_ERR_BAD_FILE;
1771                         *err_info = g_strdup_printf("pcap: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1772                             packet_size);
1773                         return -1;
1774                 }
1775                 if (!pcap_read_i2c_pseudoheader(fh, &phdr->pseudo_header,
1776                     err, err_info))
1777                         return -1;      /* Read error */
1778
1779                 /*
1780                  * Don't count the pseudo-header as part of the packet.
1781                  */
1782                 phdr_len = (int)sizeof (struct i2c_file_hdr);
1783                 break;
1784         }
1785
1786         return phdr_len;
1787 }
1788
1789 void
1790 pcap_read_post_process(int file_type, int wtap_encap,
1791     union wtap_pseudo_header *pseudo_header,
1792     guint8 *pd, guint packet_size, gboolean bytes_swapped, int fcs_len)
1793 {
1794         switch (wtap_encap) {
1795
1796         case WTAP_ENCAP_ATM_PDUS:
1797                 if (file_type == WTAP_FILE_TYPE_SUBTYPE_PCAP_NOKIA) {
1798                         /*
1799                          * Nokia IPSO ATM.
1800                          *
1801                          * Guess the traffic type based on the packet
1802                          * contents.
1803                          */
1804                         atm_guess_traffic_type(pd, packet_size, pseudo_header);
1805                 } else {
1806                         /*
1807                          * SunATM.
1808                          *
1809                          * If this is ATM LANE traffic, try to guess what
1810                          * type of LANE traffic it is based on the packet
1811                          * contents.
1812                          */
1813                         if (pseudo_header->atm.type == TRAF_LANE)
1814                                 atm_guess_lane_type(pd, packet_size,
1815                                     pseudo_header);
1816                 }
1817                 break;
1818
1819         case WTAP_ENCAP_ETHERNET:
1820                 pseudo_header->eth.fcs_len = fcs_len;
1821                 break;
1822
1823         case WTAP_ENCAP_USB_LINUX:
1824                 pcap_process_linux_usb_pseudoheader(packet_size,
1825                     bytes_swapped, FALSE, pd);
1826                 break;
1827
1828         case WTAP_ENCAP_USB_LINUX_MMAPPED:
1829                 pcap_process_linux_usb_pseudoheader(packet_size,
1830                     bytes_swapped, TRUE, pd);
1831                 break;
1832
1833         case WTAP_ENCAP_NETANALYZER:
1834                 /*
1835                  * Not strictly necessary, as the netANALYZER
1836                  * dissector calls the "Ethernet with FCS"
1837                  * dissector, but we might as well set it.
1838                  */
1839                 pseudo_header->eth.fcs_len = 4;
1840                 break;
1841
1842         default:
1843                 break;
1844         }
1845 }
1846
1847 int
1848 pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
1849 {
1850         int hdrsize;
1851
1852         switch (encap) {
1853
1854         case WTAP_ENCAP_ATM_PDUS:
1855                 hdrsize = SUNATM_LEN;
1856                 break;
1857
1858         case WTAP_ENCAP_IRDA:
1859                 hdrsize = IRDA_SLL_LEN;
1860                 break;
1861
1862         case WTAP_ENCAP_MTP2_WITH_PHDR:
1863                 hdrsize = MTP2_HDR_LEN;
1864                 break;
1865
1866         case WTAP_ENCAP_LINUX_LAPD:
1867                 hdrsize = LAPD_SLL_LEN;
1868                 break;
1869
1870         case WTAP_ENCAP_SITA:
1871                 hdrsize = SITA_HDR_LEN;
1872                 break;
1873
1874         case WTAP_ENCAP_ERF:
1875                 hdrsize = (int)sizeof (struct erf_phdr);
1876                 switch (pseudo_header->erf.phdr.type & 0x7F) {
1877
1878                 case ERF_TYPE_MC_HDLC:
1879                 case ERF_TYPE_MC_RAW:
1880                 case ERF_TYPE_MC_ATM:
1881                 case ERF_TYPE_MC_RAW_CHANNEL:
1882                 case ERF_TYPE_MC_AAL5:
1883                 case ERF_TYPE_MC_AAL2:
1884                 case ERF_TYPE_COLOR_MC_HDLC_POS:
1885                         hdrsize += (int)sizeof(struct erf_mc_hdr);
1886                         break;
1887
1888                 case ERF_TYPE_ETH:
1889                 case ERF_TYPE_COLOR_ETH:
1890                 case ERF_TYPE_DSM_COLOR_ETH:
1891                         hdrsize += (int)sizeof(struct erf_eth_hdr);
1892                         break;
1893
1894                 default:
1895                         break;
1896                 }
1897
1898                 /*
1899                  * Add in the lengths of the extension headers.
1900                  */
1901                 if (pseudo_header->erf.phdr.type & 0x80) {
1902                         int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1903                         guint8 erf_exhdr[8];
1904                         guint8 type;
1905
1906                         do {
1907                                 phtonll(erf_exhdr, pseudo_header->erf.ehdr_list[i].ehdr);
1908                                 type = erf_exhdr[0];
1909                                 hdrsize += 8;
1910                                 i++;
1911                         } while (type & 0x80 && i < max);
1912                 }
1913                 break;
1914
1915         case WTAP_ENCAP_I2C:
1916                 hdrsize = (int)sizeof (struct i2c_file_hdr);
1917                 break;
1918
1919         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1920                 hdrsize = (int)sizeof (struct libpcap_bt_phdr);
1921                 break;
1922
1923         case WTAP_ENCAP_PPP_WITH_PHDR:
1924                 hdrsize = (int)sizeof (struct libpcap_ppp_phdr);
1925                 break;
1926
1927         default:
1928                 hdrsize = 0;
1929                 break;
1930         }
1931
1932         return hdrsize;
1933 }
1934
1935 gboolean
1936 pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
1937     int *err)
1938 {
1939         guint8 atm_hdr[SUNATM_LEN];
1940         guint8 irda_hdr[IRDA_SLL_LEN];
1941         guint8 lapd_hdr[LAPD_SLL_LEN];
1942         guint8 mtp2_hdr[MTP2_HDR_LEN];
1943         guint8 sita_hdr[SITA_HDR_LEN];
1944         guint8 erf_hdr[ sizeof(struct erf_mc_phdr)];
1945         struct i2c_file_hdr i2c_hdr;
1946         struct libpcap_bt_phdr bt_hdr;
1947         struct libpcap_ppp_phdr ppp_hdr;
1948         size_t size;
1949
1950         switch (encap) {
1951
1952         case WTAP_ENCAP_ATM_PDUS:
1953                 /*
1954                  * Write the ATM header.
1955                  */
1956                 atm_hdr[SUNATM_FLAGS] =
1957                     (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1958                 switch (pseudo_header->atm.aal) {
1959
1960                 case AAL_SIGNALLING:
1961                         /* Q.2931 */
1962                         atm_hdr[SUNATM_FLAGS] |= 0x06;
1963                         break;
1964
1965                 case AAL_5:
1966                         switch (pseudo_header->atm.type) {
1967
1968                         case TRAF_LANE:
1969                                 /* LANE */
1970                                 atm_hdr[SUNATM_FLAGS] |= 0x01;
1971                                 break;
1972
1973                         case TRAF_LLCMX:
1974                                 /* RFC 1483 LLC multiplexed traffic */
1975                                 atm_hdr[SUNATM_FLAGS] |= 0x02;
1976                                 break;
1977
1978                         case TRAF_ILMI:
1979                                 /* ILMI */
1980                                 atm_hdr[SUNATM_FLAGS] |= 0x05;
1981                                 break;
1982                         }
1983                         break;
1984                 }
1985                 atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
1986                 phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1987                 if (!wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr), err))
1988                         return FALSE;
1989                 wdh->bytes_dumped += sizeof(atm_hdr);
1990                 break;
1991
1992         case WTAP_ENCAP_IRDA:
1993                 /*
1994                  * Write the IrDA header.
1995                  */
1996                 memset(irda_hdr, 0, sizeof(irda_hdr));
1997                 phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
1998                     pseudo_header->irda.pkttype);
1999                 phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
2000                 if (!wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr), err))
2001                         return FALSE;
2002                 wdh->bytes_dumped += sizeof(irda_hdr);
2003                 break;
2004
2005         case WTAP_ENCAP_MTP2_WITH_PHDR:
2006                 /*
2007                  * Write the MTP2 header.
2008                  */
2009                 memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
2010                 mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
2011                 mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
2012                 phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
2013                     pseudo_header->mtp2.link_number);
2014                 if (!wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr), err))
2015                         return FALSE;
2016                 wdh->bytes_dumped += sizeof(mtp2_hdr);
2017                 break;
2018
2019         case WTAP_ENCAP_LINUX_LAPD:
2020                 /*
2021                  * Write the LAPD header.
2022                  */
2023                 memset(&lapd_hdr, 0, sizeof(lapd_hdr));
2024                 phtons(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET],
2025                     pseudo_header->lapd.pkttype);
2026                 phtons(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
2027                 lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
2028                     pseudo_header->lapd.we_network?0x01:0x00;
2029                 if (!wtap_dump_file_write(wdh, lapd_hdr, sizeof(lapd_hdr), err))
2030                         return FALSE;
2031                 wdh->bytes_dumped += sizeof(lapd_hdr);
2032                 break;
2033
2034         case WTAP_ENCAP_SITA:
2035                 /*
2036                  * Write the SITA header.
2037                  */
2038                 memset(&sita_hdr, 0, sizeof(sita_hdr));
2039                 sita_hdr[SITA_FLAGS_OFFSET]   = pseudo_header->sita.sita_flags;
2040                 sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.sita_signals;
2041                 sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.sita_errors1;
2042                 sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.sita_errors2;
2043                 sita_hdr[SITA_PROTO_OFFSET]   = pseudo_header->sita.sita_proto;
2044                 if (!wtap_dump_file_write(wdh, sita_hdr, sizeof(sita_hdr), err))
2045                         return FALSE;
2046                 wdh->bytes_dumped += sizeof(sita_hdr);
2047                 break;
2048
2049         case WTAP_ENCAP_ERF:
2050                 /*
2051                  * Write the ERF header.
2052                  */
2053                 memset(&erf_hdr, 0, sizeof(erf_hdr));
2054                 phtolell(&erf_hdr[0], pseudo_header->erf.phdr.ts);
2055                 erf_hdr[8] = pseudo_header->erf.phdr.type;
2056                 erf_hdr[9] = pseudo_header->erf.phdr.flags;
2057                 phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
2058                 phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
2059                 phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
2060                 size = sizeof(struct erf_phdr);
2061
2062                 switch(pseudo_header->erf.phdr.type & 0x7F) {
2063                 case ERF_TYPE_MC_HDLC:
2064                 case ERF_TYPE_MC_RAW:
2065                 case ERF_TYPE_MC_ATM:
2066                 case ERF_TYPE_MC_RAW_CHANNEL:
2067                 case ERF_TYPE_MC_AAL5:
2068                 case ERF_TYPE_MC_AAL2:
2069                 case ERF_TYPE_COLOR_MC_HDLC_POS:
2070                         phtonl(&erf_hdr[16], pseudo_header->erf.subhdr.mc_hdr);
2071                         size += (int)sizeof(struct erf_mc_hdr);
2072                         break;
2073                 case ERF_TYPE_ETH:
2074                 case ERF_TYPE_COLOR_ETH:
2075                 case ERF_TYPE_DSM_COLOR_ETH:
2076                         phtons(&erf_hdr[16], pseudo_header->erf.subhdr.eth_hdr);
2077                         size += (int)sizeof(struct erf_eth_hdr);
2078                         break;
2079                 default:
2080                         break;
2081                 }
2082                 if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
2083                         return FALSE;
2084                 wdh->bytes_dumped += size;
2085
2086                 /*
2087                  * Now write out the extension headers.
2088                  */
2089                 if (pseudo_header->erf.phdr.type & 0x80) {
2090                         int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
2091                         guint8 erf_exhdr[8];
2092                         guint8 type;
2093
2094                         do {
2095                                 phtonll(erf_exhdr, pseudo_header->erf.ehdr_list[i].ehdr);
2096                                 type = erf_exhdr[0];
2097                                 if (!wtap_dump_file_write(wdh, erf_exhdr, 8, err))
2098                                         return FALSE;
2099                                 wdh->bytes_dumped += 8;
2100                                 i++;
2101                         } while (type & 0x80 && i < max);
2102                 }
2103                 break;
2104
2105         case WTAP_ENCAP_I2C:
2106                 /*
2107                  * Write the I2C header.
2108                  */
2109                 memset(&i2c_hdr, 0, sizeof(i2c_hdr));
2110                 i2c_hdr.bus = pseudo_header->i2c.bus |
2111                         (pseudo_header->i2c.is_event ? 0x80 : 0x00);
2112                 phtonl((guint8 *)&i2c_hdr.flags, pseudo_header->i2c.flags);
2113                 if (!wtap_dump_file_write(wdh, &i2c_hdr, sizeof(i2c_hdr), err))
2114                         return FALSE;
2115                 wdh->bytes_dumped += sizeof(i2c_hdr);
2116                 break;
2117
2118         case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
2119                 bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
2120                 if (!wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr, err))
2121                         return FALSE;
2122                 wdh->bytes_dumped += sizeof bt_hdr;
2123                 break;
2124
2125         case WTAP_ENCAP_PPP_WITH_PHDR:
2126                 ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
2127                 if (!wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr, err))
2128                         return FALSE;
2129                 wdh->bytes_dumped += sizeof ppp_hdr;
2130                 break;
2131         }
2132         return TRUE;
2133 }