Coverage Report

Created: 2025-12-27 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/wiretap/pcap-common.c
Line
Count
Source
1
/* pcap-common.c
2
 * Code common to pcap and pcapng file formats
3
 *
4
 * Wiretap Library
5
 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
6
 *
7
 * File format support for pcapng file format
8
 * Copyright (c) 2007 by Ulf Lamping <ulf.lamping@web.de>
9
 *
10
 * SPDX-License-Identifier: GPL-2.0-or-later
11
 */
12
13
#include "config.h"
14
#include "pcap-common.h"
15
16
#include <stdlib.h>
17
#include <string.h>
18
#include "wtap_module.h"
19
#include "file_wrappers.h"
20
#include "atm.h"
21
#include "erf_record.h"
22
#include "pcap-encap.h"
23
24
#include <wsutil/array.h>
25
#include <wsutil/ws_roundup.h>
26
#include <wsutil/pint.h>
27
28
/*
29
 * On some systems, the FDDI MAC addresses are bit-swapped.
30
 *
31
 * XXX - what we *really* need to know is whether the addresses are
32
 * bit-swapped *in a particular capture*, which depends on the system
33
 * on which it was captured, not on the system that's reading it.
34
 * Unfortunately, we can't determine that.
35
 */
36
#if !defined(ultrix) && !defined(__alpha) && !defined(__bsdi__)
37
#define BIT_SWAPPED_MAC_ADDRS
38
#endif
39
40
/*
41
 * Map link-layer header types (LINKTYPE_ values) to Wiretap encapsulations.
42
 *
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.
48
 *
49
 * The Tcpdump Group now maintains the list of link-layer header types;
50
 * they introduced a separate namespace of LINKTYPE_ values for the
51
 * values to be used in capture files, and have libpcap map between
52
 * those values in capture file headers and the DLT_ values that the
53
 * pcap_datalink() and pcap_open_dead() APIs use.  See
54
 * https://www.tcpdump.org/linktypes.html for a list of LINKTYPE_ values.
55
 *
56
 * In most cases, the corresponding LINKTYPE_ and DLT_ values are the
57
 * same.  In the cases where the same link-layer header type was given
58
 * different values in different OSes, a new LINKTYPE_ value was defined,
59
 * different from all of the existing DLT_ values.
60
 *
61
 * This table maps LINKTYPE_ values to the corresponding Wiretap
62
 * encapsulation.  For cases where multiple DLT_ values were in use,
63
 * it also checks what <pcap.h> defineds to determine how to interpret
64
 * them, so that if a file was written by a version of libpcap prior
65
 * to the introduction of the LINKTYPE_ values, and has a DLT_ value
66
 * from the OS on which it was written rather than a LINKTYPE_ value
67
 * as its linktype value in the file header, we map the numerical
68
 * DLT_ value, as interpreted by the libpcap with which we're building
69
 * Wireshark/Wiretap interprets them (which, if it doesn't support
70
 * them at all, means we don't support them either - any capture files
71
 * using them are foreign, and we don't hazard a guess as to which
72
 * platform they came from; we could, I guess, choose the most likely
73
 * platform), to the corresponding Wiretap encapsulation.
74
 *
75
 * Note: if you need a new encapsulation type for libpcap files, do
76
 * *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
77
 * add a new encapsulation type by changing an existing entry;
78
 * leave the existing entries alone.
79
 *
80
 * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking for
81
 * a new LINKTYPE_/DLT_ value, and specifying the purpose of the new
82
 * value.  When you get the new LINKTYPE_/DLT_ value, use that numerical
83
 * value in the "linktype_value" field of "pcap_to_wtap_map[]".
84
 */
