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 },
336 { 204, WTAP_ENCAP_PPP_WITH_PHDR },
338 { 209, WTAP_ENCAP_I2C },
340 { 210, WTAP_ENCAP_FLEXRAY },
342 { 211, WTAP_ENCAP_MOST },
344 { 212, WTAP_ENCAP_LIN },
345 /* X2E Xoraya serial frame */
346 { 213, WTAP_ENCAP_X2E_SERIAL },
347 /* X2E Xoraya frame */
348 { 214, WTAP_ENCAP_X2E_XORAYA },
349 /* IEEE 802.15.4 Wireless PAN non-ASK PHY */
350 { 215, WTAP_ENCAP_IEEE802_15_4_NONASK_PHY },
351 /* USB packets with padded Linux-specified header */
352 { 220, WTAP_ENCAP_USB_LINUX_MMAPPED },
353 /* Fibre Channel FC-2 frame */
354 { 224, WTAP_ENCAP_FIBRE_CHANNEL_FC2 },
355 /* Fibre Channel FC-2 frame with Delimiter */
356 { 225, WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS },
358 { 226, WTAP_ENCAP_IPNET },
359 /* SocketCAN frame */
360 { 227, WTAP_ENCAP_SOCKETCAN },
361 /* IEEE 802.15.4 Wireless PAN no fcs */
362 { 230, WTAP_ENCAP_IEEE802_15_4_NOFCS },
367 * If you need a new encapsulation type for libpcap files, do
368 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
369 * add a new encapsulation type by changing an existing entry;
370 * leave the existing entries alone.
372 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
373 * for a new DLT_ value, and specifying the purpose of the new value.
374 * When you get the new DLT_ value, use that numerical value in
375 * the "dlt_value" field of "pcap_to_wtap_map[]".
379 * The following are entries for libpcap type values that have
380 * different meanings on different OSes.
382 * We put these *after* the entries for the platform-independent
383 * libpcap type values for those Wiretap encapsulation types, so
384 * that Wireshark chooses the platform-independent libpcap type
385 * value for those encapsulatioin types, not the platform-dependent
390 * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
391 * seen that define anything other than DLT_ATM_RFC1483 as 11 are
392 * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
393 * which define it as 100, mapping the kernel's value to 100, in
394 * an attempt to hide the different values used on different
397 * If this is a platform where DLT_FR is defined as 11, we
398 * don't handle 11 at all; otherwise, we handle it as
399 * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
400 * captures from BSD/OS if running on platforms other than BSD/OS,
403 * 1) we don't yet support DLT_FR
407 * 2) nothing short of a heuristic would let us interpret
410 #if defined(DLT_FR) && (DLT_FR == 11)
411 { 11, WTAP_ENCAP_FRELAY },
413 { 11, WTAP_ENCAP_ATM_RFC1483 },
417 * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
418 * BSD/OS, and DLT_LOOP on OpenBSD.
420 * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
421 * (it's just like DLT_NULL, only with the AF_ value in network
422 * rather than host byte order - Wireshark figures out the
423 * byte order from the data, so we don't care what byte order
424 * it's in), so if DLT_LOOP is defined as 12, interpret 12
425 * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
426 * as 12, interpret it as WTAP_ENCAP_RAW_IP.
428 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
429 { 12, WTAP_ENCAP_NULL },
430 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
432 * Put entry for Cisco HDLC here.
433 * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
434 * start with a 4-byte Cisco HDLC header?
437 { 12, WTAP_ENCAP_RAW_IP },
441 * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
442 * don't actually generate it. I infer that BSD/OS translates
443 * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
444 * libpcap, as the BSD/OS link-layer header is different;
445 * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
447 * From this, I infer that there's no point in handling 13
450 * 13 is DLT_ATM_RFC1483 on BSD/OS.
452 * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
453 * of decrypted IPsec traffic.
455 * We treat 13 as WTAP_ENCAP_ENC on all systems except those
456 * that define DLT_ATM_RFC1483 as 13 - presumably only
457 * BSD/OS does so - so that, on BSD/OS systems, we still
458 * treate 13 as WTAP_ENCAP_ATM_RFC1483, but, on all other
459 * systems, we can read OpenBSD DLT_ENC captures.
461 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
462 { 13, WTAP_ENCAP_ATM_RFC1483 },
464 { 13, WTAP_ENCAP_ENC },
468 * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
469 * don't actually generate it. I infer that BSD/OS translates
470 * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
471 * libpcap, as the BSD/OS link-layer header is different;
472 * however, in BSD/OS, DLT_PPP_BSDOS is 16.
474 * From this, I infer that there's no point in handling 14
477 * 14 is DLT_RAW on BSD/OS and OpenBSD.
479 { 14, WTAP_ENCAP_RAW_IP },
484 * DLT_SLIP_BSDOS on BSD/OS;
486 * DLT_HIPPI on NetBSD;
488 * DLT_LANE8023 with Alexey Kuznetzov's patches for
491 * DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
494 * but we don't currently handle any of those.
500 * DLT_PPP_BSDOS on BSD/OS;
502 * DLT_HDLC on NetBSD (Cisco HDLC);
504 * DLT_CIP with Alexey Kuznetzov's patches for
505 * Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
507 * DLT_I4L_IP with the ISDN4Linux patches for libpcap
510 #if defined(DLT_CIP) && (DLT_CIP == 16)
511 { 16, WTAP_ENCAP_LINUX_ATM_CLIP },
513 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
514 { 16, WTAP_ENCAP_CHDLC },
518 * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
520 * It is also used as the PF (Packet Filter) logging format beginning
521 * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
522 * defined with the value 17.
524 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
525 { 17, WTAP_ENCAP_OLD_PFLOG },
529 * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
530 * DLT_CIP of 16 that the Alexey Kuznetzov patches for
531 * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
532 * I've not found any libpcap that uses it for any other purpose -
533 * hopefully nobody will do so in the future.
535 { 18, WTAP_ENCAP_LINUX_ATM_CLIP },
538 * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
539 * recent versions I've seen of the Linux ATM distribution;
540 * I've not yet found any libpcap that uses it for any other
541 * purpose - hopefully nobody will do so in the future.
543 { 19, WTAP_ENCAP_LINUX_ATM_CLIP },
548 * If you need a new encapsulation type for libpcap files, do
549 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
550 * add a new encapsulation type by changing an existing entry;
551 * leave the existing entries alone.
553 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
554 * for a new DLT_ value, and specifying the purpose of the new value.
555 * When you get the new DLT_ value, use that numerical value in
556 * the "dlt_value" field of "pcap_to_wtap_map[]".
559 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
562 wtap_pcap_encap_to_wtap_encap(int encap)
566 for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
567 if (pcap_to_wtap_map[i].dlt_value == encap)
568 return pcap_to_wtap_map[i].wtap_encap_value;
570 return WTAP_ENCAP_UNKNOWN;
574 wtap_wtap_encap_to_pcap_encap(int encap)
580 case WTAP_ENCAP_FDDI:
581 case WTAP_ENCAP_FDDI_BITSWAPPED:
582 case WTAP_ENCAP_NETTL_FDDI:
584 * Special-case WTAP_ENCAP_FDDI and
585 * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
586 * to DLT_FDDI (even though that may mean that the bit
587 * order in the FDDI MAC addresses is wrong; so it goes
588 * - libpcap format doesn't record the byte order,
589 * so that's not fixable).
591 return 10; /* that's DLT_FDDI */
593 case WTAP_ENCAP_FRELAY_WITH_PHDR:
595 * Do the same with Frame Relay.
599 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
601 * Map this to DLT_IEEE802_11, for now, even though
602 * that means the radio information will be lost.
603 * Once tcpdump support for the BSD radiotap header
604 * is sufficiently widespread, we should probably
605 * use that, instead - although we should probably
606 * ultimately just have WTAP_ENCAP_IEEE_802_11
607 * as the only Wiretap encapsulation for 802.11,
608 * and have the pseudo-header include a radiotap-style
609 * list of attributes. If we do that, though, we
610 * should probably bypass the regular Wiretap code
611 * when writing out packets during a capture, and just
612 * do the equivalent of a libpcap write (unfortunately,
613 * libpcap doesn't have an "open dump by file descriptor"
614 * function, so we can't just use "pcap_dump()"), so
615 * that we don't spend cycles mapping from libpcap to
616 * Wiretap and then back to libpcap. (There are other
617 * reasons to do that, e.g. to handle AIX libpcap better.)
622 for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
623 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
624 return pcap_to_wtap_map[i].dlt_value;
630 * Various pseudo-headers that appear at the beginning of packet data.
632 * We represent them as sets of offsets, as they might not be aligned on
633 * an appropriate structure boundary in the buffer, and as that makes them
634 * independent of the way the compiler might align fields.
638 * The link-layer header on SunATM packets.
640 #define SUNATM_FLAGS 0 /* destination and traffic type - 1 byte */
641 #define SUNATM_VPI 1 /* VPI - 1 byte */
642 #define SUNATM_VCI 2 /* VCI - 2 bytes */
643 #define SUNATM_LEN 4 /* length of the header */
646 * The link-layer header on Nokia IPSO ATM packets.
648 #define NOKIAATM_FLAGS 0 /* destination - 1 byte */
649 #define NOKIAATM_VPI 1 /* VPI - 1 byte */
650 #define NOKIAATM_VCI 2 /* VCI - 2 bytes */
651 #define NOKIAATM_LEN 4 /* length of the header */
654 * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
657 #define IRDA_SLL_PKTTYPE_OFFSET 0 /* packet type - 2 bytes */
658 /* 12 unused bytes */
659 #define IRDA_SLL_PROTOCOL_OFFSET 14 /* protocol, should be ETH_P_LAPD - 2 bytes */
660 #define IRDA_SLL_LEN 16 /* length of the header */
663 * A header containing additional MTP information.
665 #define MTP2_SENT_OFFSET 0 /* 1 byte */
666 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
667 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
668 #define MTP2_HDR_LEN 4 /* length of the header */
671 * A header containing additional SITA WAN information.
673 #define SITA_FLAGS_OFFSET 0 /* 1 byte */
674 #define SITA_SIGNALS_OFFSET 1 /* 1 byte */
675 #define SITA_ERRORS1_OFFSET 2 /* 1 byte */
676 #define SITA_ERRORS2_OFFSET 3 /* 1 byte */
677 #define SITA_PROTO_OFFSET 4 /* 1 byte */
678 #define SITA_HDR_LEN 5 /* length of the header */
681 * The fake link-layer header of LAPD packets.
684 #define ETH_P_LAPD 0x0030
687 #define LAPD_SLL_PKTTYPE_OFFSET 0 /* packet type - 2 bytes */
688 #define LAPD_SLL_HATYPE_OFFSET 2 /* hardware address type - 2 bytes */
689 #define LAPD_SLL_HALEN_OFFSET 4 /* hardware address length - 2 bytes */
690 #define LAPD_SLL_ADDR_OFFSET 6 /* address - 8 bytes */
691 #define LAPD_SLL_PROTOCOL_OFFSET 14 /* protocol, should be ETH_P_LAPD - 2 bytes */
692 #define LAPD_SLL_LEN 16 /* length of the header */
695 * I2C link-layer on-disk format
697 struct i2c_file_hdr {
703 pcap_read_sunatm_pseudoheader(FILE_T fh,
704 union wtap_pseudo_header *pseudo_header, int *err)
706 guint8 atm_phdr[SUNATM_LEN];
711 errno = WTAP_ERR_CANT_READ;
712 bytes_read = file_read(atm_phdr, 1, SUNATM_LEN, fh);
713 if (bytes_read != SUNATM_LEN) {
714 *err = file_error(fh);
716 *err = WTAP_ERR_SHORT_READ;
720 vpi = atm_phdr[SUNATM_VPI];
721 vci = pntohs(&atm_phdr[SUNATM_VCI]);
723 switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
725 case 0x01: /* LANE */
726 pseudo_header->atm.aal = AAL_5;
727 pseudo_header->atm.type = TRAF_LANE;
730 case 0x02: /* RFC 1483 LLC multiplexed traffic */
731 pseudo_header->atm.aal = AAL_5;
732 pseudo_header->atm.type = TRAF_LLCMX;
735 case 0x05: /* ILMI */
736 pseudo_header->atm.aal = AAL_5;
737 pseudo_header->atm.type = TRAF_ILMI;
740 case 0x06: /* Q.2931 */
741 pseudo_header->atm.aal = AAL_SIGNALLING;
742 pseudo_header->atm.type = TRAF_UNKNOWN;
745 case 0x03: /* MARS (RFC 2022) */
746 pseudo_header->atm.aal = AAL_5;
747 pseudo_header->atm.type = TRAF_UNKNOWN;
750 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
751 pseudo_header->atm.aal = AAL_5;
752 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
757 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
758 * case assume it's AAL_SIGNALLING; we know nothing more
761 * XXX - is this necessary? Or are we guaranteed that
762 * all signalling traffic has a type of 0x06?
764 * XXX - is this guaranteed to be AAL5? Or, if the type is
765 * 0x00 ("raw"), might it be non-AAL5 traffic?
767 if (vpi == 0 && vci == 5)
768 pseudo_header->atm.aal = AAL_SIGNALLING;
770 pseudo_header->atm.aal = AAL_5;
771 pseudo_header->atm.type = TRAF_UNKNOWN;
774 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
776 pseudo_header->atm.vpi = vpi;
777 pseudo_header->atm.vci = vci;
778 pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
780 /* We don't have this information */
781 pseudo_header->atm.flags = 0;
782 pseudo_header->atm.cells = 0;
783 pseudo_header->atm.aal5t_u2u = 0;
784 pseudo_header->atm.aal5t_len = 0;
785 pseudo_header->atm.aal5t_chksum = 0;
791 pcap_read_nokiaatm_pseudoheader(FILE_T fh,
792 union wtap_pseudo_header *pseudo_header, int *err)
794 guint8 atm_phdr[NOKIAATM_LEN];
799 errno = WTAP_ERR_CANT_READ;
800 bytes_read = file_read(atm_phdr, 1, NOKIAATM_LEN, fh);
801 if (bytes_read != NOKIAATM_LEN) {
802 *err = file_error(fh);
804 *err = WTAP_ERR_SHORT_READ;
808 vpi = atm_phdr[NOKIAATM_VPI];
809 vci = pntohs(&atm_phdr[NOKIAATM_VCI]);
811 pseudo_header->atm.vpi = vpi;
812 pseudo_header->atm.vci = vci;
813 pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
815 /* We don't have this information */
816 pseudo_header->atm.flags = 0;
817 pseudo_header->atm.cells = 0;
818 pseudo_header->atm.aal5t_u2u = 0;
819 pseudo_header->atm.aal5t_len = 0;
820 pseudo_header->atm.aal5t_chksum = 0;
826 pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
827 int *err, gchar **err_info)
829 guint8 irda_phdr[IRDA_SLL_LEN];
832 errno = WTAP_ERR_CANT_READ;
833 bytes_read = file_read(irda_phdr, 1, IRDA_SLL_LEN, fh);
834 if (bytes_read != IRDA_SLL_LEN) {
835 *err = file_error(fh);
837 *err = WTAP_ERR_SHORT_READ;
841 if (pntohs(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
842 *err = WTAP_ERR_BAD_RECORD;
843 if (err_info != NULL)
844 *err_info = g_strdup("libpcap: IrDA capture has a packet with an invalid sll_protocol field");
848 pseudo_header->irda.pkttype = pntohs(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
854 pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
856 guint8 mtp2_hdr[MTP2_HDR_LEN];
859 errno = WTAP_ERR_CANT_READ;
860 bytes_read = file_read(mtp2_hdr, 1, MTP2_HDR_LEN, fh);
861 if (bytes_read != MTP2_HDR_LEN) {
862 *err = file_error(fh);
864 *err = WTAP_ERR_SHORT_READ;
868 pseudo_header->mtp2.sent = mtp2_hdr[MTP2_SENT_OFFSET];
869 pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
870 pseudo_header->mtp2.link_number = pntohs(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
876 pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
877 int *err, gchar **err_info)
879 guint8 lapd_phdr[LAPD_SLL_LEN];
882 errno = WTAP_ERR_CANT_READ;
883 bytes_read = file_read(lapd_phdr, 1, LAPD_SLL_LEN, fh);
884 if (bytes_read != LAPD_SLL_LEN) {
885 *err = file_error(fh);
887 *err = WTAP_ERR_SHORT_READ;
891 if (pntohs(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
892 *err = WTAP_ERR_BAD_RECORD;
893 if (err_info != NULL)
894 *err_info = g_strdup("libpcap: LAPD capture has a packet with an invalid sll_protocol field");
898 pseudo_header->lapd.pkttype = pntohs(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
899 pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
905 pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
907 guint8 sita_phdr[SITA_HDR_LEN];
910 errno = WTAP_ERR_CANT_READ;
911 bytes_read = file_read(sita_phdr, 1, SITA_HDR_LEN, fh);
912 if (bytes_read != SITA_HDR_LEN) {
913 *err = file_error(fh);
915 *err = WTAP_ERR_SHORT_READ;
919 pseudo_header->sita.flags = sita_phdr[SITA_FLAGS_OFFSET];
920 pseudo_header->sita.signals = sita_phdr[SITA_SIGNALS_OFFSET];
921 pseudo_header->sita.errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
922 pseudo_header->sita.errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
923 pseudo_header->sita.proto = sita_phdr[SITA_PROTO_OFFSET];
929 * When not using the memory-mapped interface to capture USB events,
930 * code that reads those events can use the MON_IOCX_GET ioctl to
931 * read a 48-byte header consisting of a "struct linux_usb_phdr", as
932 * defined below, followed immediately by one of:
934 * 8 bytes of a "struct usb_device_setup_hdr", if "setup_flag"
935 * in the preceding "struct linux_usb_phdr" is 0;
937 * in Linux 2.6.30 or later, 8 bytes of a "struct iso_rec", if
938 * this is an isochronous transfer;
940 * 8 bytes of junk, otherwise.
942 * In Linux 2.6.31 and later, it can also use the MON_IOCX_GETX ioctl
943 * to read a 64-byte header; that header consists of the 48 bytes
944 * above, followed immediately by 16 bytes of a "struct linux_usb_phdr_ext",
947 * In Linux 2.6.21 and later, there's a memory-mapped interface to
948 * capture USB events. In that interface, the events in the memory-mapped
949 * buffer have a 64-byte header, followed immediately by the data.
950 * In Linux 2.6.21 through 2.6.30.x, the 64-byte header is the 48-byte
951 * header described above, followed by 16 bytes of zeroes; in Linux
952 * 2.6.31 and later, the 64-byte header is the 64-byte header described
955 * See linux/Documentation/usb/usbmon.txt and libpcap/pcap/usb.h for details.
957 * With WTAP_ENCAP_USB_LINUX, packets have the 48-byte header; with
958 * WTAP_ENCAP_USB_LINUX_MMAPPED, they have the 64-byte header. There
959 * is no indication of whether the header has the "struct iso_rec", or
960 * whether the last 16 bytes of a 64-byte header are all zeros or are
961 * a "struct linux_usb_phdr_ext".
965 * URB transfer_type values
967 #define URB_ISOCHRONOUS 0x0
968 #define URB_INTERRUPT 0x1
969 #define URB_CONTROL 0x2
973 * Information from the URB for Isochronous transfers.
975 * This structure is 8 bytes long.
983 * Header prepended by Linux kernel to each USB event.
985 * (Setup flag is '-', 'D', 'Z', or 0. Data flag is '<', '>', 'Z', or 0.)
987 * The values are in *host* byte order.
989 struct linux_usb_phdr {
990 guint64 id; /* urb id, to link submission and completion events */
991 guint8 event_type; /* Submit ('S'), Completed ('C'), Error ('E') */
992 guint8 transfer_type; /* ISO (0), Intr, Control, Bulk (3) */
993 guint8 endpoint_number; /* Endpoint number (0-15) and transfer direction */
994 guint8 device_address; /* 0-127 */
996 gint8 setup_flag; /* 0, if the urb setup header is meaningful */
997 gint8 data_flag; /* 0, if urb data is present */
1001 guint32 urb_len; /* whole len of urb this event refers to */
1002 guint32 data_len; /* amount of urb data really present in this event */
1005 * Packet-type-dependent data.
1006 * USB setup information of setup_flag is true.
1007 * Otherwise, some isochronous transfer information.
1015 * This data is provided by Linux 2.6.31 and later kernels.
1017 * For WTAP_ENCAP_USB_LINUX, it's not in the pseudo-header, so
1018 * the pseudo-header is always 48 bytes long, including the
1019 * packet-type-dependent data.
1021 * For WTAP_ENCAP_USB_LINUX_MMAPPED, the pseudo-header is always
1022 * 64 bytes long, with the packet-type-dependent data preceding
1023 * these last 16 bytes. In pre-2.6.31 kernels, it's zero padding;
1024 * in 2.6.31 and later, it's the following data.
1026 gint32 interval; /* only for Interrupt and Isochronous events */
1027 gint32 start_frame; /* for Isochronous */
1028 guint32 xfer_flags; /* copy of URB's transfer_flags */
1029 guint32 ndesc; /* actual number of isochronous descriptors */
1032 struct linux_usb_isodesc {
1040 * USB setup header as defined in USB specification
1041 * See usb_20.pdf, Chapter 9.3 'USB Device Requests' for details.
1042 * http://www.usb.org/developers/docs/usb_20_122909-2.zip
1044 * This structure is 8 bytes long.
1046 struct usb_device_setup_hdr {
1047 gint8 bmRequestType;
1056 * Offset of the *end* of a field within a particular structure.
1058 #define END_OFFSETOF(basep, fieldp) \
1059 (((char *)(void *)(fieldp)) - ((char *)(void *)(basep)) + \
1063 pcap_process_linux_usb_pseudoheader(guint packet_size, gboolean byte_swapped,
1064 gboolean header_len_64_bytes, guint8 *pd)
1066 struct linux_usb_phdr *phdr;
1067 struct linux_usb_isodesc *pisodesc;
1068 gint32 iso_numdesc, i;
1071 phdr = (struct linux_usb_phdr *)pd;
1073 if (packet_size < END_OFFSETOF(phdr, &phdr->id))
1075 PBSWAP64((guint8 *)&phdr->id);
1076 if (packet_size < END_OFFSETOF(phdr, &phdr->bus_id))
1078 PBSWAP16((guint8 *)&phdr->bus_id);
1079 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_sec))
1081 PBSWAP64((guint8 *)&phdr->ts_sec);
1082 if (packet_size < END_OFFSETOF(phdr, &phdr->ts_usec))
1084 PBSWAP32((guint8 *)&phdr->ts_usec);
1085 if (packet_size < END_OFFSETOF(phdr, &phdr->status))
1087 PBSWAP32((guint8 *)&phdr->status);
1088 if (packet_size < END_OFFSETOF(phdr, &phdr->urb_len))
1090 PBSWAP32((guint8 *)&phdr->urb_len);
1091 if (packet_size < END_OFFSETOF(phdr, &phdr->data_len))
1093 PBSWAP32((guint8 *)&phdr->data_len);
1095 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1096 if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.error_count))
1098 PBSWAP32((guint8 *)&phdr->s.iso.error_count);
1100 if (packet_size < END_OFFSETOF(phdr, &phdr->s.iso.numdesc))
1102 PBSWAP32((guint8 *)&phdr->s.iso.numdesc);
1106 if (header_len_64_bytes) {
1108 * This is either the "version 1" header, with
1109 * 16 bytes of additional fields at the end, or
1110 * a "version 0" header from a memory-mapped
1111 * capture, with 16 bytes of zeroed-out padding
1112 * at the end. Byte swap them as if this were
1113 * a "version 1" header.
1115 * Yes, the first argument to END_OFFSETOF() should
1116 * be phdr, not phdr_ext; we want the offset of
1117 * the additional fields from the beginning of
1120 if (packet_size < END_OFFSETOF(phdr, &phdr->interval))
1122 PBSWAP32((guint8 *)&phdr->interval);
1123 if (packet_size < END_OFFSETOF(phdr, &phdr->start_frame))
1125 PBSWAP32((guint8 *)&phdr->start_frame);
1126 if (packet_size < END_OFFSETOF(phdr, &phdr->xfer_flags))
1128 PBSWAP32((guint8 *)&phdr->xfer_flags);
1129 if (packet_size < END_OFFSETOF(phdr, &phdr->ndesc))
1131 PBSWAP32((guint8 *)&phdr->ndesc);
1134 if (phdr->transfer_type == URB_ISOCHRONOUS) {
1135 /* swap the values in struct linux_usb_isodesc */
1137 if (header_len_64_bytes) {
1138 pisodesc = (struct linux_usb_isodesc*)(pd + 64);
1140 pisodesc = (struct linux_usb_isodesc*)(pd + 48);
1142 iso_numdesc = phdr->s.iso.numdesc;
1143 for (i = 0; i < iso_numdesc; i++) {
1144 /* always check if we have enough data from the
1145 * beginnig of the packet (phdr)
1147 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_status))
1149 PBSWAP32((guint8 *)&pisodesc->iso_status);
1150 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_off))
1152 PBSWAP32((guint8 *)&pisodesc->iso_off);
1153 if (packet_size < END_OFFSETOF(phdr, &pisodesc->iso_len))
1155 PBSWAP32((guint8 *)&pisodesc->iso_len);
1156 if (packet_size < END_OFFSETOF(phdr, &pisodesc->_pad))
1158 PBSWAP32((guint8 *)&pisodesc->_pad);
1167 pcap_read_bt_pseudoheader(FILE_T fh,
1168 union wtap_pseudo_header *pseudo_header, int *err)
1171 struct libpcap_bt_phdr phdr;
1173 errno = WTAP_ERR_CANT_READ;
1174 bytes_read = file_read(&phdr, 1,
1175 sizeof (struct libpcap_bt_phdr), fh);
1176 if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
1177 *err = file_error(fh);
1179 *err = WTAP_ERR_SHORT_READ;
1182 pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
1187 pcap_read_ppp_pseudoheader(FILE_T fh,
1188 union wtap_pseudo_header *pseudo_header, int *err)
1191 struct libpcap_ppp_phdr phdr;
1193 errno = WTAP_ERR_CANT_READ;
1194 bytes_read = file_read(&phdr, 1,
1195 sizeof (struct libpcap_ppp_phdr), fh);
1196 if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
1197 *err = file_error(fh);
1199 *err = WTAP_ERR_SHORT_READ;
1202 pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
1207 pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
1208 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
1210 guint8 erf_hdr[sizeof(struct erf_phdr)];
1213 errno = WTAP_ERR_CANT_READ;
1214 bytes_read = file_read(erf_hdr, 1, sizeof(struct erf_phdr), fh);
1215 if (bytes_read != sizeof(struct erf_phdr)) {
1216 *err = file_error(fh);
1218 *err = WTAP_ERR_SHORT_READ;
1221 pseudo_header->erf.phdr.ts = pletohll(&erf_hdr[0]); /* timestamp */
1222 pseudo_header->erf.phdr.type = erf_hdr[8];
1223 pseudo_header->erf.phdr.flags = erf_hdr[9];
1224 pseudo_header->erf.phdr.rlen = pntohs(&erf_hdr[10]);
1225 pseudo_header->erf.phdr.lctr = pntohs(&erf_hdr[12]);
1226 pseudo_header->erf.phdr.wlen = pntohs(&erf_hdr[14]);
1228 /* The high 32 bits of the timestamp contain the integer number of seconds
1229 * while the lower 32 bits contain the binary fraction of the second.
1230 * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1232 guint64 ts = pseudo_header->erf.phdr.ts;
1233 whdr->ts.secs = (guint32) (ts >> 32);
1234 ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1235 ts += (ts & 0x80000000) << 1; /* rounding */
1236 whdr->ts.nsecs = ((guint32) (ts >> 32));
1237 if ( whdr->ts.nsecs >= 1000000000) {
1238 whdr->ts.nsecs -= 1000000000;
1246 * If the type of record given in the pseudo header indicate the presence of an extension
1247 * header then, read all the extension headers
1250 pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1251 int *err, gchar **err_info _U_, guint * psize)
1254 guint8 erf_exhdr[8];
1255 guint64 erf_exhdr_sw;
1256 int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1257 guint8 type = pseudo_header->erf.phdr.type;
1259 if (pseudo_header->erf.phdr.type & 0x80){
1261 errno = WTAP_ERR_CANT_READ;
1262 bytes_read = file_read(erf_exhdr, 1, 8, fh);
1263 if (bytes_read != 8 ) {
1264 *err = file_error(fh);
1266 *err = WTAP_ERR_SHORT_READ;
1269 type = erf_exhdr[0];
1270 erf_exhdr_sw = pntohll((guint64*) &(erf_exhdr[0]));
1272 memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1275 } while (type & 0x80);
1281 * If the type of record given in the pseudo header indicate the precense of a subheader
1282 * then, read this optional subheader
1285 pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1286 int *err, gchar **err_info _U_, guint * psize)
1288 guint8 erf_subhdr[sizeof(union erf_subhdr)];
1292 switch(pseudo_header->erf.phdr.type & 0x7F) {
1293 case ERF_TYPE_MC_HDLC:
1294 case ERF_TYPE_MC_RAW:
1295 case ERF_TYPE_MC_ATM:
1296 case ERF_TYPE_MC_RAW_CHANNEL:
1297 case ERF_TYPE_MC_AAL5:
1298 case ERF_TYPE_MC_AAL2:
1299 case ERF_TYPE_COLOR_MC_HDLC_POS:
1300 /* Extract the Multi Channel header to include it in the pseudo header part */
1301 errno = WTAP_ERR_CANT_READ;
1302 bytes_read = file_read(erf_subhdr, 1, sizeof(erf_mc_header_t), fh);
1303 if (bytes_read != sizeof(erf_mc_header_t) ) {
1304 *err = file_error(fh);
1306 *err = WTAP_ERR_SHORT_READ;
1309 pseudo_header->erf.subhdr.mc_hdr = pntohl(&erf_subhdr[0]);
1310 *psize = sizeof(erf_mc_header_t);
1313 case ERF_TYPE_COLOR_ETH:
1314 case ERF_TYPE_DSM_COLOR_ETH:
1315 /* Extract the Ethernet additional header to include it in the pseudo header part */
1316 errno = WTAP_ERR_CANT_READ;
1317 bytes_read = file_read(erf_subhdr, 1, sizeof(erf_eth_header_t), fh);
1318 if (bytes_read != sizeof(erf_eth_header_t) ) {
1319 *err = file_error(fh);
1321 *err = WTAP_ERR_SHORT_READ;
1324 pseudo_header->erf.subhdr.eth_hdr = pntohs(&erf_subhdr[0]);
1325 *psize = sizeof(erf_eth_header_t);
1328 /* No optional pseudo header for this ERF type */
1335 pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
1337 struct i2c_file_hdr i2c_hdr;
1340 errno = WTAP_ERR_CANT_READ;
1341 bytes_read = file_read(&i2c_hdr, 1, sizeof (i2c_hdr), fh);
1342 if (bytes_read != sizeof (i2c_hdr)) {
1343 *err = file_error(fh);
1345 *err = WTAP_ERR_SHORT_READ;
1349 pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
1350 pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
1351 pseudo_header->i2c.flags = pntohl(&i2c_hdr.flags);
1357 pcap_process_pseudo_header(FILE_T fh, int file_type, int wtap_encap,
1358 guint packet_size, gboolean check_packet_size, struct wtap_pkthdr *phdr,
1359 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1364 switch (wtap_encap) {
1366 case WTAP_ENCAP_ATM_PDUS:
1367 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1371 if (check_packet_size && packet_size < NOKIAATM_LEN) {
1373 * Uh-oh, the packet isn't big enough to even
1374 * have a pseudo-header.
1376 *err = WTAP_ERR_BAD_RECORD;
1377 *err_info = g_strdup_printf("pcap: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1381 if (!pcap_read_nokiaatm_pseudoheader(fh,
1382 pseudo_header, err))
1383 return -1; /* Read error */
1385 phdr_len = NOKIAATM_LEN;
1390 if (check_packet_size && packet_size < SUNATM_LEN) {
1392 * Uh-oh, the packet isn't big enough to even
1393 * have a pseudo-header.
1395 *err = WTAP_ERR_BAD_RECORD;
1396 *err_info = g_strdup_printf("pcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1400 if (!pcap_read_sunatm_pseudoheader(fh,
1401 pseudo_header, err))
1402 return -1; /* Read error */
1404 phdr_len = SUNATM_LEN;
1408 case WTAP_ENCAP_ETHERNET:
1410 * We don't know whether there's an FCS in this frame or not.
1412 pseudo_header->eth.fcs_len = -1;
1415 case WTAP_ENCAP_IEEE_802_11:
1416 case WTAP_ENCAP_PRISM_HEADER:
1417 case WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP:
1418 case WTAP_ENCAP_IEEE_802_11_WLAN_AVS:
1420 * We don't know whether there's an FCS in this frame or not.
1421 * XXX - are there any OSes where the capture mechanism
1424 pseudo_header->ieee_802_11.fcs_len = -1;
1425 pseudo_header->ieee_802_11.channel = 0;
1426 pseudo_header->ieee_802_11.data_rate = 0;
1427 pseudo_header->ieee_802_11.signal_level = 0;
1430 case WTAP_ENCAP_IRDA:
1431 if (check_packet_size && packet_size < IRDA_SLL_LEN) {
1433 * Uh-oh, the packet isn't big enough to even
1434 * have a pseudo-header.
1436 *err = WTAP_ERR_BAD_RECORD;
1437 *err_info = g_strdup_printf("pcap: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1441 if (!pcap_read_irda_pseudoheader(fh, pseudo_header,
1443 return -1; /* Read error */
1445 phdr_len = IRDA_SLL_LEN;
1448 case WTAP_ENCAP_MTP2_WITH_PHDR:
1449 if (check_packet_size && packet_size < MTP2_HDR_LEN) {
1451 * Uh-oh, the packet isn't big enough to even
1452 * have a pseudo-header.
1454 *err = WTAP_ERR_BAD_RECORD;
1455 *err_info = g_strdup_printf("pcap: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1459 if (!pcap_read_mtp2_pseudoheader(fh, pseudo_header,
1461 return -1; /* Read error */
1463 phdr_len = MTP2_HDR_LEN;
1466 case WTAP_ENCAP_LINUX_LAPD:
1467 if (check_packet_size && packet_size < LAPD_SLL_LEN) {
1469 * Uh-oh, the packet isn't big enough to even
1470 * have a pseudo-header.
1472 *err = WTAP_ERR_BAD_RECORD;
1473 *err_info = g_strdup_printf("pcap: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1477 if (!pcap_read_lapd_pseudoheader(fh, pseudo_header,
1479 return -1; /* Read error */
1481 phdr_len = LAPD_SLL_LEN;
1484 case WTAP_ENCAP_SITA:
1485 if (check_packet_size && packet_size < SITA_HDR_LEN) {
1487 * Uh-oh, the packet isn't big enough to even
1488 * have a pseudo-header.
1490 *err = WTAP_ERR_BAD_RECORD;
1491 *err_info = g_strdup_printf("pcap: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1495 if (!pcap_read_sita_pseudoheader(fh, pseudo_header,
1497 return -1; /* Read error */
1499 phdr_len = SITA_HDR_LEN;
1502 case WTAP_ENCAP_BLUETOOTH_H4:
1503 /* We don't have pseudoheader, so just pretend we received everything. */
1504 pseudo_header->p2p.sent = FALSE;
1507 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1508 if (check_packet_size &&
1509 packet_size < sizeof (struct libpcap_bt_phdr)) {
1511 * Uh-oh, the packet isn't big enough to even
1512 * have a pseudo-header.
1514 *err = WTAP_ERR_BAD_RECORD;
1515 *err_info = g_strdup_printf("pcap: libpcap bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1519 if (!pcap_read_bt_pseudoheader(fh,
1520 pseudo_header, err))
1521 return -1; /* Read error */
1523 phdr_len = (int)sizeof (struct libpcap_bt_phdr);
1526 case WTAP_ENCAP_PPP_WITH_PHDR:
1527 if (check_packet_size &&
1528 packet_size < sizeof (struct libpcap_ppp_phdr)) {
1530 * Uh-oh, the packet isn't big enough to even
1531 * have a pseudo-header.
1533 *err = WTAP_ERR_BAD_RECORD;
1534 *err_info = g_strdup_printf("pcap: libpcap ppp file has a %u-byte packet, too small to have even a pseudo-header",
1538 if (!pcap_read_ppp_pseudoheader(fh,
1539 pseudo_header, err))
1540 return -1; /* Read error */
1542 phdr_len = (int)sizeof (struct libpcap_ppp_phdr);
1545 case WTAP_ENCAP_ERF:
1546 if (check_packet_size &&
1547 packet_size < sizeof(struct erf_phdr) ) {
1549 * Uh-oh, the packet isn't big enough to even
1550 * have a pseudo-header.
1552 *err = WTAP_ERR_BAD_RECORD;
1553 *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1558 if (!pcap_read_erf_pseudoheader(fh, phdr, pseudo_header,
1560 return -1; /* Read error */
1562 phdr_len = (int)sizeof(struct erf_phdr);
1564 /* check the optional Extension header */
1565 if (!pcap_read_erf_exheader(fh, pseudo_header, err, err_info,
1567 return -1; /* Read error */
1571 /* check the optional Multi Channel header */
1572 if (!pcap_read_erf_subheader(fh, pseudo_header, err, err_info,
1574 return -1; /* Read error */
1579 case WTAP_ENCAP_I2C:
1580 if (check_packet_size &&
1581 packet_size < sizeof (struct i2c_file_hdr)) {
1583 * Uh-oh, the packet isn't big enough to even
1584 * have a pseudo-header.
1586 *err = WTAP_ERR_BAD_RECORD;
1587 *err_info = g_strdup_printf("pcap: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1591 if (!pcap_read_i2c_pseudoheader(fh, pseudo_header,
1593 return -1; /* Read error */
1596 * Don't count the pseudo-header as part of the packet.
1598 phdr_len = (int)sizeof (struct i2c_file_hdr);
1606 pcap_read_post_process(int wtap_encap, guint packet_size,
1607 gboolean bytes_swapped, guchar *pd)
1609 switch (wtap_encap) {
1611 case WTAP_ENCAP_USB_LINUX:
1612 pcap_process_linux_usb_pseudoheader(packet_size,
1613 bytes_swapped, FALSE, pd);
1616 case WTAP_ENCAP_USB_LINUX_MMAPPED:
1617 pcap_process_linux_usb_pseudoheader(packet_size,
1618 bytes_swapped, TRUE, pd);
1627 pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
1633 case WTAP_ENCAP_ATM_PDUS:
1634 hdrsize = SUNATM_LEN;
1637 case WTAP_ENCAP_IRDA:
1638 hdrsize = IRDA_SLL_LEN;
1641 case WTAP_ENCAP_MTP2_WITH_PHDR:
1642 hdrsize = MTP2_HDR_LEN;
1645 case WTAP_ENCAP_LINUX_LAPD:
1646 hdrsize = LAPD_SLL_LEN;
1649 case WTAP_ENCAP_SITA:
1650 hdrsize = SITA_HDR_LEN;
1653 case WTAP_ENCAP_ERF:
1654 hdrsize = (int)sizeof (struct erf_phdr);
1655 if (pseudo_header->erf.phdr.type & 0x80)
1657 switch (pseudo_header->erf.phdr.type & 0x7F) {
1659 case ERF_TYPE_MC_HDLC:
1660 case ERF_TYPE_MC_RAW:
1661 case ERF_TYPE_MC_ATM:
1662 case ERF_TYPE_MC_RAW_CHANNEL:
1663 case ERF_TYPE_MC_AAL5:
1664 case ERF_TYPE_MC_AAL2:
1665 case ERF_TYPE_COLOR_MC_HDLC_POS:
1666 hdrsize += (int)sizeof(struct erf_mc_hdr);
1670 case ERF_TYPE_COLOR_ETH:
1671 case ERF_TYPE_DSM_COLOR_ETH:
1672 hdrsize += (int)sizeof(struct erf_eth_hdr);
1680 case WTAP_ENCAP_I2C:
1681 hdrsize = (int)sizeof (struct i2c_file_hdr);
1684 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1685 hdrsize = (int)sizeof (struct libpcap_bt_phdr);
1688 case WTAP_ENCAP_PPP_WITH_PHDR:
1689 hdrsize = (int)sizeof (struct libpcap_ppp_phdr);
1701 pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
1704 guint8 atm_hdr[SUNATM_LEN];
1705 guint8 irda_hdr[IRDA_SLL_LEN];
1706 guint8 lapd_hdr[LAPD_SLL_LEN];
1707 guint8 mtp2_hdr[MTP2_HDR_LEN];
1708 guint8 sita_hdr[SITA_HDR_LEN];
1709 guint8 erf_hdr[ sizeof(struct erf_mc_phdr)];
1710 struct i2c_file_hdr i2c_hdr;
1711 struct libpcap_bt_phdr bt_hdr;
1712 struct libpcap_ppp_phdr ppp_hdr;
1717 case WTAP_ENCAP_ATM_PDUS:
1719 * Write the ATM header.
1721 atm_hdr[SUNATM_FLAGS] =
1722 (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1723 switch (pseudo_header->atm.aal) {
1725 case AAL_SIGNALLING:
1727 atm_hdr[SUNATM_FLAGS] |= 0x06;
1731 switch (pseudo_header->atm.type) {
1735 atm_hdr[SUNATM_FLAGS] |= 0x01;
1739 /* RFC 1483 LLC multiplexed traffic */
1740 atm_hdr[SUNATM_FLAGS] |= 0x02;
1745 atm_hdr[SUNATM_FLAGS] |= 0x05;
1750 atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
1751 phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1752 if (!wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr), err))
1754 wdh->bytes_dumped += sizeof(atm_hdr);
1757 case WTAP_ENCAP_IRDA:
1759 * Write the IrDA header.
1761 memset(irda_hdr, 0, sizeof(irda_hdr));
1762 phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
1763 pseudo_header->irda.pkttype);
1764 phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
1765 if (!wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr), err))
1767 wdh->bytes_dumped += sizeof(irda_hdr);
1770 case WTAP_ENCAP_MTP2_WITH_PHDR:
1772 * Write the MTP2 header.
1774 memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
1775 mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
1776 mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
1777 phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
1778 pseudo_header->mtp2.link_number);
1779 if (!wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr), err))
1781 wdh->bytes_dumped += sizeof(mtp2_hdr);
1784 case WTAP_ENCAP_LINUX_LAPD:
1786 * Write the LAPD header.
1788 memset(&lapd_hdr, 0, sizeof(lapd_hdr));
1789 phtons(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET],
1790 pseudo_header->lapd.pkttype);
1791 phtons(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
1792 lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
1793 pseudo_header->lapd.we_network?0x01:0x00;
1794 if (!wtap_dump_file_write(wdh, lapd_hdr, sizeof(lapd_hdr), err))
1796 wdh->bytes_dumped += sizeof(lapd_hdr);
1799 case WTAP_ENCAP_SITA:
1801 * Write the SITA header.
1803 memset(&sita_hdr, 0, sizeof(sita_hdr));
1804 sita_hdr[SITA_FLAGS_OFFSET] = pseudo_header->sita.flags;
1805 sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.signals;
1806 sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.errors1;
1807 sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.errors2;
1808 sita_hdr[SITA_PROTO_OFFSET] = pseudo_header->sita.proto;
1809 if (!wtap_dump_file_write(wdh, sita_hdr, sizeof(sita_hdr), err))
1811 wdh->bytes_dumped += sizeof(sita_hdr);
1814 case WTAP_ENCAP_ERF:
1816 * Write the ERF header.
1818 memset(&erf_hdr, 0, sizeof(erf_hdr));
1819 pletonll(&erf_hdr[0], pseudo_header->erf.phdr.ts);
1820 erf_hdr[8] = pseudo_header->erf.phdr.type;
1821 erf_hdr[9] = pseudo_header->erf.phdr.flags;
1822 phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
1823 phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
1824 phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
1825 size = sizeof(struct erf_phdr);
1827 switch(pseudo_header->erf.phdr.type & 0x7F) {
1828 case ERF_TYPE_MC_HDLC:
1829 case ERF_TYPE_MC_RAW:
1830 case ERF_TYPE_MC_ATM:
1831 case ERF_TYPE_MC_RAW_CHANNEL:
1832 case ERF_TYPE_MC_AAL5:
1833 case ERF_TYPE_MC_AAL2:
1834 case ERF_TYPE_COLOR_MC_HDLC_POS:
1835 phtonl(&erf_hdr[16], pseudo_header->erf.subhdr.mc_hdr);
1836 size += (int)sizeof(struct erf_mc_hdr);
1839 case ERF_TYPE_COLOR_ETH:
1840 case ERF_TYPE_DSM_COLOR_ETH:
1841 phtons(&erf_hdr[16], pseudo_header->erf.subhdr.eth_hdr);
1842 size += (int)sizeof(struct erf_eth_hdr);
1847 if (!wtap_dump_file_write(wdh, erf_hdr, size, err))
1849 wdh->bytes_dumped += size;
1852 case WTAP_ENCAP_I2C:
1854 * Write the I2C header.
1856 memset(&i2c_hdr, 0, sizeof(i2c_hdr));
1857 i2c_hdr.bus = pseudo_header->i2c.bus |
1858 (pseudo_header->i2c.is_event ? 0x80 : 0x00);
1859 phtonl((guint8 *)&i2c_hdr.flags, pseudo_header->i2c.flags);
1860 if (!wtap_dump_file_write(wdh, &i2c_hdr, sizeof(i2c_hdr), err))
1862 wdh->bytes_dumped += sizeof(i2c_hdr);
1865 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1866 bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
1867 if (!wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr, err))
1869 wdh->bytes_dumped += sizeof bt_hdr;
1872 case WTAP_ENCAP_PPP_WITH_PHDR:
1873 ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
1874 if (!wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr, err))
1876 wdh->bytes_dumped += sizeof ppp_hdr;