1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/* file-pcap.c
* Routines for PCAP File Format
* http://www.tcpdump.org/manpages/pcap-savefile.5.html
*
* Copyright 2015, Michal Labedzki for Tieto Corporation
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <epan/packet.h>
#include <epan/prefs.h>
#include <epan/expert.h>
#include <epan/exceptions.h>
#include <epan/show_exception.h>
#include "packet-pcap_pktdata.h"
static int proto_pcap;
static dissector_handle_t pcap_pktdata_handle;
static int hf_pcap_header;
static int hf_pcap_header_magic_number;
static int hf_pcap_header_version_major;
static int hf_pcap_header_version_minor;
static int hf_pcap_header_this_zone;
static int hf_pcap_header_sigfigs;
static int hf_pcap_header_snapshot_length;
static int hf_pcap_header_link_type;
static int hf_pcap_packet;
static int hf_pcap_packet_timestamp;
static int hf_pcap_packet_timestamp_sec;
static int hf_pcap_packet_timestamp_usec;
static int hf_pcap_packet_captured_length;
static int hf_pcap_packet_original_length;
static int hf_pcap_packet_data;
static expert_field ei_pcap_capt_larger_than_orig;
static expert_field ei_pcap_capt_larger_than_snap;
static int ett_pcap;
static int ett_pcap_header;
static int ett_pcap_packet;
static int ett_pcap_packet_data;
static int ett_pcap_timestamp;
static bool pref_dissect_next_layer;
void proto_register_file_pcap(void);
void proto_reg_handoff_file_pcap(void);
#define MAGIC_NUMBER_SIZE 4
static int
dissect_pcap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_)
{
static const uint8_t pcap_big_endian_magic[MAGIC_NUMBER_SIZE] = {
0xa1, 0xb2, 0xc3, 0xd4
};
static const uint8_t pcap_little_endian_magic[MAGIC_NUMBER_SIZE] = {
0xd4, 0xc3, 0xb2, 0xa1
};
static const uint8_t pcap_nsec_big_endian_magic[MAGIC_NUMBER_SIZE] = {
0xa1, 0xb2, 0x3c, 0x4d
};
static const uint8_t pcap_nsec_little_endian_magic[MAGIC_NUMBER_SIZE] = {
0x4d, 0x3c, 0xb2, 0xa1
};
volatile int offset = 0;
proto_tree *main_tree;
proto_item *main_item;
proto_tree *header_tree;
proto_item *header_item;
proto_item *magic_number_item;
proto_tree *packet_tree;
proto_item *packet_item;
proto_tree *timestamp_tree;
proto_item *timestamp_item;
proto_tree *packet_data_tree;
proto_item *packet_data_item;
proto_item *capt_len_item;
volatile uint32_t encoding;
volatile unsigned timestamp_scale_factor;
const char *magic;
uint32_t snap_length;
uint32_t captured_length;
uint32_t original_length;
uint32_t link_type;
volatile uint32_t frame_number = 1;
nstime_t timestamp;
if (tvb_memeql(tvb, 0, pcap_big_endian_magic, MAGIC_NUMBER_SIZE) == 0) {
encoding = ENC_BIG_ENDIAN;
timestamp_scale_factor = 1000;
magic = "Big-endian";
} else if (tvb_memeql(tvb, 0, pcap_little_endian_magic, MAGIC_NUMBER_SIZE) == 0) {
encoding = ENC_LITTLE_ENDIAN;
timestamp_scale_factor = 1000;
magic = "Little-endian";
} else if (tvb_memeql(tvb, 0, pcap_nsec_big_endian_magic, MAGIC_NUMBER_SIZE) == 0) {
encoding = ENC_BIG_ENDIAN;
timestamp_scale_factor = 1;
magic = "Big-endian, nanosecond resolution";
} else if (tvb_memeql(tvb, 0, pcap_nsec_little_endian_magic, MAGIC_NUMBER_SIZE) == 0) {
encoding = ENC_LITTLE_ENDIAN;
timestamp_scale_factor = 1;
magic = "Little-endian, nanosecond resolution";
} else {
/*
* Not one of the magic numbers we recognize.
* XXX - add them?
*/
return 0;
}
main_item = proto_tree_add_item(tree, proto_pcap, tvb, offset, -1, ENC_NA);
main_tree = proto_item_add_subtree(main_item, ett_pcap);
header_item = proto_tree_add_item(main_tree, hf_pcap_header , tvb, offset, 24, ENC_NA);
header_tree = proto_item_add_subtree(header_item, ett_pcap_header);
magic_number_item = proto_tree_add_item(header_tree, hf_pcap_header_magic_number, tvb, offset, 4, ENC_NA);
proto_item_append_text(magic_number_item, " (%s)", magic);
offset += 4;
proto_tree_add_item(header_tree, hf_pcap_header_version_major, tvb, offset, 2, encoding);
offset += 2;
proto_tree_add_item(header_tree, hf_pcap_header_version_minor, tvb, offset, 2, encoding);
offset += 2;
proto_tree_add_item(header_tree, hf_pcap_header_this_zone, tvb, offset, 4, encoding);
offset += 4;
proto_tree_add_item(header_tree, hf_pcap_header_sigfigs, tvb, offset, 4, encoding);
offset += 4;
proto_tree_add_item_ret_uint(header_tree, hf_pcap_header_snapshot_length, tvb, offset, 4, encoding, &snap_length);
offset += 4;
proto_tree_add_item(header_tree, hf_pcap_header_link_type, tvb, offset, 4, encoding);
link_type = tvb_get_uint32(tvb, offset, encoding);
offset += 4;
while (offset < (int) tvb_reported_length(tvb)) {
packet_item = proto_tree_add_item(main_tree, hf_pcap_packet, tvb, offset, 4 * 4, ENC_NA);
packet_tree = proto_item_add_subtree(packet_item, ett_pcap_packet);
proto_item_append_text(packet_item, " %u", frame_number);
timestamp.secs = tvb_get_uint32(tvb, offset, encoding);
timestamp.nsecs = tvb_get_uint32(tvb, offset + 4, encoding) * timestamp_scale_factor;
timestamp_item = proto_tree_add_time(packet_tree, hf_pcap_packet_timestamp, tvb, offset, 8, ×tamp);
timestamp_tree = proto_item_add_subtree(timestamp_item, ett_pcap_timestamp);
proto_tree_add_item(timestamp_tree, hf_pcap_packet_timestamp_sec, tvb, offset, 4, encoding);
offset += 4;
proto_tree_add_item(timestamp_tree, hf_pcap_packet_timestamp_usec, tvb, offset, 4, encoding);
offset += 4;
capt_len_item = proto_tree_add_item_ret_uint(packet_tree, hf_pcap_packet_captured_length, tvb, offset, 4, encoding, &captured_length);
offset += 4;
proto_tree_add_item_ret_uint(packet_tree, hf_pcap_packet_original_length, tvb, offset, 4, encoding, &original_length);
offset += 4;
if (captured_length > snap_length)
{
expert_add_info(pinfo, capt_len_item,
&ei_pcap_capt_larger_than_snap);
}
if (captured_length > original_length) {
expert_add_info(pinfo, capt_len_item,
&ei_pcap_capt_larger_than_orig);
break;
}
packet_data_item = proto_tree_add_item(packet_tree, hf_pcap_packet_data, tvb, offset, captured_length, ENC_NA);
packet_data_tree = proto_item_add_subtree(packet_data_item, ett_pcap_packet_data);
pinfo->num = frame_number;
pinfo->abs_ts = timestamp;
if (pref_dissect_next_layer) {
TRY {
call_dissector_with_data(pcap_pktdata_handle, tvb_new_subset_length_caplen(tvb, offset, captured_length, original_length), pinfo, packet_data_tree, &link_type);
}
CATCH_BOUNDS_ERRORS {
show_exception(tvb, pinfo, packet_data_tree, EXCEPT_CODE, GET_MESSAGE);
}
ENDTRY;
}
offset += captured_length;
proto_item_set_len(packet_item, 4 * 4 + captured_length);
frame_number += 1;
}
return offset;
}
static bool
dissect_pcap_heur(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
{
return dissect_pcap(tvb, pinfo, tree, data) > 0;
}
void
proto_register_file_pcap(void)
{
module_t *module;
expert_module_t *expert_pcap;
static hf_register_info hf[] = {
{ &hf_pcap_header,
{ "Header", "pcap.header",
FT_NONE, BASE_NONE, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_header_magic_number,
{ "Magic Number", "pcap.header.magic_number",
FT_BYTES, BASE_NONE, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_header_version_major,
{ "Version Major", "pcap.header.version.major",
FT_UINT16, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_header_version_minor,
{ "Version Minor", "pcap.header.version.minor",
FT_UINT16, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_header_this_zone,
{ "This Zone", "pcap.header.this_zone",
FT_INT32, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_header_sigfigs,
{ "Sigfigs", "pcap.header.sigfigs",
FT_UINT32, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_header_snapshot_length,
{ "Snapshot Length", "pcap.header.snapshot_length",
FT_UINT32, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_header_link_type,
{ "Link Type", "pcap.header.link_type",
FT_UINT32, BASE_DEC_HEX, VALS(link_type_vals), 0x00,
NULL, HFILL }
},
{ &hf_pcap_packet,
{ "Packet", "pcap.packet",
FT_NONE, BASE_NONE, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_packet_timestamp,
{ "Timestamp", "pcap.packet.timestamp",
FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_packet_timestamp_sec,
{ "Timestamp sec", "pcap.packet.timestamp.sec",
FT_UINT32, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_packet_timestamp_usec,
{ "Timestamp usec", "pcap.packet.timestamp.usec",
FT_UINT32, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_packet_captured_length,
{ "Captured Packet Length", "pcap.packet.captured_length",
FT_UINT32, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_packet_original_length,
{ "Original Packet Length", "pcap.packet.original_length",
FT_UINT32, BASE_DEC, NULL, 0x00,
NULL, HFILL }
},
{ &hf_pcap_packet_data,
{ "Data", "pcap.packet.data",
FT_NONE, BASE_NONE, NULL, 0x00,
NULL, HFILL }
},
};
static ei_register_info ei[] = {
{ &ei_pcap_capt_larger_than_orig,
{ "pcap.capt_len_larger_than_orig_len", PI_MALFORMED, PI_ERROR,
"captured length is larger than original length",
EXPFILL }},
{ &ei_pcap_capt_larger_than_snap,
{ "pcap.capt_len_larger_than_snap_len", PI_PROTOCOL, PI_WARN,
"captured length is larger than snapshot length",
EXPFILL }}
};
static int *ett[] = {
&ett_pcap,
&ett_pcap_header,
&ett_pcap_packet,
&ett_pcap_packet_data,
&ett_pcap_timestamp
};
proto_pcap = proto_register_protocol("PCAP File Format", "File-PCAP", "file-pcap");
proto_register_field_array(proto_pcap, hf, array_length(hf));
proto_register_subtree_array(ett, array_length(ett));
expert_pcap = expert_register_protocol(proto_pcap);
expert_register_field_array(expert_pcap, ei, array_length(ei));
register_dissector("file-pcap", dissect_pcap, proto_pcap);
module = prefs_register_protocol(proto_pcap, NULL);
prefs_register_static_text_preference(module, "version",
"PCAP version: >=2.4",
"Version of file-format supported by this dissector.");
prefs_register_bool_preference(module, "dissect_next_layer",
"Dissect next layer",
"Dissect next layer",
&pref_dissect_next_layer);
}
void
proto_reg_handoff_file_pcap(void)
{
heur_dissector_add("wtap_file", dissect_pcap_heur, "PCAP File", "pcap_wtap", proto_pcap, HEURISTIC_ENABLE);
pcap_pktdata_handle = find_dissector_add_dependency("pcap_pktdata", proto_pcap);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|