85
86
static const struct {
87
  int linktype_value;
88
  int wtap_encap_value;
89
} pcap_to_wtap_map[] = {
90
  /*
91
   * These are the values that are almost certainly the same
92
   * in all libpcaps (I've yet to find one where the values
93
   * in question are used for some purpose other than the
94
   * one below, but...), and thus assigned as LINKTYPE_ values,
95
   * and that Wiretap and Wireshark currently support.
96
   */
97
  { 0,    WTAP_ENCAP_NULL },  /* null encapsulation */
98
  { 1,    WTAP_ENCAP_ETHERNET },
99
  { 2,    WTAP_ENCAP_3MB_ETHERNET },
100
  { 3,    WTAP_ENCAP_AX25 },
101
  { 6,    WTAP_ENCAP_TOKEN_RING },  /* IEEE 802 Networks - assume token ring */
102
  { 7,    WTAP_ENCAP_ARCNET },
103
  { 8,    WTAP_ENCAP_SLIP },
104
  { 9,    WTAP_ENCAP_PPP },
105
#ifdef BIT_SWAPPED_MAC_ADDRS
106
  { 10,   WTAP_ENCAP_FDDI_BITSWAPPED },
107
#else
108
  { 10,   WTAP_ENCAP_FDDI },
109
#endif
110
111
  { 32,   WTAP_ENCAP_REDBACK },
112
113
  /*
114
   * 50 is DLT_PPP_SERIAL in NetBSD; it appears that DLT_PPP
115
   * on BSD (at least according to standard tcpdump) has, as
116
   * the first octet, an indication of whether the packet was
117
   * transmitted or received (rather than having the standard
118
   * PPP address value of 0xff), but that DLT_PPP_SERIAL puts
119
   * a real live PPP header there, or perhaps a Cisco PPP header
120
   * as per section 4.3.1 of RFC 1547 (implementations of this
121
   * exist in various BSDs in "sys/net/if_spppsubr.c", and
122
   * I think also exist either in standard Linux or in
123
   * various Linux patches; the implementations show how to handle
124
   * Cisco keepalive packets).
125
   *
126
   * However, I don't see any obvious place in FreeBSD "if_ppp.c"
127
   * where anything other than the standard PPP header would be
128
   * passed up.  I see some stuff that sets the first octet
129
   * to 0 for incoming and 1 for outgoing packets before applying
130
   * a BPF filter to see whether to drop packets whose protocol
131
   * field has the 0x8000 bit set, i.e. network control protocols -
132
   * those are handed up to userland - but that code puts the
133
   * address field back before passing the packet up.
134
   *
135
   * I also don't see anything immediately obvious that munges
136
   * the address field for sync PPP, either.
137
   *
138
   * Wireshark currently assumes that if the first octet of a
139
   * PPP frame is 0xFF, it's the address field and is followed
140
   * by a control field and a 2-byte protocol, otherwise the
141
   * address and control fields are absent and the frame begins
142
   * with a protocol field.  If we ever see a BSD/OS PPP
143
   * capture, we'll have to handle it differently, and we may
144
   * have to handle standard BSD captures differently if, in fact,
145
   * they don't have 0xff 0x03 as the first two bytes - but, as per
146
   * the two paragraphs preceding this, it's not clear that
147
   * the address field *is* munged into an incoming/outgoing
148
   * field when the packet is handed to the BPF device.
149
   *
150
   * For now, we just map DLT_PPP_SERIAL to WTAP_ENCAP_PPP, as
151
   * we treat WTAP_ENCAP_PPP packets as if those beginning with
152
   * 0xff have the standard RFC 1662 "PPP in HDLC-like Framing"
153
   * 0xff 0x03 address/control header, and DLT_PPP_SERIAL frames
154
   * appear to contain that unless they're Cisco frames (if we
155
   * ever see a capture with them, we'd need to implement the
156
   * RFC 1547 stuff, and the keepalive protocol stuff).
157
   *
158
   * We may have to distinguish between "PPP where if it doesn't
159
   * begin with 0xff there's no HDLC encapsulation and the frame
160
   * begins with the protocol field" (which is how we handle
161
   * WTAP_ENCAP_PPP now) and "PPP where there's either HDLC
162
   * encapsulation or Cisco PPP" (which is what DLT_PPP_SERIAL
163
   * is) at some point.
164
   *
165
   * XXX - NetBSD has DLT_HDLC, which appears to be used for
166
   * Cisco HDLC.  Ideally, they should use DLT_PPP_SERIAL
167
   * only for real live HDLC-encapsulated PPP, not for Cisco
168
   * HDLC.
169
   */
170
  { 50,   WTAP_ENCAP_PPP },
171
172
  /*
173
   * Used by NetBSD and OpenBSD pppoe(4).
174
   */
175
  { 51,   WTAP_ENCAP_PPP_ETHER },
176
177
  /*
178
   * Apparently used by the Axent Raptor firewall (now Symantec
179
   * Enterprise Firewall).
180
   * Thanks, Axent, for not reserving that type with tcpdump.org
181
   * and not telling anybody about it.
182
   */
183
  { 99,   WTAP_ENCAP_SYMANTEC },
184
185
  /*
186
   * These are the values that libpcap 0.5 and later use in
187
   * capture file headers, in an attempt to work around the
188
   * confusion decried above, and that Wiretap and Wireshark
189
   * currently support.  I.e., they're the LINKTYPE_ values
190
   * for RFC 1483 ATM and "raw IP", respectively, not the
191
   * DLT_ values for them on all platforms.
192
   */
193
  { 100,    WTAP_ENCAP_ATM_RFC1483 },
194
  { 101,    WTAP_ENCAP_RAW_IP },
195
#if 0
196
  /*
197
   * More values used by libpcap 0.5 as DLT_ values and used by the
198
   * current CVS version of libpcap in capture file headers.
199
   * They are not yet handled in Wireshark.
200
   * If we get a capture that contains them, we'll implement them.
201
   */
202
  { 102,    WTAP_ENCAP_SLIP_BSDOS },
203
  { 103,    WTAP_ENCAP_PPP_BSDOS },
204
#endif
205
206
  /*
207
   * These ones are handled in Wireshark, though.
208
   */
209
  { 104,    WTAP_ENCAP_CHDLC }, /* Cisco HDLC */
210
  { 105,    WTAP_ENCAP_IEEE_802_11 }, /* IEEE 802.11 */
211
  { 106,    WTAP_ENCAP_LINUX_ATM_CLIP },
212
  { 107,    WTAP_ENCAP_FRELAY },  /* Frame Relay */
213
  { 108,    WTAP_ENCAP_LOOP },  /* OpenBSD loopback */
214
  { 109,    WTAP_ENCAP_ENC }, /* OpenBSD IPSEC enc */
215
#if 0
216
  { 110,    WTAP_ENCAP_LANE_802_3 },/* ATM LANE 802.3 */
217
  { 111,    WTAP_ENCAP_HIPPI }, /* NetBSD HIPPI */
218
#endif
219
  { 112,    WTAP_ENCAP_CHDLC }, /* NetBSD HDLC framing */
220
221
  /*
222
   * Linux "cooked mode" captures, used by the current CVS version
223
   * of libpcap
224
   * OR
225
   * it could be a packet in Cisco's ERSPAN encapsulation which uses
226
   * this number as well (why can't people stick to protocols when it
227
   * comes to allocating/using DLT types).
228
   */
229
  { 113,    WTAP_ENCAP_SLL }, /* Linux cooked capture v1 */
230
231
  { 114,    WTAP_ENCAP_LOCALTALK }, /* Localtalk */
232
233
  /*
234
   * The tcpdump.org version of libpcap uses 117, rather than 17,
235
   * for OpenBSD packet filter logging, so as to avoid conflicting
236
   * with DLT_LANE8023 in SuSE 6.3 libpcap.
237
   */
238
  { 117,    WTAP_ENCAP_PFLOG },
239
240
  { 118,    WTAP_ENCAP_CISCO_IOS },
241
  { 119,    WTAP_ENCAP_IEEE_802_11_PRISM }, /* 802.11 plus Prism monitor mode radio header */
242
  { 121,    WTAP_ENCAP_HHDLC }, /* HiPath HDLC */
243
  { 122,    WTAP_ENCAP_IP_OVER_FC },   /* RFC 2625 IP-over-FC */
244
  { 123,    WTAP_ENCAP_ATM_PDUS },  /* SunATM */
245
  { 127,    WTAP_ENCAP_IEEE_802_11_RADIOTAP },  /* 802.11 plus radiotap radio header */
246
  { 128,    WTAP_ENCAP_TZSP },  /* Tazmen Sniffer Protocol */
247
  { 129,    WTAP_ENCAP_ARCNET_LINUX },
248
  { 130,    WTAP_ENCAP_JUNIPER_MLPPP }, /* Juniper MLPPP on ML-, LS-, AS- PICs */
249
  { 131,    WTAP_ENCAP_JUNIPER_MLFR }, /* Juniper MLFR (FRF.15) on ML-, LS-, AS- PICs */
250
  { 133,    WTAP_ENCAP_JUNIPER_GGSN},
251
  /*
252
   * Values 132 and 134 not listed here are reserved for use
253
   * in Juniper hardware.
254
   */
255
  { 135,    WTAP_ENCAP_JUNIPER_ATM2 }, /* various encapsulations captured on the ATM2 PIC */
256
  { 136,    WTAP_ENCAP_JUNIPER_SVCS }, /* various encapsulations captured on the services PIC */
257
  { 137,    WTAP_ENCAP_JUNIPER_ATM1 }, /* various encapsulations captured on the ATM1 PIC */
258
259
  { 138,    WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 },
260
            /* Apple IP-over-IEEE 1394 */
261
262
  { 139,    WTAP_ENCAP_MTP2_WITH_PHDR },
263
  { 140,    WTAP_ENCAP_MTP2 },
264
  { 141,    WTAP_ENCAP_MTP3 },
265
  { 142,    WTAP_ENCAP_SCCP },
266
  { 143,    WTAP_ENCAP_DOCSIS },
267
  { 144,    WTAP_ENCAP_IRDA },  /* IrDA capture */
268
269
  /* Reserved for private use. */
270
  { 147,    WTAP_ENCAP_USER0 },
271
  { 148,    WTAP_ENCAP_USER1 },
272
  { 149,    WTAP_ENCAP_USER2 },
273
  { 150,    WTAP_ENCAP_USER3 },
274
  { 151,    WTAP_ENCAP_USER4 },
275
  { 152,    WTAP_ENCAP_USER5 },
276
  { 153,    WTAP_ENCAP_USER6 },
277
  { 154,    WTAP_ENCAP_USER7 },
278
  { 155,    WTAP_ENCAP_USER8 },
279
  { 156,    WTAP_ENCAP_USER9 },
280
  { 157,    WTAP_ENCAP_USER10 },
281
  { 158,    WTAP_ENCAP_USER11 },
282
  { 159,    WTAP_ENCAP_USER12 },
283
  { 160,    WTAP_ENCAP_USER13 },
284
  { 161,    WTAP_ENCAP_USER14 },
285
  { 162,    WTAP_ENCAP_USER15 },
286
287
  { 163,    WTAP_ENCAP_IEEE_802_11_AVS },  /* 802.11 plus AVS radio header */
288
289
  /*
290
   * 164 is reserved for Juniper-private chassis-internal
291
   * meta-information such as QoS profiles, etc..
292
   */
293
294
  { 165,    WTAP_ENCAP_BACNET_MS_TP },
295
296
  /*
297
   * 166 is reserved for a PPP variant in which the first byte
298
   * of the 0xff03 header, the 0xff, is replaced by a direction
299
   * byte.  I don't know whether any captures look like that,
300
   * but it is used for some Linux IP filtering (ipfilter?).
301
   */
302
303
  /* Ethernet PPPoE frames captured on a service PIC */
304
  { 167,    WTAP_ENCAP_JUNIPER_PPPOE },
305
306
  /*
307
   * 168 is reserved for more Juniper private-chassis-
308
   * internal meta-information.
309
   */
310
311
  { 169,    WTAP_ENCAP_GPRS_LLC },
312
313
  /* ITU-T G.7041/Y.1303 Generic Framing Procedure. */
314
  { 170,    WTAP_ENCAP_GFP_T },
315
  { 171,    WTAP_ENCAP_GFP_F },
316
317
  /* Registered by Gcom, Inc. */
318
  { 172,    WTAP_ENCAP_GCOM_TIE1 },
319
  { 173,    WTAP_ENCAP_GCOM_SERIAL },
320
321
  { 177,    WTAP_ENCAP_LINUX_LAPD },
322
323
  /* Ethernet frames prepended with meta-information */
324
  { 178,    WTAP_ENCAP_JUNIPER_ETHER },
325
  /* PPP frames prepended with meta-information */
326
  { 179,    WTAP_ENCAP_JUNIPER_PPP },
327
  /* Frame-Relay frames prepended with meta-information */
328
  { 180,    WTAP_ENCAP_JUNIPER_FRELAY },
329
  /* C-HDLC frames prepended with meta-information */
330
  { 181,    WTAP_ENCAP_JUNIPER_CHDLC },
331
  /* VOIP Frames prepended with meta-information */
332
  { 183,    WTAP_ENCAP_JUNIPER_VP },
333
  /* Virtual Network Frames prepended with meta-information */
334
  { 184,    WTAP_ENCAP_JUNIPER_VN },
335
  /* USB packets from FreeBSD's USB BPF tap */
336
  { 186,    WTAP_ENCAP_USB_FREEBSD },
337
  /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
338
  { 187,    WTAP_ENCAP_BLUETOOTH_H4 },
339
  /* IEEE 802.16 MAC Common Part Sublayer */
340
  { 188,    WTAP_ENCAP_IEEE802_16_MAC_CPS },
341
  /* USB packets with Linux-specified header */
342
  { 189,    WTAP_ENCAP_USB_LINUX },
343
  /* CAN 2.0b frame */
344
  { 190,    WTAP_ENCAP_CAN20B },
345
  /* Per-Packet Information header */
346
  { 192,    WTAP_ENCAP_PPI },
347
  /* IEEE 802.15.4 Wireless PAN */
348
  { 195,    WTAP_ENCAP_IEEE802_15_4 },
349
  /* SITA File Encapsulation */
350
  { 196,    WTAP_ENCAP_SITA },
351
  /* Endace Record File Encapsulation */
352
  { 197,    WTAP_ENCAP_ERF },
353
  /* IPMB/I2C with Kontron pseudo-header */
354
  { 199,    WTAP_ENCAP_IPMB_KONTRON },
355
  /* Juniper-private data link type, used for capturing data on a secure tunnel interface. */
356
  { 200,    WTAP_ENCAP_JUNIPER_ST },
357
  /* Bluetooth HCI UART transport (part H:4) frames, like hcidump */
358
  { 201,    WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR },
359
  /* AX.25 packet with a 1-byte KISS header */
360
  { 202,    WTAP_ENCAP_AX25_KISS },
361
  /* LAPD frame */
362
  { 203,    WTAP_ENCAP_LAPD },
363
  /* PPP with pseudoheader */
364
  { 204,    WTAP_ENCAP_PPP_WITH_PHDR },
365
  /* I2C with a Linux-specific header (defined by Pigeon Point Systems) */
366
  { 209,    WTAP_ENCAP_I2C_LINUX },
367
  /* FlexRay frame */
368
  { 210,    WTAP_ENCAP_FLEXRAY },
369
  /* MOST frame */
370
  { 211,    WTAP_ENCAP_MOST },
371
  /* LIN frame */
372
  { 212,    WTAP_ENCAP_LIN },
373
  /* X2E Xoraya serial frame */
374
  { 213,    WTAP_ENCAP_X2E_SERIAL },
375
  /* X2E Xoraya frame */
376
  { 214,    WTAP_ENCAP_X2E_XORAYA },
377
  /* IEEE 802.15.4 Wireless PAN non-ASK PHY */
378
  { 215,    WTAP_ENCAP_IEEE802_15_4_NONASK_PHY },
379
  /* USB packets with padded Linux-specified header */
380
  { 220,    WTAP_ENCAP_USB_LINUX_MMAPPED },
381
  /* Fibre Channel FC-2 frame */
382
  { 224,    WTAP_ENCAP_FIBRE_CHANNEL_FC2 },
383
  /* Fibre Channel FC-2 frame with Delimiter */
384
  { 225,    WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS },
385
  /* Solaris IPNET */
386
  { 226,    WTAP_ENCAP_IPNET },
387
  /* SocketCAN frame */
388
  { 227,    WTAP_ENCAP_SOCKETCAN },
389
  /* Raw IPv4 */
390
  { 228,    WTAP_ENCAP_RAW_IP4 },
391
  /* Raw IPv6 */
392
  { 229,    WTAP_ENCAP_RAW_IP6 },
393
  /* IEEE 802.15.4 Wireless PAN no fcs */
394
  { 230,    WTAP_ENCAP_IEEE802_15_4_NOFCS },
395
  /* D-BUS */
396
  { 231,    WTAP_ENCAP_DBUS },
397
  /* DVB-CI (Common Interface) */
398
  { 235,    WTAP_ENCAP_DVBCI },
399
  /* MUX27010 */
400
  { 236,    WTAP_ENCAP_MUX27010 },
401
  /* STANAG 5066 - DTS(Data Transfer Sublayer) PDU */
402
  { 237,    WTAP_ENCAP_STANAG_5066_D_PDU },
403
  /* NFLOG */
404
  { 239,    WTAP_ENCAP_NFLOG },
405
  /* netANALYZER pseudo-header followed by Ethernet with CRC */
406
  { 240,    WTAP_ENCAP_NETANALYZER },
407
  /* netANALYZER pseudo-header in transparent mode */
408
  { 241,    WTAP_ENCAP_NETANALYZER_TRANSPARENT },
409
  /* IP-over-Infiniband, as specified by RFC 4391 section 6 */
410
  { 242,    WTAP_ENCAP_IP_OVER_IB_PCAP },
411
  /* ISO/IEC 13818-1 MPEG2-TS packets */
412
  { 243,    WTAP_ENCAP_MPEG_2_TS },
413
  /* NFC LLCP */
414
  { 245,    WTAP_ENCAP_NFC_LLCP },
415
  /* SCTP */
416
  { 248,    WTAP_ENCAP_SCTP},
417
  /* USBPcap */
418
  { 249,    WTAP_ENCAP_USBPCAP},
419
  /* RTAC SERIAL */
420
  { 250,    WTAP_ENCAP_RTAC_SERIAL},
421
  /* Bluetooth Low Energy Link Layer */
422
  { 251,    WTAP_ENCAP_BLUETOOTH_LE_LL},
423
  /* Wireshark Upper PDU export */
424
  { 252,    WTAP_ENCAP_WIRESHARK_UPPER_PDU},
425
  /* Netlink Protocol (nlmon devices) */
426
  { 253,    WTAP_ENCAP_NETLINK },
427
  /* Bluetooth Linux Monitor */
428
  { 254,    WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR },
429
  /* Bluetooth BR/EDR Baseband RF captures */
430
  { 255,    WTAP_ENCAP_BLUETOOTH_BREDR_BB },
431
  /* Bluetooth Low Energy Link Layer RF captures */
432
  { 256,    WTAP_ENCAP_BLUETOOTH_LE_LL_WITH_PHDR },
433
434
  /* Apple PKTAP */
435
  { 258,    WTAP_ENCAP_PKTAP },
436
437
  /* Ethernet Passive Optical Network */
438
  { 259,    WTAP_ENCAP_EPON },
439
440
  /* IPMI Trace Data Collection */
441
  { 260,    WTAP_ENCAP_IPMI_TRACE },
442
443
  /* ISO 14443 contactless smartcard standards */
444
  { 264,    WTAP_ENCAP_ISO14443 },
445
446
  /* USB packets from Darwin (macOS, iOS) BPF tap */
447
  { 266,          WTAP_ENCAP_USB_DARWIN },
448
449
  /* IBM SDLC frames containing SNA PDUs */
450
  { 268,    WTAP_ENCAP_SDLC },
451
452
  /* LoRaTap */
453
  { 270,    WTAP_ENCAP_LORATAP },
454
455
  /* Linux vsock */
456
  { 271,    WTAP_ENCAP_VSOCK },
457
458
  /* nRF Sniffer for Bluetooth LE */
459
  { 272,    WTAP_ENCAP_NORDIC_BLE },
460
461
  /* DOCSIS31 XRA31 Sniffer */
462
  { 273,    WTAP_ENCAP_DOCSIS31_XRA31 },
463
464
  /* mPackets as specified by 802.3br */
465
  { 274,    WTAP_ENCAP_ETHERNET_MPACKET },
466
467
  /* DisplayPort AUX channel monitor */
468
  { 275,    WTAP_ENCAP_DPAUXMON },
469
470
  /* Linux cooked capture v2 */
471
  { 276,    WTAP_ENCAP_SLL2 },
472
473
  /* Elektrobit High Speed Capture and Replay */
474
  { 279,    WTAP_ENCAP_EBHSCR },
475
476
  /* VPP dispatch trace */
477
  { 280,    WTAP_ENCAP_VPP },
478
479
  /* IEEE 802.15.4 TAP */
480
  { 283,    WTAP_ENCAP_IEEE802_15_4_TAP },
481
482
  /* Z-Wave Serial API */
483
  { 287,    WTAP_ENCAP_ZWAVE_SERIAL },
484
485
  /* USB 2.0/1.1/1.0 packets as transmitted over the cable */
486
  { 288,    WTAP_ENCAP_USB_2_0 },
487
488
  /* ATSC Link-Layer Protocol (A/330) packets */
489
  { 289,    WTAP_ENCAP_ATSC_ALP },
490
491
  /* Event Tracing for Windows records */
492
  { 290,    WTAP_ENCAP_ETW },
493
494
  /* Serial NCP (Network Co-Processor) protocol for Zigbee stack ZBOSS */
495
  { 292,    WTAP_ENCAP_ZBNCP },
496
497
  /* USB 2.0/1.1/1.0 packets captured on Low/Full/High speed link */
498
  { 293,    WTAP_ENCAP_USB_2_0_LOW_SPEED },
499
  { 294,    WTAP_ENCAP_USB_2_0_FULL_SPEED },
500
  { 295,    WTAP_ENCAP_USB_2_0_HIGH_SPEED },
501
502
  /* Auerswald log file captured from any supported Auerswald device */
503
  { 296,    WTAP_ENCAP_AUERSWALD_LOG },
504
505
  /* Silicon Labs debug channel */
506
  { 298,    WTAP_ENCAP_SILABS_DEBUG_CHANNEL },
507
508
  /* Ultra-wideband (UWB) controller interface protocol (UCI) */
509
  { 299,    WTAP_ENCAP_FIRA_UCI },
510
511
  /* MDB (Multi-Drop Bus) */
512
  { 300,    WTAP_ENCAP_MDB },
513
514
  /* DECT_NR (DECT-2020 New Radio (NR) MAC layer) */
515
  { 301,    WTAP_ENCAP_DECT_NR },
516
517
  /*
518
   * To repeat:
519
   *
520
   * If you need a new encapsulation type for pcap and pcapng files,
521
   * do *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
522
   * add a new encapsulation type by changing an existing entry;
523
   * leave the existing entries alone.
524
   *
525
   * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
526
   * for a new DLT_ value, and specifying the purpose of the new value.
527
   * When you get the new DLT_ value, use that numerical value in
528
   * the "linktype_value" field of "pcap_to_wtap_map[]".
529
   */
530
531
  /*
532
   * The following are entries for libpcap type values that have
533
   * different meanings on different OSes.  I.e., these are DLT_
534
   * values that are different on different OSes, and that have
535
   * a separate LINKTYPE_ value assigned to them.
536
   *
537
   * We put these *after* the entries for the LINKTYPE_ values for
538
   * those Wiretap encapsulation types, so that, when writing a
539
   * pcap or pcapng file, Wireshark writes the LINKTYPE_ value,
540
   * not the OS's DLT_ value, as the file's link-layer header type
541
   * for pcap or the interface's link-layer header type.
542
   */
543
544
  /*
545
   * 11 is DLT_ATM_RFC1483 on most platforms; the only version of
546
   * libpcap I've seen that define anything other than DLT_ATM_RFC1483
547
   * as 11 is the BSD/OS one, which defines DLT_FR as 11.  We handle
548
   * it as Frame Relay on BSD/OS and LLC-encapsulated ATM on all other
549
   * platforms.
550
   */
551
#if defined(__bsdi__) /* BSD/OS */
552
  { 11,   WTAP_ENCAP_FRELAY },
553
#else
554
  { 11,   WTAP_ENCAP_ATM_RFC1483 },
555
#endif
556
557
  /*
558
   * 12 is DLT_RAW on most platforms, but it's DLT_C_HDLC on
559
   * BSD/OS, and DLT_LOOP on OpenBSD.
560
   *
561
   * We don't yet handle DLT_C_HDLC, but we can handle DLT_LOOP
562
   * (it's just like DLT_NULL, only with the AF_ value in network
563
   * rather than host byte order - Wireshark figures out the
564
   * byte order from the data, so we don't care what byte order
565
   * it's in), so, on OpenBSD, interpret 12 as WTAP_ENCAP_LOOP,
566
   * otherwise, if we're not on BSD/OS, interpret it as
567
   * WTAP_ENCAP_RAW_IP.
568
   */
569
#if defined(__OpenBSD__)
570
  { 12,   WTAP_ENCAP_LOOP },
571
#elif defined(__bsdi__) /* BSD/OS */
572
  /*
573
   * Put entry for Cisco HDLC here.
574
   * XXX - is this just WTAP_ENCAP_CHDLC, i.e. does the frame
575
   * start with a 4-byte Cisco HDLC header?
576
   */
577
#else
578
  { 12,   WTAP_ENCAP_RAW_IP },
579
#endif
580
581
  /*
582
   * 13 is DLT_SLIP_BSDOS on FreeBSD and NetBSD, but those OSes
583
   * don't actually generate it.  I infer that BSD/OS translates
584
   * DLT_SLIP from the kernel BPF code to DLT_SLIP_BSDOS in
585
   * libpcap, as the BSD/OS link-layer header is different;
586
   * however, in BSD/OS, DLT_SLIP_BSDOS is 15.
587
   *
588
   * From this, I infer that there's no point in handling 13
589
   * as DLT_SLIP_BSDOS.
590
   *
591
   * 13 is DLT_ATM_RFC1483 on BSD/OS.
592
   *
593
   * 13 is DLT_ENC in OpenBSD, which is, I suspect, some kind
594
   * of decrypted IPsec traffic.
595
   *
596
   * We treat 13 as WTAP_ENCAP_ENC on all systems except those
597
   * that define DLT_ATM_RFC1483 as 13 - presumably only
598
   * BSD/OS does so - so that, on BSD/OS systems, we still
599
   * treat 13 as WTAP_ENCAP_ATM_RFC1483, but, on all other
600
   * systems, we can read OpenBSD DLT_ENC captures.
601
   */
602
#if defined(__bsdi__) /* BSD/OS */
603
  { 13,   WTAP_ENCAP_ATM_RFC1483 },
604
#else
605
  { 13,   WTAP_ENCAP_ENC },
606
#endif
607
608
  /*
609
   * 14 is DLT_PPP_BSDOS on FreeBSD and NetBSD, but those OSes
610
   * don't actually generate it.  I infer that BSD/OS translates
611
   * DLT_PPP from the kernel BPF code to DLT_PPP_BSDOS in
612
   * libpcap, as the BSD/OS link-layer header is different;
613
   * however, in BSD/OS, DLT_PPP_BSDOS is 16.
614
   *
615
   * From this, I infer that there's no point in handling 14
616
   * as DLT_PPP_BSDOS.
617
   *
618
   * 14 is DLT_RAW on BSD/OS and OpenBSD.
619
   */
620
  { 14,   WTAP_ENCAP_RAW_IP },
621
622
  /*
623
   * 15 is:
624
   *
625
   *  DLT_SLIP_BSDOS on BSD/OS;
626
   *
627
   *  DLT_HIPPI on NetBSD;
628
   *
629
   *  DLT_LANE8023 with Alexey Kuznetzov's patches for
630
   *  Linux libpcap;
631
   *
632
   *  DLT_I4L_RAWIP with the ISDN4Linux patches for libpcap
633
   *  (and on SuSE 6.3);
634
   *
635
   * but we don't currently handle any of those.
636
   */
637
638
  /*
639
   * 16 is:
640
   *
641
   *  DLT_PPP_BSDOS on BSD/OS;
642
   *
643
   *  DLT_HDLC on NetBSD (Cisco HDLC);
644
   *
645
   *  DLT_CIP with Alexey Kuznetzov's patches for
646
   *  Linux libpcap - this is WTAP_ENCAP_LINUX_ATM_CLIP;
647
   *
648
   *  DLT_I4L_IP with the ISDN4Linux patches for libpcap
649
   *  (and on SuSE 6.3).
650
   */
651
#if defined(__NetBSD__)
652
  { 16,   WTAP_ENCAP_CHDLC },
653
#elif !defined(__bsdi__)
654
  /*
655
   * If you care about the two different Linux interpretations
656
   * of 16, fix it yourself.
657
   */
658
  { 16,   WTAP_ENCAP_LINUX_ATM_CLIP },
659
#endif
660
661
  /*
662
   * 17 is DLT_LANE8023 in SuSE 6.3 libpcap; we don't currently
663
   * handle it.
664
   * It is also used as the PF (Packet Filter) logging format beginning
665
   * with OpenBSD 3.0; we use 17 for PF logs on OpenBSD and don't
666
   * use it otherwise.
667
   */
668
#if defined(__OpenBSD__)
669
  { 17,   WTAP_ENCAP_OLD_PFLOG },
670
#endif
671
672
  /*
673
   * 18 is DLT_CIP in SuSE 6.3 libpcap; if it's the same as the
674
   * DLT_CIP of 16 that the Alexey Kuznetzov patches for
675
   * libpcap/tcpdump define, it's WTAP_ENCAP_LINUX_ATM_CLIP.
676
   * I've not found any version of libpcap that uses it for any
677
   * other purpose - hopefully nobody will do so in the future.
678
   */
679
  { 18,   WTAP_ENCAP_LINUX_ATM_CLIP },
680
681
  /*
682
   * 19 is DLT_ATM_CLIP in the libpcap/tcpdump patches in the
683
   * recent versions I've seen of the Linux ATM distribution;
684
   * I've not yet found any version of libpcap file that uses it
685
   * for any other purpose - hopefully nobody will do so in
686
   * the future.
687
   */
688
  { 19,   WTAP_ENCAP_LINUX_ATM_CLIP },
689
690
  /*
691
   * To repeat:
692
   *
693
   * If you need a new encapsulation type for pcap and pcapng files,
694
   * do *N*O*T* use *ANY* of the values listed here!  I.e., do *NOT*
695
   * add a new encapsulation type by changing an existing entry;
696
   * leave the existing entries alone.
697
   *
698
   * Instead, send mail to tcpdump-workers@lists.tcpdump.org, asking
699
   * for a new DLT_ value, and specifying the purpose of the new value.
700
   * When you get the new DLT_ value, use that numerical value in
701
   * the "linktype_value" field of "pcap_to_wtap_map[]".
702
   */
703
};
704
173k
#define NUM_PCAP_ENCAPS array_length(pcap_to_wtap_map)
705
706
int
707
wtap_pcap_encap_to_wtap_encap(int encap)
708
1.62k
{
709
1.62k
  unsigned int i;
710
711
173k
  for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
712
173k
    if (pcap_to_wtap_map[i].linktype_value == encap)
713
1.62k
      return pcap_to_wtap_map[i].wtap_encap_value;
714
173k
  }
