/src/wireshark/wiretap/erf.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2003 Endace Technology Ltd, Hamilton, New Zealand. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This software and documentation has been developed by Endace Technology Ltd. |
6 | | * along with the DAG PCI network capture cards. For further information please |
7 | | * visit http://www.endace.com/. |
8 | | * |
9 | | * SPDX-License-Identifier: BSD-3-Clause |
10 | | */ |
11 | | |
12 | | /* |
13 | | * erf - Endace ERF (Extensible Record Format) |
14 | | * |
15 | | * Rev A: |
16 | | * http://web.archive.org/web/20050829051042/http://www.endace.com/support/EndaceRecordFormat.pdf |
17 | | * Version 1: |
18 | | * http://web.archive.org/web/20061111014023/http://www.endace.com/support/EndaceRecordFormat.pdf |
19 | | * Version 8: |
20 | | * https://gitlab.com/wireshark/wireshark/uploads/f694bfee494784425b6545892180a8b2/Endace_ERF_Types.pdf |
21 | | * (bug #4484) |
22 | | * Current version (version 21, as of 2023-03-28): |
23 | | * https://www.endace.com/erf-extensible-record-format-types.pdf |
24 | | * |
25 | | * Note that version 17 drops descriptions of records for no-longer-supported |
26 | | * DAG cards. Version 16 is probably the best version to use for those |
27 | | * older record types, but it's not in any obvious location in the Wayback |
28 | | * Machine. |
29 | | */ |
30 | | |
31 | | #include "config.h" |
32 | | #include "erf.h" |
33 | | |
34 | | #include <stdlib.h> |
35 | | #include <string.h> |
36 | | |
37 | | #include <glib.h> |
38 | | |
39 | | #include <wsutil/array.h> |
40 | | #include <wsutil/crc32.h> |
41 | | #include <wsutil/pint.h> |
42 | | #include <wsutil/strtoi.h> |
43 | | #include <wsutil/glib-compat.h> |
44 | | #include <wsutil/ws_padding_to.h> |
45 | | |
46 | | #include "wtap_module.h" |
47 | | #include "file_wrappers.h" |
48 | | #include "erf_record.h" |
49 | | #include "erf-common.h" |
50 | | |
51 | | struct erf_anchor_mapping { |
52 | | uint64_t host_id; |
53 | | uint64_t anchor_id; |
54 | | uint64_t gen_time; |
55 | | char *comment; |
56 | | }; |
57 | | |
58 | | static const unsigned erf_header_size = (unsigned)sizeof(erf_header_t); |
59 | | static const unsigned erf_mc_header_size = (unsigned)sizeof(erf_mc_header_t); |
60 | | static const unsigned erf_eth_hdr_size = (unsigned)sizeof(erf_eth_header_t); |
61 | | |
62 | | |
63 | | static bool erf_read_header(wtap *wth, FILE_T fh, |
64 | | wtap_rec *rec, |
65 | | erf_header_t *erf_header, |
66 | | int *err, |
67 | | char **err_info, |
68 | | uint32_t *bytes_read, |
69 | | uint32_t *packet_size, |
70 | | GPtrArray *anchor_mappings_to_update); |
71 | | static bool erf_read(wtap *wth, wtap_rec *rec, |
72 | | int *err, char **err_info, int64_t *data_offset); |
73 | | static bool erf_seek_read(wtap *wth, int64_t seek_off, |
74 | | wtap_rec *rec, |
75 | | int *err, char **err_info); |
76 | | static void erf_close(wtap *wth); |
77 | | |
78 | | static int populate_summary_info(erf_t *erf_priv, wtap *wth, wtap_rec *rec, uint32_t packet_size, GPtrArray *anchor_mappings_to_update, int *err, char **err_info); |
79 | | static int erf_update_anchors_from_header(erf_t *erf_priv, wtap_rec *rec, union wtap_pseudo_header *pseudo_header, uint64_t host_id, GPtrArray *anchor_mappings_to_update); |
80 | | static int erf_get_source_from_header(union wtap_pseudo_header *pseudo_header, uint64_t *host_id, uint8_t *source_id); |
81 | | static int erf_populate_interface(erf_t* erf_priv, wtap *wth, union wtap_pseudo_header *pseudo_header, uint64_t host_id, uint8_t source_id, uint8_t if_num, int *err, char **err_info); |
82 | | |
83 | | typedef struct { |
84 | | bool write_next_extra_meta; |
85 | | bool last_meta_periodic; |
86 | | uint64_t host_id; |
87 | | uint64_t implicit_host_id; |
88 | | uint64_t prev_frame_ts; |
89 | | uint8_t prev_erf_type; |
90 | | uint64_t gen_time; |
91 | | time_t first_frame_time_sec; |
92 | | time_t prev_inserted_time_sec; |
93 | | char* user_comment_ptr; |
94 | | GPtrArray* periodic_sections; |
95 | | GArray *periodic_extra_ehdrs; |
96 | | GRand *rand; |
97 | | } erf_dump_t; |
98 | | |
99 | | static erf_dump_t* erf_dump_priv_create(void); |
100 | | static void erf_dump_priv_free(erf_dump_t *dump_priv); |
101 | | static bool erf_dump_priv_compare_capture_comment(wtap_dumper *wdh, erf_dump_t *dump_priv,const union wtap_pseudo_header *pseudo_header, const uint8_t *pd); |
102 | | static bool erf_comment_to_sections(wtap_dumper *wdh, uint16_t section_type, uint16_t section_id, char *comment, GPtrArray *sections); |
103 | | static bool erf_wtap_info_to_sections(wtap_dumper *wdh, GPtrArray *sections); |
104 | | static bool get_user_comment_string(wtap_dumper *wdh, char** user_comment_ptr); |
105 | | |
106 | | static bool erf_write_meta_record(wtap_dumper *wdh, erf_dump_t *dump_priv, uint64_t timestamp, GPtrArray *sections, GArray *extra_ehdrs, int *err); |
107 | | |
108 | | static const struct { |
109 | | int erf_encap_value; |
110 | | int wtap_encap_value; |
111 | | } erf_to_wtap_map[] = { |
112 | | { ERF_TYPE_HDLC_POS, WTAP_ENCAP_CHDLC }, |
113 | | { ERF_TYPE_HDLC_POS, WTAP_ENCAP_HHDLC }, |
114 | | { ERF_TYPE_HDLC_POS, WTAP_ENCAP_CHDLC_WITH_PHDR }, |
115 | | { ERF_TYPE_HDLC_POS, WTAP_ENCAP_PPP }, |
116 | | { ERF_TYPE_HDLC_POS, WTAP_ENCAP_FRELAY }, |
117 | | { ERF_TYPE_HDLC_POS, WTAP_ENCAP_MTP2 }, |
118 | | { ERF_TYPE_ETH, WTAP_ENCAP_ETHERNET }, |
119 | | { 99, WTAP_ENCAP_ERF }, /*this type added so WTAP_ENCAP_ERF will work and then be treated at ERF->ERF*/ |
120 | | }; |
121 | | |
122 | 0 | #define NUM_ERF_ENCAPS array_length(erf_to_wtap_map) |
123 | | |
124 | 0 | #define ERF_META_TAG_HEADERLEN 4 |
125 | 0 | #define ERF_META_TAG_TOTAL_ALIGNED_LENGTH(taglength) ((((uint32_t)taglength + 0x3U) & ~0x3U) + ERF_META_TAG_HEADERLEN) |
126 | 0 | #define ERF_META_TAG_ALIGNED_LENGTH(taglength) ((((uint32_t)taglength + 0x3U) & ~0x3U)) |
127 | | |
128 | | struct erf_if_info { |
129 | | int if_index; |
130 | | char *name; |
131 | | char *descr; |
132 | | int stream_num; |
133 | | struct { |
134 | | unsigned filter:1; |
135 | | unsigned fcs_len:1; |
136 | | unsigned snaplen:1; |
137 | | } set_flags; |
138 | | }; |
139 | | |
140 | | struct erf_if_mapping { |
141 | | uint64_t host_id; |
142 | | uint8_t source_id; |
143 | | struct erf_if_info interfaces[ERF_MAX_INTERFACES]; |
144 | | |
145 | | char *module_filter_str; |
146 | | /*here because we could have captures from multiple hosts in the file*/ |
147 | | char *capture_filter_str; |
148 | | int8_t module_fcs_len; |
149 | | uint32_t module_snaplen; |
150 | | int interface_metadata; |
151 | | uint64_t interface_gentime; |
152 | | uint64_t module_gentime; |
153 | | }; |
154 | | |
155 | | struct erf_meta_section { |
156 | | uint16_t type; |
157 | | uint16_t section_id; |
158 | | uint16_t section_length; |
159 | | GPtrArray *tags; |
160 | | }; |
161 | | |
162 | | struct erf_meta_tag { |
163 | | uint16_t type; |
164 | | uint16_t length; |
165 | | uint8_t *value; |
166 | | }; |
167 | | |
168 | | struct erf_meta_read_state { |
169 | | uint8_t *tag_ptr; |
170 | | uint32_t remaining_len; |
171 | | |
172 | | struct erf_if_mapping *if_map; |
173 | | |
174 | | uint16_t sectiontype; |
175 | | uint16_t sectionid; |
176 | | uint16_t parentsectiontype; |
177 | | uint16_t parentsectionid; |
178 | | |
179 | | uint64_t gen_time; |
180 | | |
181 | | int interface_metadata; |
182 | | }; |
183 | | |
184 | | static bool erf_wtap_blocks_to_erf_sections(wtap_block_t block, GPtrArray *sections, uint16_t section_type, uint16_t section_id, wtap_block_foreach_func func); |
185 | | |
186 | | static uint32_t erf_meta_read_tag(struct erf_meta_tag*, uint8_t*, uint32_t); |
187 | | |
188 | | static int erf_file_type_subtype = -1; |
189 | | |
190 | | void register_erf(void); |
191 | | |
192 | 0 | static unsigned erf_anchor_mapping_hash(const void *key) { |
193 | 0 | const struct erf_anchor_mapping *anchor_map = (const struct erf_anchor_mapping*) key; |
194 | |
|
195 | 0 | return ((uint32_t)anchor_map->host_id ^ (uint32_t)anchor_map->anchor_id); |
196 | |
|
197 | 0 | } |
198 | | |
199 | 0 | static gboolean erf_anchor_mapping_equal(const void *a, const void *b) { |
200 | 0 | const struct erf_anchor_mapping *anchor_map_a = (const struct erf_anchor_mapping*) a ; |
201 | 0 | const struct erf_anchor_mapping *anchor_map_b = (const struct erf_anchor_mapping*) b ; |
202 | |
|
203 | 0 | return (anchor_map_a->host_id) == (anchor_map_b->host_id) && |
204 | 0 | (anchor_map_a->anchor_id & ERF_EXT_HDR_TYPE_ANCHOR_ID) == (anchor_map_b->anchor_id & ERF_EXT_HDR_TYPE_ANCHOR_ID); |
205 | 0 | } |
206 | | |
207 | 0 | static void erf_anchor_mapping_destroy(void *key) { |
208 | 0 | struct erf_anchor_mapping *anchor_map = (struct erf_anchor_mapping*) key; |
209 | |
|
210 | 0 | if(anchor_map->comment != NULL) { |
211 | 0 | g_free(anchor_map->comment); |
212 | 0 | anchor_map->comment = NULL; |
213 | 0 | } |
214 | 0 | g_free(anchor_map); |
215 | 0 | anchor_map = NULL; |
216 | 0 | } |
217 | | |
218 | | static gboolean erf_if_mapping_equal(const void *a, const void *b) |
219 | 0 | { |
220 | 0 | const struct erf_if_mapping *if_map_a = (const struct erf_if_mapping*) a; |
221 | 0 | const struct erf_if_mapping *if_map_b = (const struct erf_if_mapping*) b; |
222 | |
|
223 | 0 | return if_map_a->source_id == if_map_b->source_id && if_map_a->host_id == if_map_b->host_id; |
224 | 0 | } |
225 | | |
226 | | static unsigned erf_if_mapping_hash(const void *key) |
227 | 0 | { |
228 | 0 | const struct erf_if_mapping *if_map = (const struct erf_if_mapping*) key; |
229 | |
|
230 | 0 | return (((unsigned) if_map->host_id) << 16) | if_map->source_id; |
231 | 0 | } |
232 | | |
233 | | static void erf_if_mapping_destroy(void *key) |
234 | 0 | { |
235 | 0 | int i = 0; |
236 | 0 | struct erf_if_mapping *if_map = (struct erf_if_mapping*) key; |
237 | |
|
238 | 0 | for (i = 0; i < ERF_MAX_INTERFACES; i++) { |
239 | 0 | g_free(if_map->interfaces[i].name); |
240 | 0 | g_free(if_map->interfaces[i].descr); |
241 | 0 | } |
242 | |
|
243 | 0 | g_free(if_map->module_filter_str); |
244 | 0 | g_free(if_map); |
245 | 0 | } |
246 | | |
247 | | static struct erf_if_mapping* erf_if_mapping_create(uint64_t host_id, uint8_t source_id) |
248 | 0 | { |
249 | 0 | int i = 0; |
250 | 0 | struct erf_if_mapping *if_map = NULL; |
251 | |
|
252 | 0 | if_map = g_new0(struct erf_if_mapping, 1); |
253 | |
|
254 | 0 | if_map->host_id = host_id; |
255 | 0 | if_map->source_id = source_id; |
256 | |
|
257 | 0 | for (i = 0; i < ERF_MAX_INTERFACES; i++) { |
258 | 0 | if_map->interfaces[i].if_index = -1; |
259 | 0 | if_map->interfaces[i].stream_num = -1; |
260 | 0 | } |
261 | |
|
262 | 0 | if_map->module_fcs_len = -1; |
263 | 0 | if_map->module_snaplen = (uint32_t) -1; |
264 | | /* everything else 0 by g_malloc0*/ |
265 | |
|
266 | 0 | return if_map; |
267 | 0 | } |
268 | | |
269 | | |
270 | | erf_t *erf_priv_create(void) |
271 | 0 | { |
272 | 0 | erf_t *erf_priv; |
273 | |
|
274 | 0 | erf_priv = g_new(erf_t, 1); |
275 | 0 | erf_priv->anchor_map = g_hash_table_new_full(erf_anchor_mapping_hash, erf_anchor_mapping_equal, erf_anchor_mapping_destroy, NULL); |
276 | 0 | erf_priv->if_map = g_hash_table_new_full(erf_if_mapping_hash, erf_if_mapping_equal, erf_if_mapping_destroy, NULL); |
277 | 0 | erf_priv->implicit_host_id = ERF_META_HOST_ID_IMPLICIT; |
278 | 0 | erf_priv->capture_gentime = 0; |
279 | 0 | erf_priv->host_gentime = 0; |
280 | |
|
281 | 0 | return erf_priv; |
282 | 0 | } |
283 | | |
284 | | erf_t* erf_priv_free(erf_t* erf_priv) |
285 | 0 | { |
286 | 0 | if (erf_priv) |
287 | 0 | { |
288 | 0 | g_hash_table_destroy(erf_priv->anchor_map); |
289 | 0 | g_hash_table_destroy(erf_priv->if_map); |
290 | 0 | g_free(erf_priv); |
291 | 0 | } |
292 | |
|
293 | 0 | return NULL; |
294 | 0 | } |
295 | | |
296 | 0 | static void erf_dump_priv_free(erf_dump_t *dump_priv) { |
297 | 0 | if(dump_priv) { |
298 | 0 | if(dump_priv->periodic_sections) { |
299 | 0 | g_ptr_array_free(dump_priv->periodic_sections, true); |
300 | 0 | } |
301 | 0 | if(dump_priv->periodic_extra_ehdrs) { |
302 | 0 | g_array_free(dump_priv->periodic_extra_ehdrs, true); |
303 | 0 | } |
304 | 0 | if(dump_priv->user_comment_ptr) { |
305 | 0 | g_free(dump_priv->user_comment_ptr); |
306 | 0 | } |
307 | |
|
308 | 0 | g_free(dump_priv->rand); |
309 | |
|
310 | 0 | g_free(dump_priv); |
311 | 0 | } |
312 | |
|
313 | 0 | } |
314 | | |
315 | 0 | static void erf_meta_section_free(void *data) { |
316 | 0 | struct erf_meta_section *section_ptr = (struct erf_meta_section*) data; |
317 | 0 | if (section_ptr) { |
318 | 0 | g_ptr_array_free(section_ptr->tags, true); |
319 | 0 | section_ptr->tags = NULL; |
320 | 0 | } |
321 | 0 | g_free(section_ptr); |
322 | 0 | } |
323 | | |
324 | 0 | static void erf_meta_tag_free(void *data) { |
325 | 0 | struct erf_meta_tag *tag_ptr = (struct erf_meta_tag*) data; |
326 | 0 | if (tag_ptr) { |
327 | 0 | g_free(tag_ptr->value); |
328 | 0 | tag_ptr->value = NULL; |
329 | 0 | } |
330 | 0 | g_free(tag_ptr); |
331 | 0 | } |
332 | | |
333 | | |
334 | 0 | static bool erf_dump_finish(struct wtap_dumper *wdh, int *err, char **err_info _U_) { |
335 | 0 | erf_dump_t *dump_priv = (erf_dump_t*)wdh->priv; |
336 | 0 | bool ret = true; |
337 | | |
338 | | /* Write final metadata record. There are some corner cases where we should |
339 | | * do this (file <1 second, last record was ERF_TYPE_META with an out of date |
340 | | * comment) and there is no harm doing this always if we have already written |
341 | | * some metadata. */ |
342 | 0 | if(dump_priv->write_next_extra_meta) { |
343 | 0 | if (!dump_priv->periodic_sections) { |
344 | 0 | dump_priv->periodic_sections = g_ptr_array_new_with_free_func(erf_meta_section_free); |
345 | 0 | if (dump_priv->prev_erf_type == ERF_TYPE_META && dump_priv->last_meta_periodic) { |
346 | 0 | erf_comment_to_sections(wdh, ERF_META_SECTION_CAPTURE, 0, dump_priv->user_comment_ptr, dump_priv->periodic_sections); |
347 | 0 | } else { |
348 | | /* If we get here, metadata record was not found in the first ~1 sec |
349 | | * but we have either a capture comment or a non-ERF file (see |
350 | | * erf_dump_open) */ |
351 | 0 | erf_wtap_info_to_sections(wdh, dump_priv->periodic_sections); |
352 | 0 | } |
353 | 0 | } |
354 | |
|
355 | 0 | if (!erf_write_meta_record(wdh, dump_priv, dump_priv->prev_frame_ts, dump_priv->periodic_sections, dump_priv->periodic_extra_ehdrs, err)) ret = false; |
356 | 0 | } |
357 | | |
358 | | /* Clean up */ |
359 | 0 | erf_dump_priv_free(dump_priv); |
360 | | /* Avoid double freeing by setting it to NULL*/ |
361 | 0 | wdh->priv = NULL; |
362 | |
|
363 | 0 | return ret; |
364 | |
|
365 | 0 | } |
366 | | |
367 | | static void |
368 | | erf_free_data(void *data, void *user_data _U_) |
369 | 0 | { |
370 | 0 | g_free(data); |
371 | 0 | } |
372 | | |
373 | | |
374 | | extern wtap_open_return_val erf_open(wtap *wth, int *err, char **err_info) |
375 | 0 | { |
376 | 0 | int i, n, records_for_erf_check = RECORDS_FOR_ERF_CHECK; |
377 | 0 | int valid_prev = 0; |
378 | 0 | char *s; |
379 | 0 | erf_timestamp_t prevts,ts; |
380 | 0 | erf_header_t header; |
381 | 0 | uint32_t mc_hdr; |
382 | 0 | struct erf_eth_hdr eth_hdr; |
383 | 0 | uint32_t packet_size; |
384 | 0 | uint16_t rlen; |
385 | 0 | uint64_t erf_ext_header; |
386 | 0 | unsigned erf_ext_header_size = (unsigned)sizeof(erf_ext_header); |
387 | 0 | uint8_t type; |
388 | |
|
389 | 0 | prevts = 0; |
390 | | |
391 | | /* number of records to scan before deciding if this really is ERF */ |
392 | 0 | if ((s = getenv("ERF_RECORDS_TO_CHECK")) != NULL) { |
393 | 0 | if (ws_strtoi32(s, NULL, &n) && n >= 0 && n < 101) { |
394 | 0 | records_for_erf_check = n; |
395 | 0 | } |
396 | 0 | } |
397 | | |
398 | | /* |
399 | | * ERF is a little hard because there's no magic number; we look at |
400 | | * the first few records and see if they look enough like ERF |
401 | | * records. |
402 | | */ |
403 | |
|
404 | 0 | for (i = 0; i < records_for_erf_check; i++) { /* records_for_erf_check */ |
405 | |
|
406 | 0 | if (!wtap_read_bytes_or_eof(wth->fh,&header,erf_header_size,err,err_info)) { |
407 | 0 | if (*err == 0) { |
408 | | /* EOF - all records have been successfully checked, accept the file */ |
409 | 0 | break; |
410 | 0 | } |
411 | 0 | if (*err == WTAP_ERR_SHORT_READ) { |
412 | | /* ERF header too short accept the file, |
413 | | only if the very first records have been successfully checked */ |
414 | 0 | if (i < MIN_RECORDS_FOR_ERF_CHECK) { |
415 | 0 | return WTAP_OPEN_NOT_MINE; |
416 | 0 | } else { |
417 | | /* BREAK, the last record is too short, and will be ignored */ |
418 | 0 | break; |
419 | 0 | } |
420 | 0 | } else { |
421 | 0 | return WTAP_OPEN_ERROR; |
422 | 0 | } |
423 | 0 | } |
424 | | |
425 | 0 | rlen=g_ntohs(header.rlen); |
426 | | |
427 | | /* fail on invalid record type, invalid rlen, timestamps decreasing, or incrementing too far */ |
428 | | |
429 | | /* Test valid rlen >= 16 */ |
430 | 0 | if (rlen < 16) { |
431 | 0 | return WTAP_OPEN_NOT_MINE; |
432 | 0 | } |
433 | | |
434 | 0 | packet_size = rlen - erf_header_size; |
435 | 0 | if (packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) { |
436 | | /* |
437 | | * Probably a corrupt capture file or a file that's not an ERF file |
438 | | * but that passed earlier tests. |
439 | | */ |
440 | 0 | return WTAP_OPEN_NOT_MINE; |
441 | 0 | } |
442 | | |
443 | | /* Skip PAD records, timestamps may not be set */ |
444 | 0 | if ((header.type & 0x7F) == ERF_TYPE_PAD) { |
445 | 0 | if (!wtap_read_bytes(wth->fh, NULL, packet_size, err, err_info)) { |
446 | 0 | if (*err != WTAP_ERR_SHORT_READ) { |
447 | | /* A real error */ |
448 | 0 | return WTAP_OPEN_ERROR; |
449 | 0 | } |
450 | | /* ERF record too short, accept the file, |
451 | | only if the very first records have been successfully checked */ |
452 | 0 | if (i < MIN_RECORDS_FOR_ERF_CHECK) { |
453 | 0 | return WTAP_OPEN_NOT_MINE; |
454 | 0 | } |
455 | 0 | } |
456 | 0 | continue; |
457 | 0 | } |
458 | | |
459 | | /* ERF Type 0 is reserved for ancient legacy records which are not supported, probably not ERF */ |
460 | 0 | if ((header.type & 0x7F) == 0) { |
461 | 0 | return WTAP_OPEN_NOT_MINE; |
462 | 0 | } |
463 | | |
464 | | /* fail on decreasing timestamps */ |
465 | 0 | if ((ts = pletohu64(&header.ts)) < prevts) { |
466 | | /* reassembled AALx records may not be in time order, also records are not in strict time order between physical interfaces, so allow 1 sec fudge */ |
467 | 0 | if ( ((prevts-ts)>>32) > 1 ) { |
468 | 0 | return WTAP_OPEN_NOT_MINE; |
469 | 0 | } |
470 | 0 | } |
471 | | |
472 | | /* Check to see if timestamp increment is > 1 year */ |
473 | 0 | if ( (valid_prev) && (ts > prevts) && (((ts-prevts)>>32) > 3600*24*365) ) { |
474 | 0 | return WTAP_OPEN_NOT_MINE; |
475 | 0 | } |
476 | | |
477 | 0 | prevts = ts; |
478 | | |
479 | | /* Read over the extension headers */ |
480 | 0 | type = header.type; |
481 | 0 | while (type & 0x80){ |
482 | 0 | if (!wtap_read_bytes(wth->fh,&erf_ext_header,erf_ext_header_size,err,err_info)) { |
483 | 0 | if (*err == WTAP_ERR_SHORT_READ) { |
484 | | /* Extension header missing, not an ERF file */ |
485 | 0 | return WTAP_OPEN_NOT_MINE; |
486 | 0 | } |
487 | 0 | return WTAP_OPEN_ERROR; |
488 | 0 | } |
489 | 0 | if (packet_size < erf_ext_header_size) |
490 | 0 | return WTAP_OPEN_NOT_MINE; |
491 | 0 | packet_size -= erf_ext_header_size; |
492 | 0 | memcpy(&type, &erf_ext_header, sizeof(type)); |
493 | 0 | } |
494 | | |
495 | | |
496 | | /* Read over MC or ETH subheader */ |
497 | 0 | switch(header.type & 0x7F) { |
498 | 0 | case ERF_TYPE_MC_HDLC: |
499 | 0 | case ERF_TYPE_MC_RAW: |
500 | 0 | case ERF_TYPE_MC_ATM: |
501 | 0 | case ERF_TYPE_MC_RAW_CHANNEL: |
502 | 0 | case ERF_TYPE_MC_AAL5: |
503 | 0 | case ERF_TYPE_MC_AAL2: |
504 | 0 | case ERF_TYPE_COLOR_MC_HDLC_POS: |
505 | 0 | case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */ |
506 | 0 | if (!wtap_read_bytes(wth->fh,&mc_hdr,erf_mc_header_size,err,err_info)) { |
507 | 0 | if (*err == WTAP_ERR_SHORT_READ) { |
508 | | /* Subheader missing, not an ERF file */ |
509 | 0 | return WTAP_OPEN_NOT_MINE; |
510 | 0 | } |
511 | 0 | return WTAP_OPEN_ERROR; |
512 | 0 | } |
513 | 0 | if (packet_size < erf_mc_header_size) |
514 | 0 | return WTAP_OPEN_NOT_MINE; |
515 | 0 | packet_size -= erf_mc_header_size; |
516 | 0 | break; |
517 | 0 | case ERF_TYPE_ETH: |
518 | 0 | case ERF_TYPE_COLOR_ETH: |
519 | 0 | case ERF_TYPE_DSM_COLOR_ETH: |
520 | 0 | case ERF_TYPE_COLOR_HASH_ETH: |
521 | 0 | if (!wtap_read_bytes(wth->fh,ð_hdr,erf_eth_hdr_size,err,err_info)) { |
522 | 0 | if (*err == WTAP_ERR_SHORT_READ) { |
523 | | /* Subheader missing, not an ERF file */ |
524 | 0 | return WTAP_OPEN_NOT_MINE; |
525 | 0 | } |
526 | 0 | return WTAP_OPEN_ERROR; |
527 | 0 | } |
528 | 0 | if (packet_size < erf_eth_hdr_size) |
529 | 0 | return WTAP_OPEN_NOT_MINE; |
530 | 0 | packet_size -= erf_eth_hdr_size; |
531 | 0 | break; |
532 | 0 | default: |
533 | 0 | break; |
534 | 0 | } |
535 | | |
536 | 0 | if (!wtap_read_bytes(wth->fh, NULL, packet_size, err, err_info)) { |
537 | 0 | if (*err != WTAP_ERR_SHORT_READ) { |
538 | | /* A real error */ |
539 | 0 | return WTAP_OPEN_ERROR; |
540 | 0 | } |
541 | | /* ERF record too short, accept the file, |
542 | | only if the very first records have been successfully checked */ |
543 | 0 | if (i < MIN_RECORDS_FOR_ERF_CHECK) { |
544 | 0 | return WTAP_OPEN_NOT_MINE; |
545 | 0 | } |
546 | 0 | } |
547 | | |
548 | 0 | valid_prev = 1; |
549 | |
|
550 | 0 | } /* records_for_erf_check */ |
551 | | |
552 | 0 | if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) { /* rewind */ |
553 | 0 | return WTAP_OPEN_ERROR; |
554 | 0 | } |
555 | | |
556 | | /* This is an ERF file */ |
557 | 0 | wth->file_type_subtype = erf_file_type_subtype; |
558 | 0 | wth->snapshot_length = 0; /* not available in header, only in frame */ |
559 | | |
560 | | /* |
561 | | * Use the encapsulation for ERF records. |
562 | | */ |
563 | 0 | wth->file_encap = WTAP_ENCAP_ERF; |
564 | |
|
565 | 0 | wth->subtype_read = erf_read; |
566 | 0 | wth->subtype_seek_read = erf_seek_read; |
567 | 0 | wth->subtype_close = erf_close; |
568 | 0 | wth->file_tsprec = WTAP_TSPREC_NSEC; |
569 | |
|
570 | 0 | wth->priv = erf_priv_create(); |
571 | |
|
572 | 0 | return WTAP_OPEN_MINE; |
573 | 0 | } |
574 | | |
575 | | /* Read the next packet */ |
576 | | static bool erf_read(wtap *wth, wtap_rec *rec, |
577 | | int *err, char **err_info, int64_t *data_offset) |
578 | 0 | { |
579 | 0 | erf_header_t erf_header; |
580 | 0 | uint32_t packet_size, bytes_read; |
581 | 0 | GPtrArray *anchor_mappings_to_update; |
582 | |
|
583 | 0 | *data_offset = file_tell(wth->fh); |
584 | |
|
585 | 0 | anchor_mappings_to_update = g_ptr_array_new_with_free_func(erf_anchor_mapping_destroy); |
586 | |
|
587 | 0 | do { |
588 | 0 | if (!erf_read_header(wth, wth->fh, rec, &erf_header, |
589 | 0 | err, err_info, &bytes_read, &packet_size, |
590 | 0 | anchor_mappings_to_update)) { |
591 | 0 | g_ptr_array_free(anchor_mappings_to_update, true); |
592 | 0 | return false; |
593 | 0 | } |
594 | | |
595 | 0 | if (!wtap_read_bytes_buffer(wth->fh, &rec->data, packet_size, err, err_info)) { |
596 | 0 | g_ptr_array_free(anchor_mappings_to_update, true); |
597 | 0 | return false; |
598 | 0 | } |
599 | | |
600 | | /* |
601 | | * If Provenance metadata record, frame buffer could hold the meta erf tags. |
602 | | * It can also contain per packet comments which can be associated to another |
603 | | * frame. |
604 | | */ |
605 | 0 | if ((erf_header.type & 0x7F) == ERF_TYPE_META && packet_size > 0) |
606 | 0 | { |
607 | 0 | if (populate_summary_info((erf_t*) wth->priv, wth, rec, packet_size, anchor_mappings_to_update, err, err_info) < 0) { |
608 | 0 | g_ptr_array_free(anchor_mappings_to_update, true); |
609 | 0 | return false; |
610 | 0 | } |
611 | 0 | } |
612 | |
|
613 | 0 | } while ( erf_header.type == ERF_TYPE_PAD ); |
614 | | |
615 | 0 | g_ptr_array_free(anchor_mappings_to_update, true); |
616 | |
|
617 | 0 | return true; |
618 | 0 | } |
619 | | |
620 | | static bool erf_seek_read(wtap *wth, int64_t seek_off, |
621 | | wtap_rec *rec, |
622 | | int *err, char **err_info) |
623 | 0 | { |
624 | 0 | erf_header_t erf_header; |
625 | 0 | uint32_t packet_size; |
626 | 0 | GPtrArray *anchor_mappings_to_update; |
627 | |
|
628 | 0 | if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1) |
629 | 0 | return false; |
630 | | |
631 | 0 | anchor_mappings_to_update = g_ptr_array_new_with_free_func(erf_anchor_mapping_destroy); |
632 | |
|
633 | 0 | do { |
634 | 0 | if (!erf_read_header(wth, wth->random_fh, rec, &erf_header, |
635 | 0 | err, err_info, NULL, &packet_size, anchor_mappings_to_update)) { |
636 | 0 | g_ptr_array_free(anchor_mappings_to_update, true); |
637 | 0 | return false; |
638 | 0 | } |
639 | 0 | } while ( erf_header.type == ERF_TYPE_PAD ); |
640 | | |
641 | 0 | g_ptr_array_free(anchor_mappings_to_update, true); |
642 | |
|
643 | 0 | return wtap_read_bytes_buffer(wth->random_fh, &rec->data, packet_size, |
644 | 0 | err, err_info); |
645 | 0 | } |
646 | | |
647 | | static struct erf_anchor_mapping* erf_find_anchor_mapping(erf_t *priv, |
648 | | uint64_t host_id, |
649 | | uint64_t anchor_id) |
650 | 0 | { |
651 | 0 | struct erf_anchor_mapping mapping = { |
652 | 0 | host_id, |
653 | 0 | anchor_id, |
654 | 0 | 0, |
655 | 0 | NULL |
656 | 0 | }; |
657 | |
|
658 | 0 | if (!priv) { |
659 | 0 | return NULL; |
660 | 0 | } |
661 | | |
662 | 0 | return (struct erf_anchor_mapping*)g_hash_table_lookup(priv->anchor_map, &mapping); |
663 | |
|
664 | 0 | } |
665 | | |
666 | | static bool erf_read_header(wtap *wth, FILE_T fh, |
667 | | wtap_rec *rec, |
668 | | erf_header_t *erf_header, |
669 | | int *err, |
670 | | char **err_info, |
671 | | uint32_t *bytes_read, |
672 | | uint32_t *packet_size, |
673 | | GPtrArray *anchor_mappings_to_update) |
674 | 0 | { |
675 | 0 | union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header; |
676 | 0 | uint8_t erf_exhdr[8]; |
677 | 0 | uint64_t erf_exhdr_sw; |
678 | 0 | uint8_t type = 0; |
679 | 0 | uint32_t mc_hdr; |
680 | 0 | uint32_t aal2_hdr; |
681 | 0 | struct wtap_erf_eth_hdr eth_hdr; |
682 | 0 | uint32_t skiplen = 0; |
683 | 0 | int i = 0; |
684 | 0 | int max = array_length(pseudo_header->erf.ehdr_list); |
685 | 0 | erf_t *priv = (erf_t*)wth->priv; |
686 | 0 | int interface_id; |
687 | |
|
688 | 0 | uint64_t host_id = ERF_META_HOST_ID_IMPLICIT; |
689 | 0 | uint8_t source_id = 0; |
690 | 0 | uint8_t if_num = 0; |
691 | 0 | bool host_id_found = false; |
692 | |
|
693 | 0 | if (!wtap_read_bytes_or_eof(fh, erf_header, sizeof(*erf_header), err, err_info)) { |
694 | 0 | return false; |
695 | 0 | } |
696 | 0 | if (bytes_read != NULL) { |
697 | 0 | *bytes_read = sizeof(*erf_header); |
698 | 0 | } |
699 | |
|
700 | 0 | *packet_size = g_ntohs(erf_header->rlen) - (uint32_t)sizeof(*erf_header); |
701 | |
|
702 | 0 | if (*packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) { |
703 | | /* |
704 | | * Probably a corrupt capture file; don't blow up trying |
705 | | * to allocate space for an immensely-large packet. |
706 | | */ |
707 | 0 | *err = WTAP_ERR_BAD_FILE; |
708 | 0 | *err_info = ws_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u", |
709 | 0 | *packet_size, WTAP_MAX_PACKET_SIZE_STANDARD); |
710 | 0 | return false; |
711 | 0 | } |
712 | | |
713 | 0 | if (*packet_size == 0) { |
714 | | /* If this isn't a pad record, it's a corrupt packet; bail out */ |
715 | 0 | if ((erf_header->type & 0x7F) != ERF_TYPE_PAD) { |
716 | 0 | *err = WTAP_ERR_BAD_FILE; |
717 | 0 | *err_info = g_strdup("erf: File has 0 byte packet"); |
718 | |
|
719 | 0 | return false; |
720 | 0 | } |
721 | 0 | } |
722 | | |
723 | 0 | { |
724 | 0 | uint64_t ts = pletohu64(&erf_header->ts); |
725 | | |
726 | | /*if ((erf_header->type & 0x7f) != ERF_TYPE_META || wth->file_type_subtype != file_type_subtype_erf) {*/ |
727 | 0 | wtap_setup_packet_rec(rec, wth->file_encap); |
728 | 0 | rec->block = wtap_block_create(WTAP_BLOCK_PACKET); |
729 | | /* |
730 | | * XXX: ERF_TYPE_META records should ideally be FT_SPECIFIC for display |
731 | | * purposes, but currently ft_specific_record_phdr clashes with erf_mc_phdr |
732 | | * and the pcapng dumper assumes it is a pcapng block type. Ideally we |
733 | | * would register a block handler with pcapng and write out the closest |
734 | | * pcapng block, or a custom block/Provenance record. |
735 | | * |
736 | | */ |
737 | | #if 0 |
738 | | } else { |
739 | | /* |
740 | | * TODO: how to identify, distinguish and timestamp events? |
741 | | * What to do about ENCAP_ERF in pcap/pcapng? Filetype dissector is |
742 | | * chosen by wth->file_type_subtype? |
743 | | */ |
744 | | /* For now just treat all Provenance records as reports */ |
745 | | wtap_setup_ft_specific_report_rec(rec, erf_file_type_subtype, |
746 | | 0x00000000); /* XXX - record type */ |
747 | | rec->block = wtap_block_create(WTAP_BLOCK_FT_SPECIFIC_REPORT); |
748 | | /* XXX: phdr ft_specific_record_phdr? */ |
749 | | } |
750 | | #endif |
751 | 0 | rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID; |
752 | 0 | rec->ts.secs = (long) (ts >> 32); |
753 | 0 | ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000); |
754 | 0 | ts += (ts & 0x80000000) << 1; /* rounding */ |
755 | 0 | rec->ts.nsecs = ((int) (ts >> 32)); |
756 | 0 | if (rec->ts.nsecs >= 1000000000) { |
757 | 0 | rec->ts.nsecs -= 1000000000; |
758 | 0 | rec->ts.secs += 1; |
759 | 0 | } |
760 | |
|
761 | 0 | if_num = erf_interface_id_from_flags(erf_header->flags); |
762 | 0 | } |
763 | | |
764 | | /* Copy the ERF pseudo header */ |
765 | 0 | memset(&pseudo_header->erf, 0, sizeof(pseudo_header->erf)); |
766 | 0 | pseudo_header->erf.phdr.ts = pletohu64(&erf_header->ts); |
767 | 0 | pseudo_header->erf.phdr.type = erf_header->type; |
768 | 0 | pseudo_header->erf.phdr.flags = erf_header->flags; |
769 | 0 | pseudo_header->erf.phdr.rlen = g_ntohs(erf_header->rlen); |
770 | 0 | pseudo_header->erf.phdr.lctr = g_ntohs(erf_header->lctr); |
771 | 0 | pseudo_header->erf.phdr.wlen = g_ntohs(erf_header->wlen); |
772 | | |
773 | | /* Copy the ERF extension header into the pseudo header */ |
774 | 0 | type = erf_header->type; |
775 | 0 | while (type & 0x80){ |
776 | 0 | if (!wtap_read_bytes(fh, &erf_exhdr, sizeof(erf_exhdr), |
777 | 0 | err, err_info)) |
778 | 0 | return false; |
779 | 0 | if (bytes_read != NULL) |
780 | 0 | *bytes_read += (uint32_t)sizeof(erf_exhdr); |
781 | 0 | *packet_size -= (uint32_t)sizeof(erf_exhdr); |
782 | 0 | skiplen += (uint32_t)sizeof(erf_exhdr); |
783 | 0 | erf_exhdr_sw = pntohu64(erf_exhdr); |
784 | 0 | if (i < max) |
785 | 0 | memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw)); |
786 | 0 | type = erf_exhdr[0]; |
787 | | |
788 | | /* |
789 | | * XXX: Only want first Source ID and Host ID, and want to preserve HID n SID 0 (see |
790 | | * erf_populate_interface) |
791 | | */ |
792 | 0 | switch (type & 0x7FU) { |
793 | 0 | case ERF_EXT_HDR_TYPE_HOST_ID: |
794 | 0 | if (!host_id_found) |
795 | 0 | host_id = erf_exhdr_sw & ERF_EHDR_HOST_ID_MASK; |
796 | |
|
797 | 0 | host_id_found = true; |
798 | | /* Fall through */ |
799 | 0 | case ERF_EXT_HDR_TYPE_FLOW_ID: |
800 | | /* Source ID is present in both Flow ID and Host ID extension headers */ |
801 | 0 | if (!source_id) |
802 | 0 | source_id = (erf_exhdr_sw >> 48) & 0xff; |
803 | 0 | break; |
804 | 0 | case ERF_EXT_HDR_TYPE_ANCHOR_ID: |
805 | | /* handled below*/ |
806 | 0 | break; |
807 | 0 | } |
808 | 0 | i++; |
809 | 0 | } |
810 | | |
811 | 0 | interface_id = erf_populate_interface((erf_t*) wth->priv, wth, pseudo_header, host_id, source_id, if_num, err, err_info); |
812 | 0 | if (interface_id < 0) { |
813 | 0 | return false; |
814 | 0 | } |
815 | 0 | rec->rec_header.packet_header.interface_id = (unsigned) interface_id; |
816 | | |
817 | | /* Try to find comment links using Anchor ID. Done here after we found the first Host ID and have updated the implicit Host ID. */ |
818 | 0 | erf_update_anchors_from_header(priv, rec, pseudo_header, host_id, anchor_mappings_to_update); |
819 | |
|
820 | 0 | switch (erf_header->type & 0x7F) { |
821 | 0 | case ERF_TYPE_IPV4: |
822 | 0 | case ERF_TYPE_IPV6: |
823 | 0 | case ERF_TYPE_RAW_LINK: |
824 | 0 | case ERF_TYPE_INFINIBAND: |
825 | 0 | case ERF_TYPE_INFINIBAND_LINK: |
826 | 0 | case ERF_TYPE_META: |
827 | 0 | case ERF_TYPE_OPA_SNC: |
828 | 0 | case ERF_TYPE_OPA_9B: |
829 | | #if 0 |
830 | | { |
831 | | rec->rec_header.packet_header.len = g_htons(erf_header->wlen); |
832 | | rec->rec_header.packet_header.caplen = g_htons(erf_header->wlen); |
833 | | } |
834 | | return true; |
835 | | #endif |
836 | 0 | break; |
837 | 0 | case ERF_TYPE_PAD: |
838 | 0 | case ERF_TYPE_HDLC_POS: |
839 | 0 | case ERF_TYPE_COLOR_HDLC_POS: |
840 | 0 | case ERF_TYPE_DSM_COLOR_HDLC_POS: |
841 | 0 | case ERF_TYPE_COLOR_HASH_POS: |
842 | 0 | case ERF_TYPE_ATM: |
843 | 0 | case ERF_TYPE_AAL5: |
844 | 0 | break; |
845 | | |
846 | 0 | case ERF_TYPE_ETH: |
847 | 0 | case ERF_TYPE_COLOR_ETH: |
848 | 0 | case ERF_TYPE_DSM_COLOR_ETH: |
849 | 0 | case ERF_TYPE_COLOR_HASH_ETH: |
850 | 0 | if (!wtap_read_bytes(fh, ð_hdr, sizeof(eth_hdr), err, err_info)) |
851 | 0 | return false; |
852 | 0 | if (bytes_read != NULL) |
853 | 0 | *bytes_read += (uint32_t)sizeof(eth_hdr); |
854 | 0 | *packet_size -= (uint32_t)sizeof(eth_hdr); |
855 | 0 | skiplen += (uint32_t)sizeof(eth_hdr); |
856 | 0 | pseudo_header->erf.subhdr.eth_hdr = eth_hdr; |
857 | 0 | break; |
858 | | |
859 | 0 | case ERF_TYPE_MC_HDLC: |
860 | 0 | case ERF_TYPE_MC_RAW: |
861 | 0 | case ERF_TYPE_MC_ATM: |
862 | 0 | case ERF_TYPE_MC_RAW_CHANNEL: |
863 | 0 | case ERF_TYPE_MC_AAL5: |
864 | 0 | case ERF_TYPE_MC_AAL2: |
865 | 0 | case ERF_TYPE_COLOR_MC_HDLC_POS: |
866 | 0 | if (!wtap_read_bytes(fh, &mc_hdr, sizeof(mc_hdr), err, err_info)) |
867 | 0 | return false; |
868 | 0 | if (bytes_read != NULL) |
869 | 0 | *bytes_read += (uint32_t)sizeof(mc_hdr); |
870 | 0 | *packet_size -= (uint32_t)sizeof(mc_hdr); |
871 | 0 | skiplen += (uint32_t)sizeof(mc_hdr); |
872 | 0 | pseudo_header->erf.subhdr.mc_hdr = g_ntohl(mc_hdr); |
873 | 0 | break; |
874 | | |
875 | 0 | case ERF_TYPE_AAL2: |
876 | 0 | if (!wtap_read_bytes(fh, &aal2_hdr, sizeof(aal2_hdr), err, err_info)) |
877 | 0 | return false; |
878 | 0 | if (bytes_read != NULL) |
879 | 0 | *bytes_read += (uint32_t)sizeof(aal2_hdr); |
880 | 0 | *packet_size -= (uint32_t)sizeof(aal2_hdr); |
881 | 0 | skiplen += (uint32_t)sizeof(aal2_hdr); |
882 | 0 | pseudo_header->erf.subhdr.aal2_hdr = g_ntohl(aal2_hdr); |
883 | 0 | break; |
884 | | |
885 | 0 | case ERF_TYPE_IP_COUNTER: |
886 | 0 | case ERF_TYPE_TCP_FLOW_COUNTER: |
887 | | /* unsupported, continue with default: */ |
888 | 0 | default: |
889 | | /* let the dissector dissect as unknown record type for forwards compatibility */ |
890 | 0 | break; |
891 | 0 | } |
892 | | |
893 | 0 | { |
894 | 0 | rec->rec_header.packet_header.len = g_ntohs(erf_header->wlen); |
895 | 0 | rec->rec_header.packet_header.caplen = MIN( g_ntohs(erf_header->wlen), |
896 | 0 | g_ntohs(erf_header->rlen) - (uint32_t)sizeof(*erf_header) - skiplen ); |
897 | 0 | } |
898 | |
|
899 | 0 | if (*packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) { |
900 | | /* |
901 | | * Probably a corrupt capture file; don't blow up trying |
902 | | * to allocate space for an immensely-large packet. |
903 | | */ |
904 | 0 | *err = WTAP_ERR_BAD_FILE; |
905 | 0 | *err_info = ws_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u", |
906 | 0 | *packet_size, WTAP_MAX_PACKET_SIZE_STANDARD); |
907 | 0 | return false; |
908 | 0 | } |
909 | | |
910 | 0 | return true; |
911 | 0 | } |
912 | | |
913 | | static int wtap_wtap_encap_to_erf_encap(int encap) |
914 | 0 | { |
915 | 0 | unsigned int i; |
916 | 0 | for(i = 0; i < NUM_ERF_ENCAPS; i++){ |
917 | 0 | if(erf_to_wtap_map[i].wtap_encap_value == encap) |
918 | 0 | return erf_to_wtap_map[i].erf_encap_value; |
919 | 0 | } |
920 | 0 | return -1; |
921 | 0 | } |
922 | | |
923 | | static bool erf_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pseudo_header *pseudo_header, int * err) |
924 | 0 | { |
925 | 0 | uint8_t erf_hdr[sizeof(struct erf_mc_phdr)]; |
926 | 0 | uint8_t erf_subhdr[sizeof(union erf_subhdr)]; |
927 | 0 | uint8_t ehdr[8*MAX_ERF_EHDR]; |
928 | 0 | size_t size = 0; |
929 | 0 | size_t subhdr_size = 0; |
930 | 0 | int i = 0; |
931 | 0 | uint8_t has_more = 0; |
932 | |
|
933 | 0 | switch(encap){ |
934 | 0 | case WTAP_ENCAP_ERF: |
935 | 0 | memset(&erf_hdr, 0, sizeof(erf_hdr)); |
936 | 0 | phtoleu64(&erf_hdr[0], pseudo_header->erf.phdr.ts); |
937 | 0 | erf_hdr[8] = pseudo_header->erf.phdr.type; |
938 | 0 | erf_hdr[9] = pseudo_header->erf.phdr.flags; |
939 | 0 | phtonu16(&erf_hdr[10], pseudo_header->erf.phdr.rlen); |
940 | 0 | phtonu16(&erf_hdr[12], pseudo_header->erf.phdr.lctr); |
941 | 0 | phtonu16(&erf_hdr[14], pseudo_header->erf.phdr.wlen); |
942 | 0 | size = sizeof(struct erf_phdr); |
943 | |
|
944 | 0 | switch(pseudo_header->erf.phdr.type & 0x7F) { |
945 | 0 | case ERF_TYPE_MC_HDLC: |
946 | 0 | case ERF_TYPE_MC_RAW: |
947 | 0 | case ERF_TYPE_MC_ATM: |
948 | 0 | case ERF_TYPE_MC_RAW_CHANNEL: |
949 | 0 | case ERF_TYPE_MC_AAL5: |
950 | 0 | case ERF_TYPE_MC_AAL2: |
951 | 0 | case ERF_TYPE_COLOR_MC_HDLC_POS: |
952 | 0 | phtonu32(&erf_subhdr[0], pseudo_header->erf.subhdr.mc_hdr); |
953 | 0 | subhdr_size += (int)sizeof(struct erf_mc_hdr); |
954 | 0 | break; |
955 | 0 | case ERF_TYPE_AAL2: |
956 | 0 | phtonu32(&erf_subhdr[0], pseudo_header->erf.subhdr.aal2_hdr); |
957 | 0 | subhdr_size += (int)sizeof(struct erf_aal2_hdr); |
958 | 0 | break; |
959 | 0 | case ERF_TYPE_ETH: |
960 | 0 | case ERF_TYPE_COLOR_ETH: |
961 | 0 | case ERF_TYPE_DSM_COLOR_ETH: |
962 | 0 | case ERF_TYPE_COLOR_HASH_ETH: |
963 | 0 | memcpy(&erf_subhdr[0], &pseudo_header->erf.subhdr.eth_hdr, sizeof pseudo_header->erf.subhdr.eth_hdr); |
964 | 0 | subhdr_size += erf_eth_hdr_size; |
965 | 0 | break; |
966 | 0 | default: |
967 | 0 | break; |
968 | 0 | } |
969 | 0 | break; |
970 | 0 | default: |
971 | 0 | return false; |
972 | |
|
973 | 0 | } |
974 | 0 | if (!wtap_dump_file_write(wdh, erf_hdr, size, err)) |
975 | 0 | return false; |
976 | | |
977 | | /*write out up to MAX_ERF_EHDR extension headers*/ |
978 | 0 | has_more = pseudo_header->erf.phdr.type & 0x80; |
979 | 0 | if(has_more){ /*we have extension headers*/ |
980 | 0 | do{ |
981 | 0 | phtonu64(ehdr+(i*8), pseudo_header->erf.ehdr_list[i].ehdr); |
982 | 0 | if(i == MAX_ERF_EHDR-1) ehdr[i*8] = ehdr[i*8] & 0x7F; |
983 | 0 | has_more = ehdr[i*8] & 0x80; |
984 | 0 | i++; |
985 | 0 | }while(has_more && i < MAX_ERF_EHDR); |
986 | 0 | if (!wtap_dump_file_write(wdh, ehdr, 8*i, err)) |
987 | 0 | return false; |
988 | 0 | } |
989 | | |
990 | 0 | if(!wtap_dump_file_write(wdh, erf_subhdr, subhdr_size, err)) |
991 | 0 | return false; |
992 | | |
993 | 0 | return true; |
994 | 0 | } |
995 | | |
996 | | |
997 | 0 | static void erf_dump_priv_init_gen_time(erf_dump_t *dump_priv) { |
998 | 0 | int64_t real_time; |
999 | |
|
1000 | 0 | real_time = g_get_real_time(); |
1001 | | /* Convert TimeVal to ERF timestamp */ |
1002 | 0 | dump_priv->gen_time = ((real_time / G_USEC_PER_SEC) << 32) + ((real_time % G_USEC_PER_SEC) << 32) / 1000 / 1000; |
1003 | 0 | } |
1004 | | |
1005 | | |
1006 | | static bool erf_write_wtap_option_to_capture_tag(wtap_block_t block _U_, |
1007 | | unsigned option_id, |
1008 | | wtap_opttype_e option_type _U_, |
1009 | | wtap_optval_t *optval, |
1010 | 0 | void* user_data) { |
1011 | |
|
1012 | 0 | struct erf_meta_section *section_ptr = (struct erf_meta_section*) user_data; |
1013 | 0 | struct erf_meta_tag *tag_ptr = NULL; |
1014 | |
|
1015 | 0 | tag_ptr = g_new0(struct erf_meta_tag, 1); |
1016 | |
|
1017 | 0 | switch(option_id) { |
1018 | 0 | case OPT_SHB_USERAPPL: |
1019 | 0 | tag_ptr->type = ERF_META_TAG_app_name; |
1020 | 0 | tag_ptr->value = (uint8_t*)g_strdup(optval->stringval); |
1021 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1022 | 0 | break; |
1023 | 0 | case OPT_COMMENT: |
1024 | 0 | tag_ptr->type = ERF_META_TAG_comment; |
1025 | 0 | tag_ptr->value = (uint8_t*)g_strdup(optval->stringval); |
1026 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1027 | 0 | break; |
1028 | 0 | default: |
1029 | 0 | erf_meta_tag_free(tag_ptr); |
1030 | 0 | tag_ptr = NULL; |
1031 | 0 | break; |
1032 | 0 | } |
1033 | | |
1034 | 0 | if (tag_ptr) |
1035 | 0 | g_ptr_array_add(section_ptr->tags, tag_ptr); |
1036 | |
|
1037 | 0 | return true; /* we always succeed */ |
1038 | 0 | } |
1039 | | |
1040 | | static bool erf_write_wtap_option_to_host_tag(wtap_block_t block _U_, |
1041 | | unsigned option_id, |
1042 | | wtap_opttype_e option_type _U_, |
1043 | | wtap_optval_t *optval, |
1044 | 0 | void* user_data) { |
1045 | |
|
1046 | 0 | struct erf_meta_section *section_ptr = (struct erf_meta_section*) user_data; |
1047 | 0 | struct erf_meta_tag *tag_ptr = NULL; |
1048 | |
|
1049 | 0 | tag_ptr = g_new0(struct erf_meta_tag, 1); |
1050 | |
|
1051 | 0 | switch(option_id) { |
1052 | 0 | case OPT_SHB_HARDWARE: |
1053 | 0 | tag_ptr->type = ERF_META_TAG_cpu; |
1054 | 0 | tag_ptr->value = (uint8_t*)g_strdup(optval->stringval); |
1055 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1056 | 0 | break; |
1057 | 0 | case OPT_SHB_OS: |
1058 | 0 | tag_ptr->type = ERF_META_TAG_os; |
1059 | 0 | tag_ptr->value = (uint8_t*)g_strdup(optval->stringval); |
1060 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1061 | 0 | break; |
1062 | 0 | default: |
1063 | 0 | erf_meta_tag_free(tag_ptr); |
1064 | 0 | tag_ptr = NULL; |
1065 | 0 | break; |
1066 | 0 | } |
1067 | | |
1068 | 0 | if (tag_ptr) |
1069 | 0 | g_ptr_array_add(section_ptr->tags, tag_ptr); |
1070 | |
|
1071 | 0 | return true; /* we always succeed */ |
1072 | 0 | } |
1073 | | |
1074 | | static bool erf_write_wtap_option_to_interface_tag(wtap_block_t block _U_, |
1075 | | unsigned option_id, |
1076 | | wtap_opttype_e option_type _U_, |
1077 | | wtap_optval_t *optval, |
1078 | 0 | void* user_data) { |
1079 | |
|
1080 | 0 | struct erf_meta_section *section_ptr = (struct erf_meta_section*) user_data; |
1081 | 0 | struct erf_meta_tag *tag_ptr = NULL; |
1082 | |
|
1083 | 0 | tag_ptr = g_new0(struct erf_meta_tag, 1); |
1084 | |
|
1085 | 0 | switch(option_id) { |
1086 | 0 | case OPT_COMMENT: |
1087 | 0 | tag_ptr->type = ERF_META_TAG_comment; |
1088 | 0 | tag_ptr->value = (uint8_t*)g_strdup(optval->stringval); |
1089 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1090 | 0 | break; |
1091 | 0 | case OPT_IDB_NAME: |
1092 | 0 | tag_ptr->type = ERF_META_TAG_name; |
1093 | 0 | tag_ptr->value = (uint8_t*)g_strdup(optval->stringval); |
1094 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1095 | 0 | break; |
1096 | 0 | case OPT_IDB_DESCRIPTION: |
1097 | 0 | tag_ptr->type = ERF_META_TAG_descr; |
1098 | 0 | tag_ptr->value = (uint8_t*)g_strdup(optval->stringval); |
1099 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1100 | 0 | break; |
1101 | 0 | case OPT_IDB_OS: |
1102 | 0 | tag_ptr->type = ERF_META_TAG_os; |
1103 | 0 | tag_ptr->value = (uint8_t*)g_strdup(optval->stringval); |
1104 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1105 | 0 | break; |
1106 | 0 | case OPT_IDB_TSOFFSET: |
1107 | 0 | tag_ptr->type = ERF_META_TAG_ts_offset; |
1108 | 0 | tag_ptr->length = 8; |
1109 | 0 | tag_ptr->value = (uint8_t*)g_malloc(sizeof(optval->uint64val)); |
1110 | | /* convert to relative ERF timestamp */ |
1111 | 0 | phtoleu64(tag_ptr->value, optval->uint64val << 32); |
1112 | 0 | break; |
1113 | 0 | case OPT_IDB_SPEED: |
1114 | 0 | tag_ptr->type = ERF_META_TAG_if_speed; |
1115 | 0 | tag_ptr->length = 8; |
1116 | 0 | tag_ptr->value = (uint8_t*)g_malloc(sizeof(optval->uint64val)); |
1117 | 0 | phtonu64(tag_ptr->value, optval->uint64val); |
1118 | 0 | break; |
1119 | 0 | case OPT_IDB_IP4ADDR: |
1120 | 0 | tag_ptr->type = ERF_META_TAG_if_ipv4; |
1121 | 0 | tag_ptr->length = 4; |
1122 | 0 | tag_ptr->value = (uint8_t*)g_malloc(sizeof(optval->ipv4val)); |
1123 | 0 | memcpy(tag_ptr->value, &optval->ipv4val, sizeof(optval->ipv4val)); |
1124 | 0 | break; |
1125 | 0 | case OPT_IDB_IP6ADDR: |
1126 | 0 | tag_ptr->type = ERF_META_TAG_if_ipv6; |
1127 | 0 | tag_ptr->length = 16; |
1128 | 0 | tag_ptr->value = (uint8_t*)g_malloc(sizeof(optval->ipv6val)); |
1129 | 0 | memcpy(tag_ptr->value, &optval->ipv6val, sizeof(optval->ipv6val)); |
1130 | 0 | break; |
1131 | 0 | case OPT_IDB_FILTER: |
1132 | 0 | { |
1133 | 0 | if_filter_opt_t *filter; |
1134 | 0 | filter = &optval->if_filterval; |
1135 | 0 | tag_ptr->type = 0xF800; |
1136 | 0 | if(filter->type == if_filter_pcap) { |
1137 | 0 | tag_ptr->type = ERF_META_TAG_filter; |
1138 | 0 | tag_ptr->value = (uint8_t*)g_strdup(filter->data.filter_str); |
1139 | 0 | tag_ptr->length = (uint16_t)strlen((char*)tag_ptr->value); |
1140 | 0 | } |
1141 | 0 | } |
1142 | 0 | break; |
1143 | 0 | case OPT_IDB_FCSLEN: |
1144 | 0 | tag_ptr->type = ERF_META_TAG_fcs_len; |
1145 | 0 | tag_ptr->length = 4; |
1146 | 0 | tag_ptr->value = (uint8_t*)g_malloc(tag_ptr->length); |
1147 | 0 | phtonu32(tag_ptr->value, (uint32_t)optval->uint8val); |
1148 | 0 | break; |
1149 | | /* TODO: Don't know what to do with these yet */ |
1150 | 0 | case OPT_IDB_EUIADDR: |
1151 | | #if 0 |
1152 | | tag_ptr->type = ERF_META_TAG_if_eui; |
1153 | | tag_ptr->length = 8; |
1154 | | tag_ptr->value = (uint8_t*)g_malloc(sizeof(optval->eui64val)); |
1155 | | memcpy(tag_ptr->value, &optval->euival, sizeof(optval->eui64val)); |
1156 | | break; |
1157 | | #endif |
1158 | 0 | case OPT_IDB_MACADDR: |
1159 | | #if 0 |
1160 | | tag_ptr->type = ERF_META_TAG_if_mac; |
1161 | | tag_ptr->length = 6; |
1162 | | /*value same format as pcapng (6-byte canonical, padded by write |
1163 | | * function automatically to 32-bit boundary)*/ |
1164 | | tag_ptr->value = (uint8_t*)g_malloc(sizeof(optval->macval)); |
1165 | | memcpy(tag_ptr->value, &optval->macval, sizeof(optval->macval)); |
1166 | | break; |
1167 | | #endif |
1168 | 0 | case OPT_IDB_TSRESOL: |
1169 | 0 | case OPT_IDB_TZONE: |
1170 | | /* Fall through */ |
1171 | 0 | default: |
1172 | 0 | erf_meta_tag_free(tag_ptr); |
1173 | 0 | tag_ptr = NULL; |
1174 | 0 | break; |
1175 | 0 | } |
1176 | | |
1177 | 0 | if (tag_ptr) |
1178 | 0 | g_ptr_array_add(section_ptr->tags, tag_ptr); |
1179 | |
|
1180 | 0 | return true; /* we always succeed */ |
1181 | 0 | } |
1182 | | |
1183 | 0 | static void erf_populate_section_length_by_tags(struct erf_meta_section *section_ptr) { |
1184 | 0 | unsigned i = 0; |
1185 | 0 | struct erf_meta_tag *tag_ptr; |
1186 | |
|
1187 | 0 | section_ptr->section_length = 8; |
1188 | |
|
1189 | 0 | for(;i < section_ptr->tags->len; i++) { |
1190 | 0 | tag_ptr = (struct erf_meta_tag*)g_ptr_array_index(section_ptr->tags, i); |
1191 | 0 | section_ptr->section_length += ERF_META_TAG_TOTAL_ALIGNED_LENGTH(tag_ptr->length); |
1192 | 0 | } |
1193 | 0 | } |
1194 | | |
1195 | | /** |
1196 | | * @brief Converts a wtap_block_t block to ERF metadata sections |
1197 | | * @param block a wtap_block_t block |
1198 | | * @param sections pointer to a GPtrArray containing pointers to sections |
1199 | | * @param section_type the pre-specified section_type |
1200 | | * @param section_id Section ID to assign |
1201 | | * @param func a wtap_block_foreach_func call back function to specify |
1202 | | * what needs to be done on the block |
1203 | | * @return true if success, false if failed |
1204 | | */ |
1205 | 0 | static bool erf_wtap_blocks_to_erf_sections(wtap_block_t block, GPtrArray *sections, uint16_t section_type, uint16_t section_id, wtap_block_foreach_func func) { |
1206 | |
|
1207 | 0 | if(!block || !sections || !func) { |
1208 | 0 | return false; |
1209 | 0 | } |
1210 | | |
1211 | 0 | struct erf_meta_section *section_ptr; |
1212 | |
|
1213 | 0 | section_ptr = g_new(struct erf_meta_section, 1); |
1214 | 0 | section_ptr->tags = g_ptr_array_new_with_free_func(erf_meta_tag_free); |
1215 | 0 | section_ptr->type = section_type; |
1216 | 0 | section_ptr->section_id = section_id; |
1217 | |
|
1218 | 0 | wtap_block_foreach_option(block, func, (void*)section_ptr); |
1219 | 0 | erf_populate_section_length_by_tags(section_ptr); |
1220 | 0 | g_ptr_array_add(sections, section_ptr); |
1221 | |
|
1222 | 0 | return true; |
1223 | 0 | } |
1224 | | |
1225 | | |
1226 | 0 | static bool erf_meta_write_tag(wtap_dumper *wdh, struct erf_meta_tag *tag_ptr, int *err) { |
1227 | |
|
1228 | 0 | uint16_t data[2]; |
1229 | 0 | unsigned pad = 0; |
1230 | | /* we only need to pad up to 32 bits*/ |
1231 | 0 | uint32_t padbuf = 0; |
1232 | |
|
1233 | 0 | pad = ERF_META_TAG_ALIGNED_LENGTH(tag_ptr->length) - tag_ptr->length; |
1234 | 0 | data[0] = g_htons(tag_ptr->type); |
1235 | 0 | data[1] = g_htons(tag_ptr->length); |
1236 | |
|
1237 | 0 | if(!wtap_dump_file_write(wdh, data, sizeof(data), err)) return false; |
1238 | | |
1239 | 0 | if(!wtap_dump_file_write(wdh, tag_ptr->value, tag_ptr->length, err)) return false; |
1240 | | |
1241 | 0 | if(pad) { |
1242 | 0 | if(!wtap_dump_file_write(wdh, &padbuf, pad, err)) return false; |
1243 | 0 | } |
1244 | | |
1245 | 0 | return true; |
1246 | |
|
1247 | 0 | } |
1248 | | |
1249 | 0 | static bool erf_meta_write_section(wtap_dumper *wdh, struct erf_meta_section *section_ptr, int *err) { |
1250 | |
|
1251 | 0 | struct erf_meta_tag *tag_ptr; |
1252 | 0 | unsigned i; |
1253 | 0 | uint16_t data[4]; |
1254 | |
|
1255 | 0 | data[0] = g_htons(section_ptr->type); |
1256 | 0 | data[1] = g_htons(4); /*section header length*/ |
1257 | 0 | data[2] = g_htons(section_ptr->section_id); |
1258 | 0 | data[3] = g_htons(section_ptr->section_length); |
1259 | |
|
1260 | 0 | if(!wtap_dump_file_write(wdh, data, sizeof(data), err)) return false; |
1261 | | |
1262 | 0 | for(i = 0; i < section_ptr->tags->len; i++) { |
1263 | 0 | tag_ptr = (struct erf_meta_tag*)g_ptr_array_index(section_ptr->tags, i); |
1264 | 0 | if(!erf_meta_write_tag(wdh, tag_ptr, err)) return false; |
1265 | 0 | } |
1266 | | |
1267 | 0 | return true; |
1268 | |
|
1269 | 0 | } |
1270 | | |
1271 | 0 | static bool erf_wtap_info_to_sections(wtap_dumper *wdh, GPtrArray *sections) { |
1272 | 0 | wtap_block_t block; |
1273 | 0 | unsigned i = 0; |
1274 | |
|
1275 | 0 | block = g_array_index(wdh->shb_hdrs, wtap_block_t, 0); |
1276 | 0 | erf_wtap_blocks_to_erf_sections(block, sections, ERF_META_SECTION_CAPTURE, 0, erf_write_wtap_option_to_capture_tag); |
1277 | |
|
1278 | 0 | block = g_array_index(wdh->shb_hdrs, wtap_block_t, 0); |
1279 | 0 | erf_wtap_blocks_to_erf_sections(block, sections, ERF_META_SECTION_HOST, 0, erf_write_wtap_option_to_host_tag); |
1280 | | |
1281 | | /*TODO: support >4 interfaces by using more Source IDs. Affects more than this |
1282 | | * function as need more metadata records. Just dump them all out for now. */ |
1283 | 0 | for(i = 0; i < wdh->interface_data->len; i++) { |
1284 | 0 | block = g_array_index(wdh->interface_data, wtap_block_t, i); |
1285 | 0 | erf_wtap_blocks_to_erf_sections(block, sections, ERF_META_SECTION_INTERFACE, (int16_t)i+1, erf_write_wtap_option_to_interface_tag); |
1286 | 0 | } |
1287 | |
|
1288 | 0 | return true; |
1289 | 0 | } |
1290 | | |
1291 | 0 | static bool erf_comment_to_sections(wtap_dumper *wdh _U_, uint16_t section_type, uint16_t section_id, char *comment, GPtrArray *sections){ |
1292 | 0 | struct erf_meta_section *section_ptr; |
1293 | 0 | struct erf_meta_tag *comment_tag_ptr = NULL; |
1294 | 0 | struct erf_meta_tag *user_tag_ptr = NULL; |
1295 | 0 | const char *user = NULL; |
1296 | | |
1297 | | /* Generate the section */ |
1298 | 0 | section_ptr = g_new(struct erf_meta_section, 1); |
1299 | 0 | section_ptr->type = section_type; |
1300 | 0 | section_ptr->section_id = section_id; |
1301 | 0 | section_ptr->tags = g_ptr_array_new_with_free_func(erf_meta_tag_free); |
1302 | | |
1303 | | /* Generate the comment tag */ |
1304 | 0 | comment_tag_ptr = g_new(struct erf_meta_tag, 1); |
1305 | 0 | comment_tag_ptr->type = ERF_META_TAG_comment; |
1306 | | /* XXX: if the comment has been cleared write the empty string (which |
1307 | | * conveniently is all a zero length tag which means the value is |
1308 | | * invalidated) */ |
1309 | 0 | comment_tag_ptr->value = (uint8_t*)g_strdup(comment ? comment : ""); |
1310 | 0 | comment_tag_ptr->length = (uint16_t)strlen((char*)comment_tag_ptr->value); |
1311 | 0 | g_ptr_array_add(section_ptr->tags, comment_tag_ptr); |
1312 | |
|
1313 | 0 | user = g_get_user_name(); |
1314 | 0 | if (user) { |
1315 | | /* Generate username tag */ |
1316 | 0 | user_tag_ptr = g_new(struct erf_meta_tag, 1); |
1317 | 0 | user_tag_ptr->type = ERF_META_TAG_user; |
1318 | 0 | user_tag_ptr->value = (uint8_t*)g_strdup(user); |
1319 | 0 | user_tag_ptr->length = (uint16_t)strlen((char*)user_tag_ptr->value); |
1320 | 0 | g_ptr_array_add(section_ptr->tags, user_tag_ptr); |
1321 | 0 | } |
1322 | |
|
1323 | 0 | erf_populate_section_length_by_tags(section_ptr); |
1324 | |
|
1325 | 0 | g_ptr_array_add(sections, section_ptr); |
1326 | |
|
1327 | 0 | return true; |
1328 | 0 | } |
1329 | | |
1330 | 0 | static uint64_t erf_get_random_anchor_id(erf_dump_t *dump_priv) { |
1331 | 0 | return (((uint64_t)g_rand_int(dump_priv->rand) << 32) | (uint64_t)g_rand_int(dump_priv->rand)) >> 16; |
1332 | 0 | } |
1333 | | |
1334 | 0 | static uint64_t erf_metaid_ext_hdr(uint8_t exthdr_type, uint64_t id, uint8_t srcid_flags) { |
1335 | 0 | uint64_t ext_hdr; |
1336 | |
|
1337 | 0 | ext_hdr = id & ERF_EHDR_HOST_ID_MASK; |
1338 | 0 | ext_hdr |= ((uint64_t)srcid_flags) << 48; |
1339 | 0 | ext_hdr |= ((uint64_t)exthdr_type) << 56; |
1340 | |
|
1341 | 0 | return ext_hdr; |
1342 | 0 | } |
1343 | 0 | #define erf_host_id_ext_hdr(host_id, source_id) erf_metaid_ext_hdr(ERF_EXT_HDR_TYPE_HOST_ID, host_id, source_id) |
1344 | 0 | #define erf_anchor_id_ext_hdr(anchor_id, flags) erf_metaid_ext_hdr(ERF_EXT_HDR_TYPE_ANCHOR_ID, anchor_id, flags) |
1345 | | |
1346 | 0 | static inline bool erf_add_ext_hdr_to_list(uint64_t ext_hdr, uint64_t comparison_mask, GArray *extra_ehdrs) { |
1347 | | /* check for existing Host ID in set and add */ |
1348 | 0 | unsigned i = 0; |
1349 | 0 | struct erf_ehdr ehdr_tmp; |
1350 | 0 | struct erf_ehdr *ehdr_ptr = NULL; |
1351 | |
|
1352 | 0 | if (!extra_ehdrs) |
1353 | 0 | return false; |
1354 | | |
1355 | 0 | ext_hdr = ext_hdr & ~ERF_EHDR_MORE_EXTHDR_MASK; |
1356 | 0 | if (comparison_mask == 0) |
1357 | 0 | comparison_mask = UINT64_MAX; |
1358 | |
|
1359 | 0 | comparison_mask &= ~ERF_EHDR_MORE_EXTHDR_MASK; |
1360 | |
|
1361 | 0 | for (i = 0; i < extra_ehdrs->len; i++) { |
1362 | 0 | ehdr_ptr = &g_array_index(extra_ehdrs, struct erf_ehdr, i); |
1363 | | /* Check if we already have this Host ID extension header */ |
1364 | 0 | if (ext_hdr == (ehdr_ptr->ehdr & comparison_mask)) { |
1365 | 0 | return true; |
1366 | 0 | } |
1367 | 0 | } |
1368 | | |
1369 | | /* set more flag on last extension header */ |
1370 | 0 | if (ehdr_ptr) { |
1371 | 0 | ehdr_ptr->ehdr |= ERF_EHDR_MORE_EXTHDR_MASK; |
1372 | 0 | } |
1373 | |
|
1374 | 0 | ehdr_tmp.ehdr = ext_hdr; /*more flag already cleared above*/ |
1375 | 0 | g_array_append_val(extra_ehdrs, ehdr_tmp); |
1376 | |
|
1377 | 0 | return true; |
1378 | 0 | } |
1379 | | |
1380 | 0 | static inline bool erf_append_ext_hdr_to_list(uint64_t ext_hdr, GArray *extra_ehdrs) { |
1381 | 0 | struct erf_ehdr ehdr_tmp; |
1382 | |
|
1383 | 0 | if (!extra_ehdrs) |
1384 | 0 | return false; |
1385 | | |
1386 | 0 | ehdr_tmp.ehdr = ext_hdr & ~ERF_EHDR_MORE_EXTHDR_MASK; |
1387 | | |
1388 | | /* set more flag on last extension header */ |
1389 | 0 | if (extra_ehdrs->len) { |
1390 | 0 | g_array_index(extra_ehdrs, struct erf_ehdr, extra_ehdrs->len - 1).ehdr |= ERF_EHDR_MORE_EXTHDR_MASK; |
1391 | 0 | } |
1392 | |
|
1393 | 0 | g_array_append_val(extra_ehdrs, ehdr_tmp); |
1394 | |
|
1395 | 0 | return true; |
1396 | 0 | } |
1397 | | |
1398 | 0 | static bool erf_update_host_id_ext_hdrs_list(erf_dump_t *dump_priv, const union wtap_pseudo_header *pseudo_header, GArray *extra_ehdrs) { |
1399 | 0 | uint8_t type; |
1400 | 0 | uint8_t erf_type; |
1401 | 0 | int has_more; |
1402 | 0 | uint64_t hdr; |
1403 | 0 | int i = 0; |
1404 | 0 | uint8_t source_id = 0; |
1405 | 0 | uint64_t host_id = 0; |
1406 | 0 | bool host_id_found = false; |
1407 | |
|
1408 | 0 | if (!extra_ehdrs) |
1409 | 0 | return false; |
1410 | | |
1411 | 0 | erf_type = pseudo_header->erf.phdr.type & 0x7f; |
1412 | 0 | has_more = pseudo_header->erf.phdr.type & 0x80; |
1413 | |
|
1414 | 0 | while (has_more && i < MAX_ERF_EHDR) { |
1415 | 0 | hdr = pseudo_header->erf.ehdr_list[i].ehdr; |
1416 | 0 | type = (uint8_t) (hdr >> 56); |
1417 | |
|
1418 | 0 | switch (type & 0x7f) { |
1419 | 0 | case ERF_EXT_HDR_TYPE_HOST_ID: |
1420 | 0 | host_id = hdr & ERF_EHDR_HOST_ID_MASK; |
1421 | 0 | source_id = (hdr >> 48) & 0xff; |
1422 | | |
1423 | | /* Don't add the wireshark Host ID Source ID 0 twice since we already add it to metadata records */ |
1424 | 0 | if (host_id != dump_priv->host_id || source_id != 0) |
1425 | 0 | if (!erf_add_ext_hdr_to_list(hdr, 0, extra_ehdrs)) return false; |
1426 | | |
1427 | 0 | if (!host_id_found) { |
1428 | | /* XXX: Take the opportunity to update the implicit Host ID if we |
1429 | | * don't know it yet. Ideally we should pass this through from the |
1430 | | * reader as a custom option or similar. */ |
1431 | 0 | if (erf_type == ERF_TYPE_META && ((hdr >> 48) & 0xff) > 0) { |
1432 | 0 | if (dump_priv->implicit_host_id == ERF_META_HOST_ID_IMPLICIT) { |
1433 | 0 | dump_priv->implicit_host_id = host_id; |
1434 | 0 | } |
1435 | 0 | } |
1436 | 0 | } |
1437 | |
|
1438 | 0 | host_id_found = true; |
1439 | 0 | break; |
1440 | 0 | case ERF_EXT_HDR_TYPE_FLOW_ID: |
1441 | 0 | if (source_id == 0) /* If no Host ID extension header use the first Source ID only */ |
1442 | 0 | source_id = (hdr >> 48) & 0xff; |
1443 | 0 | break; |
1444 | 0 | } |
1445 | | |
1446 | 0 | has_more = type & 0x80; |
1447 | 0 | i++; |
1448 | 0 | } |
1449 | | |
1450 | | /* Add Source ID with implicit Host ID if not found */ |
1451 | 0 | if (!host_id_found) { |
1452 | 0 | uint64_t implicit_host_id = dump_priv->implicit_host_id == ERF_META_HOST_ID_IMPLICIT ? 0 : dump_priv->implicit_host_id; |
1453 | | /* Don't add the wireshark Host ID Source ID 0 twice since we already add it to metadata records */ |
1454 | 0 | if (implicit_host_id != dump_priv->host_id || source_id != 0) |
1455 | 0 | if (!erf_add_ext_hdr_to_list(erf_host_id_ext_hdr(implicit_host_id, source_id), 0, extra_ehdrs)) return false; |
1456 | 0 | } |
1457 | | |
1458 | 0 | return true; |
1459 | 0 | } |
1460 | | |
1461 | | /** |
1462 | | * Writes a metadata record with a randomly generated Anchor ID with the |
1463 | | * user comment attached to its comment section, also updates the |
1464 | | * modified frame header to include a Host ID extension header and |
1465 | | * a Anchor ID extension header to link the records together. |
1466 | | * @param wdh the wtap_dumper structure |
1467 | | * @param dump_priv private data for the dump stream |
1468 | | * @param rec record metadata from which to get user comment |
1469 | | * @param mutable_hdr pseudo_header to update with Anchor ID for comment record |
1470 | | * @param err the error value |
1471 | | * @return A bool value to indicate whether the dump was successful |
1472 | | */ |
1473 | 0 | static bool erf_write_anchor_meta_update_phdr(wtap_dumper *wdh, erf_dump_t *dump_priv, const wtap_rec *rec, union wtap_pseudo_header *mutable_hdr, int *err) { |
1474 | 0 | GArray *meta_ehdrs; |
1475 | 0 | GPtrArray* sections = NULL; |
1476 | 0 | uint8_t has_more; |
1477 | 0 | uint8_t i = 0; |
1478 | 0 | uint8_t ext_hdr_count = 0; |
1479 | 0 | uint8_t j = 0; |
1480 | 0 | uint64_t host_id_src_hdr = ERF_META_HOST_ID_IMPLICIT; |
1481 | 0 | uint64_t host_id_own_hdr = erf_host_id_ext_hdr(dump_priv->host_id, 0); |
1482 | 0 | uint64_t flow_id_hdr = 0; |
1483 | 0 | uint64_t anchor_id_hdr = 0; |
1484 | 0 | bool found_host_id = false; |
1485 | 0 | bool found_own_host_id = false; |
1486 | 0 | bool found_flow_id = false; |
1487 | 0 | int new_ext_hdrs = 0; |
1488 | 0 | uint8_t insert_idx = 0; |
1489 | 0 | uint8_t source_id = 0; |
1490 | 0 | bool ret = false; |
1491 | 0 | uint64_t implicit_host_id = dump_priv->implicit_host_id == ERF_META_HOST_ID_IMPLICIT ? 0 : dump_priv->implicit_host_id; |
1492 | 0 | char *pkt_comment; |
1493 | | |
1494 | | |
1495 | | /* |
1496 | | * There are 3 possible scenarios: |
1497 | | * a. The record has a source Host ID but not our Host ID. We need to add our |
1498 | | * Host ID extension header then our Anchor ID extension header. |
1499 | | * b. The record already has our Host ID extension header on it. We should |
1500 | | * insert the Anchor ID at the end of the list for that Host ID just |
1501 | | * before the next Host ID extension header. |
1502 | | * c. The record has no Host ID extension header at all. We need to add the Host ID |
1503 | | * extension header making the Implicit Host ID explicit before we add our |
1504 | | * one to avoid claiming the packet was captured by us. |
1505 | | */ |
1506 | | |
1507 | | /* |
1508 | | * Extract information from the packet extension header stack |
1509 | | * 1. original source Host ID extension header. |
1510 | | * 2. Anchor ID extension header insertion point (see b., above). |
1511 | | * 3. Flow ID extension header so we can add it for reference to the metadata |
1512 | | * record. |
1513 | | * 4. Enough information to generate an explicit Host ID extension header if |
1514 | | * there wasn't one (see erf_get_source_from_header). |
1515 | | */ |
1516 | |
|
1517 | 0 | has_more = mutable_hdr->erf.phdr.type & 0x80; |
1518 | |
|
1519 | 0 | while (has_more && (i < MAX_ERF_EHDR)) { |
1520 | 0 | uint64_t hdr = mutable_hdr->erf.ehdr_list[i].ehdr; |
1521 | 0 | uint8_t type = (uint8_t) (hdr >> 56); |
1522 | |
|
1523 | 0 | switch (type & 0x7f) { |
1524 | 0 | case ERF_EXT_HDR_TYPE_HOST_ID: |
1525 | | /* Set insertion point of anchor ID to be at end of Host ID list (i.e. |
1526 | | * just before the next one). */ |
1527 | 0 | if (found_own_host_id && !insert_idx) |
1528 | 0 | insert_idx = i; |
1529 | |
|
1530 | 0 | if ((hdr & ERF_EHDR_HOST_ID_MASK) == dump_priv->host_id){ |
1531 | 0 | found_own_host_id = true; |
1532 | 0 | } |
1533 | |
|
1534 | 0 | if (!found_host_id) |
1535 | 0 | host_id_src_hdr = hdr; |
1536 | |
|
1537 | 0 | found_host_id = true; |
1538 | 0 | break; |
1539 | | |
1540 | 0 | case ERF_EXT_HDR_TYPE_FLOW_ID: |
1541 | | /*XXX: we only use this when making the implicit host id explicit, |
1542 | | * otherwise we'd need to check the one in Host ID header too*/ |
1543 | 0 | if (source_id == 0) |
1544 | 0 | source_id = (uint8_t)(hdr >> 48); |
1545 | |
|
1546 | 0 | if (!found_flow_id) |
1547 | 0 | flow_id_hdr = hdr; |
1548 | |
|
1549 | 0 | found_flow_id = true; |
1550 | 0 | break; |
1551 | 0 | } |
1552 | | |
1553 | 0 | has_more = type & 0x80; |
1554 | 0 | i += 1; |
1555 | 0 | } |
1556 | | |
1557 | 0 | ext_hdr_count = i; |
1558 | |
|
1559 | 0 | if (!insert_idx) |
1560 | 0 | insert_idx = i; |
1561 | | |
1562 | | /* Don't need to add our own Host ID twice if it is the same as the implicit*/ |
1563 | 0 | if (!found_host_id && implicit_host_id == dump_priv->host_id) { |
1564 | 0 | found_own_host_id = true; |
1565 | 0 | } |
1566 | | |
1567 | | /* |
1568 | | * Update the packet record pseudo_header with Anchor ID and extension header(s) |
1569 | | */ |
1570 | 0 | new_ext_hdrs = 1 /*anchor id*/ + (found_own_host_id?0:1) + (found_host_id?0:1); |
1571 | |
|
1572 | 0 | if(ext_hdr_count + new_ext_hdrs > MAX_ERF_EHDR |
1573 | 0 | || mutable_hdr->erf.phdr.rlen + new_ext_hdrs * 8 > 65535) { |
1574 | | /* Not enough extension header slots to add Anchor ID */ |
1575 | 0 | *err = WTAP_ERR_PACKET_TOO_LARGE; |
1576 | 0 | return false; |
1577 | 0 | } |
1578 | | |
1579 | 0 | mutable_hdr->erf.phdr.rlen += new_ext_hdrs * 8; |
1580 | | |
1581 | | /* Set the more extension headers flag */ |
1582 | 0 | mutable_hdr->erf.phdr.type |= 0x80; |
1583 | 0 | if (insert_idx > 0) { |
1584 | 0 | mutable_hdr->erf.ehdr_list[insert_idx-1].ehdr |= ERF_EHDR_MORE_EXTHDR_MASK; |
1585 | 0 | } |
1586 | | |
1587 | | /* Generate the Anchor ID extension header */ |
1588 | 0 | anchor_id_hdr = erf_anchor_id_ext_hdr(erf_get_random_anchor_id(dump_priv), 0); |
1589 | | |
1590 | | /* Either we can insert Anchor ID at the end of the list for our Host ID or we |
1591 | | * need to append the Host ID(s) and Anchor ID */ |
1592 | 0 | if (insert_idx < ext_hdr_count) { |
1593 | | /* shuffle up any following extension headers FIRST - we know we have room now */ |
1594 | 0 | for (j = ext_hdr_count; j > insert_idx; j--) { |
1595 | 0 | mutable_hdr->erf.ehdr_list[j].ehdr = mutable_hdr->erf.ehdr_list[j-1].ehdr; |
1596 | 0 | } |
1597 | | |
1598 | | /* copy more extension headers bit from previous extension header */ |
1599 | 0 | anchor_id_hdr |= ERF_EHDR_MORE_EXTHDR_MASK; |
1600 | 0 | } |
1601 | |
|
1602 | 0 | if(!found_host_id) { |
1603 | | /* No Host ID extension header found and we have an implicit Host ID which |
1604 | | * we want to make explicit */ |
1605 | | |
1606 | | /* XXX: it is important that we know the implicit Host ID here or we end |
1607 | | * up semi-permanently associating the packet with Host 0 (unknown), we should |
1608 | | * pass it through from the reader. In theory we should be on the |
1609 | | * original capture machine if we have no Host ID extension headers. */ |
1610 | 0 | host_id_src_hdr = erf_host_id_ext_hdr(implicit_host_id, source_id); |
1611 | 0 | mutable_hdr->erf.ehdr_list[insert_idx++].ehdr = ERF_EHDR_SET_MORE_EXTHDR(host_id_src_hdr); |
1612 | 0 | } |
1613 | |
|
1614 | 0 | if(!found_own_host_id) { |
1615 | | /* Add our Host ID extension header */ |
1616 | 0 | mutable_hdr->erf.ehdr_list[insert_idx++].ehdr = ERF_EHDR_SET_MORE_EXTHDR(host_id_own_hdr); |
1617 | 0 | } |
1618 | | |
1619 | | /*Add the Anchor ID extension header */ |
1620 | 0 | mutable_hdr->erf.ehdr_list[insert_idx].ehdr = anchor_id_hdr; |
1621 | | |
1622 | | |
1623 | | /* |
1624 | | * Now construct the metadata Anchor record with the same Anchor ID |
1625 | | */ |
1626 | |
|
1627 | 0 | meta_ehdrs = g_array_new(false, false, sizeof(struct erf_ehdr)); |
1628 | | |
1629 | | /* We need up to 4 extension headers on the Provenance metadata record */ |
1630 | | /*Required*/ |
1631 | | /* 1. Added by erf_write_meta_record: HostID exthdr to indicate this Anchor |
1632 | | * record was generated by this host. Source ID 0 to avoid changing the |
1633 | | * implicit Host ID. */ |
1634 | | |
1635 | | /* 2. AnchorID exthdr with 'unique' per-host Anchor ID assigned by this host |
1636 | | * (in this case Wireshark). Anchor definition flag set to 1 to indicate this |
1637 | | * record contains a definition of the ID, in this case a comment on a single |
1638 | | * packet. Tied to above extension header by ordering like a list */ |
1639 | 0 | erf_append_ext_hdr_to_list(anchor_id_hdr | ERF_EHDR_ANCHOR_ID_DEFINITION_MASK, meta_ehdrs); |
1640 | | |
1641 | | /*Helpful for indexing*/ |
1642 | | /* 3. HostID exthdr with the original Source (first Host ID extension header) of the packet record */ |
1643 | 0 | erf_append_ext_hdr_to_list(host_id_src_hdr, meta_ehdrs); |
1644 | | |
1645 | | /* Flow ID extension header from the packet record if we have one */ |
1646 | 0 | if (found_flow_id) { |
1647 | | /* 4. FlowID exthdr with Flow ID from the packet so a flow search will find the comment |
1648 | | * record too. Must come here so the (redundant here) Source ID is scoped to the |
1649 | | * correct Host ID. */ |
1650 | | /* Clear the stack type just in case something tries to assume we're an IP |
1651 | | * packet without looking at the ERF type. Clear Source ID too just in case |
1652 | | * we're trying to associate with the wrong Host ID. */ |
1653 | 0 | erf_append_ext_hdr_to_list(flow_id_hdr & ~(ERF_EHDR_FLOW_ID_STACK_TYPE_MASK|ERF_EHDR_FLOW_ID_SOURCE_ID_MASK), meta_ehdrs); |
1654 | 0 | } |
1655 | | |
1656 | | /* Generate the metadata payload with the packet comment */ |
1657 | | /* XXX - can ERF have more than one comment? */ |
1658 | 0 | sections = g_ptr_array_new_with_free_func(erf_meta_section_free); |
1659 | 0 | if (WTAP_OPTTYPE_SUCCESS != wtap_block_get_nth_string_option_value(rec->block, OPT_COMMENT, 0, &pkt_comment)) { |
1660 | 0 | pkt_comment = NULL; |
1661 | 0 | } |
1662 | 0 | erf_comment_to_sections(wdh, ERF_META_SECTION_INFO, 0x8000 /*local to record*/, pkt_comment, sections); |
1663 | | |
1664 | | /* Write the metadata record, but not the packet record as what we do depends |
1665 | | * on the WTAP_ENCAP */ |
1666 | 0 | ret = erf_write_meta_record(wdh, dump_priv, mutable_hdr->erf.phdr.ts, sections, meta_ehdrs, err); |
1667 | 0 | g_ptr_array_free(sections, true); |
1668 | 0 | g_array_free(meta_ehdrs, true); |
1669 | |
|
1670 | 0 | return ret; |
1671 | 0 | } |
1672 | | |
1673 | 0 | static bool erf_write_meta_record(wtap_dumper *wdh, erf_dump_t *dump_priv, uint64_t timestamp, GPtrArray *sections, GArray *extra_ehdrs, int *err) { |
1674 | 0 | union wtap_pseudo_header other_header; |
1675 | 0 | struct erf_meta_tag gen_time_tag; |
1676 | 0 | struct erf_meta_section *section_ptr; |
1677 | 0 | unsigned total_wlen = 0; |
1678 | 0 | unsigned total_rlen = 0; |
1679 | 0 | int64_t alignbytes = 0; |
1680 | 0 | unsigned i; |
1681 | 0 | unsigned num_extra_ehdrs = 0; |
1682 | |
|
1683 | 0 | if(!sections || sections->len <= 0) |
1684 | 0 | return false; |
1685 | | |
1686 | 0 | for(i = 0; i < sections->len; i++) { |
1687 | 0 | section_ptr = (struct erf_meta_section*)g_ptr_array_index(sections, i); |
1688 | 0 | total_wlen += section_ptr->section_length; |
1689 | 0 | } |
1690 | |
|
1691 | 0 | gen_time_tag.type = ERF_META_TAG_gen_time; |
1692 | 0 | gen_time_tag.length = 8U; |
1693 | 0 | gen_time_tag.value = (uint8_t*)&dump_priv->gen_time; |
1694 | 0 | total_wlen += gen_time_tag.length + 4; |
1695 | |
|
1696 | 0 | total_rlen = total_wlen + 24; /* 24 is the header + extension header length */ |
1697 | 0 | if (extra_ehdrs) { |
1698 | | /* |
1699 | | * These will be appended to the first extension header in |
1700 | | * other_header.erf.ehdr_list. There are a total of MAX_ERF_EHDR |
1701 | | * extension headers in that array, so we can append no more than |
1702 | | * MAX_ERF_EHDR - 1 extension headers. |
1703 | | */ |
1704 | 0 | num_extra_ehdrs = MIN(extra_ehdrs->len, MAX_ERF_EHDR - 1); |
1705 | 0 | total_rlen += num_extra_ehdrs * 8; |
1706 | 0 | } |
1707 | | /*padding to 8 byte alignment*/ |
1708 | 0 | total_rlen += WS_PADDING_TO_8(total_rlen); |
1709 | |
|
1710 | 0 | if(total_rlen > 65535) { |
1711 | 0 | *err = WTAP_ERR_PACKET_TOO_LARGE; |
1712 | 0 | return false; |
1713 | 0 | } |
1714 | | |
1715 | 0 | other_header.erf.phdr.ts = timestamp; |
1716 | 0 | other_header.erf.phdr.type = ERF_TYPE_META | 0x80; |
1717 | 0 | other_header.erf.phdr.flags = 0x04; /* Varying record length */ |
1718 | 0 | other_header.erf.phdr.lctr = 0; |
1719 | 0 | other_header.erf.phdr.wlen = (uint16_t)total_wlen; |
1720 | 0 | other_header.erf.phdr.rlen = (uint16_t)total_rlen; |
1721 | | /*Add our Host ID in Host ID extension header indicating we generated this |
1722 | | * record. Source ID 0 to avoid affecting implicit Host ID. */ |
1723 | 0 | other_header.erf.ehdr_list[0].ehdr = erf_host_id_ext_hdr(dump_priv->host_id, 0); |
1724 | | /*Additional extension headers*/ |
1725 | | /*XXX: If we end up cutting the list short, erf_write_phdr will correct the |
1726 | | * unterminated extension header list*/ |
1727 | 0 | if (num_extra_ehdrs > 0) { |
1728 | 0 | other_header.erf.ehdr_list[0].ehdr |= ERF_EHDR_MORE_EXTHDR_MASK; |
1729 | 0 | memcpy(&other_header.erf.ehdr_list[1], extra_ehdrs->data, sizeof(struct erf_ehdr) * num_extra_ehdrs); |
1730 | 0 | } |
1731 | | |
1732 | | /* Make sure we always write out rlen, regardless of what happens */ |
1733 | 0 | alignbytes = wdh->bytes_dumped + other_header.erf.phdr.rlen; |
1734 | |
|
1735 | 0 | if(!erf_write_phdr(wdh, WTAP_ENCAP_ERF, &other_header, err)) return false; |
1736 | | |
1737 | | /* Generation time */ |
1738 | 0 | erf_meta_write_tag(wdh, &gen_time_tag, err); |
1739 | | |
1740 | | /* Section(s) */ |
1741 | 0 | for(i = 0; i < sections->len; i++) { |
1742 | 0 | section_ptr = (struct erf_meta_section*)g_ptr_array_index(sections, i); |
1743 | 0 | erf_meta_write_section(wdh, section_ptr, err); |
1744 | 0 | } |
1745 | |
|
1746 | 0 | while(wdh->bytes_dumped < alignbytes){ |
1747 | 0 | if(!wtap_dump_file_write(wdh, "", 1, err)) return false; |
1748 | 0 | } |
1749 | | |
1750 | | /* We wrote new packets, reloading is required */ |
1751 | 0 | wdh->needs_reload = true; |
1752 | |
|
1753 | 0 | return true; |
1754 | |
|
1755 | 0 | } |
1756 | | |
1757 | 0 | static erf_dump_t *erf_dump_priv_create(void) { |
1758 | 0 | erf_dump_t *dump_priv; |
1759 | |
|
1760 | 0 | dump_priv = g_new(erf_dump_t, 1); |
1761 | 0 | dump_priv->write_next_extra_meta = false; |
1762 | 0 | dump_priv->last_meta_periodic = false; |
1763 | 0 | dump_priv->gen_time = 0; |
1764 | 0 | dump_priv->host_id = ERF_WS_DEFAULT_HOST_ID; |
1765 | 0 | dump_priv->implicit_host_id = ERF_META_HOST_ID_IMPLICIT; |
1766 | 0 | dump_priv->first_frame_time_sec = 0; |
1767 | 0 | dump_priv->prev_inserted_time_sec = 0; |
1768 | 0 | dump_priv->prev_frame_ts = 0; |
1769 | 0 | dump_priv->prev_erf_type = 0; |
1770 | 0 | dump_priv->user_comment_ptr = NULL; |
1771 | 0 | dump_priv->periodic_sections = NULL; |
1772 | 0 | dump_priv->periodic_extra_ehdrs = g_array_new(false, false, sizeof(struct erf_ehdr)); |
1773 | 0 | dump_priv->rand = g_rand_new(); |
1774 | |
|
1775 | 0 | return dump_priv; |
1776 | 0 | } |
1777 | | |
1778 | | static bool erf_dump( |
1779 | | wtap_dumper *wdh, |
1780 | | const wtap_rec *rec, |
1781 | | int *err, |
1782 | | char **err_info _U_) |
1783 | 0 | { |
1784 | 0 | const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header; |
1785 | 0 | union wtap_pseudo_header other_phdr; |
1786 | 0 | const uint8_t *pd = ws_buffer_start_ptr(&rec->data); |
1787 | 0 | int erf_type; |
1788 | 0 | int64_t alignbytes = 0; |
1789 | 0 | unsigned padbytes = 0; |
1790 | 0 | int round_down = 0; |
1791 | 0 | bool must_add_crc = false; |
1792 | 0 | uint32_t crc32 = 0x00000000; |
1793 | 0 | erf_dump_t *dump_priv = (erf_dump_t*)wdh->priv; |
1794 | | /* Host ID extension header with Host ID 0 (unknown). For now use Source ID 1. */ |
1795 | | /* TODO: How to know if record was captured by this Wireshark? */ |
1796 | 0 | uint64_t non_erf_host_id_ehdr = erf_host_id_ext_hdr(0, 1); |
1797 | | |
1798 | | /* Don't write anything bigger than we're willing to read. */ |
1799 | 0 | if(rec->rec_header.packet_header.caplen > WTAP_MAX_PACKET_SIZE_STANDARD) { |
1800 | 0 | *err = WTAP_ERR_PACKET_TOO_LARGE; |
1801 | 0 | return false; |
1802 | 0 | } |
1803 | | |
1804 | 0 | if(!dump_priv->gen_time) { |
1805 | 0 | erf_dump_priv_init_gen_time(dump_priv); |
1806 | 0 | dump_priv->first_frame_time_sec = rec->ts.secs; |
1807 | 0 | } |
1808 | | |
1809 | | /* |
1810 | | * ERF doesn't have a per-file encapsulation type, and it |
1811 | | * doesn't have a pcapng-style notion of interfaces that |
1812 | | * support a per-interface encapsulation type. Therefore, |
1813 | | * we can just use this particular packet's encapsulation |
1814 | | * without checking whether it's the encapsulation for the |
1815 | | * dump file (as there isn't an encapsulation for an ERF |
1816 | | * file, unless you count WTAP_ENCAP_ERF as the encapsulation |
1817 | | * for all files, but we add ERF metadata if a packet is |
1818 | | * written with an encapsulation other than WTAP_ENCAP_ERF). |
1819 | | * |
1820 | | * We will check whether the encapsulation is something we |
1821 | | * support later. |
1822 | | */ |
1823 | 0 | if (rec->rec_header.packet_header.pkt_encap != WTAP_ENCAP_ERF) { |
1824 | 0 | unsigned int total_rlen; |
1825 | 0 | unsigned int total_wlen; |
1826 | | |
1827 | | /* Non-ERF encapsulation; generate ERF metadata */ |
1828 | |
|
1829 | 0 | total_rlen = rec->rec_header.packet_header.caplen+16; |
1830 | 0 | total_wlen = rec->rec_header.packet_header.len; |
1831 | | |
1832 | | /* We can only convert packet records. */ |
1833 | 0 | if (rec->rec_type != REC_TYPE_PACKET) { |
1834 | 0 | *err = WTAP_ERR_UNWRITABLE_REC_TYPE; |
1835 | 0 | *err_info = wtap_unwritable_rec_type_err_string(rec); |
1836 | 0 | return false; |
1837 | 0 | } |
1838 | | |
1839 | 0 | erf_type = wtap_wtap_encap_to_erf_encap(rec->rec_header.packet_header.pkt_encap); |
1840 | 0 | if (erf_type == -1) { |
1841 | 0 | *err = WTAP_ERR_UNWRITABLE_ENCAP; |
1842 | 0 | return false; |
1843 | 0 | } |
1844 | | |
1845 | | /* Generate a fake header in other_phdr using data that we know*/ |
1846 | 0 | memset(&other_phdr, 0, sizeof(union wtap_pseudo_header)); |
1847 | | /* Convert time erf timestamp format*/ |
1848 | 0 | other_phdr.erf.phdr.ts = ((uint64_t) rec->ts.secs << 32) + (((uint64_t) rec->ts.nsecs <<32) / 1000 / 1000 / 1000); |
1849 | 0 | other_phdr.erf.phdr.type = (uint8_t)erf_type; |
1850 | | /* Support up to 4 interfaces */ |
1851 | | /* TODO: use multiple Source IDs and metadata records to support >4 interfaces */ |
1852 | 0 | other_phdr.erf.phdr.flags = rec->rec_header.packet_header.interface_id % ERF_MAX_INTERFACES; |
1853 | 0 | other_phdr.erf.phdr.flags |= 0x4; /*vlen flag set because we're creating variable length records*/ |
1854 | |
|
1855 | 0 | other_phdr.erf.phdr.lctr = 0; |
1856 | | |
1857 | | /*now we work out rlen, accounting for all the different headers and missing fcs(eth)*/ |
1858 | 0 | switch(other_phdr.erf.phdr.type & 0x7F){ |
1859 | 0 | case ERF_TYPE_ETH: |
1860 | 0 | total_rlen += 2; /*2 bytes for erf eth_type*/ |
1861 | 0 | if (pseudo_header->eth.fcs_len != 4) { |
1862 | | /* Either this packet doesn't include the FCS |
1863 | | (pseudo_header->eth.fcs_len = 0), or we don't |
1864 | | know whether it has an FCS (= -1). We have to |
1865 | | synthesize an FCS.*/ |
1866 | 0 | if(!(rec->rec_header.packet_header.caplen < rec->rec_header.packet_header.len)){ /*don't add FCS if packet has been snapped off*/ |
1867 | 0 | crc32 = crc32_ccitt_seed(pd, rec->rec_header.packet_header.caplen, 0xFFFFFFFF); |
1868 | 0 | total_rlen += 4; /*4 bytes for added checksum*/ |
1869 | 0 | total_wlen += 4; |
1870 | 0 | must_add_crc = true; |
1871 | 0 | } |
1872 | 0 | } |
1873 | 0 | break; |
1874 | 0 | case ERF_TYPE_HDLC_POS: |
1875 | | /*we assume that it's missing a FCS checksum, make one up*/ |
1876 | 0 | if(!(rec->rec_header.packet_header.caplen < rec->rec_header.packet_header.len)){ /*unless of course, the packet has been snapped off*/ |
1877 | 0 | crc32 = crc32_ccitt_seed(pd, rec->rec_header.packet_header.caplen, 0xFFFFFFFF); |
1878 | 0 | total_rlen += 4; /*4 bytes for added checksum*/ |
1879 | 0 | total_wlen += 4; |
1880 | 0 | must_add_crc = true; /* XXX - these never have an FCS? */ |
1881 | 0 | } |
1882 | 0 | break; |
1883 | 0 | default: |
1884 | 0 | break; |
1885 | 0 | } |
1886 | | |
1887 | | /* Add Host ID extension header with Host ID 0 (unknown). For now use Source ID 1. */ |
1888 | 0 | other_phdr.erf.phdr.type |= 0x80; |
1889 | 0 | other_phdr.erf.ehdr_list[0].ehdr = non_erf_host_id_ehdr; |
1890 | 0 | total_rlen += 8; |
1891 | |
|
1892 | 0 | padbytes = WS_PADDING_TO_8(total_rlen); /*calculate how much padding will be required */ |
1893 | 0 | if(rec->rec_header.packet_header.caplen < rec->rec_header.packet_header.len){ /*if packet has been snapped, we need to round down what we output*/ |
1894 | 0 | round_down = (8 - padbytes) % 8; |
1895 | 0 | total_rlen -= round_down; |
1896 | 0 | }else{ |
1897 | 0 | total_rlen += padbytes; |
1898 | 0 | } |
1899 | |
|
1900 | 0 | if (total_rlen > UINT16_MAX || total_wlen > UINT16_MAX) { |
1901 | 0 | *err = WTAP_ERR_PACKET_TOO_LARGE; |
1902 | 0 | return false; |
1903 | 0 | } |
1904 | | |
1905 | 0 | other_phdr.erf.phdr.rlen = (uint16_t)total_rlen; |
1906 | 0 | other_phdr.erf.phdr.wlen = (uint16_t)total_wlen; |
1907 | |
|
1908 | 0 | pseudo_header = &other_phdr; |
1909 | 0 | } else if (rec->presence_flags & WTAP_HAS_TS) { |
1910 | | // Update timestamp if changed. |
1911 | 0 | time_t secs; |
1912 | 0 | int nsecs; |
1913 | 0 | uint64_t ts = pseudo_header->erf.phdr.ts; |
1914 | |
|
1915 | 0 | secs = (long) (ts >> 32); |
1916 | 0 | ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000); |
1917 | 0 | ts += (ts & 0x80000000) << 1; /* rounding */ |
1918 | 0 | nsecs = ((int) (ts >> 32)); |
1919 | 0 | if (nsecs >= 1000000000) { |
1920 | 0 | nsecs -= 1000000000; |
1921 | 0 | secs += 1; |
1922 | 0 | } |
1923 | |
|
1924 | 0 | if (secs != rec->ts.secs || nsecs != rec->ts.nsecs) { |
1925 | 0 | other_phdr = *pseudo_header; |
1926 | 0 | other_phdr.erf.phdr.ts = ((uint64_t) rec->ts.secs << 32) + (((uint64_t) rec->ts.nsecs <<32) / 1000 / 1000 / 1000); |
1927 | 0 | pseudo_header = &other_phdr; |
1928 | 0 | } |
1929 | 0 | } |
1930 | | |
1931 | | /* We now have a (real or fake) ERF record */ |
1932 | 0 | erf_type = pseudo_header->erf.phdr.type & 0x7FU; |
1933 | | |
1934 | | /* Accumulate Host ID/Source ID to put in updated periodic metadata */ |
1935 | | /* TODO: pass these through from read interface list instead? */ |
1936 | | /* Note: this includes the one we made for the fake ERF header */ |
1937 | 0 | erf_update_host_id_ext_hdrs_list(dump_priv, pseudo_header, dump_priv->periodic_extra_ehdrs); |
1938 | | |
1939 | | /* Insert new metadata record depending on whether the capture comment has |
1940 | | * changed. Write metadata each second at boundaries. If there is metadata |
1941 | | * write at the end of each of metadata records so we update the metadata. */ |
1942 | 0 | if (erf_type == ERF_TYPE_META) { |
1943 | | /* Check whether the capture comment string has changed */ |
1944 | | /* Updates write_next_extra_meta */ |
1945 | 0 | dump_priv->last_meta_periodic = erf_dump_priv_compare_capture_comment(wdh, dump_priv, pseudo_header, pd); |
1946 | 0 | } else { /* don't want to insert a new metadata record while looking at another */ |
1947 | 0 | if (dump_priv->prev_erf_type == ERF_TYPE_META && dump_priv->last_meta_periodic) { |
1948 | | /* Last frame was a periodic (non-comment) metadata record (and this frame is not), check if we |
1949 | | * need to insert one to update metadata. */ |
1950 | |
|
1951 | 0 | if(dump_priv->write_next_extra_meta) { |
1952 | 0 | if (!dump_priv->periodic_sections) { |
1953 | | /* If we've seen metadata just insert the capture comment and not the |
1954 | | * rest of the metadata */ |
1955 | 0 | dump_priv->periodic_sections = g_ptr_array_new_with_free_func(erf_meta_section_free); |
1956 | 0 | erf_comment_to_sections(wdh, ERF_META_SECTION_CAPTURE, 0, dump_priv->user_comment_ptr, dump_priv->periodic_sections); |
1957 | 0 | } |
1958 | |
|
1959 | 0 | if (!erf_write_meta_record(wdh, dump_priv, dump_priv->prev_frame_ts, dump_priv->periodic_sections, dump_priv->periodic_extra_ehdrs, err)) return false; |
1960 | 0 | dump_priv->prev_inserted_time_sec = rec->ts.secs; |
1961 | | /*TODO: clear accumulated existing extension headers here?*/ |
1962 | 0 | } |
1963 | | |
1964 | | /* If we have seen a metadata record in the first ~1 second it |
1965 | | * means that we are dealing with an ERF file with metadata already in them. |
1966 | | * We don't want to write extra metadata if nothing has changed. We can't |
1967 | | * trust the Wireshark representation since we massage the fields on |
1968 | | * read. */ |
1969 | | /* restart searching for next meta record to update capture comment at */ |
1970 | 0 | dump_priv->write_next_extra_meta = false; |
1971 | 0 | } else if (rec->ts.secs > dump_priv->first_frame_time_sec + 1 |
1972 | 0 | && dump_priv->prev_inserted_time_sec != rec->ts.secs) { |
1973 | | /* For compatibility, don't insert metadata for older ERF files with no changed metadata */ |
1974 | 0 | if (dump_priv->write_next_extra_meta) { |
1975 | 0 | if (!dump_priv->periodic_sections) { |
1976 | | /* If we get here, metadata record was not found in the first ~1 sec |
1977 | | * but we have either a capture comment or a non-ERF file (see |
1978 | | * erf_dump_open) */ |
1979 | | /* Start inserting metadata records from wtap data at second boundaries */ |
1980 | 0 | dump_priv->periodic_sections = g_ptr_array_new_with_free_func(erf_meta_section_free); |
1981 | 0 | erf_wtap_info_to_sections(wdh, dump_priv->periodic_sections); |
1982 | 0 | } |
1983 | 0 | } |
1984 | | |
1985 | | /* At second boundaries insert either the updated comment (if we've seen some metadata records |
1986 | | * already) or the full metadata */ |
1987 | 0 | if (dump_priv->periodic_sections) { |
1988 | 0 | if (!erf_write_meta_record(wdh, dump_priv, (uint64_t)(rec->ts.secs) << 32, dump_priv->periodic_sections, dump_priv->periodic_extra_ehdrs, err)) return false; |
1989 | 0 | dump_priv->prev_inserted_time_sec = rec->ts.secs; |
1990 | 0 | } |
1991 | 0 | } |
1992 | 0 | } |
1993 | | |
1994 | | /* If the packet user comment has changed, we need to |
1995 | | * construct a new header with additional Host ID and Anchor ID |
1996 | | * and insert a metadata record before that frame */ |
1997 | | /*XXX: The user may have changed the comment to cleared! */ |
1998 | 0 | if(rec->block_was_modified) { |
1999 | 0 | if (rec->rec_header.packet_header.pkt_encap == WTAP_ENCAP_ERF) { |
2000 | | /* XXX: What about ERF-in-pcapng with existing comment (that wasn't |
2001 | | * modified)? */ |
2002 | 0 | if(rec->block_was_modified) { |
2003 | 0 | memmove(&other_phdr, pseudo_header, sizeof(union wtap_pseudo_header)); |
2004 | 0 | if(!erf_write_anchor_meta_update_phdr(wdh, dump_priv, rec, &other_phdr, err)) return false; |
2005 | 0 | pseudo_header = &other_phdr; |
2006 | 0 | } |
2007 | 0 | } else { |
2008 | | /* Always write the comment if non-ERF */ |
2009 | 0 | if(!erf_write_anchor_meta_update_phdr(wdh, dump_priv, rec, &other_phdr, err)) return false; |
2010 | 0 | } |
2011 | 0 | } |
2012 | | |
2013 | | /* Make sure we always write out rlen, regardless of what happens */ |
2014 | 0 | alignbytes = wdh->bytes_dumped + pseudo_header->erf.phdr.rlen; |
2015 | |
|
2016 | 0 | if(!erf_write_phdr(wdh, WTAP_ENCAP_ERF, pseudo_header, err)) return false; |
2017 | | |
2018 | 0 | if(!wtap_dump_file_write(wdh, pd, rec->rec_header.packet_header.caplen - round_down, err)) return false; |
2019 | | |
2020 | | /*add the 4 byte CRC if necessary*/ |
2021 | 0 | if(must_add_crc){ |
2022 | 0 | if(!wtap_dump_file_write(wdh, &crc32, 4, err)) return false; |
2023 | 0 | } |
2024 | | |
2025 | | /*XXX: In the case of ENCAP_ERF, this pads the record to its original length, which is fine in most |
2026 | | * cases. However with >MAX_ERF_EHDR unnecessary padding will be added, and |
2027 | | * if the record was truncated this will be incorrectly treated as payload. |
2028 | | * More than 8 extension headers is unusual though, only the first 8 are |
2029 | | * written out anyway and fixing properly would require major refactor.*/ |
2030 | | /*records should be 8byte aligned, so we add padding to our calculated rlen */ |
2031 | 0 | while(wdh->bytes_dumped < alignbytes){ |
2032 | 0 | if(!wtap_dump_file_write(wdh, "", 1, err)) return false; |
2033 | 0 | } |
2034 | | |
2035 | 0 | dump_priv->prev_erf_type = pseudo_header->erf.phdr.type & 0x7FU; |
2036 | 0 | dump_priv->prev_frame_ts = pseudo_header->erf.phdr.ts; |
2037 | |
|
2038 | 0 | return true; |
2039 | 0 | } |
2040 | | |
2041 | | static int erf_dump_can_write_encap(int encap) |
2042 | 0 | { |
2043 | |
|
2044 | 0 | if(encap == WTAP_ENCAP_PER_PACKET) |
2045 | 0 | return 0; |
2046 | | |
2047 | 0 | if (wtap_wtap_encap_to_erf_encap(encap) == -1) |
2048 | 0 | return WTAP_ERR_UNWRITABLE_ENCAP; |
2049 | | |
2050 | 0 | return 0; |
2051 | 0 | } |
2052 | | |
2053 | | static bool erf_dump_open(wtap_dumper *wdh, int *err _U_, char **err_info _U_) |
2054 | 0 | { |
2055 | 0 | erf_dump_t *dump_priv; |
2056 | 0 | char *s; |
2057 | 0 | uint64_t host_id; |
2058 | 0 | char *first_shb_comment = NULL; |
2059 | |
|
2060 | 0 | dump_priv = erf_dump_priv_create(); |
2061 | |
|
2062 | 0 | wdh->subtype_write = erf_dump; |
2063 | 0 | wdh->priv = dump_priv; |
2064 | 0 | wdh->subtype_finish = erf_dump_finish; |
2065 | | |
2066 | | /* Get the first capture comment string */ |
2067 | 0 | get_user_comment_string(wdh, &first_shb_comment); |
2068 | | /* Save a copy of it */ |
2069 | 0 | dump_priv->user_comment_ptr = g_strdup(first_shb_comment); |
2070 | | /* XXX: If we have a capture comment or a non-ERF file assume we need to |
2071 | | * write metadata unless we see existing metadata in the first second. */ |
2072 | 0 | if (dump_priv->user_comment_ptr || wdh->file_encap != WTAP_ENCAP_ERF) |
2073 | 0 | dump_priv->write_next_extra_meta = true; |
2074 | | |
2075 | | /* Read Host ID from environment variable */ |
2076 | | /* TODO: generate one from MAC address? */ |
2077 | 0 | if ((s = getenv("ERF_HOST_ID")) != NULL) { |
2078 | | /* TODO: support both decimal and hex strings (base 0)? */ |
2079 | 0 | if (ws_hexstrtou64(s, NULL, &host_id)) { |
2080 | 0 | dump_priv->host_id = host_id & ERF_EHDR_HOST_ID_MASK; |
2081 | 0 | } |
2082 | 0 | } |
2083 | |
|
2084 | 0 | return true; |
2085 | 0 | } |
2086 | | |
2087 | | static int erf_get_source_from_header(union wtap_pseudo_header *pseudo_header, uint64_t *host_id, uint8_t *source_id) |
2088 | 0 | { |
2089 | 0 | uint8_t type; |
2090 | 0 | uint8_t has_more; |
2091 | 0 | uint64_t hdr; |
2092 | 0 | int i = 0; |
2093 | 0 | bool host_id_found = false; |
2094 | |
|
2095 | 0 | if (!pseudo_header || !host_id || !source_id) |
2096 | 0 | return -1; |
2097 | | |
2098 | 0 | *host_id = ERF_META_HOST_ID_IMPLICIT; |
2099 | 0 | *source_id = 0; |
2100 | |
|
2101 | 0 | has_more = pseudo_header->erf.phdr.type & 0x80; |
2102 | |
|
2103 | 0 | while (has_more && (i < MAX_ERF_EHDR)) { |
2104 | 0 | hdr = pseudo_header->erf.ehdr_list[i].ehdr; |
2105 | 0 | type = (uint8_t) (hdr >> 56); |
2106 | | |
2107 | | /* |
2108 | | * XXX: Only want first Source ID and Host ID, and want to preserve HID n SID 0 (see |
2109 | | * erf_populate_interface) |
2110 | | */ |
2111 | 0 | switch (type & 0x7f) { |
2112 | 0 | case ERF_EXT_HDR_TYPE_HOST_ID: |
2113 | 0 | if (!host_id_found) |
2114 | 0 | *host_id = hdr & ERF_EHDR_HOST_ID_MASK; |
2115 | |
|
2116 | 0 | host_id_found = true; |
2117 | | /* Fall through */ |
2118 | 0 | case ERF_EXT_HDR_TYPE_FLOW_ID: |
2119 | 0 | if (*source_id == 0) |
2120 | 0 | *source_id = (hdr >> 48) & 0xff; |
2121 | 0 | break; |
2122 | 0 | } |
2123 | | |
2124 | 0 | if (host_id_found) |
2125 | 0 | break; |
2126 | | |
2127 | 0 | has_more = type & 0x80; |
2128 | 0 | i += 1; |
2129 | 0 | } |
2130 | | |
2131 | 0 | return 0; |
2132 | 0 | } |
2133 | | |
2134 | | int erf_populate_interface_from_header(erf_t *erf_priv, wtap *wth, union wtap_pseudo_header *pseudo_header, int *err, char **err_info) |
2135 | 0 | { |
2136 | 0 | uint64_t host_id; |
2137 | 0 | uint8_t source_id; |
2138 | 0 | uint8_t if_num; |
2139 | |
|
2140 | 0 | if (!pseudo_header) |
2141 | 0 | return -1; |
2142 | | |
2143 | 0 | if_num = (pseudo_header->erf.phdr.flags & 0x03) | ((pseudo_header->erf.phdr.flags & 0x40)>>4); |
2144 | |
|
2145 | 0 | erf_get_source_from_header(pseudo_header, &host_id, &source_id); |
2146 | |
|
2147 | 0 | return erf_populate_interface(erf_priv, wth, pseudo_header, host_id, source_id, if_num, err, err_info); |
2148 | 0 | } |
2149 | | |
2150 | | static struct erf_if_mapping* erf_find_interface_mapping(erf_t *erf_priv, uint64_t host_id, uint8_t source_id) |
2151 | 0 | { |
2152 | 0 | struct erf_if_mapping if_map_lookup; |
2153 | | |
2154 | | /* XXX: erf_priv should never be NULL here */ |
2155 | 0 | if (!erf_priv) |
2156 | 0 | return NULL; |
2157 | | |
2158 | 0 | if_map_lookup.host_id = host_id; |
2159 | 0 | if_map_lookup.source_id = source_id; |
2160 | |
|
2161 | 0 | return (struct erf_if_mapping*) g_hash_table_lookup(erf_priv->if_map, &if_map_lookup); |
2162 | 0 | } |
2163 | | |
2164 | | static void erf_set_interface_descr(wtap_block_t block, unsigned option_id, uint64_t host_id, uint8_t source_id, uint8_t if_num, const char *descr) |
2165 | 0 | { |
2166 | | /* Source XXX,*/ |
2167 | 0 | char sourceid_buf[16]; |
2168 | | /* Host XXXXXXXXXXXX,*/ |
2169 | 0 | char hostid_buf[24]; |
2170 | |
|
2171 | 0 | sourceid_buf[0] = '\0'; |
2172 | 0 | hostid_buf[0] = '\0'; |
2173 | | |
2174 | | /* Implicit Host ID defaults to 0 */ |
2175 | 0 | if (host_id == ERF_META_HOST_ID_IMPLICIT) { |
2176 | 0 | host_id = 0; |
2177 | 0 | } |
2178 | |
|
2179 | 0 | if (host_id > 0) { |
2180 | 0 | snprintf(hostid_buf, sizeof(hostid_buf), " Host %012" PRIx64 ",", host_id); |
2181 | 0 | } |
2182 | |
|
2183 | 0 | if (source_id > 0) { |
2184 | 0 | snprintf(sourceid_buf, sizeof(sourceid_buf), " Source %u,", source_id); |
2185 | 0 | } |
2186 | |
|
2187 | 0 | if (descr) { |
2188 | 0 | wtap_block_set_string_option_value_format(block, option_id, "%s (ERF%s%s Interface %d)", descr, hostid_buf, sourceid_buf, if_num); |
2189 | 0 | } else { |
2190 | 0 | wtap_block_set_string_option_value_format(block, option_id, "Port %c (ERF%s%s Interface %d)", 'A'+if_num, hostid_buf, sourceid_buf, if_num); |
2191 | 0 | } |
2192 | 0 | } |
2193 | | |
2194 | | static int erf_update_anchors_from_header(erf_t *erf_priv, wtap_rec *rec, union wtap_pseudo_header *pseudo_header, uint64_t host_id, GPtrArray *anchor_mappings_to_update) |
2195 | 0 | { |
2196 | 0 | uint8_t type; |
2197 | 0 | uint8_t has_more; |
2198 | 0 | uint64_t hdr; |
2199 | 0 | uint64_t comment_gen_time = 0; |
2200 | 0 | uint64_t host_id_current; |
2201 | 0 | uint64_t anchor_id_current = 0; |
2202 | 0 | int i = 0; |
2203 | 0 | char *comment = NULL; |
2204 | |
|
2205 | 0 | if (!rec || !pseudo_header) |
2206 | 0 | return -1; |
2207 | | |
2208 | | /* Start with the first Host ID that was found on the record |
2209 | | * as the Anchor ID isn't required to be the first extension header' */ |
2210 | 0 | host_id_current = host_id == ERF_META_HOST_ID_IMPLICIT ? erf_priv->implicit_host_id : host_id; |
2211 | |
|
2212 | 0 | has_more = pseudo_header->erf.phdr.type & 0x80; |
2213 | |
|
2214 | 0 | while (has_more && (i < MAX_ERF_EHDR)) { |
2215 | 0 | hdr = pseudo_header->erf.ehdr_list[i].ehdr; |
2216 | 0 | type = (uint8_t) (hdr >> 56); |
2217 | |
|
2218 | 0 | switch (type & 0x7f) { |
2219 | 0 | case ERF_EXT_HDR_TYPE_HOST_ID: |
2220 | 0 | host_id_current = hdr & ERF_EHDR_HOST_ID_MASK; |
2221 | 0 | break; |
2222 | | |
2223 | 0 | case ERF_EXT_HDR_TYPE_ANCHOR_ID: |
2224 | 0 | { |
2225 | 0 | anchor_id_current = hdr & ERF_EHDR_ANCHOR_ID_MASK; |
2226 | 0 | if (!(ERF_ANCHOR_ID_IS_DEFINITION(hdr))) { |
2227 | | /* |
2228 | | * Anchor definition flag is 0, attempt to associate a comment with this record |
2229 | | * XXX: currently the comment count may be wrong on the first pass! |
2230 | | */ |
2231 | | /* We may not have found the implicit Host ID yet, if so we are unlikely to find anything */ |
2232 | 0 | struct erf_anchor_mapping* lookup_result; |
2233 | 0 | lookup_result = erf_find_anchor_mapping(erf_priv, host_id_current, anchor_id_current); |
2234 | 0 | if (lookup_result) { |
2235 | 0 | if (lookup_result->gen_time > comment_gen_time) { |
2236 | | /* XXX: we might have a comment that clears the comment (i.e. |
2237 | | * empty string)! */ |
2238 | 0 | if (lookup_result->comment && lookup_result->comment[0] != '\0') { |
2239 | 0 | comment = lookup_result->comment; |
2240 | 0 | } |
2241 | 0 | comment_gen_time = lookup_result->gen_time; |
2242 | 0 | } |
2243 | 0 | } |
2244 | 0 | } |
2245 | 0 | else { |
2246 | 0 | if (anchor_mappings_to_update && (pseudo_header->erf.phdr.type & 0x7f) == ERF_TYPE_META) { |
2247 | | /* |
2248 | | * Anchor definition flag is 1, put the mapping in an array |
2249 | | * which we will later update when we walk through |
2250 | | * the metadata tags |
2251 | | */ |
2252 | | /* Only Provenance record can contain the information we need */ |
2253 | 0 | struct erf_anchor_mapping *mapping_ptr = |
2254 | 0 | g_new0(struct erf_anchor_mapping, 1); |
2255 | | /* May be ERF_META_HOST_ID_IMPLICIT */ |
2256 | 0 | mapping_ptr->host_id = host_id_current; |
2257 | 0 | mapping_ptr->anchor_id = anchor_id_current; |
2258 | 0 | g_ptr_array_add(anchor_mappings_to_update, mapping_ptr); |
2259 | 0 | } |
2260 | 0 | } |
2261 | 0 | break; |
2262 | 0 | } |
2263 | 0 | } |
2264 | | |
2265 | 0 | has_more = type & 0x80; |
2266 | 0 | i += 1; |
2267 | 0 | } |
2268 | | |
2269 | 0 | if (comment) { |
2270 | 0 | wtap_block_add_string_option(rec->block, OPT_COMMENT, comment, strlen(comment)); |
2271 | 0 | } |
2272 | |
|
2273 | 0 | return 0; |
2274 | 0 | } |
2275 | | |
2276 | | /** |
2277 | | * @brief Update the implicit Host ID and Anchor Mapping information |
2278 | | */ |
2279 | | static int erf_update_implicit_host_id(erf_t *erf_priv, wtap *wth, uint64_t implicit_host_id) |
2280 | 0 | { |
2281 | 0 | GHashTableIter iter; |
2282 | 0 | void *iter_value; |
2283 | 0 | GList* implicit_list = NULL; |
2284 | 0 | GList* item = NULL; |
2285 | 0 | wtap_block_t int_data; |
2286 | 0 | struct erf_if_mapping* if_map = NULL; |
2287 | 0 | struct erf_if_mapping* if_map_other = NULL; |
2288 | 0 | struct erf_if_info* if_info = NULL; |
2289 | 0 | struct erf_anchor_mapping* anchor_mapping = NULL; |
2290 | 0 | struct erf_anchor_mapping* anchor_mapping_other = NULL; |
2291 | 0 | char *oldstr = NULL; |
2292 | 0 | char portstr_buf[16]; |
2293 | 0 | int i; |
2294 | |
|
2295 | 0 | if (!erf_priv) |
2296 | 0 | return -1; |
2297 | | |
2298 | 0 | erf_priv->implicit_host_id = implicit_host_id; |
2299 | | |
2300 | | /* |
2301 | | * We need to update the descriptions of all the interfaces with no Host |
2302 | | * ID to the correct Host ID. |
2303 | | */ |
2304 | 0 | g_hash_table_iter_init(&iter, erf_priv->if_map); |
2305 | | |
2306 | | /* Remove the implicit mappings from the mapping table */ |
2307 | 0 | while (g_hash_table_iter_next(&iter, &iter_value, NULL)) { |
2308 | 0 | if_map = (struct erf_if_mapping*) iter_value; |
2309 | |
|
2310 | 0 | if (if_map->host_id == ERF_META_HOST_ID_IMPLICIT) { |
2311 | | /* Check we don't have an existing interface that matches */ |
2312 | 0 | if_map_other = erf_find_interface_mapping(erf_priv, implicit_host_id, if_map->source_id); |
2313 | |
|
2314 | 0 | if (!if_map_other) { |
2315 | | /* Pull mapping for update */ |
2316 | | /* XXX: Can't add while iterating hash table so use list instead */ |
2317 | 0 | g_hash_table_iter_steal(&iter); |
2318 | 0 | implicit_list = g_list_prepend(implicit_list, if_map); |
2319 | 0 | } else { |
2320 | | /* |
2321 | | * XXX: We have duplicate interfaces in this case, but not much else we |
2322 | | * can do since we have already dissected the earlier packets. Expected |
2323 | | * to be unusual as it requires a mix of explicit and implicit Host ID |
2324 | | * (e.g. FlowID extension header only) packets with the same effective |
2325 | | * Host ID before the first ERF_TYPE_META record. |
2326 | | */ |
2327 | | |
2328 | | /* |
2329 | | * Update the description of the ERF_META_HOST_ID_IMPLICIT interface(s) |
2330 | | * for the first records in one pass mode. In 2 pass mode (Wireshark |
2331 | | * initial open, TShark in 2 pass mode) we will update the interface |
2332 | | * mapping for the frames on the second pass. Relatively consistent |
2333 | | * with the dissector behaviour. |
2334 | | * |
2335 | | * TODO: Can we delete this interface on the second (or even first) |
2336 | | * pass? Should we try to merge in other metadata? |
2337 | | * Needs a wtap_block_copy() that supports overwriting and/or expose |
2338 | | * custom option copy and do with wtap_block_foreach_option(). |
2339 | | */ |
2340 | 0 | for (i = 0; i < ERF_MAX_INTERFACES; i++) { |
2341 | 0 | if_info = &if_map->interfaces[i]; |
2342 | |
|
2343 | 0 | if (if_info->if_index >= 0) { |
2344 | | /* XXX: this is a pointer! */ |
2345 | 0 | int_data = g_array_index(wth->interface_data, wtap_block_t, if_info->if_index); |
2346 | |
|
2347 | 0 | snprintf(portstr_buf, sizeof(portstr_buf), "Port %c", 'A'+i); |
2348 | |
|
2349 | 0 | oldstr = if_info->name; |
2350 | 0 | if_info->name = g_strconcat(oldstr ? oldstr : portstr_buf, " [unmatched implicit]", NULL); |
2351 | 0 | g_free(oldstr); /* probably null, but g_free doesn't care */ |
2352 | |
|
2353 | 0 | oldstr = if_info->descr; |
2354 | 0 | if_info->descr = g_strconcat(oldstr ? oldstr : portstr_buf, " [unmatched implicit]", NULL); |
2355 | 0 | g_free(oldstr); |
2356 | |
|
2357 | 0 | erf_set_interface_descr(int_data, OPT_IDB_NAME, implicit_host_id, if_map->source_id, (uint8_t) i, if_info->name); |
2358 | 0 | erf_set_interface_descr(int_data, OPT_IDB_DESCRIPTION, implicit_host_id, if_map->source_id, (uint8_t) i, if_info->descr); |
2359 | 0 | } |
2360 | 0 | } |
2361 | 0 | } |
2362 | 0 | } |
2363 | 0 | } |
2364 | | |
2365 | | /* Re-add the non-clashing items under the real implicit Host ID */ |
2366 | 0 | if (implicit_list) { |
2367 | 0 | item = implicit_list; |
2368 | 0 | do { |
2369 | 0 | if_map = (struct erf_if_mapping*) item->data; |
2370 | |
|
2371 | 0 | for (i = 0; i < ERF_MAX_INTERFACES; i++) { |
2372 | 0 | if_info = &if_map->interfaces[i]; |
2373 | |
|
2374 | 0 | if (if_info->if_index >= 0) { |
2375 | | /* XXX: this is a pointer! */ |
2376 | 0 | int_data = g_array_index(wth->interface_data, wtap_block_t, if_info->if_index); |
2377 | 0 | erf_set_interface_descr(int_data, OPT_IDB_NAME, implicit_host_id, if_map->source_id, (uint8_t) i, if_info->name); |
2378 | 0 | erf_set_interface_descr(int_data, OPT_IDB_DESCRIPTION, implicit_host_id, if_map->source_id, (uint8_t) i, if_info->descr); |
2379 | 0 | } |
2380 | 0 | } |
2381 | |
|
2382 | 0 | if_map->host_id = implicit_host_id; |
2383 | | /* g_hash_table_add() only exists since 2.32. */ |
2384 | 0 | g_hash_table_replace(erf_priv->if_map, if_map, if_map); |
2385 | 0 | } while ((item = g_list_next(item))); |
2386 | |
|
2387 | 0 | g_list_free(implicit_list); |
2388 | 0 | implicit_list = NULL; |
2389 | 0 | } |
2390 | | |
2391 | | /* |
2392 | | * We also need to update the anchor comment mappings |
2393 | | * to the correct Host ID. |
2394 | | */ |
2395 | 0 | g_hash_table_iter_init(&iter, erf_priv->anchor_map); |
2396 | | |
2397 | | /* Remove the implicit mappings from the mapping table */ |
2398 | 0 | while (g_hash_table_iter_next(&iter, &iter_value, NULL)) { |
2399 | 0 | anchor_mapping = (struct erf_anchor_mapping*) iter_value; |
2400 | |
|
2401 | 0 | if (anchor_mapping->host_id == ERF_META_HOST_ID_IMPLICIT) { |
2402 | | /* Check we don't have an existing anchor that matches */ |
2403 | 0 | anchor_mapping_other = erf_find_anchor_mapping(erf_priv, implicit_host_id, |
2404 | 0 | anchor_mapping->anchor_id); |
2405 | |
|
2406 | 0 | if (anchor_mapping_other && anchor_mapping_other->gen_time >= anchor_mapping->gen_time) { |
2407 | | /* |
2408 | | * XXX: Duplicate entry of anchor mapping, keep the one with newer |
2409 | | * gen_time. |
2410 | | */ |
2411 | 0 | g_hash_table_iter_remove(&iter); |
2412 | 0 | } else { |
2413 | | /* Pull mapping for update */ |
2414 | | /* XXX: Can't add while iterating hash table so use list instead */ |
2415 | 0 | g_hash_table_iter_steal(&iter); |
2416 | 0 | implicit_list = g_list_prepend(implicit_list, anchor_mapping); |
2417 | | /* existing entry (if any) will be removed by g_hash_table_replace */ |
2418 | 0 | } |
2419 | 0 | } |
2420 | 0 | } |
2421 | | |
2422 | | /* Re-add the non-clashing items under the real implicit Host ID */ |
2423 | 0 | if (implicit_list) { |
2424 | 0 | item = implicit_list; |
2425 | 0 | do { |
2426 | 0 | anchor_mapping = (struct erf_anchor_mapping*) item->data; |
2427 | 0 | anchor_mapping->host_id = implicit_host_id; |
2428 | 0 | g_hash_table_replace(erf_priv->anchor_map, anchor_mapping, anchor_mapping); |
2429 | 0 | } while ((item = g_list_next(item))); |
2430 | |
|
2431 | 0 | g_list_free(implicit_list); |
2432 | 0 | implicit_list = NULL; |
2433 | 0 | } |
2434 | |
|
2435 | 0 | return 0; |
2436 | 0 | } |
2437 | | |
2438 | | static int erf_populate_interface(erf_t *erf_priv, wtap *wth, union wtap_pseudo_header *pseudo_header, uint64_t host_id, uint8_t source_id, uint8_t if_num, int *err, char **err_info) |
2439 | 0 | { |
2440 | 0 | wtap_block_t int_data; |
2441 | 0 | wtapng_if_descr_mandatory_t* int_data_mand; |
2442 | 0 | struct erf_if_mapping* if_map = NULL; |
2443 | |
|
2444 | 0 | if (!wth) { |
2445 | 0 | *err = WTAP_ERR_INTERNAL; |
2446 | 0 | *err_info = ws_strdup_printf("erf: erf_populate_interface called with wth NULL"); |
2447 | 0 | return -1; |
2448 | 0 | } |
2449 | 0 | if (!pseudo_header) { |
2450 | 0 | *err = WTAP_ERR_INTERNAL; |
2451 | 0 | *err_info = ws_strdup_printf("erf: erf_populate_interface called with pseudo_header NULL"); |
2452 | 0 | return -1; |
2453 | 0 | } |
2454 | 0 | if (!erf_priv) { |
2455 | 0 | *err = WTAP_ERR_INTERNAL; |
2456 | 0 | *err_info = ws_strdup_printf("erf: erf_populate_interface called with erf_priv NULL"); |
2457 | 0 | return -1; |
2458 | 0 | } |
2459 | 0 | if (if_num > ERF_MAX_INTERFACES-1) { |
2460 | 0 | *err = WTAP_ERR_INTERNAL; |
2461 | 0 | *err_info = ws_strdup_printf("erf: erf_populate_interface called with if_num %u > %u", |
2462 | 0 | if_num, ERF_MAX_INTERFACES-1); |
2463 | 0 | return -1; |
2464 | 0 | } |
2465 | | |
2466 | 0 | if (host_id == ERF_META_HOST_ID_IMPLICIT) { |
2467 | | /* Defaults to ERF_META_HOST_ID_IMPLICIT so we can update mapping later */ |
2468 | 0 | host_id = erf_priv->implicit_host_id; |
2469 | 0 | } else if ((pseudo_header->erf.phdr.type & 0x7f) == ERF_TYPE_META) { |
2470 | | /* |
2471 | | * XXX: We assume there is only one Implicit Host ID. As a special case a first |
2472 | | * Host ID extension header with Source ID 0 on a record does not change |
2473 | | * the implicit Host ID. We respect this even though we support only one |
2474 | | * Implicit Host ID. |
2475 | | */ |
2476 | 0 | if (erf_priv->implicit_host_id == ERF_META_HOST_ID_IMPLICIT && source_id > 0) { |
2477 | 0 | erf_update_implicit_host_id(erf_priv, wth, host_id); |
2478 | 0 | } |
2479 | 0 | } |
2480 | |
|
2481 | 0 | if_map = erf_find_interface_mapping(erf_priv, host_id, source_id); |
2482 | |
|
2483 | 0 | if (!if_map) { |
2484 | 0 | if_map = erf_if_mapping_create(host_id, source_id); |
2485 | | /* g_hash_table_add() only exists since 2.32. */ |
2486 | 0 | g_hash_table_replace(erf_priv->if_map, if_map, if_map); |
2487 | |
|
2488 | 0 | } |
2489 | | |
2490 | | /* Return the existing interface if we have it */ |
2491 | 0 | if (if_map->interfaces[if_num].if_index >= 0) { |
2492 | 0 | return if_map->interfaces[if_num].if_index; |
2493 | 0 | } |
2494 | | |
2495 | 0 | int_data = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO); |
2496 | 0 | int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data); |
2497 | |
|
2498 | 0 | int_data_mand->wtap_encap = WTAP_ENCAP_ERF; |
2499 | | /* int_data.time_units_per_second = (1LL<<32); ERF format resolution is 2^-32, capture resolution is unknown */ |
2500 | 0 | int_data_mand->time_units_per_second = 1000000000; /* XXX Since Wireshark only supports down to nanosecond resolution we have to dilute to this */ |
2501 | 0 | int_data_mand->tsprecision = WTAP_TSPREC_NSEC; |
2502 | 0 | int_data_mand->snap_len = 65535; /* ERF max length */ |
2503 | | |
2504 | | /* XXX: if_IPv4addr opt 4 Interface network address and netmask.*/ |
2505 | | /* XXX: if_IPv6addr opt 5 Interface network address and prefix length (stored in the last byte).*/ |
2506 | | /* XXX: if_MACaddr opt 6 Interface Hardware MAC address (48 bits).*/ |
2507 | | /* XXX: if_EUIaddr opt 7 Interface Hardware EUI address (64 bits)*/ |
2508 | | /* XXX: if_speed opt 8 Interface speed (in bits per second)*/ |
2509 | | /* int_data.if_tsresol = 0xa0; ERF format resolution is 2^-32 = 0xa0, capture resolution is unknown */ |
2510 | 0 | wtap_block_add_uint8_option(int_data, OPT_IDB_TSRESOL, 0x09); /* XXX Since Wireshark only supports down to nanosecond resolution we have to dilute to this */ |
2511 | | /* XXX: if_tzone 10 Time zone for GMT support (TODO: specify better). */ |
2512 | | /* XXX if_tsoffset; opt 14 A 64 bits integer value that specifies an offset (in seconds)...*/ |
2513 | | /* Interface statistics */ |
2514 | 0 | int_data_mand->num_stat_entries = 0; |
2515 | 0 | int_data_mand->interface_statistics = NULL; |
2516 | |
|
2517 | 0 | erf_set_interface_descr(int_data, OPT_IDB_NAME, host_id, source_id, if_num, NULL); |
2518 | 0 | erf_set_interface_descr(int_data, OPT_IDB_DESCRIPTION, host_id, source_id, if_num, NULL); |
2519 | |
|
2520 | 0 | if_map->interfaces[if_num].if_index = (int) wth->interface_data->len; |
2521 | 0 | wtap_add_idb(wth, int_data); |
2522 | |
|
2523 | 0 | return if_map->interfaces[if_num].if_index; |
2524 | 0 | } |
2525 | | |
2526 | | static uint32_t erf_meta_read_tag(struct erf_meta_tag* tag, uint8_t *tag_ptr, uint32_t remaining_len) |
2527 | 0 | { |
2528 | 0 | uint16_t tagtype; |
2529 | 0 | uint16_t taglength; |
2530 | 0 | uint32_t tagtotallength; |
2531 | |
|
2532 | 0 | if (!tag_ptr || !tag || remaining_len < ERF_META_TAG_HEADERLEN) |
2533 | 0 | return 0; |
2534 | | |
2535 | | /* tagtype (2 bytes) */ |
2536 | 0 | tagtype = pntohu16(&tag_ptr[0]); |
2537 | | |
2538 | | /* length (2 bytes) */ |
2539 | 0 | taglength = pntohu16(&tag_ptr[2]); |
2540 | |
|
2541 | 0 | tagtotallength = ERF_META_TAG_TOTAL_ALIGNED_LENGTH(taglength); |
2542 | |
|
2543 | 0 | if (remaining_len < tagtotallength) { |
2544 | 0 | return 0; |
2545 | 0 | } |
2546 | | |
2547 | 0 | tag->type = tagtype; |
2548 | 0 | tag->length = taglength; |
2549 | 0 | tag->value = &tag_ptr[4]; |
2550 | |
|
2551 | 0 | return tagtotallength; |
2552 | 0 | } |
2553 | | |
2554 | | static int populate_capture_host_info(erf_t *erf_priv, wtap *wth, union wtap_pseudo_header *pseudo_header _U_, struct erf_meta_read_state *state, int *err, char **err_info) |
2555 | 0 | { |
2556 | 0 | struct erf_meta_tag tag = {0, 0, NULL}; |
2557 | |
|
2558 | 0 | wtap_block_t shb_hdr; |
2559 | 0 | char* tmp; |
2560 | 0 | char* app_name = NULL; |
2561 | 0 | char* app_version = NULL; |
2562 | 0 | char* model = NULL; |
2563 | 0 | char* descr = NULL; |
2564 | 0 | char* cpu = NULL; |
2565 | 0 | char* modelcpu = NULL; |
2566 | 0 | uint32_t tagtotallength; |
2567 | |
|
2568 | 0 | if (!wth) { |
2569 | 0 | *err = WTAP_ERR_INTERNAL; |
2570 | 0 | *err_info = ws_strdup_printf("erf: populate_capture_host_info called with wth NULL"); |
2571 | 0 | return -1; |
2572 | 0 | } |
2573 | 0 | if (!state) { |
2574 | 0 | *err = WTAP_ERR_INTERNAL; |
2575 | 0 | *err_info = ws_strdup_printf("erf: populate_capture_host_info called with state NULL"); |
2576 | 0 | return -1; |
2577 | 0 | } |
2578 | 0 | if (!wth->shb_hdrs) { |
2579 | 0 | *err = WTAP_ERR_INTERNAL; |
2580 | 0 | *err_info = ws_strdup_printf("erf: populate_capture_host_info called with wth->shb_hdrs NULL"); |
2581 | 0 | return -1; |
2582 | 0 | } |
2583 | 0 | if (wth->shb_hdrs->len == 0) { |
2584 | 0 | *err = WTAP_ERR_INTERNAL; |
2585 | 0 | *err_info = ws_strdup_printf("erf: populate_capture_host_info called with wth->shb_hdrs->len 0"); |
2586 | 0 | return -1; |
2587 | 0 | } |
2588 | | |
2589 | | /* XXX: wth->shb_hdr is already created by different layer, using directly for now. */ |
2590 | | /* XXX: Only one section header is supported at this time */ |
2591 | 0 | shb_hdr = g_array_index(wth->shb_hdrs, wtap_block_t, 0); |
2592 | |
|
2593 | 0 | while ((tagtotallength = erf_meta_read_tag(&tag, state->tag_ptr, state->remaining_len)) && !ERF_META_IS_SECTION(tag.type)) { |
2594 | 0 | switch (state->sectiontype) { |
2595 | 0 | case ERF_META_SECTION_CAPTURE: |
2596 | 0 | { |
2597 | 0 | if (erf_priv->capture_gentime > state->gen_time) { |
2598 | 0 | return 0; |
2599 | 0 | } |
2600 | | |
2601 | 0 | switch (tag.type) { |
2602 | 0 | case ERF_META_TAG_comment: |
2603 | 0 | { |
2604 | 0 | char *existing_comment = NULL; |
2605 | | /*XXX: hack to make changing capture comment work since Wireshark only |
2606 | | * displays one. For now just overwrite the comment as we won't |
2607 | | * pick up all of them yet due to the gen_time check above */ |
2608 | 0 | if (wtap_block_get_nth_string_option_value(shb_hdr, OPT_COMMENT, 0, &existing_comment) == WTAP_OPTTYPE_SUCCESS) { |
2609 | 0 | wtap_block_set_nth_string_option_value(shb_hdr, OPT_COMMENT, 0, (const char*)tag.value, tag.length); |
2610 | 0 | } else { |
2611 | 0 | wtap_block_add_string_option(shb_hdr, OPT_COMMENT, (const char*)tag.value, tag.length); |
2612 | 0 | } |
2613 | 0 | break; |
2614 | 0 | } |
2615 | 0 | } |
2616 | 0 | } |
2617 | | /* Fall through */ |
2618 | 0 | case ERF_META_SECTION_HOST: |
2619 | 0 | { |
2620 | 0 | if (erf_priv->host_gentime > state->gen_time) { |
2621 | 0 | return 0; |
2622 | 0 | } |
2623 | | |
2624 | 0 | switch (tag.type) { |
2625 | 0 | case ERF_META_TAG_model: |
2626 | 0 | g_free(model); |
2627 | 0 | model = g_strndup((char*) tag.value, tag.length); |
2628 | 0 | break; |
2629 | 0 | case ERF_META_TAG_cpu: |
2630 | 0 | g_free(cpu); |
2631 | 0 | cpu = g_strndup((char*) tag.value, tag.length); |
2632 | 0 | break; |
2633 | 0 | case ERF_META_TAG_descr: |
2634 | 0 | g_free(descr); |
2635 | 0 | descr = g_strndup((char*) tag.value, tag.length); |
2636 | 0 | break; |
2637 | 0 | case ERF_META_TAG_os: |
2638 | 0 | wtap_block_set_string_option_value(shb_hdr, OPT_SHB_OS, (const char*)tag.value, tag.length); |
2639 | 0 | break; |
2640 | 0 | case ERF_META_TAG_app_name: |
2641 | 0 | g_free(app_name); |
2642 | 0 | app_name = g_strndup((char*) tag.value, tag.length); |
2643 | 0 | break; |
2644 | 0 | case ERF_META_TAG_app_version: |
2645 | 0 | g_free(app_version); |
2646 | 0 | app_version = g_strndup((char*) tag.value, tag.length); |
2647 | 0 | break; |
2648 | | /* TODO: dag_version? */ |
2649 | | /* TODO: could concatenate comment(s)? */ |
2650 | 0 | case ERF_META_TAG_filter: |
2651 | 0 | g_free(state->if_map->capture_filter_str); |
2652 | 0 | state->if_map->capture_filter_str = g_strndup((char*) tag.value, tag.length); |
2653 | 0 | break; |
2654 | 0 | default: |
2655 | 0 | break; |
2656 | 0 | } |
2657 | 0 | } |
2658 | 0 | break; |
2659 | 0 | } |
2660 | | |
2661 | 0 | state->tag_ptr += tagtotallength; |
2662 | 0 | state->remaining_len -= tagtotallength; |
2663 | 0 | } |
2664 | | |
2665 | | /* Post processing */ |
2666 | | |
2667 | 0 | if (app_name || app_version) { |
2668 | | /* |
2669 | | * If we have no app_name, we use "(Unknown application)". |
2670 | | * |
2671 | | * If we have no app_version, this will just use app_name. |
2672 | | */ |
2673 | | // coverity[var_deref_model:FALSE] |
2674 | 0 | tmp = g_strjoin(" ", app_name ? app_name : "(Unknown application)", app_version, NULL); |
2675 | 0 | wtap_block_set_string_option_value(shb_hdr, OPT_SHB_USERAPPL, tmp, strlen(tmp)); |
2676 | 0 | g_free(tmp); |
2677 | |
|
2678 | 0 | g_free(app_name); |
2679 | 0 | g_free(app_version); |
2680 | 0 | app_name = NULL; |
2681 | 0 | app_version = NULL; |
2682 | 0 | } |
2683 | | |
2684 | | /* For the hardware field show description followed by (model; cpu) */ |
2685 | | /* Build "Model; CPU" part */ |
2686 | 0 | if (model || cpu) { |
2687 | | /* g_strjoin() would be nice to use here if the API didn't stop on the first NULL... */ |
2688 | 0 | if (model && cpu) { |
2689 | 0 | modelcpu = g_strconcat(model, "; ", cpu, NULL); |
2690 | 0 | } else if (cpu) { |
2691 | 0 | modelcpu = cpu; |
2692 | | /* avoid double-free */ |
2693 | 0 | cpu = NULL; |
2694 | 0 | } else { |
2695 | 0 | modelcpu = model; |
2696 | | /* avoid double-free */ |
2697 | 0 | model = NULL; |
2698 | 0 | } |
2699 | 0 | } |
2700 | | |
2701 | | /* Combine into "Description (Model; CPU)" */ |
2702 | 0 | if (state->sectiontype == ERF_META_SECTION_HOST && descr) { |
2703 | 0 | if (modelcpu) { |
2704 | 0 | wtap_block_set_string_option_value_format(shb_hdr, OPT_SHB_HARDWARE, "%s (%s)", descr, modelcpu); |
2705 | 0 | } else { |
2706 | 0 | wtap_block_set_string_option_value(shb_hdr, OPT_SHB_HARDWARE, descr, strlen(descr)); |
2707 | | /*descr = NULL;*/ |
2708 | 0 | } |
2709 | 0 | } else if (modelcpu) { |
2710 | 0 | wtap_block_set_string_option_value(shb_hdr, OPT_SHB_HARDWARE, modelcpu, strlen(modelcpu)); |
2711 | | /*modelcpu = NULL;*/ |
2712 | 0 | } |
2713 | | |
2714 | | /* Free the fields we didn't end up using */ |
2715 | 0 | g_free(modelcpu); |
2716 | 0 | g_free(model); |
2717 | 0 | g_free(descr); |
2718 | 0 | g_free(cpu); |
2719 | |
|
2720 | 0 | if (state->sectiontype == ERF_META_SECTION_CAPTURE) { |
2721 | 0 | erf_priv->capture_gentime = state->gen_time; |
2722 | 0 | } else { |
2723 | 0 | erf_priv->host_gentime = state->gen_time; |
2724 | 0 | } |
2725 | |
|
2726 | 0 | return 1; |
2727 | 0 | } |
2728 | | |
2729 | | static int populate_module_info(erf_t *erf_priv _U_, wtap *wth, union wtap_pseudo_header *pseudo_header _U_, struct erf_meta_read_state *state, int *err, char **err_info) |
2730 | 0 | { |
2731 | 0 | struct erf_meta_tag tag = {0, 0, NULL}; |
2732 | |
|
2733 | 0 | uint32_t tagtotallength; |
2734 | |
|
2735 | 0 | if (!wth) { |
2736 | 0 | *err = WTAP_ERR_INTERNAL; |
2737 | 0 | *err_info = ws_strdup_printf("erf: populate_module_info called with wth NULL"); |
2738 | 0 | return -1; |
2739 | 0 | } |
2740 | 0 | if (!state) { |
2741 | 0 | *err = WTAP_ERR_INTERNAL; |
2742 | 0 | *err_info = ws_strdup_printf("erf: populate_module_info called with stat NULL"); |
2743 | 0 | return -1; |
2744 | 0 | } |
2745 | | |
2746 | 0 | if (state->if_map->module_gentime > state->gen_time) { |
2747 | 0 | return 0; |
2748 | 0 | } |
2749 | | |
2750 | 0 | while ((tagtotallength = erf_meta_read_tag(&tag, state->tag_ptr, state->remaining_len)) && !ERF_META_IS_SECTION(tag.type)) { |
2751 | 0 | switch (tag.type) { |
2752 | 0 | case ERF_META_TAG_fcs_len: |
2753 | 0 | if (tag.length >= 4) { |
2754 | 0 | state->if_map->module_fcs_len = (int8_t) pntohu32(tag.value); |
2755 | 0 | } |
2756 | 0 | break; |
2757 | 0 | case ERF_META_TAG_snaplen: |
2758 | | /* XXX: this is generally per stream */ |
2759 | 0 | if (tag.length >= 4) { |
2760 | 0 | state->if_map->module_snaplen = pntohu32(tag.value); |
2761 | 0 | } |
2762 | 0 | break; |
2763 | 0 | case ERF_META_TAG_filter: |
2764 | 0 | g_free(state->if_map->module_filter_str); |
2765 | 0 | state->if_map->module_filter_str = g_strndup((char*) tag.value, tag.length); |
2766 | 0 | break; |
2767 | 0 | } |
2768 | | |
2769 | 0 | state->tag_ptr += tagtotallength; |
2770 | 0 | state->remaining_len -= tagtotallength; |
2771 | 0 | } |
2772 | | |
2773 | 0 | state->if_map->module_gentime = state->gen_time; |
2774 | |
|
2775 | 0 | return 1; |
2776 | 0 | } |
2777 | | |
2778 | | static int populate_interface_info(erf_t *erf_priv, wtap *wth, union wtap_pseudo_header *pseudo_header, struct erf_meta_read_state *state, int *err, char **err_info) |
2779 | 0 | { |
2780 | 0 | struct erf_meta_tag tag = {0, 0, NULL}; |
2781 | 0 | uint32_t tagtotallength; |
2782 | 0 | int interface_index = -1; |
2783 | 0 | wtap_block_t int_data = NULL; |
2784 | 0 | wtapng_if_descr_mandatory_t* int_data_mand = NULL; |
2785 | 0 | if_filter_opt_t if_filter; |
2786 | 0 | uint32_t if_num = 0; |
2787 | 0 | struct erf_if_info* if_info = NULL; |
2788 | |
|
2789 | 0 | if (!wth) { |
2790 | 0 | *err = WTAP_ERR_INTERNAL; |
2791 | 0 | *err_info = ws_strdup_printf("erf: populate_interface_info called with wth NULL"); |
2792 | 0 | return -1; |
2793 | 0 | } |
2794 | 0 | if (!state) { |
2795 | 0 | *err = WTAP_ERR_INTERNAL; |
2796 | 0 | *err_info = ws_strdup_printf("erf: populate_interface_info called with state NULL"); |
2797 | 0 | return -1; |
2798 | 0 | } |
2799 | 0 | if (!pseudo_header) { |
2800 | 0 | *err = WTAP_ERR_INTERNAL; |
2801 | 0 | *err_info = ws_strdup_printf("erf: populate_interface_info called with pseudo_header NULL"); |
2802 | 0 | return -1; |
2803 | 0 | } |
2804 | 0 | if (!state->if_map) { |
2805 | 0 | *err = WTAP_ERR_INTERNAL; |
2806 | 0 | *err_info = ws_strdup_printf("erf: populate_interface_info called with state->if_map NULL"); |
2807 | 0 | return -1; |
2808 | 0 | } |
2809 | | |
2810 | | /* Section ID of interface is defined to match ERF interface id. */ |
2811 | 0 | if_num = state->sectionid - 1; |
2812 | | /* |
2813 | | * Get or create the interface (there can be multiple interfaces in |
2814 | | * a Provenance record). |
2815 | | */ |
2816 | 0 | if (if_num < ERF_MAX_INTERFACES) { /* Note: -1u > ERF_MAX_INTERFACES */ |
2817 | 0 | if_info = &state->if_map->interfaces[if_num]; |
2818 | 0 | interface_index = if_info->if_index; |
2819 | | |
2820 | | /* Check if the interface information is still uninitialized */ |
2821 | 0 | if (interface_index == -1) { |
2822 | 0 | uint8_t *tag_ptr_tmp = state->tag_ptr; |
2823 | 0 | uint32_t remaining_len_tmp = state->remaining_len; |
2824 | | |
2825 | | /* First iterate tags, checking we aren't looking at a timing port */ |
2826 | | /* |
2827 | | * XXX: we deliberately only do this logic here rather than the per-packet |
2828 | | * population function so that if somehow we do see packets for an |
2829 | | * 'invalid' port the interface will be created at that time. |
2830 | | */ |
2831 | 0 | while ((tagtotallength = erf_meta_read_tag(&tag, tag_ptr_tmp, remaining_len_tmp)) && !ERF_META_IS_SECTION(tag.type)) { |
2832 | 0 | if (tag.type == ERF_META_TAG_if_port_type) { |
2833 | 0 | if (tag.length >= 4 && pntohu32(tag.value) == 2) { |
2834 | | /* This is a timing port, skip it from now on */ |
2835 | | /* XXX: should we skip all non-capture ports instead? */ |
2836 | |
|
2837 | 0 | if_info->if_index = -2; |
2838 | 0 | interface_index = -2; |
2839 | 0 | } |
2840 | 0 | } else if (tag.type == ERF_META_TAG_stream_num) { |
2841 | 0 | if (tag.length >= 4) { |
2842 | 0 | if_info->stream_num = (int32_t) pntohu32(tag.value); |
2843 | 0 | } |
2844 | 0 | } |
2845 | |
|
2846 | 0 | tag_ptr_tmp += tagtotallength; |
2847 | 0 | remaining_len_tmp -= tagtotallength; |
2848 | 0 | } |
2849 | | |
2850 | | /* If the interface is valid but uninitialized, create it */ |
2851 | 0 | if (interface_index == -1) { |
2852 | 0 | interface_index = erf_populate_interface(erf_priv, wth, pseudo_header, state->if_map->host_id, state->if_map->source_id, (uint8_t) if_num, err, err_info); |
2853 | 0 | if (interface_index == -1) { |
2854 | 0 | return -1; |
2855 | 0 | } |
2856 | 0 | } |
2857 | 0 | } |
2858 | | |
2859 | | /* Get the wiretap interface metadata */ |
2860 | 0 | if (interface_index >= 0) { |
2861 | 0 | int_data = g_array_index(wth->interface_data, wtap_block_t, interface_index); |
2862 | 0 | int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data); |
2863 | 0 | } else if (interface_index == -2) { |
2864 | | /* timing/unknown port */ |
2865 | 0 | return 0; |
2866 | 0 | } else { |
2867 | 0 | *err = WTAP_ERR_INTERNAL; |
2868 | 0 | *err_info = ws_strdup_printf("erf: populate_interface_info got interface_index %d < 0 and != -2", interface_index); |
2869 | 0 | return -1; |
2870 | 0 | } |
2871 | 0 | } |
2872 | | |
2873 | | /* |
2874 | | * Bail if already have interface metadata or no interface to associate with. |
2875 | | * We also don't support metadata for >ERF_MAX_INTERFACES interfaces per Host + Source |
2876 | | * as we only use interface ID. |
2877 | | */ |
2878 | 0 | if (!int_data) |
2879 | 0 | return 0; |
2880 | | |
2881 | 0 | if (state->if_map->interface_gentime > state->gen_time && state->if_map->interface_metadata & (1 << if_num)) |
2882 | 0 | return 0; |
2883 | | |
2884 | 0 | while ((tagtotallength = erf_meta_read_tag(&tag, state->tag_ptr, state->remaining_len)) && !ERF_META_IS_SECTION(tag.type)) { |
2885 | 0 | switch (tag.type) { |
2886 | 0 | case ERF_META_TAG_name: |
2887 | | /* TODO: fall back to module "dev_name Port N"? */ |
2888 | 0 | if (!if_info->name) { |
2889 | 0 | if_info->name = g_strndup((char*) tag.value, tag.length); |
2890 | 0 | erf_set_interface_descr(int_data, OPT_IDB_NAME, state->if_map->host_id, state->if_map->source_id, (uint8_t) if_num, if_info->name); |
2891 | | |
2892 | | /* If we have no description, also copy to wtap if_description */ |
2893 | 0 | if (!if_info->descr) { |
2894 | 0 | erf_set_interface_descr(int_data, OPT_IDB_DESCRIPTION, state->if_map->host_id, state->if_map->source_id, (uint8_t) if_num, if_info->name); |
2895 | 0 | } |
2896 | 0 | } |
2897 | 0 | break; |
2898 | 0 | case ERF_META_TAG_descr: |
2899 | 0 | if (!if_info->descr) { |
2900 | 0 | if_info->descr = g_strndup((char*) tag.value, tag.length); |
2901 | 0 | erf_set_interface_descr(int_data, OPT_IDB_DESCRIPTION, state->if_map->host_id, state->if_map->source_id, (uint8_t) if_num, if_info->descr); |
2902 | | |
2903 | | /* If we have no name, also copy to wtap if_name */ |
2904 | 0 | if (!if_info->name) { |
2905 | 0 | erf_set_interface_descr(int_data, OPT_IDB_NAME, state->if_map->host_id, state->if_map->source_id, (uint8_t) if_num, if_info->descr); |
2906 | 0 | } |
2907 | 0 | } |
2908 | 0 | break; |
2909 | 0 | case ERF_META_TAG_if_speed: |
2910 | 0 | if (tag.length >= 8) |
2911 | 0 | wtap_block_add_uint64_option(int_data, OPT_IDB_SPEED, pntohu64(tag.value)); |
2912 | 0 | break; |
2913 | 0 | case ERF_META_TAG_if_num: |
2914 | | /* |
2915 | | * XXX: We ignore this as Section ID must match the ERF ifid and |
2916 | | * that is all we care about/have space for at the moment. if_num |
2917 | | * is only really useful with >ERF_MAX_INTERFACES interfaces. |
2918 | | */ |
2919 | | /* TODO: might want to put this number in description */ |
2920 | 0 | break; |
2921 | 0 | case ERF_META_TAG_fcs_len: |
2922 | 0 | if (tag.length >= 4) { |
2923 | 0 | wtap_block_add_uint8_option(int_data, OPT_IDB_FCSLEN, (uint8_t) pntohu32(tag.value)); |
2924 | 0 | if_info->set_flags.fcs_len = 1; |
2925 | 0 | } |
2926 | 0 | break; |
2927 | 0 | case ERF_META_TAG_snaplen: |
2928 | | /* XXX: this generally per stream */ |
2929 | 0 | if (tag.length >= 4) { |
2930 | 0 | int_data_mand->snap_len = pntohu32(tag.value); |
2931 | 0 | if_info->set_flags.snaplen = 1; |
2932 | 0 | } |
2933 | 0 | break; |
2934 | 0 | case ERF_META_TAG_comment: |
2935 | 0 | wtap_block_add_string_option(int_data, OPT_COMMENT, (const char*)tag.value, tag.length); |
2936 | 0 | break; |
2937 | 0 | case ERF_META_TAG_filter: |
2938 | 0 | if_filter.type = if_filter_pcap; |
2939 | 0 | if_filter.data.filter_str = g_strndup((char*) tag.value, tag.length); |
2940 | 0 | wtap_block_add_if_filter_option(int_data, OPT_IDB_FILTER, &if_filter); |
2941 | 0 | g_free(if_filter.data.filter_str); |
2942 | 0 | if_info->set_flags.filter = 1; |
2943 | 0 | break; |
2944 | 0 | default: |
2945 | 0 | break; |
2946 | 0 | } |
2947 | | |
2948 | 0 | state->tag_ptr += tagtotallength; |
2949 | 0 | state->remaining_len -= tagtotallength; |
2950 | 0 | } |
2951 | | |
2952 | | /* Post processing */ |
2953 | | /* |
2954 | | * XXX: Assumes module defined first. It is higher in hierarchy so only set |
2955 | | * if not already. |
2956 | | */ |
2957 | | |
2958 | | /* |
2959 | | * XXX: Missing exposed existence/type-check. No way currently to check if |
2960 | | * been set in the optionblock. |
2961 | | */ |
2962 | 0 | if (!if_info->set_flags.filter) { |
2963 | 0 | if (state->if_map->module_filter_str) { |
2964 | | /* Duplicate because might use with multiple interfaces */ |
2965 | 0 | if_filter.type = if_filter_pcap; |
2966 | 0 | if_filter.data.filter_str = state->if_map->module_filter_str; |
2967 | 0 | wtap_block_add_if_filter_option(int_data, OPT_IDB_FILTER, &if_filter); |
2968 | | /* |
2969 | | * Don't set flag because stream is more specific than module. |
2970 | | */ |
2971 | 0 | } else if (state->if_map->capture_filter_str) { |
2972 | | /* TODO: display separately? Note that we could have multiple captures |
2973 | | * from multiple hosts in the file */ |
2974 | 0 | if_filter.type = if_filter_pcap; |
2975 | 0 | if_filter.data.filter_str = state->if_map->capture_filter_str; |
2976 | 0 | wtap_block_add_if_filter_option(int_data, OPT_IDB_FILTER, &if_filter); |
2977 | 0 | } |
2978 | 0 | } |
2979 | |
|
2980 | 0 | if (state->if_map->module_fcs_len != -1 && !if_info->set_flags.fcs_len) { |
2981 | 0 | wtap_block_add_uint8_option(int_data, OPT_IDB_FCSLEN, (uint8_t) state->if_map->module_fcs_len); |
2982 | 0 | if_info->set_flags.fcs_len = 1; |
2983 | 0 | } |
2984 | |
|
2985 | 0 | if (state->if_map->module_snaplen != (uint32_t) -1 && !if_info->set_flags.snaplen && tag.value) { |
2986 | 0 | int_data_mand->snap_len = pntohu32(tag.value); |
2987 | 0 | if_info->set_flags.snaplen = 1; |
2988 | 0 | } |
2989 | |
|
2990 | 0 | state->interface_metadata |= 1 << if_num; |
2991 | |
|
2992 | 0 | return 1; |
2993 | 0 | } |
2994 | | |
2995 | | static int populate_stream_info(erf_t *erf_priv _U_, wtap *wth, union wtap_pseudo_header *pseudo_header, struct erf_meta_read_state *state, int *err, char **err_info) |
2996 | 0 | { |
2997 | 0 | struct erf_meta_tag tag = {0, 0, NULL}; |
2998 | 0 | uint32_t tagtotallength; |
2999 | 0 | int interface_index = -1; |
3000 | 0 | wtap_block_t int_data = NULL; |
3001 | 0 | wtapng_if_descr_mandatory_t* int_data_mand = NULL; |
3002 | 0 | if_filter_opt_t if_filter; |
3003 | 0 | uint32_t if_num = 0; |
3004 | 0 | int32_t stream_num = -1; |
3005 | 0 | uint8_t *tag_ptr_tmp; |
3006 | 0 | uint32_t remaining_len_tmp; |
3007 | 0 | struct erf_if_info* if_info = NULL; |
3008 | |
|
3009 | 0 | if (!wth) { |
3010 | 0 | *err = WTAP_ERR_INTERNAL; |
3011 | 0 | *err_info = ws_strdup_printf("erf: populate_stream_info called with wth NULL"); |
3012 | 0 | return -1; |
3013 | 0 | } |
3014 | 0 | if (!pseudo_header) { |
3015 | 0 | *err = WTAP_ERR_INTERNAL; |
3016 | 0 | *err_info = ws_strdup_printf("erf: populate_stream_info called with pseudo_header NULL"); |
3017 | 0 | return -1; |
3018 | 0 | } |
3019 | 0 | if (!state) { |
3020 | 0 | *err = WTAP_ERR_INTERNAL; |
3021 | 0 | *err_info = ws_strdup_printf("erf: populate_stream_info called with state NULL"); |
3022 | 0 | return -1; |
3023 | 0 | } |
3024 | 0 | if (!state->if_map) { |
3025 | 0 | *err = WTAP_ERR_INTERNAL; |
3026 | 0 | *err_info = ws_strdup_printf("erf: populate_stream_info called with state->if_map NULL"); |
3027 | 0 | return -1; |
3028 | 0 | } |
3029 | | |
3030 | 0 | tag_ptr_tmp = state->tag_ptr; |
3031 | 0 | remaining_len_tmp = state->remaining_len; |
3032 | | |
3033 | | /* |
3034 | | * XXX: We ignore parent section ID because it doesn't represent the |
3035 | | * many-to-many relationship of interfaces and streams very well. The stream is |
3036 | | * associated with all interfaces in the record that don't have a stream_num |
3037 | | * that says otherwise. |
3038 | | */ |
3039 | |
|
3040 | 0 | if (state->sectionid > 0 && state->sectionid != 0x7fff) { |
3041 | | /* Section ID of stream is supposed to match stream_num. */ |
3042 | 0 | stream_num = state->sectionid - 1; |
3043 | 0 | } else { |
3044 | | /* First iterate tags, looking for the stream number interfaces might associate with. */ |
3045 | 0 | while ((tagtotallength = erf_meta_read_tag(&tag, tag_ptr_tmp, remaining_len_tmp)) && !ERF_META_IS_SECTION(tag.type)) { |
3046 | 0 | if (tag.type == ERF_META_TAG_stream_num) { |
3047 | 0 | if (tag.length >= 4) { |
3048 | 0 | stream_num = (int32_t) pntohu32(tag.value); |
3049 | 0 | } |
3050 | 0 | } |
3051 | |
|
3052 | 0 | tag_ptr_tmp += tagtotallength; |
3053 | 0 | remaining_len_tmp -= tagtotallength; |
3054 | 0 | } |
3055 | 0 | } |
3056 | | /* Otherwise assume the stream applies to all interfaces in the record */ |
3057 | |
|
3058 | 0 | for (if_num = 0; if_num < ERF_MAX_INTERFACES; if_num++) { |
3059 | 0 | tag_ptr_tmp = state->tag_ptr; |
3060 | 0 | remaining_len_tmp = state->remaining_len; |
3061 | 0 | if_info = &state->if_map->interfaces[if_num]; |
3062 | | |
3063 | | /* Check if we should be handling this interface */ |
3064 | | /* XXX: currently skips interfaces that are not in the record. */ |
3065 | 0 | if (state->if_map->interface_metadata & (1 << if_num) |
3066 | 0 | || !(state->interface_metadata & (1 << if_num))) { |
3067 | 0 | continue; |
3068 | 0 | } |
3069 | | |
3070 | 0 | if (if_info->stream_num != -1 |
3071 | 0 | && if_info->stream_num != stream_num) { |
3072 | 0 | continue; |
3073 | 0 | } |
3074 | | |
3075 | 0 | interface_index = if_info->if_index; |
3076 | | /* Get the wiretap interface metadata */ |
3077 | 0 | if (interface_index >= 0) { |
3078 | 0 | int_data = g_array_index(wth->interface_data, wtap_block_t, interface_index); |
3079 | 0 | int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data); |
3080 | 0 | } |
3081 | |
|
3082 | 0 | if (!int_data) { |
3083 | 0 | continue; |
3084 | 0 | } |
3085 | | |
3086 | 0 | while ((tagtotallength = erf_meta_read_tag(&tag, tag_ptr_tmp, remaining_len_tmp)) && !ERF_META_IS_SECTION(tag.type)) { |
3087 | 0 | switch (tag.type) { |
3088 | 0 | case ERF_META_TAG_fcs_len: |
3089 | 0 | if (tag.length >= 4) { |
3090 | | /* Use the largest fcslen of matching streams */ |
3091 | 0 | int8_t fcs_len = (int8_t) pntohu32(tag.value); |
3092 | 0 | uint8_t old_fcs_len = 0; |
3093 | |
|
3094 | 0 | switch (wtap_block_get_uint8_option_value(int_data, OPT_IDB_FCSLEN, &old_fcs_len)) { |
3095 | | |
3096 | 0 | case WTAP_OPTTYPE_SUCCESS: |
3097 | | /* We already have an FCS length option; update it. */ |
3098 | 0 | if (fcs_len > old_fcs_len || !if_info->set_flags.fcs_len) { |
3099 | 0 | wtap_block_set_uint8_option_value(int_data, OPT_IDB_FCSLEN, (uint8_t) pntohu32(tag.value)); |
3100 | 0 | if_info->set_flags.fcs_len = 1; |
3101 | 0 | } |
3102 | 0 | break; |
3103 | | |
3104 | 0 | case WTAP_OPTTYPE_NOT_FOUND: |
3105 | | /* We don't have an FCS length option; add it. */ |
3106 | 0 | wtap_block_add_uint8_option(int_data, OPT_IDB_FCSLEN, (uint8_t) pntohu32(tag.value)); |
3107 | 0 | if_info->set_flags.fcs_len = 1; |
3108 | 0 | break; |
3109 | | |
3110 | 0 | default: |
3111 | | /* "shouldn't happen" */ |
3112 | 0 | break; |
3113 | 0 | } |
3114 | 0 | } |
3115 | 0 | break; |
3116 | 0 | case ERF_META_TAG_snaplen: |
3117 | 0 | if (tag.length >= 4) { |
3118 | | /* Use the largest snaplen of matching streams */ |
3119 | 0 | uint32_t snaplen = pntohu32(tag.value); |
3120 | |
|
3121 | 0 | if (snaplen > int_data_mand->snap_len || !if_info->set_flags.snaplen) { |
3122 | 0 | int_data_mand->snap_len = pntohu32(tag.value); |
3123 | 0 | if_info->set_flags.snaplen = 1; |
3124 | 0 | } |
3125 | 0 | } |
3126 | 0 | break; |
3127 | 0 | case ERF_META_TAG_filter: |
3128 | | /* Override only if not set */ |
3129 | 0 | if (!if_info->set_flags.filter) { |
3130 | 0 | if_filter.type = if_filter_pcap; |
3131 | 0 | if_filter.data.filter_str = g_strndup((char*) tag.value, tag.length); |
3132 | 0 | wtap_block_add_if_filter_option(int_data, OPT_IDB_FILTER, &if_filter); |
3133 | 0 | g_free(if_filter.data.filter_str); |
3134 | 0 | if_info->set_flags.filter = 1; |
3135 | 0 | } |
3136 | 0 | break; |
3137 | 0 | default: |
3138 | 0 | break; |
3139 | 0 | } |
3140 | | |
3141 | 0 | tag_ptr_tmp += tagtotallength; |
3142 | 0 | remaining_len_tmp -= tagtotallength; |
3143 | 0 | } |
3144 | 0 | } |
3145 | 0 | state->tag_ptr = tag_ptr_tmp; |
3146 | 0 | state->remaining_len = remaining_len_tmp; |
3147 | |
|
3148 | 0 | return 1; |
3149 | 0 | } |
3150 | | |
3151 | 0 | static int populate_anchor_info(erf_t *erf_priv, wtap *wth, union wtap_pseudo_header *pseudo_header, struct erf_meta_read_state *state, GPtrArray *anchor_mappings_to_update, int *err, char **err_info) { |
3152 | 0 | struct erf_meta_tag tag = {0, 0, NULL}; |
3153 | 0 | uint32_t tagtotallength; |
3154 | 0 | char *comment_ptr = NULL; |
3155 | 0 | unsigned i = 0; |
3156 | |
|
3157 | 0 | if (!wth) { |
3158 | 0 | *err = WTAP_ERR_INTERNAL; |
3159 | 0 | *err_info = ws_strdup_printf("erf: populate_anchor_info called with wth NULL"); |
3160 | 0 | return -1; |
3161 | 0 | } |
3162 | 0 | if (!state) { |
3163 | 0 | *err = WTAP_ERR_INTERNAL; |
3164 | 0 | *err_info = ws_strdup_printf("erf: populate_anchor_info called with state NULL"); |
3165 | 0 | return -1; |
3166 | 0 | } |
3167 | 0 | if (!pseudo_header) { |
3168 | 0 | *err = WTAP_ERR_INTERNAL; |
3169 | 0 | *err_info = ws_strdup_printf("erf: populate_anchor_info called with pseudo_header NULL"); |
3170 | 0 | return -1; |
3171 | 0 | } |
3172 | | |
3173 | 0 | if (!anchor_mappings_to_update || anchor_mappings_to_update->len == 0) |
3174 | 0 | return 0; |
3175 | | |
3176 | 0 | while ((tagtotallength = erf_meta_read_tag(&tag, state->tag_ptr, state->remaining_len)) && !ERF_META_IS_SECTION(tag.type)) { |
3177 | | /* XXX:Always gets the first comment tag in the section */ |
3178 | 0 | switch(tag.type) { |
3179 | 0 | case ERF_META_TAG_comment: |
3180 | 0 | if(!comment_ptr) { |
3181 | 0 | comment_ptr = g_strndup((char*)tag.value, tag.length); |
3182 | 0 | } |
3183 | 0 | break; |
3184 | 0 | default: |
3185 | 0 | break; |
3186 | 0 | } |
3187 | | |
3188 | 0 | state->tag_ptr += tagtotallength; |
3189 | 0 | state->remaining_len -= tagtotallength; |
3190 | 0 | } |
3191 | | |
3192 | 0 | if(comment_ptr) { |
3193 | 0 | for(i = 0; i < anchor_mappings_to_update->len; i++) { |
3194 | 0 | struct erf_anchor_mapping *mapping; |
3195 | 0 | struct erf_anchor_mapping *lookup_result; |
3196 | |
|
3197 | 0 | mapping = (struct erf_anchor_mapping*)g_ptr_array_index(anchor_mappings_to_update, i); |
3198 | 0 | lookup_result = (struct erf_anchor_mapping*)g_hash_table_lookup(erf_priv->anchor_map, mapping); |
3199 | | |
3200 | | /* Use the most recent comment, across all anchors associated with the |
3201 | | * record. */ |
3202 | 0 | if(lookup_result) { |
3203 | 0 | if(lookup_result->gen_time < state->gen_time) { |
3204 | 0 | lookup_result->gen_time = state->gen_time; |
3205 | 0 | g_free(lookup_result->comment); |
3206 | 0 | lookup_result->comment = g_strdup(comment_ptr); |
3207 | 0 | } |
3208 | 0 | } |
3209 | 0 | else { |
3210 | | /* !lookup_result */ |
3211 | 0 | struct erf_anchor_mapping *new_mapping; |
3212 | 0 | new_mapping = g_new0(struct erf_anchor_mapping, 1); |
3213 | 0 | new_mapping->anchor_id = mapping->anchor_id; |
3214 | 0 | new_mapping->host_id = mapping->host_id; |
3215 | 0 | new_mapping->gen_time = state->gen_time; |
3216 | 0 | new_mapping->comment = g_strdup(comment_ptr); |
3217 | 0 | g_hash_table_replace(erf_priv->anchor_map, new_mapping, new_mapping); |
3218 | 0 | } |
3219 | 0 | } |
3220 | 0 | } |
3221 | |
|
3222 | 0 | g_free(comment_ptr); |
3223 | |
|
3224 | 0 | return 1; |
3225 | 0 | } |
3226 | | |
3227 | | /* Populates the capture and interface information for display on the Capture File Properties */ |
3228 | | static int populate_summary_info(erf_t *erf_priv, wtap *wth, wtap_rec *rec, uint32_t packet_size, GPtrArray *anchor_mappings_to_update, int *err, char **err_info) |
3229 | 0 | { |
3230 | 0 | struct erf_meta_read_state state = {0}; |
3231 | 0 | struct erf_meta_read_state *state_post = NULL; |
3232 | 0 | uint64_t host_id = 0; |
3233 | 0 | uint8_t source_id = 0; |
3234 | 0 | GList *post_list = NULL; |
3235 | 0 | GList *item = NULL; |
3236 | |
|
3237 | 0 | struct erf_meta_tag tag = {0, 0, NULL}; |
3238 | 0 | uint32_t tagtotallength; |
3239 | |
|
3240 | 0 | if (!wth) { |
3241 | 0 | *err = WTAP_ERR_INTERNAL; |
3242 | 0 | *err_info = ws_strdup_printf("erf: populate_summary_info called with wth NULL"); |
3243 | 0 | return -1; |
3244 | 0 | } |
3245 | 0 | if (!erf_priv) { |
3246 | 0 | *err = WTAP_ERR_INTERNAL; |
3247 | 0 | *err_info = ws_strdup_printf("erf: populate_summary_info called with erf_priv NULL"); |
3248 | 0 | return -1; |
3249 | 0 | } |
3250 | | |
3251 | 0 | erf_get_source_from_header(&rec->rec_header.packet_header.pseudo_header, &host_id, &source_id); |
3252 | |
|
3253 | 0 | if (host_id == 0) { |
3254 | 0 | host_id = erf_priv->implicit_host_id; |
3255 | 0 | } |
3256 | |
|
3257 | 0 | state.if_map = erf_find_interface_mapping(erf_priv, host_id, source_id); |
3258 | |
|
3259 | 0 | if (!state.if_map) { |
3260 | 0 | state.if_map = erf_if_mapping_create(host_id, source_id); |
3261 | | /* g_hash_table_add() only exists since 2.32. */ |
3262 | 0 | g_hash_table_replace(erf_priv->if_map, state.if_map, state.if_map); |
3263 | |
|
3264 | 0 | } |
3265 | | |
3266 | |
|
3267 | 0 | state.tag_ptr = rec->data.data; |
3268 | 0 | state.remaining_len = packet_size; |
3269 | | |
3270 | | /* Read until see next section tag */ |
3271 | 0 | while ((tagtotallength = erf_meta_read_tag(&tag, state.tag_ptr, state.remaining_len))) { |
3272 | | /* |
3273 | | * Obtain the gen_time from the non-section at the beginning of the record |
3274 | | */ |
3275 | 0 | if (!ERF_META_IS_SECTION(tag.type)) { |
3276 | 0 | if(state.gen_time == 0U |
3277 | 0 | && tag.type == ERF_META_TAG_gen_time |
3278 | 0 | ) { |
3279 | 0 | memcpy(&state.gen_time, tag.value, sizeof(state.gen_time)); |
3280 | | |
3281 | | /* |
3282 | | * Since wireshark doesn't have a concept of different summary metadata |
3283 | | * over time, skip the record if metadata is older than what we already have. |
3284 | | */ |
3285 | | /* TODO: This doesn't work very well for some tags that map to |
3286 | | * pcapng options where the pcapng specification only allows one |
3287 | | * instance per block, which is the case for most options. The |
3288 | | * only current exceptions are: |
3289 | | * |
3290 | | * comments; |
3291 | | * IPv4 and IPv6 addresses for an interface; |
3292 | | * hash values for a packet; |
3293 | | * custom options. |
3294 | | * |
3295 | | * For options where only one instance is allowed per block, |
3296 | | * wtap_block_add_XXX_option() is currently used to add a new |
3297 | | * instance of an option to a block that has no instance (it |
3298 | | * fails if there's already an instance), and |
3299 | | * wtap_block_set_XXX_optin() is currently used to change the |
3300 | | * value of an option in a block that has one instance (it fails |
3301 | | * if there isn't already an instance). |
3302 | | * |
3303 | | * For options where more than one instance is allowed per block, |
3304 | | * wtap_block_add_XXX_option() is used to add a new instance to |
3305 | | * a block, no matter how many instances it currently has, and |
3306 | | * wtap_block_set_nth_XXX_option() is used to change the value |
3307 | | * of the Nth instance of an option in a block (the block must |
3308 | | * *have* an Nth instance). |
3309 | | * |
3310 | | * Currently we only particularly care about updating the capture comment |
3311 | | * and a few counters anyway. |
3312 | | */ |
3313 | 0 | if ((state.if_map->interface_metadata & 0xff) |
3314 | 0 | && state.gen_time < erf_priv->host_gentime && state.gen_time < erf_priv->capture_gentime |
3315 | 0 | && (!anchor_mappings_to_update || !anchor_mappings_to_update->len)) { |
3316 | 0 | return 0; |
3317 | 0 | } |
3318 | 0 | } |
3319 | | /* |
3320 | | * Skip until we get to the next section tag (which could be the current tag |
3321 | | * after an empty section or successful parsing). |
3322 | | */ |
3323 | | /* adjust offset */ |
3324 | 0 | state.tag_ptr += tagtotallength; |
3325 | 0 | state.remaining_len -= tagtotallength; |
3326 | 0 | continue; |
3327 | 0 | } |
3328 | | |
3329 | | /* |
3330 | | * We are now looking at the next section (and would have exited the loop |
3331 | | * if we reached the end). |
3332 | | */ |
3333 | | |
3334 | | /* Update parent section. Implicit grouping is by a change in section except Interface and Stream. */ |
3335 | 0 | if (tag.type != state.sectiontype) { |
3336 | 0 | if ((tag.type == ERF_META_SECTION_STREAM && state.sectiontype == ERF_META_SECTION_INTERFACE) || |
3337 | 0 | (tag.type == ERF_META_SECTION_INTERFACE && state.sectiontype == ERF_META_SECTION_STREAM)) { |
3338 | | /* do nothing */ |
3339 | 0 | } else { |
3340 | 0 | state.parentsectiontype = state.sectiontype; |
3341 | 0 | state.parentsectionid = state.sectionid; |
3342 | 0 | } |
3343 | 0 | } |
3344 | | |
3345 | | /* Update with new sectiontype */ |
3346 | 0 | state.sectiontype = tag.type; |
3347 | 0 | if (tag.length >= 4) { |
3348 | 0 | state.sectionid = pntohu16(tag.value); |
3349 | 0 | } else { |
3350 | 0 | state.sectionid = 0; |
3351 | 0 | } |
3352 | | |
3353 | | /* Adjust offset to that of first tag in section */ |
3354 | 0 | state.tag_ptr += tagtotallength; |
3355 | 0 | state.remaining_len -= tagtotallength; |
3356 | |
|
3357 | 0 | if (erf_meta_read_tag(&tag, state.tag_ptr, state.remaining_len)) { |
3358 | | /* |
3359 | | * Process parent section tag if present (which must be the first tag in |
3360 | | * the section). |
3361 | | */ |
3362 | 0 | if (tag.type == ERF_META_TAG_parent_section && tag.length >= 4) { |
3363 | 0 | state.parentsectiontype = pntohu16(tag.value); |
3364 | 0 | state.parentsectionid = pntohu16(&tag.value[2]); |
3365 | 0 | } |
3366 | 0 | } |
3367 | | |
3368 | | /* Skip empty sections (includes if above read fails) */ |
3369 | 0 | if (ERF_META_IS_SECTION(tag.type)) { |
3370 | 0 | continue; |
3371 | 0 | } |
3372 | | |
3373 | | /* |
3374 | | * Skip sections that don't apply to the general set of records |
3375 | | * (extension point for per-packet/event metadata). |
3376 | | * Unless we need to update the anchor info |
3377 | | * in which case, read into it |
3378 | | */ |
3379 | 0 | if (state.sectionid & 0x8000) { |
3380 | 0 | if(state.sectiontype & (ERF_META_SECTION_INFO)) { |
3381 | | /* TODO: do we care if it returns 0 or 1? */ |
3382 | 0 | if (populate_anchor_info(erf_priv, wth, &rec->rec_header.packet_header.pseudo_header, &state, anchor_mappings_to_update, err, err_info) < 0) { |
3383 | 0 | return -1; |
3384 | 0 | } |
3385 | 0 | } |
3386 | 0 | continue; |
3387 | 0 | } |
3388 | | |
3389 | | /* |
3390 | | * Start at first tag in section, makes loop |
3391 | | * simpler in called functions too. Also makes iterating after failure |
3392 | | * much simpler. |
3393 | | */ |
3394 | 0 | switch (state.sectiontype) { |
3395 | 0 | case ERF_META_SECTION_CAPTURE: |
3396 | 0 | case ERF_META_SECTION_HOST: |
3397 | | /* TODO: do we care if it returns 0 or 1? */ |
3398 | 0 | if (populate_capture_host_info(erf_priv, wth, &rec->rec_header.packet_header.pseudo_header, &state, err, err_info) < 0) { |
3399 | 0 | return -1; |
3400 | 0 | } |
3401 | 0 | break; |
3402 | 0 | case ERF_META_SECTION_MODULE: |
3403 | | /* TODO: do we care if it returns 0 or 1? */ |
3404 | 0 | if (populate_module_info(erf_priv, wth, &rec->rec_header.packet_header.pseudo_header, &state, err, err_info) < 0) { |
3405 | 0 | return -1; |
3406 | 0 | } |
3407 | 0 | break; |
3408 | 0 | case ERF_META_SECTION_INTERFACE: |
3409 | | /* TODO: do we care if it returns 0 or 1? */ |
3410 | 0 | if (populate_interface_info(erf_priv, wth, &rec->rec_header.packet_header.pseudo_header, &state, err, err_info) < 0) { |
3411 | 0 | return -1; |
3412 | 0 | } |
3413 | 0 | break; |
3414 | 0 | case ERF_META_SECTION_STREAM: |
3415 | | /* |
3416 | | * XXX: Treat streams specially in case the stream information appears |
3417 | | * before the interface information, as we associate them to interface |
3418 | | * data. |
3419 | | */ |
3420 | 0 | post_list = g_list_append(post_list, g_memdup2(&state, sizeof(struct erf_meta_read_state))); |
3421 | 0 | break; |
3422 | 0 | case ERF_META_SECTION_SOURCE: |
3423 | 0 | case ERF_META_SECTION_DNS: |
3424 | 0 | default: |
3425 | | /* TODO: Not yet implemented */ |
3426 | 0 | break; |
3427 | 0 | } |
3428 | 0 | } |
3429 | | |
3430 | | /* Process streams last */ |
3431 | 0 | if (post_list) { |
3432 | 0 | item = post_list; |
3433 | 0 | do { |
3434 | 0 | state_post = (struct erf_meta_read_state*) item->data; |
3435 | 0 | switch (state_post->sectiontype) { |
3436 | 0 | case ERF_META_SECTION_STREAM: |
3437 | 0 | if (populate_stream_info(erf_priv, wth, &rec->rec_header.packet_header.pseudo_header, state_post, err, err_info) < 0) { |
3438 | 0 | g_list_foreach(post_list, erf_free_data, NULL); |
3439 | 0 | g_list_free(post_list); |
3440 | 0 | return -1; |
3441 | 0 | } |
3442 | 0 | break; |
3443 | 0 | } |
3444 | 0 | } while ((item = g_list_next(item))); |
3445 | | /* g_list_free_full() only exists since 2.28. */ |
3446 | 0 | g_list_foreach(post_list, erf_free_data, NULL); |
3447 | 0 | g_list_free(post_list); |
3448 | 0 | } |
3449 | | |
3450 | | /* |
3451 | | * Update known metadata so we only examine the first set of metadata. Need to |
3452 | | * do this here so can have interface and stream in same record. |
3453 | | */ |
3454 | 0 | if (state.interface_metadata) { |
3455 | 0 | state.if_map->interface_metadata |= state.interface_metadata; |
3456 | 0 | state.if_map->interface_gentime = state.gen_time; |
3457 | 0 | } |
3458 | |
|
3459 | 0 | return 0; |
3460 | 0 | } |
3461 | | |
3462 | 0 | static bool get_user_comment_string(wtap_dumper *wdh, char** user_comment_ptr) { |
3463 | 0 | wtap_block_t wtap_block; |
3464 | 0 | wtap_opttype_return_val ret; |
3465 | |
|
3466 | 0 | wtap_block = NULL; |
3467 | |
|
3468 | 0 | if(wdh->shb_hdrs && (wdh->shb_hdrs->len > 0)) { |
3469 | 0 | wtap_block = g_array_index(wdh->shb_hdrs, wtap_block_t, 0); |
3470 | 0 | } |
3471 | |
|
3472 | 0 | if(wtap_block != NULL) { |
3473 | 0 | ret = wtap_block_get_nth_string_option_value(wtap_block, OPT_COMMENT, 0, user_comment_ptr); |
3474 | 0 | if(ret != WTAP_OPTTYPE_SUCCESS) { |
3475 | 0 | return false; |
3476 | 0 | } |
3477 | 0 | } |
3478 | | |
3479 | 0 | return true; |
3480 | 0 | } |
3481 | | |
3482 | 0 | static bool erf_dump_priv_compare_capture_comment(wtap_dumper *wdh _U_, erf_dump_t *dump_priv, const union wtap_pseudo_header *pseudo_header, const uint8_t *pd){ |
3483 | 0 | struct erf_meta_read_state state = {0}; |
3484 | 0 | struct erf_meta_tag tag = {0, 0, NULL}; |
3485 | 0 | uint32_t tagtotallength; |
3486 | 0 | bool found_capture_section = false; |
3487 | 0 | bool found_normal_section = false; |
3488 | 0 | char* comment_ptr = NULL; |
3489 | |
|
3490 | 0 | state.remaining_len = pseudo_header->erf.phdr.wlen; |
3491 | 0 | memcpy(&(state.tag_ptr), &pd, sizeof(pd)); |
3492 | |
|
3493 | 0 | while((tagtotallength = erf_meta_read_tag(&tag, state.tag_ptr, state.remaining_len))) { |
3494 | 0 | if (ERF_META_IS_SECTION(tag.type)) { |
3495 | 0 | state.sectiontype = tag.type; |
3496 | 0 | if (tag.length >= 4) { |
3497 | 0 | state.sectionid = pntohu16(tag.value); |
3498 | 0 | } else { |
3499 | 0 | state.sectionid = 0; |
3500 | 0 | } |
3501 | | |
3502 | | /* Skip sections that don't apply to the general set of records */ |
3503 | 0 | if (!(state.sectionid & 0x8000)) { |
3504 | 0 | found_normal_section = true; |
3505 | |
|
3506 | 0 | if(tag.type == ERF_META_SECTION_CAPTURE) { |
3507 | | /* Found the Capture Section */ |
3508 | 0 | found_capture_section = true; |
3509 | 0 | } |
3510 | 0 | } |
3511 | 0 | } else { |
3512 | 0 | if (state.sectiontype == ERF_META_SECTION_CAPTURE && !(state.sectionid & 0x8000)) { |
3513 | 0 | if (tag.type == ERF_META_TAG_comment) { |
3514 | | /* XXX: Only compare the first comment tag */ |
3515 | 0 | if(!comment_ptr) { |
3516 | 0 | comment_ptr = g_strndup((char*)tag.value, tag.length); |
3517 | 0 | } |
3518 | 0 | break; |
3519 | 0 | } |
3520 | 0 | } |
3521 | 0 | } |
3522 | | |
3523 | | /* Read until we have the Capture section */ |
3524 | 0 | state.tag_ptr += tagtotallength; |
3525 | 0 | state.remaining_len -= tagtotallength; |
3526 | 0 | } |
3527 | |
|
3528 | 0 | if(found_capture_section && (comment_ptr || dump_priv->user_comment_ptr)) { |
3529 | 0 | if(g_strcmp0(comment_ptr, dump_priv->user_comment_ptr) |
3530 | 0 | && !(dump_priv->user_comment_ptr == NULL && comment_ptr && comment_ptr[0] == '\0')) { |
3531 | | /* Also treat "" in ERF as equivalent to NULL as that is how we clear the comment on write. */ |
3532 | | |
3533 | | /* Comments are different, we should write extra metadata record at the end of the list */ |
3534 | 0 | dump_priv->write_next_extra_meta = true; |
3535 | 0 | g_free(comment_ptr); |
3536 | 0 | return true; |
3537 | 0 | } else { |
3538 | | /* We have a capture comment but there is no change, we don't |
3539 | | * need to insert the 'changed' comment. This most likely happened |
3540 | | * because we were looking at list of periodic records and got up to the |
3541 | | * one where the comment was last set. */ |
3542 | 0 | dump_priv->write_next_extra_meta = false; |
3543 | 0 | } |
3544 | | /* Otherwise no effect on whether we need to write extra metadata record */ |
3545 | 0 | } |
3546 | | /* We didn't find a capture section (e.g. looking at a comment Anchor |
3547 | | * record), or the comment hadn't changed. */ |
3548 | | |
3549 | 0 | g_free(comment_ptr); |
3550 | | /* Return whether we found any non-local metadata (i.e. whether the record has |
3551 | | * metadata that is more than just packet 'comments') */ |
3552 | 0 | return found_normal_section; |
3553 | 0 | } |
3554 | | |
3555 | | static void erf_close(wtap *wth) |
3556 | 0 | { |
3557 | 0 | erf_t* erf_priv = (erf_t*)wth->priv; |
3558 | |
|
3559 | 0 | erf_priv_free(erf_priv); |
3560 | | /* XXX: Prevent double free by wtap_close() */ |
3561 | 0 | wth->priv = NULL; |
3562 | 0 | } |
3563 | | |
3564 | | static const struct supported_option_type section_block_options_supported[] = { |
3565 | | { OPT_COMMENT, ONE_OPTION_SUPPORTED }, /* XXX - multiple? */ |
3566 | | { OPT_SHB_USERAPPL, ONE_OPTION_SUPPORTED } |
3567 | | }; |
3568 | | |
3569 | | static const struct supported_option_type interface_block_options_supported[] = { |
3570 | | { OPT_COMMENT, ONE_OPTION_SUPPORTED }, /* XXX - multiple? */ |
3571 | | { OPT_IDB_NAME, ONE_OPTION_SUPPORTED }, |
3572 | | { OPT_IDB_DESCRIPTION, ONE_OPTION_SUPPORTED }, |
3573 | | { OPT_IDB_OS, ONE_OPTION_SUPPORTED }, |
3574 | | { OPT_IDB_TSOFFSET, ONE_OPTION_SUPPORTED }, |
3575 | | { OPT_IDB_SPEED, ONE_OPTION_SUPPORTED }, |
3576 | | { OPT_IDB_IP4ADDR, ONE_OPTION_SUPPORTED }, /* XXX - multiple? */ |
3577 | | { OPT_IDB_IP6ADDR, ONE_OPTION_SUPPORTED }, /* XXX - multiple? */ |
3578 | | { OPT_IDB_FILTER, ONE_OPTION_SUPPORTED }, |
3579 | | { OPT_IDB_FCSLEN, ONE_OPTION_SUPPORTED } |
3580 | | }; |
3581 | | |
3582 | | static const struct supported_option_type packet_block_options_supported[] = { |
3583 | | { OPT_COMMENT, ONE_OPTION_SUPPORTED } /* XXX - multiple? */ |
3584 | | }; |
3585 | | |
3586 | | static const struct supported_block_type erf_blocks_supported[] = { |
3587 | | /* |
3588 | | * Per-file comments and application supported; section blocks |
3589 | | * are used for that. |
3590 | | * ERF files have only one section. (XXX - true?) |
3591 | | */ |
3592 | | { WTAP_BLOCK_SECTION, ONE_BLOCK_SUPPORTED, OPTION_TYPES_SUPPORTED(section_block_options_supported) }, |
3593 | | |
3594 | | /* |
3595 | | * ERF supports multiple interfaces, with information, and |
3596 | | * supports associating packets with interfaces. Interface |
3597 | | * description blocks are used for that. |
3598 | | */ |
3599 | | { WTAP_BLOCK_IF_ID_AND_INFO, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(interface_block_options_supported) }, |
3600 | | |
3601 | | /* |
3602 | | * Name resolution is supported, but we don't support comments. |
3603 | | */ |
3604 | | { WTAP_BLOCK_NAME_RESOLUTION, ONE_BLOCK_SUPPORTED, NO_OPTIONS_SUPPORTED }, |
3605 | | |
3606 | | /* |
3607 | | * ERF is a capture format, so it obviously supports packets. |
3608 | | */ |
3609 | | { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(packet_block_options_supported) } |
3610 | | }; |
3611 | | |
3612 | | static const struct file_type_subtype_info erf_info = { |
3613 | | "Endace ERF capture", "erf", "erf", NULL, |
3614 | | false, BLOCKS_SUPPORTED(erf_blocks_supported), |
3615 | | erf_dump_can_write_encap, erf_dump_open, NULL |
3616 | | }; |
3617 | | |
3618 | | void register_erf(void) |
3619 | 14 | { |
3620 | 14 | erf_file_type_subtype = wtap_register_file_type_subtype(&erf_info); |
3621 | | |
3622 | | /* |
3623 | | * Register name for backwards compatibility with the |
3624 | | * wtap_filetypes table in Lua. |
3625 | | */ |
3626 | 14 | wtap_register_backwards_compatibility_lua_name("ERF", erf_file_type_subtype); |
3627 | 14 | } |
3628 | | |
3629 | | /* |
3630 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
3631 | | * |
3632 | | * Local Variables: |
3633 | | * c-basic-offset: 2 |
3634 | | * tab-width: 8 |
3635 | | * indent-tabs-mode: nil |
3636 | | * End: |
3637 | | * |
3638 | | * vi: set shiftwidth=2 tabstop=8 expandtab: |
3639 | | * :indentSize=2:tabSize=8:noTabs=true: |
3640 | | */ |