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"
37 #include "pcap-encap.h"
38 #include "pcap-common.h"
41 * Map link-layer types (LINKTYPE_ values) to Wiretap encapsulations.
44 * Either LBL NRG wasn't an adequate central registry (e.g., because of
45 * the slow rate of releases from them), or nobody bothered using them
46 * as a central registry, as many different groups have patched libpcap
47 * (and BPF, on the BSDs) to add new encapsulation types, and have ended
48 * up using the same DLT_ values for different encapsulation types.
50 * For those numerical encapsulation type values that everybody uses for
51 * the same encapsulation type (which inclues those that some platforms
52 * specify different DLT_ names for but don't appear to use), we map
53 * those values to the appropriate Wiretap values.
55 * For those numerical encapsulation type values that different libpcap
56 * variants use for different encapsulation types, we check what
57 * <pcap.h> defined to determine how to interpret them, so that we
58 * interpret them the way the libpcap with which we're building
59 * Wireshark/Wiretap interprets them (which, if it doesn't support
60 * them at all, means we don't support them either - any capture files
61 * using them are foreign, and we don't hazard a guess as to which
62 * platform they came from; we could, I guess, choose the most likely
65 * Note: if you need a new encapsulation type for libpcap files, do
66 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
67 * add a new encapsulation type by changing an existing entry;
68 * leave the existing entries alone.
70 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking for
71 * a new DLT_ value, and specifying the purpose of the new value. When
72 * you get the new DLT_ value, use that numerical value in the "dlt_value"
73 * field of "pcap_to_wtap_map[]".
79 } pcap_to_wtap_map[] = {
81 * These are the values that are almost certainly the same
82 * in all libpcaps (I've yet to find one where the values
83 * in question are used for some purpose other than the
84 * one below, but...), and that Wiretap and Wireshark
87 { 0, WTAP_ENCAP_NULL }, /* null encapsulation */
88 { 1, WTAP_ENCAP_ETHERNET },
89 { 6, WTAP_ENCAP_TOKEN_RING }, /* IEEE 802 Networks - assume token ring */
90 { 7, WTAP_ENCAP_ARCNET },
91 { 8, WTAP_ENCAP_SLIP },
92 { 9, WTAP_ENCAP_PPP },
93 #ifdef BIT_SWAPPED_MAC_ADDRS
94 { 10, WTAP_ENCAP_FDDI_BITSWAPPED },
96 { 10, WTAP_ENCAP_FDDI },
99 { 32, WTAP_ENCAP_REDBACK },
102 * 50 is DLT_PPP_SERIAL in NetBSD; it appears that DLT_PPP
103 * on BSD (at least according to standard tcpdump) has, as
104 * the first octet, an indication of whether the packet was
105 * transmitted or received (rather than having the standard
106 * PPP address value of 0xff), but that DLT_PPP_SERIAL puts
107 * a real live PPP header there, or perhaps a Cisco PPP header
108 * as per section 4.3.1 of RFC 1547 (implementations of this
109 * exist in various BSDs in "sys/net/if_spppsubr.c", and
110 * I think also exist either in standard Linux or in
111 * various Linux patches; the implementations show how to handle
112 * Cisco keepalive packets).
114 * However, I don't see any obvious place in FreeBSD "if_ppp.c"
115 * where anything other than the standard PPP header would be
116 * passed up. I see some stuff that sets the first octet
117 * to 0 for incoming and 1 for outgoing packets before applying
118 * a BPF filter to see whether to drop packets whose protocol
119 * field has the 0x8000 bit set, i.e. network control protocols -
120 * those are handed up to userland - but that code puts the
121 * address field back before passing the packet up.
123 * I also don't see anything immediately obvious that munges
124 * the address field for sync PPP, either.
126 * Wireshark currently assumes that if the first octet of a
127 * PPP frame is 0xFF, it's the address field and is followed
128 * by a control field and a 2-byte protocol, otherwise the
129 * address and control fields are absent and the frame begins
130 * with a protocol field. If we ever see a BSD/OS PPP
131 * capture, we'll have to handle it differently, and we may
132 * have to handle standard BSD captures differently if, in fact,
133 * they don't have 0xff 0x03 as the first two bytes - but, as per
134 * the two paragraphs preceding this, it's not clear that
135 * the address field *is* munged into an incoming/outgoing
136 * field when the packet is handed to the BPF device.
138 * For now, we just map DLT_PPP_SERIAL to WTAP_ENCAP_PPP, as
139 * we treat WTAP_ENCAP_PPP packets as if those beginning with
140 * 0xff have the standard RFC 1662 "PPP in HDLC-like Framing"
141 * 0xff 0x03 address/control header, and DLT_PPP_SERIAL frames
142 * appear to contain that unless they're Cisco frames (if we
143 * ever see a capture with them, we'd need to implement the
144 * RFC 1547 stuff, and the keepalive protocol stuff).
146 * We may have to distinguish between "PPP where if it doesn't
147 * begin with 0xff there's no HDLC encapsulation and the frame
148 * begins with the protocol field" (which is how we handle
149 * WTAP_ENCAP_PPP now) and "PPP where there's either HDLC
150 * encapsulation or Cisco PPP" (which is what DLT_PPP_SERIAL
153 * XXX - NetBSD has DLT_HDLC, which appears to be used for
154 * Cisco HDLC. Ideally, they should use DLT_PPP_SERIAL
155 * only for real live HDLC-encapsulated PPP, not for Cisco
158 { 50, WTAP_ENCAP_PPP },
161 * Apparently used by the Axent Raptor firewall (now Symantec
162 * Enterprise Firewall).
163 * Thanks, Axent, for not reserving that type with tcpdump.org
164 * and not telling anybody about it.
166 { 99, WTAP_ENCAP_SYMANTEC },
169 * These are the values that libpcap 0.5 and later use in
170 * capture file headers, in an attempt to work around the
171 * confusion decried above, and that Wiretap and Wireshark
174 { 100, WTAP_ENCAP_ATM_RFC1483 },
175 { 101, WTAP_ENCAP_RAW_IP },
178 * More values used by libpcap 0.5 as DLT_ values and used by the
179 * current CVS version of libpcap in capture file headers.
180 * They are not yet handled in Wireshark.
181 * If we get a capture that contains them, we'll implement them.
183 { 102, WTAP_ENCAP_SLIP_BSDOS },
184 { 103, WTAP_ENCAP_PPP_BSDOS },
188 * These ones are handled in Wireshark, though.
190 { 104, WTAP_ENCAP_CHDLC }, /* Cisco HDLC */
191 { 105, WTAP_ENCAP_IEEE_802_11 }, /* IEEE 802.11 */
192 { 106, WTAP_ENCAP_LINUX_ATM_CLIP },
193 { 107, WTAP_ENCAP_FRELAY }, /* Frame Relay */
194 { 108, WTAP_ENCAP_NULL }, /* OpenBSD loopback */
195 { 109, WTAP_ENCAP_ENC }, /* OpenBSD IPSEC enc */
197 { 110, WTAP_ENCAP_LANE_802_3 },/* ATM LANE 802.3 */
198 { 111, WTAP_ENCAP_HIPPI }, /* NetBSD HIPPI */
200 { 112, WTAP_ENCAP_CHDLC }, /* NetBSD HDLC framing */
203 * Linux "cooked mode" captures, used by the current CVS version
206 * it could be a packet in Cisco's ERSPAN encapsulation which uses
207 * this number as well (why can't people stick to protocols when it
208 * comes to allocating/using DLT types).
210 { 113, WTAP_ENCAP_SLL }, /* Linux cooked capture */
212 { 114, WTAP_ENCAP_LOCALTALK }, /* Localtalk */
215 * The tcpdump.org version of libpcap uses 117, rather than 17,
216 * for OpenBSD packet filter logging, so as to avoid conflicting
217 * with DLT_LANE8023 in SuSE 6.3 libpcap.
219 { 117, WTAP_ENCAP_PFLOG },
221 { 118, WTAP_ENCAP_CISCO_IOS },
222 { 119, WTAP_ENCAP_PRISM_HEADER }, /* Prism monitor mode hdr */
223 { 121, WTAP_ENCAP_HHDLC }, /* HiPath HDLC */
224 { 122, WTAP_ENCAP_IP_OVER_FC }, /* RFC 2625 IP-over-FC */
225 { 123, WTAP_ENCAP_ATM_PDUS }, /* SunATM */
226 { 127, WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP }, /* 802.11 plus radiotap WLAN header */
227 { 128, WTAP_ENCAP_TZSP }, /* Tazmen Sniffer Protocol */
228 { 129, WTAP_ENCAP_ARCNET_LINUX },
229 { 130, WTAP_ENCAP_JUNIPER_MLPPP }, /* Juniper MLPPP on ML-, LS-, AS- PICs */
230 { 131, WTAP_ENCAP_JUNIPER_MLFR }, /* Juniper MLFR (FRF.15) on ML-, LS-, AS- PICs */
231 { 133, WTAP_ENCAP_JUNIPER_GGSN},
233 * Values 132-134, 136 not listed here are reserved for use
234 * in Juniper hardware.
236 { 135, WTAP_ENCAP_JUNIPER_ATM2 }, /* various encapsulations captured on the ATM2 PIC */
237 { 137, WTAP_ENCAP_JUNIPER_ATM1 }, /* various encapsulations captured on the ATM1 PIC */
239 { 138, WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 },
240 /* Apple IP-over-IEEE 1394 */
242 { 139, WTAP_ENCAP_MTP2_WITH_PHDR },
243 { 140, WTAP_ENCAP_MTP2 },
244 { 141, WTAP_ENCAP_MTP3 },
245 { 142, WTAP_ENCAP_SCCP },
246 { 143, WTAP_ENCAP_DOCSIS },
247 { 144, WTAP_ENCAP_IRDA }, /* IrDA capture */
249 /* Reserved for private use. */
250 { 147, WTAP_ENCAP_USER0 },
251 { 148, WTAP_ENCAP_USER1 },
252 { 149, WTAP_ENCAP_USER2 },
253 { 150, WTAP_ENCAP_USER3 },
254 { 151, WTAP_ENCAP_USER4 },
255 { 152, WTAP_ENCAP_USER5 },
256 { 153, WTAP_ENCAP_USER6 },
257 { 154, WTAP_ENCAP_USER7 },
258 { 155, WTAP_ENCAP_USER8 },
259 { 156, WTAP_ENCAP_USER9 },
260 { 157, WTAP_ENCAP_USER10 },
261 { 158, WTAP_ENCAP_USER11 },
262 { 159, WTAP_ENCAP_USER12 },
263 { 160, WTAP_ENCAP_USER13 },
264 { 161, WTAP_ENCAP_USER14 },
265 { 162, WTAP_ENCAP_USER15 },
267 { 163, WTAP_ENCAP_IEEE_802_11_WLAN_AVS }, /* 802.11 plus AVS WLAN header */
270 * 164 is reserved for Juniper-private chassis-internal
271 * meta-information such as QoS profiles, etc..
274 { 165, WTAP_ENCAP_BACNET_MS_TP },
277 * 166 is reserved for a PPP variant in which the first byte
278 * of the 0xff03 header, the 0xff, is replaced by a direction
279 * byte. I don't know whether any captures look like that,
280 * but it is used for some Linux IP filtering (ipfilter?).
283 /* Ethernet PPPoE frames captured on a service PIC */
284 { 167, WTAP_ENCAP_JUNIPER_PPPOE },
287 * 168 is reserved for more Juniper private-chassis-
288 * internal meta-information.
291 { 169, WTAP_ENCAP_GPRS_LLC },
294 * 170 and 171 are reserved for ITU-T G.7041/Y.1303 Generic
298 /* Registered by Gcom, Inc. */
299 { 172, WTAP_ENCAP_GCOM_TIE1 },
300 { 173, WTAP_ENCAP_GCOM_SERIAL },
302 { 177, WTAP_ENCAP_LINUX_LAPD },
304 /* Ethernet frames prepended with meta-information */
305 { 178, WTAP_ENCAP_JUNIPER_ETHER },
306 /* PPP frames prepended with meta-information */
307 { 179, WTAP_ENCAP_JUNIPER_PPP },
308 /* Frame-Relay frames prepended with meta-information */
309 { 180, WTAP_ENCAP_JUNIPER_FRELAY },
310 /* C-HDLC frames prepended with meta-information */
311 { 181, WTAP_ENCAP_JUNIPER_CHDLC },
312 /* VOIP Frames prepended with meta-information */
313 { 183, WTAP_ENCAP_JUNIPER_VP },
314 /* raw USB packets */
315 { 186, WTAP_ENCAP_USB },
316 /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
317 { 187, WTAP_ENCAP_BLUETOOTH_H4 },
318 /* IEEE 802.16 MAC Common Part Sublayer */
319 { 188, WTAP_ENCAP_IEEE802_16_MAC_CPS },
320 /* USB packets with Linux-specified header */
321 { 189, WTAP_ENCAP_USB_LINUX },
323 { 190, WTAP_ENCAP_CAN20B },
324 /* Per-Packet Information header */
325 { 192, WTAP_ENCAP_PPI },
326 /* IEEE 802.15.4 Wireless PAN */
327 { 195, WTAP_ENCAP_IEEE802_15_4 },
328 /* SITA File Encapsulation */
329 { 196, WTAP_ENCAP_SITA },
330 /* Endace Record File Encapsulation */
331 { 197, WTAP_ENCAP_ERF },
333 { 199, WTAP_ENCAP_IPMB },
334 /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
335 { 201, WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR },
337 { 203, WTAP_ENCAP_LAPD },
338 /* PPP with pseudoheader */
339 { 204, WTAP_ENCAP_PPP_WITH_PHDR },
341 { 209, WTAP_ENCAP_I2C },
343 { 210, WTAP_ENCAP_FLEXRAY },
345 { 211, WTAP_ENCAP_MOST },
347 { 212, WTAP_ENCAP_LIN },
348 /* X2E Xoraya serial frame */
349 { 213, WTAP_ENCAP_X2E_SERIAL },
350 /* X2E Xoraya frame */
351 { 214, WTAP_ENCAP_X2E_XORAYA },
352 /* IEEE 802.15.4 Wireless PAN non-ASK PHY */
353 { 215, WTAP_ENCAP_IEEE802_15_4_NONASK_PHY },
354 /* USB packets with padded Linux-specified header */
355 { 220, WTAP_ENCAP_USB_LINUX_MMAPPED },
356 /* Fibre Channel FC-2 frame */
357 { 224, WTAP_ENCAP_FIBRE_CHANNEL_FC2 },
358 /* Fibre Channel FC-2 frame with Delimiter */
359 { 225, WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS },
361 { 226, WTAP_ENCAP_IPNET },
362 /* SocketCAN frame */
363 { 227, WTAP_ENCAP_SOCKETCAN },
365 { 228, WTAP_ENCAP_RAW_IP4 },
367 { 229, WTAP_ENCAP_RAW_IP6 },
368 /* IEEE 802.15.4 Wireless PAN no fcs */
369 { 230, WTAP_ENCAP_IEEE802_15_4_NOFCS },
370 /* DVB-CI (Common Interface) */
371 { 235, WTAP_ENCAP_DVBCI },
376 * If you need a new encapsulation type for libpcap files, do
377 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
378 * add a new encapsulation type by changing an existing entry;
379 * leave the existing entries alone.
381 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
382 * for a new DLT_ value, and specifying the purpose of the new value.
383 * When you get the new DLT_ value, use that numerical value in
384 * the "dlt_value" field of "pcap_to_wtap_map[]".
388 * The following are entries for libpcap type values that have
389 * different meanings on different OSes.
391 * We put these *after* the entries for the platform-independent
392 * libpcap type values for those Wiretap encapsulation types, so
393 * that Wireshark chooses the platform-independent libpcap type
394 * value for those encapsulatioin types, not the platform-dependent
399 * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
400 * seen that define anything other than DLT_ATM_RFC1483 as 11 are
401 * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
402 * which define it as 100, mapping the kernel's value to 100, in
403 * an attempt to hide the different values used on different
406 * If this is a platform where DLT_FR is defined as 11, we
407 * don't handle 11 at all; otherwise, we handle it as
408 * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
409 * captures from BSD/OS if running on platforms other than BSD/OS,
412 * 1) we don't yet support DLT_FR
416 * 2) nothing short of a heuristic would let us interpret
419 #if defined(DLT_FR) && (DLT_FR == 11)
420 { 11, WTAP_ENCAP_FRELAY },
422 { 11, WTAP_ENCAP_ATM_RFC1483 },
426 * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
427 * BSD/OS, and DLT_LOOP on OpenBSD.
429 * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
430 * (it's just like DLT_NULL, only with the AF_ value in network
431 * rather than host byte order - Wireshark figures out the
432 * byte order from the data, so we don't care what byte order
433 * it's in), so if DLT_LOOP is defined as 12, interpret 12
434 * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
435 * as 12, interpret it as WTAP_ENCAP_RAW_IP.
437 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
438 { 12, WTAP_ENCAP_NULL },
439 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
441 * Put entry for Cisco HDLC here.
442 * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
443 * start with a 4-byte Cisco HDLC header?
446 { 12, WTAP_ENCAP_RAW_IP },
450 * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
451 * don't actually generate it. I infer that BSD/OS translates
452 * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
453 * libpcap, as the BSD/OS link-layer header is different;
454 * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
456 * From this, I infer that there's no point in handling 13
459 * 13 is DLT_ATM_RFC1483 on BSD/OS.
461 * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
462 * of decrypted IPsec traffic.
464 * We treat 13 as WTAP_ENCAP_ENC on all systems except those
465 * that define DLT_ATM_RFC1483 as 13 - presumably only
466 * BSD/OS does so - so that, on BSD/OS systems, we still
467 * treate 13 as WTAP_ENCAP_ATM_RFC1483, but, on all other
468 * systems, we can read OpenBSD DLT_ENC captures.
470 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
471 { 13, WTAP_ENCAP_ATM_RFC1483 },
473 { 13, WTAP_ENCAP_ENC },
477 * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
478 * don't actually generate it. I infer that BSD/OS translates
479 * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
480 * libpcap, as the BSD/OS link-layer header is different;
481 * however, in BSD/OS, DLT_PPP_BSDOS is 16.
483 * From this, I infer that there's no point in handling 14
486 * 14 is DLT_RAW on BSD/OS and OpenBSD.
488 { 14, WTAP_ENCAP_RAW_IP },
493 * DLT_SLIP_BSDOS on BSD/OS;
495 * DLT_HIPPI on NetBSD;
497 * DLT_LANE8023 with Alexey Kuznetzov's patches for
500 * DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
503 * but we don't currently handle any of those.
509 * DLT_PPP_BSDOS on BSD/OS;
511 * DLT_HDLC on NetBSD (Cisco HDLC);
513 * DLT_CIP with Alexey Kuznetzov's patches for
514 * Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
516 * DLT_I4L_IP with the ISDN4Linux patches for libpcap
519 #if defined(DLT_CIP) && (DLT_CIP == 16)
520 { 16, WTAP_ENCAP_LINUX_ATM_CLIP },
522 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
523 { 16, WTAP_ENCAP_CHDLC },
527 * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
529 * It is also used as the PF (Packet Filter) logging format beginning
530 * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
531 * defined with the value 17.
533 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
534 { 17, WTAP_ENCAP_OLD_PFLOG },
538 * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
539 * DLT_CIP of 16 that the Alexey Kuznetzov patches for
540 * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
541 * I've not found any libpcap that uses it for any other purpose -
542 * hopefully nobody will do so in the future.
544 { 18, WTAP_ENCAP_LINUX_ATM_CLIP },
547 * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
548 * recent versions I've seen of the Linux ATM distribution;
549 * I've not yet found any libpcap that uses it for any other
550 * purpose - hopefully nobody will do so in the future.
552 { 19, WTAP_ENCAP_LINUX_ATM_CLIP },
557 * If you need a new encapsulation type for libpcap files, do
558 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
559 * add a new encapsulation type by changing an existing entry;
560 * leave the existing entries alone.
562 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
563 * for a new DLT_ value, and specifying the purpose of the new value.
564 * When you get the new DLT_ value, use that numerical value in
565 * the "dlt_value" field of "pcap_to_wtap_map[]".
568 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
571 wtap_pcap_encap_to_wtap_encap(int encap)
575 for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
576 if (pcap_to_wtap_map[i].dlt_value == encap)
577 return pcap_to_wtap_map[i].wtap_encap_value;
579 return WTAP_ENCAP_UNKNOWN;
583 wtap_wtap_encap_to_pcap_encap(int encap)
589 case WTAP_ENCAP_FDDI:
590 case WTAP_ENCAP_FDDI_BITSWAPPED:
591 case WTAP_ENCAP_NETTL_FDDI:
593 * Special-case WTAP_ENCAP_FDDI and
594 * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
595 * to DLT_FDDI (even though that may mean that the bit
596 * order in the FDDI MAC addresses is wrong; so it goes
597 * - libpcap format doesn't record the byte order,
598 * so that's not fixable).
600 return 10; /* that's DLT_FDDI */
602 case WTAP_ENCAP_FRELAY_WITH_PHDR:
604 * Do the same with Frame Relay.
608 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
610 * Map this to DLT_IEEE802_11, for now, even though
611 * that means the radio information will be lost.
612 * Once tcpdump support for the BSD radiotap header
613 * is sufficiently widespread, we should probably
614 * use that, instead - although we should probably
615 * ultimately just have WTAP_ENCAP_IEEE_802_11
616 * as the only Wiretap encapsulation for 802.11,
617 * and have the pseudo-header include a radiotap-style
618 * list of attributes. If we do that, though, we
619 * should probably bypass the regular Wiretap code
620 * when writing out packets during a capture, and just
621 * do the equivalent of a libpcap write (unfortunately,
622 * libpcap doesn't have an "open dump by file descriptor"
623 * function, so we can't just use "pcap_dump()"), so
624 * that we don't spend cycles mapping from libpcap to
625 * Wiretap and then back to libpcap. (There are other
626 * reasons to do that, e.g. to handle AIX libpcap better.)
631 for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
632 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
633 return pcap_to_wtap_map[i].dlt_value;
639 * Various pseudo-headers that appear at the beginning of packet data.
641 * We represent them as sets of offsets, as they might not be aligned on
642 * an appropriate structure boundary in the buffer, and as that makes them
643 * independent of the way the compiler might align fields.
647 * The link-layer header on SunATM packets.
649 #define SUNATM_FLAGS 0 /* destination and traffic type - 1 byte */
650 #define SUNATM_VPI 1 /* VPI - 1 byte */
651 #define SUNATM_VCI 2 /* VCI - 2 bytes */
652 #define SUNATM_LEN 4 /* length of the header */
655 * The link-layer header on Nokia IPSO ATM packets.
657 #define NOKIAATM_FLAGS 0 /* destination - 1 byte */
658 #define NOKIAATM_VPI 1 /* VPI - 1 byte */
659 #define NOKIAATM_VCI 2 /* VCI - 2 bytes */
660 #define NOKIAATM_LEN 4 /* length of the header */
663 * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
666 #define IRDA_SLL_PKTTYPE_OFFSET 0 /* packet type - 2 bytes */
667 /* 12 unused bytes */
668 #define IRDA_SLL_PROTOCOL_OFFSET 14 /* protocol, should be ETH_P_LAPD - 2 bytes */
669 #define IRDA_SLL_LEN 16 /* length of the header */
672 * A header containing additional MTP information.
674 #define MTP2_SENT_OFFSET 0 /* 1 byte */
675 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
676 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
677 #define MTP2_HDR_LEN 4 /* length of the header */
680 * A header containing additional SITA WAN information.
682 #define SITA_FLAGS_OFFSET 0 /* 1 byte */
683 #define SITA_SIGNALS_OFFSET 1 /* 1 byte */
684 #define SITA_ERRORS1_OFFSET 2 /* 1 byte */
685 #define SITA_ERRORS2_OFFSET 3 /* 1 byte */
686 #define SITA_PROTO_OFFSET 4 /* 1 byte */
687 #define SITA_HDR_LEN 5 /* length of the header */
690 * The fake link-layer header of LAPD packets.
693 #define ETH_P_LAPD 0x0030
696 #define LAPD_SLL_PKTTYPE_OFFSET 0 /* packet type - 2 bytes */
697 #define LAPD_SLL_HATYPE_OFFSET 2 /* hardware address type - 2 bytes */
698 #define LAPD_SLL_HALEN_OFFSET 4 /* hardware address length - 2 bytes */
699 #define LAPD_SLL_ADDR_OFFSET 6 /* address - 8 bytes */
700 #define LAPD_SLL_PROTOCOL_OFFSET 14 /* protocol, should be ETH_P_LAPD - 2 bytes */
701 #define LAPD_SLL_LEN 16 /* length of the header */
704 * I2C link-layer on-disk format
706 struct i2c_file_hdr {
712 pcap_read_sunatm_pseudoheader(FILE_T fh,
713 union wtap_pseudo_header *pseudo_header, int *err)
715 guint8 atm_phdr[SUNATM_LEN];
720 errno = WTAP_ERR_CANT_READ;
721 bytes_read = file_read(atm_phdr, 1, SUNATM_LEN, fh);
722 if (bytes_read != SUNATM_LEN) {
723 *err = file_error(fh);
725 *err = WTAP_ERR_SHORT_READ;
729 vpi = atm_phdr[SUNATM_VPI];
730 vci = pntohs(&atm_phdr[SUNATM_VCI]);
732 switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
734 case 0x01: /* LANE */
735 pseudo_header->atm.aal = AAL_5;
736 pseudo_header->atm.type = TRAF_LANE;
739 case 0x02: /* RFC 1483 LLC multiplexed traffic */
740 pseudo_header->atm.aal = AAL_5;
741 pseudo_header->atm.type = TRAF_LLCMX;
744 case 0x05: /* ILMI */
745 pseudo_header->atm.aal = AAL_5;
746 pseudo_header->atm.type = TRAF_ILMI;
749 case 0x06: /* Q.2931 */
750 pseudo_header->atm.aal = AAL_SIGNALLING;
751 pseudo_header->atm.type = TRAF_UNKNOWN;
754 case 0x03: /* MARS (RFC 2022) */
755 pseudo_header->atm.aal = AAL_5;
756 pseudo_header->atm.type = TRAF_UNKNOWN;
759 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
760 pseudo_header->atm.aal = AAL_5;
761 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
766 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
767 * case assume it's AAL_SIGNALLING; we know nothing more
770 * XXX - is this necessary? Or are we guaranteed that
771 * all signalling traffic has a type of 0x06?
773 * XXX - is this guaranteed to be AAL5? Or, if the type is
774 * 0x00 ("raw"), might it be non-AAL5 traffic?
776 if (vpi == 0 && vci == 5)
777 pseudo_header->atm.aal = AAL_SIGNALLING;
779 pseudo_header->atm.aal = AAL_5;
780 pseudo_header->atm.type = TRAF_UNKNOWN;
783 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
785 pseudo_header->atm.vpi = vpi;
786 pseudo_header->atm.vci = vci;
787 pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
789 /* We don't have this information */
790 pseudo_header->atm.flags = 0;
791 pseudo_header->atm.cells = 0;
792 pseudo_header->atm.aal5t_u2u = 0;
793 pseudo_header->atm.aal5t_len = 0;
794 pseudo_header->atm.aal5t_chksum = 0;
800 pcap_read_nokiaatm_pseudoheader(FILE_T fh,
801 union wtap_pseudo_header *pseudo_header, int *err)
803 guint8 atm_phdr[NOKIAATM_LEN];
808 errno = WTAP_ERR_CANT_READ;
809 bytes_read = file_read(atm_phdr, 1, NOKIAATM_LEN, fh);
810 if (bytes_read != NOKIAATM_LEN) {
811 *err = file_error(fh);
813 *err = WTAP_ERR_SHORT_READ;
817 vpi = atm_phdr[NOKIAATM_VPI];
818 vci = pntohs(&atm_phdr[NOKIAATM_VCI]);
820 pseudo_header->atm.vpi = vpi;
821 pseudo_header->atm.vci = vci;
822 pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
824 /* We don't have this information */
825 pseudo_header->atm.flags = 0;
826 pseudo_header->atm.cells = 0;
827 pseudo_header->atm.aal5t_u2u = 0;
828 pseudo_header->atm.aal5t_len = 0;
829 pseudo_header->atm.aal5t_chksum = 0;
835 pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
836 int *err, gchar **err_info)
838 guint8 irda_phdr[IRDA_SLL_LEN];
841 errno = WTAP_ERR_CANT_READ;
842 bytes_read = file_read(irda_phdr, 1, IRDA_SLL_LEN, fh);
843 if (bytes_read != IRDA_SLL_LEN) {
844 *err = file_error(fh);
846 *err = WTAP_ERR_SHORT_READ;
850 if (pntohs(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
851 *err = WTAP_ERR_BAD_RECORD;
852 if (err_info != NULL)
853 *err_info = g_strdup("libpcap: IrDA capture has a packet with an invalid sll_protocol field");
857 pseudo_header->irda.pkttype = pntohs(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
863 pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
865 guint8 mtp2_hdr[MTP2_HDR_LEN];
868 errno = WTAP_ERR_CANT_READ;
869 bytes_read = file_read(mtp2_hdr, 1, MTP2_HDR_LEN, fh);
870 if (bytes_read != MTP2_HDR_LEN) {
871 *err = file_error(fh);
873 *err = WTAP_ERR_SHORT_READ;
877 pseudo_header->mtp2.sent = mtp2_hdr[MTP2_SENT_OFFSET];
878 pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
879 pseudo_header->mtp2.link_number = pntohs(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
885 pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
886 int *err, gchar **err_info)
888 guint8 lapd_phdr[LAPD_SLL_LEN];
891 errno = WTAP_ERR_CANT_READ;
892 bytes_read = file_read(lapd_phdr, 1, LAPD_SLL_LEN, fh);
893 if (bytes_read != LAPD_SLL_LEN) {
894 *err = file_error(fh);
896 *err = WTAP_ERR_SHORT_READ;
900 if (pntohs(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
901 *err = WTAP_ERR_BAD_RECORD;
902 if (err_info != NULL)
903 *err_info = g_strdup("libpcap: LAPD capture has a packet with an invalid sll_protocol field");
907 pseudo_header->lapd.pkttype = pntohs(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
908 pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
914 pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
916 guint8 sita_phdr[SITA_HDR_LEN];
919 errno = WTAP_ERR_CANT_READ;
920 bytes_read = file_read(sita_phdr, 1, SITA_HDR_LEN, fh);
921 if (bytes_read != SITA_HDR_LEN) {
922 *err = file_error(fh);
924 *err = WTAP_ERR_SHORT_READ;
928 pseudo_header->sita.flags = sita_phdr[SITA_FLAGS_OFFSET];
929 pseudo_header->sita.signals = sita_phdr[SITA_SIGNALS_OFFSET];
930 pseudo_header->sita.errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
931 pseudo_header->sita.errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
932 pseudo_header->sita.proto = sita_phdr[SITA_PROTO_OFFSET];
938 * When not using the memory-mapped interface to capture USB events,
939 * code that reads those events can use the MON_IOCX_GET ioctl to
940 * read a 48-byte header consisting of a "struct linux_usb_phdr", as
941 * defined below, followed immediately by one of:
943 * 8 bytes of a "struct usb_device_setup_hdr", if "setup_flag"
944 * in the preceding "struct linux_usb_phdr" is 0;
946 * in Linux 2.6.30 or later, 8 bytes of a "struct iso_rec", if
947 * this is an isochronous transfer;
949 * 8 bytes of junk, otherwise.
951 * In Linux 2.6.31 and later, it can also use the MON_IOCX_GETX ioctl
952 * to read a 64-byte header; that header consists of the 48 bytes
953 * above, followed immediately by 16 bytes of a "struct linux_usb_phdr_ext",
956 * In Linux 2.6.21 and later, there's a memory-mapped interface to
957 * capture USB events. In that interface, the events in the memory-mapped
958 * buffer have a 64-byte header, followed immediately by the data.
959 * In Linux 2.6.21 through 2.6.30.x, the 64-byte header is the 48-byte
960 * header described above, followed by 16 bytes of zeroes; in Linux
961 * 2.6.31 and later, the 64-byte header is the 64-byte header described
964 * See linux/Documentation/usb/usbmon.txt and libpcap/pcap/usb.h for details.
966 * With WTAP_ENCAP_USB_LINUX, packets have the 48-byte header; with
967 * WTAP_ENCAP_USB_LINUX_MMAPPED, they have the 64-byte header. There
968 * is no indication of whether the header has the "struct iso_rec", or
969 * whether the last 16 bytes of a 64-byte header are all zeros or are
970 * a "struct linux_usb_phdr_ext".
974 * URB transfer_type values
976 #define URB_ISOCHRONOUS 0x0
977 #define URB_INTERRUPT 0x1
978 #define URB_CONTROL 0x2
982 * Information from the URB for Isochronous transfers.
984 * This structure is 8 bytes long.
992 * Header prepended by Linux kernel to each USB event.
994 * (Setup flag is '-', 'D', 'Z', or 0. Data flag is '<', '>', 'Z', or 0.)
996 * The values are in *host* byte order.
998 struct linux_usb_phdr {
999 guint64 id; /* urb id, to link submission and completion events */
1000 guint8 event_type; /* Submit ('S'), Completed ('C'), Error ('E') */
1001 guint8 transfer_type; /* ISO (0), Intr, Control, Bulk (3) */
1002 guint8 endpoint_number; /* Endpoint number (0-15) and transfer direction */
1003 guint8 device_address; /* 0-127 */
1005 gint8 setup_flag; /* 0, if the urb setup header is meaningful */
1006 gint8 data_flag; /* 0, if urb data is present */
1010 guint32 urb_len; /* whole len of urb this event refers to */
1011 guint32 data_len; /* amount of urb data really present in this event */
1014 * Packet-type-dependent data.
1015 * USB setup information of setup_flag is true.
1016 * Otherwise, some isochronous transfer information.
1024 * This data is provided by Linux 2.6.31 and later kernels.
1026 * For WTAP_ENCAP_USB_LINUX, it's not in the pseudo-header, so
1027 * the pseudo-header is always 48 bytes long, including the
1028 * packet-type-dependent data.
1030 * For WTAP_ENCAP_USB_LINUX_MMAPPED, the pseudo-header is always
1031 * 64 bytes long, with the packet-type-dependent data preceding
1032 * these last 16 bytes. In pre-2.6.31 kernels, it's zero padding;
1033 * in 2.6.31 and later, it's the following data.
1035 gint32 interval; /* only for Interrupt and Isochronous events */
1036 gint32 start_frame; /* for Isochronous */
1037 guint32 xfer_flags; /* copy of URB's transfer_flags */
1038 guint32 ndesc; /* actual number of isochronous descriptors */
1041 struct linux_usb_isodesc {
1049 * USB setup header as defined in USB specification
1050 * See usb_20.pdf, Chapter 9.3 'USB Device Requests' for details.
1051 * http://www.usb.org/developers/docs/usb_20_122909-2.zip
1053 * This structure is 8 bytes long.
1055 struct usb_device_setup_hdr {
1056 gint8 bmRequestType;
1065 * Offset of the *end* of a field within a particular structure.
1067 #define END_OFFSETOF(basep, fieldp) \
1068 (((char *)(void *)(fieldp)) - ((char *)(void *)(basep)) + \
1072 pcap_process_linux_usb_pseudoheader(guint packet_size, gboolean byte_swapped,
1073 gboolean header_len_64_bytes, guint8 *pd)
1075 struct linux_usb_phdr *phdr;
1076 struct linux_usb_isodesc *pisodesc;
1077 gint32 iso_numdesc, i;
1080 phdr = (struct linux_usb_phdr *)pd;
1082 if (packet_size < END_OFFSETOF(phdr, &phdr->id))
1084 PBSWAP64((guint8 *)&phdr->id);
1085 if (packet_size < END_OFFSETOF(phdr, &phdr->bus_id))
1087 PBSWAP16((guint8 *)&phdr->bus_id);
1088 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_sec))
1090 PBSWAP64((guint8 *)&phdr->ts_sec);
1091 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_usec))
1093 PBSWAP32((guint8 *)&phdr->ts_usec);
1094 if (packet_size < END_OFFSETOF(phdr, &phdr->status))
1096 PBSWAP32((guint8 *)&phdr->status);
1097 if (packet_size < END_OFFSETOF(phdr, &phdr->urb_len))
1099 PBSWAP32((guint8 *)&phdr->urb_len);
1100 if (packet_size < END_OFFSETOF(phdr, &phdr->data_len))
1102 PBSWAP32((guint8 *)&phdr->data_len);
1104 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1105 if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.error_count))
1107 PBSWAP32((guint8 *)&phdr->s.iso.error_count);
1109 if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.numdesc))
1111 PBSWAP32((guint8 *)&phdr->s.iso.numdesc);
1115 if (header_len_64_bytes) {
1117 * This is either the "version 1" header, with
1118 * 16 bytes of additional fields at the end, or
1119 * a "version 0" header from a memory-mapped
1120 * capture, with 16 bytes of zeroed-out padding
1121 * at the end. Byte swap them as if this were
1122 * a "version 1" header.
1124 * Yes, the first argument to END_OFFSETOF() should
1125 * be phdr, not phdr_ext; we want the offset of
1126 * the additional fields from the beginning of
1129 if (packet_size < END_OFFSETOF(phdr, &phdr->interval))
1131 PBSWAP32((guint8 *)&phdr->interval);
1132 if (packet_size < END_OFFSETOF(phdr, &phdr->start_frame))
1134 PBSWAP32((guint8 *)&phdr->start_frame);
1135 if (packet_size < END_OFFSETOF(phdr, &phdr->xfer_flags))
1137 PBSWAP32((guint8 *)&phdr->xfer_flags);
1138 if (packet_size < END_OFFSETOF(phdr, &phdr->ndesc))
1140 PBSWAP32((guint8 *)&phdr->ndesc);
1143 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1144 /* swap the values in struct linux_usb_isodesc */
1146 if (header_len_64_bytes) {
1147 pisodesc = (struct linux_usb_isodesc*)(pd + 64);
1149 pisodesc = (struct linux_usb_isodesc*)(pd + 48);
1151 iso_numdesc = phdr->s.iso.numdesc;
1152 for (i = 0; i < iso_numdesc; i++) {
1153 /* always check if we have enough data from the
1154 * beginnig of the packet (phdr)
1156 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_status))
1158 PBSWAP32((guint8 *)&pisodesc->iso_status);
1159 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_off))
1161 PBSWAP32((guint8 *)&pisodesc->iso_off);
1162 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_len))
1164 PBSWAP32((guint8 *)&pisodesc->iso_len);
1165 if (packet_size < END_OFFSETOF(phdr, &pisodesc->_pad))
1167 PBSWAP32((guint8 *)&pisodesc->_pad);
1176 pcap_read_bt_pseudoheader(FILE_T fh,
1177 union wtap_pseudo_header *pseudo_header, int *err)
1180 struct libpcap_bt_phdr phdr;
1182 errno = WTAP_ERR_CANT_READ;
1183 bytes_read = file_read(&phdr, 1,
1184 sizeof (struct libpcap_bt_phdr), fh);
1185 if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
1186 *err = file_error(fh);
1188 *err = WTAP_ERR_SHORT_READ;
1191 pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
1196 pcap_read_ppp_pseudoheader(FILE_T fh,
1197 union wtap_pseudo_header *pseudo_header, int *err)
1200 struct libpcap_ppp_phdr phdr;
1202 errno = WTAP_ERR_CANT_READ;
1203 bytes_read = file_read(&phdr, 1,
1204 sizeof (struct libpcap_ppp_phdr), fh);
1205 if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
1206 *err = file_error(fh);
1208 *err = WTAP_ERR_SHORT_READ;
1211 pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
1216 pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
1217 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
1219 guint8 erf_hdr[sizeof(struct erf_phdr)];
1222 errno = WTAP_ERR_CANT_READ;
1223 bytes_read = file_read(erf_hdr, 1, sizeof(struct erf_phdr), fh);
1224 if (bytes_read != sizeof(struct erf_phdr)) {
1225 *err = file_error(fh);
1227 *err = WTAP_ERR_SHORT_READ;
1230 pseudo_header->erf.phdr.ts = pletohll(&erf_hdr[0]); /* timestamp */
1231 pseudo_header->erf.phdr.type = erf_hdr[8];
1232 pseudo_header->erf.phdr.flags = erf_hdr[9];
1233 pseudo_header->erf.phdr.rlen = pntohs(&erf_hdr[10]);
1234 pseudo_header->erf.phdr.lctr = pntohs(&erf_hdr[12]);
1235 pseudo_header->erf.phdr.wlen = pntohs(&erf_hdr[14]);
1237 /* The high 32 bits of the timestamp contain the integer number of seconds
1238 * while the lower 32 bits contain the binary fraction of the second.
1239 * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1241 guint64 ts = pseudo_header->erf.phdr.ts;
1242 whdr->ts.secs = (guint32) (ts >> 32);
1243 ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1244 ts += (ts & 0x80000000) << 1; /* rounding */
1245 whdr->ts.nsecs = ((guint32) (ts >> 32));
1246 if ( whdr->ts.nsecs >= 1000000000) {
1247 whdr->ts.nsecs -= 1000000000;
1255 * If the type of record given in the pseudo header indicate the presence of an extension
1256 * header then, read all the extension headers
1259 pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1260 int *err, gchar **err_info _U_, guint * psize)
1263 guint8 erf_exhdr[8];
1264 guint64 erf_exhdr_sw;
1265 int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1268 if (pseudo_header->erf.phdr.type & 0x80){
1270 errno = WTAP_ERR_CANT_READ;
1271 bytes_read = file_read(erf_exhdr, 1, 8, fh);
1272 if (bytes_read != 8 ) {
1273 *err = file_error(fh);
1275 *err = WTAP_ERR_SHORT_READ;
1278 type = erf_exhdr[0];
1279 erf_exhdr_sw = pntohll((guint64*) &(erf_exhdr[0]));
1281 memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1284 } while (type & 0x80);
1290 * If the type of record given in the pseudo header indicate the precense of a subheader
1291 * then, read this optional subheader
1294 pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1295 int *err, gchar **err_info _U_, guint * psize)
1297 guint8 erf_subhdr[sizeof(union erf_subhdr)];
1301 switch(pseudo_header->erf.phdr.type & 0x7F) {
1302 case ERF_TYPE_MC_HDLC:
1303 case ERF_TYPE_MC_RAW:
1304 case ERF_TYPE_MC_ATM:
1305 case ERF_TYPE_MC_RAW_CHANNEL:
1306 case ERF_TYPE_MC_AAL5:
1307 case ERF_TYPE_MC_AAL2:
1308 case ERF_TYPE_COLOR_MC_HDLC_POS:
1309 /* Extract the Multi Channel header to include it in the pseudo header part */
1310 errno = WTAP_ERR_CANT_READ;
1311 bytes_read = file_read(erf_subhdr, 1, sizeof(erf_mc_header_t), fh);
1312 if (bytes_read != sizeof(erf_mc_header_t) ) {
1313 *err = file_error(fh);
1315 *err = WTAP_ERR_SHORT_READ;
1318 pseudo_header->erf.subhdr.mc_hdr = pntohl(&erf_subhdr[0]);
1319 *psize = sizeof(erf_mc_header_t);
1322 case ERF_TYPE_COLOR_ETH:
1323 case ERF_TYPE_DSM_COLOR_ETH:
1324 /* Extract the Ethernet additional header to include it in the pseudo header part */
1325 errno = WTAP_ERR_CANT_READ;
1326 bytes_read = file_read(erf_subhdr, 1, sizeof(erf_eth_header_t), fh);
1327 if (bytes_read != sizeof(erf_eth_header_t) ) {
1328 *err = file_error(fh);
1330 *err = WTAP_ERR_SHORT_READ;
1333 pseudo_header->erf.subhdr.eth_hdr = pntohs(&erf_subhdr[0]);
1334 *psize = sizeof(erf_eth_header_t);
1337 /* No optional pseudo header for this ERF type */
1344 pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
1346 struct i2c_file_hdr i2c_hdr;
1349 errno = WTAP_ERR_CANT_READ;
1350 bytes_read = file_read(&i2c_hdr, 1, sizeof (i2c_hdr), fh);
1351 if (bytes_read != sizeof (i2c_hdr)) {
1352 *err = file_error(fh);
1354 *err = WTAP_ERR_SHORT_READ;
1358 pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
1359 pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
1360 pseudo_header->i2c.flags = pntohl(&i2c_hdr.flags);
1366 pcap_process_pseudo_header(FILE_T fh, int file_type, int wtap_encap,
1367 guint packet_size, gboolean check_packet_size, struct wtap_pkthdr *phdr,
1368 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1373 switch (wtap_encap) {
1375 case WTAP_ENCAP_ATM_PDUS:
1376 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1380 if (check_packet_size && packet_size < NOKIAATM_LEN) {
1382 * Uh-oh, the packet isn't big enough to even
1383 * have a pseudo-header.
1385 *err = WTAP_ERR_BAD_RECORD;
1386 *err_info = g_strdup_printf("pcap: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1390 if (!pcap_read_nokiaatm_pseudoheader(fh,
1391 pseudo_header, err))
1392 return -1; /* Read error */
1394 phdr_len = NOKIAATM_LEN;
1399 if (check_packet_size && packet_size < SUNATM_LEN) {
1401 * Uh-oh, the packet isn't big enough to even
1402 * have a pseudo-header.
1404 *err = WTAP_ERR_BAD_RECORD;
1405 *err_info = g_strdup_printf("pcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1409 if (!pcap_read_sunatm_pseudoheader(fh,
1410 pseudo_header, err))
1411 return -1; /* Read error */
1413 phdr_len = SUNATM_LEN;
1417 case WTAP_ENCAP_ETHERNET:
1419 * We don't know whether there's an FCS in this frame or not.
1421 pseudo_header->eth.fcs_len = -1;
1424 case WTAP_ENCAP_IEEE_802_11:
1425 case WTAP_ENCAP_PRISM_HEADER:
1426 case WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP:
1427 case WTAP_ENCAP_IEEE_802_11_WLAN_AVS:
1429 * We don't know whether there's an FCS in this frame or not.
1430 * XXX - are there any OSes where the capture mechanism
1433 pseudo_header->ieee_802_11.fcs_len = -1;
1434 pseudo_header->ieee_802_11.channel = 0;
1435 pseudo_header->ieee_802_11.data_rate = 0;
1436 pseudo_header->ieee_802_11.signal_level = 0;
1439 case WTAP_ENCAP_IRDA:
1440 if (check_packet_size && packet_size < IRDA_SLL_LEN) {
1442 * Uh-oh, the packet isn't big enough to even
1443 * have a pseudo-header.
1445 *err = WTAP_ERR_BAD_RECORD;
1446 *err_info = g_strdup_printf("pcap: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1450 if (!pcap_read_irda_pseudoheader(fh, pseudo_header,
1452 return -1; /* Read error */
1454 phdr_len = IRDA_SLL_LEN;
1457 case WTAP_ENCAP_MTP2_WITH_PHDR:
1458 if (check_packet_size && packet_size < MTP2_HDR_LEN) {
1460 * Uh-oh, the packet isn't big enough to even
1461 * have a pseudo-header.
1463 *err = WTAP_ERR_BAD_RECORD;
1464 *err_info = g_strdup_printf("pcap: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1468 if (!pcap_read_mtp2_pseudoheader(fh, pseudo_header,
1470 return -1; /* Read error */
1472 phdr_len = MTP2_HDR_LEN;
1475 case WTAP_ENCAP_LINUX_LAPD:
1476 if (check_packet_size && packet_size < LAPD_SLL_LEN) {
1478 * Uh-oh, the packet isn't big enough to even
1479 * have a pseudo-header.
1481 *err = WTAP_ERR_BAD_RECORD;
1482 *err_info = g_strdup_printf("pcap: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1486 if (!pcap_read_lapd_pseudoheader(fh, pseudo_header,
1488 return -1; /* Read error */
1490 phdr_len = LAPD_SLL_LEN;
1493 case WTAP_ENCAP_SITA:
1494 if (check_packet_size && packet_size < SITA_HDR_LEN) {
1496 * Uh-oh, the packet isn't big enough to even
1497 * have a pseudo-header.
1499 *err = WTAP_ERR_BAD_RECORD;
1500 *err_info = g_strdup_printf("pcap: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1504 if (!pcap_read_sita_pseudoheader(fh, pseudo_header,
1506 return -1; /* Read error */
1508 phdr_len = SITA_HDR_LEN;
1511 case WTAP_ENCAP_BLUETOOTH_H4:
1512 /* We don't have pseudoheader, so just pretend we received everything. */
1513 pseudo_header->p2p.sent = FALSE;
1516 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1517 if (check_packet_size &&
1518 packet_size < sizeof (struct libpcap_bt_phdr)) {
1520 * Uh-oh, the packet isn't big enough to even
1521 * have a pseudo-header.
1523 *err = WTAP_ERR_BAD_RECORD;
1524 *err_info = g_strdup_printf("pcap: libpcap bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1528 if (!pcap_read_bt_pseudoheader(fh,
1529 pseudo_header, err))
1530 return -1; /* Read error */
1532 phdr_len = (int)sizeof (struct libpcap_bt_phdr);
1535 case WTAP_ENCAP_PPP_WITH_PHDR:
1536 if (check_packet_size &&
1537 packet_size < sizeof (struct libpcap_ppp_phdr)) {
1539 * Uh-oh, the packet isn't big enough to even
1540 * have a pseudo-header.
1542 *err = WTAP_ERR_BAD_RECORD;
1543 *err_info = g_strdup_printf("pcap: libpcap ppp file has a %u-byte packet, too small to have even a pseudo-header",
1547 if (!pcap_read_ppp_pseudoheader(fh,
1548 pseudo_header, err))
1549 return -1; /* Read error */
1551 phdr_len = (int)sizeof (struct libpcap_ppp_phdr);
1554 case WTAP_ENCAP_ERF:
1555 if (check_packet_size &&
1556 packet_size < sizeof(struct erf_phdr) ) {
1558 * Uh-oh, the packet isn't big enough to even
1559 * have a pseudo-header.
1561 *err = WTAP_ERR_BAD_RECORD;
1562 *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1567 if (!pcap_read_erf_pseudoheader(fh, phdr, pseudo_header,
1569 return -1; /* Read error */
1571 phdr_len = (int)sizeof(struct erf_phdr);
1573 /* check the optional Extension header */
1574 if (!pcap_read_erf_exheader(fh, pseudo_header, err, err_info,
1576 return -1; /* Read error */
1580 /* check the optional Multi Channel header */
1581 if (!pcap_read_erf_subheader(fh, pseudo_header, err, err_info,
1583 return -1; /* Read error */
1588 case WTAP_ENCAP_I2C:
1589 if (check_packet_size &&
1590 packet_size < sizeof (struct i2c_file_hdr)) {
1592 * Uh-oh, the packet isn't big enough to even
1593 * have a pseudo-header.
1595 *err = WTAP_ERR_BAD_RECORD;
1596 *err_info = g_strdup_printf("pcap: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1600 if (!pcap_read_i2c_pseudoheader(fh, pseudo_header,
1602 return -1; /* Read error */
1605 * Don't count the pseudo-header as part of the packet.
1607 phdr_len = (int)sizeof (struct i2c_file_hdr);
1615 pcap_read_post_process(int wtap_encap, guint packet_size,
1616 gboolean bytes_swapped, guchar *pd)
1618 switch (wtap_encap) {
1620 case WTAP_ENCAP_USB_LINUX:
1621 pcap_process_linux_usb_pseudoheader(packet_size,
1622 bytes_swapped, FALSE, pd);
1625 case WTAP_ENCAP_USB_LINUX_MMAPPED:
1626 pcap_process_linux_usb_pseudoheader(packet_size,
1627 bytes_swapped, TRUE, pd);
1636 pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
1642 case WTAP_ENCAP_ATM_PDUS:
1643 hdrsize = SUNATM_LEN;
1646 case WTAP_ENCAP_IRDA:
1647 hdrsize = IRDA_SLL_LEN;
1650 case WTAP_ENCAP_MTP2_WITH_PHDR:
1651 hdrsize = MTP2_HDR_LEN;
1654 case WTAP_ENCAP_LINUX_LAPD:
1655 hdrsize = LAPD_SLL_LEN;
1658 case WTAP_ENCAP_SITA:
1659 hdrsize = SITA_HDR_LEN;
1662 case WTAP_ENCAP_ERF:
1663 hdrsize = (int)sizeof (struct erf_phdr);
1664 if (pseudo_header->erf.phdr.type & 0x80)
1666 switch (pseudo_header->erf.phdr.type & 0x7F) {
1668 case ERF_TYPE_MC_HDLC:
1669 case ERF_TYPE_MC_RAW:
1670 case ERF_TYPE_MC_ATM:
1671 case ERF_TYPE_MC_RAW_CHANNEL:
1672 case ERF_TYPE_MC_AAL5:
1673 case ERF_TYPE_MC_AAL2:
1674 case ERF_TYPE_COLOR_MC_HDLC_POS:
1675 hdrsize += (int)sizeof(struct erf_mc_hdr);
1679 case ERF_TYPE_COLOR_ETH:
1680 case ERF_TYPE_DSM_COLOR_ETH:
1681 hdrsize += (int)sizeof(struct erf_eth_hdr);
1689 case WTAP_ENCAP_I2C:
1690 hdrsize = (int)sizeof (struct i2c_file_hdr);
1693 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1694 hdrsize = (int)sizeof (struct libpcap_bt_phdr);
1697 case WTAP_ENCAP_PPP_WITH_PHDR:
1698 hdrsize = (int)sizeof (struct libpcap_ppp_phdr);
1710 pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
1713 guint8 atm_hdr[SUNATM_LEN];
1714 guint8 irda_hdr[IRDA_SLL_LEN];
1715 guint8 lapd_hdr[LAPD_SLL_LEN];
1716 guint8 mtp2_hdr[MTP2_HDR_LEN];
1717 guint8 sita_hdr[SITA_HDR_LEN];
1718 guint8 erf_hdr[ sizeof(struct erf_mc_phdr)];
1719 struct i2c_file_hdr i2c_hdr;
1720 struct libpcap_bt_phdr bt_hdr;
1721 struct libpcap_ppp_phdr ppp_hdr;
1726 case WTAP_ENCAP_ATM_PDUS:
1728 * Write the ATM header.
1730 atm_hdr[SUNATM_FLAGS] =
1731 (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1732 switch (pseudo_header->atm.aal) {
1734 case AAL_SIGNALLING:
1736 atm_hdr[SUNATM_FLAGS] |= 0x06;
1740 switch (pseudo_header->atm.type) {
1744 atm_hdr[SUNATM_FLAGS] |= 0x01;
1748 /* RFC 1483 LLC multiplexed traffic */
1749 atm_hdr[SUNATM_FLAGS] |= 0x02;
1754 atm_hdr[SUNATM_FLAGS] |= 0x05;
1759 atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
1760 phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1761 if (!wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr), err))
1763 wdh->bytes_dumped += sizeof(atm_hdr);
1766 case WTAP_ENCAP_IRDA:
1768 * Write the IrDA header.
1770 memset(irda_hdr, 0, sizeof(irda_hdr));
1771 phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
1772 pseudo_header->irda.pkttype);
1773 phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
1774 if (!wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr), err))
1776 wdh->bytes_dumped += sizeof(irda_hdr);
1779 case WTAP_ENCAP_MTP2_WITH_PHDR:
1781 * Write the MTP2 header.
1783 memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
1784 mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
1785 mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
1786 phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
1787 pseudo_header->mtp2.link_number);
1788 if (!wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr), err))
1790 wdh->bytes_dumped += sizeof(mtp2_hdr);
1793 case WTAP_ENCAP_LINUX_LAPD:
1795 * Write the LAPD header.
1797 memset(&lapd_hdr, 0, sizeof(lapd_hdr));
1798 phtons(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET],
1799 pseudo_header->lapd.pkttype);
1800 phtons(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
1801 lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
1802 pseudo_header->lapd.we_network?0x01:0x00;
1803 if (!wtap_dump_file_write(wdh, lapd_hdr, sizeof(lapd_hdr), err))
1805 wdh->bytes_dumped += sizeof(lapd_hdr);
1808 case WTAP_ENCAP_SITA:
1810 * Write the SITA header.
1812 memset(&sita_hdr, 0, sizeof(sita_hdr));
1813 sita_hdr[SITA_FLAGS_OFFSET] = pseudo_header->sita.flags;
1814 sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.signals;
1815 sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.errors1;
1816 sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.errors2;
1817 sita_hdr[SITA_PROTO_OFFSET] = pseudo_header->sita.proto;
1818 if (!wtap_dump_file_write(wdh, sita_hdr, sizeof(sita_hdr), err))
1820 wdh->bytes_dumped += sizeof(sita_hdr);
1823 case WTAP_ENCAP_ERF:
1825 * Write the ERF header.
1827 memset(&erf_hdr, 0, sizeof(erf_hdr));
1828 pletonll(&erf_hdr[0], pseudo_header->erf.phdr.ts);
1829 erf_hdr[8] = pseudo_header->erf.phdr.type;
1830 erf_hdr[9] = pseudo_header->erf.phdr.flags;
1831 phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
1832 phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
1833 phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
1834 size = sizeof(struct erf_phdr);
1836 switch(pseudo_header->erf.phdr.type & 0x7F) {
1837 case ERF_TYPE_MC_HDLC:
1838 case ERF_TYPE_MC_RAW:
1839 case ERF_TYPE_MC_ATM:
1840 case ERF_TYPE_MC_RAW_CHANNEL:
1841 case ERF_TYPE_MC_AAL5:
1842 case ERF_TYPE_MC_AAL2:
1843 case ERF_TYPE_COLOR_MC_HDLC_POS:
1844 phtonl(&erf_hdr[16], pseudo_header->erf.subhdr.mc_hdr);
1845 size += (int)sizeof(struct erf_mc_hdr);
1848 case ERF_TYPE_COLOR_ETH:
1849 case ERF_TYPE_DSM_COLOR_ETH:
1850 phtons(&erf_hdr[16], pseudo_header->erf.subhdr.eth_hdr);
1851 size += (int)sizeof(struct erf_eth_hdr);
1856 if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
1858 wdh->bytes_dumped += size;
1861 case WTAP_ENCAP_I2C:
1863 * Write the I2C header.
1865 memset(&i2c_hdr, 0, sizeof(i2c_hdr));
1866 i2c_hdr.bus = pseudo_header->i2c.bus |
1867 (pseudo_header->i2c.is_event ? 0x80 : 0x00);
1868 phtonl((guint8 *)&i2c_hdr.flags, pseudo_header->i2c.flags);
1869 if (!wtap_dump_file_write(wdh, &i2c_hdr, sizeof(i2c_hdr), err))
1871 wdh->bytes_dumped += sizeof(i2c_hdr);
1874 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1875 bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
1876 if (!wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr, err))
1878 wdh->bytes_dumped += sizeof bt_hdr;
1881 case WTAP_ENCAP_PPP_WITH_PHDR:
1882 ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
1883 if (!wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr, err))
1885 wdh->bytes_dumped += sizeof ppp_hdr;