715
7
  return WTAP_ENCAP_UNKNOWN;
716
1.62k
}
717
718
int
719
wtap_wtap_encap_to_pcap_encap(int encap)
720
0
{
721
0
  unsigned int i;
722
723
0
  switch (encap) {
724
725
0
  case WTAP_ENCAP_FDDI:
726
0
  case WTAP_ENCAP_FDDI_BITSWAPPED:
727
    /*
728
     * Special-case WTAP_ENCAP_FDDI and
729
     * WTAP_ENCAP_FDDI_BITSWAPPED; both of them get mapped
730
     * to DLT_FDDI (even though that may mean that the bit
731
     * order in the FDDI MAC addresses is wrong; so it goes
732
     * - libpcap format doesn't record the byte order,
733
     * so that's not fixable).
734
     *
735
     * The pcap_to_wtap_map[] table will only have an
736
     * entry for one of the above, which is why we have
737
     * to special-case them.
738
     */
739
0
    return 10;  /* that's DLT_FDDI */
740
741
0
  case WTAP_ENCAP_NETTL_FDDI:
742
    /*
743
     * This will discard the nettl information, as that's
744
     * in the pseudo-header.
745
     *
746
     * XXX - what about Ethernet and Token Ring?
747
     */
748
0
    return 10;  /* that's DLT_FDDI */
749
750
0
  case WTAP_ENCAP_FRELAY_WITH_PHDR:
751
    /*
752
     * This will discard the pseudo-header information.
753
     */
754
0
    return 107;
755
756
0
  case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
757
    /*
758
     * Map this to DLT_IEEE802_11, for now, even though
759
     * that means the radio information will be lost.
760
     * We should try to map those values to radiotap
761
     * values and write this out as a radiotap file,
762
     * if possible.
763
     */
764
0
    return 105;
765
0
  }
766
767
0
  for (i = 0; i < NUM_PCAP_ENCAPS; i++) {
768
0
    if (pcap_to_wtap_map[i].wtap_encap_value == encap)
769
0
      return pcap_to_wtap_map[i].linktype_value;
770
0
  }
771
0
  return -1;
772
0
}
773
774
/*
775
 * For most encapsulations, we use WTAP_MAX_PACKET_SIZE_STANDARD, as
776
 * that should be enough for most link-layer types, and shouldn't be
777
 * too big.
778
 *
779
 * For some link-layer types, we use larger types, because, for each
780
 * of them, the maximum packet size is larger than the standard
781
 * maximum, and is bigger than we'd want for all link-layer types - files
782
 * with that snapshot length might cause some programs reading them to
783
 * allocate a huge and wasteful buffer and, at least on 32-bit platforms,
784
 * run the risk of running out of memory.
785
 */
786
unsigned
787
wtap_max_snaplen_for_encap(int wtap_encap)
788
0
{
789
0
  switch (wtap_encap) {
790
791
0
  case WTAP_ENCAP_DBUS:
792
0
    return WTAP_MAX_PACKET_SIZE_DBUS;
793
794
0
  case WTAP_ENCAP_EBHSCR:
795
0
    return WTAP_MAX_PACKET_SIZE_EBHSCR;
796
797
0
  case WTAP_ENCAP_USBPCAP:
798
0
  case WTAP_ENCAP_USB_LINUX:
799
0
  case WTAP_ENCAP_USB_LINUX_MMAPPED:
800
0
  case WTAP_ENCAP_USB_DARWIN:
801
0
  case WTAP_ENCAP_USB_FREEBSD:
802
0
    return WTAP_MAX_PACKET_SIZE_USBPCAP;
803
804
0
  default:
805
0
    return WTAP_MAX_PACKET_SIZE_STANDARD;
806
0
  }
807
0
}
808
809
/*
810
 * Various pseudo-headers that appear at the beginning of packet data.
811
 *
812
 * We represent them as sets of offsets, as they might not be aligned on
813
 * an appropriate structure boundary in the buffer, and as that makes them
814
 * independent of the way the compiler might align fields.
815
 */
816
817
/*
818
 * The link-layer header on Nokia IPSO ATM packets.
819
 */
820
0
#define NOKIAATM_FLAGS  0  /* destination - 1 byte */
821
0
#define NOKIAATM_VPI  1  /* VPI - 1 byte */
822
0
#define NOKIAATM_VCI  2  /* VCI - 2 bytes */
823
0
#define NOKIAATM_LEN  4  /* length of the header */
824
825
static int
826
pcap_read_nokiaatm_pseudoheader(FILE_T fh,
827
    union wtap_pseudo_header *pseudo_header, unsigned packet_size,
828
    int *err, char **err_info)
829
0
{
830
0
  uint8_t atm_phdr[NOKIAATM_LEN];
831
0
  uint8_t vpi;
832
0
  uint16_t  vci;
833
834
0
  if (packet_size < NOKIAATM_LEN) {
835
    /*
836
     * Uh-oh, the packet isn't big enough to even
837
     * have a pseudo-header.
838
     */
839
0
    *err = WTAP_ERR_BAD_FILE;
840
0
    *err_info = ws_strdup_printf("pcap/pcapng: Nokia IPSO ATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
841
0
        packet_size);
842
0
    return -1;
843
0
  }
844
0
  if (!wtap_read_bytes(fh, atm_phdr, NOKIAATM_LEN, err, err_info))
845
0
    return -1;
846
847
0
  vpi = atm_phdr[NOKIAATM_VPI];
848
0
  vci = pntohu16(&atm_phdr[NOKIAATM_VCI]);
849
850
0
  pseudo_header->atm.vpi = vpi;
851
0
  pseudo_header->atm.vci = vci;
852
0
  pseudo_header->atm.channel = (atm_phdr[NOKIAATM_FLAGS] & 0x80) ? 0 : 1;
853
854
  /* We don't have this information */
855
0
  pseudo_header->atm.flags = 0;
856
0
  pseudo_header->atm.cells = 0;
857
0
  pseudo_header->atm.aal5t_u2u = 0;
858
0
  pseudo_header->atm.aal5t_len = 0;
859
0
  pseudo_header->atm.aal5t_chksum = 0;
860
861
0
  return NOKIAATM_LEN;
862
0
}
863
864
/*
865
 * The link-layer header on SunATM packets.
866
 */
867
0
#define SUNATM_FLAGS  0  /* destination and traffic type - 1 byte */
868
0
#define SUNATM_VPI  1  /* VPI - 1 byte */
869
0
#define SUNATM_VCI  2  /* VCI - 2 bytes */
870
0
#define SUNATM_LEN  4  /* length of the header */
871
872
static int
873
pcap_read_sunatm_pseudoheader(FILE_T fh,
874
    union wtap_pseudo_header *pseudo_header, unsigned packet_size,
875
    int *err, char **err_info)
876
0
{
877
0
  uint8_t atm_phdr[SUNATM_LEN];
878
0
  uint8_t vpi;
879
0
  uint16_t  vci;
880
881
0
  if (packet_size < SUNATM_LEN) {
882
    /*
883
     * Uh-oh, the packet isn't big enough to even
884
     * have a pseudo-header.
885
     */
886
0
    *err = WTAP_ERR_BAD_FILE;
887
0
    *err_info = ws_strdup_printf("pcap/pcapng: SunATM file has a %u-byte packet, too small to have even an ATM pseudo-header",
888
0
        packet_size);
889
0
    return -1;
890
0
  }
891
0
  if (!wtap_read_bytes(fh, atm_phdr, SUNATM_LEN, err, err_info))
892
0
    return -1;
893
894
0
  vpi = atm_phdr[SUNATM_VPI];
895
0
  vci = pntohu16(&atm_phdr[SUNATM_VCI]);
896
897
0
  switch (atm_phdr[SUNATM_FLAGS] & 0x0F) {
898
899
0
  case 0x01:  /* LANE */
900
0
    pseudo_header->atm.aal = AAL_5;
901
0
    pseudo_header->atm.type = TRAF_LANE;
902
0
    break;
903
904
0
  case 0x02:  /* RFC 1483 LLC multiplexed traffic */
905
0
    pseudo_header->atm.aal = AAL_5;
906
0
    pseudo_header->atm.type = TRAF_LLCMX;
907
0
    break;
908
909
0
  case 0x05:  /* ILMI */
910
0
    pseudo_header->atm.aal = AAL_5;
911
0
    pseudo_header->atm.type = TRAF_ILMI;
912
0
    break;
913
914
0
  case 0x06:  /* Q.2931 */
915
0
    pseudo_header->atm.aal = AAL_SIGNALLING;
916
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
917
0
    break;
918
919
0
  case 0x03:  /* MARS (RFC 2022) */
920
0
    pseudo_header->atm.aal = AAL_5;
921
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
922
0
    break;
923
924
0
  case 0x04:  /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
925
0
    pseudo_header->atm.aal = AAL_5;
926
0
    pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
927
0
    break;
928
929
0
  default:
930
    /*
931
     * Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
932
     * case assume it's AAL_SIGNALLING; we know nothing more
933
     * about it.
934
     *
935
     * XXX - is this necessary?  Or are we guaranteed that
936
     * all signalling traffic has a type of 0x06?
937
     *
938
     * XXX - is this guaranteed to be AAL5?  Or, if the type is
939
     * 0x00 ("raw"), might it be non-AAL5 traffic?
940
     */
941
0
    if (vpi == 0 && vci == 5)
942
0
      pseudo_header->atm.aal = AAL_SIGNALLING;
943
0
    else
944
0
      pseudo_header->atm.aal = AAL_5;
945
0
    pseudo_header->atm.type = TRAF_UNKNOWN;
946
0
    break;
947
0
  }
948
0
  pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
949
950
0
  pseudo_header->atm.vpi = vpi;
951
0
  pseudo_header->atm.vci = vci;
952
0
  pseudo_header->atm.channel = (atm_phdr[SUNATM_FLAGS] & 0x80) ? 0 : 1;
953
954
  /* We don't have this information */
955
0
  pseudo_header->atm.flags = 0;
956
0
  pseudo_header->atm.cells = 0;
957
0
  pseudo_header->atm.aal5t_u2u = 0;
958
0
  pseudo_header->atm.aal5t_len = 0;
959
0
  pseudo_header->atm.aal5t_chksum = 0;
960
961
0
  return SUNATM_LEN;
962
0
}
963
964
static bool
965
pcap_write_sunatm_pseudoheader(wtap_dumper *wdh,
966
    const union wtap_pseudo_header *pseudo_header, int *err)
967
0
{
968
0
  uint8_t atm_hdr[SUNATM_LEN];
969
970
  /*
971
   * Write the ATM header.
972
   */
973
0
  atm_hdr[SUNATM_FLAGS] =
974
0
      (pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
975
0
  switch (pseudo_header->atm.aal) {
976
977
0
  case AAL_SIGNALLING:
978
    /* Q.2931 */
979
0
    atm_hdr[SUNATM_FLAGS] |= 0x06;
980
0
    break;
981
982
0
  case AAL_5:
983
0
    switch (pseudo_header->atm.type) {
984
985
0
    case TRAF_LANE:
986
      /* LANE */
987
0
      atm_hdr[SUNATM_FLAGS] |= 0x01;
988
0
      break;
989
990
0
    case TRAF_LLCMX:
991
      /* RFC 1483 LLC multiplexed traffic */
992
0
      atm_hdr[SUNATM_FLAGS] |= 0x02;
993
0
      break;
994
995
0
    case TRAF_ILMI:
996
      /* ILMI */
997
0
      atm_hdr[SUNATM_FLAGS] |= 0x05;
998
0
      break;
999
0
    }
1000
0
    break;
1001
0
  }
1002
0
  atm_hdr[SUNATM_VPI] = (uint8_t)pseudo_header->atm.vpi;
1003
0
  phtonu16(&atm_hdr[SUNATM_VCI], pseudo_header->atm.vci);
1004
0
  if (!wtap_dump_file_write(wdh, atm_hdr, sizeof(atm_hdr), err))
1005
0
    return false;
1006
0
  return true;
1007
0
}
1008
1009
/*
1010
 * The fake link-layer header of IrDA packets as introduced by Jean Tourrilhes
1011
 * to libpcap.
1012
 */
1013
0
#define IRDA_SLL_PKTTYPE_OFFSET   0  /* packet type - 2 bytes */
1014
/* 12 unused bytes */
1015
0
#define IRDA_SLL_PROTOCOL_OFFSET  14  /* protocol, should be ETH_P_LAPD - 2 bytes */
1016
0
#define IRDA_SLL_LEN      16  /* length of the header */
1017
1018
static int
1019
pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1020
    unsigned packet_size, int *err, char **err_info)
1021
0
{
1022
0
  uint8_t irda_phdr[IRDA_SLL_LEN];
1023
1024
0
  if (packet_size < IRDA_SLL_LEN) {
1025
    /*
1026
     * Uh-oh, the packet isn't big enough to even
1027
     * have a pseudo-header.
1028
     */
1029
0
    *err = WTAP_ERR_BAD_FILE;
1030
0
    *err_info = ws_strdup_printf("pcap/pcapng: IrDA file has a %u-byte packet, too small to have even an IrDA pseudo-header",
1031
0
        packet_size);
1032
0
    return -1;
1033
0
  }
1034
0
  if (!wtap_read_bytes(fh, irda_phdr, IRDA_SLL_LEN, err, err_info))
1035
0
    return -1;
1036
1037
0
  if (pntohu16(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
1038
0
    *err = WTAP_ERR_BAD_FILE;
1039
0
    if (err_info != NULL)
1040
0
      *err_info = g_strdup("pcap/pcapng: IrDA capture has a packet with an invalid sll_protocol field");
1041
0
    return -1;
1042
0
  }
1043
1044
0
  pseudo_header->irda.pkttype = pntohu16(&irda_phdr[IRDA_SLL_PKTTYPE_OFFSET]);
1045
1046
0
  return IRDA_SLL_LEN;
1047
0
}
1048
1049
static bool
1050
pcap_write_irda_pseudoheader(wtap_dumper *wdh,
1051
    const union wtap_pseudo_header *pseudo_header, int *err)
1052
0
{
1053
0
  uint8_t irda_hdr[IRDA_SLL_LEN];
1054
1055
  /*
1056
   * Write the IrDA header.
1057
   */
1058
0
  memset(irda_hdr, 0, sizeof(irda_hdr));
1059
0
  phtonu16(&irda_hdr[IRDA_SLL_PKTTYPE_OFFSET], pseudo_header->irda.pkttype);
1060
0
  phtonu16(&irda_hdr[IRDA_SLL_PROTOCOL_OFFSET], 0x0017);
1061
0
  if (!wtap_dump_file_write(wdh, irda_hdr, sizeof(irda_hdr), err))
1062
0
    return false;
1063
0
  return true;
1064
0
}
1065
1066
/*
1067
 * A header containing additional MTP information.
1068
 */
1069
0
#define MTP2_SENT_OFFSET    0  /* 1 byte */
1070
0
#define MTP2_ANNEX_A_USED_OFFSET  1  /* 1 byte */
1071
0
#define MTP2_LINK_NUMBER_OFFSET   2  /* 2 bytes */
1072
0
#define MTP2_HDR_LEN      4  /* length of the header */
1073
1074
static int
1075
pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1076
    unsigned packet_size, int *err, char **err_info)
