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-common.h"
40 * Map link-layer types (LINKTYPE_ values) to Wiretap encapsulations.
43 * Either LBL NRG wasn't an adequate central registry (e.g., because of
44 * the slow rate of releases from them), or nobody bothered using them
45 * as a central registry, as many different groups have patched libpcap
46 * (and BPF, on the BSDs) to add new encapsulation types, and have ended
47 * up using the same DLT_ values for different encapsulation types.
49 * For those numerical encapsulation type values that everybody uses for
50 * the same encapsulation type (which inclues those that some platforms
51 * specify different DLT_ names for but don't appear to use), we map
52 * those values to the appropriate Wiretap values.
54 * For those numerical encapsulation type values that different libpcap
55 * variants use for different encapsulation types, we check what
56 * <pcap.h> defined to determine how to interpret them, so that we
57 * interpret them the way the libpcap with which we're building
58 * Wireshark/Wiretap interprets them (which, if it doesn't support
59 * them at all, means we don't support them either - any capture files
60 * using them are foreign, and we don't hazard a guess as to which
61 * platform they came from; we could, I guess, choose the most likely
64 * Note: if you need a new encapsulation type for libpcap files, do
65 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
66 * add a new encapsulation type by changing an existing entry;
67 * leave the existing entries alone.
69 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking for
70 * a new DLT_ value, and specifying the purpose of the new value. When
71 * you get the new DLT_ value, use that numerical value in the "dlt_value"
72 * field of "pcap_to_wtap_map[]".
78 } pcap_to_wtap_map[] = {
80 * These are the values that are almost certainly the same
81 * in all libpcaps (I've yet to find one where the values
82 * in question are used for some purpose other than the
83 * one below, but...), and that Wiretap and Wireshark
86 { 0, WTAP_ENCAP_NULL }, /* null encapsulation */
87 { 1, WTAP_ENCAP_ETHERNET },
88 { 6, WTAP_ENCAP_TOKEN_RING }, /* IEEE 802 Networks - assume token ring */
89 { 7, WTAP_ENCAP_ARCNET },
90 { 8, WTAP_ENCAP_SLIP },
91 { 9, WTAP_ENCAP_PPP },
92 #ifdef BIT_SWAPPED_MAC_ADDRS
93 { 10, WTAP_ENCAP_FDDI_BITSWAPPED },
95 { 10, WTAP_ENCAP_FDDI },
98 { 32, WTAP_ENCAP_REDBACK },
101 * 50 is DLT_PPP_SERIAL in NetBSD; it appears that DLT_PPP
102 * on BSD (at least according to standard tcpdump) has, as
103 * the first octet, an indication of whether the packet was
104 * transmitted or received (rather than having the standard
105 * PPP address value of 0xff), but that DLT_PPP_SERIAL puts
106 * a real live PPP header there, or perhaps a Cisco PPP header
107 * as per section 4.3.1 of RFC 1547 (implementations of this
108 * exist in various BSDs in "sys/net/if_spppsubr.c", and
109 * I think also exist either in standard Linux or in
110 * various Linux patches; the implementations show how to handle
111 * Cisco keepalive packets).
113 * However, I don't see any obvious place in FreeBSD "if_ppp.c"
114 * where anything other than the standard PPP header would be
115 * passed up. I see some stuff that sets the first octet
116 * to 0 for incoming and 1 for outgoing packets before applying
117 * a BPF filter to see whether to drop packets whose protocol
118 * field has the 0x8000 bit set, i.e. network control protocols -
119 * those are handed up to userland - but that code puts the
120 * address field back before passing the packet up.
122 * I also don't see anything immediately obvious that munges
123 * the address field for sync PPP, either.
125 * Wireshark currently assumes that if the first octet of a
126 * PPP frame is 0xFF, it's the address field and is followed
127 * by a control field and a 2-byte protocol, otherwise the
128 * address and control fields are absent and the frame begins
129 * with a protocol field. If we ever see a BSD/OS PPP
130 * capture, we'll have to handle it differently, and we may
131 * have to handle standard BSD captures differently if, in fact,
132 * they don't have 0xff 0x03 as the first two bytes - but, as per
133 * the two paragraphs preceding this, it's not clear that
134 * the address field *is* munged into an incoming/outgoing
135 * field when the packet is handed to the BPF device.
137 * For now, we just map DLT_PPP_SERIAL to WTAP_ENCAP_PPP, as
138 * we treat WTAP_ENCAP_PPP packets as if those beginning with
139 * 0xff have the standard RFC 1662 "PPP in HDLC-like Framing"
140 * 0xff 0x03 address/control header, and DLT_PPP_SERIAL frames
141 * appear to contain that unless they're Cisco frames (if we
142 * ever see a capture with them, we'd need to implement the
143 * RFC 1547 stuff, and the keepalive protocol stuff).
145 * We may have to distinguish between "PPP where if it doesn't
146 * begin with 0xff there's no HDLC encapsulation and the frame
147 * begins with the protocol field" (which is how we handle
148 * WTAP_ENCAP_PPP now) and "PPP where there's either HDLC
149 * encapsulation or Cisco PPP" (which is what DLT_PPP_SERIAL
152 * XXX - NetBSD has DLT_HDLC, which appears to be used for
153 * Cisco HDLC. Ideally, they should use DLT_PPP_SERIAL
154 * only for real live HDLC-encapsulated PPP, not for Cisco
157 { 50, WTAP_ENCAP_PPP },
160 * Apparently used by the Axent Raptor firewall (now Symantec
161 * Enterprise Firewall).
162 * Thanks, Axent, for not reserving that type with tcpdump.org
163 * and not telling anybody about it.
165 { 99, WTAP_ENCAP_SYMANTEC },
168 * These are the values that libpcap 0.5 and later use in
169 * capture file headers, in an attempt to work around the
170 * confusion decried above, and that Wiretap and Wireshark
173 { 100, WTAP_ENCAP_ATM_RFC1483 },
174 { 101, WTAP_ENCAP_RAW_IP },
177 * More values used by libpcap 0.5 as DLT_ values and used by the
178 * current CVS version of libpcap in capture file headers.
179 * They are not yet handled in Wireshark.
180 * If we get a capture that contains them, we'll implement them.
182 { 102, WTAP_ENCAP_SLIP_BSDOS },
183 { 103, WTAP_ENCAP_PPP_BSDOS },
187 * These ones are handled in Wireshark, though.
189 { 104, WTAP_ENCAP_CHDLC }, /* Cisco HDLC */
190 { 105, WTAP_ENCAP_IEEE_802_11 }, /* IEEE 802.11 */
191 { 106, WTAP_ENCAP_LINUX_ATM_CLIP },
192 { 107, WTAP_ENCAP_FRELAY }, /* Frame Relay */
193 { 108, WTAP_ENCAP_NULL }, /* OpenBSD loopback */
194 { 109, WTAP_ENCAP_ENC }, /* OpenBSD IPSEC enc */
196 { 110, WTAP_ENCAP_LANE_802_3 },/* ATM LANE 802.3 */
197 { 111, WTAP_ENCAP_HIPPI }, /* NetBSD HIPPI */
199 { 112, WTAP_ENCAP_CHDLC }, /* NetBSD HDLC framing */
202 * Linux "cooked mode" captures, used by the current CVS version
205 * it could be a packet in Cisco's ERSPAN encapsulation which uses
206 * this number as well (why can't people stick to protocols when it
207 * comes to allocating/using DLT types).
209 { 113, WTAP_ENCAP_SLL }, /* Linux cooked capture */
211 { 114, WTAP_ENCAP_LOCALTALK }, /* Localtalk */
214 * The tcpdump.org version of libpcap uses 117, rather than 17,
215 * for OpenBSD packet filter logging, so as to avoid conflicting
216 * with DLT_LANE8023 in SuSE 6.3 libpcap.
218 { 117, WTAP_ENCAP_PFLOG },
220 { 118, WTAP_ENCAP_CISCO_IOS },
221 { 119, WTAP_ENCAP_PRISM_HEADER }, /* Prism monitor mode hdr */
222 { 121, WTAP_ENCAP_HHDLC }, /* HiPath HDLC */
223 { 122, WTAP_ENCAP_IP_OVER_FC }, /* RFC 2625 IP-over-FC */
224 { 123, WTAP_ENCAP_ATM_PDUS }, /* SunATM */
225 { 127, WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP }, /* 802.11 plus radiotap WLAN header */
226 { 128, WTAP_ENCAP_TZSP }, /* Tazmen Sniffer Protocol */
227 { 129, WTAP_ENCAP_ARCNET_LINUX },
228 { 130, WTAP_ENCAP_JUNIPER_MLPPP }, /* Juniper MLPPP on ML-, LS-, AS- PICs */
229 { 131, WTAP_ENCAP_JUNIPER_MLFR }, /* Juniper MLFR (FRF.15) on ML-, LS-, AS- PICs */
230 { 133, WTAP_ENCAP_JUNIPER_GGSN},
232 * Values 132-134, 136 not listed here are reserved for use
233 * in Juniper hardware.
235 { 135, WTAP_ENCAP_JUNIPER_ATM2 }, /* various encapsulations captured on the ATM2 PIC */
236 { 137, WTAP_ENCAP_JUNIPER_ATM1 }, /* various encapsulations captured on the ATM1 PIC */
238 { 138, WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 },
239 /* Apple IP-over-IEEE 1394 */
241 { 139, WTAP_ENCAP_MTP2_WITH_PHDR },
242 { 140, WTAP_ENCAP_MTP2 },
243 { 141, WTAP_ENCAP_MTP3 },
244 { 142, WTAP_ENCAP_SCCP },
245 { 143, WTAP_ENCAP_DOCSIS },
246 { 144, WTAP_ENCAP_IRDA }, /* IrDA capture */
248 /* Reserved for private use. */
249 { 147, WTAP_ENCAP_USER0 },
250 { 148, WTAP_ENCAP_USER1 },
251 { 149, WTAP_ENCAP_USER2 },
252 { 150, WTAP_ENCAP_USER3 },
253 { 151, WTAP_ENCAP_USER4 },
254 { 152, WTAP_ENCAP_USER5 },
255 { 153, WTAP_ENCAP_USER6 },
256 { 154, WTAP_ENCAP_USER7 },
257 { 155, WTAP_ENCAP_USER8 },
258 { 156, WTAP_ENCAP_USER9 },
259 { 157, WTAP_ENCAP_USER10 },
260 { 158, WTAP_ENCAP_USER11 },
261 { 159, WTAP_ENCAP_USER12 },
262 { 160, WTAP_ENCAP_USER13 },
263 { 161, WTAP_ENCAP_USER14 },
264 { 162, WTAP_ENCAP_USER15 },
266 { 163, WTAP_ENCAP_IEEE_802_11_WLAN_AVS }, /* 802.11 plus AVS WLAN header */
269 * 164 is reserved for Juniper-private chassis-internal
270 * meta-information such as QoS profiles, etc..
273 { 165, WTAP_ENCAP_BACNET_MS_TP },
276 * 166 is reserved for a PPP variant in which the first byte
277 * of the 0xff03 header, the 0xff, is replaced by a direction
278 * byte. I don't know whether any captures look like that,
279 * but it is used for some Linux IP filtering (ipfilter?).
282 /* Ethernet PPPoE frames captured on a service PIC */
283 { 167, WTAP_ENCAP_JUNIPER_PPPOE },
286 * 168 is reserved for more Juniper private-chassis-
287 * internal meta-information.
290 { 169, WTAP_ENCAP_GPRS_LLC },
293 * 170 and 171 are reserved for ITU-T G.7041/Y.1303 Generic
297 /* Registered by Gcom, Inc. */
298 { 172, WTAP_ENCAP_GCOM_TIE1 },
299 { 173, WTAP_ENCAP_GCOM_SERIAL },
301 { 177, WTAP_ENCAP_LINUX_LAPD },
303 /* Ethernet frames prepended with meta-information */
304 { 178, WTAP_ENCAP_JUNIPER_ETHER },
305 /* PPP frames prepended with meta-information */
306 { 179, WTAP_ENCAP_JUNIPER_PPP },
307 /* Frame-Relay frames prepended with meta-information */
308 { 180, WTAP_ENCAP_JUNIPER_FRELAY },
309 /* C-HDLC frames prepended with meta-information */
310 { 181, WTAP_ENCAP_JUNIPER_CHDLC },
311 /* VOIP Frames prepended with meta-information */
312 { 183, WTAP_ENCAP_JUNIPER_VP },
313 /* raw USB packets */
314 { 186, WTAP_ENCAP_USB },
315 /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
316 { 187, WTAP_ENCAP_BLUETOOTH_H4 },
317 /* IEEE 802.16 MAC Common Part Sublayer */
318 { 188, WTAP_ENCAP_IEEE802_16_MAC_CPS },
319 /* USB packets with Linux-specified header */
320 { 189, WTAP_ENCAP_USB_LINUX },
322 { 190, WTAP_ENCAP_CAN20B },
323 /* Per-Packet Information header */
324 { 192, WTAP_ENCAP_PPI },
325 /* IEEE 802.15.4 Wireless PAN */
326 { 195, WTAP_ENCAP_IEEE802_15_4 },
327 /* SITA File Encapsulation */
328 { 196, WTAP_ENCAP_SITA },
329 /* Endace Record File Encapsulation */
330 { 197, WTAP_ENCAP_ERF },
332 { 199, WTAP_ENCAP_IPMB },
333 /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
334 { 201, WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR },
335 { 204, WTAP_ENCAP_PPP_WITH_PHDR },
337 { 209, WTAP_ENCAP_I2C },
339 { 210, WTAP_ENCAP_FLEXRAY },
341 { 211, WTAP_ENCAP_MOST },
343 { 212, WTAP_ENCAP_LIN },
344 /* X2E Xoraya serial frame */
345 { 213, WTAP_ENCAP_X2E_SERIAL },
346 /* X2E Xoraya frame */
347 { 214, WTAP_ENCAP_X2E_XORAYA },
348 /* IEEE 802.15.4 Wireless PAN non-ASK PHY */
349 { 215, WTAP_ENCAP_IEEE802_15_4_NONASK_PHY },
350 /* USB packets with padded Linux-specified header */
351 { 220, WTAP_ENCAP_USB_LINUX_MMAPPED },
352 /* Fibre Channel FC-2 frame */
353 { 224, WTAP_ENCAP_FIBRE_CHANNEL_FC2 },
354 /* Fibre Channel FC-2 frame with Delimiter */
355 { 225, WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS },
357 { 226, WTAP_ENCAP_IPNET },
359 /* SocketCAN frame */
360 { 227, WTAP_ENCAP_SOCKETCAN },
365 * If you need a new encapsulation type for libpcap files, do
366 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
367 * add a new encapsulation type by changing an existing entry;
368 * leave the existing entries alone.
370 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
371 * for a new DLT_ value, and specifying the purpose of the new value.
372 * When you get the new DLT_ value, use that numerical value in
373 * the "dlt_value" field of "pcap_to_wtap_map[]".
377 * The following are entries for libpcap type values that have
378 * different meanings on different OSes.
380 * We put these *after* the entries for the platform-independent
381 * libpcap type values for those Wiretap encapsulation types, so
382 * that Wireshark chooses the platform-independent libpcap type
383 * value for those encapsulatioin types, not the platform-dependent
388 * 11 is DLT_ATM_RFC1483 on most platforms; the only libpcaps I've
389 * seen that define anything other than DLT_ATM_RFC1483 as 11 are
390 * the BSD/OS one, which defines DLT_FR as 11, and libpcap 0.5,
391 * which define it as 100, mapping the kernel's value to 100, in
392 * an attempt to hide the different values used on different
395 * If this is a platform where DLT_FR is defined as 11, we
396 * don't handle 11 at all; otherwise, we handle it as
397 * DLT_ATM_RFC1483 (this means we'd misinterpret Frame Relay
398 * captures from BSD/OS if running on platforms other than BSD/OS,
401 * 1) we don't yet support DLT_FR
405 * 2) nothing short of a heuristic would let us interpret
408 #if defined(DLT_FR) && (DLT_FR == 11)
409 { 11, WTAP_ENCAP_FRELAY },
411 { 11, WTAP_ENCAP_ATM_RFC1483 },
415 * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
416 * BSD/OS, and DLT_LOOP on OpenBSD.
418 * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
419 * (it's just like DLT_NULL, only with the AF_ value in network
420 * rather than host byte order - Wireshark figures out the
421 * byte order from the data, so we don't care what byte order
422 * it's in), so if DLT_LOOP is defined as 12, interpret 12
423 * as WTAP_ENCAP_NULL, otherwise, unless DLT_C_HDLC is defined
424 * as 12, interpret it as WTAP_ENCAP_RAW_IP.
426 #if defined(DLT_LOOP) && (DLT_LOOP == 12)
427 { 12, WTAP_ENCAP_NULL },
428 #elif defined(DLT_C_HDLC) && (DLT_C_HDLC == 12)
430 * Put entry for Cisco HDLC here.
431 * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
432 * start with a 4-byte Cisco HDLC header?
435 { 12, WTAP_ENCAP_RAW_IP },
439 * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
440 * don't actually generate it. I infer that BSD/OS translates
441 * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
442 * libpcap, as the BSD/OS link-layer header is different;
443 * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
445 * From this, I infer that there's no point in handling 13
448 * 13 is DLT_ATM_RFC1483 on BSD/OS.
450 * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
451 * of decrypted IPsec traffic.
453 * We treat 13 as WTAP_ENCAP_ENC on all systems except those
454 * that define DLT_ATM_RFC1483 as 13 - presumably only
455 * BSD/OS does so - so that, on BSD/OS systems, we still
456 * treate 13 as WTAP_ENCAP_ATM_RFC1483, but, on all other
457 * systems, we can read OpenBSD DLT_ENC captures.
459 #if defined(DLT_ATM_RFC1483) && (DLT_ATM_RFC1483 == 13)
460 { 13, WTAP_ENCAP_ATM_RFC1483 },
462 { 13, WTAP_ENCAP_ENC },
466 * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
467 * don't actually generate it. I infer that BSD/OS translates
468 * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
469 * libpcap, as the BSD/OS link-layer header is different;
470 * however, in BSD/OS, DLT_PPP_BSDOS is 16.
472 * From this, I infer that there's no point in handling 14
475 * 14 is DLT_RAW on BSD/OS and OpenBSD.
477 { 14, WTAP_ENCAP_RAW_IP },
482 * DLT_SLIP_BSDOS on BSD/OS;
484 * DLT_HIPPI on NetBSD;
486 * DLT_LANE8023 with Alexey Kuznetzov's patches for
489 * DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
492 * but we don't currently handle any of those.
498 * DLT_PPP_BSDOS on BSD/OS;
500 * DLT_HDLC on NetBSD (Cisco HDLC);
502 * DLT_CIP with Alexey Kuznetzov's patches for
503 * Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
505 * DLT_I4L_IP with the ISDN4Linux patches for libpcap
508 #if defined(DLT_CIP) && (DLT_CIP == 16)
509 { 16, WTAP_ENCAP_LINUX_ATM_CLIP },
511 #if defined(DLT_HDLC) && (DLT_HDLC == 16)
512 { 16, WTAP_ENCAP_CHDLC },
516 * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
518 * It is also used as the PF (Packet Filter) logging format beginning
519 * with OpenBSD 3.0; we use 17 for PF logs unless DLT_LANE8023 is
520 * defined with the value 17.
522 #if !defined(DLT_LANE8023) || (DLT_LANE8023 != 17)
523 { 17, WTAP_ENCAP_OLD_PFLOG },
527 * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
528 * DLT_CIP of 16 that the Alexey Kuznetzov patches for
529 * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
530 * I've not found any libpcap that uses it for any other purpose -
531 * hopefully nobody will do so in the future.
533 { 18, WTAP_ENCAP_LINUX_ATM_CLIP },
536 * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
537 * recent versions I've seen of the Linux ATM distribution;
538 * I've not yet found any libpcap that uses it for any other
539 * purpose - hopefully nobody will do so in the future.
541 { 19, WTAP_ENCAP_LINUX_ATM_CLIP },
546 * If you need a new encapsulation type for libpcap files, do
547 * *N*O*T* use *ANY* of the values listed here! I.e., do *NOT*
548 * add a new encapsulation type by changing an existing entry;
549 * leave the existing entries alone.
551 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
552 * for a new DLT_ value, and specifying the purpose of the new value.
553 * When you get the new DLT_ value, use that numerical value in
554 * the "dlt_value" field of "pcap_to_wtap_map[]".
557 #define NUM_PCAP_ENCAPS (sizeof pcap_to_wtap_map / sizeof pcap_to_wtap_map[0])
560 wtap_pcap_encap_to_wtap_encap(int encap)
564 for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
565 if (pcap_to_wtap_map[i].dlt_value == encap)
566 return pcap_to_wtap_map[i].wtap_encap_value;
568 return WTAP_ENCAP_UNKNOWN;
572 wtap_wtap_encap_to_pcap_encap(int encap)
578 case WTAP_ENCAP_FDDI:
579 case WTAP_ENCAP_FDDI_BITSWAPPED:
580 case WTAP_ENCAP_NETTL_FDDI:
582 * Special-case WTAP_ENCAP_FDDI and
583 * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
584 * to DLT_FDDI (even though that may mean that the bit
585 * order in the FDDI MAC addresses is wrong; so it goes
586 * - libpcap format doesn't record the byte order,
587 * so that's not fixable).
589 return 10; /* that's DLT_FDDI */
591 case WTAP_ENCAP_FRELAY_WITH_PHDR:
593 * Do the same with Frame Relay.
597 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
599 * Map this to DLT_IEEE802_11, for now, even though
600 * that means the radio information will be lost.
601 * Once tcpdump support for the BSD radiotap header
602 * is sufficiently widespread, we should probably
603 * use that, instead - although we should probably
604 * ultimately just have WTAP_ENCAP_IEEE_802_11
605 * as the only Wiretap encapsulation for 802.11,
606 * and have the pseudo-header include a radiotap-style
607 * list of attributes. If we do that, though, we
608 * should probably bypass the regular Wiretap code
609 * when writing out packets during a capture, and just
610 * do the equivalent of a libpcap write (unfortunately,
611 * libpcap doesn't have an "open dump by file descriptor"
612 * function, so we can't just use "pcap_dump()"), so
613 * that we don't spend cycles mapping from libpcap to
614 * Wiretap and then back to libpcap. (There are other
615 * reasons to do that, e.g. to handle AIX libpcap better.)
620 for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
621 if (pcap_to_wtap_map[i].wtap_encap_value == encap)
622 return pcap_to_wtap_map[i].dlt_value;
628 * Various pseudo-headers that appear at the beginning of packet data.
630 * We represent them as sets of offsets, as they might not be aligned on
631 * an appropriate structure boundary in the buffer, and as that makes them
632 * independent of the way the compiler might align fields.
636 * The link-layer header on SunATM packets.
638 #define SUNATM_FLAGS 0 /* destination and traffic type - 1 byte */
639 #define SUNATM_VPI 1 /* VPI - 1 byte */
640 #define SUNATM_VCI 2 /* VCI - 2 bytes */
641 #define SUNATM_LEN 4 /* length of the header */
644 * The link-layer header on Nokia IPSO ATM packets.
646 #define NOKIAATM_FLAGS 0 /* destination - 1 byte */
647 #define NOKIAATM_VPI 1 /* VPI - 1 byte */
648 #define NOKIAATM_VCI 2 /* VCI - 2 bytes */
649 #define NOKIAATM_LEN 4 /* length of the header */
652 * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
655 #define IRDA_SLL_PKTTYPE_OFFSET 0 /* packet type - 2 bytes */
656 /* 12 unused bytes */
657 #define IRDA_SLL_PROTOCOL_OFFSET 14 /* protocol, should be ETH_P_LAPD - 2 bytes */
658 #define IRDA_SLL_LEN 16 /* length of the header */
661 * A header containing additional MTP information.
663 #define MTP2_SENT_OFFSET 0 /* 1 byte */
664 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
665 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
666 #define MTP2_HDR_LEN 4 /* length of the header */
669 * A header containing additional SITA WAN information.
671 #define SITA_FLAGS_OFFSET 0 /* 1 byte */
672 #define SITA_SIGNALS_OFFSET 1 /* 1 byte */
673 #define SITA_ERRORS1_OFFSET 2 /* 1 byte */
674 #define SITA_ERRORS2_OFFSET 3 /* 1 byte */
675 #define SITA_PROTO_OFFSET 4 /* 1 byte */
676 #define SITA_HDR_LEN 5 /* length of the header */
679 * The fake link-layer header of LAPD packets.
682 #define ETH_P_LAPD 0x0030
685 #define LAPD_SLL_PKTTYPE_OFFSET 0 /* packet type - 2 bytes */
686 #define LAPD_SLL_HATYPE_OFFSET 2 /* hardware address type - 2 bytes */
687 #define LAPD_SLL_HALEN_OFFSET 4 /* hardware address length - 2 bytes */
688 #define LAPD_SLL_ADDR_OFFSET 6 /* address - 8 bytes */
689 #define LAPD_SLL_PROTOCOL_OFFSET 14 /* protocol, should be ETH_P_LAPD - 2 bytes */
690 #define LAPD_SLL_LEN 16 /* length of the header */
693 * I2C link-layer on-disk format
695 struct i2c_file_hdr {
701 pcap_read_sunatm_pseudoheader(FILE_T fh,
702 union wtap_pseudo_header *pseudo_header, int *err)
704 guint8 atm_phdr[SUNATM_LEN];
709 errno = WTAP_ERR_CANT_READ;
710 bytes_read = file_read(atm_phdr, 1, SUNATM_LEN, fh);
711 if (bytes_read != SUNATM_LEN) {
712 *err = file_error(fh);
714 *err = WTAP_ERR_SHORT_READ;
718 vpi = atm_phdr[SUNATM_VPI];
719 vci = pntohs(&atm_phdr[SUNATM_VCI]);
721 switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
723 case 0x01: /* LANE */
724 pseudo_header->atm.aal = AAL_5;
725 pseudo_header->atm.type = TRAF_LANE;
728 case 0x02: /* RFC 1483 LLC multiplexed traffic */
729 pseudo_header->atm.aal = AAL_5;
730 pseudo_header->atm.type = TRAF_LLCMX;
733 case 0x05: /* ILMI */
734 pseudo_header->atm.aal = AAL_5;
735 pseudo_header->atm.type = TRAF_ILMI;
738 case 0x06: /* Q.2931 */
739 pseudo_header->atm.aal = AAL_SIGNALLING;
740 pseudo_header->atm.type = TRAF_UNKNOWN;
743 case 0x03: /* MARS (RFC 2022) */
744 pseudo_header->atm.aal = AAL_5;
745 pseudo_header->atm.type = TRAF_UNKNOWN;
748 case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
749 pseudo_header->atm.aal = AAL_5;
750 pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
755 * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
756 * case assume it's AAL_SIGNALLING; we know nothing more
759 * XXX - is this necessary? Or are we guaranteed that
760 * all signalling traffic has a type of 0x06?
762 * XXX - is this guaranteed to be AAL5? Or, if the type is
763 * 0x00 ("raw"), might it be non-AAL5 traffic?
765 if (vpi == 0 && vci == 5)
766 pseudo_header->atm.aal = AAL_SIGNALLING;
768 pseudo_header->atm.aal = AAL_5;
769 pseudo_header->atm.type = TRAF_UNKNOWN;
772 pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
774 pseudo_header->atm.vpi = vpi;
775 pseudo_header->atm.vci = vci;
776 pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
778 /* We don't have this information */
779 pseudo_header->atm.flags = 0;
780 pseudo_header->atm.cells = 0;
781 pseudo_header->atm.aal5t_u2u = 0;
782 pseudo_header->atm.aal5t_len = 0;
783 pseudo_header->atm.aal5t_chksum = 0;
789 pcap_read_nokiaatm_pseudoheader(FILE_T fh,
790 union wtap_pseudo_header *pseudo_header, int *err)
792 guint8 atm_phdr[NOKIAATM_LEN];
797 errno = WTAP_ERR_CANT_READ;
798 bytes_read = file_read(atm_phdr, 1, NOKIAATM_LEN, fh);
799 if (bytes_read != NOKIAATM_LEN) {
800 *err = file_error(fh);
802 *err = WTAP_ERR_SHORT_READ;
806 vpi = atm_phdr[NOKIAATM_VPI];
807 vci = pntohs(&atm_phdr[NOKIAATM_VCI]);
809 pseudo_header->atm.vpi = vpi;
810 pseudo_header->atm.vci = vci;
811 pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
813 /* We don't have this information */
814 pseudo_header->atm.flags = 0;
815 pseudo_header->atm.cells = 0;
816 pseudo_header->atm.aal5t_u2u = 0;
817 pseudo_header->atm.aal5t_len = 0;
818 pseudo_header->atm.aal5t_chksum = 0;
824 pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
825 int *err, gchar **err_info)
827 guint8 irda_phdr[IRDA_SLL_LEN];
830 errno = WTAP_ERR_CANT_READ;
831 bytes_read = file_read(irda_phdr, 1, IRDA_SLL_LEN, fh);
832 if (bytes_read != IRDA_SLL_LEN) {
833 *err = file_error(fh);
835 *err = WTAP_ERR_SHORT_READ;
839 if (pntohs(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
840 *err = WTAP_ERR_BAD_RECORD;
841 if (err_info != NULL)
842 *err_info = g_strdup("libpcap: IrDA capture has a packet with an invalid sll_protocol field");
846 pseudo_header->irda.pkttype = pntohs(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
852 pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
854 guint8 mtp2_hdr[MTP2_HDR_LEN];
857 errno = WTAP_ERR_CANT_READ;
858 bytes_read = file_read(mtp2_hdr, 1, MTP2_HDR_LEN, fh);
859 if (bytes_read != MTP2_HDR_LEN) {
860 *err = file_error(fh);
862 *err = WTAP_ERR_SHORT_READ;
866 pseudo_header->mtp2.sent = mtp2_hdr[MTP2_SENT_OFFSET];
867 pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
868 pseudo_header->mtp2.link_number = pntohs(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
874 pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
875 int *err, gchar **err_info)
877 guint8 lapd_phdr[LAPD_SLL_LEN];
880 errno = WTAP_ERR_CANT_READ;
881 bytes_read = file_read(lapd_phdr, 1, LAPD_SLL_LEN, fh);
882 if (bytes_read != LAPD_SLL_LEN) {
883 *err = file_error(fh);
885 *err = WTAP_ERR_SHORT_READ;
889 if (pntohs(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
890 *err = WTAP_ERR_BAD_RECORD;
891 if (err_info != NULL)
892 *err_info = g_strdup("libpcap: LAPD capture has a packet with an invalid sll_protocol field");
896 pseudo_header->lapd.pkttype = pntohs(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
897 pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
903 pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
905 guint8 sita_phdr[SITA_HDR_LEN];
908 errno = WTAP_ERR_CANT_READ;
909 bytes_read = file_read(sita_phdr, 1, SITA_HDR_LEN, fh);
910 if (bytes_read != SITA_HDR_LEN) {
911 *err = file_error(fh);
913 *err = WTAP_ERR_SHORT_READ;
917 pseudo_header->sita.flags = sita_phdr[SITA_FLAGS_OFFSET];
918 pseudo_header->sita.signals = sita_phdr[SITA_SIGNALS_OFFSET];
919 pseudo_header->sita.errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
920 pseudo_header->sita.errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
921 pseudo_header->sita.proto = sita_phdr[SITA_PROTO_OFFSET];
927 pcap_read_linux_usb_pseudoheader(FILE_T fh,
928 union wtap_pseudo_header *pseudo_header, gboolean byte_swapped, int *err)
932 errno = WTAP_ERR_CANT_READ;
933 bytes_read = file_read(&pseudo_header->linux_usb, 1,
934 sizeof (struct linux_usb_phdr), fh);
935 if (bytes_read != sizeof (struct linux_usb_phdr)) {
936 *err = file_error(fh);
938 *err = WTAP_ERR_SHORT_READ;
943 pseudo_header->linux_usb.id = GUINT64_SWAP_LE_BE(pseudo_header->linux_usb.id);
944 pseudo_header->linux_usb.bus_id = GUINT16_SWAP_LE_BE(pseudo_header->linux_usb.bus_id);
945 pseudo_header->linux_usb.ts_sec = GUINT64_SWAP_LE_BE(pseudo_header->linux_usb.ts_sec);
946 pseudo_header->linux_usb.ts_usec = GUINT32_SWAP_LE_BE(pseudo_header->linux_usb.ts_usec);
947 pseudo_header->linux_usb.status = GUINT32_SWAP_LE_BE(pseudo_header->linux_usb.status);
948 pseudo_header->linux_usb.urb_len = GUINT32_SWAP_LE_BE(pseudo_header->linux_usb.urb_len);
949 pseudo_header->linux_usb.data_len = GUINT32_SWAP_LE_BE(pseudo_header->linux_usb.data_len);
956 pcap_read_bt_pseudoheader(FILE_T fh,
957 union wtap_pseudo_header *pseudo_header, int *err)
960 struct libpcap_bt_phdr phdr;
962 errno = WTAP_ERR_CANT_READ;
963 bytes_read = file_read(&phdr, 1,
964 sizeof (struct libpcap_bt_phdr), fh);
965 if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
966 *err = file_error(fh);
968 *err = WTAP_ERR_SHORT_READ;
971 pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
976 pcap_read_ppp_pseudoheader(FILE_T fh,
977 union wtap_pseudo_header *pseudo_header, int *err)
980 struct libpcap_ppp_phdr phdr;
982 errno = WTAP_ERR_CANT_READ;
983 bytes_read = file_read(&phdr, 1,
984 sizeof (struct libpcap_ppp_phdr), fh);
985 if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
986 *err = file_error(fh);
988 *err = WTAP_ERR_SHORT_READ;
991 pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
996 pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
997 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
999 guint8 erf_hdr[sizeof(struct erf_phdr)];
1002 errno = WTAP_ERR_CANT_READ;
1003 bytes_read = file_read(erf_hdr, 1, sizeof(struct erf_phdr), fh);
1004 if (bytes_read != sizeof(struct erf_phdr)) {
1005 *err = file_error(fh);
1007 *err = WTAP_ERR_SHORT_READ;
1010 pseudo_header->erf.phdr.ts = pletohll(&erf_hdr[0]); /* timestamp */
1011 pseudo_header->erf.phdr.type = erf_hdr[8];
1012 pseudo_header->erf.phdr.flags = erf_hdr[9];
1013 pseudo_header->erf.phdr.rlen = pntohs(&erf_hdr[10]);
1014 pseudo_header->erf.phdr.lctr = pntohs(&erf_hdr[12]);
1015 pseudo_header->erf.phdr.wlen = pntohs(&erf_hdr[14]);
1017 /* The high 32 bits of the timestamp contain the integer number of seconds
1018 * while the lower 32 bits contain the binary fraction of the second.
1019 * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1021 guint64 ts = pseudo_header->erf.phdr.ts;
1022 whdr->ts.secs = (guint32) (ts >> 32);
1023 ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1024 ts += (ts & 0x80000000) << 1; /* rounding */
1025 whdr->ts.nsecs = ((guint32) (ts >> 32));
1026 if ( whdr->ts.nsecs >= 1000000000) {
1027 whdr->ts.nsecs -= 1000000000;
1035 * If the type of record given in the pseudo header indicate the presence of an extension
1036 * header then, read all the extension headers
1039 pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1040 int *err, gchar **err_info _U_, guint * psize)
1043 guint8 erf_exhdr[8];
1044 guint64 erf_exhdr_sw;
1045 int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
1046 guint8 type = pseudo_header->erf.phdr.type;
1048 if (pseudo_header->erf.phdr.type & 0x80){
1050 errno = WTAP_ERR_CANT_READ;
1051 bytes_read = file_read(erf_exhdr, 1, 8, fh);
1052 if (bytes_read != 8 ) {
1053 *err = file_error(fh);
1055 *err = WTAP_ERR_SHORT_READ;
1058 type = erf_exhdr[0];
1059 erf_exhdr_sw = pntohll((guint64*) &(erf_exhdr[0]));
1061 memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1064 } while (type & 0x80);
1070 * If the type of record given in the pseudo header indicate the precense of a subheader
1071 * then, read this optional subheader
1074 pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1075 int *err, gchar **err_info _U_, guint * psize)
1077 guint8 erf_subhdr[sizeof(union erf_subhdr)];
1081 switch(pseudo_header->erf.phdr.type & 0x7F) {
1082 case ERF_TYPE_MC_HDLC:
1083 case ERF_TYPE_MC_RAW:
1084 case ERF_TYPE_MC_ATM:
1085 case ERF_TYPE_MC_RAW_CHANNEL:
1086 case ERF_TYPE_MC_AAL5:
1087 case ERF_TYPE_MC_AAL2:
1088 case ERF_TYPE_COLOR_MC_HDLC_POS:
1089 /* Extract the Multi Channel header to include it in the pseudo header part */
1090 errno = WTAP_ERR_CANT_READ;
1091 bytes_read = file_read(erf_subhdr, 1, sizeof(erf_mc_header_t), fh);
1092 if (bytes_read != sizeof(erf_mc_header_t) ) {
1093 *err = file_error(fh);
1095 *err = WTAP_ERR_SHORT_READ;
1098 pseudo_header->erf.subhdr.mc_hdr = pntohl(&erf_subhdr[0]);
1099 *psize = sizeof(erf_mc_header_t);
1102 case ERF_TYPE_COLOR_ETH:
1103 case ERF_TYPE_DSM_COLOR_ETH:
1104 /* Extract the Ethernet additional header to include it in the pseudo header part */
1105 errno = WTAP_ERR_CANT_READ;
1106 bytes_read = file_read(erf_subhdr, 1, sizeof(erf_eth_header_t), fh);
1107 if (bytes_read != sizeof(erf_eth_header_t) ) {
1108 *err = file_error(fh);
1110 *err = WTAP_ERR_SHORT_READ;
1113 pseudo_header->erf.subhdr.eth_hdr = pntohs(&erf_subhdr[0]);
1114 *psize = sizeof(erf_eth_header_t);
1117 /* No optional pseudo header for this ERF type */
1124 pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info _U_)
1126 struct i2c_file_hdr i2c_hdr;
1129 errno = WTAP_ERR_CANT_READ;
1130 bytes_read = file_read(&i2c_hdr, 1, sizeof (i2c_hdr), fh);
1131 if (bytes_read != sizeof (i2c_hdr)) {
1132 *err = file_error(fh);
1134 *err = WTAP_ERR_SHORT_READ;
1138 pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
1139 pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
1140 pseudo_header->i2c.flags = pntohl(&i2c_hdr.flags);
1146 pcap_process_pseudo_header(FILE_T fh, int file_type, int wtap_encap, gboolean bytes_swapped, guint packet_size,
1147 gboolean check_packet_size, struct wtap_pkthdr *phdr,
1148 union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
1153 switch (wtap_encap) {
1155 case WTAP_ENCAP_ATM_PDUS:
1156 if (file_type == WTAP_FILE_PCAP_NOKIA) {
1160 if (check_packet_size && packet_size < NOKIAATM_LEN) {
1162 * Uh-oh, the packet isn't big enough to even
1163 * have a pseudo-header.
1165 *err = WTAP_ERR_BAD_RECORD;
1166 *err_info = g_strdup_printf("pcap: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1170 if (!pcap_read_nokiaatm_pseudoheader(fh,
1171 pseudo_header, err))
1172 return -1; /* Read error */
1174 phdr_len = NOKIAATM_LEN;
1179 if (check_packet_size && packet_size < SUNATM_LEN) {
1181 * Uh-oh, the packet isn't big enough to even
1182 * have a pseudo-header.
1184 *err = WTAP_ERR_BAD_RECORD;
1185 *err_info = g_strdup_printf("pcap: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
1189 if (!pcap_read_sunatm_pseudoheader(fh,
1190 pseudo_header, err))
1191 return -1; /* Read error */
1193 phdr_len = SUNATM_LEN;
1197 case WTAP_ENCAP_ETHERNET:
1199 * We don't know whether there's an FCS in this frame or not.
1201 pseudo_header->eth.fcs_len = -1;
1204 case WTAP_ENCAP_IEEE_802_11:
1205 case WTAP_ENCAP_PRISM_HEADER:
1206 case WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP:
1207 case WTAP_ENCAP_IEEE_802_11_WLAN_AVS:
1209 * We don't know whether there's an FCS in this frame or not.
1210 * XXX - are there any OSes where the capture mechanism
1213 pseudo_header->ieee_802_11.fcs_len = -1;
1214 pseudo_header->ieee_802_11.channel = 0;
1215 pseudo_header->ieee_802_11.data_rate = 0;
1216 pseudo_header->ieee_802_11.signal_level = 0;
1219 case WTAP_ENCAP_IRDA:
1220 if (check_packet_size && packet_size < IRDA_SLL_LEN) {
1222 * Uh-oh, the packet isn't big enough to even
1223 * have a pseudo-header.
1225 *err = WTAP_ERR_BAD_RECORD;
1226 *err_info = g_strdup_printf("pcap: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1230 if (!pcap_read_irda_pseudoheader(fh, pseudo_header,
1232 return -1; /* Read error */
1234 phdr_len = IRDA_SLL_LEN;
1237 case WTAP_ENCAP_MTP2_WITH_PHDR:
1238 if (check_packet_size && packet_size < MTP2_HDR_LEN) {
1240 * Uh-oh, the packet isn't big enough to even
1241 * have a pseudo-header.
1243 *err = WTAP_ERR_BAD_RECORD;
1244 *err_info = g_strdup_printf("pcap: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1248 if (!pcap_read_mtp2_pseudoheader(fh, pseudo_header,
1250 return -1; /* Read error */
1252 phdr_len = MTP2_HDR_LEN;
1255 case WTAP_ENCAP_LINUX_LAPD:
1256 if (check_packet_size && packet_size < LAPD_SLL_LEN) {
1258 * Uh-oh, the packet isn't big enough to even
1259 * have a pseudo-header.
1261 *err = WTAP_ERR_BAD_RECORD;
1262 *err_info = g_strdup_printf("pcap: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1266 if (!pcap_read_lapd_pseudoheader(fh, pseudo_header,
1268 return -1; /* Read error */
1270 phdr_len = LAPD_SLL_LEN;
1273 case WTAP_ENCAP_SITA:
1274 if (check_packet_size && packet_size < SITA_HDR_LEN) {
1276 * Uh-oh, the packet isn't big enough to even
1277 * have a pseudo-header.
1279 *err = WTAP_ERR_BAD_RECORD;
1280 *err_info = g_strdup_printf("pcap: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1284 if (!pcap_read_sita_pseudoheader(fh, pseudo_header,
1286 return -1; /* Read error */
1288 phdr_len = SITA_HDR_LEN;
1291 case WTAP_ENCAP_USB_LINUX:
1292 case WTAP_ENCAP_USB_LINUX_MMAPPED:
1293 if (check_packet_size &&
1294 packet_size < sizeof (struct linux_usb_phdr)) {
1296 * Uh-oh, the packet isn't big enough to even
1297 * have a pseudo-header.
1299 *err = WTAP_ERR_BAD_RECORD;
1300 *err_info = g_strdup_printf("pcap: Linux USB file has a %u-byte packet, too small to have even a Linux USB pseudo-header",
1304 if (!pcap_read_linux_usb_pseudoheader(fh,
1305 pseudo_header, bytes_swapped, err))
1306 return -1; /* Read error */
1308 phdr_len = (int)sizeof (struct linux_usb_phdr);
1311 case WTAP_ENCAP_BLUETOOTH_H4:
1312 /* We don't have pseudoheader, so just pretend we received everything. */
1313 pseudo_header->p2p.sent = FALSE;
1316 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1317 if (check_packet_size &&
1318 packet_size < sizeof (struct libpcap_bt_phdr)) {
1320 * Uh-oh, the packet isn't big enough to even
1321 * have a pseudo-header.
1323 *err = WTAP_ERR_BAD_RECORD;
1324 *err_info = g_strdup_printf("pcap: lipcap bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1328 if (!pcap_read_bt_pseudoheader(fh,
1329 pseudo_header, err))
1330 return -1; /* Read error */
1332 phdr_len = (int)sizeof (struct libpcap_bt_phdr);
1335 case WTAP_ENCAP_PPP_WITH_PHDR:
1336 if (check_packet_size &&
1337 packet_size < sizeof (struct libpcap_ppp_phdr)) {
1339 * Uh-oh, the packet isn't big enough to even
1340 * have a pseudo-header.
1342 *err = WTAP_ERR_BAD_RECORD;
1343 *err_info = g_strdup_printf("pcap: lipcap ppp file has a %u-byte packet, too small to have even a pseudo-header",
1347 if (!pcap_read_ppp_pseudoheader(fh,
1348 pseudo_header, err))
1349 return -1; /* Read error */
1351 phdr_len = (int)sizeof (struct libpcap_ppp_phdr);
1354 case WTAP_ENCAP_ERF:
1355 if (check_packet_size &&
1356 packet_size < sizeof(struct erf_phdr) ) {
1358 * Uh-oh, the packet isn't big enough to even
1359 * have a pseudo-header.
1361 *err = WTAP_ERR_BAD_RECORD;
1362 *err_info = g_strdup_printf("pcap: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1367 if (!pcap_read_erf_pseudoheader(fh, phdr, pseudo_header,
1369 return -1; /* Read error */
1371 phdr_len = (int)sizeof(struct erf_phdr);
1373 /* check the optional Extension header */
1374 if (!pcap_read_erf_exheader(fh, pseudo_header, err, err_info,
1376 return -1; /* Read error */
1380 /* check the optional Multi Channel header */
1381 if (!pcap_read_erf_subheader(fh, pseudo_header, err, err_info,
1383 return -1; /* Read error */
1388 case WTAP_ENCAP_I2C:
1389 if (check_packet_size &&
1390 packet_size < sizeof (struct i2c_file_hdr)) {
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: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1400 if (!pcap_read_i2c_pseudoheader(fh, pseudo_header,
1402 return -1; /* Read error */
1405 * Don't count the pseudo-header as part of the packet.
1407 phdr_len = (int)sizeof (struct i2c_file_hdr);
1415 pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
1421 case WTAP_ENCAP_ATM_PDUS:
1422 hdrsize = SUNATM_LEN;
1425 case WTAP_ENCAP_IRDA:
1426 hdrsize = IRDA_SLL_LEN;
1429 case WTAP_ENCAP_MTP2_WITH_PHDR:
1430 hdrsize = MTP2_HDR_LEN;
1433 case WTAP_ENCAP_LINUX_LAPD:
1434 hdrsize = LAPD_SLL_LEN;
1437 case WTAP_ENCAP_SITA:
1438 hdrsize = SITA_HDR_LEN;
1441 case WTAP_ENCAP_USB_LINUX:
1442 case WTAP_ENCAP_USB_LINUX_MMAPPED:
1443 hdrsize = (int)sizeof (struct linux_usb_phdr);
1446 case WTAP_ENCAP_ERF:
1447 hdrsize = (int)sizeof (struct erf_phdr);
1448 if (pseudo_header->erf.phdr.type & 0x80)
1450 switch (pseudo_header->erf.phdr.type & 0x7F) {
1452 case ERF_TYPE_MC_HDLC:
1453 case ERF_TYPE_MC_RAW:
1454 case ERF_TYPE_MC_ATM:
1455 case ERF_TYPE_MC_RAW_CHANNEL:
1456 case ERF_TYPE_MC_AAL5:
1457 case ERF_TYPE_MC_AAL2:
1458 case ERF_TYPE_COLOR_MC_HDLC_POS:
1459 hdrsize += (int)sizeof(struct erf_mc_hdr);
1463 case ERF_TYPE_COLOR_ETH:
1464 case ERF_TYPE_DSM_COLOR_ETH:
1465 hdrsize += (int)sizeof(struct erf_eth_hdr);
1473 case WTAP_ENCAP_I2C:
1474 hdrsize = (int)sizeof (struct i2c_file_hdr);
1477 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1478 hdrsize = (int)sizeof (struct libpcap_bt_phdr);
1481 case WTAP_ENCAP_PPP_WITH_PHDR:
1482 hdrsize = (int)sizeof (struct libpcap_ppp_phdr);
1494 pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
1497 guint8 atm_hdr[SUNATM_LEN];
1498 guint8 irda_hdr[IRDA_SLL_LEN];
1499 guint8 lapd_hdr[LAPD_SLL_LEN];
1500 guint8 mtp2_hdr[MTP2_HDR_LEN];
1501 guint8 sita_hdr[SITA_HDR_LEN];
1502 guint8 erf_hdr[ sizeof(struct erf_mc_phdr)];
1503 struct i2c_file_hdr i2c_hdr;
1504 struct libpcap_bt_phdr bt_hdr;
1505 struct libpcap_ppp_phdr ppp_hdr;
1511 case WTAP_ENCAP_ATM_PDUS:
1513 * Write the ATM header.
1515 atm_hdr[SUNATM_FLAGS] =
1516 (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
1517 switch (pseudo_header->atm.aal) {
1519 case AAL_SIGNALLING:
1521 atm_hdr[SUNATM_FLAGS] |= 0x06;
1525 switch (pseudo_header->atm.type) {
1529 atm_hdr[SUNATM_FLAGS] |= 0x01;
1533 /* RFC 1483 LLC multiplexed traffic */
1534 atm_hdr[SUNATM_FLAGS] |= 0x02;
1539 atm_hdr[SUNATM_FLAGS] |= 0x05;
1544 atm_hdr[SUNATM_VPI] = (guint8)pseudo_header->atm.vpi;
1545 phtons(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1546 nwritten = wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr));
1547 if (nwritten != sizeof(atm_hdr)) {
1548 if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1549 *err = wtap_dump_file_ferror(wdh);
1551 *err = WTAP_ERR_SHORT_WRITE;
1554 wdh->bytes_dumped += sizeof(atm_hdr);
1557 case WTAP_ENCAP_IRDA:
1559 * Write the IrDA header.
1561 memset(irda_hdr, 0, sizeof(irda_hdr));
1562 phtons(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET],
1563 pseudo_header->irda.pkttype);
1564 phtons(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
1565 nwritten = wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr));
1566 if (nwritten != sizeof(irda_hdr)) {
1567 if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1568 *err = wtap_dump_file_ferror(wdh);
1570 *err = WTAP_ERR_SHORT_WRITE;
1573 wdh->bytes_dumped += sizeof(irda_hdr);
1576 case WTAP_ENCAP_MTP2_WITH_PHDR:
1578 * Write the MTP2 header.
1580 memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
1581 mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
1582 mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
1583 phtons(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
1584 pseudo_header->mtp2.link_number);
1585 nwritten = wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr));
1586 if (nwritten != sizeof(mtp2_hdr)) {
1587 if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1588 *err = wtap_dump_file_ferror(wdh);
1590 *err = WTAP_ERR_SHORT_WRITE;
1593 wdh->bytes_dumped += sizeof(mtp2_hdr);
1596 case WTAP_ENCAP_LINUX_LAPD:
1598 * Write the LAPD header.
1600 memset(&lapd_hdr, 0, sizeof(lapd_hdr));
1601 phtons(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET],
1602 pseudo_header->lapd.pkttype);
1603 phtons(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
1604 lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
1605 pseudo_header->lapd.we_network?0x01:0x00;
1606 nwritten = fwrite(&lapd_hdr, 1, sizeof(lapd_hdr), wdh->fh);
1607 if (nwritten != sizeof(lapd_hdr)) {
1608 if (nwritten == 0 && ferror(wdh->fh))
1611 *err = WTAP_ERR_SHORT_WRITE;
1614 wdh->bytes_dumped += sizeof(lapd_hdr);
1617 case WTAP_ENCAP_SITA:
1619 * Write the SITA header.
1621 memset(&sita_hdr, 0, sizeof(sita_hdr));
1622 sita_hdr[SITA_FLAGS_OFFSET] = pseudo_header->sita.flags;
1623 sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.signals;
1624 sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.errors1;
1625 sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.errors2;
1626 sita_hdr[SITA_PROTO_OFFSET] = pseudo_header->sita.proto;
1627 nwritten = fwrite(&sita_hdr, 1, sizeof(sita_hdr), wdh->fh);
1628 if (nwritten != sizeof(sita_hdr)) {
1629 if (nwritten == 0 && ferror(wdh->fh))
1632 *err = WTAP_ERR_SHORT_WRITE;
1635 wdh->bytes_dumped += sizeof(sita_hdr);
1638 case WTAP_ENCAP_USB_LINUX:
1639 case WTAP_ENCAP_USB_LINUX_MMAPPED:
1641 * Write out the pseudo-header; it has the same format
1642 * as the Linux USB header, and that header is supposed
1643 * to be written in the host byte order of the machine
1646 nwritten = fwrite(&pseudo_header->linux_usb, 1,
1647 sizeof(pseudo_header->linux_usb), wdh->fh);
1648 if (nwritten != sizeof(pseudo_header->linux_usb)) {
1649 if (nwritten == 0 && ferror(wdh->fh))
1652 *err = WTAP_ERR_SHORT_WRITE;
1655 wdh->bytes_dumped += sizeof(lapd_hdr);
1658 case WTAP_ENCAP_ERF:
1660 * Write the ERF header.
1662 memset(&erf_hdr, 0, sizeof(erf_hdr));
1663 pletonll(&erf_hdr[0], pseudo_header->erf.phdr.ts);
1664 erf_hdr[8] = pseudo_header->erf.phdr.type;
1665 erf_hdr[9] = pseudo_header->erf.phdr.flags;
1666 phtons(&erf_hdr[10], pseudo_header->erf.phdr.rlen);
1667 phtons(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
1668 phtons(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
1669 size = sizeof(struct erf_phdr);
1671 switch(pseudo_header->erf.phdr.type & 0x7F) {
1672 case ERF_TYPE_MC_HDLC:
1673 case ERF_TYPE_MC_RAW:
1674 case ERF_TYPE_MC_ATM:
1675 case ERF_TYPE_MC_RAW_CHANNEL:
1676 case ERF_TYPE_MC_AAL5:
1677 case ERF_TYPE_MC_AAL2:
1678 case ERF_TYPE_COLOR_MC_HDLC_POS:
1679 phtonl(&erf_hdr[16], pseudo_header->erf.subhdr.mc_hdr);
1680 size += (int)sizeof(struct erf_mc_hdr);
1683 case ERF_TYPE_COLOR_ETH:
1684 case ERF_TYPE_DSM_COLOR_ETH:
1685 phtons(&erf_hdr[16], pseudo_header->erf.subhdr.eth_hdr);
1686 size += (int)sizeof(struct erf_eth_hdr);
1691 nwritten = wtap_dump_file_write(wdh, erf_hdr, size);
1692 if (nwritten != (guint) size) {
1693 if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1694 *err = wtap_dump_file_ferror(wdh);
1696 *err = WTAP_ERR_SHORT_WRITE;
1699 wdh->bytes_dumped += size;
1702 case WTAP_ENCAP_I2C:
1704 * Write the I2C header.
1706 memset(&i2c_hdr, 0, sizeof(i2c_hdr));
1707 i2c_hdr.bus = pseudo_header->i2c.bus |
1708 (pseudo_header->i2c.is_event ? 0x80 : 0x00);
1709 phtonl((guint8 *)&i2c_hdr.flags, pseudo_header->i2c.flags);
1710 nwritten = fwrite(&i2c_hdr, 1, sizeof(i2c_hdr), wdh->fh);
1711 if (nwritten != sizeof(i2c_hdr)) {
1712 if (nwritten == 0 && ferror(wdh->fh))
1715 *err = WTAP_ERR_SHORT_WRITE;
1718 wdh->bytes_dumped += sizeof(i2c_hdr);
1721 case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
1722 bt_hdr.direction = GUINT32_TO_BE(pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV);
1723 nwritten = wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr);
1724 if (nwritten != sizeof bt_hdr) {
1725 if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1726 *err = wtap_dump_file_ferror(wdh);
1728 *err = WTAP_ERR_SHORT_WRITE;
1731 wdh->bytes_dumped += sizeof bt_hdr;
1734 case WTAP_ENCAP_PPP_WITH_PHDR:
1735 ppp_hdr.direction = (pseudo_header->p2p.sent ? LIBPCAP_PPP_PHDR_SENT : LIBPCAP_PPP_PHDR_RECV);
1736 nwritten = wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr);
1737 if (nwritten != sizeof ppp_hdr) {
1738 if (nwritten == 0 && wtap_dump_file_ferror(wdh))
1739 *err = wtap_dump_file_ferror(wdh);
1741 *err = WTAP_ERR_SHORT_WRITE;
1744 wdh->bytes_dumped += sizeof ppp_hdr;