2 * Code common to libpcap and pcap-NG file formats
7 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
9 * File format support for pcap-ng file format
10 * Copyright (c) 2007 by Ulf Lamping <ulf.lamping@web.de>
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.
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.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include "file_wrappers.h"
38 #include "pcap-encap.h"
39 #include "pcap-common.h"
42 * Map link-layer types (LINKTYPE_ values) to Wiretap encapsulations.
45 * Either LBL NRG wasn't an adequate central registry (e.g., because of
46 * the slow rate of releases from them), or nobody bothered using them
47 * as a central registry, as many different groups have patched libpcap
48 * (and BPF, on the BSDs) to add new encapsulation types, and have ended
49 * up using the same DLT_ values for different encapsulation types.
51 * For those numerical encapsulation type values that everybody uses for
52 * the same encapsulation type (which inclues those that some platforms
53 * specify different DLT_ names for but don't appear to use), we map
54 * those values to the appropriate Wiretap values.
56 * For those numerical encapsulation type values that different libpcap
57 * variants use for different encapsulation types, we check what
58 * <pcap.h> defined to determine how to interpret them, so that we
59 * interpret them the way the libpcap with which we're building
60 * Wireshark/Wiretap interprets them (which, if it doesn't support
61 * them at all, means we don't support them either - any capture files
62 * using them are foreign, and we don't hazard a guess as to which
63 * platform they came from; we could, I guess, choose the most likely
66 * Note: if you need a new encapsulation type for libpcap files, do
67 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
68 * add a new encapsulation type by changing an existing entry;
69 * leave the existing entries alone.
71 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking for
72 * a new DLT_ value, and specifying the purpose of the new value. When
73 * you get the new DLT_ value, use that numerical value in the "dlt_value"
74 * field of "pcap_to_wtap_map[]".
80 } pcap_to_wtap_map[] = {
82 * These are the values that are almost certainly the same
83 * in all libpcaps (I've yet to find one where the values
84 * in question are used for some purpose other than the
85 * one below, but...), and that Wiretap and Wireshark
88 { 0, WTAP_ENCAP_NULL }, /* null encapsulation */
89 { 1, WTAP_ENCAP_ETHERNET },
90 { 6, WTAP_ENCAP_TOKEN_RING }, /* IEEE 802 Networks - assume token ring */
91 { 7, WTAP_ENCAP_ARCNET },
92 { 8, WTAP_ENCAP_SLIP },
93 { 9, WTAP_ENCAP_PPP },
94 #ifdef BIT_SWAPPED_MAC_ADDRS
95 { 10, WTAP_ENCAP_FDDI_BITSWAPPED },
97 { 10, WTAP_ENCAP_FDDI },
100 { 32, WTAP_ENCAP_REDBACK },
103 * 50 is DLT_PPP_SERIAL in NetBSD; it appears that DLT_PPP
104 * on BSD (at least according to standard tcpdump) has, as
105 * the first octet, an indication of whether the packet was
106 * transmitted or received (rather than having the standard
107 * PPP address value of 0xff), but that DLT_PPP_SERIAL puts
108 * a real live PPP header there, or perhaps a Cisco PPP header
109 * as per section 4.3.1 of RFC 1547 (implementations of this
110 * exist in various BSDs in "sys/net/if_spppsubr.c", and
111 * I think also exist either in standard Linux or in
112 * various Linux patches; the implementations show how to handle
113 * Cisco keepalive packets).
115 * However, I don't see any obvious place in FreeBSD "if_ppp.c"
116 * where anything other than the standard PPP header would be
117 * passed up. I see some stuff that sets the first octet
118 * to 0 for incoming and 1 for outgoing packets before applying
119 * a BPF filter to see whether to drop packets whose protocol
120 * field has the 0x8000 bit set, i.e. network control protocols -
121 * those are handed up to userland - but that code puts the
122 * address field back before passing the packet up.
124 * I also don't see anything immediately obvious that munges
125 * the address field for sync PPP, either.
127 * Wireshark currently assumes that if the first octet of a
128 * PPP frame is 0xFF, it's the address field and is followed
129 * by a control field and a 2-byte protocol, otherwise the
130 * address and control fields are absent and the frame begins
131 * with a protocol field. If we ever see a BSD/OS PPP
132 * capture, we'll have to handle it differently, and we may
133 * have to handle standard BSD captures differently if, in fact,
134 * they don't have 0xff 0x03 as the first two bytes - but, as per
135 * the two paragraphs preceding this, it's not clear that
136 * the address field *is* munged into an incoming/outgoing
137 * field when the packet is handed to the BPF device.
139 * For now, we just map DLT_PPP_SERIAL to WTAP_ENCAP_PPP, as
140 * we treat WTAP_ENCAP_PPP packets as if those beginning with
141 * 0xff have the standard RFC 1662 "PPP in HDLC-like Framing"
142 * 0xff 0x03 address/control header, and DLT_PPP_SERIAL frames
143 * appear to contain that unless they're Cisco frames (if we
144 * ever see a capture with them, we'd need to implement the
145 * RFC 1547 stuff, and the keepalive protocol stuff).
147 * We may have to distinguish between "PPP where if it doesn't
148 * begin with 0xff there's no HDLC encapsulation and the frame
149 * begins with the protocol field" (which is how we handle
150 * WTAP_ENCAP_PPP now) and "PPP where there's either HDLC
151 * encapsulation or Cisco PPP" (which is what DLT_PPP_SERIAL
154 * XXX - NetBSD has DLT_HDLC, which appears to be used for
155 * Cisco HDLC. Ideally, they should use DLT_PPP_SERIAL
156 * only for real live HDLC-encapsulated PPP, not for Cisco
159 { 50, WTAP_ENCAP_PPP },
162 * Apparently used by the Axent Raptor firewall (now Symantec
163 * Enterprise Firewall).
164 * Thanks, Axent, for not reserving that type with tcpdump.org
165 * and not telling anybody about it.
167 { 99, WTAP_ENCAP_SYMANTEC },
170 * These are the values that libpcap 0.5 and later use in
171 * capture file headers, in an attempt to work around the
172 * confusion decried above, and that Wiretap and Wireshark
175 { 100, WTAP_ENCAP_ATM_RFC1483 },
176 { 101, WTAP_ENCAP_RAW_IP },
179 * More values used by libpcap 0.5 as DLT_ values and used by the
180 * current CVS version of libpcap in capture file headers.
181 * They are not yet handled in Wireshark.
182 * If we get a capture that contains them, we'll implement them.
184 { 102, WTAP_ENCAP_SLIP_BSDOS },
185 { 103, WTAP_ENCAP_PPP_BSDOS },
189 * These ones are handled in Wireshark, though.
191 { 104, WTAP_ENCAP_CHDLC }, /* Cisco HDLC */
192 { 105, WTAP_ENCAP_IEEE_802_11 }, /* IEEE 802.11 */
193 { 106, WTAP_ENCAP_LINUX_ATM_CLIP },
194 { 107, WTAP_ENCAP_FRELAY }, /* Frame Relay */
195 { 108, WTAP_ENCAP_NULL }, /* OpenBSD loopback */
196 { 109, WTAP_ENCAP_ENC }, /* OpenBSD IPSEC enc */
198 { 110, WTAP_ENCAP_LANE_802_3 },/* ATM LANE 802.3 */
199 { 111, WTAP_ENCAP_HIPPI }, /* NetBSD HIPPI */
201 { 112, WTAP_ENCAP_CHDLC }, /* NetBSD HDLC framing */
204 * Linux "cooked mode" captures, used by the current CVS version
207 * it could be a packet in Cisco's ERSPAN encapsulation which uses
208 * this number as well (why can't people stick to protocols when it
209 * comes to allocating/using DLT types).
211 { 113, WTAP_ENCAP_SLL }, /* Linux cooked capture */
213 { 114, WTAP_ENCAP_LOCALTALK }, /* Localtalk */
216 * The tcpdump.org version of libpcap uses 117, rather than 17,
217 * for OpenBSD packet filter logging, so as to avoid conflicting
218 * with DLT_LANE8023 in SuSE 6.3 libpcap.
220 { 117, WTAP_ENCAP_PFLOG },
222 { 118, WTAP_ENCAP_CISCO_IOS },
223 { 119, WTAP_ENCAP_PRISM_HEADER }, /* Prism monitor mode hdr */
224 { 121, WTAP_ENCAP_HHDLC }, /* HiPath HDLC */
225 { 122, WTAP_ENCAP_IP_OVER_FC }, /* RFC 2625 IP-over-FC */
226 { 123, WTAP_ENCAP_ATM_PDUS }, /* SunATM */
227 { 127, WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP }, /* 802.11 plus radiotap WLAN header */
228 { 128, WTAP_ENCAP_TZSP }, /* Tazmen Sniffer Protocol */
229 { 129, WTAP_ENCAP_ARCNET_LINUX },
230 { 130, WTAP_ENCAP_JUNIPER_MLPPP }, /* Juniper MLPPP on ML-, LS-, AS- PICs */
231 { 131, WTAP_ENCAP_JUNIPER_MLFR }, /* Juniper MLFR (FRF.15) on ML-, LS-, AS- PICs */
232 { 133, WTAP_ENCAP_JUNIPER_GGSN},
234 * Values 132-134, 136 not listed here are reserved for use
235 * in Juniper hardware.
237 { 135, WTAP_ENCAP_JUNIPER_ATM2 }, /* various encapsulations captured on the ATM2 PIC */
238 { 137, WTAP_ENCAP_JUNIPER_ATM1 }, /* various encapsulations captured on the ATM1 PIC */
240 { 138, WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 },
241 /* Apple IP-over-IEEE 1394 */
243 { 139, WTAP_ENCAP_MTP2_WITH_PHDR },
244 { 140, WTAP_ENCAP_MTP2 },
245 { 141, WTAP_ENCAP_MTP3 },
246 { 142, WTAP_ENCAP_SCCP },
247 { 143, WTAP_ENCAP_DOCSIS },
248 { 144, WTAP_ENCAP_IRDA }, /* IrDA capture */
250 /* Reserved for private use. */
251 { 147, WTAP_ENCAP_USER0 },
252 { 148, WTAP_ENCAP_USER1 },
253 { 149, WTAP_ENCAP_USER2 },
254 { 150, WTAP_ENCAP_USER3 },
255 { 151, WTAP_ENCAP_USER4 },
256 { 152, WTAP_ENCAP_USER5 },
257 { 153, WTAP_ENCAP_USER6 },
258 { 154, WTAP_ENCAP_USER7 },
259 { 155, WTAP_ENCAP_USER8 },
260 { 156, WTAP_ENCAP_USER9 },
261 { 157, WTAP_ENCAP_USER10 },
262 { 158, WTAP_ENCAP_USER11 },
263 { 159, WTAP_ENCAP_USER12 },
264 { 160, WTAP_ENCAP_USER13 },
265 { 161, WTAP_ENCAP_USER14 },
266 { 162, WTAP_ENCAP_USER15 },
268 { 163, WTAP_ENCAP_IEEE_802_11_WLAN_AVS }, /* 802.11 plus AVS WLAN header */
271 * 164 is reserved for Juniper-private chassis-internal
272 * meta-information such as QoS profiles, etc..
275 { 165, WTAP_ENCAP_BACNET_MS_TP },
278 * 166 is reserved for a PPP variant in which the first byte
279 * of the 0xff03 header, the 0xff, is replaced by a direction
280 * byte. I don't know whether any captures look like that,
281 * but it is used for some Linux IP filtering (ipfilter?).
284 /* Ethernet PPPoE frames captured on a service PIC */
285 { 167, WTAP_ENCAP_JUNIPER_PPPOE },
288 * 168 is reserved for more Juniper private-chassis-
289 * internal meta-information.
292 { 169, WTAP_ENCAP_GPRS_LLC },
295 * 170 and 171 are reserved for ITU-T G.7041/Y.1303 Generic
299 /* Registered by Gcom, Inc. */
300 { 172, WTAP_ENCAP_GCOM_TIE1 },
301 { 173, WTAP_ENCAP_GCOM_SERIAL },
303 { 177, WTAP_ENCAP_LINUX_LAPD },
305 /* Ethernet frames prepended with meta-information */
306 { 178, WTAP_ENCAP_JUNIPER_ETHER },
307 /* PPP frames prepended with meta-information */
308 { 179, WTAP_ENCAP_JUNIPER_PPP },
309 /* Frame-Relay frames prepended with meta-information */
310 { 180, WTAP_ENCAP_JUNIPER_FRELAY },
311 /* C-HDLC frames prepended with meta-information */
312 { 181, WTAP_ENCAP_JUNIPER_CHDLC },
313 /* VOIP Frames prepended with meta-information */
314 { 183, WTAP_ENCAP_JUNIPER_VP },
315 /* raw USB packets */
316 { 186, WTAP_ENCAP_USB },
317 /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
318 { 187, WTAP_ENCAP_BLUETOOTH_H4 },
319 /* IEEE 802.16 MAC Common Part Sublayer */
320 { 188, WTAP_ENCAP_IEEE802_16_MAC_CPS },
321 /* USB packets with Linux-specified header */
322 { 189, WTAP_ENCAP_USB_LINUX },
324 { 190, WTAP_ENCAP_CAN20B },
325 /* Per-Packet Information header */
326 { 192, WTAP_ENCAP_PPI },
327 /* IEEE 802.15.4 Wireless PAN */
328 { 195, WTAP_ENCAP_IEEE802_15_4 },
329 /* SITA File Encapsulation */
330 { 196, WTAP_ENCAP_SITA },
331 /* Endace Record File Encapsulation */
332 { 197, WTAP_ENCAP_ERF },
334 { 199, WTAP_ENCAP_IPMB },
335 /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
336 { 201, WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR },
338 { 203, WTAP_ENCAP_LAPD },
339 /* PPP with pseudoheader */
340 { 204, WTAP_ENCAP_PPP_WITH_PHDR },
342 { 209, WTAP_ENCAP_I2C },
344 { 210, WTAP_ENCAP_FLEXRAY },
346 { 211, WTAP_ENCAP_MOST },
348 { 212, WTAP_ENCAP_LIN },
349 /* X2E Xoraya serial frame */
350 { 213, WTAP_ENCAP_X2E_SERIAL },
351 /* X2E Xoraya frame */
352 { 214, WTAP_ENCAP_X2E_XORAYA },
353 /* IEEE 802.15.4 Wireless PAN non-ASK PHY */
354 { 215, WTAP_ENCAP_IEEE802_15_4_NONASK_PHY },
355 /* USB packets with padded Linux-specified header */
356 { 220, WTAP_ENCAP_USB_LINUX_MMAPPED },
357 /* Fibre Channel FC-2 frame */
358 { 224, WTAP_ENCAP_FIBRE_CHANNEL_FC2 },
359 /* Fibre Channel FC-2 frame with Delimiter */
360 { 225, WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS },
362 { 226, WTAP_ENCAP_IPNET },
363 /* SocketCAN frame */
364 { 227, WTAP_ENCAP_SOCKETCAN },
366 { 228, WTAP_ENCAP_RAW_IP4 },
368 { 229, WTAP_ENCAP_RAW_IP6 },
369 /* IEEE 802.15.4 Wireless PAN no fcs */
370 { 230, WTAP_ENCAP_IEEE802_15_4_NOFCS },
371 /* DVB-CI (Common Interface) */
372 { 235, WTAP_ENCAP_DVBCI },
374 { 236, WTAP_ENCAP_MUX27010 },
375 /* netANALYZER pseudo-header followed by Ethernet with CRC */
376 { 240, WTAP_ENCAP_NETANALYZER },
377 /* netANALYZER pseudo-header in transparent mode */
378 { 241, WTAP_ENCAP_NETANALYZER_TRANSPARENT },
379 /* IP-over-Infiniband, as specified by RFC 4391 section 6 */
380 { 242, WTAP_ENCAP_IP_OVER_IB },
381 /* ISO/IEC 13818-1 MPEG2-TS packets */
382 { 243, WTAP_ENCAP_MPEG_2_TS },
387 * If you need a new encapsulation type for libpcap files, do
388 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
389 * add a new encapsulation type by changing an existing entry;
390 * leave the existing entries alone.
392 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
393 * for a new DLT_ value, and specifying the purpose of the new value.
394 * When you get the new DLT_ value, use that numerical value in
395 * the "dlt_value" field of "pcap_to_wtap_map[]".
399 * The following are entries for libpcap type values that have
400 * different meanings on different OSes.
402 * We put these *after* the entries for the platform-independent
403 * libpcap type values for those Wiretap encapsulation types, so
404 * that Wireshark chooses the platform-independent libpcap type
405 * value for those encapsulatioin types, not the platform-dependent
410 * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
411 * seen that define anything other than DLT_ATM_RFC1483 as 11 are
412 * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
413 * which define it as 100, mapping the kernel's value to 100, in
414 * an attempt to hide the different values used on different
417 * If this is a platform where DLT_FR is defined as 11, we
418 * don't handle 11 at all; otherwise, we handle it as
419 * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
420 * captures from BSD/OS if running on platforms other than BSD/OS,
423 * 1) we don't yet support DLT_FR
427 * 2) nothing short of a heuristic would let us interpret
430 #if defined(DLT_FR) && (DLT_FR == 11)
431 { 11, WTAP_ENCAP_FRELAY },
433 { 11, WTAP_ENCAP_ATM_RFC1483 },
437 * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
438 * BSD/OS, and DLT_LOOP on OpenBSD.
440 * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
441 * (it's just like DLT_NULL, only with the AF_ value in network
442 * rather than host byte order - Wireshark figures out the
443 * byte order from the data, so we don't care what byte order
444 * it's in), so if DLT_LOOP is defined as 12, interpret 12
445 * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
446 * as 12, interpret it as WTAP_ENCAP_RAW_IP.
448 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
449 { 12, WTAP_ENCAP_NULL },
450 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
452 * Put entry for Cisco HDLC here.
453 * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
454 * start with a 4-byte Cisco HDLC header?
457 { 12, WTAP_ENCAP_RAW_IP },
461 * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
462 * don't actually generate it. I infer that BSD/OS translates
463 * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
464 * libpcap, as the BSD/OS link-layer header is different;
465 * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
467 * From this, I infer that there's no point in handling 13
470 * 13 is DLT_ATM_RFC1483 on BSD/OS.
472 * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
473 * of decrypted IPsec traffic.
475 * We treat 13 as WTAP_ENCAP_ENC on all systems except those
476 * that define DLT_ATM_RFC1483 as 13 - presumably only
477 * BSD/OS does so - so that, on BSD/OS systems, we still
478 * treate 13 as WTAP_ENCAP_ATM_RFC1483, but, on all other
479 * systems, we can read OpenBSD DLT_ENC captures.
481 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
482 { 13, WTAP_ENCAP_ATM_RFC1483 },
484 { 13, WTAP_ENCAP_ENC },
488 * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
489 * don't actually generate it. I infer that BSD/OS translates
490 * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
491 * libpcap, as the BSD/OS link-layer header is different;
492 * however, in BSD/OS, DLT_PPP_BSDOS is 16.
494 * From this, I infer that there's no point in handling 14
497 * 14 is DLT_RAW on BSD/OS and OpenBSD.
499 { 14, WTAP_ENCAP_RAW_IP },
504 * DLT_SLIP_BSDOS on BSD/OS;
506 * DLT_HIPPI on NetBSD;
508 * DLT_LANE8023 with Alexey Kuznetzov's patches for
511 * DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
514 * but we don't currently handle any of those.
520 * DLT_PPP_BSDOS on BSD/OS;
522 * DLT_HDLC on NetBSD (Cisco HDLC);
524 * DLT_CIP with Alexey Kuznetzov's patches for
525 * Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
527 * DLT_I4L_IP with the ISDN4Linux patches for libpcap
530 #if defined(DLT_CIP) && (DLT_CIP == 16)
531 { 16, WTAP_ENCAP_LINUX_ATM_CLIP },
533 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
534 { 16, WTAP_ENCAP_CHDLC },
538 * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
540 * It is also used as the PF (Packet Filter) logging format beginning
541 * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
542 * defined with the value 17.
544 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
545 { 17, WTAP_ENCAP_OLD_PFLOG },
549 * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
550 * DLT_CIP of 16 that the Alexey Kuznetzov patches for
551 * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
552 * I've not found any libpcap that uses it for any other purpose -
553 * hopefully nobody will do so in the future.
555 { 18, WTAP_ENCAP_LINUX_ATM_CLIP },
558 * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
559 * recent versions I've seen of the Linux ATM distribution;
560 * I've not yet found any libpcap that uses it for any other
561 * purpose - hopefully nobody will do so in the future.
563 { 19, WTAP_ENCAP_LINUX_ATM_CLIP },
568 * If you need a new encapsulation type for libpcap files, do
569 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
570 * add a new encapsulation type by changing an existing entry;
571 * leave the existing entries alone.
573 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
574 * for a new DLT_ value, and specifying the purpose of the new value.
575 * When you get the new DLT_ value, use that numerical value in
576 * the "dlt_value" field of "pcap_to_wtap_map[]".
579 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
582 wtap_pcap_encap_to_wtap_encap(int encap)
586 for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
587 if (pcap_to_wtap_map[i].dlt_value == encap)
588 return pcap_to_wtap_map[i].wtap_encap_value;
590 return WTAP_ENCAP_UNKNOWN;
594 wtap_wtap_encap_to_pcap_encap(int encap)
600 case WTAP_ENCAP_FDDI:
601 case WTAP_ENCAP_FDDI_BITSWAPPED:
602 case WTAP_ENCAP_NETTL_FDDI:
604 * Special-case WTAP_ENCAP_FDDI and
605 * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
606 * to DLT_FDDI (even though that may mean that the bit
607 * order in the FDDI MAC addresses is wrong; so it goes
608 * - libpcap format doesn't record the byte order,
609 * so that's not fixable).
611 return 10; /* that's DLT_FDDI */
613 case WTAP_ENCAP_FRELAY_WITH_PHDR:
615 * Do the same with Frame Relay.
619 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
621 * Map this to DLT_IEEE802_11, for now, even though
622 * that means the radio information will be lost.
623 * Once tcpdump support for the BSD radiotap header
624 * is sufficiently widespread, we should probably
625 * use that, instead - although we should probably
626 * ultimately just have WTAP_ENCAP_IEEE_802_11
627 * as the only Wiretap encapsulation for 802.11,
628 * and have the pseudo-header include a radiotap-style
629 * list of attributes. If we do that, though, we
630 * should probably bypass the regular Wiretap code
631 * when writing out packets during a capture, and just
632 * do the equivalent of a libpcap write (unfortunately,
633 * libpcap doesn't have an "open dump by file descriptor"
634 * function, so we can't just use "pcap_dump()"), so
635 * that we don't spend cycles mapping from libpcap to
636 * Wiretap and then back to libpcap. (There are other
637 * reasons to do that, e.g. to handle AIX libpcap better.)
642 for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
643 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
644 return pcap_to_wtap_map[i].dlt_value;
650 * Various pseudo-headers that appear at the beginning of packet data.
652 * We represent them as sets of offsets, as they might not be aligned on
653 * an appropriate structure boundary in the buffer, and as that makes them
654 * independent of the way the compiler might align fields.
658 * The link-layer header on SunATM packets.
660 #define SUNATM_FLAGS 0 /* destination and traffic type - 1 byte */
661 #define SUNATM_VPI 1 /* VPI - 1 byte */
662 #define SUNATM_VCI 2 /* VCI - 2 bytes */
663 #define SUNATM_LEN 4 /* length of the header */
666 * The link-layer header on Nokia IPSO ATM packets.
668 #define NOKIAATM_FLAGS 0 /* destination - 1 byte */
669 #define NOKIAATM_VPI 1 /* VPI - 1 byte */
670 #define NOKIAATM_VCI 2 /* VCI - 2 bytes */
671 #define NOKIAATM_LEN 4 /* length of the header */
674 * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
677 #define IRDA_SLL_PKTTYPE_OFFSET 0 /* packet type - 2 bytes */
678 /* 12 unused bytes */
679 #define IRDA_SLL_PROTOCOL_OFFSET 14 /* protocol, should be ETH_P_LAPD - 2 bytes */
680 #define IRDA_SLL_LEN 16 /* length of the header */
683 * A header containing additional MTP information.
685 #define MTP2_SENT_OFFSET 0 /* 1 byte */
686 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
687 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
688 #define MTP2_HDR_LEN 4 /* length of the header */
691 * A header containing additional SITA WAN information.
693 #define SITA_FLAGS_OFFSET 0 /* 1 byte */
694 #define SITA_SIGNALS_OFFSET 1 /* 1 byte */
695 #define SITA_ERRORS1_OFFSET 2 /* 1 byte */
696 #define SITA_ERRORS2_OFFSET 3 /* 1 byte */
697 #define SITA_PROTO_OFFSET 4 /* 1 byte */
698 #define SITA_HDR_LEN 5 /* length of the header */
701 * The fake link-layer header of LAPD packets.
704 #define ETH_P_LAPD 0x0030
707 #define LAPD_SLL_PKTTYPE_OFFSET 0 /* packet type - 2 bytes */
708 #define LAPD_SLL_HATYPE_OFFSET 2 /* hardware address type - 2 bytes */
709 #define LAPD_SLL_HALEN_OFFSET 4 /* hardware address length - 2 bytes */
710 #define LAPD_SLL_ADDR_OFFSET 6 /* address - 8 bytes */
711 #define LAPD_SLL_PROTOCOL_OFFSET 14 /* protocol, should be ETH_P_LAPD - 2 bytes */
712 #define LAPD_SLL_LEN 16 /* length of the header */
715 * I2C link-layer on-disk format
717 struct i2c_file_hdr {
723 pcap_read_sunatm_pseudoheader(FILE_T fh,
724 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
726 guint8 atm_phdr[SUNATM_LEN];
731 errno = WTAP_ERR_CANT_READ;
732 bytes_read = file_read(atm_phdr, SUNATM_LEN, fh);
733 if (bytes_read != SUNATM_LEN) {
734 *err = file_error(fh, err_info);
736 *err = WTAP_ERR_SHORT_READ;
740 vpi = atm_phdr[SUNATM_VPI];
741 vci = pntohs(&atm_phdr[SUNATM_VCI]);
743 switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
745 case 0x01: /* LANE */
746 pseudo_header->atm.aal = AAL_5;
747 pseudo_header->atm.type = TRAF_LANE;
750 case 0x02: /* RFC 1483 LLC multiplexed traffic */
751 pseudo_header->atm.aal = AAL_5;
752 pseudo_header->atm.type = TRAF_LLCMX;
755 case 0x05: /* ILMI */
756 pseudo_header->atm.aal = AAL_5;
757 pseudo_header->atm.type = TRAF_ILMI;
760 case 0x06: /* Q.2931 */
761 pseudo_header->atm.aal = AAL_SIGNALLING;
762 pseudo_header->atm.type = TRAF_UNKNOWN;
765 case 0x03: /* MARS (RFC 2022) */
766 pseudo_header->atm.aal = AAL_5;
767 pseudo_header->atm.type = TRAF_UNKNOWN;
770 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
771 pseudo_header->atm.aal = AAL_5;
772 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
777 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
778 * case assume it's AAL_SIGNALLING; we know nothing more
781 * XXX - is this necessary? Or are we guaranteed that
782 * all signalling traffic has a type of 0x06?
784 * XXX - is this guaranteed to be AAL5? Or, if the type is
785 * 0x00 ("raw"), might it be non-AAL5 traffic?
787 if (vpi == 0 && vci == 5)
788 pseudo_header->atm.aal = AAL_SIGNALLING;
790 pseudo_header->atm.aal = AAL_5;
791 pseudo_header->atm.type = TRAF_UNKNOWN;
794 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
796 pseudo_header->atm.vpi = vpi;
797 pseudo_header->atm.vci = vci;
798 pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
800 /* We don't have this information */
801 pseudo_header->atm.flags = 0;
802 pseudo_header->atm.cells = 0;
803 pseudo_header->atm.aal5t_u2u = 0;
804 pseudo_header->atm.aal5t_len = 0;
805 pseudo_header->atm.aal5t_chksum = 0;
811 pcap_read_nokiaatm_pseudoheader(FILE_T fh,
812 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
814 guint8 atm_phdr[NOKIAATM_LEN];
819 errno = WTAP_ERR_CANT_READ;
820 bytes_read = file_read(atm_phdr, NOKIAATM_LEN, fh);
821 if (bytes_read != NOKIAATM_LEN) {
822 *err = file_error(fh, err_info);
824 *err = WTAP_ERR_SHORT_READ;
828 vpi = atm_phdr[NOKIAATM_VPI];
829 vci = pntohs(&atm_phdr[NOKIAATM_VCI]);
831 pseudo_header->atm.vpi = vpi;
832 pseudo_header->atm.vci = vci;
833 pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
835 /* We don't have this information */
836 pseudo_header->atm.flags = 0;
837 pseudo_header->atm.cells = 0;
838 pseudo_header->atm.aal5t_u2u = 0;
839 pseudo_header->atm.aal5t_len = 0;
840 pseudo_header->atm.aal5t_chksum = 0;
846 pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
847 int *err, gchar **err_info)
849 guint8 irda_phdr[IRDA_SLL_LEN];
852 errno = WTAP_ERR_CANT_READ;
853 bytes_read = file_read(irda_phdr, IRDA_SLL_LEN, fh);
854 if (bytes_read != IRDA_SLL_LEN) {
855 *err = file_error(fh, err_info);
857 *err = WTAP_ERR_SHORT_READ;
861 if (pntohs(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
862 *err = WTAP_ERR_BAD_FILE;
863 if (err_info != NULL)
864 *err_info = g_strdup("libpcap: IrDA capture has a packet with an invalid sll_protocol field");
868 pseudo_header->irda.pkttype = pntohs(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
874 pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
876 guint8 mtp2_hdr[MTP2_HDR_LEN];
879 errno = WTAP_ERR_CANT_READ;
880 bytes_read = file_read(mtp2_hdr, MTP2_HDR_LEN, fh);
881 if (bytes_read != MTP2_HDR_LEN) {
882 *err = file_error(fh, err_info);
884 *err = WTAP_ERR_SHORT_READ;
888 pseudo_header->mtp2.sent = mtp2_hdr[MTP2_SENT_OFFSET];
889 pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
890 pseudo_header->mtp2.link_number = pntohs(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
896 pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
897 int *err, gchar **err_info)
899 guint8 lapd_phdr[LAPD_SLL_LEN];
902 errno = WTAP_ERR_CANT_READ;
903 bytes_read = file_read(lapd_phdr, LAPD_SLL_LEN, fh);
904 if (bytes_read != LAPD_SLL_LEN) {
905 *err = file_error(fh, err_info);
907 *err = WTAP_ERR_SHORT_READ;
911 if (pntohs(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
912 *err = WTAP_ERR_BAD_FILE;
913 if (err_info != NULL)
914 *err_info = g_strdup("libpcap: LAPD capture has a packet with an invalid sll_protocol field");
918 pseudo_header->lapd.pkttype = pntohs(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
919 pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
925 pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
927 guint8 sita_phdr[SITA_HDR_LEN];
930 errno = WTAP_ERR_CANT_READ;
931 bytes_read = file_read(sita_phdr, SITA_HDR_LEN, fh);
932 if (bytes_read != SITA_HDR_LEN) {
933 *err = file_error(fh, err_info);
935 *err = WTAP_ERR_SHORT_READ;
939 pseudo_header->sita.sita_flags = sita_phdr[SITA_FLAGS_OFFSET];
940 pseudo_header->sita.sita_signals = sita_phdr[SITA_SIGNALS_OFFSET];
941 pseudo_header->sita.sita_errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
942 pseudo_header->sita.sita_errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
943 pseudo_header->sita.sita_proto = sita_phdr[SITA_PROTO_OFFSET];
949 * When not using the memory-mapped interface to capture USB events,
950 * code that reads those events can use the MON_IOCX_GET ioctl to
951 * read a 48-byte header consisting of a "struct linux_usb_phdr", as
952 * defined below, followed immediately by one of:
954 * 8 bytes of a "struct usb_device_setup_hdr", if "setup_flag"
955 * in the preceding "struct linux_usb_phdr" is 0;
957 * in Linux 2.6.30 or later, 8 bytes of a "struct iso_rec", if
958 * this is an isochronous transfer;
960 * 8 bytes of junk, otherwise.
962 * In Linux 2.6.31 and later, it can also use the MON_IOCX_GETX ioctl
963 * to read a 64-byte header; that header consists of the 48 bytes
964 * above, followed immediately by 16 bytes of a "struct linux_usb_phdr_ext",
967 * In Linux 2.6.21 and later, there's a memory-mapped interface to
968 * capture USB events. In that interface, the events in the memory-mapped
969 * buffer have a 64-byte header, followed immediately by the data.
970 * In Linux 2.6.21 through 2.6.30.x, the 64-byte header is the 48-byte
971 * header described above, followed by 16 bytes of zeroes; in Linux
972 * 2.6.31 and later, the 64-byte header is the 64-byte header described
975 * See linux/Documentation/usb/usbmon.txt and libpcap/pcap/usb.h for details.
977 * With WTAP_ENCAP_USB_LINUX, packets have the 48-byte header; with
978 * WTAP_ENCAP_USB_LINUX_MMAPPED, they have the 64-byte header. There
979 * is no indication of whether the header has the "struct iso_rec", or
980 * whether the last 16 bytes of a 64-byte header are all zeros or are
981 * a "struct linux_usb_phdr_ext".
985 * URB transfer_type values
987 #define URB_ISOCHRONOUS 0x0
988 #define URB_INTERRUPT 0x1
989 #define URB_CONTROL 0x2
993 * Information from the URB for Isochronous transfers.
995 * This structure is 8 bytes long.
1003 * Header prepended by Linux kernel to each USB event.
1005 * (Setup flag is '-', 'D', 'Z', or 0. Data flag is '<', '>', 'Z', or 0.)
1007 * The values are in *host* byte order.
1009 struct linux_usb_phdr {
1010 guint64 id; /* urb id, to link submission and completion events */
1011 guint8 event_type; /* Submit ('S'), Completed ('C'), Error ('E') */
1012 guint8 transfer_type; /* ISO (0), Intr, Control, Bulk (3) */
1013 guint8 endpoint_number; /* Endpoint number (0-15) and transfer direction */
1014 guint8 device_address; /* 0-127 */
1016 gint8 setup_flag; /* 0, if the urb setup header is meaningful */
1017 gint8 data_flag; /* 0, if urb data is present */
1021 guint32 urb_len; /* whole len of urb this event refers to */
1022 guint32 data_len; /* amount of urb data really present in this event */
1025 * Packet-type-dependent data.
1026 * USB setup information of setup_flag is true.
1027 * Otherwise, some isochronous transfer information.
1035 * This data is provided by Linux 2.6.31 and later kernels.
1037 * For WTAP_ENCAP_USB_LINUX, it's not in the pseudo-header, so
1038 * the pseudo-header is always 48 bytes long, including the
1039 * packet-type-dependent data.
1041 * For WTAP_ENCAP_USB_LINUX_MMAPPED, the pseudo-header is always
1042 * 64 bytes long, with the packet-type-dependent data preceding
1043 * these last 16 bytes. In pre-2.6.31 kernels, it's zero padding;
1044 * in 2.6.31 and later, it's the following data.
1046 gint32 interval; /* only for Interrupt and Isochronous events */
1047 gint32 start_frame; /* for Isochronous */
1048 guint32 xfer_flags; /* copy of URB's transfer_flags */
1049 guint32 ndesc; /* actual number of isochronous descriptors */
1052 struct linux_usb_isodesc {
1060 * USB setup header as defined in USB specification
1061 * See usb_20.pdf, Chapter 9.3 'USB Device Requests' for details.
1062 * http://www.usb.org/developers/docs/usb_20_122909-2.zip
1064 * This structure is 8 bytes long.
1066 struct usb_device_setup_hdr {
1067 gint8 bmRequestType;
1076 * Offset of the *end* of a field within a particular structure.
1078 #define END_OFFSETOF(basep, fieldp) \
1079 (((char *)(void *)(fieldp)) - ((char *)(void *)(basep)) + \
1083 pcap_process_linux_usb_pseudoheader(guint packet_size, gboolean byte_swapped,
1084 gboolean header_len_64_bytes, guint8 *pd)
1086 struct linux_usb_phdr *phdr;
1087 struct linux_usb_isodesc *pisodesc;
1088 gint32 iso_numdesc, i;
1092 * Greasy hack, but we never directly direference any of
1093 * the fields in *phdr, we just get offsets of and
1094 * addresses of its members, so it's safe.
1096 phdr = (struct linux_usb_phdr *)(void *)pd;
1098 if (packet_size < END_OFFSETOF(phdr, &phdr->id))
1100 PBSWAP64((guint8 *)&phdr->id);
1101 if (packet_size < END_OFFSETOF(phdr, &phdr->bus_id))
1103 PBSWAP16((guint8 *)&phdr->bus_id);
1104 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_sec))
1106 PBSWAP64((guint8 *)&phdr->ts_sec);
1107 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_usec))
1109 PBSWAP32((guint8 *)&phdr->ts_usec);
1110 if (packet_size < END_OFFSETOF(phdr, &phdr->status))
1112 PBSWAP32((guint8 *)&phdr->status);
1113 if (packet_size < END_OFFSETOF(phdr, &phdr->urb_len))
1115 PBSWAP32((guint8 *)&phdr->urb_len);
1116 if (packet_size < END_OFFSETOF(phdr, &phdr->data_len))
1118 PBSWAP32((guint8 *)&phdr->data_len);
1120 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1121 if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.error_count))
1123 PBSWAP32((guint8 *)&phdr->s.iso.error_count);
1125 if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.numdesc))
1127 PBSWAP32((guint8 *)&phdr->s.iso.numdesc);
1131 if (header_len_64_bytes) {
1133 * This is either the "version 1" header, with
1134 * 16 bytes of additional fields at the end, or
1135 * a "version 0" header from a memory-mapped
1136 * capture, with 16 bytes of zeroed-out padding
1137 * at the end. Byte swap them as if this were
1138 * a "version 1" header.
1140 * Yes, the first argument to END_OFFSETOF() should
1141 * be phdr, not phdr_ext; we want the offset of
1142 * the additional fields from the beginning of
1145 if (packet_size < END_OFFSETOF(phdr, &phdr->interval))
1147 PBSWAP32((guint8 *)&phdr->interval);
1148 if (packet_size < END_OFFSETOF(phdr, &phdr->start_frame))
1150 PBSWAP32((guint8 *)&phdr->start_frame);
1151 if (packet_size < END_OFFSETOF(phdr, &phdr->xfer_flags))
1153 PBSWAP32((guint8 *)&phdr->xfer_flags);
1154 if (packet_size < END_OFFSETOF(phdr, &phdr->ndesc))
1156 PBSWAP32((guint8 *)&phdr->ndesc);
1159 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1160 /* swap the values in struct linux_usb_isodesc */
1163 * See previous "Greasy hack" comment.
1165 if (header_len_64_bytes) {
1166 pisodesc = (struct linux_usb_isodesc*)(void *)(pd + 64);
1168 pisodesc = (struct linux_usb_isodesc*)(void *)(pd + 48);
1170 iso_numdesc = phdr->s.iso.numdesc;
1171 for (i = 0; i < iso_numdesc; i++) {
1172 /* always check if we have enough data from the
1173 * beginnig of the packet (phdr)
1175 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_status))
1177 PBSWAP32((guint8 *)&pisodesc->iso_status);
1178 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_off))
1180 PBSWAP32((guint8 *)&pisodesc->iso_off);
1181 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_len))
1183 PBSWAP32((guint8 *)&pisodesc->iso_len);
1184 if (packet_size < END_OFFSETOF(phdr, &pisodesc->_pad))
1186 PBSWAP32((guint8 *)&pisodesc->_pad);
1195 pcap_read_bt_pseudoheader(FILE_T fh,
1196 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1199 struct libpcap_bt_phdr phdr;
1201 errno = WTAP_ERR_CANT_READ;
1202 bytes_read = file_read(&phdr,
1203 sizeof (struct libpcap_bt_phdr), fh);
1204 if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
1205 *err = file_error(fh, err_info);
1207 *err = WTAP_ERR_SHORT_READ;
1210 pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
1215 pcap_read_ppp_pseudoheader(FILE_T fh,
1216 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1219 struct libpcap_ppp_phdr phdr;
1221 errno = WTAP_ERR_CANT_READ;
1222 bytes_read = file_read(&phdr,
1223 sizeof (struct libpcap_ppp_phdr), fh);
1224 if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
1225 *err = file_error(fh, err_info);
1227 *err = WTAP_ERR_SHORT_READ;
1230 pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
1235 pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
1236 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1238 guint8 erf_hdr[sizeof(struct erf_phdr)];
1241 errno = WTAP_ERR_CANT_READ;
1242 bytes_read = file_read(erf_hdr, sizeof(struct erf_phdr), fh);
1243 if (bytes_read != sizeof(struct erf_phdr)) {
1244 *err = file_error(fh, err_info);
1246 *err = WTAP_ERR_SHORT_READ;
1249 pseudo_header->erf.phdr.ts = pletohll(&erf_hdr[0]); /* timestamp */
1250 pseudo_header->erf.phdr.type = erf_hdr[8];
1251 pseudo_header->erf.phdr.flags = erf_hdr[9];
1252 pseudo_header->erf.phdr.rlen = pntohs(&erf_hdr[10]);
1253 pseudo_header->erf.phdr.lctr = pntohs(&erf_hdr[12]);
1254 pseudo_header->erf.phdr.wlen = pntohs(&erf_hdr[14]);
1256 /* The high 32 bits of the timestamp contain the integer number of seconds
1257 * while the lower 32 bits contain the binary fraction of the second.
1258 * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1260 guint64 ts = pseudo_header->erf.phdr.ts;
1261 whdr->ts.secs = (guint32) (ts >> 32);
1262 ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1263 ts += (ts & 0x80000000) << 1; /* rounding */
1264 whdr->ts.nsecs = ((guint32) (ts >> 32));
1265 if ( whdr->ts.nsecs >= 1000000000) {
1266 whdr->ts.nsecs -= 1000000000;
1274 * If the type of record given in the pseudo header indicate the presence of an extension
1275 * header then, read all the extension headers
1278 pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1279 int *err, gchar **err_info, guint * psize)
1282 guint8 erf_exhdr[8];
1283 guint64 erf_exhdr_sw;
1284 int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1287 if (pseudo_header->erf.phdr.type & 0x80){
1289 errno = WTAP_ERR_CANT_READ;
1290 bytes_read = file_read(erf_exhdr, 8, fh);
1291 if (bytes_read != 8 ) {
1292 *err = file_error(fh, err_info);
1294 *err = WTAP_ERR_SHORT_READ;
1297 type = erf_exhdr[0];
1298 erf_exhdr_sw = pntohll(erf_exhdr);
1300 memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1303 } while (type & 0x80);
1309 * If the type of record given in the pseudo header indicate the precense of a subheader
1310 * then, read this optional subheader
1313 pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1314 int *err, gchar **err_info, guint * psize)
1316 guint8 erf_subhdr[sizeof(union erf_subhdr)];
1320 switch(pseudo_header->erf.phdr.type & 0x7F) {
1321 case ERF_TYPE_MC_HDLC:
1322 case ERF_TYPE_MC_RAW:
1323 case ERF_TYPE_MC_ATM:
1324 case ERF_TYPE_MC_RAW_CHANNEL:
1325 case ERF_TYPE_MC_AAL5:
1326 case ERF_TYPE_MC_AAL2:
1327 case ERF_TYPE_COLOR_MC_HDLC_POS:
1328 /* Extract the Multi Channel header to include it in the pseudo header part */
1329 errno = WTAP_ERR_CANT_READ;
1330 bytes_read = file_read(erf_subhdr, sizeof(erf_mc_header_t), fh);
1331 if (bytes_read != sizeof(erf_mc_header_t) ) {
1332 *err = file_error(fh, err_info);
1334 *err = WTAP_ERR_SHORT_READ;
1337 pseudo_header->erf.subhdr.mc_hdr = pntohl(&erf_subhdr[0]);
1338 *psize = sizeof(erf_mc_header_t);
1341 case ERF_TYPE_COLOR_ETH:
1342 case ERF_TYPE_DSM_COLOR_ETH:
1343 /* Extract the Ethernet additional header to include it in the pseudo header part */
1344 errno = WTAP_ERR_CANT_READ;
1345 bytes_read = file_read(erf_subhdr, sizeof(erf_eth_header_t), fh);
1346 if (bytes_read != sizeof(erf_eth_header_t) ) {
1347 *err = file_error(fh, err_info);
1349 *err = WTAP_ERR_SHORT_READ;
1352 pseudo_header->erf.subhdr.eth_hdr = pntohs(&erf_subhdr[0]);
1353 *psize = sizeof(erf_eth_header_t);
1356 /* No optional pseudo header for this ERF type */
1363 pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1365 struct i2c_file_hdr i2c_hdr;
1368 errno = WTAP_ERR_CANT_READ;
1369 bytes_read = file_read(&i2c_hdr, sizeof (i2c_hdr), fh);
1370 if (bytes_read != sizeof (i2c_hdr)) {
1371 *err = file_error(fh, err_info);
1373 *err = WTAP_ERR_SHORT_READ;
1377 pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
1378 pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
1379 pseudo_header->i2c.flags = pntohl(&i2c_hdr.flags);
1385 pcap_process_pseudo_header(FILE_T fh, int file_type, int wtap_encap,
1386 guint packet_size, gboolean check_packet_size, struct wtap_pkthdr *phdr,
1387 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1392 switch (wtap_encap) {
1394 case WTAP_ENCAP_ATM_PDUS:
1395 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1399 if (check_packet_size && packet_size < NOKIAATM_LEN) {
1401 * Uh-oh, the packet isn't big enough to even
1402 * have a pseudo-header.
1404 *err = WTAP_ERR_BAD_FILE;
1405 *err_info = g_strdup_printf("pcap: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1409 if (!pcap_read_nokiaatm_pseudoheader(fh,
1410 pseudo_header, err, err_info))
1411 return -1; /* Read error */
1413 phdr_len = NOKIAATM_LEN;
1418 if (check_packet_size && packet_size < SUNATM_LEN) {
1420 * Uh-oh, the packet isn't big enough to even
1421 * have a pseudo-header.
1423 *err = WTAP_ERR_BAD_FILE;
1424 *err_info = g_strdup_printf("pcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1428 if (!pcap_read_sunatm_pseudoheader(fh,
1429 pseudo_header, err, err_info))
1430 return -1; /* Read error */
1432 phdr_len = SUNATM_LEN;
1436 case WTAP_ENCAP_ETHERNET:
1438 * We don't know whether there's an FCS in this frame or not.
1440 pseudo_header->eth.fcs_len = -1;
1443 case WTAP_ENCAP_IEEE_802_11:
1444 case WTAP_ENCAP_PRISM_HEADER:
1445 case WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP:
1446 case WTAP_ENCAP_IEEE_802_11_WLAN_AVS:
1448 * We don't know whether there's an FCS in this frame or not.
1449 * XXX - are there any OSes where the capture mechanism
1452 pseudo_header->ieee_802_11.fcs_len = -1;
1453 pseudo_header->ieee_802_11.channel = 0;
1454 pseudo_header->ieee_802_11.data_rate = 0;
1455 pseudo_header->ieee_802_11.signal_level = 0;
1458 case WTAP_ENCAP_IRDA:
1459 if (check_packet_size && packet_size < IRDA_SLL_LEN) {
1461 * Uh-oh, the packet isn't big enough to even
1462 * have a pseudo-header.
1464 *err = WTAP_ERR_BAD_FILE;
1465 *err_info = g_strdup_printf("pcap: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1469 if (!pcap_read_irda_pseudoheader(fh, pseudo_header,
1471 return -1; /* Read error */
1473 phdr_len = IRDA_SLL_LEN;
1476 case WTAP_ENCAP_MTP2_WITH_PHDR:
1477 if (check_packet_size && packet_size < MTP2_HDR_LEN) {
1479 * Uh-oh, the packet isn't big enough to even
1480 * have a pseudo-header.
1482 *err = WTAP_ERR_BAD_FILE;
1483 *err_info = g_strdup_printf("pcap: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1487 if (!pcap_read_mtp2_pseudoheader(fh, pseudo_header,
1489 return -1; /* Read error */
1491 phdr_len = MTP2_HDR_LEN;
1494 case WTAP_ENCAP_LINUX_LAPD:
1495 if (check_packet_size && packet_size < LAPD_SLL_LEN) {
1497 * Uh-oh, the packet isn't big enough to even
1498 * have a pseudo-header.
1500 *err = WTAP_ERR_BAD_FILE;
1501 *err_info = g_strdup_printf("pcap: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1505 if (!pcap_read_lapd_pseudoheader(fh, pseudo_header,
1507 return -1; /* Read error */
1509 phdr_len = LAPD_SLL_LEN;
1512 case WTAP_ENCAP_SITA:
1513 if (check_packet_size && packet_size < SITA_HDR_LEN) {
1515 * Uh-oh, the packet isn't big enough to even
1516 * have a pseudo-header.
1518 *err = WTAP_ERR_BAD_FILE;
1519 *err_info = g_strdup_printf("pcap: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1523 if (!pcap_read_sita_pseudoheader(fh, pseudo_header,
1525 return -1; /* Read error */
1527 phdr_len = SITA_HDR_LEN;
1530 case WTAP_ENCAP_BLUETOOTH_H4:
1531 /* We don't have pseudoheader, so just pretend we received everything. */
1532 pseudo_header->p2p.sent = FALSE;
1535 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1536 if (check_packet_size &&
1537 packet_size < sizeof (struct libpcap_bt_phdr)) {
1539 * Uh-oh, the packet isn't big enough to even
1540 * have a pseudo-header.
1542 *err = WTAP_ERR_BAD_FILE;
1543 *err_info = g_strdup_printf("pcap: libpcap bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1547 if (!pcap_read_bt_pseudoheader(fh,
1548 pseudo_header, err, err_info))
1549 return -1; /* Read error */
1551 phdr_len = (int)sizeof (struct libpcap_bt_phdr);
1554 case WTAP_ENCAP_PPP_WITH_PHDR:
1555 if (check_packet_size &&
1556 packet_size < sizeof (struct libpcap_ppp_phdr)) {
1558 * Uh-oh, the packet isn't big enough to even
1559 * have a pseudo-header.
1561 *err = WTAP_ERR_BAD_FILE;
1562 *err_info = g_strdup_printf("pcap: libpcap ppp file has a %u-byte packet, too small to have even a pseudo-header",
1566 if (!pcap_read_ppp_pseudoheader(fh,
1567 pseudo_header, err, err_info))
1568 return -1; /* Read error */
1570 phdr_len = (int)sizeof (struct libpcap_ppp_phdr);
1573 case WTAP_ENCAP_ERF:
1574 if (check_packet_size &&
1575 packet_size < sizeof(struct erf_phdr) ) {
1577 * Uh-oh, the packet isn't big enough to even
1578 * have a pseudo-header.
1580 *err = WTAP_ERR_BAD_FILE;
1581 *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1586 if (!pcap_read_erf_pseudoheader(fh, phdr, pseudo_header,
1588 return -1; /* Read error */
1590 phdr_len = (int)sizeof(struct erf_phdr);
1592 /* check the optional Extension header */
1593 if (!pcap_read_erf_exheader(fh, pseudo_header, err, err_info,
1595 return -1; /* Read error */
1599 /* check the optional Multi Channel header */
1600 if (!pcap_read_erf_subheader(fh, pseudo_header, err, err_info,
1602 return -1; /* Read error */
1607 case WTAP_ENCAP_I2C:
1608 if (check_packet_size &&
1609 packet_size < sizeof (struct i2c_file_hdr)) {
1611 * Uh-oh, the packet isn't big enough to even
1612 * have a pseudo-header.
1614 *err = WTAP_ERR_BAD_FILE;
1615 *err_info = g_strdup_printf("pcap: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1619 if (!pcap_read_i2c_pseudoheader(fh, pseudo_header,
1621 return -1; /* Read error */
1624 * Don't count the pseudo-header as part of the packet.
1626 phdr_len = (int)sizeof (struct i2c_file_hdr);
1634 pcap_read_post_process(int file_type, int wtap_encap,
1635 union wtap_pseudo_header *pseudo_header,
1636 guint8 *pd, guint packet_size, gboolean bytes_swapped, int fcs_len)
1638 switch (wtap_encap) {
1640 case WTAP_ENCAP_ATM_PDUS:
1641 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1645 * Guess the traffic type based on the packet
1648 atm_guess_traffic_type(pd, packet_size, pseudo_header);
1653 * If this is ATM LANE traffic, try to guess what
1654 * type of LANE traffic it is based on the packet
1657 if (pseudo_header->atm.type == TRAF_LANE)
1658 atm_guess_lane_type(pd, packet_size,
1663 case WTAP_ENCAP_ETHERNET:
1664 pseudo_header->eth.fcs_len = fcs_len;
1667 case WTAP_ENCAP_USB_LINUX:
1668 pcap_process_linux_usb_pseudoheader(packet_size,
1669 bytes_swapped, FALSE, pd);
1672 case WTAP_ENCAP_USB_LINUX_MMAPPED:
1673 pcap_process_linux_usb_pseudoheader(packet_size,
1674 bytes_swapped, TRUE, pd);
1677 case WTAP_ENCAP_NETANALYZER:
1679 * Not strictly necessary, as the netANALYZER
1680 * dissector calls the "Ethernet with FCS"
1681 * dissector, but we might as well set it.
1683 pseudo_header->eth.fcs_len = 4;
1692 pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
1698 case WTAP_ENCAP_ATM_PDUS:
1699 hdrsize = SUNATM_LEN;
1702 case WTAP_ENCAP_IRDA:
1703 hdrsize = IRDA_SLL_LEN;
1706 case WTAP_ENCAP_MTP2_WITH_PHDR:
1707 hdrsize = MTP2_HDR_LEN;
1710 case WTAP_ENCAP_LINUX_LAPD:
1711 hdrsize = LAPD_SLL_LEN;
1714 case WTAP_ENCAP_SITA:
1715 hdrsize = SITA_HDR_LEN;
1718 case WTAP_ENCAP_ERF:
1719 hdrsize = (int)sizeof (struct erf_phdr);
1720 switch (pseudo_header->erf.phdr.type & 0x7F) {
1722 case ERF_TYPE_MC_HDLC:
1723 case ERF_TYPE_MC_RAW:
1724 case ERF_TYPE_MC_ATM:
1725 case ERF_TYPE_MC_RAW_CHANNEL:
1726 case ERF_TYPE_MC_AAL5:
1727 case ERF_TYPE_MC_AAL2:
1728 case ERF_TYPE_COLOR_MC_HDLC_POS:
1729 hdrsize += (int)sizeof(struct erf_mc_hdr);
1733 case ERF_TYPE_COLOR_ETH:
1734 case ERF_TYPE_DSM_COLOR_ETH:
1735 hdrsize += (int)sizeof(struct erf_eth_hdr);
1743 * Add in the lengths of the extension headers.
1745 if (pseudo_header->erf.phdr.type & 0x80) {
1746 int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1747 guint8 erf_exhdr[8];
1751 phtonll(erf_exhdr, pseudo_header->erf.ehdr_list[i].ehdr);
1752 type = erf_exhdr[0];
1755 } while (type & 0x80 && i < max);
1759 case WTAP_ENCAP_I2C:
1760 hdrsize = (int)sizeof (struct i2c_file_hdr);
1763 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1764 hdrsize = (int)sizeof (struct libpcap_bt_phdr);
1767 case WTAP_ENCAP_PPP_WITH_PHDR:
1768 hdrsize = (int)sizeof (struct libpcap_ppp_phdr);
1780 pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
1783 guint8 atm_hdr[SUNATM_LEN];
1784 guint8 irda_hdr[IRDA_SLL_LEN];
1785 guint8 lapd_hdr[LAPD_SLL_LEN];
1786 guint8 mtp2_hdr[MTP2_HDR_LEN];
1787 guint8 sita_hdr[SITA_HDR_LEN];
1788 guint8 erf_hdr[ sizeof(struct erf_mc_phdr)];
1789 struct i2c_file_hdr i2c_hdr;
1790 struct libpcap_bt_phdr bt_hdr;
1791 struct libpcap_ppp_phdr ppp_hdr;
1796 case WTAP_ENCAP_ATM_PDUS:
1798 * Write the ATM header.
1800 atm_hdr[SUNATM_FLAGS] =
1801 (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1802 switch (pseudo_header->atm.aal) {
1804 case AAL_SIGNALLING:
1806 atm_hdr[SUNATM_FLAGS] |= 0x06;
1810 switch (pseudo_header->atm.type) {
1814 atm_hdr[SUNATM_FLAGS] |= 0x01;
1818 /* RFC 1483 LLC multiplexed traffic */
1819 atm_hdr[SUNATM_FLAGS] |= 0x02;
1824 atm_hdr[SUNATM_FLAGS] |= 0x05;
1829 atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
1830 phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1831 if (!wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr), err))
1833 wdh->bytes_dumped += sizeof(atm_hdr);
1836 case WTAP_ENCAP_IRDA:
1838 * Write the IrDA header.
1840 memset(irda_hdr, 0, sizeof(irda_hdr));
1841 phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
1842 pseudo_header->irda.pkttype);
1843 phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
1844 if (!wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr), err))
1846 wdh->bytes_dumped += sizeof(irda_hdr);
1849 case WTAP_ENCAP_MTP2_WITH_PHDR:
1851 * Write the MTP2 header.
1853 memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
1854 mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
1855 mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
1856 phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
1857 pseudo_header->mtp2.link_number);
1858 if (!wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr), err))
1860 wdh->bytes_dumped += sizeof(mtp2_hdr);
1863 case WTAP_ENCAP_LINUX_LAPD:
1865 * Write the LAPD header.
1867 memset(&lapd_hdr, 0, sizeof(lapd_hdr));
1868 phtons(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET],
1869 pseudo_header->lapd.pkttype);
1870 phtons(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
1871 lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
1872 pseudo_header->lapd.we_network?0x01:0x00;
1873 if (!wtap_dump_file_write(wdh, lapd_hdr, sizeof(lapd_hdr), err))
1875 wdh->bytes_dumped += sizeof(lapd_hdr);
1878 case WTAP_ENCAP_SITA:
1880 * Write the SITA header.
1882 memset(&sita_hdr, 0, sizeof(sita_hdr));
1883 sita_hdr[SITA_FLAGS_OFFSET] = pseudo_header->sita.sita_flags;
1884 sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.sita_signals;
1885 sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.sita_errors1;
1886 sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.sita_errors2;
1887 sita_hdr[SITA_PROTO_OFFSET] = pseudo_header->sita.sita_proto;
1888 if (!wtap_dump_file_write(wdh, sita_hdr, sizeof(sita_hdr), err))
1890 wdh->bytes_dumped += sizeof(sita_hdr);
1893 case WTAP_ENCAP_ERF:
1895 * Write the ERF header.
1897 memset(&erf_hdr, 0, sizeof(erf_hdr));
1898 phtolell(&erf_hdr[0], pseudo_header->erf.phdr.ts);
1899 erf_hdr[8] = pseudo_header->erf.phdr.type;
1900 erf_hdr[9] = pseudo_header->erf.phdr.flags;
1901 phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
1902 phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
1903 phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
1904 size = sizeof(struct erf_phdr);
1906 switch(pseudo_header->erf.phdr.type & 0x7F) {
1907 case ERF_TYPE_MC_HDLC:
1908 case ERF_TYPE_MC_RAW:
1909 case ERF_TYPE_MC_ATM:
1910 case ERF_TYPE_MC_RAW_CHANNEL:
1911 case ERF_TYPE_MC_AAL5:
1912 case ERF_TYPE_MC_AAL2:
1913 case ERF_TYPE_COLOR_MC_HDLC_POS:
1914 phtonl(&erf_hdr[16], pseudo_header->erf.subhdr.mc_hdr);
1915 size += (int)sizeof(struct erf_mc_hdr);
1918 case ERF_TYPE_COLOR_ETH:
1919 case ERF_TYPE_DSM_COLOR_ETH:
1920 phtons(&erf_hdr[16], pseudo_header->erf.subhdr.eth_hdr);
1921 size += (int)sizeof(struct erf_eth_hdr);
1926 if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
1928 wdh->bytes_dumped += size;
1931 * Now write out the extension headers.
1933 if (pseudo_header->erf.phdr.type & 0x80) {
1934 int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1935 guint8 erf_exhdr[8];
1939 phtonll(erf_exhdr, pseudo_header->erf.ehdr_list[i].ehdr);
1940 type = erf_exhdr[0];
1941 if (!wtap_dump_file_write(wdh, erf_exhdr, 8, err))
1943 wdh->bytes_dumped += 8;
1945 } while (type & 0x80 && i < max);
1949 case WTAP_ENCAP_I2C:
1951 * Write the I2C header.
1953 memset(&i2c_hdr, 0, sizeof(i2c_hdr));
1954 i2c_hdr.bus = pseudo_header->i2c.bus |
1955 (pseudo_header->i2c.is_event ? 0x80 : 0x00);
1956 phtonl((guint8 *)&i2c_hdr.flags, pseudo_header->i2c.flags);
1957 if (!wtap_dump_file_write(wdh, &i2c_hdr, sizeof(i2c_hdr), err))
1959 wdh->bytes_dumped += sizeof(i2c_hdr);
1962 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1963 bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
1964 if (!wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr, err))
1966 wdh->bytes_dumped += sizeof bt_hdr;
1969 case WTAP_ENCAP_PPP_WITH_PHDR:
1970 ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
1971 if (!wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr, err))
1973 wdh->bytes_dumped += sizeof ppp_hdr;