1077
0
{
1078
0
  uint8_t mtp2_hdr[MTP2_HDR_LEN];
1079
1080
0
  if (packet_size < MTP2_HDR_LEN) {
1081
    /*
1082
     * Uh-oh, the packet isn't big enough to even
1083
     * have a pseudo-header.
1084
     */
1085
0
    *err = WTAP_ERR_BAD_FILE;
1086
0
    *err_info = ws_strdup_printf("pcap/pcapng: MTP2 file has a %u-byte packet, too small to have even an MTP2 pseudo-header",
1087
0
        packet_size);
1088
0
    return -1;
1089
0
  }
1090
0
  if (!wtap_read_bytes(fh, mtp2_hdr, MTP2_HDR_LEN, err, err_info))
1091
0
    return -1;
1092
1093
0
  pseudo_header->mtp2.sent         = mtp2_hdr[MTP2_SENT_OFFSET];
1094
0
  pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
1095
0
  pseudo_header->mtp2.link_number  = pntohu16(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET]);
1096
1097
0
  return MTP2_HDR_LEN;
1098
0
}
1099
1100
static bool
1101
pcap_write_mtp2_pseudoheader(wtap_dumper *wdh,
1102
    const union wtap_pseudo_header *pseudo_header, int *err)
1103
0
{
1104
0
  uint8_t mtp2_hdr[MTP2_HDR_LEN];
1105
1106
  /*
1107
   * Write the MTP2 header.
1108
   */
1109
0
  memset(&mtp2_hdr, 0, sizeof(mtp2_hdr));
1110
0
  mtp2_hdr[MTP2_SENT_OFFSET] = pseudo_header->mtp2.sent;
1111
0
  mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET] = pseudo_header->mtp2.annex_a_used;
1112
0
  phtonu16(&mtp2_hdr[MTP2_LINK_NUMBER_OFFSET],
1113
0
      pseudo_header->mtp2.link_number);
1114
0
  if (!wtap_dump_file_write(wdh, mtp2_hdr, sizeof(mtp2_hdr), err))
1115
0
    return false;
1116
0
  return true;
1117
0
}
1118
1119
/*
1120
 * The fake link-layer header of LAPD packets.
1121
 */
1122
#ifndef ETH_P_LAPD
1123
0
#define ETH_P_LAPD 0x0030
1124
#endif
1125
1126
0
#define LAPD_SLL_PKTTYPE_OFFSET   0  /* packet type - 2 bytes */
1127
#define LAPD_SLL_HATYPE_OFFSET    2 /* hardware address type - 2 bytes */
1128
#define LAPD_SLL_HALEN_OFFSET   4 /* hardware address length - 2 bytes */
1129
0
#define LAPD_SLL_ADDR_OFFSET    6  /* address - 8 bytes */
1130
0
#define LAPD_SLL_PROTOCOL_OFFSET  14  /* protocol, should be ETH_P_LAPD - 2 bytes */
1131
0
#define LAPD_SLL_LEN      16  /* length of the header */
1132
1133
static int
1134
pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1135
    unsigned packet_size, int *err, char **err_info)
1136
0
{
1137
0
  uint8_t lapd_phdr[LAPD_SLL_LEN];
1138
1139
0
  if (packet_size < LAPD_SLL_LEN) {
1140
    /*
1141
     * Uh-oh, the packet isn't big enough to even
1142
     * have a pseudo-header.
1143
     */
1144
0
    *err = WTAP_ERR_BAD_FILE;
1145
0
    *err_info = ws_strdup_printf("pcap/pcapng: LAPD file has a %u-byte packet, too small to have even a LAPD pseudo-header",
1146
0
        packet_size);
1147
0
    return -1;
1148
0
  }
1149
0
  if (!wtap_read_bytes(fh, lapd_phdr, LAPD_SLL_LEN, err, err_info))
1150
0
    return -1;
1151
1152
0
  if (pntohu16(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
1153
0
    *err = WTAP_ERR_BAD_FILE;
1154
0
    if (err_info != NULL)
1155
0
      *err_info = g_strdup("pcap/pcapng: LAPD capture has a packet with an invalid sll_protocol field");
1156
0
    return -1;
1157
0
  }
1158
1159
0
  pseudo_header->lapd.pkttype = pntohu16(&lapd_phdr[LAPD_SLL_PKTTYPE_OFFSET]);
1160
0
  pseudo_header->lapd.we_network = !!lapd_phdr[LAPD_SLL_ADDR_OFFSET+0];
1161
1162
0
  return LAPD_SLL_LEN;
1163
0
}
1164
1165
static bool
1166
pcap_write_lapd_pseudoheader(wtap_dumper *wdh,
1167
    const union wtap_pseudo_header *pseudo_header, int *err)
1168
0
{
1169
0
  uint8_t lapd_hdr[LAPD_SLL_LEN];
1170
1171
  /*
1172
   * Write the LAPD header.
1173
   */
1174
0
  memset(&lapd_hdr, 0, sizeof(lapd_hdr));
1175
0
  phtonu16(&lapd_hdr[LAPD_SLL_PKTTYPE_OFFSET], pseudo_header->lapd.pkttype);
1176
0
  phtonu16(&lapd_hdr[LAPD_SLL_PROTOCOL_OFFSET], ETH_P_LAPD);
1177
0
  lapd_hdr[LAPD_SLL_ADDR_OFFSET + 0] =
1178
0
      pseudo_header->lapd.we_network?0x01:0x00;
1179
0
  if (!wtap_dump_file_write(wdh, lapd_hdr, sizeof(lapd_hdr), err))
1180
0
    return false;
1181
0
  return true;
1182
0
}
1183
1184
/*
1185
 * A header containing additional SITA WAN information.
1186
 */
1187
0
#define SITA_FLAGS_OFFSET   0  /* 1 byte */
1188
0
#define SITA_SIGNALS_OFFSET   1  /* 1 byte */
1189
0
#define SITA_ERRORS1_OFFSET   2  /* 1 byte */
1190
0
#define SITA_ERRORS2_OFFSET   3  /* 1 byte */
1191
0
#define SITA_PROTO_OFFSET   4  /* 1 byte */
1192
0
#define SITA_HDR_LEN      5  /* length of the header */
1193
1194
static int
1195
pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1196
    unsigned packet_size, int *err, char **err_info)
1197
0
{
1198
0
  uint8_t sita_phdr[SITA_HDR_LEN];
1199
1200
0
  if (packet_size < SITA_HDR_LEN) {
1201
    /*
1202
     * Uh-oh, the packet isn't big enough to even
1203
     * have a pseudo-header.
1204
     */
1205
0
    *err = WTAP_ERR_BAD_FILE;
1206
0
    *err_info = ws_strdup_printf("pcap/pcapng: SITA file has a %u-byte packet, too small to have even a SITA pseudo-header",
1207
0
        packet_size);
1208
0
    return -1;
1209
0
  }
1210
0
  if (!wtap_read_bytes(fh, sita_phdr, SITA_HDR_LEN, err, err_info))
1211
0
    return -1;
1212
1213
0
  pseudo_header->sita.sita_flags   = sita_phdr[SITA_FLAGS_OFFSET];
1214
0
  pseudo_header->sita.sita_signals = sita_phdr[SITA_SIGNALS_OFFSET];
1215
0
  pseudo_header->sita.sita_errors1 = sita_phdr[SITA_ERRORS1_OFFSET];
1216
0
  pseudo_header->sita.sita_errors2 = sita_phdr[SITA_ERRORS2_OFFSET];
1217
0
  pseudo_header->sita.sita_proto   = sita_phdr[SITA_PROTO_OFFSET];
1218
1219
0
  return SITA_HDR_LEN;
1220
0
}
1221
1222
static bool
1223
pcap_write_sita_pseudoheader(wtap_dumper *wdh,
1224
    const union wtap_pseudo_header *pseudo_header, int *err)
1225
0
{
1226
0
  uint8_t sita_hdr[SITA_HDR_LEN];
1227
1228
  /*
1229
   * Write the SITA header.
1230
   */
1231
0
  memset(&sita_hdr, 0, sizeof(sita_hdr));
1232
0
  sita_hdr[SITA_FLAGS_OFFSET]   = pseudo_header->sita.sita_flags;
1233
0
  sita_hdr[SITA_SIGNALS_OFFSET] = pseudo_header->sita.sita_signals;
1234
0
  sita_hdr[SITA_ERRORS1_OFFSET] = pseudo_header->sita.sita_errors1;
1235
0
  sita_hdr[SITA_ERRORS2_OFFSET] = pseudo_header->sita.sita_errors2;
1236
0
  sita_hdr[SITA_PROTO_OFFSET]   = pseudo_header->sita.sita_proto;
1237
0
  if (!wtap_dump_file_write(wdh, sita_hdr, sizeof(sita_hdr), err))
1238
0
    return false;
1239
0
  return true;
1240
0
}
1241
1242
/*
1243
 * Pseudo-header at the beginning of DLT_BLUETOOTH_HCI_H4_WITH_PHDR frames.
1244
 * Values in network byte order.
1245
 */
1246
struct pcap_bt_phdr {
1247
  uint32_t direction;     /* Bit 0 hold the frame direction. */
1248
};
1249
1250
0
#define LIBPCAP_BT_PHDR_SENT    0
1251
0
#define LIBPCAP_BT_PHDR_RECV    1
1252
1253
static int
1254
pcap_read_bt_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1255
    unsigned packet_size, int *err, char **err_info)
1256
0
{
1257
0
  struct pcap_bt_phdr phdr;
1258
1259
0
  if (packet_size < sizeof (struct pcap_bt_phdr)) {
1260
    /*
1261
     * Uh-oh, the packet isn't big enough to even
1262
     * have a pseudo-header.
1263
     */
1264
0
    *err = WTAP_ERR_BAD_FILE;
1265
0
    *err_info = ws_strdup_printf("pcap/pcapng: Bluetooth file has a %u-byte packet, too small to have even a pseudo-header",
1266
0
        packet_size);
1267
0
    return -1;
1268
0
  }
1269
0
  if (!wtap_read_bytes(fh, &phdr, sizeof (struct pcap_bt_phdr),
1270
0
      err, err_info))
1271
0
    return -1;
1272
0
  pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? true: false;
1273
0
  return (int)sizeof (struct pcap_bt_phdr);
1274
0
}
1275
1276
static bool
1277
pcap_write_bt_pseudoheader(wtap_dumper *wdh,
1278
    const union wtap_pseudo_header *pseudo_header, int *err)
1279
0
{
1280
0
  uint32_t direction;
1281
0
  struct pcap_bt_phdr bt_hdr;
1282
1283
0
  direction = pseudo_header->p2p.sent ? LIBPCAP_BT_PHDR_SENT : LIBPCAP_BT_PHDR_RECV;
1284
0
  bt_hdr.direction = GUINT32_TO_BE(direction);
1285
0
  if (!wtap_dump_file_write(wdh, &bt_hdr, sizeof bt_hdr, err))
1286
0
    return false;
1287
0
  return true;
1288
0
}
1289
1290
/*
1291
 * Pseudo-header at the beginning of DLT_BLUETOOTH_LINUX_MONITOR frames.
1292
 * Values in network byte order.
1293
 */
1294
struct pcap_bt_monitor_phdr {
1295
  uint16_t adapter_id;
1296
  uint16_t opcode;
1297
};
1298
1299
static int
1300
pcap_read_bt_monitor_pseudoheader(FILE_T fh,
1301
    union wtap_pseudo_header *pseudo_header, unsigned packet_size,
1302
    int *err, char **err_info)
1303
0
{
1304
0
  struct pcap_bt_monitor_phdr phdr;
1305
1306
0
  if (packet_size < sizeof (struct pcap_bt_monitor_phdr)) {
1307
    /*
1308
     * Uh-oh, the packet isn't big enough to even
1309
     * have a pseudo-header.
1310
     */
1311
0
    *err = WTAP_ERR_BAD_FILE;
1312
0
    *err_info = ws_strdup_printf("pcap/pcapng: Bluetooth monitor file has a %u-byte packet, too small to have even a pseudo-header",
1313
0
        packet_size);
1314
0
    return -1;
1315
0
  }
1316
0
  if (!wtap_read_bytes(fh, &phdr, sizeof (struct pcap_bt_monitor_phdr),
1317
0
      err, err_info))
1318
0
    return -1;
1319
1320
0
  pseudo_header->btmon.adapter_id = g_ntohs(phdr.adapter_id);
1321
0
  pseudo_header->btmon.opcode = g_ntohs(phdr.opcode);
1322
0
  return (int)sizeof (struct pcap_bt_monitor_phdr);
1323
0
}
1324
1325
static bool
1326
pcap_write_bt_monitor_pseudoheader(wtap_dumper *wdh,
1327
    const union wtap_pseudo_header *pseudo_header, int *err)
1328
0
{
1329
0
  struct pcap_bt_monitor_phdr bt_monitor_hdr;
1330
1331
0
  bt_monitor_hdr.adapter_id = GUINT16_TO_BE(pseudo_header->btmon.adapter_id);
1332
0
  bt_monitor_hdr.opcode = GUINT16_TO_BE(pseudo_header->btmon.opcode);
1333
1334
0
  if (!wtap_dump_file_write(wdh, &bt_monitor_hdr, sizeof bt_monitor_hdr, err))
1335
0
    return false;
1336
0
  return true;
1337
0
}
1338
1339
/*
1340
 * The NFC LLCP per-packet header.
1341
 */
1342
0
#define LLCP_ADAPTER_OFFSET   0
1343
0
#define LLCP_FLAGS_OFFSET   1
1344
0
#define LLCP_HEADER_LEN     2
1345
1346
static int
1347
pcap_read_llcp_pseudoheader(FILE_T fh,
1348
    union wtap_pseudo_header *pseudo_header, unsigned packet_size,
1349
    int *err, char **err_info)
1350
0
{
1351
0
  uint8_t phdr[LLCP_HEADER_LEN];
1352
1353
0
  if (packet_size < LLCP_HEADER_LEN) {
1354
0
    *err = WTAP_ERR_BAD_FILE;
1355
0
    *err_info = ws_strdup_printf("pcap/pcapng: NFC LLCP file has a %u-byte packet, too small to have even a pseudo-header",
1356
0
        packet_size);
1357
0
    return -1;
1358
0
  }
1359
0
  if (!wtap_read_bytes(fh, phdr, LLCP_HEADER_LEN, err, err_info))
1360
0
    return -1;
1361
0
  pseudo_header->llcp.adapter = phdr[LLCP_ADAPTER_OFFSET];
1362
0
  pseudo_header->llcp.flags = phdr[LLCP_FLAGS_OFFSET];
1363
0
  return LLCP_HEADER_LEN;
1364
0
}
1365
1366
static bool
1367
pcap_write_llcp_pseudoheader(wtap_dumper *wdh,
1368
    const union wtap_pseudo_header *pseudo_header, int *err)
1369
0
{
1370
0
  uint8_t phdr[LLCP_HEADER_LEN];
1371
1372
0
  phdr[LLCP_ADAPTER_OFFSET] = pseudo_header->llcp.adapter;
1373
0
  phdr[LLCP_FLAGS_OFFSET] = pseudo_header->llcp.flags;
1374
0
  if (!wtap_dump_file_write(wdh, &phdr, sizeof phdr, err))
1375
0
    return false;
1376
0
  return true;
1377
0
}
1378
1379
/*
1380
 * Pseudo-header at the beginning of DLT_PPP_WITH_DIR frames.
1381
 */
1382
struct pcap_ppp_phdr {
1383
  uint8_t direction;
1384
};
1385
1386
/*
1387
 * Pseudo-header at the beginning of DLT_PPP_WITH_DIR frames.
1388
 */
1389
static int
1390
pcap_read_ppp_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1391
    unsigned packet_size, int *err, char **err_info)
1392
0
{
1393
0
  struct pcap_ppp_phdr phdr;
1394
1395
0
  if (packet_size < sizeof (struct pcap_ppp_phdr)) {
1396
    /*
1397
     * Uh-oh, the packet isn't big enough to even
1398
     * have a pseudo-header.
1399
     */
1400
0
    *err = WTAP_ERR_BAD_FILE;
1401
0
    *err_info = ws_strdup_printf("pcap/pcapng: PPP file has a %u-byte packet, too small to have even a pseudo-header",
1402
0
        packet_size);
1403
0
    return -1;
1404
0
  }
1405
0
  if (!wtap_read_bytes(fh, &phdr, sizeof (struct pcap_ppp_phdr),
1406
0
      err, err_info))
1407
0
    return -1;
1408
  /* Any non-zero value means "sent" */
1409
0
  pseudo_header->p2p.sent = (phdr.direction != 0) ? true: false;
1410
0
  return (int)sizeof (struct pcap_ppp_phdr);
1411
0
}
1412
1413
static bool
1414
pcap_write_ppp_pseudoheader(wtap_dumper *wdh,
1415
    const union wtap_pseudo_header *pseudo_header, int *err)
1416
0
{
1417
0
  struct pcap_ppp_phdr ppp_hdr;
1418
1419
  /* Any non-zero value means "sent" */
1420
0
  ppp_hdr.direction = (pseudo_header->p2p.sent ? 1 : 0);
1421
0
  if (!wtap_dump_file_write(wdh, &ppp_hdr, sizeof ppp_hdr, err))
1422
0
    return false;
1423
0
  return true;
1424
0
}
1425
1426
static int
1427
pcap_read_erf_pseudoheader(FILE_T fh, wtap_rec *rec,
1428
    union wtap_pseudo_header *pseudo_header, unsigned packet_size,
1429
    int *err, char **err_info)
1430
0
{
1431
0
  uint8_t erf_hdr[sizeof(struct erf_phdr)];
1432
0
  uint8_t erf_subhdr[sizeof(union erf_subhdr)];
1433
0
  int phdr_len;
1434
1435
0
  if (packet_size < sizeof(struct erf_phdr)) {
1436
    /*
1437
     * Uh-oh, the packet isn't big enough to even
1438
     * have a pseudo-header.
1439
     */
1440
0
    *err = WTAP_ERR_BAD_FILE;
1441
0
    *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a %u-byte packet, too small to have even an ERF pseudo-header",
1442
0
        packet_size);
1443
0
    return -1;
1444
0
  }
1445
0
  if (!wtap_read_bytes(fh, erf_hdr, sizeof(struct erf_phdr), err, err_info))
1446
0
    return -1;
1447
0
  phdr_len = (int)sizeof(struct erf_phdr);
1448
0
  pseudo_header->erf.phdr.ts = pletohu64(&erf_hdr[0]); /* timestamp */
1449
0
  pseudo_header->erf.phdr.type = erf_hdr[8];
1450
0
  pseudo_header->erf.phdr.flags = erf_hdr[9];
1451
0
  pseudo_header->erf.phdr.rlen = pntohu16(&erf_hdr[10]);
1452
0
  pseudo_header->erf.phdr.lctr = pntohu16(&erf_hdr[12]);
1453
0
  pseudo_header->erf.phdr.wlen = pntohu16(&erf_hdr[14]);
1454
1455
  /* The high 32 bits of the timestamp contain the integer number of seconds
1456
   * while the lower 32 bits contain the binary fraction of the second.
1457
   * This allows an ultimate resolution of 1/(2^32) seconds, or approximately 233 picoseconds */
1458
0
  if (rec) {
1459
0
    uint64_t ts = pseudo_header->erf.phdr.ts;
1460
0
    rec->ts.secs = (time_t) (ts >> 32);
1461
0
    ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
1462
0
    ts += (ts & 0x80000000) << 1; /* rounding */
1463
0
    rec->ts.nsecs = ((uint32_t) (ts >> 32));
1464
0
    if (rec->ts.nsecs >= 1000000000) {
1465
0
      rec->ts.nsecs -= 1000000000;
1466
0
      rec->ts.secs += 1;
1467
0
    }
1468
1469
    /*
1470
     * This time stamp came from the ERF header, not from the
1471
     * pcap packet header or pcapng block header, so its
1472
     * precision is that of ERF time stamps, not the pcap
1473
     * file's time stamp or the pcapng interface's time
1474
     * stamp.
1475
     */
1476
0
    rec->tsprec = WTAP_TSPREC_NSEC;
1477
0
  }
1478
1479
  /*
1480
   * If the type of record given in the pseudo header indicates
1481
   * the presence of an extension header, then read all the
1482
   * extension headers.
1483
   */
1484
0
  if (pseudo_header->erf.phdr.type & 0x80) {
1485
0
    int i = 0, max = array_length(pseudo_header->erf.ehdr_list);
1486
0
    uint8_t erf_exhdr[8];
1487
0
    uint8_t type;
1488
1489
0
    do {
1490
0
      if (phdr_len > INT_MAX - 8) {
1491
0
        *err = WTAP_ERR_BAD_FILE;
1492
0
        *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a packet larger than %d bytes",
1493
0
            INT_MAX);
1494
0
        return -1;
1495
0
      }
1496
0
      if (packet_size < (unsigned)phdr_len + 8) {
1497
0
        *err = WTAP_ERR_BAD_FILE;
1498
0
        *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a %u-byte packet, too small to include the extension headers",
1499
0
            packet_size);
1500
0
        return -1;
1501
0
      }
1502
0
      if (!wtap_read_bytes(fh, erf_exhdr, 8, err, err_info))
1503
0
        return -1;
1504
0
      type = erf_exhdr[0];
1505
0
      if (i < max) {
1506
0
        uint64_t erf_exhdr_sw;
1507
1508
0
        erf_exhdr_sw = pntohu64(erf_exhdr);
1509
0
        memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
1510
0
      }
1511
0
      phdr_len += 8;
1512
0
      i++;
1513
0
    } while (type & 0x80);
1514
0
  }
1515
1516
  /* check the optional subheader */
1517
0
  switch (pseudo_header->erf.phdr.type & 0x7F) {
1518
0
  case ERF_TYPE_MC_HDLC:
1519
0
  case ERF_TYPE_MC_RAW:
1520
0
  case ERF_TYPE_MC_ATM:
1521
0
  case ERF_TYPE_MC_RAW_CHANNEL:
1522
0
  case ERF_TYPE_MC_AAL5:
1523
0
  case ERF_TYPE_MC_AAL2:
1524
0
  case ERF_TYPE_COLOR_MC_HDLC_POS:
1525
    /* Extract the Multi Channel header to include it in the pseudo header part */
1526
0
    if (phdr_len > INT_MAX - (int)sizeof(erf_mc_header_t)) {
1527
0
      *err = WTAP_ERR_BAD_FILE;
1528
0
      *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a packet larger than %d bytes",
1529
0
          INT_MAX);
1530
0
      return -1;
1531
0
    }
1532
0
    if (packet_size < (unsigned)(phdr_len + (int)sizeof(erf_mc_header_t))) {
1533
0
      *err = WTAP_ERR_BAD_FILE;
1534
0
      *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a %u-byte packet, too small to include the Multi Channel header",
1535
0
          packet_size);
1536
0
      return -1;
1537
0
    }
1538
0
    if (!wtap_read_bytes(fh, erf_subhdr, sizeof(erf_mc_header_t), err, err_info))
1539
0
      return -1;
1540
0
    pseudo_header->erf.subhdr.mc_hdr = pntohu32(&erf_subhdr[0]);
1541
0
    phdr_len += sizeof(erf_mc_header_t);
1542
0
    break;
1543
0
  case ERF_TYPE_AAL2:
1544
    /* Extract the AAL2 header to include it in the pseudo header part */
1545
0
    if (phdr_len > INT_MAX - (int)sizeof(erf_aal2_header_t)) {
1546
0
      *err = WTAP_ERR_BAD_FILE;
1547
0
      *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a packet larger than %d bytes",
1548
0
          INT_MAX);
1549
0
      return -1;
1550
0
    }
1551
0
    if (packet_size < (unsigned)(phdr_len + (int)sizeof(erf_aal2_header_t))) {
1552
0
      *err = WTAP_ERR_BAD_FILE;
1553
0
      *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a %u-byte packet, too small to include the AAL2 header",
1554
0
          packet_size);
1555
0
      return -1;
1556
0
    }
1557
0
    if (!wtap_read_bytes(fh, erf_subhdr, sizeof(erf_aal2_header_t), err, err_info))
1558
0
      return -1;
1559
0
    pseudo_header->erf.subhdr.aal2_hdr = pntohu32(&erf_subhdr[0]);
1560
0
    phdr_len += sizeof(erf_aal2_header_t);
1561
0
    break;
1562
0
  case ERF_TYPE_ETH:
1563
0
  case ERF_TYPE_COLOR_ETH:
1564
0
  case ERF_TYPE_DSM_COLOR_ETH:
1565
0
  case ERF_TYPE_COLOR_HASH_ETH:
1566
    /* Extract the Ethernet additional header to include it in the pseudo header part */
1567
0
    if (phdr_len > INT_MAX - (int)sizeof(erf_eth_header_t)) {
1568
0
      *err = WTAP_ERR_BAD_FILE;
1569
0
      *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a packet larger than %d bytes",
1570
0
          INT_MAX);
1571
0
      return -1;
1572
0
    }
1573
0
    if (packet_size < (unsigned)(phdr_len + (int)sizeof(erf_eth_header_t))) {
1574
0
      *err = WTAP_ERR_BAD_FILE;
1575
0
      *err_info = ws_strdup_printf("pcap/pcapng: ERF file has a %u-byte packet, too small to include the Ethernet additional header",
1576
0
          packet_size);
1577
0
      return -1;
1578
0
    }
1579
0
    if (!wtap_read_bytes(fh, erf_subhdr, sizeof(erf_eth_header_t), err, err_info))
1580
0
      return -1;
1581
0
    memcpy(&pseudo_header->erf.subhdr.eth_hdr, erf_subhdr, sizeof pseudo_header->erf.subhdr.eth_hdr);
1582
0
    phdr_len += sizeof(erf_eth_header_t);
1583
0
    break;
1584
0
  default:
1585
    /* No optional pseudo header for this ERF type */
1586
0
    break;
1587
0
  }
1588
0
  return phdr_len;
1589
0
}
1590
1591
static bool
1592
pcap_write_erf_pseudoheader(wtap_dumper *wdh,
1593
    const union wtap_pseudo_header *pseudo_header, int *err)
1594
0
{
1595
0
  uint8_t erf_hdr[sizeof(struct erf_phdr)];
1596
0
  uint8_t erf_subhdr[sizeof(union erf_subhdr)];
1597
1598
  /*
1599
   * Write the ERF header.
1600
   */
1601
0
  memset(&erf_hdr, 0, sizeof(erf_hdr));
1602
0
  phtoleu64(&erf_hdr[0], pseudo_header->erf.phdr.ts);
1603
0
  erf_hdr[8] = pseudo_header->erf.phdr.type;
1604
0
  erf_hdr[9] = pseudo_header->erf.phdr.flags;
1605
1606
  /*
1607
   * Recalculate rlen as padding (and maybe extension headers)
1608
   * have been stripped from caplen.
1609
   *
1610
   * XXX: Since we don't have rec->rec_header.packet_header.caplen
1611
   * here, assume caplen was calculated correctly and
1612
   * recalculate from wlen.
1613
   */
1614
0
  phtonu16(&erf_hdr[10],
1615
0
      MIN(pseudo_header->erf.phdr.rlen, pseudo_header->erf.phdr.wlen + pcap_get_phdr_size(WTAP_ENCAP_ERF, pseudo_header)));
1616
1617
0
  phtonu16(&erf_hdr[12], pseudo_header->erf.phdr.lctr);
1618
0
  phtonu16(&erf_hdr[14], pseudo_header->erf.phdr.wlen);
1619
0
  if (!wtap_dump_file_write(wdh, erf_hdr,  sizeof(struct erf_phdr), err))
1620
0
    return false;
1621
1622
  /*
1623
   * Now write out the extension headers.
1624
   */
1625
0
  if (pseudo_header->erf.phdr.type & 0x80) {
1626
0
    int i = 0, max = array_length(pseudo_header->erf.ehdr_list);
1627
0
    uint8_t erf_exhdr[8];
1628
0
    uint8_t type;
1629
1630
0
    do {
1631
0
      phtonu64(erf_exhdr, pseudo_header->erf.ehdr_list[i].ehdr);
1632
0
      type = erf_exhdr[0];
1633
      /* Clear more extension headers bit if > 8 */
1634
0
      if(i == max-1)
1635
0
        erf_exhdr[0] = erf_exhdr[0] & 0x7F;
1636
0
      if (!wtap_dump_file_write(wdh, erf_exhdr, 8, err))
1637
0
        return false;
1638
0
      i++;
1639
0
    } while (type & 0x80 && i < max);
1640
0
  }
1641
1642
  /*
1643
   * Now write out the subheader, if any
1644
   */
1645
0
  switch (pseudo_header->erf.phdr.type & 0x7F) {
1646
0
  case ERF_TYPE_MC_HDLC:
1647
0
  case ERF_TYPE_MC_RAW:
1648
0
  case ERF_TYPE_MC_ATM:
1649
0
  case ERF_TYPE_MC_RAW_CHANNEL:
1650
0
  case ERF_TYPE_MC_AAL5:
1651
0
  case ERF_TYPE_MC_AAL2:
1652
0
  case ERF_TYPE_COLOR_MC_HDLC_POS:
1653
0
    phtonu32(&erf_subhdr[0], pseudo_header->erf.subhdr.mc_hdr);
1654
0
    if (!wtap_dump_file_write(wdh, erf_subhdr,
1655
0
        sizeof(struct erf_mc_hdr), err))
1656
0
      return false;
1657
0
    break;
1658
0
  case ERF_TYPE_AAL2:
1659
0
    phtonu32(&erf_subhdr[0], pseudo_header->erf.subhdr.aal2_hdr);
1660
0
    if (!wtap_dump_file_write(wdh, erf_subhdr,
1661
0
        sizeof(struct erf_aal2_hdr), err))
1662
0
      return false;
1663
0
    break;
1664
0
  case ERF_TYPE_ETH:
1665
0
  case ERF_TYPE_COLOR_ETH:
1666
0
  case ERF_TYPE_DSM_COLOR_ETH:
1667
0
  case ERF_TYPE_COLOR_HASH_ETH:
1668
0
    memcpy(&erf_subhdr[0], &pseudo_header->erf.subhdr.eth_hdr, sizeof pseudo_header->erf.subhdr.eth_hdr);
1669
0
    if (!wtap_dump_file_write(wdh, erf_subhdr,
1670
0
        sizeof(struct erf_eth_hdr), err))
1671
0
      return false;
1672
0
    break;
1673
0
  default:
1674
0
    break;
1675
0
  }
1676
0
  return true;
1677
0
}
1678
1679
/*
1680
 * I2C-with=Linux-pseudoheader link-layer on-disk format, as defined by
1681
 * Pigeon Point Systems.
1682
 */
1683
struct i2c_linux_file_hdr {
1684
  uint8_t bus;
1685
  uint8_t flags[4];
1686
};
1687
1688
static int
1689
pcap_read_i2c_linux_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
1690
    unsigned packet_size, int *err, char **err_info)
1691
0
{
1692
0
  struct i2c_linux_file_hdr i2c_linux_hdr;
1693
1694
0
  if (packet_size < sizeof (struct i2c_linux_file_hdr)) {
1695
    /*
1696
     * Uh-oh, the packet isn't big enough to even
1697
     * have a pseudo-header.
1698
     */
1699
0
    *err = WTAP_ERR_BAD_FILE;
1700
0
    *err_info = ws_strdup_printf("pcap/pcapng: I2C file has a %u-byte packet, too small to have even a I2C pseudo-header",
1701
0
        packet_size);
1702
0
    return -1;
1703
0
  }
1704
0
  if (!wtap_read_bytes(fh, &i2c_linux_hdr, sizeof (i2c_linux_hdr), err, err_info))
1705
0
    return -1;
1706
1707
0
  pseudo_header->i2c.is_event = i2c_linux_hdr.bus & 0x80 ? 1 : 0;
1708
0
  pseudo_header->i2c.bus = i2c_linux_hdr.bus & 0x7f;
1709
0
  pseudo_header->i2c.flags = pntohu32(&i2c_linux_hdr.flags);
1710
1711
0
  return (int)sizeof (struct i2c_linux_file_hdr);
1712
0
}
1713
1714
static bool
1715
pcap_write_i2c_linux_pseudoheader(wtap_dumper *wdh,
1716
    const union wtap_pseudo_header *pseudo_header, int *err)
1717
0
{
1718
0
  struct i2c_linux_file_hdr i2c_linux_hdr;
1719
1720
  /*
1721
   * Write the I2C Linux-specific pseudo-header.
1722
   */
1723
0
  memset(&i2c_linux_hdr, 0, sizeof(i2c_linux_hdr));
1724
0
  i2c_linux_hdr.bus = pseudo_header->i2c.bus |
1725
0
      (pseudo_header->i2c.is_event ? 0x80 : 0x00);
1726
0
  phtonu32((uint8_t *)&i2c_linux_hdr.flags, pseudo_header->i2c.flags);
1727
0
  if (!wtap_dump_file_write(wdh, &i2c_linux_hdr, sizeof(i2c_linux_hdr), err))
1728
0
    return false;
1729
0
  return true;
1730
0
}
1731
1732
/*
1733
 * The link-layer header on Nokia IPSO packets.
1734
 */
1735
0
#define NOKIA_LEN 4  /* length of the header */
1736
1737
static bool
1738
pcap_read_nokia_pseudoheader(FILE_T fh,
1739
    union wtap_pseudo_header *pseudo_header, int *err, char **err_info)
1740
0
{
1741
0
  uint8_t phdr[NOKIA_LEN];
1742
1743
1744
  /* backtrack to read the 4 mysterious bytes that aren't considered
1745
  * part of the packet size
1746
  */
1747
0
  if (file_seek(fh, -NOKIA_LEN, SEEK_CUR, err) == -1)
1748
0
  {
1749
0
    *err = file_error(fh, err_info);
1750
0
    if (*err == 0)
1751
0
      *err = WTAP_ERR_SHORT_READ;
1752
0
    return false;
1753
0
  }
1754
1755
0
  if (!wtap_read_bytes(fh, phdr, NOKIA_LEN, err, err_info))
1756
0
    return false;
1757
1758
0
  memcpy(pseudo_header->nokia.stuff, phdr, NOKIA_LEN);
1759
1760
0
  return true;
1761
0
}
1762
1763
/*
1764
 * When not using the memory-mapped interface to capture USB events,
1765
 * code that reads those events can use the MON_IOCX_GET ioctl to
1766
 * read a 48-byte header consisting of a "struct linux_usb_phdr", as
1767
 * defined below, followed immediately by one of:
1768
 *
1769
 *  8 bytes of a "struct usb_device_setup_hdr", if "setup_flag"
1770
 *  in the preceding "struct linux_usb_phdr" is 0;
1771
 *
1772
 *  in Linux 2.6.30 or later, 8 bytes of a "struct iso_rec", if
1773
 *  this is an isochronous transfer;
1774
 *
1775
 *  8 bytes of junk, otherwise.
1776
 *
1777
 * In Linux 2.6.31 and later, it can also use the MON_IOCX_GETX ioctl
1778
 * to read a 64-byte header; that header consists of the 48 bytes
1779
 * above, followed immediately by 16 bytes of a "struct linux_usb_phdr_ext",
1780
 * as defined below.
1781
 *
1782
 * In Linux 2.6.21 and later, there's a memory-mapped interface to
1783
 * capture USB events.  In that interface, the events in the memory-mapped
1784
 * buffer have a 64-byte header, followed immediately by the data.
1785
 * In Linux 2.6.21 through 2.6.30.x, the 64-byte header is the 48-byte
1786
 * header described above, followed by 16 bytes of zeroes; in Linux
1787
 * 2.6.31 and later, the 64-byte header is the 64-byte header described
1788
 * above.
1789
 *
1790
 * See linux/Documentation/usb/usbmon.txt and libpcap/pcap/usb.h for details.
1791
 *
1792
 * With WTAP_ENCAP_USB_LINUX, packets have the 48-byte header; with
1793
 * WTAP_ENCAP_USB_LINUX_MMAPPED, they have the 64-byte header.  There
1794
 * is no indication of whether the header has the "struct iso_rec", or
1795
 * whether the last 16 bytes of a 64-byte header are all zeros or are
1796
 * a "struct linux_usb_phdr_ext".
1797
 */
1798
1799
/*
1800
 * URB transfer_type values
1801
 */
1802
#define URB_ISOCHRONOUS   0x0
1803
#define URB_INTERRUPT     0x1
1804
#define URB_CONTROL       0x2
1805
#define URB_BULK          0x3
1806
1807
/*
1808
 * Information from the URB for Isochronous transfers.
1809
 *
1810
 * This structure is 8 bytes long.
1811
 */
1812
struct iso_rec {
1813
  int32_t error_count;
1814
  int32_t numdesc;
1815
};
1816
1817
/*
1818
 * Header prepended by Linux kernel to each USB event.
1819
 *
1820
 * (Setup flag is '-', 'D', 'Z', or 0.  Data flag is '<', '>', 'Z', or 0.)
1821
 *
1822
 * The values are in *host* byte order.
1823
 */
1824
struct linux_usb_phdr {
1825
  uint64_t id;             /* urb id, to link submission and completion events */
1826
  uint8_t event_type;      /* Submit ('S'), Completed ('C'), Error ('E') */
1827
  uint8_t transfer_type;   /* ISO (0), Intr, Control, Bulk (3) */
1828
  uint8_t endpoint_number; /* Endpoint number (0-15) and transfer direction */
1829
  uint8_t device_address;  /* 0-127 */
1830
  uint16_t bus_id;
1831
  int8_t setup_flag;       /* 0, if the urb setup header is meaningful */
1832
  int8_t data_flag;        /* 0, if urb data is present */
1833
  int64_t ts_sec;
1834
  int32_t ts_usec;
1835
  int32_t status;
1836
  uint32_t urb_len;        /* whole len of urb this event refers to */
1837
  uint32_t data_len;       /* amount of urb data really present in this event */
1838
1839
  /*
1840
   * Packet-type-dependent data.
1841
   * USB setup information of setup_flag is true.
1842
   * Otherwise, some isochronous transfer information.
1843
   */
1844
  union {
1845
    uint8_t data[8];
1846
    struct iso_rec iso;
1847
  } s;
1848
1849
  /*
1850
   * This data is provided by Linux 2.6.31 and later kernels.
1851
   *
1852
   * For WTAP_ENCAP_USB_LINUX, it's not in the pseudo-header, so
1853
   * the pseudo-header is always 48 bytes long, including the
1854
   * packet-type-dependent data.
1855
   *
1856
   * For WTAP_ENCAP_USB_LINUX_MMAPPED, the pseudo-header is always
1857
   * 64 bytes long, with the packet-type-dependent data preceding
1858
   * these last 16 bytes.  In pre-2.6.31 kernels, it's zero padding;
1859
   * in 2.6.31 and later, it's the following data.
1860
   */
1861
  int32_t interval;    /* only for Interrupt and Isochronous events */
1862
  int32_t start_frame; /* for Isochronous */
1863
  uint32_t xfer_flags; /* copy of URB's transfer_flags */
1864
  uint32_t ndesc;      /* actual number of isochronous descriptors */
1865
};
1866
1867
/*
1868
 * event_type values
1869
 */
1870
#define URB_SUBMIT        'S'
1871
0
#define URB_COMPLETE      'C'
1872
#define URB_ERROR         'E'
1873
1874
/*
1875
 * URB transfer_type values
1876
 */
1877
0
#define URB_ISOCHRONOUS   0x0
1878
#define URB_INTERRUPT     0x1
1879
#define URB_CONTROL       0x2
1880
#define URB_BULK          0x3
1881
#define URB_UNKNOWN       0xFF
1882
1883
0
#define URB_TRANSFER_IN   0x80    /* to host */
1884
1885
struct linux_usb_isodesc {
1886
  int32_t iso_status;
1887
  uint32_t iso_off;
1888
  uint32_t iso_len;
1889
  uint32_t _pad;
1890
};
1891
1892
/*
1893
 * USB setup header as defined in USB specification
1894
 * See usb_20.pdf, Chapter 9.3 'USB Device Requests' for details.
1895
 * https://www.usb.org/document-library/usb-20-specification
1896
 *
1897
 * This structure is 8 bytes long.
1898
 */
1899
struct usb_device_setup_hdr {
1900
  int8_t bmRequestType;
1901
  uint8_t bRequest;
1902
  uint16_t wValue;
1903
  uint16_t wIndex;
1904
  uint16_t wLength;
1905
};
1906
1907
/*
1908
 * Offset of the *end* of a field within a particular structure.
1909
 */
1910
#define END_OFFSETOF(basep, fieldp) \
1911
0
  (((char *)(void *)(fieldp)) - ((char *)(void *)(basep)) + \
1912
0
      sizeof(*fieldp))
1913
1914
/*
1915
 * Is that offset within the bounds of the packet?
1916
 */
1917
#define WITHIN_PACKET(basep, fieldp) \
1918
0
  (packet_size >= END_OFFSETOF((basep), (fieldp)))
1919
1920
#define CHECK_AND_SWAP16(fieldp) \
1921
0
  { \
1922
0
    if (!WITHIN_PACKET(usb_phdr, fieldp)) \
1923
0
      return; \
1924
0
    PBSWAP16((uint8_t *)fieldp); \
1925
0
  }
1926
1927
#define CHECK_AND_SWAP32(fieldp) \
1928
0
  { \
1929
0
    if (!WITHIN_PACKET(usb_phdr, fieldp)) \
1930
0
      return; \
1931
0
    PBSWAP32((uint8_t *)fieldp); \
1932
0
  }
1933
1934
#define CHECK_AND_SWAP64(fieldp) \
1935
0
  { \
1936
0
    if (!WITHIN_PACKET(usb_phdr, fieldp)) \
1937
0
      return; \
1938
0
    PBSWAP64((uint8_t *)fieldp); \
1939
0
  }
1940
1941
/*
1942
 * Offset and length of the CAN ID field in the CAN classic/CAN FD
1943
 * SocketCAN header.
1944
 */
1945
0
#define CAN_CANFD_CAN_ID_OFFSET   0
1946
0
#define CAN_CANFD_CAN_ID_LEN    4
1947
1948
/*
1949
 * Offsets and lengths of fields in the CAN XL SocketCAN header.
1950
 */
1951
0
#define CANXL_PRIORITY_VCID_OFFSET  0
1952
0
#define CANXL_PRIORITY_VCID_LEN   4
1953
0
#define CANXL_FLAGS_OFFSET    (CANXL_PRIORITY_VCID_OFFSET + CANXL_PRIORITY_VCID_LEN)
1954
0
#define CANXL_FLAGS_LEN     1
1955
0
#define CANXL_SDU_TYPE_OFFSET   (CANXL_FLAGS_OFFSET + CANXL_FLAGS_LEN)
1956
0
#define CANXL_SDU_TYPE_LEN    1
1957
0
#define CANXL_PAYLOAD_LENGTH_OFFSET (CANXL_SDU_TYPE_OFFSET + CANXL_SDU_TYPE_LEN)
1958
0
#define CANXL_PAYLOAD_LENGTH_LEN  2
1959
0
#define CANXL_ACCEPTANCE_FIELD_OFFSET (CANXL_PAYLOAD_LENGTH_OFFSET + CANXL_PAYLOAD_LENGTH_LEN)
1960
0
#define CANXL_ACCEPTANCE_FIELD_LEN  4
1961
1962
/*
1963
 * CAN fake link-layer headers in Linux cooked packets.
1964
 */
1965
0
#define LINUX_SLL_PROTOCOL_OFFSET 14  /* protocol */
1966
0
#define LINUX_SLL_LEN     16  /* length of the header */
1967
1968
0
#define LINUX_SLL2_PROTOCOL_OFFSET  0  /* protocol */
1969
0
#define LINUX_SLL2_LEN      20  /* length of the header */
1970
1971
/*
1972
 * The protocols we have to check for.
1973
 */
1974
0
#define LINUX_SLL_P_CAN     0x000C  /* Controller Area Network classic */
1975
0
#define LINUX_SLL_P_CANFD   0x000D  /* Controller Area Network flexible data rate */
1976
0
#define LINUX_SLL_P_CANXL   0x000E  /* Controller Area Network extended length */
1977
1978
static void
1979
pcap_byteswap_can_socketcan_pseudoheader(unsigned packet_size, uint16_t protocol,
1980
    uint8_t *pd)
1981
0
{
1982
0
  switch (protocol) {
1983
1984
0
  case LINUX_SLL_P_CAN:
1985
0
  case LINUX_SLL_P_CANFD:
1986
    /*
1987
     * CAN classic or CAN FD; byte-swap the ID/flags field
1988
     * into our host byte order.
1989
     *
1990
     * Make sure we have the entire field.
1991
     */
1992
0
    if (packet_size < (CAN_CANFD_CAN_ID_OFFSET + CAN_CANFD_CAN_ID_LEN)) {
1993
      /* Not enough data to have the full CAN ID */
1994
0
      return;
1995
0
    }
1996
0
    PBSWAP32(&pd[CAN_CANFD_CAN_ID_OFFSET]);
1997
0
    break;
1998
1999
0
  case LINUX_SLL_P_CANXL:
2000
    /*
2001
     * CAN classic or CAN FD; byte-swap the priority-and-VCID
2002
     * field, the payload length, ad the acceptance field
2003
     * into our host byte order.
2004
     */
2005
0
    if (packet_size < (CANXL_PRIORITY_VCID_OFFSET + CANXL_PRIORITY_VCID_LEN)) {
2006
      /* Not enough data to have the full priority/VCID field */
2007
0
      return;
2008
0
    }
2009
0
    PBSWAP32(&pd[CANXL_PRIORITY_VCID_OFFSET]);
2010
0
    if (packet_size < (CANXL_PAYLOAD_LENGTH_OFFSET + CANXL_PAYLOAD_LENGTH_LEN)) {
2011
      /* Not enough data to have the full payload length field */
2012
0
      return;
2013
0
    }
2014
0
    PBSWAP16(&pd[CANXL_PAYLOAD_LENGTH_OFFSET]);
2015
0
    if (packet_size < (CANXL_ACCEPTANCE_FIELD_OFFSET + CANXL_ACCEPTANCE_FIELD_LEN)) {
2016
      /* Not enough data to have the full payload length field */
2017
0
      return;
2018
0
    }
2019
0
    PBSWAP32(&pd[CANXL_ACCEPTANCE_FIELD_OFFSET]);
2020
0
    break;
2021
2022
0
  default:
2023
    /* Not a CAN packet; nothing to fix */
2024
0
    return;
2025
0
  }
2026
0
}
2027
2028
static void
2029
pcap_byteswap_linux_sll_pseudoheader(wtap_rec *rec)
2030
0
{
2031
0
  uint8_t *pd;
2032
0
  unsigned packet_size;
2033
0
  uint16_t protocol;
2034
2035
0
  pd = ws_buffer_start_ptr(&rec->data);
2036
2037
  /*
2038
   * Minimum of captured and actual length (just in case the
2039
   * actual length < the captured length, which Should Never
2040
   * Happen).
2041
   */
2042
0
  packet_size = rec->rec_header.packet_header.caplen;
2043
0
  if (packet_size > rec->rec_header.packet_header.len)
2044
0
    packet_size = rec->rec_header.packet_header.len;
2045
2046
0
  if (packet_size < LINUX_SLL_LEN) {
2047
    /* Not enough data to have the protocol */
2048
0
    return;
2049
0
  }
2050
2051
  /*
2052
   * Byte-swap the SocketCAN pseudoheader, if we have one.
2053
   */
2054
0
  protocol = pntohu16(&pd[LINUX_SLL_PROTOCOL_OFFSET]);
2055
0
  pcap_byteswap_can_socketcan_pseudoheader(packet_size - LINUX_SLL_LEN,
2056
0
      protocol, pd + LINUX_SLL_LEN);
2057
0
}
2058
2059
static void
2060
pcap_byteswap_linux_sll2_pseudoheader(wtap_rec *rec)
2061
0
{
2062
0
  uint8_t *pd;
2063
0
  unsigned packet_size;
2064
0
  uint16_t protocol;
2065
2066
0
  pd = ws_buffer_start_ptr(&rec->data);
2067
2068
  /*
2069
   * Minimum of captured and actual length (just in case the
2070
   * actual length < the captured length, which Should Never
2071
   * Happen).
2072
   */
2073
0
  packet_size = rec->rec_header.packet_header.caplen;
2074
0
  if (packet_size > rec->rec_header.packet_header.len)
2075
0
    packet_size = rec->rec_header.packet_header.len;
2076
2077
0
  if (packet_size < LINUX_SLL2_LEN) {
2078
    /* Not enough data to have the protocol */
2079
0
    return;
2080
0
  }
2081
2082
  /*
2083
   * Byte-swap the SocketCAN pseudoheader, if we have one.
2084
   */
2085
0
  protocol = pntohu16(&pd[LINUX_SLL2_PROTOCOL_OFFSET]);
2086
0
  pcap_byteswap_can_socketcan_pseudoheader(packet_size - LINUX_SLL2_LEN,
2087
0
      protocol, pd + LINUX_SLL2_LEN);
2088
0
}
2089
2090
static void
2091
pcap_byteswap_linux_usb_pseudoheader(wtap_rec *rec, bool header_len_64_bytes)
2092
0
{
2093
0
  uint8_t *pd;
2094
0
  unsigned packet_size;
2095
0
  struct linux_usb_phdr *usb_phdr;
2096
0
  struct linux_usb_isodesc *pisodesc;
2097
0
  int32_t iso_numdesc, i;
2098
2099
0
  pd = ws_buffer_start_ptr(&rec->data);
2100
2101
  /*
2102
   * Minimum of captured and actual length (just in case the
2103
   * actual length < the captured length, which Should Never
2104
   * Happen).
2105
   */
2106
0
  packet_size = rec->rec_header.packet_header.caplen;
2107
0
  if (packet_size > rec->rec_header.packet_header.len)
2108
0
    packet_size = rec->rec_header.packet_header.len;
2109
2110
  /*
2111
   * Greasy hack, but we never directly dereference any of
2112
   * the fields in *usb_phdr, we just get offsets of and
2113
   * addresses of its members and byte-swap it with a
2114
   * byte-at-a-time macro, so it's alignment-safe.
2115
   */
2116
0
  usb_phdr = (struct linux_usb_phdr *)(void *)pd;
2117
2118
0
  CHECK_AND_SWAP64(&usb_phdr->id);
2119
0
  CHECK_AND_SWAP16(&usb_phdr->bus_id);
2120
0
  CHECK_AND_SWAP64(&usb_phdr->ts_sec);
2121
0
  CHECK_AND_SWAP32(&usb_phdr->ts_usec);
2122
0
  CHECK_AND_SWAP32(&usb_phdr->status);
2123
0
  CHECK_AND_SWAP32(&usb_phdr->urb_len);
2124
0
  CHECK_AND_SWAP32(&usb_phdr->data_len);
2125
2126
0
  if (usb_phdr->transfer_type == URB_ISOCHRONOUS) {
2127
0
    CHECK_AND_SWAP32(&usb_phdr->s.iso.error_count);
2128
0
    CHECK_AND_SWAP32(&usb_phdr->s.iso.numdesc);
2129
0
  }
2130
2131
0
  if (header_len_64_bytes) {
2132
    /*
2133
     * This is either the "version 1" header, with
2134
     * 16 bytes of additional fields at the end, or
2135
     * a "version 0" header from a memory-mapped
2136
     * capture, with 16 bytes of zeroed-out padding
2137
     * at the end.  Byte swap them as if this were
2138
     * a "version 1" header.
2139
     *
2140
     * Yes, the first argument to END_OFFSETOF() should
2141
     * be usb_phdr, not usb_phdr_ext; we want the offset of
2142
     * the additional fields from the beginning of
2143
     * the packet.
2144
     */
2145
0
    CHECK_AND_SWAP32(&usb_phdr->interval);
2146
0
    CHECK_AND_SWAP32(&usb_phdr->start_frame);
2147
0
    CHECK_AND_SWAP32(&usb_phdr->xfer_flags);
2148
0
    CHECK_AND_SWAP32(&usb_phdr->ndesc);
2149
0
  }
2150
2151
0
  if (usb_phdr->transfer_type == URB_ISOCHRONOUS) {
2152
    /* swap the values in struct linux_usb_isodesc */
2153
2154
    /*
2155
     * See previous "Greasy hack" comment.
2156
     */
2157
0
    if (header_len_64_bytes) {
2158
0
      pisodesc = (struct linux_usb_isodesc*)(void *)(pd + 64);
2159
0
    } else {
2160
0
      pisodesc = (struct linux_usb_isodesc*)(void *)(pd + 48);
2161
0
    }
2162
0
    iso_numdesc = usb_phdr->s.iso.numdesc;
2163
0
    for (i = 0; i < iso_numdesc; i++) {
2164
0
      CHECK_AND_SWAP32(&pisodesc->iso_status);
2165
0
      CHECK_AND_SWAP32(&pisodesc->iso_off);
2166
0
      CHECK_AND_SWAP32(&pisodesc->iso_len);
2167
0
      CHECK_AND_SWAP32(&pisodesc->_pad);
2168
2169
0
      pisodesc++;
2170
0
    }
2171
0
  }
2172
0
}
2173
2174
struct nflog_hdr {
2175
  uint8_t nflog_family;   /* address family */
2176
  uint8_t nflog_version;    /* version */
2177
  uint16_t nflog_rid;   /* resource ID */
2178
};
2179
2180
struct nflog_tlv {
2181
  uint16_t tlv_length;    /* tlv length */
2182
  uint16_t tlv_type;    /* tlv type */
2183
  /* value follows this */
2184
};
2185
2186
static void
2187
pcap_byteswap_nflog_pseudoheader(wtap_rec *rec)
2188
0
{
2189
0
  uint8_t *pd;
2190
0
  unsigned packet_size;
2191
0
  uint8_t *p;
2192
0
  struct nflog_hdr *nfhdr;
2193
0
  struct nflog_tlv *tlv;
2194
0
  unsigned size;
2195
2196
0
  pd = ws_buffer_start_ptr(&rec->data);
2197
2198
  /*
2199
   * Minimum of captured and actual length (just in case the
2200
   * actual length < the captured length, which Should Never
2201
   * Happen).
2202
   */
2203
0
  packet_size = rec->rec_header.packet_header.caplen;
2204
0
  if (packet_size > rec->rec_header.packet_header.len)
2205
0
    packet_size = rec->rec_header.packet_header.len;
2206
2207
0
  if (packet_size < sizeof(struct nflog_hdr)) {
2208
    /* Not enough data to have any TLVs. */
2209
0
    return;
2210
0
  }
2211
2212
0
  p = pd;
2213
0
  nfhdr = (struct nflog_hdr *)pd;
2214
0
  if (nfhdr->nflog_version != 0) {
2215
    /* Unknown NFLOG version */
2216
0
    return;
2217
0
  }
2218
2219
0
  packet_size -= (unsigned)sizeof(struct nflog_hdr);
2220
0
  p += sizeof(struct nflog_hdr);
2221
2222
0
  while (packet_size >= sizeof(struct nflog_tlv)) {
2223
0
    tlv = (struct nflog_tlv *) p;
2224
2225
    /* Swap the type and length. */
2226
0
    PBSWAP16((uint8_t *)&tlv->tlv_type);
2227
0
    PBSWAP16((uint8_t *)&tlv->tlv_length);
2228
2229
    /* Get the length of the TLV. */
2230
0
    size = tlv->tlv_length;
2231
0
    size = WS_ROUNDUP_4(size);
2232
2233
    /* Is the TLV's length less than the minimum? */
2234
0
    if (size < sizeof(struct nflog_tlv)) {
2235
      /* Yes. Give up now. */
2236
0
      return;
2237
0
    }
2238
2239
    /* Do we have enough data for the full TLV? */
2240
0
    if (packet_size < size) {
2241
      /* No. */
2242
0
      return;
2243
0
    }
2244
2245
    /* Skip over the TLV. */
2246
0
    packet_size -= size;
2247
0
    p += size;
2248
0
  }
2249
0
}
2250
2251
/*
2252
 * pflog headers, at least as they exist now.
2253
 */
2254
#define PFLOG_IFNAMSIZ    16
2255
#define PFLOG_RULESET_NAME_SIZE 16
2256
2257
struct pfloghdr {
2258
  uint8_t   length;
2259
  uint8_t   af;
2260
  uint8_t   action;
2261
  uint8_t   reason;
2262
  char    ifname[PFLOG_IFNAMSIZ];
2263
  char    ruleset[PFLOG_RULESET_NAME_SIZE];
2264
  uint32_t    rulenr;
2265
  uint32_t    subrulenr;
2266
  uint32_t    uid;
2267
  int32_t   pid;
2268
  uint32_t    rule_uid;
2269
  int32_t   rule_pid;
2270
  uint8_t   dir;
2271
  /* More follows, depending on the header length */
2272
};
2273
2274
static void
2275
pcap_byteswap_pflog_pseudoheader(wtap_rec *rec)
2276
0
{
2277
0
  uint8_t *pd;
2278
0
  unsigned packet_size;
2279
0
  struct pfloghdr *pflhdr;
2280
2281
0
  pd = ws_buffer_start_ptr(&rec->data);
2282
2283
  /*
2284
   * Minimum of captured and actual length (just in case the
2285
   * actual length < the captured length, which Should Never
2286
   * Happen).
2287
   */
2288
0
  packet_size = rec->rec_header.packet_header.caplen;
2289
0
  if (packet_size > rec->rec_header.packet_header.len)
2290
0
    packet_size = rec->rec_header.packet_header.len;
2291
2292
0
  if (packet_size < sizeof(struct pfloghdr)) {
2293
    /* Not enough data to have the UID and PID fields */
2294
0
    return;
2295
0
  }
2296
2297
0
  pflhdr = (struct pfloghdr *)pd;
2298
0
  if (pflhdr->length < (unsigned) (offsetof(struct pfloghdr, rule_pid) + sizeof pflhdr->rule_pid)) {
2299
    /* Header doesn't include the UID and PID fields */
2300
0
    return;
2301
0
  }
2302
0
  PBSWAP32((uint8_t *)&pflhdr->uid);
2303
0
  PBSWAP32((uint8_t *)&pflhdr->pid);
2304
0
  PBSWAP32((uint8_t *)&pflhdr->rule_uid);
2305
0
  PBSWAP32((uint8_t *)&pflhdr->rule_pid);
2306
0
}
2307
2308
int
2309
pcap_process_pseudo_header(FILE_T fh, bool is_nokia, int wtap_encap,
2310
    unsigned packet_size, wtap_rec *rec, int *err, char **err_info)
2311
0
{
2312
0
  int phdr_len = 0;
2313
2314
0
  switch (wtap_encap) {
2315
2316
0
  case WTAP_ENCAP_ATM_PDUS:
2317
0
    if (is_nokia) {
2318
      /*
2319
       * Nokia IPSO ATM.
2320
       */
2321
0
      phdr_len = pcap_read_nokiaatm_pseudoheader(fh,
2322
0
          &rec->rec_header.packet_header.pseudo_header,
2323
0
          packet_size, err, err_info);
2324
0
      if (phdr_len == -1)
2325
0
        return -1; /* Read error */
2326
0
    } else {
2327
      /*
2328
       * SunATM.
2329
       */
2330
0
      phdr_len = pcap_read_sunatm_pseudoheader(fh,
2331
0
          &rec->rec_header.packet_header.pseudo_header,
2332
0
          packet_size, err, err_info);
2333
0
      if (phdr_len == -1)
2334
0
        return -1; /* Read error */
2335
0
    }
2336
0
    break;
2337
2338
0
  case WTAP_ENCAP_ETHERNET:
2339
0
    if (is_nokia) {
2340
      /*
2341
       * Nokia IPSO.  Pseudo header has already been read, but it's not considered
2342
       * part of the packet size, so reread it to store the data for later (when saving)
2343
       */
2344
0
      if (!pcap_read_nokia_pseudoheader(fh, &rec->rec_header.packet_header.pseudo_header, err, err_info))
2345
0
        return -1; /* Read error */
2346
0
    }
2347
2348
    /*
2349
     * We don't know whether there's an FCS in this frame or not.
2350
     */
2351
0
    rec->rec_header.packet_header.pseudo_header.eth.fcs_len = -1;
2352
0
    break;
2353
2354
0
  case WTAP_ENCAP_IEEE_802_11:
2355
0
  case WTAP_ENCAP_IEEE_802_11_PRISM:
2356
0
  case WTAP_ENCAP_IEEE_802_11_RADIOTAP:
2357
0
  case WTAP_ENCAP_IEEE_802_11_AVS:
2358
    /*
2359
     * We don't know whether there's an FCS in this frame or not,
2360
     * at least in pcap files.  For radiotap, that's indicated in
2361
     * the radiotap header.
2362
     *
2363
     * XXX - in pcapng, there *could* be a packet option
2364
     * indicating the FCS length.
2365
     */
2366
0
    memset(&rec->rec_header.packet_header.pseudo_header.ieee_802_11, 0, sizeof(rec->rec_header.packet_header.pseudo_header.ieee_802_11));
2367
0
    rec->rec_header.packet_header.pseudo_header.ieee_802_11.fcs_len = -1;
2368
0
    rec->rec_header.packet_header.pseudo_header.ieee_802_11.decrypted = false;
2369
0
    rec->rec_header.packet_header.pseudo_header.ieee_802_11.datapad = false;
2370
0
    break;
2371
2372
0
  case WTAP_ENCAP_IRDA:
2373
0
    phdr_len = pcap_read_irda_pseudoheader(fh,
2374
0
        &rec->rec_header.packet_header.pseudo_header,
2375
0
        packet_size, err, err_info);
2376
0
    if (phdr_len == -1)
2377
0
      return -1; /* Read error */
2378
0
    break;
2379
2380
0
  case WTAP_ENCAP_MTP2_WITH_PHDR:
2381
0
    phdr_len = pcap_read_mtp2_pseudoheader(fh,
2382
0
        &rec->rec_header.packet_header.pseudo_header,
2383
0
        packet_size, err, err_info);
2384
0
    if (phdr_len == -1)
2385
0
      return -1; /* Read error */
2386
0
    break;
2387
2388
0
  case WTAP_ENCAP_LINUX_LAPD:
2389
0
    phdr_len = pcap_read_lapd_pseudoheader(fh,
2390
0
        &rec->rec_header.packet_header.pseudo_header,
2391
0
        packet_size, err, err_info);
2392
0
    if (phdr_len == -1)
2393
0
      return -1; /* Read error */
2394
0
    break;
2395
2396
0
  case WTAP_ENCAP_SITA:
2397
0
    phdr_len = pcap_read_sita_pseudoheader(fh,
2398
0
        &rec->rec_header.packet_header.pseudo_header,
2399
0
        packet_size, err, err_info);
2400
0
    if (phdr_len == -1)
2401
0
      return -1; /* Read error */
2402
0
    break;
2403
2404
0
  case WTAP_ENCAP_BLUETOOTH_H4:
2405
    /* We don't have pseudoheader, so just pretend we received everything. */
2406
0
    rec->rec_header.packet_header.pseudo_header.p2p.sent = false;
2407
0
    break;
2408
2409
0
  case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
2410
0
    phdr_len = pcap_read_bt_pseudoheader(fh,
2411
0
        &rec->rec_header.packet_header.pseudo_header,
2412
0
        packet_size, err, err_info);
2413
0
    if (phdr_len == -1)
2414
0
      return -1; /* Read error */
2415
0
    break;
2416
2417
0
  case WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR:
2418
0
    phdr_len = pcap_read_bt_monitor_pseudoheader(fh,
2419
0
        &rec->rec_header.packet_header.pseudo_header,
2420
0
        packet_size, err, err_info);
2421
0
    if (phdr_len == -1)
2422
0
      return -1; /* Read error */
2423
0
    break;
2424
2425
0
  case WTAP_ENCAP_NFC_LLCP:
2426
0
    phdr_len = pcap_read_llcp_pseudoheader(fh,
2427
0
        &rec->rec_header.packet_header.pseudo_header,
2428
0
        packet_size, err, err_info);
2429
0
    if (phdr_len == -1)
2430
0
      return -1; /* Read error */
2431
0
    break;
2432
2433
0
  case WTAP_ENCAP_PPP_WITH_PHDR:
2434
0
    phdr_len = pcap_read_ppp_pseudoheader(fh,
2435
0
        &rec->rec_header.packet_header.pseudo_header,
2436
0
        packet_size, err, err_info);
2437
0
    if (phdr_len == -1)
2438
0
      return -1; /* Read error */
2439
0
    break;
2440
2441
0
  case WTAP_ENCAP_ERF:
2442
0
    phdr_len = pcap_read_erf_pseudoheader(fh, rec,
2443
0
        &rec->rec_header.packet_header.pseudo_header,
2444
0
        packet_size, err, err_info);
2445
0
    if (phdr_len == -1)
2446
0
      return -1; /* Read error */
2447
0
    break;
2448
2449
0
  case WTAP_ENCAP_I2C_LINUX:
2450
0
    phdr_len = pcap_read_i2c_linux_pseudoheader(fh,
2451
0
        &rec->rec_header.packet_header.pseudo_header,
2452
0
        packet_size, err, err_info);
2453
0
    if (phdr_len == -1)
2454
0
      return -1; /* Read error */
2455
0
    break;
2456
0
  }
2457
2458
0
  return phdr_len;
2459
0
}
2460
2461
/*
2462
 * Compute, from the data provided by the Linux USB memory-mapped capture
2463
 * mechanism, the amount of packet data that would have been provided
2464
 * had the capture mechanism not chopped off any data at the end, if, in
2465
 * fact, it did so.
2466
 *
2467
 * Set the "unsliced length" field of the packet header to that value.
2468
 */
2469
static void
2470
fix_linux_usb_mmapped_length(wtap_rec *rec, const uint8_t *bp)
2471
0
{
2472
0
  const struct linux_usb_phdr *hdr;
2473
0
  unsigned bytes_left;
2474
2475
  /*
2476
   * All callers of this routine must ensure that pkth->caplen is
2477
   * >= sizeof (struct linux_usb_phdr).
2478
   */
2479
0
  bytes_left = rec->rec_header.packet_header.caplen;
2480
0
  bytes_left -= sizeof (struct linux_usb_phdr);
2481
2482
0
  hdr = (const struct linux_usb_phdr *) bp;
2483
0
  if (!hdr->data_flag && hdr->transfer_type == URB_ISOCHRONOUS &&
2484
0
      hdr->event_type == URB_COMPLETE &&
2485
0
      (hdr->endpoint_number & URB_TRANSFER_IN) &&
2486
0
      rec->rec_header.packet_header.len == sizeof(struct linux_usb_phdr) +
2487
0
                   (hdr->ndesc * sizeof (struct linux_usb_isodesc)) + hdr->urb_len) {
2488
0
    struct linux_usb_isodesc *descs;
2489
0
    unsigned pre_truncation_data_len, pre_truncation_len;
2490
2491
0
    descs = (struct linux_usb_isodesc *) (bp + sizeof(struct linux_usb_phdr));
2492
2493
    /*
2494
     * We have data (yes, data_flag is 0 if we *do* have data),
2495
     * and this is a "this is complete" incoming isochronous
2496
     * transfer event, and the length was calculated based
2497
     * on the URB length.
2498
     *
2499
     * That's not correct, because the data isn't contiguous,
2500
     * and the isochronous descriptors show how it's scattered.
2501
     *
2502
     * Find the end of the last chunk of data in the buffer
2503
     * referred to by the isochronous descriptors; that indicates
2504
     * how far into the buffer the data would have gone.
2505
     *
2506
     * Make sure we don't run past the end of the captured data
2507
     * while processing the isochronous descriptors.
2508
     */
2509
0
    pre_truncation_data_len = 0;
2510
0
    for (uint32_t desc = 0;
2511
0
        desc < hdr->ndesc && bytes_left >= sizeof (struct linux_usb_isodesc);
2512
0
        desc++, bytes_left -= sizeof (struct linux_usb_isodesc)) {
2513
0
      unsigned desc_end;
2514
2515
0
      if (descs[desc].iso_len != 0) {
2516
0
        desc_end = descs[desc].iso_off + descs[desc].iso_len;
2517
0
        if (desc_end > pre_truncation_data_len)
2518
0
          pre_truncation_data_len = desc_end;
2519
0
      }
2520
0
    }
2521
2522
    /*
2523
     * Now calculate the total length based on that data
2524
     * length.
2525
     */
2526
0
    pre_truncation_len = sizeof(struct linux_usb_phdr) +
2527
0
        (hdr->ndesc * sizeof (struct linux_usb_isodesc)) +
2528
0
        pre_truncation_data_len;
2529
2530
    /*
2531
     * If that's greater than or equal to the captured length,
2532
     * use that as the length.
2533
     */
2534
0
    if (pre_truncation_len >= rec->rec_header.packet_header.caplen)
2535
0
      rec->rec_header.packet_header.len = pre_truncation_len;
2536
2537
    /*
2538
     * If the captured length is greater than the length,
2539
     * use the captured length.
2540
     *
2541
     * For completion events for incoming isochronous transfers,
2542
     * it's based on data_len, which is calculated the same way
2543
     * we calculated pre_truncation_data_len above, except that
2544
     * it has access to all the isochronous descriptors, not
2545
     * just the ones that the kernel were able to provide us or,
2546
     * for a capture file, that weren't sliced off by a snapshot
2547
     * length.
2548
     *
2549
     * However, it might have been reduced by the USB capture
2550
     * mechanism arbitrarily limiting the amount of data it
2551
     * provides to userland, or by the libpcap capture code
2552
     * limiting it to being no more than the snapshot, so
2553
     * we don't want to just use it all the time; we only
2554
     * do so to try to get a better estimate of the actual
2555
     * length - and to make sure the on-the-network length
2556
     * is always >= the captured length.
2557
     */
2558
0
    if (rec->rec_header.packet_header.caplen > rec->rec_header.packet_header.len)
2559
0
      rec->rec_header.packet_header.len = rec->rec_header.packet_header.caplen;
2560
0
  }
2561
0
}
2562
2563
static void
2564
pcap_fixup_len(wtap_rec *rec)
2565
0
{
2566
0
  const uint8_t *pd;
2567
0
  struct linux_usb_phdr *usb_phdr;
2568
2569
0
  pd = ws_buffer_start_ptr(&rec->data);
2570
2571
  /*
2572
   * Greasy hack, but we never directly dereference any of
2573
   * the fields in *usb_phdr, we just get offsets of and
2574
   * addresses of its members and byte-swap it with a
2575
   * byte-at-a-time macro, so it's alignment-safe.
2576
   */
2577
0
  usb_phdr = (struct linux_usb_phdr *)(void *)pd;
2578
2579
0
  if (rec->rec_header.packet_header.caplen >=
2580
0
      sizeof (struct linux_usb_phdr)) {
2581
    /*
2582
     * In older versions of libpcap, in memory-mapped captures,
2583
     * the "on-the-bus length" for completion events for
2584
     * incoming isochronous transfers was miscalculated; it
2585
     * needed to be calculated based on the* offsets and lengths
2586
     * in the descriptors, not on the raw URB length, but it
2587
     * wasn't.
2588
     *
2589
     * If this packet contains transferred data (yes, data_flag
2590
     * is 0 if we *do* have data), and the total on-the-network
2591
     * length is equal to the value calculated from the raw URB
2592
     * length, then it might be one of those transfers.
2593
     *
2594
     * We only do this if we have the full USB pseudo-header.
2595
     */
2596
0
    if (!usb_phdr->data_flag &&
2597
0
        rec->rec_header.packet_header.len == sizeof (struct linux_usb_phdr) +
2598
0
          (usb_phdr->ndesc * sizeof (struct linux_usb_isodesc)) + usb_phdr->urb_len) {
2599
      /*
2600
       * It might need fixing; fix it if it's a completion
2601
       * event for an incoming isochronous transfer.
2602
       */
2603
0
      fix_linux_usb_mmapped_length(rec, pd);
2604
0
    }
2605
0
  }
2606
0
}
2607
2608
void
2609
pcap_read_post_process(bool is_nokia, int wtap_encap,
2610
    wtap_rec *rec, bool bytes_swapped, int fcs_len)
2611
0
{
2612
0
  switch (wtap_encap) {
2613
2614
0
  case WTAP_ENCAP_ATM_PDUS:
2615
0
    if (is_nokia) {
2616
      /*
2617
       * Nokia IPSO ATM.
2618
       *
2619
       * Guess the traffic type based on the packet
2620
       * contents.
2621
       */
2622
0
      atm_guess_traffic_type(rec);
2623
0
    } else {
2624
      /*
2625
       * SunATM.
2626
       *
2627
       * If this is ATM LANE traffic, try to guess what
2628
       * type of LANE traffic it is based on the packet
2629
       * contents.
2630
       */
2631
0
      if (rec->rec_header.packet_header.pseudo_header.atm.type == TRAF_LANE)
2632
0
        atm_guess_lane_type(rec);
2633
0
    }
2634
0
    break;
2635
2636
0
  case WTAP_ENCAP_ETHERNET:
2637
    /*
2638
     * The FCS length is supposed to be in bits.
2639
     * If it's < 8, assume it's in bytes; otherwise,
2640
     * convert it to bytes.
2641
     */
2642
0
    if (fcs_len < 8)
2643
0
      rec->rec_header.packet_header.pseudo_header.eth.fcs_len = fcs_len;
2644
0
    else
2645
0
      rec->rec_header.packet_header.pseudo_header.eth.fcs_len = fcs_len/8;
2646
0
    break;
2647
2648
0
  case WTAP_ENCAP_SLL:
2649
0
    if (bytes_swapped)
2650
0
      pcap_byteswap_linux_sll_pseudoheader(rec);
2651
0
    break;
2652
2653
0
  case WTAP_ENCAP_SLL2:
2654
0
    if (bytes_swapped)
2655
0
      pcap_byteswap_linux_sll2_pseudoheader(rec);
2656
0
    break;
2657
2658
0
  case WTAP_ENCAP_USB_LINUX:
2659
0
    if (bytes_swapped)
2660
0
      pcap_byteswap_linux_usb_pseudoheader(rec, false);
2661
0
    break;
2662
2663
0
  case WTAP_ENCAP_USB_LINUX_MMAPPED:
2664
0
    if (bytes_swapped)
2665
0
      pcap_byteswap_linux_usb_pseudoheader(rec, true);
2666
2667
    /*
2668
     * Fix up the on-the-network length if necessary.
2669
     */
2670
0
    pcap_fixup_len(rec);
2671
0
    break;
2672
2673
0
  case WTAP_ENCAP_NETANALYZER:
2674
    /*
2675
     * Not strictly necessary, as the netANALYZER
2676
     * dissector calls the "Ethernet with FCS"
2677
     * dissector, but we might as well set it.
2678
     */
2679
0
    rec->rec_header.packet_header.pseudo_header.eth.fcs_len = 4;
2680
0
    break;
2681
2682
0
  case WTAP_ENCAP_NFLOG:
2683
0
    if (bytes_swapped)
2684
0
      pcap_byteswap_nflog_pseudoheader(rec);
2685
0
    break;
2686
2687
0
  case WTAP_ENCAP_ERF:
2688
    /*
2689
     * Update packet size to account for ERF padding and snapping.
2690
     * Captured length is minimum of wlen and previously calculated
2691
     * caplen (which would have included padding but not phdr).
2692
     */
2693
0
    rec->rec_header.packet_header.len = rec->rec_header.packet_header.pseudo_header.erf.phdr.wlen;
2694
0
    rec->rec_header.packet_header.caplen = MIN(rec->rec_header.packet_header.len, rec->rec_header.packet_header.caplen);
2695
0
    break;
2696
2697
0
  case WTAP_ENCAP_PFLOG:
2698
0
    if (bytes_swapped)
2699
0
      pcap_byteswap_pflog_pseudoheader(rec);
2700
0
    break;
2701
2702
0
  default:
2703
0
    break;
2704
0
  }
2705
0
}
2706
2707
bool
2708
wtap_encap_requires_phdr(int wtap_encap)
2709
1.62k
{
2710
1.62k
  switch (wtap_encap) {
2711
2712
1
  case WTAP_ENCAP_ATM_PDUS:
2713
1
  case WTAP_ENCAP_IRDA:
2714
1
  case WTAP_ENCAP_MTP2_WITH_PHDR:
2715
1
  case WTAP_ENCAP_LINUX_LAPD:
2716
1
  case WTAP_ENCAP_SITA:
2717
147
  case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
2718
147
  case WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR:
2719
147
  case WTAP_ENCAP_NFC_LLCP:
2720
147
  case WTAP_ENCAP_PPP_WITH_PHDR:
2721
147
  case WTAP_ENCAP_ERF:
2722
147
  case WTAP_ENCAP_I2C_LINUX:
2723
147
    return true;
2724
1.62k
  }
2725
1.47k
  return false;
2726
1.62k
}
2727
2728
unsigned
2729
pcap_get_phdr_size(int encap, const union wtap_pseudo_header *pseudo_header)
2730
0
{
2731
0
  unsigned hdrsize;
2732
2733
0
  switch (encap) {
2734
2735
0
  case WTAP_ENCAP_ATM_PDUS:
2736
0
    hdrsize = SUNATM_LEN;
2737
0
    break;
2738
2739
0
  case WTAP_ENCAP_IRDA:
2740
0
    hdrsize = IRDA_SLL_LEN;
2741
0
    break;
2742
2743
0
  case WTAP_ENCAP_MTP2_WITH_PHDR:
2744
0
    hdrsize = MTP2_HDR_LEN;
2745
0
    break;
2746
2747
0
  case WTAP_ENCAP_LINUX_LAPD:
2748
0
    hdrsize = LAPD_SLL_LEN;
2749
0
    break;
2750
2751
0
  case WTAP_ENCAP_SITA:
2752
0
    hdrsize = SITA_HDR_LEN;
2753
0
    break;
2754
2755
0
  case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
2756
0
    hdrsize = (unsigned)sizeof (struct pcap_bt_phdr);
2757
0
    break;
2758
2759
0
  case WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR:
2760
0
    hdrsize = (unsigned)sizeof (struct pcap_bt_monitor_phdr);
2761
0
    break;
2762
2763
0
  case WTAP_ENCAP_NFC_LLCP:
2764
0
    hdrsize = LLCP_HEADER_LEN;
2765
0
    break;
2766
2767
0
  case WTAP_ENCAP_PPP_WITH_PHDR:
2768
0
    hdrsize = (unsigned)sizeof (struct pcap_ppp_phdr);
2769
0
    break;
2770
2771
0
  case WTAP_ENCAP_ERF:
2772
0
    hdrsize = (unsigned)sizeof (struct erf_phdr);
2773
2774
    /*
2775
     * If the type of record given in the pseudo header
2776
     * indicates the presence of an extension header, then
2777
     * add in the lengths of the extension headers.
2778
     */
2779
0
    if (pseudo_header->erf.phdr.type & 0x80) {
2780
0
      int i = 0, max = array_length(pseudo_header->erf.ehdr_list);
2781
0
      uint8_t erf_exhdr[8];
2782
0
      uint8_t type;
2783
2784
0
      do {
2785
0
        phtonu64(erf_exhdr, pseudo_header->erf.ehdr_list[i].ehdr);
2786
0
        type = erf_exhdr[0];
2787
0
        hdrsize += 8;
2788
0
        i++;
2789
0
      } while (type & 0x80 && i < max);
2790
0
    }
2791
2792
    /*
2793
     * Now add in the length of the subheader, if any.
2794
     */
2795
0
    switch (pseudo_header->erf.phdr.type & 0x7F) {
2796
2797
0
    case ERF_TYPE_MC_HDLC:
2798
0
    case ERF_TYPE_MC_RAW:
2799
0
    case ERF_TYPE_MC_ATM:
2800
0
    case ERF_TYPE_MC_RAW_CHANNEL:
2801
0
    case ERF_TYPE_MC_AAL5:
2802
0
    case ERF_TYPE_MC_AAL2:
2803
0
    case ERF_TYPE_COLOR_MC_HDLC_POS:
2804
0
      hdrsize += (unsigned)sizeof(struct erf_mc_hdr);
2805
0
      break;
2806
0
    case ERF_TYPE_AAL2:
2807
0
      hdrsize += (unsigned)sizeof(struct erf_aal2_hdr);
2808
0
      break;
2809
2810
0
    case ERF_TYPE_ETH:
2811
0
    case ERF_TYPE_COLOR_ETH:
2812
0
    case ERF_TYPE_DSM_COLOR_ETH:
2813
0
    case ERF_TYPE_COLOR_HASH_ETH:
2814
0
      hdrsize += (unsigned)sizeof(struct erf_eth_hdr);
2815
0
      break;
2816
2817
0
    default:
2818
0
      break;
2819
0
    }
2820
0
    break;
2821
2822
0
  case WTAP_ENCAP_I2C_LINUX:
2823
0
    hdrsize = (unsigned)sizeof (struct i2c_linux_file_hdr);
2824
0
    break;
2825
2826
0
  default:
2827
0
    hdrsize = 0;
2828
0
    break;
2829
0
  }
2830
2831
0
  return hdrsize;
2832
0
}
2833
2834
bool
2835
pcap_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header,
2836
    int *err)
2837
0
{
2838
0
  switch (encap) {
2839
2840
0
  case WTAP_ENCAP_ATM_PDUS:
2841
0
    if (!pcap_write_sunatm_pseudoheader(wdh, pseudo_header, err))
2842
0
      return false;
2843
0
    break;
2844
2845
0
  case WTAP_ENCAP_IRDA:
2846
0
    if (!pcap_write_irda_pseudoheader(wdh, pseudo_header, err))
2847
0
      return false;
2848
0
    break;
2849
2850
0
  case WTAP_ENCAP_MTP2_WITH_PHDR:
2851
0
    if (!pcap_write_mtp2_pseudoheader(wdh, pseudo_header, err))
2852
0
      return false;
2853
0
    break;
2854
2855
0
  case WTAP_ENCAP_LINUX_LAPD:
2856
0
    if (!pcap_write_lapd_pseudoheader(wdh, pseudo_header, err))
2857
0
      return false;
2858
0
    break;
2859
2860
0
  case WTAP_ENCAP_SITA:
2861
0
    if (!pcap_write_sita_pseudoheader(wdh, pseudo_header, err))
2862
0
      return false;
2863
0
    break;
2864
2865
0
  case WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR:
2866
0
    if (!pcap_write_bt_pseudoheader(wdh, pseudo_header, err))
2867
0
      return false;
2868
0
    break;
2869
2870
0
  case WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR:
2871
0
    if (!pcap_write_bt_monitor_pseudoheader(wdh, pseudo_header, err))
2872
0
      return false;
2873
0
    break;
2874
2875
0
  case WTAP_ENCAP_NFC_LLCP:
2876
0
    if (!pcap_write_llcp_pseudoheader(wdh, pseudo_header, err))
2877
0
      return false;
2878
0
    break;
2879
2880
0
  case WTAP_ENCAP_PPP_WITH_PHDR:
2881
0
    if (!pcap_write_ppp_pseudoheader(wdh, pseudo_header, err))
2882
0
      return false;
2883
0
    break;
2884
2885
0
  case WTAP_ENCAP_ERF:
2886
0
    if (!pcap_write_erf_pseudoheader(wdh, pseudo_header, err))
2887
0
      return false;
2888
0
    break;
2889
2890
0
  case WTAP_ENCAP_I2C_LINUX:
2891
0
    if (!pcap_write_i2c_linux_pseudoheader(wdh, pseudo_header, err))
2892
0
      return false;
2893
0
    break;
2894
0
  }
2895
0
  return true;
2896
0
}
2897
2898
/*
2899
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
2900
 *
2901
 * Local variables:
2902
 * c-basic-offset: 8
2903
 * tab-width: 8
2904
 * indent-tabs-mode: t
2905
 * End:
2906
 *
2907
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2908
 * :indentSize=8:tabSize=8:noTabs=false:
2909
 */