### Bug Summary

File: | builds/wireshark/wireshark/epan/reassemble.c
Warning: | line 2012, column 6 Potential leak of memory pointed to by 'data'

### Annotated Source Code

Press '?' to see keyboard shortcuts

Show analyzer invocation
clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name reassemble.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -isystem /builds/wireshark/wireshark/epan -isystem /builds/wireshark/wireshark/build/epan -isystem /usr/include/mit-krb5 -isystem /usr/include/lua5.5 -isystem /usr/include/libxml2 -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D REX_API=extern -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D epan_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/wiretap -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-08-07-100414-3660-1 -x c /builds/wireshark/wireshark/epan/reassemble.c

Keyboard shortcuts:

- Use 'j/k' keys for keyboard navigation
- Use 'Shift+S' to show/hide relevant lines
- Use '?' to toggle this window

Close
Show only relevant lines Show control flow arrows

1 | /* reassemble.c
2 | * Routines for {fragment,segment} reassembly
3 | *
4 | * Wireshark - Network traffic analyzer
5 | * By Gerald Combs <gerald@wireshark.org>
6 | * Copyright 1998 Gerald Combs
7 | *
8 | * SPDX-License-Identifier: GPL-2.0-or-later
9 | */
10 |
11 | #include "config.h"
12 |
13 | #include <string.h>
14 |
15 | #include <epan/packet.h>
16 | #include <epan/exceptions.h>
17 | #include <epan/reassemble.h>
18 | #include <epan/tvbuff-int.h>
19 |
20 | #include <wsutil/str_util.h>
21 | #include <wsutil/ws_assert.h>
22 |
23 | /*
24 | * Functions for reassembly tables where the endpoint addresses, and a
25 | * fragment ID, are used as the key.
26 | */
27 | typedef struct _fragment_addresses_key {
28 | address src;
29 | address dst;
30 | uint32_t id;
31 | } fragment_addresses_key;
32 |
33 | static GList* reassembly_table_list;
34 |
35 | static unsigned
36 | fragment_addresses_hash(const void *k)
37 | {
38 | const fragment_addresses_key* key = (const fragment_addresses_key*) k;
39 | unsigned hash_val;
40 | /*
41 | int i;
42 | */
43 |
44 | hash_val = 0;
45 |
46 | /* More than likely: in most captures src and dst addresses are the
47 | same, and would hash the same.
48 | We only use id as the hash as an optimization.
49 |
50 | for (i = 0; i < key->src.len; i++)
51 | hash_val += key->src.data[i];
52 | for (i = 0; i < key->dst.len; i++)
53 | hash_val += key->dst.data[i];
54 | */
55 |
56 | hash_val += key->id;
57 |
58 | return hash_val;
59 | }
60 |
61 | static int
62 | fragment_addresses_equal(const void *k1, const void *k2)
63 | {
64 | const fragment_addresses_key* key1 = (const fragment_addresses_key*) k1;
65 | const fragment_addresses_key* key2 = (const fragment_addresses_key*) k2;
66 |
67 | /*
68 | * key.id is the first item to compare since it's the item most
69 | * likely to differ between sessions, thus short-circuiting
70 | * the comparison of addresses.
71 | */
72 | return (key1->id == key2->id) &&
73 | (addresses_equal(&key1->src, &key2->src)) &&
74 | (addresses_equal(&key1->dst, &key2->dst));
75 | }
76 |
77 | /*
78 | * Create a fragment key for temporary use; it can point to non-
79 | * persistent data, and so must only be used to look up and
80 | * delete entries, not to add them.
81 | */
82 | static void *
83 | fragment_addresses_temporary_key(const packet_info *pinfo, const uint32_t id,
84 | const void *data _U___attribute__((unused)))
85 | {
86 | fragment_addresses_key *key = g_slice_new(fragment_addresses_key)((fragment_addresses_key*) g_slice_alloc ((sizeof (fragment_addresses_key ) > 0 ? sizeof (fragment_addresses_key) : 1)));
87 |
88 | /*
89 | * Do a shallow copy of the addresses.
90 | */
91 | copy_address_shallow(&key->src, &pinfo->src);
92 | copy_address_shallow(&key->dst, &pinfo->dst);
93 | key->id = id;
94 |
95 | return (void *)key;
96 | }
97 |
98 | /*
99 | * Create a fragment key for permanent use; it must point to persistent
100 | * data, so that it can be used to add entries.
101 | */
102 | static void *
103 | fragment_addresses_persistent_key(const packet_info *pinfo, const uint32_t id,
104 | const void *data _U___attribute__((unused)))
105 | {
106 | fragment_addresses_key *key = g_slice_new(fragment_addresses_key)((fragment_addresses_key*) g_slice_alloc ((sizeof (fragment_addresses_key ) > 0 ? sizeof (fragment_addresses_key) : 1)));
107 |
108 | /*
109 | * Do a deep copy of the addresses.
110 | */
111 | copy_address(&key->src, &pinfo->src);
112 | copy_address(&key->dst, &pinfo->dst);
113 | key->id = id;
114 |
115 | return (void *)key;
116 | }
117 |
118 | static void
119 | fragment_addresses_free_temporary_key(void *ptr)
120 | {
121 | fragment_addresses_key *key = (fragment_addresses_key *)ptr;
122 | g_slice_free(fragment_addresses_key, key)do { if (1) g_slice_free1 (sizeof (fragment_addresses_key), ( key)); else (void) ((fragment_addresses_key*) 0 == (key)); } while (0);
123 | }
124 |
125 | static void
126 | fragment_addresses_free_persistent_key(void *ptr)
127 | {
128 | fragment_addresses_key *key = (fragment_addresses_key *)ptr;
129 |
130 | if(key){
131 | /*
132 | * Free up the copies of the addresses from the old key.
133 | */
134 | free_address(&key->src);
135 | free_address(&key->dst);
136 |
137 | g_slice_free(fragment_addresses_key, key)do { if (1) g_slice_free1 (sizeof (fragment_addresses_key), ( key)); else (void) ((fragment_addresses_key*) 0 == (key)); } while (0);
138 | }
139 | }
140 |
141 | const reassembly_table_functions
142 | addresses_reassembly_table_functions = {
143 | fragment_addresses_hash,
144 | fragment_addresses_equal,
145 | fragment_addresses_temporary_key,
146 | fragment_addresses_persistent_key,
147 | fragment_addresses_free_temporary_key,
148 | fragment_addresses_free_persistent_key
149 | };
150 |
151 | /*
152 | * Functions for reassembly tables where the endpoint addresses and ports,
153 | * and a fragment ID, are used as the key.
154 | */
155 | typedef struct _fragment_addresses_ports_key {
156 | address src_addr;
157 | address dst_addr;
158 | uint32_t src_port;
159 | uint32_t dst_port;
160 | uint32_t id;
161 | } fragment_addresses_ports_key;
162 |
163 | static unsigned
164 | fragment_addresses_ports_hash(const void *k)
165 | {
166 | const fragment_addresses_ports_key* key = (const fragment_addresses_ports_key*) k;
167 | unsigned hash_val;
168 | /*
169 | int i;
170 | */
171 |
172 | hash_val = 0;
173 |
174 | /* More than likely: in most captures src and dst addresses and ports
175 | are the same, and would hash the same.
176 | We only use id as the hash as an optimization.
177 |
178 | for (i = 0; i < key->src.len; i++)
179 | hash_val += key->src_addr.data[i];
180 | for (i = 0; i < key->dst.len; i++)
181 | hash_val += key->dst_addr.data[i];
182 | hash_val += key->src_port;
183 | hash_val += key->dst_port;
184 | */
185 |
186 | hash_val += key->id;
187 |
188 | return hash_val;
189 | }
190 |
191 | static int
192 | fragment_addresses_ports_equal(const void *k1, const void *k2)
193 | {
194 | const fragment_addresses_ports_key* key1 = (const fragment_addresses_ports_key*) k1;
195 | const fragment_addresses_ports_key* key2 = (const fragment_addresses_ports_key*) k2;
196 |
197 | /*
198 | * key.id is the first item to compare since it's the item most
199 | * likely to differ between sessions, thus short-circuiting
200 | * the comparison of addresses and ports.
201 | */
202 | return (key1->id == key2->id) &&
203 | (addresses_equal(&key1->src_addr, &key2->src_addr)) &&
204 | (addresses_equal(&key1->dst_addr, &key2->dst_addr)) &&
205 | (key1->src_port == key2->src_port) &&
206 | (key1->dst_port == key2->dst_port);
207 | }
208 |
209 | /*
210 | * Create a fragment key for temporary use; it can point to non-
211 | * persistent data, and so must only be used to look up and
212 | * delete entries, not to add them.
213 | */
214 | static void *
215 | fragment_addresses_ports_temporary_key(const packet_info *pinfo, const uint32_t id,
216 | const void *data _U___attribute__((unused)))
217 | {
218 | fragment_addresses_ports_key *key = g_slice_new(fragment_addresses_ports_key)((fragment_addresses_ports_key*) g_slice_alloc ((sizeof (fragment_addresses_ports_key ) > 0 ? sizeof (fragment_addresses_ports_key) : 1)));
219 |
220 | /*
221 | * Do a shallow copy of the addresses.
222 | */
223 | copy_address_shallow(&key->src_addr, &pinfo->src);
224 | copy_address_shallow(&key->dst_addr, &pinfo->dst);
225 | key->src_port = pinfo->srcport;
226 | key->dst_port = pinfo->destport;
227 | key->id = id;
228 |
229 | return (void *)key;
230 | }
231 |
232 | /*
233 | * Create a fragment key for permanent use; it must point to persistent
234 | * data, so that it can be used to add entries.
235 | */
236 | static void *
237 | fragment_addresses_ports_persistent_key(const packet_info *pinfo,
238 | const uint32_t id, const void *data _U___attribute__((unused)))
239 | {
240 | fragment_addresses_ports_key *key = g_slice_new(fragment_addresses_ports_key)((fragment_addresses_ports_key*) g_slice_alloc ((sizeof (fragment_addresses_ports_key ) > 0 ? sizeof (fragment_addresses_ports_key) : 1)));
241 |
242 | /*
243 | * Do a deep copy of the addresses.
244 | */
245 | copy_address(&key->src_addr, &pinfo->src);
246 | copy_address(&key->dst_addr, &pinfo->dst);
247 | key->src_port = pinfo->srcport;
248 | key->dst_port = pinfo->destport;
249 | key->id = id;
250 |
251 | return (void *)key;
252 | }
253 |
254 | static void
255 | fragment_addresses_ports_free_temporary_key(void *ptr)
256 | {
257 | fragment_addresses_ports_key *key = (fragment_addresses_ports_key *)ptr;
258 | g_slice_free(fragment_addresses_ports_key, key)do { if (1) g_slice_free1 (sizeof (fragment_addresses_ports_key ), (key)); else (void) ((fragment_addresses_ports_key*) 0 == ( key)); } while (0);
259 | }
260 |
261 | static void
262 | fragment_addresses_ports_free_persistent_key(void *ptr)
263 | {
264 | fragment_addresses_ports_key *key = (fragment_addresses_ports_key *)ptr;
265 |
266 | if(key){
267 | /*
268 | * Free up the copies of the addresses from the old key.
269 | */
270 | free_address(&key->src_addr);
271 | free_address(&key->dst_addr);
272 |
273 | g_slice_free(fragment_addresses_ports_key, key)do { if (1) g_slice_free1 (sizeof (fragment_addresses_ports_key ), (key)); else (void) ((fragment_addresses_ports_key*) 0 == ( key)); } while (0);
274 | }
275 | }
276 |
277 | const reassembly_table_functions
278 | addresses_ports_reassembly_table_functions = {
279 | fragment_addresses_ports_hash,
280 | fragment_addresses_ports_equal,
281 | fragment_addresses_ports_temporary_key,
282 | fragment_addresses_ports_persistent_key,
283 | fragment_addresses_ports_free_temporary_key,
284 | fragment_addresses_ports_free_persistent_key
285 | };
286 |
287 | typedef struct _reassembled_key {
288 | uint32_t id;
289 | uint32_t frame;
290 | } reassembled_key;
291 |
292 | static int
293 | reassembled_equal(const void *k1, const void *k2)
294 | {
295 | const reassembled_key* key1 = (const reassembled_key*) k1;
296 | const reassembled_key* key2 = (const reassembled_key*) k2;
297 |
298 | /*
299 | * We assume that the frame numbers are unlikely to be equal,
300 | * so we check them first.
301 | */
302 | return key1->frame == key2->frame && key1->id == key2->id;
303 | }
304 |
305 | static unsigned
306 | reassembled_hash(const void *k)
307 | {
308 | const reassembled_key* key = (const reassembled_key*) k;
309 |
310 | return key->frame;
311 | }
312 |
313 | static void
314 | reassembled_key_free(void *ptr)
315 | {
316 | g_slice_free(reassembled_key, (reassembled_key *)ptr)do { if (1) g_slice_free1 (sizeof (reassembled_key), ((reassembled_key *)ptr)); else (void) ((reassembled_key*) 0 == ((reassembled_key *)ptr)); } while (0);
317 | }
318 |
319 | /* --------------fragment_item functions ----------- */
320 | static fragment_item*
321 | new_fragment_item(uint32_t frame, uint32_t offset, uint32_t len)
322 | {
323 | fragment_item *fd;
324 |
325 | fd = g_slice_new(fragment_item)((fragment_item*) g_slice_alloc ((sizeof (fragment_item) > 0 ? sizeof (fragment_item) : 1)));
326 | fd->next = NULL((void*)0);
327 | fd->flags = 0;
328 | fd->frame = frame;
329 | fd->offset = offset;
330 | fd->len = len;
331 | fd->tvb_data = NULL((void*)0);
332 |
333 | return fd;
334 | }
335 |
336 | static void
337 | fragment_item_free_tvb(fragment_item *fd_i)
338 | {
339 | /* If this is a subset of the tvb created for the head after
340 | * dissembly, don't free it (that would cause memory errors;
341 | * the parent will be freed later.) */
342 | if (fd_i->flags & FD_SUBSET_TVB0x0020)
343 | fd_i->flags &= ~FD_SUBSET_TVB0x0020;
344 | else if (fd_i->tvb_data)
345 | tvb_free(fd_i->tvb_data);
346 |
347 | fd_i->tvb_data=NULL((void*)0);
348 | }
349 |
350 | /* Returns the pointer to the next item so that the list can be freed. */
351 | static fragment_item*
352 | fragment_item_free(fragment_item *fd_i)
353 | {
354 | fragment_item *fd_next = fd_i->next;
355 | fragment_item_free_tvb(fd_i);
356 | g_slice_free(fragment_item, fd_i)do { if (1) g_slice_free1 (sizeof (fragment_item), (fd_i)); else (void) ((fragment_item*) 0 == (fd_i)); } while (0);
357 | return fd_next;
358 | }
359 |
360 | /* ------------------------- */
361 | static fragment_head *new_head(const uint32_t flags)
362 | {
363 | fragment_head *fd_head;
364 | /* If head/first structure in list only holds no other data than
365 | * 'datalen' then we don't have to change the head of the list
366 | * even if we want to keep it sorted
367 | */
368 | fd_head=g_slice_new0(fragment_head)((fragment_head*) g_slice_alloc0 ((sizeof (fragment_head) > 0 ? sizeof (fragment_head) : 1)));
369 |
370 | fd_head->flags=flags;
371 | return fd_head;
372 | }
373 |
374 | /*
375 | * For a reassembled-packet hash table entry, free the fragment data
376 | * to which the value refers. (The key is freed by reassembled_key_free.)
377 | */
378 | static void
379 | free_fd_head(fragment_head *fd_head)
380 | {
381 | fragment_item *fd_i;
382 |
383 | if (fd_head->flags & FD_SUBSET_TVB0x0020)
384 | fd_head->tvb_data = NULL((void*)0);
385 | if (fd_head->tvb_data)
386 | tvb_free(fd_head->tvb_data);
387 | fd_i = fd_head->next;
388 | while (fd_i != NULL((void*)0)) {
389 | fd_i = fragment_item_free(fd_i);
390 | }
391 | g_slice_free(fragment_head, fd_head)do { if (1) g_slice_free1 (sizeof (fragment_head), (fd_head)) ; else (void) ((fragment_head*) 0 == (fd_head)); } while (0);
392 | }
393 |
394 | static void
395 | unref_fd_head(void *data)
396 | {
397 | fragment_head *fd_head = (fragment_head *) data;
398 | fd_head->ref_count--;
399 |
400 | if (fd_head->ref_count == 0) {
401 | free_fd_head(fd_head);
402 | }
403 | }
404 |
405 | /*
406 | * For a fragment hash table entry, free the associated fragments.
407 | * The entry value (fd_chain) is freed herein and the entry is freed
408 | * when the key freeing routine is called (as a consequence of returning
409 | * true from this function).
410 | */
411 | static gboolean
412 | free_all_fragments(void *key_arg _U___attribute__((unused)), void *value, void *user_data _U___attribute__((unused)))
413 | {
414 | fragment_head *fd_head;
415 |
416 | /* g_hash_table_new_full() was used to supply a function
417 | * to free the key and anything to which it points
418 | */
419 | fd_head = (fragment_head *)value;
420 | free_fd_head(fd_head);
421 |
422 | return TRUE(!(0));
423 | }
424 |
425 | static void
426 | reassembled_table_insert(GHashTable *reassembled_table, reassembled_key *key, fragment_head *fd_head)
427 | {
428 | fragment_head *old_fd_head;
429 | fd_head->ref_count++;
430 | if ((old_fd_head = g_hash_table_lookup(reassembled_table, key)) != NULL((void*)0)) {
431 | if (old_fd_head->ref_count == 1) {
432 | /* We're replacing the last entry in the reassembled
433 | * table for an old reassembly. Does it have a tvb?
434 | * We might still be using that tvb's memory for an
435 | * address via set_address_tvb(). (See #19094.)
436 | */
437 | if (old_fd_head->tvb_data && fd_head->tvb_data) {
438 | /* Free it when the new tvb is freed */
439 | tvb_set_child_real_data_tvbuff(fd_head->tvb_data, old_fd_head->tvb_data);
440 | }
441 | /* XXX: Set the old data to NULL regardless. If we
442 | * have old data but not new data, that is odd (we're
443 | * replacing a reassembly with tvb data with something
444 | * with no tvb data, possibly because a zero length or
445 | * null tvb was passed into a defragment function,
446 | * which is a dissector bug.)
447 | * This leaks the tvb data if we couldn't add it to
448 | * a new tvb's chain, but we might not be able to free
449 | * it yet if set_address_tvb() was used.
450 | */
451 | old_fd_head->tvb_data = NULL((void*)0);
452 | }
453 | }
454 | g_hash_table_insert(reassembled_table, key, fd_head);
455 | }
456 |
457 | typedef struct register_reassembly_table {
458 | reassembly_table *table;
459 | const reassembly_table_functions *funcs;
460 | } register_reassembly_table_t;
461 |
462 | /*
463 | * Register a reassembly table.
464 | */
465 | void
466 | reassembly_table_register(reassembly_table *table,
467 | const reassembly_table_functions *funcs)
468 | {
469 | register_reassembly_table_t* reg_table;
470 |
471 | DISSECTOR_ASSERT(table)((void) ((table) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\"" , "epan/reassemble.c", 471, "table"))));
472 | DISSECTOR_ASSERT(funcs)((void) ((funcs) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\"" , "epan/reassemble.c", 472, "funcs"))));
473 |
474 | reg_table = g_new(register_reassembly_table_t,1)((register_reassembly_table_t *) g_malloc_n ((1), sizeof (register_reassembly_table_t )));
475 |
476 | reg_table->table = table;
477 | reg_table->funcs = funcs;
478 |
479 | reassembly_table_list = g_list_prepend(reassembly_table_list, reg_table);
480 | }
481 |
482 | /*
483 | * Initialize a reassembly table, with specified functions.
484 | */
485 | void
486 | reassembly_table_init(reassembly_table *table,
487 | const reassembly_table_functions *funcs)
488 | {
489 | if (table->temporary_key_func == NULL((void*)0))
490 | table->temporary_key_func = funcs->temporary_key_func;
491 | if (table->persistent_key_func == NULL((void*)0))
492 | table->persistent_key_func = funcs->persistent_key_func;
493 | if (table->free_temporary_key_func == NULL((void*)0))
494 | table->free_temporary_key_func = funcs->free_temporary_key_func;
495 | if (table->fragment_table != NULL((void*)0)) {
496 | /*
497 | * The fragment hash table exists.
498 | *
499 | * Remove all entries and free fragment data for each entry.
500 | *
501 | * The keys, and anything to which they point, are freed by
502 | * calling the table's key freeing function. The values
503 | * are freed in free_all_fragments().
504 | */
505 | g_hash_table_foreach_remove(table->fragment_table,
506 | free_all_fragments, NULL((void*)0));
507 | } else {
508 | /* The fragment table does not exist. Create it */
509 | table->fragment_table = g_hash_table_new_full(funcs->hash_func,
510 | funcs->equal_func, funcs->free_persistent_key_func, NULL((void*)0));
511 | }
512 |
513 | if (table->reassembled_table != NULL((void*)0)) {
514 | /*
515 | * The reassembled-packet hash table exists.
516 | *
517 | * Remove all entries and free reassembled packet
518 | * data and key for each entry.
519 | */
520 | g_hash_table_remove_all(table->reassembled_table);
521 | } else {
522 | /* The fragment table does not exist. Create it */
523 | table->reassembled_table = g_hash_table_new_full(reassembled_hash,
524 | reassembled_equal, reassembled_key_free, unref_fd_head);
525 | }
526 | }
527 |
528 | /*
529 | * Destroy a reassembly table.
530 | */
531 | void
532 | reassembly_table_destroy(reassembly_table *table)
533 | {
534 | /*
535 | * Clear the function pointers.
536 | */
537 | table->temporary_key_func = NULL((void*)0);
538 | table->persistent_key_func = NULL((void*)0);
539 | table->free_temporary_key_func = NULL((void*)0);
540 | if (table->fragment_table != NULL((void*)0)) {
541 | /*
542 | * The fragment hash table exists.
543 | *
544 | * Remove all entries and free fragment data for each entry.
545 | *
546 | * The keys, and anything to which they point, are freed by
547 | * calling the table's key freeing function. The values
548 | * are freed in free_all_fragments().
549 | */
550 | g_hash_table_foreach_remove(table->fragment_table,
551 | free_all_fragments, NULL((void*)0));
552 |
553 | /*
554 | * Now destroy the hash table.
555 | */
556 | g_hash_table_destroy(table->fragment_table);
557 | table->fragment_table = NULL((void*)0);
558 | }
559 | if (table->reassembled_table != NULL((void*)0)) {
560 | /*
561 | * The reassembled-packet hash table exists.
562 | *
563 | * Remove all entries and free reassembled packet
564 | * data and key for each entry.
565 | */
566 |
567 | g_hash_table_remove_all(table->reassembled_table);
568 |
569 | /*
570 | * Now destroy the hash table.
571 | */
572 | g_hash_table_destroy(table->reassembled_table);
573 | table->reassembled_table = NULL((void*)0);
574 | }
575 | }
576 |
577 | /*
578 | * Look up an fd_head in the fragment table, optionally returning the key
579 | * for it.
580 | */
581 | static fragment_head *
582 | lookup_fd_head(reassembly_table *table, const packet_info *pinfo,
583 | const uint32_t id, const void *data, void * *orig_keyp)
584 | {
585 | void *key;
586 | void *value;
587 |
588 | /* Create key to search hash with */
589 | key = table->temporary_key_func(pinfo, id, data);
590 |
591 | /*
592 | * Look up the reassembly in the fragment table.
593 | */
594 | if (!g_hash_table_lookup_extended(table->fragment_table, key, orig_keyp,
595 | &value))
596 | value = NULL((void*)0);
597 | /* Free the key */
598 | table->free_temporary_key_func(key);
599 |
600 | return (fragment_head *)value;
601 | }
602 |
603 | /*
604 | * Insert an fd_head into the fragment table, and return the key used.
605 | */
606 | static void *
607 | insert_fd_head(reassembly_table *table, fragment_head *fd_head,
608 | const packet_info *pinfo, const uint32_t id, const void *data)
609 | {
610 | void *key;
611 |
612 | /*
613 | * We're going to use the key to insert the fragment,
614 | * so make a persistent version of it.
615 | */
616 | key = table->persistent_key_func(pinfo, id, data);
617 | g_hash_table_insert(table->fragment_table, key, fd_head);
618 | return key;
619 | }
620 |
621 | /* This function cleans up the stored state and removes the reassembly data and
622 | * (with one exception) all allocated memory for matching reassembly.
623 | *
624 | * The exception is :
625 | * If the PDU was already completely reassembled, then the tvbuff containing the
626 | * reassembled data WILL NOT be free()d, and the pointer to that tvbuff will be
627 | * returned.
628 | * Othervise the function will return NULL.
629 | *
630 | * So, if you call fragment_delete and it returns non-NULL, YOU are responsible
631 | * to tvb_free() that tvbuff.
632 | */
633 | tvbuff_t *
634 | fragment_delete(reassembly_table *table, const packet_info *pinfo,
635 | const uint32_t id, const void *data)
636 | {
637 | fragment_head *fd_head;
638 | fragment_item *fd;
639 | tvbuff_t *fd_tvb_data=NULL((void*)0);
640 | void *key;
641 |
642 | fd_head = lookup_fd_head(table, pinfo, id, data, &key);
643 | if(fd_head==NULL((void*)0)){
644 | /* We do not recognize this as a PDU we have seen before. return */
645 | return NULL((void*)0);
646 | }
647 |
648 | fd_tvb_data=fd_head->tvb_data;
649 | /* loop over all partial fragments and free any tvbuffs */
650 | fd = fd_head->next;
651 | while (fd != NULL((void*)0)) {
652 | fd = fragment_item_free(fd);
653 | }
654 | g_slice_free(fragment_head, fd_head)do { if (1) g_slice_free1 (sizeof (fragment_head), (fd_head)) ; else (void) ((fragment_head*) 0 == (fd_head)); } while (0);
655 | g_hash_table_remove(table->fragment_table, key);
656 |
657 | return fd_tvb_data;
658 | }
659 |
660 | /* This function is used to check if there is partial or completed reassembly state
661 | * matching this packet. I.e. Is there reassembly going on or not for this packet?
662 | */
663 | fragment_head *
664 | fragment_get(reassembly_table *table, const packet_info *pinfo,
665 | const uint32_t id, const void *data)
666 | {
667 | return lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
668 | }
669 |
670 | fragment_head *
671 | fragment_get_reassembled_id(reassembly_table *table, const packet_info *pinfo,
672 | const uint32_t id)
673 | {
674 | fragment_head *fd_head;
675 | reassembled_key key;
676 |
677 | /* create key to search hash with */
678 | key.frame = pinfo->num;
679 | key.id = id;
680 | fd_head = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &key);
681 |
682 | return fd_head;
683 | }
684 |
685 | /* To specify the offset for the fragment numbering, the first fragment is added with 0, and
686 | * afterwards this offset is set. All additional calls to off_seq_check will calculate
687 | * the number in sequence in regards to the offset */
688 | void
689 | fragment_add_seq_offset(reassembly_table *table, const packet_info *pinfo, const uint32_t id,
690 | const void *data, const uint32_t fragment_offset)
691 | {
692 | fragment_head *fd_head;
693 |
694 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
695 | if (!fd_head)
696 | return;
697 |
698 | /* Resetting the offset is not allowed */
699 | if ( fd_head->fragment_nr_offset != 0 )
700 | return;
701 |
702 | fd_head->fragment_nr_offset = fragment_offset;
703 | }
704 |
705 | static void
706 | update_first_gap(fragment_head *fd_head, fragment_item *inserted, bool_Bool multi_insert)
707 | {
708 | uint32_t frag_end = inserted->offset + inserted->len;
709 | fragment_item *iter;
710 | uint32_t contiguous;
711 |
712 | if (inserted->offset > fd_head->contiguous_len) {
713 | /* first inserted node is after first gap */
714 | return;
715 | } else if (fd_head->first_gap == NULL((void*)0)) {
716 | /* we haven't seen first fragment yet */
717 | if (inserted->offset != 0) {
718 | /* inserted node is not first fragment */
719 | return;
720 | }
721 | contiguous = inserted->len;
722 | iter = inserted;
723 | } else {
724 | contiguous = MAX(fd_head->contiguous_len, frag_end)(((fd_head->contiguous_len) > (frag_end)) ? (fd_head-> contiguous_len) : (frag_end));
725 | iter = multi_insert ? inserted : fd_head->first_gap;
726 | }
727 |
728 | while (iter->next) {
729 | if (iter->next->offset > contiguous) {
730 | break;
731 | }
732 | iter = iter->next;
733 | contiguous = MAX(contiguous, iter->offset + iter->len)(((contiguous) > (iter->offset + iter->len)) ? (contiguous ) : (iter->offset + iter->len));
734 | }
735 |
736 | /* iter is either pointing to last fragment before gap or tail */
737 | fd_head->first_gap = iter;
738 | fd_head->contiguous_len = contiguous;
739 | }
740 |
741 | /*
742 | * Keeping first gap and contiguous length in sync significantly speeds up
743 | * LINK_FRAG() when fragments in capture file are mostly ordered. However, when
744 | * fragments are removed from the list, the first gap can point to fragments
745 | * that were either moved to another list or freed. Therefore when any fragment
746 | * before first gap is removed, the first gap (and contiguous length) must be
747 | * invalidated.
748 | */
749 | static void fragment_reset_first_gap(fragment_head *fd_head)
750 | {
751 | fd_head->first_gap = NULL((void*)0);
752 | fd_head->contiguous_len = 0;
753 | if (fd_head->next) {
754 | bool_Bool multi_insert = (fd_head->next->next != NULL((void*)0));
755 | update_first_gap(fd_head, fd_head->next, multi_insert);
756 | }
757 | }
758 |
759 | /*
760 | * Determines whether list modification requires first gap reset. On entry
761 | * modified is NULL if all elements were removed, otherwise it points to
762 | * element (reachable from fd_head) whose next pointer was changed.
763 | */
764 | static void fragment_items_removed(fragment_head *fd_head, fragment_item *modified)
765 | {
766 | if ((fd_head->first_gap == modified) ||
767 | ((modified != NULL((void*)0)) && (modified->offset > fd_head->contiguous_len))) {
768 | /* Removed elements were after first gap */
769 | return;
770 | }
771 | fragment_reset_first_gap(fd_head);
772 | }
773 |
774 | /*
775 | * For use with fragment_add (and not the fragment_add_seq functions).
776 | * When the reassembled result is wrong (perhaps it needs to be extended), this
777 | * function clears any previous reassembly result, allowing the new reassembled
778 | * length to be set again.
779 | */
780 | static void
781 | fragment_reset_defragmentation(fragment_head *fd_head)
782 | {
783 | /* Caller must ensure that this function is only called when
784 | * defragmentation is safe to undo. */
785 | DISSECTOR_ASSERT(fd_head->flags & FD_DEFRAGMENTED)((void) ((fd_head->flags & 0x0001) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 785, "fd_head->flags & 0x0001" ))));
786 |
787 | fd_head->flags &= ~(FD_DEFRAGMENTED0x0001|FD_PARTIAL_REASSEMBLY0x0040|FD_DATALEN_SET0x0400);
788 | /* We have to clear TOOLONGFRAGMENT and MULTIPLETAILS because they
789 | * might change when extending the reassembly. If those flags weren't
790 | * set on the head, they're not set on any item. */
791 | if (fd_head->flags & (FD_TOOLONGFRAGMENT0x0010|FD_MULTIPLETAILS0x0008)) {
792 | for (fragment_item *fd_i = fd_head->next; fd_i; fd_i = fd_i->next) {
793 | fd_i->flags &= (~FD_TOOLONGFRAGMENT0x0010) & (~FD_MULTIPLETAILS0x0008);
794 | }
795 | fd_head->flags &= ~(FD_TOOLONGFRAGMENT0x0010|FD_MULTIPLETAILS0x0008);
796 | }
797 | fd_head->datalen = 0;
798 | fd_head->reassembled_in = 0;
799 | fd_head->reas_in_layer_num = 0;
800 | }
801 |
802 | /* This function can be used to explicitly set the total length (if known)
803 | * for reassembly of a PDU.
804 | * This is useful for reassembly of PDUs where one may have the total length specified
805 | * in the first fragment instead of as for, say, IPv4 where a flag indicates which
806 | * is the last fragment.
807 | *
808 | * Such protocols might fragment_add with a more_frags==true for every fragment
809 | * and just tell the reassembly engine the expected total length of the reassembled data
810 | * using fragment_set_tot_len immediately after doing fragment_add for the first packet.
811 | *
812 | * Note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment.
813 | * i.e. since the block numbers start at 0, if we specify tot_len==2, that
814 | * actually means we want to defragment 3 blocks, block 0, 1 and 2.
815 | */
816 | void
817 | fragment_set_tot_len(reassembly_table *table, const packet_info *pinfo,
818 | const uint32_t id, const void *data, const uint32_t tot_len)
819 | {
820 | fragment_head *fd_head;
821 | fragment_item *fd;
822 | uint32_t max_offset = 0;
823 |
824 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
825 | if (!fd_head)
826 | return;
827 |
828 | /* If we're setting a block sequence number, verify that it
829 | * doesn't conflict with values set by existing fragments.
830 | * XXX - eliminate this check?
831 | */
832 | if (fd_head->flags & FD_BLOCKSEQUENCE0x0100) {
833 | for (fd = fd_head->next; fd; fd = fd->next) {
834 | if (fd->offset > max_offset) {
835 | max_offset = fd->offset;
836 | if (max_offset > tot_len) {
837 | fd_head->error = "Bad total reassembly block count";
838 | THROW_MESSAGE(ReassemblyError, fd_head->error)except_throw(1, (9), (fd_head->error));
839 | }
840 | }
841 | }
842 | }
843 |
844 | if (fd_head->flags & FD_DEFRAGMENTED0x0001) {
845 | if (max_offset != tot_len) {
846 | fd_head->error = "Defragmented complete but total length not satisfied";
847 | THROW_MESSAGE(ReassemblyError, fd_head->error)except_throw(1, (9), (fd_head->error));
848 | }
849 | }
850 |
851 | /* We got this far so the value is sane. */
852 | fd_head->datalen = tot_len;
853 | fd_head->flags |= FD_DATALEN_SET0x0400;
854 | }
855 |
856 | void
857 | fragment_reset_tot_len(reassembly_table *table, const packet_info *pinfo,
858 | const uint32_t id, const void *data, const uint32_t tot_len)
859 | {
860 | fragment_head *fd_head;
861 |
862 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
863 | if (!fd_head)
864 | return;
865 |
866 | /*
867 | * If FD_PARTIAL_REASSEMBLY is set, it would make the next fragment_add
868 | * call set the reassembled length based on the fragment offset and
869 | * length. As the length is known now, be sure to disable that magic.
870 | */
871 | fd_head->flags &= ~FD_PARTIAL_REASSEMBLY0x0040;
872 |
873 | /* If the length is already as expected, there is nothing else to do. */
874 | if (tot_len == fd_head->datalen)
875 | return;
876 |
877 | if (fd_head->flags & FD_DEFRAGMENTED0x0001) {
878 | /*
879 | * Fragments were reassembled before, clear it to allow
880 | * increasing the reassembled length.
881 | */
882 | fragment_reset_defragmentation(fd_head);
883 | }
884 |
885 | fd_head->datalen = tot_len;
886 | fd_head->flags |= FD_DATALEN_SET0x0400;
887 | }
888 |
889 | void
890 | fragment_truncate(reassembly_table *table, const packet_info *pinfo,
891 | const uint32_t id, const void *data, const uint32_t tot_len)
892 |
893 | {
894 | tvbuff_t *old_tvb_data;
895 | fragment_head *fd_head;
896 |
897 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
898 | if (!fd_head)
899 | return;
900 |
901 | /* Caller must ensure that this function is only called when
902 | * we are defragmented. */
903 | DISSECTOR_ASSERT(fd_head->flags & FD_DEFRAGMENTED)((void) ((fd_head->flags & 0x0001) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 903, "fd_head->flags & 0x0001" ))));
904 |
905 | /*
906 | * If FD_PARTIAL_REASSEMBLY is set, it would make the next fragment_add
907 | * call set the reassembled length based on the fragment offset and
908 | * length. As the length is known now, be sure to disable that magic.
909 | */
910 | fd_head->flags &= ~FD_PARTIAL_REASSEMBLY0x0040;
911 |
912 | /* If the length is already as expected, there is nothing else to do. */
913 | if (tot_len == fd_head->datalen)
914 | return;
915 |
916 | DISSECTOR_ASSERT(fd_head->datalen > tot_len)((void) ((fd_head->datalen > tot_len) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 916, "fd_head->datalen > tot_len" ))));
917 |
918 | old_tvb_data=fd_head->tvb_data;
919 | fd_head->tvb_data = tvb_clone_offset_len(old_tvb_data, 0, tot_len);
920 | tvb_set_free_cb(fd_head->tvb_data, g_free);
921 |
922 | if (old_tvb_data)
923 | tvb_add_to_chain(fd_head->tvb_data, old_tvb_data);
924 | fd_head->datalen = tot_len;
925 |
926 | /* Keep the fragments before the split point, dividing any if
927 | * necessary.
928 | * XXX: In rare cases, there might be fragments marked as overlap that
929 | * have data both before and after the split point, and which only
930 | * overlap after the split point. In that case, after dividing the
931 | * fragments the first part no longer overlap.
932 | * However, at this point we can't test for overlap conflicts,
933 | * so we'll just leave the overlap flags as-is.
934 | */
935 | fd_head->flags &= ~(FD_OVERLAP0x0002|FD_OVERLAPCONFLICT0x0004|FD_TOOLONGFRAGMENT0x0010|FD_MULTIPLETAILS0x0008);
936 | fragment_item *fd_i, *prev_fd = NULL((void*)0);
937 | for (fd_i = fd_head->next; fd_i && (fd_i->offset < tot_len); fd_i = fd_i->next) {
938 | fd_i->flags &= ~(FD_TOOLONGFRAGMENT0x0010|FD_MULTIPLETAILS0x0008);
939 | /* Check for the split point occurring in the middle of the
940 | * fragment. */
941 | if (fd_i->offset + fd_i->len > tot_len) {
942 | fd_i->len = tot_len - fd_i->offset;
943 | }
944 | fd_head->flags |= fd_i->flags & (FD_OVERLAP0x0002|FD_OVERLAPCONFLICT0x0004);
945 | prev_fd = fd_i;
946 |
947 | /* Below should do nothing since this is already defragmented */
948 | fragment_item_free_tvb(fd_i);
949 | }
950 |
951 | /* Remove all the other fragments, as they are past the split point. */
952 | if (prev_fd) {
953 | prev_fd->next = NULL((void*)0);
954 | } else {
955 | fd_head->next = NULL((void*)0);
956 | }
957 | fd_head->contiguous_len = MIN(fd_head->contiguous_len, tot_len)(((fd_head->contiguous_len) < (tot_len)) ? (fd_head-> contiguous_len) : (tot_len));
958 | fragment_items_removed(fd_head, prev_fd);
959 | while (fd_i != NULL((void*)0)) {
960 | fd_i = fragment_item_free(fd_i);
961 | }
962 | }
963 |
964 | uint32_t
965 | fragment_get_tot_len(reassembly_table *table, const packet_info *pinfo,
966 | const uint32_t id, const void *data)
967 | {
968 | fragment_head *fd_head;
969 |
970 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
971 |
972 | if(fd_head){
973 | return fd_head->datalen;
974 | }
975 |
976 | return 0;
977 | }
978 |
979 | /* This function will set the partial reassembly flag for a fh.
980 | When this function is called, the fh MUST already exist, i.e.
981 | the fh MUST be created by the initial call to fragment_add() before
982 | this function is called.
983 | Also note that this function MUST be called to indicate a fh will be
984 | extended (increase the already stored data)
985 | */
986 |
987 | void
988 | fragment_set_partial_reassembly(reassembly_table *table,
989 | const packet_info *pinfo, const uint32_t id,
990 | const void *data)
991 | {
992 | fragment_head *fd_head;
993 |
994 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
995 |
996 | /*
997 | * XXX - why not do all the stuff done early in "fragment_add_work()",
998 | * turning off FD_DEFRAGMENTED and pointing the fragments' data
999 | * pointers to the appropriate part of the already-reassembled
1000 | * data, and clearing the data length and "reassembled in" frame
1001 | * number, here? We currently have a hack in the TCP dissector
1002 | * not to set the "reassembled in" value if the "partial reassembly"
1003 | * flag is set, so that in the first pass through the packets
1004 | * we don't falsely set a packet as reassembled in that packet
1005 | * if the dissector decided that even more reassembly was needed.
1006 | */
1007 | if(fd_head){
1008 | fd_head->flags |= FD_PARTIAL_REASSEMBLY0x0040;
1009 | }
1010 | }
1011 |
1012 | /*
1013 | * This function gets rid of an entry from a fragment table, given
1014 | * a pointer to the key for that entry.
1015 | *
1016 | * The key freeing routine will be called by g_hash_table_remove().
1017 | */
1018 | static void
1019 | fragment_unhash(reassembly_table *table, void *key)
1020 | {
1021 | /*
1022 | * Remove the entry from the fragment table.
1023 | */
1024 | g_hash_table_remove(table->fragment_table, key);
1025 | }
1026 |
1027 | /*
1028 | * This function adds fragment_head structure to a reassembled-packet
1029 | * hash table, using the frame numbers of each of the frames from
1030 | * which it was reassembled as keys, and sets the "reassembled_in"
1031 | * frame number.
1032 | */
1033 | static void
1034 | fragment_reassembled(reassembly_table *table, fragment_head *fd_head,
1035 | const packet_info *pinfo, const uint32_t id)
1036 | {
1037 | reassembled_key *new_key;
1038 | fragment_item *fd;
1039 |
1040 | fd_head->ref_count = 0;
1041 | if (fd_head->next == NULL((void*)0)) {
1042 | /*
1043 | * This was not fragmented, so there's no fragment
1044 | * table; just hash it using the current frame number.
1045 | */
1046 | new_key = g_slice_new(reassembled_key)((reassembled_key*) g_slice_alloc ((sizeof (reassembled_key) > 0 ? sizeof (reassembled_key) : 1)));
1047 | new_key->frame = pinfo->num;
1048 | new_key->id = id;
1049 | reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1050 | } else {
1051 | /*
1052 | * Hash it with the frame numbers for all the frames.
1053 | */
1054 | for (fd = fd_head->next; fd != NULL((void*)0); fd = fd->next){
1055 | new_key = g_slice_new(reassembled_key)((reassembled_key*) g_slice_alloc ((sizeof (reassembled_key) > 0 ? sizeof (reassembled_key) : 1)));
1056 | new_key->frame = fd->frame;
1057 | new_key->id = id;
1058 | reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1059 | }
1060 | }
1061 | fd_head->flags |= FD_DEFRAGMENTED0x0001;
1062 | fd_head->reassembled_in = pinfo->num;
1063 | fd_head->reas_in_layer_num = pinfo->curr_layer_num;
1064 | }
1065 |
1066 | /*
1067 | * This function is a variant of the above for the single sequence
1068 | * case, using id+offset (i.e., the original sequence number) for the id
1069 | * in the key.
1070 | */
1071 | static void
1072 | fragment_reassembled_single(reassembly_table *table, fragment_head *fd_head,
1073 | const packet_info *pinfo, const uint32_t id)
1074 | {
1075 | reassembled_key *new_key;
1076 | fragment_item *fd;
1077 |
1078 | fd_head->ref_count = 0;
1079 | if (fd_head->next == NULL((void*)0)) {
1080 | /*
1081 | * This was not fragmented, so there's no fragment
1082 | * table; just hash it using the current frame number.
1083 | */
1084 | new_key = g_slice_new(reassembled_key)((reassembled_key*) g_slice_alloc ((sizeof (reassembled_key) > 0 ? sizeof (reassembled_key) : 1)));
1085 | new_key->frame = pinfo->num;
1086 | new_key->id = id;
1087 | reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1088 | } else {
1089 | /*
1090 | * Hash it with the frame numbers for all the frames.
1091 | */
1092 | for (fd = fd_head->next; fd != NULL((void*)0); fd = fd->next){
1093 | new_key = g_slice_new(reassembled_key)((reassembled_key*) g_slice_alloc ((sizeof (reassembled_key) > 0 ? sizeof (reassembled_key) : 1)));
1094 | new_key->frame = fd->frame;
1095 | new_key->id = id + fd->offset;
1096 | reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1097 | }
1098 | }
1099 | fd_head->flags |= FD_DEFRAGMENTED0x0001;
1100 | fd_head->reassembled_in = pinfo->num;
1101 | fd_head->reas_in_layer_num = pinfo->curr_layer_num;
1102 | }
1103 |
1104 | static void
1105 | LINK_FRAG(fragment_head *fd_head,fragment_item *fd)
1106 | {
1107 | fragment_item *fd_i;
1108 |
1109 | /* add fragment to list, keep list sorted */
1110 | /* It is important that new fragments are added *after* any
1111 | * fragments with the same offset (as currently done.) */
1112 | if (fd_head->next == NULL((void*)0) || fd->offset < fd_head->next->offset) {
1113 | /* New first fragment */
1114 | fd->next = fd_head->next;
1115 | fd_head->next = fd;
1116 | } else {
1117 | fd_i = fd_head->next;
1118 | if (fd_head->first_gap != NULL((void*)0)) {
1119 | if (fd->offset >= fd_head->first_gap->offset) {
1120 | /* fragment is after first gap */
1121 | fd_i = fd_head->first_gap;
1122 | }
1123 | }
1124 | for(; fd_i->next; fd_i=fd_i->next) {
1125 | if (fd->offset < fd_i->next->offset )
1126 | break;
1127 | }
1128 | fd->next = fd_i->next;
1129 | fd_i->next = fd;
1130 | }
1131 |
1132 | update_first_gap(fd_head, fd, false0);
1133 | }
1134 |
1135 | static void
1136 | MERGE_FRAG(fragment_head *fd_head, fragment_item *fd)
1137 | {
1138 | fragment_item *fd_i, *tmp, *inserted = fd;
1139 | bool_Bool multi_insert;
1140 |
1141 | if (fd == NULL((void*)0)) return;
1142 |
1143 | multi_insert = (fd->next != NULL((void*)0));
1144 |
1145 | if (fd_head->next == NULL((void*)0)) {
1146 | fd_head->next = fd;
1147 | update_first_gap(fd_head, fd, multi_insert);
1148 | return;
1149 | }
1150 |
1151 | if ((fd_head->first_gap != NULL((void*)0)) &&
1152 | (fd->offset >= fd_head->first_gap->offset)) {
1153 | /* all new fragments go after first gap */
1154 | fd_i = fd_head->first_gap;
1155 | } else {
1156 | /* at least one new fragment goes before first gap */
1157 | if (fd->offset < fd_head->next->offset) {
1158 | /* inserted fragment is new head, "swap" the lists */
1159 | tmp = fd_head->next;
1160 | fd_head->next = fd;
1161 | fd = tmp;
1162 | }
1163 | fd_i = fd_head->next;
1164 | }
1165 |
1166 | /* Traverse the list linked to fragment head ("main" list), checking if
1167 | * fd pointer ("merge" list) should go before or after fd_i->next. Swap
1168 | * fd_i->next ("main") and fd pointers ("merge") if "merge" list should
1169 | * go before iterated element (fd_i). After the swap what formerly was
1170 | * "merge" list essentially becomes part of "main" list (just detached
1171 | * element, i.e. fd, is now head of new "merge list").
1172 | */
1173 | for(; fd_i->next; fd_i=fd_i->next) {
1174 | if (fd->offset < fd_i->next->offset) {
1175 | tmp = fd_i->next;
1176 | fd_i->next = fd;
1177 | fd = tmp;
1178 | }
1179 | }
1180 | /* Reached "main" list end, attach remaining elements */
1181 | fd_i->next = fd;
1182 |
1183 | update_first_gap(fd_head, inserted, multi_insert);
1184 | }
1185 |
1186 | /*
1187 | * This function adds a new fragment to the fragment hash table.
1188 | * If this is the first fragment seen for this datagram, a new entry
1189 | * is created in the hash table, otherwise this fragment is just added
1190 | * to the linked list of fragments for this packet.
1191 | * The list of fragments for a specific datagram is kept sorted for
1192 | * easier handling.
1193 | *
1194 | * Returns a pointer to the head of the fragment data list if we have all the
1195 | * fragments, NULL otherwise.
1196 | *
1197 | * This function assumes frag_offset being a byte offset into the defragment
1198 | * packet.
1199 | *
1200 | * 01-2002
1201 | * Once the fh is defragmented (= FD_DEFRAGMENTED set), it can be
1202 | * extended using the FD_PARTIAL_REASSEMBLY flag. This flag should be set
1203 | * using fragment_set_partial_reassembly() before calling fragment_add
1204 | * with the new fragment. FD_TOOLONGFRAGMENT and FD_MULTIPLETAILS flags
1205 | * are lowered when a new extension process is started.
1206 | */
1207 | static bool_Bool
1208 | fragment_add_work(fragment_head *fd_head, tvbuff_t *tvb, const int offset,
1209 | const packet_info *pinfo, const uint32_t frag_offset,
1210 | const uint32_t frag_data_len, const bool_Bool more_frags,
1211 | const uint32_t frag_frame, const bool_Bool allow_overlaps)
1212 | {
1213 | fragment_item *fd;
1214 | fragment_item *fd_i;
1215 | uint32_t dfpos, fraglen, overlap;
1216 | tvbuff_t *old_tvb_data;
1217 | uint8_t *data;
1218 |
1219 | /* create new fd describing this fragment */
1220 | fd = new_fragment_item(frag_frame, frag_offset, frag_data_len);
1221 |
1222 | /*
1223 | * Are we adding to an already-completed reassembly?
1224 | */
1225 | if (fd_head->flags & FD_DEFRAGMENTED0x0001) {
1226 | /*
1227 | * Yes. Does this fragment go past the end of the results
1228 | * of that reassembly?
1229 | */
1230 | if (frag_offset + frag_data_len > fd_head->datalen) {
1231 | /*
1232 | * Yes. Have we been requested to continue reassembly?
1233 | */
1234 | if (fd_head->flags & FD_PARTIAL_REASSEMBLY0x0040) {
1235 | /*
1236 | * Yes. Set flag in already empty fds &
1237 | * point old fds to malloc'ed data.
1238 | */
1239 | fragment_reset_defragmentation(fd_head);
1240 | } else if (!allow_overlaps) {
1241 | /*
1242 | * No. Bail out since we have no idea what to
1243 | * do with this fragment (and if we keep going
1244 | * we'll run past the end of a buffer sooner
1245 | * or later).
1246 | */
1247 | g_slice_free(fragment_item, fd)do { if (1) g_slice_free1 (sizeof (fragment_item), (fd)); else (void) ((fragment_item*) 0 == (fd)); } while (0);
1248 |
1249 | /*
1250 | * This is an attempt to add a fragment to a
1251 | * reassembly that had already completed.
1252 | * If it had no error, we don't want to
1253 | * mark it with an error, and if it had an
1254 | * error, we don't want to overwrite it, so
1255 | * we don't set fd_head->error.
1256 | */
1257 | if (frag_offset >= fd_head->datalen) {
1258 | /*
1259 | * The fragment starts past the end
1260 | * of the reassembled data.
1261 | */
1262 | THROW_MESSAGE(ReassemblyError, "New fragment past old data limits")except_throw(1, (9), ("New fragment past old data limits"));
1263 | } else {
1264 | /*
1265 | * The fragment starts before the end
1266 | * of the reassembled data, but
1267 | * runs past the end. That could
1268 | * just be a retransmission with extra
1269 | * data, but the calling dissector
1270 | * didn't set FD_PARTIAL_REASSEMBLY
1271 | * so it won't be handled correctly.
1272 | *
1273 | * XXX: We could set FD_TOOLONGFRAGMENT
1274 | * below instead.
1275 | */
1276 | THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)")except_throw(1, (9), ("New fragment overlaps old data (retransmission?)" ));
1277 | }
1278 | }
1279 | } else {
1280 | /*
1281 | * No. That means it overlaps the completed reassembly.
1282 | * This is probably a retransmission and normal
1283 | * behavior. (If not, it's because the dissector
1284 | * doesn't handle reused sequence numbers correctly,
1285 | * e.g. #10503). Handle below.
1286 | */
1287 | }
1288 | }
1289 |
1290 | /* Do this after we may have bailed out (above) so that we don't leave
1291 | * fd_head->frame in a bad state if we do */
1292 | if (fd->frame > fd_head->frame)
1293 | fd_head->frame = fd->frame;
1294 |
1295 | if (!more_frags) {
1296 | /*
1297 | * This is the tail fragment in the sequence.
1298 | */
1299 | if (fd_head->flags & FD_DATALEN_SET0x0400) {
1300 | /* ok we have already seen other tails for this packet
1301 | * it might be a duplicate.
1302 | */
1303 | if (fd_head->datalen != (fd->offset + fd->len) ){
1304 | /* Oops, this tail indicates a different packet
1305 | * len than the previous ones. Something's wrong.
1306 | */
1307 | fd->flags |= FD_MULTIPLETAILS0x0008;
1308 | fd_head->flags |= FD_MULTIPLETAILS0x0008;
1309 | }
1310 | } else {
1311 | /* This was the first tail fragment; now we know
1312 | * what the length of the packet should be.
1313 | */
1314 | fd_head->datalen = fd->offset + fd->len;
1315 | fd_head->flags |= FD_DATALEN_SET0x0400;
1316 | }
1317 | }
1318 |
1319 |
1320 |
1321 | /* If the packet is already defragmented, this MUST be an overlap.
1322 | * The entire defragmented packet is in fd_head->data.
1323 | * Even if we have previously defragmented this packet, we still
1324 | * check it. Someone might play overlap and TTL games.
1325 | */
1326 | if (fd_head->flags & FD_DEFRAGMENTED0x0001) {
1327 | uint32_t end_offset = fd->offset + fd->len;
1328 | fd->flags |= FD_OVERLAP0x0002|FD_DEFRAGMENTED0x0001;
1329 | fd_head->flags |= FD_OVERLAP0x0002;
1330 | /* make sure it's not too long */
1331 | /* XXX: We probably don't call this, unlike the _seq()
1332 | * functions, because we throw an exception above.
1333 | */
1334 | if (end_offset > fd_head->datalen || end_offset < fd->offset || end_offset < fd->len) {
1335 | fd->flags |= FD_TOOLONGFRAGMENT0x0010;
1336 | fd_head->flags |= FD_TOOLONGFRAGMENT0x0010;
1337 | }
1338 | /* make sure it doesn't conflict with previous data */
1339 | else if ( tvb_memeql(fd_head->tvb_data, fd->offset,
1340 | tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
1341 | fd->flags |= FD_OVERLAPCONFLICT0x0004;
1342 | fd_head->flags |= FD_OVERLAPCONFLICT0x0004;
1343 | }
1344 | /* it was just an overlap, link it and return */
1345 | LINK_FRAG(fd_head,fd);
1346 | return true1;
1347 | }
1348 |
1349 |
1350 |
1351 | /* If we have reached this point, the packet is not defragmented yet.
1352 | * Save all payload in a buffer until we can defragment.
1353 | */
1354 | if (!tvb_bytes_exist(tvb, offset, fd->len)) {
1355 | g_slice_free(fragment_item, fd)do { if (1) g_slice_free1 (sizeof (fragment_item), (fd)); else (void) ((fragment_item*) 0 == (fd)); } while (0);
1356 | THROW(BoundsError)except_throw(1, (1), ((void*)0));
1357 | }
1358 | fd->tvb_data = tvb_clone_offset_len(tvb, offset, fd->len);
1359 | LINK_FRAG(fd_head,fd);
1360 |
1361 |
1362 | if( !(fd_head->flags & FD_DATALEN_SET0x0400) ){
1363 | /* if we don't know the datalen, there are still missing
1364 | * packets. Cheaper than the check below.
1365 | */
1366 | return false0;
1367 | }
1368 |
1369 | /* Check if we have received the entire fragment. */
1370 | if (fd_head->contiguous_len < fd_head->datalen) {
1371 | /*
1372 | * The amount of contiguous data we have is less than the
1373 | * amount of data we're trying to reassemble, so we haven't
1374 | * received all packets yet.
1375 | */
1376 | return false0;
1377 | }
1378 |
1379 | /* we have received an entire packet, defragment it and
1380 | * free all fragments
1381 | */
1382 | /* store old data just in case */
1383 | old_tvb_data=fd_head->tvb_data;
1384 | data = (uint8_t *) g_malloc(fd_head->datalen);
1385 | fd_head->tvb_data = tvb_new_real_data(data, fd_head->datalen, fd_head->datalen);
1386 | tvb_set_free_cb(fd_head->tvb_data, g_free);
1387 |
1388 | dfpos = old_tvb_data ? tvb_captured_length(old_tvb_data) : 0;
1389 | if (dfpos) {
1390 | memcpy(data, tvb_get_ptr(old_tvb_data, 0, dfpos), MIN(fd_head->datalen, dfpos)(((fd_head->datalen) < (dfpos)) ? (fd_head->datalen) : (dfpos)));
1391 | }
1392 | /* add all data fragments that have not already been added, i.e.,
1393 | * if the defragmentation was reset after partial reassembly,
1394 | * but we have to check the previously added ones as well for
1395 | * TOOLONGFRAGMENT as the datalen has changed. */
1396 | for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1397 | if (fd_i->len) {
1398 | /*
1399 | * The contiguous length check above also
1400 | * ensures that the only gaps that exist here
1401 | * are ones where a fragment starts past the
1402 | * end of the reassembled datagram, and there's
1403 | * a gap between the previous fragment and
1404 | * that fragment.
1405 | *
1406 | * A "DESEGMENT_UNTIL_FIN" was involved wherein the
1407 | * FIN packet had an offset less than the highest
1408 | * fragment offset seen. [Seen from a fuzz-test:
1409 | * bug #2470]).
1410 | *
1411 | * Note that the "overlap" compare must only be
1412 | * done for fragments with (offset+len) <= fd_head->datalen
1413 | * and thus within the newly g_malloc'd buffer.
1414 | */
1415 |
1416 | if (fd_i->offset >= fd_head->datalen) {
1417 | /*
1418 | * Fragment starts after the end
1419 | * of the reassembled packet.
1420 | *
1421 | * This can happen if the length was
1422 | * set after the offending fragment
1423 | * was added to the reassembly.
1424 | *
1425 | * Flag this fragment, but don't
1426 | * try to extract any data from
1427 | * it, as there's no place to put
1428 | * it.
1429 | *
1430 | * XXX - add different flag value
1431 | * for this.
1432 | */
1433 | fd_i->flags |= FD_TOOLONGFRAGMENT0x0010;
1434 | fd_head->flags |= FD_TOOLONGFRAGMENT0x0010;
1435 | } else if (fd_i->offset + fd_i->len < fd_i->offset) {
1436 | /* Integer overflow, unhandled by rest of
1437 | * code so error out. This check handles
1438 | * all possible remaining overflows.
1439 | */
1440 | fd_head->error = "offset + len < offset";
1441 | } else {
1442 | fraglen = fd_i->len;
1443 | if (fd_i->offset + fraglen > fd_head->datalen) {
1444 | /*
1445 | * Fragment goes past the end
1446 | * of the packet, as indicated
1447 | * by the last fragment.
1448 | *
1449 | * This can happen if the
1450 | * length was set after the
1451 | * offending fragment was
1452 | * added to the reassembly.
1453 | *
1454 | * Mark it as such, and only
1455 | * copy from it what fits in
1456 | * the packet.
1457 | */
1458 | fd_i->flags |= FD_TOOLONGFRAGMENT0x0010;
1459 | fd_head->flags |= FD_TOOLONGFRAGMENT0x0010;
1460 | fraglen = fd_head->datalen - fd_i->offset;
1461 | }
1462 | if (fd_i->flags & FD_DEFRAGMENTED0x0001) {
1463 | /* If we already added the item the
1464 | * previous time, we're done. */
1465 | continue;
1466 | }
1467 | if (!fd_i->tvb_data) {
1468 | /* We check this here because
1469 | * previously added items now
1470 | * have no data (not an error). */
1471 | fd_head->error = "no data";
1472 | continue;
1473 | }
1474 | overlap = 0;
1475 | if (fd_i->offset < dfpos) {
1476 | /* The new item's begins before the existing end. */
1477 | /* duplicate/retransmission/overlap */
1478 | fd_i->flags |= FD_OVERLAP0x0002;
1479 | fd_head->flags |= FD_OVERLAP0x0002;
1480 |
1481 | /* How much overlap is there with data in the buffer?
1482 | * (It's possible that multiple fragments conflict in
1483 | * data past the given datalen; we don't check that.) */
1484 | overlap = MIN(dfpos, fd_head->datalen)(((dfpos) < (fd_head->datalen)) ? (dfpos) : (fd_head-> datalen)) - fd_i->offset;
1485 | uint32_t cmp_len = MIN(fd_i->len,overlap)(((fd_i->len) < (overlap)) ? (fd_i->len) : (overlap) );
1486 |
1487 | if ( cmp_len && memcmp(data + fd_i->offset,
1488 | tvb_get_ptr(fd_i->tvb_data, 0, cmp_len),
1489 | cmp_len)
1490 | ) {
1491 | fd_i->flags |= FD_OVERLAPCONFLICT0x0004;
1492 | fd_head->flags |= FD_OVERLAPCONFLICT0x0004;
1493 | }
1494 | }
1495 | /* XXX: As in the fragment_add_seq funcs
1496 | * like fragment_defragment_and_free() the
1497 | * existing behavior does not overwrite
1498 | * overlapping bytes even if there is a
1499 | * conflict. It only adds new bytes.
1500 | *
1501 | * Since we only add fragments to a reassembly
1502 | * if the reassembly isn't complete, the most
1503 | * common case for overlap conflicts is when
1504 | * an earlier reassembly isn't fully contained
1505 | * in the capture, and we've reused an
1506 | * identification number / wrapped around
1507 | * offset sequence numbers much later in the
1508 | * capture. In that case, we probably *do*
1509 | * want to overwrite conflicting bytes, since
1510 | * the earlier fragments didn't form a complete
1511 | * reassembly and should be effectively thrown
1512 | * out rather than mixed with the new ones?
1513 | */
1514 | if (fd_i->offset + fraglen > dfpos) {
1515 | memcpy(data+dfpos,
1516 | tvb_get_ptr(fd_i->tvb_data, overlap, fraglen-overlap),
1517 | fraglen-overlap);
1518 | dfpos = fd_i->offset + fraglen;
1519 | }
1520 | }
1521 | /* Mark that this fragment as used and clear data. */
1522 | fd_i->flags |= FD_DEFRAGMENTED0x0001;
1523 | fragment_item_free_tvb(fd_i);
1524 | }
1525 | }
1526 |
1527 | if (old_tvb_data)
1528 | tvb_add_to_chain(tvb, old_tvb_data);
1529 | /* mark this packet as defragmented.
1530 | allows us to skip any trailing fragments */
1531 | fd_head->flags |= FD_DEFRAGMENTED0x0001;
1532 | fd_head->reassembled_in=pinfo->num;
1533 | fd_head->reas_in_layer_num = pinfo->curr_layer_num;
1534 |
1535 | /* we don't throw until here to avoid leaking old_data and others */
1536 | if (fd_head->error) {
1537 | THROW_MESSAGE(ReassemblyError, fd_head->error)except_throw(1, (9), (fd_head->error));
1538 | }
1539 |
1540 | return true1;
1541 | }
1542 |
1543 | static fragment_head *
1544 | fragment_add_common(reassembly_table *table, tvbuff_t *tvb, const int offset,
1545 | const packet_info *pinfo, const uint32_t id,
1546 | const void *data, const uint32_t frag_offset,
1547 | const uint32_t frag_data_len, const bool_Bool more_frags,
1548 | const bool_Bool check_already_added,
1549 | const uint32_t frag_frame)
1550 | {
1551 | fragment_head *fd_head;
1552 | fragment_item *fd_item;
1553 | bool_Bool already_added;
1554 |
1555 |
1556 | /*
1557 | * Dissector shouldn't give us garbage tvb info.
1558 | *
1559 | * XXX - should this code take responsibility for preventing
1560 | * reassembly if data is missing due to the packets being
1561 | * sliced, rather than leaving it up to dissectors?
1562 | */
1563 | DISSECTOR_ASSERT(tvb_bytes_exist(tvb, offset, frag_data_len))((void) ((tvb_bytes_exist(tvb, offset, frag_data_len)) ? (void )0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\"" , "epan/reassemble.c", 1563, "tvb_bytes_exist(tvb, offset, frag_data_len)" ))));
1564 |
1565 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
1566 |
1567 | #if 0
1568 | /* debug output of associated fragments. */
1569 | /* leave it here for future debugging sessions */
1570 | if(strcmp(pinfo->current_proto, "DCERPC") == 0) {
1571 | printf("proto:%s num:%u id:%u offset:%u len:%u more:%u visited:%u\n",
1572 | pinfo->current_proto, pinfo->num, id, frag_offset, frag_data_len, more_frags, pinfo->fd->visited);
1573 | if(fd_head != NULL((void*)0)) {
1574 | for(fd_item=fd_head->next;fd_item;fd_item=fd_item->next){
1575 | printf("fd_frame:%u fd_offset:%u len:%u datalen:%u\n",
1576 | fd_item->frame, fd_item->offset, fd_item->len, fd_item->datalen);
1577 | }
1578 | }
1579 | }
1580 | #endif
1581 |
1582 | /*
1583 | * Is this the first pass through the capture?
1584 | */
1585 | if (!pinfo->fd->visited) {
1586 | /*
1587 | * Yes, so we could be doing reassembly. If
1588 | * "check_already_added" is true, and fd_head is non-null,
1589 | * meaning that this fragment would be added to an
1590 | * in-progress reassembly, check if we have seen this
1591 | * fragment before, i.e., if we have already added it to
1592 | * that reassembly. That can be true even on the first pass
1593 | * since we sometimes might call a subdissector multiple
1594 | * times.
1595 | *
1596 | * We check both the frame number and the fragment offset,
1597 | * so that we support multiple fragments from the same
1598 | * frame being added to the same reassembled PDU.
1599 | */
1600 | if (check_already_added && fd_head != NULL((void*)0)) {
1601 | /*
1602 | * fd_head->frame is the maximum of the frame
1603 | * numbers of all the fragments added to this
1604 | * reassembly; if this frame is later than that
1605 | * frame, we know it hasn't been added yet.
1606 | */
1607 | if (frag_frame <= fd_head->frame) {
1608 | already_added = false0;
1609 | /*
1610 | * The first item in the reassembly list
1611 | * is not a fragment, it's a data structure
1612 | * for the reassembled packet, so we
1613 | * start checking with the next item.
1614 | */
1615 | for (fd_item = fd_head->next; fd_item;
1616 | fd_item = fd_item->next) {
1617 | if (frag_frame == fd_item->frame &&
1618 | frag_offset == fd_item->offset) {
1619 | already_added = true1;
1620 | break;
1621 | }
1622 | }
1623 | if (already_added) {
1624 | /*
1625 | * Have we already finished
1626 | * reassembling?
1627 | */
1628 | if (fd_head->flags & FD_DEFRAGMENTED0x0001) {
1629 | /*
1630 | * Yes.
1631 | * XXX - can this ever happen?
1632 | */
1633 | THROW_MESSAGE(ReassemblyError,except_throw(1, (9), ("Frame already added in first pass"))
1634 | "Frame already added in first pass")except_throw(1, (9), ("Frame already added in first pass"));
1635 | } else {
1636 | /*
1637 | * No.
1638 | */
1639 | return NULL((void*)0);
1640 | }
1641 | }
1642 | }
1643 | }
1644 | } else {
1645 | /*
1646 | * No, so we've already done all the reassembly and added
1647 | * all the fragments. Do we have a reassembly and, if so,
1648 | * have we finished reassembling?
1649 | */
1650 | if (fd_head != NULL((void*)0) && fd_head->flags & FD_DEFRAGMENTED0x0001) {
1651 | /*
1652 | * Yes. This is probably being done after the
1653 | * first pass, and we've already done the work
1654 | * on the first pass.
1655 | *
1656 | * If the reassembly got a fatal error, throw that
1657 | * error again.
1658 | */
1659 | if (fd_head->error)
1660 | THROW_MESSAGE(ReassemblyError, fd_head->error)except_throw(1, (9), (fd_head->error));
1661 |
1662 | /*
1663 | * Is it later in the capture than all of the
1664 | * fragments in the reassembly?
1665 | */
1666 | if (frag_frame > fd_head->frame) {
1667 | /*
1668 | * Yes, so report this as a problem,
1669 | * possibly a retransmission.
1670 | */
1671 | THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)")except_throw(1, (9), ("New fragment overlaps old data (retransmission?)" ));
1672 | }
1673 |
1674 | /*
1675 | * Does this fragment go past the end of the
1676 | * results of that reassembly?
1677 | */
1678 | if (frag_offset + frag_data_len > fd_head->datalen) {
1679 | /*
1680 | * Yes.
1681 | */
1682 | if (frag_offset >= fd_head->datalen) {
1683 | /*
1684 | * The fragment starts past the
1685 | * end of the reassembled data.
1686 | */
1687 | THROW_MESSAGE(ReassemblyError, "New fragment past old data limits")except_throw(1, (9), ("New fragment past old data limits"));
1688 | } else {
1689 | /*
1690 | * The fragment starts before the end
1691 | * of the reassembled data, but
1692 | * runs past the end. That could
1693 | * just be a retransmission.
1694 | */
1695 | THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)")except_throw(1, (9), ("New fragment overlaps old data (retransmission?)" ));
1696 | }
1697 | }
1698 |
1699 | return fd_head;
1700 | } else {
1701 | /*
1702 | * No.
1703 | */
1704 | return NULL((void*)0);
1705 | }
1706 | }
1707 |
1708 | if (fd_head==NULL((void*)0)){
1709 | /* not found, this must be the first snooped fragment for this
1710 | * packet. Create list-head.
1711 | */
1712 | fd_head = new_head(0);
1713 |
1714 | /*
1715 | * Insert it into the hash table.
1716 | */
1717 | insert_fd_head(table, fd_head, pinfo, id, data);
1718 | }
1719 |
1720 | if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
1721 | frag_data_len, more_frags, frag_frame, false0)) {
1722 | /*
1723 | * Reassembly is complete.
1724 | */
1725 | return fd_head;
1726 | } else {
1727 | /*
1728 | * Reassembly isn't complete.
1729 | */
1730 | return NULL((void*)0);
1731 | }
1732 | }
1733 |
1734 | fragment_head *
1735 | fragment_add(reassembly_table *table, tvbuff_t *tvb, const int offset,
1736 | const packet_info *pinfo, const uint32_t id, const void *data,
1737 | const uint32_t frag_offset, const uint32_t frag_data_len,
1738 | const bool_Bool more_frags)
1739 | {
1740 | return fragment_add_common(table, tvb, offset, pinfo, id, data,
1741 | frag_offset, frag_data_len, more_frags, true1, pinfo->num);
1742 | }
1743 |
1744 | /*
1745 | * For use when you can have multiple fragments in the same frame added
1746 | * to the same reassembled PDU, e.g. with ONC RPC-over-TCP.
1747 | */
1748 | fragment_head *
1749 | fragment_add_multiple_ok(reassembly_table *table, tvbuff_t *tvb,
1750 | const int offset, const packet_info *pinfo,
1751 | const uint32_t id, const void *data,
1752 | const uint32_t frag_offset,
1753 | const uint32_t frag_data_len, const bool_Bool more_frags)
1754 | {
1755 | return fragment_add_common(table, tvb, offset, pinfo, id, data,
1756 | frag_offset, frag_data_len, more_frags, false0, pinfo->num);
1757 | }
1758 |
1759 | /*
1760 | * For use in protocols like TCP when you are adding an out of order segment
1761 | * that arrived in an earlier frame because the correct fragment id could not
1762 | * be determined until later. By allowing fd->frame to be different than
1763 | * pinfo->num, show_fragment_tree will display the correct fragment numbers.
1764 | *
1765 | * Note that pinfo is still used to set reassembled_in if we have all the
1766 | * fragments, so that results on subsequent passes can be the same as the
1767 | * first pass.
1768 | */
1769 | fragment_head *
1770 | fragment_add_out_of_order(reassembly_table *table, tvbuff_t *tvb,
1771 | const int offset, const packet_info *pinfo,
1772 | const uint32_t id, const void *data,
1773 | const uint32_t frag_offset,
1774 | const uint32_t frag_data_len,
1775 | const bool_Bool more_frags, const uint32_t frag_frame)
1776 | {
1777 | return fragment_add_common(table, tvb, offset, pinfo, id, data,
1778 | frag_offset, frag_data_len, more_frags, true1, frag_frame);
1779 | }
1780 |
1781 |
1782 | static fragment_head *
1783 | fragment_add_check_common(reassembly_table *table, tvbuff_t *tvb, const int offset,
1784 | const packet_info *pinfo, const uint32_t id,
1785 | const void *data, uint32_t frag_offset,
1786 | const uint32_t frag_data_len, const bool_Bool more_frags,
1787 | const uint32_t flags, const uint32_t fallback_frame)
1788 | {
1789 | reassembled_key reass_key;
1790 | fragment_head *fd_head;
1791 | void *orig_key;
1792 | bool_Bool late_retransmission = false0;
1793 |
1794 | /*
1795 | * If this isn't the first pass, look for this frame in the table
1796 | * of reassembled packets.
1797 | */
1798 | if (pinfo->fd->visited) {
1799 | reass_key.frame = pinfo->num;
1800 | reass_key.id = id;
1801 | return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
1802 | }
1803 |
1804 | /* Looks up a key in the GHashTable, returning the original key and the associated value
1805 | * and a bool which is true if the key was found. This is useful if you need to free
1806 | * the memory allocated for the original key, for example before calling g_hash_table_remove()
1807 | */
1808 | fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
1809 | if ((fd_head == NULL((void*)0)) && (fallback_frame != pinfo->num)) {
1810 | /* Check if there is completed reassembly reachable from fallback frame */
1811 | reass_key.frame = fallback_frame;
1812 | reass_key.id = id;
1813 | fd_head = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
1814 | if (fd_head != NULL((void*)0)) {
1815 | /* Found completely reassembled packet, hash it with current frame number */
1816 | reassembled_key *new_key = g_slice_new(reassembled_key)((reassembled_key*) g_slice_alloc ((sizeof (reassembled_key) > 0 ? sizeof (reassembled_key) : 1)));
1817 | new_key->frame = pinfo->num;
1818 | new_key->id = id;
1819 | reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1820 | late_retransmission = true1;
1821 | }
1822 | }
1823 | if (fd_head == NULL((void*)0)) {
1824 | /* not found, this must be the first snooped fragment for this
1825 | * packet. Create list-head.
1826 | */
1827 | fd_head = new_head(0);
1828 |
1829 | if((flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER0x0001)
1830 | && !more_frags) {
1831 | /*
1832 | * This is the last fragment for this packet, and
1833 | * is the only one we've seen.
1834 | *
1835 | * We assume this is the first and only fragment for
1836 | * this packet; just add the head of the list to
1837 | * the table of reassembled packets.
1838 | */
1839 | /* To save memory, we don't actually copy the
1840 | * fragment from the tvbuff to the fragment, and in
1841 | * process_reassembled_data just return back a subset
1842 | * of the original tvbuff (which must be passed in).
1843 | */
1844 | fd_head->datalen = frag_data_len;
1845 | fd_head->reassembled_in=pinfo->num;
1846 | fd_head->reas_in_layer_num = pinfo->curr_layer_num;
1847 | /*
1848 | * Add this item to the table of reassembled packets.
1849 | */
1850 | fragment_reassembled(table, fd_head, pinfo, id);
1851 | return fd_head;
1852 | }
1853 | /*
1854 | * Save the key, for unhashing it later.
1855 | */
1856 | orig_key = insert_fd_head(table, fd_head, pinfo, id, data);
1857 |
1858 | if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER0x0001)
1859 | frag_offset = 0;
1860 | } else {
1861 | if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER0x0001) {
1862 | /* There are no gaps by construction, so we can
1863 | * simply use the contiguous length. */
1864 | frag_offset = fd_head->contiguous_len;
1865 | }
1866 | }
1867 |
1868 | /*
1869 | * If this is a short frame, then we can't, and don't, do
1870 | * reassembly on it. We just give up.
1871 | */
1872 | if (!tvb_bytes_exist(tvb, offset, frag_data_len)) {
1873 | return NULL((void*)0);
1874 | }
1875 |
1876 | if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
1877 | frag_data_len, more_frags, pinfo->num, late_retransmission)) {
1878 | /* Nothing left to do if it was a late retransmission */
1879 | if (late_retransmission) {
1880 | return fd_head;
1881 | }
1882 | /*
1883 | * Reassembly is complete.
1884 | * Remove this from the table of in-progress
1885 | * reassemblies, add it to the table of
1886 | * reassembled packets, and return it.
1887 | */
1888 |
1889 | /*
1890 | * Remove this from the table of in-progress reassemblies,
1891 | * and free up any memory used for it in that table.
1892 | */
1893 | fragment_unhash(table, orig_key);
1894 |
1895 | /*
1896 | * Add this item to the table of reassembled packets.
1897 | */
1898 | fragment_reassembled(table, fd_head, pinfo, id);
1899 | return fd_head;
1900 | } else {
1901 | /*
1902 | * Reassembly isn't complete.
1903 | */
1904 | return NULL((void*)0);
1905 | }
1906 | }
1907 |
1908 | fragment_head *
1909 | fragment_add_check_with_fallback(reassembly_table *table, tvbuff_t *tvb, const int offset,
1910 | const packet_info *pinfo, const uint32_t id,
1911 | const void *data, const uint32_t frag_offset,
1912 | const uint32_t frag_data_len, const bool_Bool more_frags,
1913 | const uint32_t fallback_frame)
1914 | {
1915 | return fragment_add_check_common(table, tvb, offset, pinfo, id, data,
1916 | frag_offset, frag_data_len, more_frags, 0, fallback_frame);
1917 | }
1918 |
1919 | fragment_head *
1920 | fragment_add_check(reassembly_table *table, tvbuff_t *tvb, const int offset,
1921 | const packet_info *pinfo, const uint32_t id,
1922 | const void *data, const uint32_t frag_offset,
1923 | const uint32_t frag_data_len, const bool_Bool more_frags)
1924 | {
1925 | return fragment_add_check_common(table, tvb, offset, pinfo, id, data,
1926 | frag_offset, frag_data_len, more_frags, 0, pinfo->num);
1927 | }
1928 |
1929 | fragment_head *
1930 | fragment_add_check_next(reassembly_table *table, tvbuff_t *tvb, const int offset,
1931 | const packet_info *pinfo, const uint32_t id,
1932 | const void *data,
1933 | const uint32_t frag_data_len, const bool_Bool more_frags)
1934 | {
1935 | return fragment_add_check_common(table, tvb, offset, pinfo, id, data,
1936 | 0, frag_data_len, more_frags, REASSEMBLE_FLAGS_NO_FRAG_NUMBER0x0001, pinfo->num);
1937 | }
1938 |
1939 | static void
1940 | fragment_defragment_and_free (fragment_head *fd_head, const packet_info *pinfo)
1941 | {
1942 | fragment_item *fd_i = NULL((void*)0);
1943 | fragment_item *last_fd = NULL((void*)0);
1944 | uint32_t dfpos = 0, old_dfpos = 0, size = 0;
1945 | tvbuff_t *old_tvb_data = NULL((void*)0);
1946 | uint8_t *data;
1947 |
1948 | for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
7 | ← | Loop condition is false. Execution continues on line 1961 | →
1949 | if(!last_fd || last_fd->offset!=fd_i->offset){
1950 | if (ckd_add(&size, size, fd_i->len)__builtin_add_overflow((size), (fd_i->len), (&size)) || size > INT32_MAX(2147483647)) {
1951 | /* Change this maximum size when unsigned lengths
1952 | * are fully supported (#20103). */
1953 | size = INT32_MAX(2147483647);
1954 | break;
1955 | }
1956 | }
1957 | last_fd=fd_i;
1958 | }
1959 |
1960 | /* store old data in case the fd_i->data pointers refer to it */
1961 | old_tvb_data=fd_head->tvb_data;
1962 | data = (uint8_t *) g_malloc(size);
8 | ← | Memory is allocated | →
1963 | fd_head->tvb_data = tvb_new_real_data(data, size, size);
1964 | tvb_set_free_cb(fd_head->tvb_data, g_free);
1965 | fd_head->len = size; /* record size for caller */
1966 |
1967 | if (old_tvb_data) {
9 | ← | Assuming 'old_tvb_data' is null | →
10 | ← | Taking false branch | →
1968 | dfpos = tvb_captured_length(old_tvb_data);
1969 | memcpy(data, tvb_get_ptr(old_tvb_data, 0, dfpos), MIN(size, dfpos)(((size) < (dfpos)) ? (size) : (dfpos)));
1970 | }
1971 |
1972 | /* add all data fragments */
1973 | last_fd=NULL((void*)0);
1974 | dfpos = 0;
1975 | for (fd_i=fd_head->next; fd_i; fd_i=fd_i->next) {
11 | ← | Loop condition is false. Execution jumps to the end of the function | →
1976 | if (fd_i->len) {
1977 | if (dfpos >= size) {
1978 | fd_i->flags |= FD_TOOLONGFRAGMENT0x0010; // FD_OVERFLOW?
1979 | fd_head->flags |= FD_TOOLONGFRAGMENT0x0010; // FD_OVERFLOW?
1980 | } else if (!last_fd || last_fd->offset != fd_i->offset) {
1981 | /* First fragment or in-sequence fragment */
1982 | /* We need the position for overlap calculation of new fragments */
1983 | uint32_t copy_len = fd_i->len;
1984 | old_dfpos = dfpos;
1985 | if (ckd_add(&dfpos, dfpos, fd_i->len)__builtin_add_overflow((dfpos), (fd_i->len), (&dfpos)) || dfpos > size) {
1986 | copy_len = size - old_dfpos;
1987 | dfpos = size;
1988 | fd_i->flags |= FD_TOOLONGFRAGMENT0x0010; // FD_OVERFLOW?
1989 | fd_head->flags |= FD_TOOLONGFRAGMENT0x0010; // FD_OVERFLOW?
1990 | }
1991 | if (!(fd_i->flags & FD_DEFRAGMENTED0x0001)) {
1992 | /* Copy if not already copied on the first pass */
1993 | memcpy(data + old_dfpos, tvb_get_ptr(fd_i->tvb_data, 0, fd_i->len), copy_len);
1994 | }
1995 | } else if (!(fd_i->flags & FD_DEFRAGMENTED0x0001)){
1996 | /* duplicate/retransmission/overlap */
1997 | /* Note that overlaps of old fragments were already calculated. */
1998 | fd_i->flags |= FD_OVERLAP0x0002;
1999 | fd_head->flags |= FD_OVERLAP0x0002;
2000 | if((old_dfpos + fd_i->len != dfpos)
2001 | || tvb_memeql(fd_i->tvb_data, 0, data+old_dfpos, fd_i->len) ) {
2002 | fd_i->flags |= FD_OVERLAPCONFLICT0x0004;
2003 | fd_head->flags |= FD_OVERLAPCONFLICT0x0004;
2004 | }
2005 | }
2006 | fragment_item_free_tvb(fd_i);
2007 | fd_i->flags |= FD_DEFRAGMENTED0x0001;
2008 | }
2009 | last_fd=fd_i;
2010 | }
2011 |
2012 | if (old_tvb_data)
12 | ← | Potential leak of memory pointed to by 'data'
2013 | tvb_free(old_tvb_data);
2014 |
2015 | /* mark this packet as defragmented.
2016 | * allows us to skip any trailing fragments.
2017 | */
2018 | fd_head->flags |= FD_DEFRAGMENTED0x0001;
2019 | fd_head->reassembled_in=pinfo->num;
2020 | fd_head->reas_in_layer_num = pinfo->curr_layer_num;
2021 | }
2022 |
2023 | /*
2024 | * This function adds a new fragment to the entry for a reassembly
2025 | * operation.
2026 | *
2027 | * The list of fragments for a specific datagram is kept sorted for
2028 | * easier handling.
2029 | *
2030 | * Returns true if we have all the fragments, false otherwise.
2031 | *
2032 | * This function assumes frag_number being a block sequence number.
2033 | * The bsn for the first block is 0.
2034 | */
2035 | static bool_Bool
2036 | fragment_add_seq_work(fragment_head *fd_head, tvbuff_t *tvb, const int offset,
2037 | const packet_info *pinfo, const uint32_t frag_number,
2038 | const uint32_t frag_data_len, const bool_Bool more_frags)
2039 | {
2040 | fragment_item *fd;
2041 | fragment_item *fd_i;
2042 | fragment_item *last_fd;
2043 | uint32_t max, dfpos;
2044 | uint32_t frag_number_work;
2045 |
2046 | /* Enables the use of fragment sequence numbers, which do not start with 0 */
2047 | frag_number_work = frag_number;
2048 | if ( fd_head->fragment_nr_offset != 0 )
2049 | if ( frag_number_work >= fd_head->fragment_nr_offset )
2050 | frag_number_work = frag_number - fd_head->fragment_nr_offset;
2051 |
2052 | /* if the partial reassembly flag has been set, and we are extending
2053 | * the pdu, un-reassemble the pdu. This means pointing old fds to malloc'ed data.
2054 | */
2055 | if(fd_head->flags & FD_DEFRAGMENTED0x0001 && frag_number_work >= fd_head->datalen &&
2056 | fd_head->flags & FD_PARTIAL_REASSEMBLY0x0040){
2057 |
2058 | fragment_reset_defragmentation(fd_head);
2059 | }
2060 |
2061 |
2062 | /* create new fd describing this fragment */
2063 | fd = new_fragment_item(pinfo->num, frag_number_work, frag_data_len);
2064 |
2065 | /* fd_head->frame is the maximum of the frame numbers of all the
2066 | * fragments added to the reassembly. */
2067 | if (fd->frame > fd_head->frame)
2068 | fd_head->frame = fd->frame;
2069 |
2070 | if (!more_frags) {
2071 | /*
2072 | * This is the tail fragment in the sequence.
2073 | */
2074 | if (fd_head->flags&FD_DATALEN_SET0x0400) {
2075 | /* ok we have already seen other tails for this packet
2076 | * it might be a duplicate.
2077 | */
2078 | if (fd_head->datalen != fd->offset ){
2079 | /* Oops, this tail indicates a different packet
2080 | * len than the previous ones. Something's wrong.
2081 | */
2082 | fd->flags |= FD_MULTIPLETAILS0x0008;
2083 | fd_head->flags |= FD_MULTIPLETAILS0x0008;
2084 | }
2085 | } else {
2086 | /* this was the first tail fragment, now we know the
2087 | * sequence number of that fragment (which is NOT
2088 | * the length of the packet!)
2089 | */
2090 | fd_head->datalen = fd->offset;
2091 | fd_head->flags |= FD_DATALEN_SET0x0400;
2092 | }
2093 | }
2094 |
2095 | /* If the packet is already defragmented, this MUST be an overlap.
2096 | * The entire defragmented packet is in fd_head->data
2097 | * Even if we have previously defragmented this packet, we still check
2098 | * check it. Someone might play overlap and TTL games.
2099 | */
2100 | if (fd_head->flags & FD_DEFRAGMENTED0x0001) {
2101 | fd->flags |= FD_OVERLAP0x0002|FD_DEFRAGMENTED0x0001;
2102 | fd_head->flags |= FD_OVERLAP0x0002;
2103 |
2104 | /* make sure it's not past the end */
2105 | if (fd->offset > fd_head->datalen) {
2106 | /* new fragment comes after the end */
2107 | fd->flags |= FD_TOOLONGFRAGMENT0x0010;
2108 | fd_head->flags |= FD_TOOLONGFRAGMENT0x0010;
2109 | LINK_FRAG(fd_head,fd);
2110 | return true1;
2111 | }
2112 | /* make sure it doesn't conflict with previous data */
2113 | dfpos=0;
2114 | last_fd=NULL((void*)0);
2115 | for (fd_i=fd_head->next;fd_i && (fd_i->offset!=fd->offset);fd_i=fd_i->next) {
2116 | if (!last_fd || last_fd->offset!=fd_i->offset){
2117 | dfpos += fd_i->len;
2118 | }
2119 | last_fd=fd_i;
2120 | }
2121 | if(fd_i){
2122 | /* new fragment overlaps existing fragment */
2123 | if(fd_i->len!=fd->len){
2124 | /*
2125 | * They have different lengths; this
2126 | * is definitely a conflict.
2127 | */
2128 | fd->flags |= FD_OVERLAPCONFLICT0x0004;
2129 | fd_head->flags |= FD_OVERLAPCONFLICT0x0004;
2130 | LINK_FRAG(fd_head,fd);
2131 | return true1;
2132 | }
2133 | DISSECTOR_ASSERT(fd_head->len >= dfpos + fd->len)((void) ((fd_head->len >= dfpos + fd->len) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\"" , "epan/reassemble.c", 2133, "fd_head->len >= dfpos + fd->len" ))));
2134 | if (tvb_memeql(fd_head->tvb_data, dfpos,
2135 | tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
2136 | /*
2137 | * They have the same length, but the
2138 | * data isn't the same.
2139 | */
2140 | fd->flags |= FD_OVERLAPCONFLICT0x0004;
2141 | fd_head->flags |= FD_OVERLAPCONFLICT0x0004;
2142 | LINK_FRAG(fd_head,fd);
2143 | return true1;
2144 | }
2145 | /* it was just an overlap, link it and return */
2146 | LINK_FRAG(fd_head,fd);
2147 | return true1;
2148 | } else {
2149 | /*
2150 | * New fragment doesn't overlap an existing
2151 | * fragment - there was presumably a gap in
2152 | * the sequence number space.
2153 | *
2154 | * XXX - what should we do here? Is it always
2155 | * the case that there are no gaps, or are there
2156 | * protcols using sequence numbers where there
2157 | * can be gaps?
2158 | *
2159 | * If the former, the check below for having
2160 | * received all the fragments should check for
2161 | * holes in the sequence number space and for the
2162 | * first sequence number being 0. If we do that,
2163 | * the only way we can get here is if this fragment
2164 | * is past the end of the sequence number space -
2165 | * but the check for "fd->offset > fd_head->datalen"
2166 | * would have caught that above, so it can't happen.
2167 | *
2168 | * If the latter, we don't have a good way of
2169 | * knowing whether reassembly is complete if we
2170 | * get packet out of order such that the "last"
2171 | * fragment doesn't show up last - but, unless
2172 | * in-order reliable delivery of fragments is
2173 | * guaranteed, an implementation of the protocol
2174 | * has no way of knowing whether reassembly is
2175 | * complete, either.
2176 | *
2177 | * For now, we just link the fragment in and
2178 | * return.
2179 | */
2180 | LINK_FRAG(fd_head,fd);
2181 | return true1;
2182 | }
2183 | }
2184 |
2185 | /* If we have reached this point, the packet is not defragmented yet.
2186 | * Save all payload in a buffer until we can defragment.
2187 | */
2188 | /* check len, there may be a fragment with 0 len, that is actually the tail */
2189 | if (fd->len) {
2190 | if (!tvb_bytes_exist(tvb, offset, fd->len)) {
2191 | /* abort if we didn't capture the entire fragment due
2192 | * to a too-short snapshot length */
2193 | g_slice_free(fragment_item, fd)do { if (1) g_slice_free1 (sizeof (fragment_item), (fd)); else (void) ((fragment_item*) 0 == (fd)); } while (0);
2194 | return false0;
2195 | }
2196 |
2197 | fd->tvb_data = tvb_clone_offset_len(tvb, offset, fd->len);
2198 | }
2199 | LINK_FRAG(fd_head,fd);
2200 |
2201 |
2202 | if( !(fd_head->flags & FD_DATALEN_SET0x0400) ){
2203 | /* if we don't know the sequence number of the last fragment,
2204 | * there are definitely still missing packets. Cheaper than
2205 | * the check below.
2206 | */
2207 | return false0;
2208 | }
2209 |
2210 |
2211 | /* check if we have received the entire fragment
2212 | * this is easy since the list is sorted and the head is faked.
2213 | * common case the whole list is scanned.
2214 | */
2215 | max = 0;
2216 | for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
2217 | if ( fd_i->offset==max ){
2218 | max++;
2219 | }
2220 | }
2221 | /* max will now be datalen+1 if all fragments have been seen */
2222 |
2223 | if (max <= fd_head->datalen) {
2224 | /* we have not received all packets yet */
2225 | return false0;
2226 | }
2227 |
2228 |
2229 | if (max > (fd_head->datalen+1)) {
2230 | /* oops, too long fragment detected */
2231 | fd->flags |= FD_TOOLONGFRAGMENT0x0010;
2232 | fd_head->flags |= FD_TOOLONGFRAGMENT0x0010;
2233 | }
2234 |
2235 |
2236 | /* we have received an entire packet, defragment it and
2237 | * free all fragments
2238 | */
2239 | fragment_defragment_and_free(fd_head, pinfo);
2240 |
2241 | return true1;
2242 | }
2243 |
2244 | /*
2245 | * This function adds a new fragment to the fragment hash table.
2246 | * If this is the first fragment seen for this datagram, a new entry
2247 | * is created in the hash table, otherwise this fragment is just added
2248 | * to the linked list of fragments for this packet.
2249 | *
2250 | * Returns a pointer to the head of the fragment data list if we have all the
2251 | * fragments, NULL otherwise.
2252 | *
2253 | * This function assumes frag_number being a block sequence number.
2254 | * The bsn for the first block is 0.
2255 | */
2256 | static fragment_head *
2257 | fragment_add_seq_common(reassembly_table *table, tvbuff_t *tvb,
2258 | const int offset, const packet_info *pinfo,
2259 | const uint32_t id, const void *data,
2260 | uint32_t frag_number, const uint32_t frag_data_len,
2261 | const bool_Bool more_frags, const uint32_t flags,
2262 | void * *orig_keyp)
2263 | {
2264 | fragment_head *fd_head;
2265 | void *orig_key;
2266 |
2267 | fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
2268 |
2269 | /* have we already seen this frame ?*/
2270 | if (pinfo->fd->visited) {
2271 | if (fd_head != NULL((void*)0) && fd_head->flags & FD_DEFRAGMENTED0x0001) {
2272 | if (orig_keyp != NULL((void*)0))
2273 | *orig_keyp = orig_key;
2274 | return fd_head;
2275 | } else {
2276 | return NULL((void*)0);
2277 | }
2278 | }
2279 |
2280 | if (fd_head==NULL((void*)0)){
2281 | /* not found, this must be the first snooped fragment for this
2282 | * packet. Create list-head.
2283 | */
2284 | fd_head = new_head(FD_BLOCKSEQUENCE0x0100);
2285 |
2286 | if((flags & (REASSEMBLE_FLAGS_NO_FRAG_NUMBER0x0001|REASSEMBLE_FLAGS_802_11_HACK0x0002))
2287 | && !more_frags) {
2288 | /*
2289 | * This is the last fragment for this packet, and
2290 | * is the only one we've seen.
2291 | *
2292 | * Either we don't have sequence numbers, in which
2293 | * case we assume this is the first fragment for
2294 | * this packet, or we're doing special 802.11
2295 | * processing, in which case we assume it's one
2296 | * of those reassembled packets with a non-zero
2297 | * fragment number (see packet-80211.c); just
2298 | * return a pointer to the head of the list;
2299 | * fragment_add_seq_check will then add it to the table
2300 | * of reassembled packets.
2301 | */
2302 | if (orig_keyp != NULL((void*)0))
2303 | *orig_keyp = NULL((void*)0);
2304 | /* To save memory, we don't actually copy the
2305 | * fragment from the tvbuff to the fragment, and in
2306 | * process_reassembled_data just return back a subset
2307 | * of the original tvbuff (which must be passed in).
2308 | */
2309 | fd_head->len = frag_data_len;
2310 | fd_head->reassembled_in=pinfo->num;
2311 | fd_head->reas_in_layer_num = pinfo->curr_layer_num;
2312 | return fd_head;
2313 | }
2314 |
2315 | orig_key = insert_fd_head(table, fd_head, pinfo, id, data);
2316 | if (orig_keyp != NULL((void*)0))
2317 | *orig_keyp = orig_key;
2318 |
2319 | /*
2320 | * If we weren't given an initial fragment number,
2321 | * make it 0.
2322 | */
2323 | if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER0x0001)
2324 | frag_number = 0;
2325 | } else {
2326 | if (orig_keyp != NULL((void*)0))
2327 | *orig_keyp = orig_key;
2328 |
2329 | /*
2330 | * XXX If fd_head exists, but nothing has been added to it,
2331 | * i.e. it was created with a known datalen with
2332 | * fragment_start_seq_check, we should also be able to
2333 | * do the memory saving trick as in the case above
2334 | * when first creating it.
2335 | */
2336 | if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER0x0001) {
2337 | fragment_item *fd;
2338 | /*
2339 | * If we weren't given an initial fragment number,
2340 | * use the next expected fragment number as the fragment
2341 | * number for this fragment.
2342 | *
2343 | * XXX - Use fd_head->first_gap to speed this up?
2344 | */
2345 | for (fd = fd_head->next; fd != NULL((void*)0); fd = fd->next) {
2346 | if (fd->next == NULL((void*)0))
2347 | frag_number = fd->offset + 1;
2348 | }
2349 | }
2350 | }
2351 |
2352 | if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
2353 | frag_number, frag_data_len, more_frags)) {
2354 | /*
2355 | * Reassembly is complete.
2356 | */
2357 | return fd_head;
2358 | } else {
2359 | /*
2360 | * Reassembly isn't complete.
2361 | */
2362 | return NULL((void*)0);
2363 | }
2364 | }
2365 |
2366 | fragment_head *
2367 | fragment_add_seq(reassembly_table *table, tvbuff_t *tvb, const int offset,
2368 | const packet_info *pinfo, const uint32_t id, const void *data,
2369 | const uint32_t frag_number, const uint32_t frag_data_len,
2370 | const bool_Bool more_frags, const uint32_t flags)
2371 | {
2372 | return fragment_add_seq_common(table, tvb, offset, pinfo, id, data,
2373 | frag_number, frag_data_len,
2374 | more_frags, flags, NULL((void*)0));
2375 | }
2376 |
2377 | /*
2378 | * This does the work for "fragment_add_seq_check()" and
2379 | * "fragment_add_seq_next()".
2380 | *
2381 | * This function assumes frag_number being a block sequence number.
2382 | * The bsn for the first block is 0.
2383 | *
2384 | * If REASSEMBLE_FLAGS_NO_FRAG_NUMBER, it uses the next expected fragment number
2385 | * as the fragment number if there is a reassembly in progress, otherwise
2386 | * it uses 0.
2387 | *
2388 | * If not REASSEMBLE_FLAGS_NO_FRAG_NUMBER, it uses the "frag_number" argument as
2389 | * the fragment number.
2390 | *
2391 | * If this is the first fragment seen for this datagram, a new
2392 | * "fragment_head" structure is allocated to refer to the reassembled
2393 | * packet.
2394 | *
2395 | * This fragment is added to the linked list of fragments for this packet.
2396 | *
2397 | * If "more_frags" is false and REASSEMBLE_FLAGS_802_11_HACK (as the name
2398 | * implies, a special hack for 802.11) or REASSEMBLE_FLAGS_NO_FRAG_NUMBER
2399 | * (implying messages must be in order since there's no sequence number) are
2400 | * set in "flags", then this (one element) list is returned.
2401 | *
2402 | * If, after processing this fragment, we have all the fragments,
2403 | * "fragment_add_seq_check_work()" removes that from the fragment hash
2404 | * table if necessary and adds it to the table of reassembled fragments,
2405 | * and returns a pointer to the head of the fragment list.
2406 | *
2407 | * Otherwise, it returns NULL.
2408 | *
2409 | * XXX - Should we simply return NULL for zero-length fragments?
2410 | */
2411 | static fragment_head *
2412 | fragment_add_seq_check_work(reassembly_table *table, tvbuff_t *tvb,
2413 | const int offset, const packet_info *pinfo,
2414 | const uint32_t id, const void *data,
2415 | const uint32_t frag_number,
2416 | const uint32_t frag_data_len,
2417 | const bool_Bool more_frags, const uint32_t flags)
2418 | {
2419 | reassembled_key reass_key;
2420 | fragment_head *fd_head;
2421 | void *orig_key;
2422 |
2423 | /*
2424 | * Have we already seen this frame?
2425 | * If so, look for it in the table of reassembled packets.
2426 | */
2427 | if (pinfo->fd->visited) {
2428 | reass_key.frame = pinfo->num;
2429 | reass_key.id = id;
2430 | return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
2431 | }
2432 |
2433 | fd_head = fragment_add_seq_common(table, tvb, offset, pinfo, id, data,
2434 | frag_number, frag_data_len,
2435 | more_frags,
2436 | flags,
2437 | &orig_key);
2438 | if (fd_head) {
2439 | /*
2440 | * Reassembly is complete.
2441 | *
2442 | * If this is in the table of in-progress reassemblies,
2443 | * remove it from that table. (It could be that this
2444 | * was the first and last fragment, so that no
2445 | * reassembly was done.)
2446 | */
2447 | if (orig_key != NULL((void*)0))
2448 | fragment_unhash(table, orig_key);
2449 |
2450 | /*
2451 | * Add this item to the table of reassembled packets.
2452 | */
2453 | fragment_reassembled(table, fd_head, pinfo, id);
2454 | return fd_head;
2455 | } else {
2456 | /*
2457 | * Reassembly isn't complete.
2458 | */
2459 | return NULL((void*)0);
2460 | }
2461 | }
2462 |
2463 | fragment_head *
2464 | fragment_add_seq_check(reassembly_table *table, tvbuff_t *tvb, const int offset,
2465 | const packet_info *pinfo, const uint32_t id,
2466 | const void *data,
2467 | const uint32_t frag_number, const uint32_t frag_data_len,
2468 | const bool_Bool more_frags)
2469 | {
2470 | return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2471 | frag_number, frag_data_len,
2472 | more_frags, 0);
2473 | }
2474 |
2475 | fragment_head *
2476 | fragment_add_seq_802_11(reassembly_table *table, tvbuff_t *tvb,
2477 | const int offset, const packet_info *pinfo,
2478 | const uint32_t id, const void *data,
2479 | const uint32_t frag_number, const uint32_t frag_data_len,
2480 | const bool_Bool more_frags)
2481 | {
2482 | return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2483 | frag_number, frag_data_len,
2484 | more_frags,
2485 | REASSEMBLE_FLAGS_802_11_HACK0x0002);
2486 | }
2487 |
2488 | fragment_head *
2489 | fragment_add_seq_next(reassembly_table *table, tvbuff_t *tvb, const int offset,
2490 | const packet_info *pinfo, const uint32_t id,
2491 | const void *data, const uint32_t frag_data_len,
2492 | const bool_Bool more_frags)
2493 | {
2494 | /* Use a dummy frag_number (0), it is ignored since
2495 | * REASSEMBLE_FLAGS_NO_FRAG_NUMBER is set. */
2496 | return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2497 | 0, frag_data_len, more_frags,
2498 | REASSEMBLE_FLAGS_NO_FRAG_NUMBER0x0001);
2499 | }
2500 |
2501 | static void
2502 | fragment_add_seq_single_move(reassembly_table *table, const packet_info *pinfo,
2503 | const uint32_t id, const void *data,
2504 | const uint32_t offset)
2505 | {
2506 | fragment_head *fh, *new_fh;
2507 | fragment_item *fd, *prev_fd;
2508 | tvbuff_t *old_tvb_data;
2509 | if (offset == 0) {
2510 | return;
2511 | }
2512 | fh = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
2513 | if (fh == NULL((void*)0)) {
2514 | /* Shouldn't be called this way.
2515 | * Probably wouldn't hurt to just create fh in this case. */
2516 | ws_assert_not_reached()ws_log_fatal_full("", LOG_LEVEL_ERROR, "epan/reassemble.c", 2516 , __func__, "assertion \"not reached\" failed");
2517 | return;
2518 | }
2519 | if (fh->flags & FD_DATALEN_SET0x0400 && fh->datalen <= offset) {
2520 | /* Don't take from past the end. <= because we don't
2521 | * want to take a First fragment from the next one
2522 | * either */
2523 | return;
2524 | }
2525 | new_fh = lookup_fd_head(table, pinfo, id+offset, data, NULL((void*)0));
2526 | if (new_fh != NULL((void*)0)) {
2527 | /* Attach to the end of the sorted list. */
2528 | prev_fd = NULL((void*)0);
2529 | for(fd = fh->next; fd != NULL((void*)0); fd=fd->next) {
2530 | prev_fd = fd;
2531 | }
2532 | /* Don't take a reassembly starting with a First fragment. */
2533 | fd = new_fh->next;
2534 | if (fd && fd->offset != 0) {
2535 | fragment_item *inserted = fd;
2536 | bool_Bool multi_insert = (inserted->next != NULL((void*)0));
2537 | if (prev_fd) {
2538 | prev_fd->next = fd;
2539 | } else {
2540 | fh->next = fd;
2541 | }
2542 | for (; fd; fd=fd->next) {
2543 | fd->offset += offset;
2544 | if (fh->frame < fd->frame) {
2545 | fh->frame = fd->frame;
2546 | }
2547 | }
2548 | update_first_gap(fh, inserted, multi_insert);
2549 | /* If previously found a Last fragment,
2550 | * transfer that info to the new one. */
2551 | if (new_fh->flags & FD_DATALEN_SET0x0400) {
2552 | fh->flags |= FD_DATALEN_SET0x0400;
2553 | fh->datalen = new_fh->datalen + offset;
2554 | }
2555 | /* Now remove and delete */
2556 | new_fh->next = NULL((void*)0);
2557 | old_tvb_data = fragment_delete(table, pinfo, id+offset, data);
2558 | if (old_tvb_data)
2559 | tvb_free(old_tvb_data);
2560 | }
2561 | }
2562 | }
2563 |
2564 | static fragment_head *
2565 | fragment_add_seq_single_work(reassembly_table *table, tvbuff_t *tvb,
2566 | const int offset, const packet_info *pinfo,
2567 | const uint32_t id, const void* data,
2568 | const uint32_t frag_data_len,
2569 | const bool_Bool first, const bool_Bool last,
2570 | const uint32_t max_frags, const uint32_t max_age,
2571 | const uint32_t flags)
2572 | {
2573 | reassembled_key reass_key;
2574 | tvbuff_t *old_tvb_data;
2575 | void *orig_key;
2576 | fragment_head *fh, *new_fh;
2577 | fragment_item *fd, *prev_fd;
2578 | uint32_t frag_number, tmp_offset;
2579 | /* Have we already seen this frame?
2580 | * If so, look for it in the table of reassembled packets.
2581 | * Note here we store in the reassembly table by the single sequence
2582 | * number rather than the sequence number of the First fragment. */
2583 | if (pinfo->fd->visited) {
2584 | reass_key.frame = pinfo->num;
2585 | reass_key.id = id;
2586 | fh = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
2587 | return fh;
2588 | }
2589 | /* First let's figure out where we want to add our new fragment */
2590 | fh = NULL((void*)0);
2591 | if (first) {
2592 | frag_number = 0;
2593 | fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL((void*)0));
2594 | if ((flags & REASSEMBLE_FLAGS_AGING0x0001) &&
2595 | fh && ((fh->frame + max_age) < pinfo->num)) {
2596 | old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data);
2597 | if (old_tvb_data)
2598 | tvb_free(old_tvb_data);
2599 | fh = NULL((void*)0);
2600 | }
2601 | if (fh == NULL((void*)0)) {
2602 | /* Not found. Create list-head. */
2603 | fh = new_head(FD_BLOCKSEQUENCE0x0100);
2604 | insert_fd_head(table, fh, pinfo, id-frag_number, data);
2605 | }
2606 | /* As this is the first fragment, we might have added segments
2607 | * for this reassembly to the previous one in-progress. */
2608 | fd = NULL((void*)0);
2609 | for (frag_number=1; frag_number < max_frags; frag_number++) {
2610 | new_fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL((void*)0));
2611 | if (new_fh != NULL((void*)0)) {
2612 | prev_fd = NULL((void*)0);
2613 | new_fh->frame = 0;
2614 | for (fd=new_fh->next; fd && fd->offset < frag_number; fd=fd->next) {
2615 | prev_fd = fd;
2616 | if (new_fh->frame < fd->frame) {
2617 | new_fh->frame = fd->frame;
2618 | }
2619 | }
2620 | if (prev_fd) {
2621 | prev_fd->next = NULL((void*)0);
2622 | } else {
2623 | new_fh->next = NULL((void*)0);
2624 | }
2625 | fragment_items_removed(new_fh, prev_fd);
2626 | break;
2627 | }
2628 | }
2629 | if (fd != NULL((void*)0)) {
2630 | tmp_offset = 0;
2631 | for (prev_fd = fd; prev_fd; prev_fd = prev_fd->next) {
2632 | prev_fd->offset -= frag_number;
2633 | tmp_offset = prev_fd->offset;
2634 | if (fh->frame < prev_fd->frame) {
2635 | fh->frame = prev_fd->frame;
2636 | }
2637 | }
2638 | MERGE_FRAG(fh, fd);
2639 | if (new_fh != NULL((void*)0)) {
2640 | /* If we've moved a Last packet, change datalen.
2641 | * Second part of this test prob. redundant? */
2642 | if (new_fh->flags & FD_DATALEN_SET0x0400 &&
2643 | new_fh->datalen >= frag_number) {
2644 | fh->flags |= FD_DATALEN_SET0x0400;
2645 | fh->datalen = new_fh->datalen - frag_number;
2646 | new_fh->flags &= ~FD_DATALEN_SET0x0400;
2647 | new_fh->datalen = 0;
2648 | }
2649 | /* If we've moved all the fragments,
2650 | * delete the old head */
2651 | if (new_fh->next == NULL((void*)0)) {
2652 | old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data);
2653 | if (old_tvb_data)
2654 | tvb_free(old_tvb_data);
2655 | }
2656 | } else {
2657 | /* Look forward and take off the next (this is
2658 | * necessary in some edge cases where max_frags
2659 | * prevented some fragments from going on the
2660 | * previous First, but they can go on this one. */
2661 | fragment_add_seq_single_move(table, pinfo, id,
2662 | data, tmp_offset);
2663 | }
2664 | }
2665 | frag_number = 0; /* For the rest of the function */
2666 | } else {
2667 | for (frag_number=1; frag_number < max_frags; frag_number++) {
2668 | fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL((void*)0));
2669 | if ((flags & REASSEMBLE_FLAGS_AGING0x0001) &&
2670 | fh && ((fh->frame + max_age) < pinfo->num)) {
2671 | old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data);
2672 | if (old_tvb_data)
2673 | tvb_free(old_tvb_data);
2674 | fh = NULL((void*)0);
2675 | }
2676 | if (fh != NULL((void*)0)) {
2677 | if (fh->flags & FD_DATALEN_SET0x0400 &&
2678 | fh->datalen < frag_number) {
2679 | /* This fragment is after the Last
2680 | * fragment, so must go after here. */
2681 | fh = NULL((void*)0);
2682 | }
2683 | break;
2684 | }
2685 | }
2686 | if (fh == NULL((void*)0)) { /* Didn't find location, use default */
2687 | frag_number = 1;
2688 | /* Already looked for frag_number 1, so just create */
2689 | fh = new_head(FD_BLOCKSEQUENCE0x0100);
2690 | insert_fd_head(table, fh, pinfo, id-frag_number, data);
2691 | }
2692 | }
2693 | if (last) {
2694 | /* Look for fragments past the end set by this Last fragment. */
2695 | prev_fd = NULL((void*)0);
2696 | for (fd=fh->next; fd && fd->offset <= frag_number; fd=fd->next) {
2697 | prev_fd = fd;
2698 | }
2699 | /* fd is now all fragments offset > frag_number (the Last).
2700 | * It shouldn't have a fragment with offset frag_number+1,
2701 | * as that would be a First fragment not marked as such.
2702 | * However, this can happen if we had unreassembled fragments
2703 | * (missing, or at the start of the capture) and we've also
2704 | * looped around on the sequence numbers. It can also happen
2705 | * if bit errors mess up Last or First. */
2706 | if (fd != NULL((void*)0)) {
2707 | if (prev_fd) {
2708 | prev_fd->next = NULL((void*)0);
2709 | } else {
2710 | fh->next = NULL((void*)0);
2711 | }
2712 | fragment_items_removed(fh, prev_fd);
2713 | fh->frame = 0;
2714 | for (prev_fd=fh->next; prev_fd; prev_fd=prev_fd->next) {
2715 | if (fh->frame < prev_fd->frame) {
2716 | fh->frame = prev_fd->frame;
2717 | }
2718 | }
2719 | while (fd && fd->offset == frag_number+1) {
2720 | /* Definitely have bad data here. Best to
2721 | * delete these and leave unreassembled. */
2722 | fd = fragment_item_free(fd);
2723 | }
2724 | }
2725 | if (fd != NULL((void*)0)) {
2726 | /* Move these onto the next frame. */
2727 | new_fh = lookup_fd_head(table, pinfo, id+1, data, NULL((void*)0));
2728 | if (new_fh==NULL((void*)0)) {
2729 | /* Not found. Create list-head. */
2730 | new_fh = new_head(FD_BLOCKSEQUENCE0x0100);
2731 | insert_fd_head(table, new_fh, pinfo, id+1, data);
2732 | }
2733 | tmp_offset = 0;
2734 | for (prev_fd = fd; prev_fd; prev_fd = prev_fd->next) {
2735 | prev_fd->offset -= (frag_number+1);
2736 | tmp_offset = prev_fd->offset;
2737 | if (new_fh->frame < fd->frame) {
2738 | new_fh->frame = fd->frame;
2739 | }
2740 | }
2741 | MERGE_FRAG(new_fh, fd);
2742 | /* If we previously found a different Last fragment,
2743 | * transfer that information to the new reassembly. */
2744 | if (fh->flags & FD_DATALEN_SET0x0400 &&
2745 | fh->datalen > frag_number) {
2746 | new_fh->flags |= FD_DATALEN_SET0x0400;
2747 | new_fh->datalen = fh->datalen - (frag_number+1);
2748 | fh->flags &= ~FD_DATALEN_SET0x0400;
2749 | fh->datalen = 0;
2750 | } else {
2751 | /* Look forward and take off the next (this is
2752 | * necessary in some edge cases where max_frags
2753 | * prevented some fragments from going on the
2754 | * previous First, but they can go on this one. */
2755 | fragment_add_seq_single_move(table, pinfo, id+1,
2756 | data, tmp_offset);
2757 | }
2758 | }
2759 | } else {
2760 | fragment_add_seq_single_move(table, pinfo, id-frag_number, data,
2761 | frag_number+1);
2762 | }
2763 | /* Having cleaned up everything, finally ready to add our new
2764 | * fragment. Note that only this will ever complete a reassembly. */
2765 | fh = fragment_add_seq_common(table, tvb, offset, pinfo,
2766 | id-frag_number, data,
2767 | frag_number, frag_data_len,
2768 | !last, 0, &orig_key);
2769 | if (fh) {
2770 | /*
2771 | * Reassembly is complete.
2772 | *
2773 | * If this is in the table of in-progress reassemblies,
2774 | * remove it from that table. (It could be that this
2775 | * was the first and last fragment, so that no
2776 | * reassembly was done.)
2777 | */
2778 | if (orig_key != NULL((void*)0))
2779 | fragment_unhash(table, orig_key);
2780 |
2781 | /*
2782 | * Add this item to the table of reassembled packets.
2783 | */
2784 | fragment_reassembled_single(table, fh, pinfo, id-frag_number);
2785 | return fh;
2786 | } else {
2787 | /*
2788 | * Reassembly isn't complete.
2789 | */
2790 | return NULL((void*)0);
2791 | }
2792 | }
2793 |
2794 | fragment_head *
2795 | fragment_add_seq_single(reassembly_table *table, tvbuff_t *tvb,
2796 | const int offset, const packet_info *pinfo,
2797 | const uint32_t id, const void* data,
2798 | const uint32_t frag_data_len,
2799 | const bool_Bool first, const bool_Bool last,
2800 | const uint32_t max_frags)
2801 | {
2802 | return fragment_add_seq_single_work(table, tvb, offset, pinfo,
2803 | id, data, frag_data_len,
2804 | first, last, max_frags, 0, 0);
2805 | }
2806 |
2807 | fragment_head *
2808 | fragment_add_seq_single_aging(reassembly_table *table, tvbuff_t *tvb,
2809 | const int offset, const packet_info *pinfo,
2810 | const uint32_t id, const void* data,
2811 | const uint32_t frag_data_len,
2812 | const bool_Bool first, const bool_Bool last,
2813 | const uint32_t max_frags, const uint32_t max_age)
2814 | {
2815 | return fragment_add_seq_single_work(table, tvb, offset, pinfo,
2816 | id, data, frag_data_len,
2817 | first, last, max_frags, max_age,
2818 | REASSEMBLE_FLAGS_AGING0x0001);
2819 | }
2820 |
2821 | void
2822 | fragment_start_seq_check(reassembly_table *table, const packet_info *pinfo,
2823 | const uint32_t id, const void *data,
2824 | const uint32_t tot_len)
2825 | {
2826 | fragment_head *fd_head;
2827 |
2828 | /* Have we already seen this frame ?*/
2829 | if (pinfo->fd->visited) {
2830 | return;
2831 | }
2832 |
2833 | /* Check if fragment data exists */
2834 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL((void*)0));
2835 |
2836 | if (fd_head == NULL((void*)0)) {
2837 | /* Create list-head. */
2838 | fd_head = new_head(FD_BLOCKSEQUENCE0x0100|FD_DATALEN_SET0x0400);
2839 | fd_head->datalen = tot_len;
2840 |
2841 | insert_fd_head(table, fd_head, pinfo, id, data);
2842 | }
2843 | }
2844 |
2845 | fragment_head *
2846 | fragment_end_seq_next(reassembly_table *table, const packet_info *pinfo,
2847 | const uint32_t id, const void *data)
2848 | {
2849 | reassembled_key reass_key;
2850 | reassembled_key *new_key;
2851 | fragment_head *fd_head;
2852 | fragment_item *fd;
2853 | void *orig_key;
2854 | uint32_t max_offset = 0;
2855 |
2856 | /*
2857 | * Have we already seen this frame?
2858 | * If so, look for it in the table of reassembled packets.
2859 | */
2860 | if (pinfo->fd->visited) {
1 | Assuming field 'visited' is 0 | →
2 | ← | Taking false branch | →
2861 | reass_key.frame = pinfo->num;
2862 | reass_key.id = id;
2863 | return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
2864 | }
2865 |
2866 | fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
2867 |
2868 | if (fd_head) {
3 | ← | Assuming 'fd_head' is non-null | →
4 | ← | Taking true branch | →
2869 | for (fd = fd_head->next; fd; fd = fd->next) {
5 | ← | Loop condition is false. Execution continues on line 2874 | →
2870 | if (fd->offset > max_offset) {
2871 | max_offset = fd->offset;
2872 | }
2873 | }
2874 | fd_head->datalen = max_offset;
2875 | fd_head->flags |= FD_DATALEN_SET0x0400;
2876 |
2877 | fragment_defragment_and_free (fd_head, pinfo);
6 | ← | Calling 'fragment_defragment_and_free' | →
2878 |
2879 | /*
2880 | * Remove this from the table of in-progress reassemblies,
2881 | * and free up any memory used for it in that table.
2882 | */
2883 | fragment_unhash(table, orig_key);
2884 |
2885 | /*
2886 | * Add this item to the table of reassembled packets.
2887 | */
2888 | fragment_reassembled(table, fd_head, pinfo, id);
2889 | if (fd_head->next != NULL((void*)0)) {
2890 | new_key = g_slice_new(reassembled_key)((reassembled_key*) g_slice_alloc ((sizeof (reassembled_key) > 0 ? sizeof (reassembled_key) : 1)));
2891 | new_key->frame = pinfo->num;
2892 | new_key->id = id;
2893 | reassembled_table_insert(table->reassembled_table, new_key, fd_head);
2894 | }
2895 |
2896 | return fd_head;
2897 | } else {
2898 | /*
2899 | * Fragment data not found.
2900 | */
2901 | return NULL((void*)0);
2902 | }
2903 | }
2904 |
2905 | /*
2906 | * Process reassembled data; if we're on the frame in which the data
2907 | * was reassembled, put the fragment information into the protocol
2908 | * tree, and construct a tvbuff with the reassembled data, otherwise
2909 | * just put a "reassembled in" item into the protocol tree.
2910 | * offset from start of tvb, result up to end of tvb
2911 | */
2912 | tvbuff_t *
2913 | process_reassembled_data(tvbuff_t *tvb, const int offset, packet_info *pinfo,
2914 | const char *name, fragment_head *fd_head, const fragment_items *fit,
2915 | bool_Bool *update_col_infop, proto_tree *tree)
2916 | {
2917 | tvbuff_t *next_tvb;
2918 | bool_Bool update_col_info;
2919 | proto_item *frag_tree_item;
2920 |
2921 | if (fd_head != NULL((void*)0) && pinfo->num == fd_head->reassembled_in && pinfo->curr_layer_num == fd_head->reas_in_layer_num) {
2922 | /*
2923 | * OK, we've reassembled this.
2924 | * Is this something that's been reassembled from more
2925 | * than one fragment?
2926 | */
2927 | if (fd_head->next != NULL((void*)0)) {
2928 | /*
2929 | * Yes.
2930 | * Allocate a new tvbuff, referring to the
2931 | * reassembled payload, and set
2932 | * the tvbuff to the list of tvbuffs to which
2933 | * the tvbuff we were handed refers, so it'll get
2934 | * cleaned up when that tvbuff is cleaned up.
2935 | */
2936 | next_tvb = tvb_new_chain(tvb, fd_head->tvb_data);
2937 |
2938 | /* Add the defragmented data to the data source list. */
2939 | add_new_data_source(pinfo, next_tvb, name);
2940 |
2941 | /* show all fragments */
2942 | if (fd_head->flags & FD_BLOCKSEQUENCE0x0100) {
2943 | update_col_info = !show_fragment_seq_tree(
2944 | fd_head, fit, tree, pinfo, next_tvb, &frag_tree_item);
2945 | } else {
2946 | update_col_info = !show_fragment_tree(fd_head,
2947 | fit, tree, pinfo, next_tvb, &frag_tree_item);
2948 | }
2949 | } else {
2950 | /*
2951 | * No.
2952 | * Return a tvbuff with the payload, a subset of the
2953 | * tvbuff passed in. (The dissector SHOULD pass in
2954 | * the correct tvbuff and offset.)
2955 | */
2956 | int len;
2957 | /* For FD_BLOCKSEQUENCE, len is the length in bytes,
2958 | * datalen is the number of fragments.
2959 | */
2960 | if (fd_head->flags & FD_BLOCKSEQUENCE0x0100) {
2961 | len = fd_head->len;
2962 | } else {
2963 | len = fd_head->datalen;
2964 | }
2965 | next_tvb = tvb_new_subset_length(tvb, offset, len);
2966 | pinfo->fragmented = false0; /* one-fragment packet */
2967 | update_col_info = true1;
2968 | }
2969 | if (update_col_infop != NULL((void*)0))
2970 | *update_col_infop = update_col_info;
2971 | } else {
2972 | /*
2973 | * We don't have the complete reassembled payload, or this
2974 | * isn't the final frame of that payload.
2975 | */
2976 | next_tvb = NULL((void*)0);
2977 |
2978 | /*
2979 | * If we know what frame this was reassembled in,
2980 | * and if there's a field to use for the number of
2981 | * the frame in which the packet was reassembled,
2982 | * add it to the protocol tree.
2983 | */
2984 | if (fd_head != NULL((void*)0) && fit->hf_reassembled_in != NULL((void*)0)) {
2985 | proto_item *fei = proto_tree_add_uint(tree,
2986 | *(fit->hf_reassembled_in), tvb,
2987 | 0, 0, fd_head->reassembled_in);
2988 | proto_item_set_generated(fei);
2989 | }
2990 | }
2991 | return next_tvb;
2992 | }
2993 |
2994 | /*
2995 | * Show a single fragment in a fragment subtree, and put information about
2996 | * it in the top-level item for that subtree.
2997 | */
2998 | static void
2999 | show_fragment(fragment_item *fd, const int offset, const fragment_items *fit,
3000 | proto_tree *ft, proto_item *fi, const bool_Bool first_frag,
3001 | const uint32_t count, tvbuff_t *tvb, packet_info *pinfo)
3002 | {
3003 | proto_item *fei=NULL((void*)0);
3004 | int hf;
3005 |
3006 | if (first_frag) {
3007 | char *name;
3008 | if (count == 1) {
3009 | name = g_strdup(proto_registrar_get_name(*(fit->hf_fragment)))g_strdup_inline (proto_registrar_get_name(*(fit->hf_fragment )));
3010 | } else {
3011 | name = g_strdup(proto_registrar_get_name(*(fit->hf_fragments)))g_strdup_inline (proto_registrar_get_name(*(fit->hf_fragments )));
3012 | }
3013 | proto_item_set_text(fi, "%u %s (%u byte%s): ", count, name, tvb_captured_length(tvb),
3014 | plurality(tvb_captured_length(tvb), "", "s")((tvb_captured_length(tvb)) == 1 ? ("") : ("s")));
3015 | g_free(name)(__builtin_object_size ((name), 0) != ((size_t) - 1)) ? g_free_sized (name, __builtin_object_size ((name), 0)) : (g_free) (name);
3016 | } else {
3017 | proto_item_append_text(fi, ", ");
3018 | }
3019 | proto_item_append_text(fi, "#%u(%u)", fd->frame, fd->len);
3020 |
3021 | if (fd->flags & (FD_OVERLAPCONFLICT0x0004
3022 | |FD_MULTIPLETAILS0x0008|FD_TOOLONGFRAGMENT0x0010) ) {
3023 | hf = *(fit->hf_fragment_error);
3024 | } else {
3025 | hf = *(fit->hf_fragment);
3026 | }
3027 | if (fd->len == 0) {
3028 | fei = proto_tree_add_uint_format(ft, hf,
3029 | tvb, offset, fd->len,
3030 | fd->frame,
3031 | "Frame: %u (no data)",
3032 | fd->frame);
3033 | } else {
3034 | fei = proto_tree_add_uint_format(ft, hf,
3035 | tvb, offset, fd->len,
3036 | fd->frame,
3037 | "Frame: %u, payload: %u-%u (%u byte%s)",
3038 | fd->frame,
3039 | offset,
3040 | offset+fd->len-1,
3041 | fd->len,
3042 | plurality(fd->len, "", "s")((fd->len) == 1 ? ("") : ("s")));
3043 | }
3044 | proto_item_set_generated(fei);
3045 | mark_frame_as_depended_upon(pinfo->fd, fd->frame);
3046 | if (fd->flags & (FD_OVERLAP0x0002|FD_OVERLAPCONFLICT0x0004
3047 | |FD_MULTIPLETAILS0x0008|FD_TOOLONGFRAGMENT0x0010) ) {
3048 | /* this fragment has some flags set, create a subtree
3049 | * for it and display the flags.
3050 | */
3051 | proto_tree *fet=NULL((void*)0);
3052 |
3053 | fet = proto_item_add_subtree(fei, *(fit->ett_fragment));
3054 | if (fd->flags&FD_OVERLAP0x0002) {
3055 | fei=proto_tree_add_boolean(fet,
3056 | *(fit->hf_fragment_overlap),
3057 | tvb, 0, 0,
3058 | true1);
3059 | proto_item_set_generated(fei);
3060 | }
3061 | if (fd->flags&FD_OVERLAPCONFLICT0x0004) {
3062 | fei=proto_tree_add_boolean(fet,
3063 | *(fit->hf_fragment_overlap_conflict),
3064 | tvb, 0, 0,
3065 | true1);
3066 | proto_item_set_generated(fei);
3067 | }
3068 | if (fd->flags&FD_MULTIPLETAILS0x0008) {
3069 | fei=proto_tree_add_boolean(fet,
3070 | *(fit->hf_fragment_multiple_tails),
3071 | tvb, 0, 0,
3072 | true1);
3073 | proto_item_set_generated(fei);
3074 | }
3075 | if (fd->flags&FD_TOOLONGFRAGMENT0x0010) {
3076 | fei=proto_tree_add_boolean(fet,
3077 | *(fit->hf_fragment_too_long_fragment),
3078 | tvb, 0, 0,
3079 | true1);
3080 | proto_item_set_generated(fei);
3081 | }
3082 | }
3083 | }
3084 |
3085 | static bool_Bool
3086 | show_fragment_errs_in_col(fragment_head *fd_head, const fragment_items *fit,
3087 | packet_info *pinfo)
3088 | {
3089 | if (fd_head->flags & (FD_OVERLAPCONFLICT0x0004
3090 | |FD_MULTIPLETAILS0x0008|FD_TOOLONGFRAGMENT0x0010) ) {
3091 | col_add_fstr(pinfo->cinfo, COL_INFO, "[Illegal %s]", fit->tag);
3092 | return true1;
3093 | }
3094 |
3095 | return false0;
3096 | }
3097 |
3098 | /* This function will build the fragment subtree; it's for fragments
3099 | reassembled with "fragment_add()".
3100 |
3101 | It will return true if there were fragmentation errors
3102 | or false if fragmentation was ok.
3103 | */
3104 | bool_Bool
3105 | show_fragment_tree(fragment_head *fd_head, const fragment_items *fit,
3106 | proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi)
3107 | {
3108 | fragment_item *fd;
3109 | proto_tree *ft;
3110 | bool_Bool first_frag;
3111 | uint32_t count = 0;
3112 | /* It's not fragmented. */
3113 | pinfo->fragmented = false0;
3114 |
3115 | *fi = proto_tree_add_item(tree, *(fit->hf_fragments), tvb, 0, -1, ENC_NA0x00000000);
3116 | proto_item_set_generated(*fi);
3117 |
3118 | ft = proto_item_add_subtree(*fi, *(fit->ett_fragments));
3119 | first_frag = true1;
3120 | for (fd = fd_head->next; fd != NULL((void*)0); fd = fd->next) {
3121 | count++;
3122 | }
3123 | for (fd = fd_head->next; fd != NULL((void*)0); fd = fd->next) {
3124 | show_fragment(fd, fd->offset, fit, ft, *fi, first_frag, count, tvb, pinfo);
3125 | first_frag = false0;
3126 | }
3127 |
3128 | if (fit->hf_fragment_count) {
3129 | proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_fragment_count),
3130 | tvb, 0, 0, count);
3131 | proto_item_set_generated(fli);
3132 | }
3133 |
3134 | if (fit->hf_reassembled_length) {
3135 | proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_reassembled_length),
3136 | tvb, 0, 0, tvb_captured_length (tvb));
3137 | proto_item_set_generated(fli);
3138 | }
3139 |
3140 | if (fit->hf_reassembled_data) {
3141 | proto_item *fli = proto_tree_add_item(ft, *(fit->hf_reassembled_data),
3142 | tvb, 0, tvb_captured_length(tvb), ENC_NA0x00000000);
3143 | proto_item_set_generated(fli);
3144 | }
3145 |
3146 | return show_fragment_errs_in_col(fd_head, fit, pinfo);
3147 | }
3148 |
3149 | /* This function will build the fragment subtree; it's for fragments
3150 | reassembled with "fragment_add_seq()" or "fragment_add_seq_check()".
3151 |
3152 | It will return true if there were fragmentation errors
3153 | or false if fragmentation was ok.
3154 | */
3155 | bool_Bool
3156 | show_fragment_seq_tree(fragment_head *fd_head, const fragment_items *fit,
3157 | proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi)
3158 | {
3159 | uint32_t offset, next_offset, count = 0;
3160 | fragment_item *fd, *last_fd;
3161 | proto_tree *ft;
3162 | bool_Bool first_frag;
3163 |
3164 | /* It's not fragmented. */
3165 | pinfo->fragmented = false0;
3166 |
3167 | *fi = proto_tree_add_item(tree, *(fit->hf_fragments), tvb, 0, -1, ENC_NA0x00000000);
3168 | proto_item_set_generated(*fi);
3169 |
3170 | ft = proto_item_add_subtree(*fi, *(fit->ett_fragments));
3171 | offset = 0;
3172 | next_offset = 0;
3173 | last_fd = NULL((void*)0);
3174 | first_frag = true1;
3175 | for (fd = fd_head->next; fd != NULL((void*)0); fd = fd->next){
3176 | count++;
3177 | }
3178 | for (fd = fd_head->next; fd != NULL((void*)0); fd = fd->next){
3179 | if (last_fd == NULL((void*)0) || last_fd->offset != fd->offset) {
3180 | offset = next_offset;
3181 | next_offset += fd->len;
3182 | }
3183 | last_fd = fd;
3184 | show_fragment(fd, offset, fit, ft, *fi, first_frag, count, tvb, pinfo);
3185 | first_frag = false0;
3186 | }
3187 |
3188 | if (fit->hf_fragment_count) {
3189 | proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_fragment_count),
3190 | tvb, 0, 0, count);
3191 | proto_item_set_generated(fli);
3192 | }
3193 |
3194 | if (fit->hf_reassembled_length) {
3195 | proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_reassembled_length),
3196 | tvb, 0, 0, tvb_captured_length (tvb));
3197 | proto_item_set_generated(fli);
3198 | }
3199 |
3200 | if (fit->hf_reassembled_data) {
3201 | proto_item *fli = proto_tree_add_item(ft, *(fit->hf_reassembled_data),
3202 | tvb, 0, tvb_captured_length(tvb), ENC_NA0x00000000);
3203 | proto_item_set_generated(fli);
3204 | }
3205 |
3206 | return show_fragment_errs_in_col(fd_head, fit, pinfo);
3207 | }
3208 |
3209 | static void
3210 | reassembly_table_init_reg_table(void *p, void *user_data _U___attribute__((unused)))
3211 | {
3212 | register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p;
3213 | reassembly_table_init(reg_table->table, reg_table->funcs);
3214 | }
3215 |
3216 | static void
3217 | reassembly_table_init_reg_tables(void)
3218 | {
3219 | g_list_foreach(reassembly_table_list, reassembly_table_init_reg_table, NULL((void*)0));
3220 | }
3221 |
3222 | static void
3223 | reassembly_table_cleanup_reg_table(void *p, void *user_data _U___attribute__((unused)))
3224 | {
3225 | register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p;
3226 | reassembly_table_destroy(reg_table->table);
3227 | }
3228 |
3229 | static void
3230 | reassembly_table_cleanup_reg_tables(void)
3231 | {
3232 | g_list_foreach(reassembly_table_list, reassembly_table_cleanup_reg_table, NULL((void*)0));
3233 | }
3234 |
3235 | void reassembly_tables_init(void)
3236 | {
3237 | register_init_routine(&reassembly_table_init_reg_tables);
3238 | register_cleanup_routine(&reassembly_table_cleanup_reg_tables);
3239 | }
3240 |
3241 | static void
3242 | reassembly_table_free(void *p, void *user_data _U___attribute__((unused)))
3243 | {
3244 | register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p;
3245 | reassembly_table_destroy(reg_table->table);
3246 | g_free(reg_table)(__builtin_object_size ((reg_table), 0) != ((size_t) - 1)) ? g_free_sized (reg_table, __builtin_object_size ((reg_table), 0)) : (g_free ) (reg_table);
3247 | }
3248 |
3249 | void
3250 | reassembly_table_cleanup(void)
3251 | {
3252 | g_list_foreach(reassembly_table_list, reassembly_table_free, NULL((void*)0));
3253 | g_list_free(reassembly_table_list);
3254 | }
3255 |
3256 | /* One instance of this structure is created for each pdu that spans across
3257 | * multiple segments. (MSP) */
3258 | typedef struct _multisegment_pdu_t {
3259 | uint64_t first_frame;
3260 | uint64_t last_frame;
3261 | unsigned start_offset_at_first_frame;
3262 | unsigned end_offset_at_last_frame;
3263 | int length; /* length of this MSP */
3264 | uint32_t streaming_reassembly_id;
3265 | /* pointer to previous multisegment_pdu */
3266 | struct _multisegment_pdu_t* prev_msp;
3267 | } multisegment_pdu_t;
3268 |
3269 | /* struct for keeping the reassembly information of each stream */
3270 | struct streaming_reassembly_info_t {
3271 | /* This map is keyed by frame num and keeps track of all MSPs for this
3272 | * stream. Different frames will point to the same MSP if they contain
3273 | * part data of this MSP. If a frame contains data that
3274 | * belongs to two MSPs, it will point to the second MSP. */
3275 | wmem_map_t* multisegment_pdus;
3276 | /* This map is keyed by frame num and keeps track of the frag_offset
3277 | * of the first byte of frames for fragment_add() after first scan. */
3278 | wmem_map_t* frame_num_frag_offset_map;
3279 | /* how many bytes the current uncompleted MSP still needs. (only valid for first scan) */
3280 | int prev_deseg_len;
3281 | /* the current uncompleted MSP (only valid for first scan) */
3282 | multisegment_pdu_t* last_msp;
3283 | };
3284 |
3285 | static uint32_t
3286 | create_streaming_reassembly_id(void)
3287 | {
3288 | static uint32_t global_streaming_reassembly_id = 0;
3289 | return ++global_streaming_reassembly_id;
3290 | }
3291 |
3292 | streaming_reassembly_info_t*
3293 | streaming_reassembly_info_new(void)
3294 | {
3295 | return wmem_new0(wmem_file_scope(), streaming_reassembly_info_t)((streaming_reassembly_info_t*)wmem_alloc0((wmem_file_scope() ), sizeof(streaming_reassembly_info_t)));
3296 | }
3297 |
3298 | /* Following is an example of ProtoA and ProtoB protocols from the declaration of this function in 'reassemble.h':
3299 | *
3300 | * +------------------ A Multisegment PDU of ProtoB ----------------------+
3301 | * | |
3302 | * +--- ProtoA payload1 ---+ +- payload2 -+ +- Payload3 -+ +- Payload4 -+ +- ProtoA payload5 -+
3303 | * | EoMSP | OmNFP | BoMSP | | MoMSP | | MoMSP | | MoMSP | | EoMSP | BoMSP |
3304 | * +-------+-------+-------+ +------------+ +------------+ +------------+ +---------+---------+
3305 | * | |
3306 | * +----------------------------------------------------------------------+
3307 | *
3308 | * For a ProtoA payload composed of EoMSP + OmNFP + BoMSP will call fragment_add() twice on EoMSP and BoMSP; and call
3309 | * process_reassembled_data() once for generating tvb of a MSP to which EoMSP belongs; and call subdissector twice on
3310 | * reassembled MSP of EoMSP and OmNFP + BoMSP. After that finds BoMSP is a beginning of a MSP at first scan.
3311 | *
3312 | * The rules are:
3313 | *
3314 | * - If a ProtoA payload contains EoMSP, we will need call fragment_add(), process_reassembled_data() and subdissector
3315 | * once on it to end a MSP. (May run twice or more times at first scan, because subdissector may only return the
3316 | * head length of message by pinfo->desegment_len. We need run second time for subdissector to determine the length
3317 | * of entire message).
3318 | *
3319 | * - If a ProtoA payload contains OmNFP, we will need only call subdissector once on it. The subdissector need dissect
3320 | * all non-fragment PDUs in it. (no desegment_len should output)
3321 | *
3322 | * - If a ProtoA payload contains BoMSP, we will need call subdissector once on BoMSP or OmNFP+BoMSP (because unknown
3323 | * during first scan). The subdissector will output desegment_len (!= 0). Then we will call fragment_add()
3324 | * with a new reassembly id on BoMSP for starting a new MSP.
3325 | *
3326 | * - If a ProtoA payload only contains MoMSP (entire payload is part of a MSP), we will only call fragment_add() once
3327 | * or twice (at first scan) on it. The subdissector will not be called.
3328 | *
3329 | * In this implementation, only multisegment PDUs are recorded in multisegment_pdus map keyed by the numbers (uint64_t)
3330 | * of frames belongs to MSPs. Each MSP in the map has a pointer referred to previous MSP, because we may need
3331 | * two MSPs to dissect a ProtoA payload that contains EoMSP + BoMSP at the same time. The multisegment_pdus map is built
3332 | * during first scan (pinfo->visited == false) with help of prev_deseg_len and last_msp fields of streaming_reassembly_info_t
3333 | * for each direction of a ProtoA STREAM. The prev_deseg_len record how many bytes of subsequent ProtoA payloads belong to
3334 | * previous PDU during first scan. The last_msp member of streaming_reassembly_info_t is always point to last MSP which
3335 | * is created during scan previous or early ProtoA payloads. Since subdissector might return only the head length of entire
3336 | * message (by pinfo->desegment_len) when there is not enough data to determine the message length, we need to reopen
3337 | * reassembly fragments for adding more bytes during scanning the next ProtoA payload. We have to use fragment_add()
3338 | * instead of fragment_add_check() or fragment_add_seq_next().
3339 | *
3340 | * Read more: please refer to comments of the declaration of this function in 'reassemble.h'.
3341 | */
3342 | int
3343 | reassemble_streaming_data_and_call_subdissector(
3344 | tvbuff_t* tvb, packet_info* pinfo, unsigned offset, int length,
3345 | proto_tree* segment_tree, proto_tree* reassembled_tree, reassembly_table streaming_reassembly_table,
3346 | streaming_reassembly_info_t* reassembly_info, uint64_t cur_frame_num,
3347 | dissector_handle_t subdissector_handle, proto_tree* subdissector_tree, void* subdissector_data,
3348 | const char* label, const fragment_items* frag_hf_items, int hf_segment_data
3349 | )
3350 | {
3351 | int orig_length = length;
3352 | int datalen = 0;
3353 | int bytes_belong_to_prev_msp = 0; /* bytes belong to previous MSP */
3354 | uint32_t reassembly_id = 0, frag_offset = 0;
3355 | fragment_head* head = NULL((void*)0);
3356 | bool_Bool need_more = false0;
3357 | bool_Bool found_BoMSP = false0;
3358 | multisegment_pdu_t* cur_msp = NULL((void*)0), * prev_msp = NULL((void*)0);
3359 | uint16_t save_can_desegment;
3360 | int save_desegment_offset;
3361 | uint32_t save_desegment_len;
3362 | uint64_t* frame_ptr;
3363 |
3364 | save_can_desegment = pinfo->can_desegment;
3365 | save_desegment_offset = pinfo->desegment_offset;
3366 | save_desegment_len = pinfo->desegment_len;
3367 |
3368 | /* calculate how many bytes of this payload belongs to previous MSP (EoMSP) */
3369 | if (!PINFO_FD_VISITED(pinfo)((pinfo)->fd->visited)) {
3370 | /* this is first scan */
3371 | if (reassembly_info->prev_deseg_len == DESEGMENT_ONE_MORE_SEGMENT0x0fffffff) {
3372 | /* assuming the entire tvb belongs to the previous MSP */
3373 | bytes_belong_to_prev_msp = length;
3374 | reassembly_info->prev_deseg_len = length;
3375 | } else if (reassembly_info->prev_deseg_len > 0) {
3376 | /* part or all of current payload belong to previous MSP */
3377 | bytes_belong_to_prev_msp = MIN(reassembly_info->prev_deseg_len, length)(((reassembly_info->prev_deseg_len) < (length)) ? (reassembly_info ->prev_deseg_len) : (length));
3378 | reassembly_info->prev_deseg_len -= bytes_belong_to_prev_msp;
3379 | need_more = (reassembly_info->prev_deseg_len > 0);
3380 | } /* else { beginning of a new PDU (might be a NFP or MSP) } */
3381 |
3382 | if (bytes_belong_to_prev_msp > 0) {
3383 | DISSECTOR_ASSERT(reassembly_info->last_msp != NULL)((void) ((reassembly_info->last_msp != ((void*)0)) ? (void )0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\"" , "epan/reassemble.c", 3383, "reassembly_info->last_msp != ((void*)0)" ))));
3384 | reassembly_id = reassembly_info->last_msp->streaming_reassembly_id;
3385 | frag_offset = reassembly_info->last_msp->length;
3386 | if (reassembly_info->frame_num_frag_offset_map == NULL((void*)0)) {
3387 | reassembly_info->frame_num_frag_offset_map = wmem_map_new(wmem_file_scope(), g_int64_hash, g_int64_equal);
3388 | }
3389 | frame_ptr = (uint64_t*)wmem_memdup(wmem_file_scope(), &cur_frame_num, sizeof(uint64_t));
3390 | wmem_map_insert(reassembly_info->frame_num_frag_offset_map, frame_ptr, GUINT_TO_POINTER(frag_offset)((gpointer) (gulong) (frag_offset)));
3391 | /* This payload contains the data of previous msp, so we point to it. That may be overridden late. */
3392 | wmem_map_insert(reassembly_info->multisegment_pdus, frame_ptr, reassembly_info->last_msp);
3393 | }
3394 | } else {
3395 | /* not first scan, use information of multisegment_pdus built during first scan */
3396 | if (reassembly_info->multisegment_pdus) {
3397 | cur_msp = (multisegment_pdu_t*)wmem_map_lookup(reassembly_info->multisegment_pdus, &cur_frame_num);
3398 | }
3399 | if (cur_msp) {
3400 | if (cur_msp->first_frame == cur_frame_num) {
3401 | /* Current payload contains a beginning of a MSP. (BoMSP)
3402 | * The cur_msp contains information about the beginning MSP.
3403 | * If prev_msp is not null, that means this payload also contains
3404 | * the last part of previous MSP. (EoMSP) */
3405 | prev_msp = cur_msp->prev_msp;
3406 | } else {
3407 | /* Current payload is not a first frame of a MSP (not include BoMSP). */
3408 | prev_msp = cur_msp;
3409 | cur_msp = NULL((void*)0);
3410 | }
3411 | }
3412 |
3413 | if (prev_msp && prev_msp->last_frame >= cur_frame_num) {
3414 | if (prev_msp->last_frame == cur_frame_num) {
3415 | /* this payload contains part of previous MSP (contains EoMSP) */
3416 | bytes_belong_to_prev_msp = prev_msp->end_offset_at_last_frame - offset;
3417 | } else { /* if (prev_msp->last_frame > cur_frame_num) */
3418 | /* this payload all belongs to previous MSP */
3419 | bytes_belong_to_prev_msp = length;
3420 | need_more = true1;
3421 | }
3422 | reassembly_id = prev_msp->streaming_reassembly_id;
3423 | }
3424 | if (reassembly_info->frame_num_frag_offset_map) {
3425 | frag_offset = GPOINTER_TO_UINT(wmem_map_lookup(reassembly_info->frame_num_frag_offset_map, &cur_frame_num))((guint) (gulong) (wmem_map_lookup(reassembly_info->frame_num_frag_offset_map , &cur_frame_num)));
3426 | }
3427 | }
3428 |
3429 | /* handling EoMSP or MoMSP (entire payload being middle part of a MSP) */
3430 | while (bytes_belong_to_prev_msp > 0) {
3431 | tvbuff_t* reassembled_tvb = NULL((void*)0);
3432 | DISSECTOR_ASSERT(reassembly_id > 0)((void) ((reassembly_id > 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 3432, "reassembly_id > 0"))));
3433 | pinfo->can_desegment = 2; /* this will be decreased one while passing to subdissector */
3434 | pinfo->desegment_offset = 0;
3435 | pinfo->desegment_len = 0;
3436 |
3437 | head = fragment_add(&streaming_reassembly_table, tvb, offset, pinfo, reassembly_id, NULL((void*)0),
3438 | frag_offset, bytes_belong_to_prev_msp, need_more);
3439 |
3440 | if (head) {
3441 | if (frag_hf_items->hf_reassembled_in) {
3442 | proto_item_set_generated(
3443 | proto_tree_add_uint(segment_tree, *(frag_hf_items->hf_reassembled_in), tvb, offset,
3444 | bytes_belong_to_prev_msp, head->reassembled_in)
3445 | );
3446 | }
3447 |
3448 | if (!need_more) {
3449 | reassembled_tvb = process_reassembled_data(tvb, offset, pinfo,
3450 | wmem_strdup_printf(pinfo->pool, "Reassembled %s", label),
3451 | head, frag_hf_items, NULL((void*)0), reassembled_tree);
3452 | }
3453 | }
3454 |
3455 | proto_tree_add_bytes_format(segment_tree, hf_segment_data, tvb, offset,
3456 | bytes_belong_to_prev_msp, NULL((void*)0), "%s Segment data (%u byte%s)", label,
3457 | bytes_belong_to_prev_msp, plurality(bytes_belong_to_prev_msp, "", "s")((bytes_belong_to_prev_msp) == 1 ? ("") : ("s")));
3458 |
3459 | if (reassembled_tvb) {
3460 | /* normally, this stage will dissect one or more completed pdus */
3461 | /* Note, don't call_dissector_with_data because sometime the pinfo->curr_layer_num will changed
3462 | * after calling that will make reassembly failed! */
3463 | call_dissector_only(subdissector_handle, reassembled_tvb, pinfo, subdissector_tree, subdissector_data);
3464 | }
3465 |
3466 | if (pinfo->desegment_len) {
3467 | /* that must only happen during first scan the reassembly_info->prev_deseg_len might be only the
3468 | * head length of entire message. */
3469 | DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo))((void) ((!((pinfo)->fd->visited)) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 3469, "!((pinfo)->fd->visited)"))));
3470 | DISSECTOR_ASSERT_HINT(pinfo->desegment_len != DESEGMENT_UNTIL_FIN, "Subdissector MUST NOT "((void) ((pinfo->desegment_len != 0x0ffffffe) ? (void)0 : ( proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3472, "pinfo->desegment_len != 0x0ffffffe" , "Subdissector MUST NOT " "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to " " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined." ))))
3471 | "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to "((void) ((pinfo->desegment_len != 0x0ffffffe) ? (void)0 : ( proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3472, "pinfo->desegment_len != 0x0ffffffe" , "Subdissector MUST NOT " "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to " " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined." ))))
3472 | " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined.")((void) ((pinfo->desegment_len != 0x0ffffffe) ? (void)0 : ( proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3472, "pinfo->desegment_len != 0x0ffffffe" , "Subdissector MUST NOT " "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to " " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined." ))));
3473 |
3474 | if (pinfo->desegment_offset > 0) {
3475 | DISSECTOR_ASSERT_HINT(pinfo->desegment_offset > reassembly_info->last_msp->length((void) ((pinfo->desegment_offset > reassembly_info-> last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp ) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3479, "pinfo->desegment_offset > reassembly_info->last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp" , wmem_strdup_printf(pinfo->pool, "Subdissector MUST NOT set pinfo->desegment_offset(%d) in previous or next part of MSP, must between (%d, %d)." , pinfo->desegment_offset, reassembly_info->last_msp-> length, reassembly_info->last_msp->length + bytes_belong_to_prev_msp )))))
3476 | && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp,((void) ((pinfo->desegment_offset > reassembly_info-> last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp ) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3479, "pinfo->desegment_offset > reassembly_info->last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp" , wmem_strdup_printf(pinfo->pool, "Subdissector MUST NOT set pinfo->desegment_offset(%d) in previous or next part of MSP, must between (%d, %d)." , pinfo->desegment_offset, reassembly_info->last_msp-> length, reassembly_info->last_msp->length + bytes_belong_to_prev_msp )))))
3477 | wmem_strdup_printf(pinfo->pool,((void) ((pinfo->desegment_offset > reassembly_info-> last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp ) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3479, "pinfo->desegment_offset > reassembly_info->last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp" , wmem_strdup_printf(pinfo->pool, "Subdissector MUST NOT set pinfo->desegment_offset(%d) in previous or next part of MSP, must between (%d, %d)." , pinfo->desegment_offset, reassembly_info->last_msp-> length, reassembly_info->last_msp->length + bytes_belong_to_prev_msp )))))
3478 | "Subdissector MUST NOT set pinfo->desegment_offset(%d) in previous or next part of MSP, must between (%d, %d).",((void) ((pinfo->desegment_offset > reassembly_info-> last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp ) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3479, "pinfo->desegment_offset > reassembly_info->last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp" , wmem_strdup_printf(pinfo->pool, "Subdissector MUST NOT set pinfo->desegment_offset(%d) in previous or next part of MSP, must between (%d, %d)." , pinfo->desegment_offset, reassembly_info->last_msp-> length, reassembly_info->last_msp->length + bytes_belong_to_prev_msp )))))
3479 | pinfo->desegment_offset, reassembly_info->last_msp->length, reassembly_info->last_msp->length + bytes_belong_to_prev_msp))((void) ((pinfo->desegment_offset > reassembly_info-> last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp ) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3479, "pinfo->desegment_offset > reassembly_info->last_msp->length && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp" , wmem_strdup_printf(pinfo->pool, "Subdissector MUST NOT set pinfo->desegment_offset(%d) in previous or next part of MSP, must between (%d, %d)." , pinfo->desegment_offset, reassembly_info->last_msp-> length, reassembly_info->last_msp->length + bytes_belong_to_prev_msp )))));
3480 |
3481 | /* shorten the bytes_belong_to_prev_msp and just truncate the reassembled tvb */
3482 | bytes_belong_to_prev_msp = pinfo->desegment_offset - reassembly_info->last_msp->length;
3483 | fragment_truncate(&streaming_reassembly_table, pinfo, reassembly_id, NULL((void*)0), pinfo->desegment_offset);
3484 | found_BoMSP = true1;
3485 | } else {
3486 | if (pinfo->desegment_len == DESEGMENT_ONE_MORE_SEGMENT0x0fffffff) {
3487 | /* just need more bytes, all remaining bytes belongs to previous MSP (to run fragment_add again) */
3488 | bytes_belong_to_prev_msp = length;
3489 | }
3490 |
3491 | /* Remove the data added by previous fragment_add(), and reopen fragments for adding more bytes. */
3492 | fragment_truncate(&streaming_reassembly_table, pinfo, reassembly_id, NULL((void*)0), reassembly_info->last_msp->length);
3493 | fragment_set_partial_reassembly(&streaming_reassembly_table, pinfo, reassembly_id, NULL((void*)0));
3494 |
3495 | reassembly_info->prev_deseg_len = bytes_belong_to_prev_msp + pinfo->desegment_len;
3496 | bytes_belong_to_prev_msp = MIN(reassembly_info->prev_deseg_len, length)(((reassembly_info->prev_deseg_len) < (length)) ? (reassembly_info ->prev_deseg_len) : (length));
3497 | reassembly_info->prev_deseg_len -= bytes_belong_to_prev_msp;
3498 | need_more = (reassembly_info->prev_deseg_len > 0);
3499 | continue;
3500 | }
3501 | }
3502 |
3503 | if (pinfo->desegment_len == 0 || found_BoMSP) {
3504 | /* We will arrive here, only when the MSP is defragmented and dissected or this
3505 | * payload all belongs to previous MSP (only fragment_add() with need_more=true called)
3506 | * or BoMSP is parsed while pinfo->desegment_offset > 0 and pinfo->desegment_len != 0
3507 | */
3508 | offset += bytes_belong_to_prev_msp;
3509 | length -= bytes_belong_to_prev_msp;
3510 | DISSECTOR_ASSERT(length >= 0)((void) ((length >= 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 3510, "length >= 0"))));
3511 | if (!PINFO_FD_VISITED(pinfo)((pinfo)->fd->visited)) {
3512 | reassembly_info->last_msp->length += bytes_belong_to_prev_msp;
3513 | }
3514 |
3515 | if (!PINFO_FD_VISITED(pinfo)((pinfo)->fd->visited) && reassembled_tvb) {
3516 | /* completed current msp */
3517 | reassembly_info->last_msp->last_frame = cur_frame_num;
3518 | reassembly_info->last_msp->end_offset_at_last_frame = offset;
3519 | reassembly_info->prev_deseg_len = pinfo->desegment_len;
3520 | }
3521 | bytes_belong_to_prev_msp = 0; /* break */
3522 | }
3523 | }
3524 |
3525 | /* to find and handle OmNFP, and find BoMSP at first scan. */
3526 | if (length > 0 && !found_BoMSP) {
3527 | if (!PINFO_FD_VISITED(pinfo)((pinfo)->fd->visited)) {
3528 | /* It is first scan, to dissect remaining bytes to find whether it is OmNFP only, or BoMSP only or OmNFP + BoMSP. */
3529 | datalen = length;
3530 | DISSECTOR_ASSERT(cur_msp == NULL)((void) ((cur_msp == ((void*)0)) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 3530, "cur_msp == ((void*)0)"))));
3531 | } else {
3532 | /* Not first scan */
3533 | if (cur_msp) {
3534 | /* There's a BoMSP. Let's calculate the length of OmNFP between EoMSP and BoMSP */
3535 | datalen = cur_msp->start_offset_at_first_frame - offset; /* if result is zero that means no OmNFP */
3536 | } else {
3537 | /* This payload is not a beginning of MSP. The remaining bytes all belong to OmNFP without BoMSP */
3538 | datalen = length;
3539 | }
3540 | }
3541 | DISSECTOR_ASSERT(datalen >= 0)((void) ((datalen >= 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 3541, "datalen >= 0"))));
3542 |
3543 | /* Dissect the remaining of this payload. If (datalen == 0) means remaining only have one BoMSP without OmNFP. */
3544 | if (datalen > 0) {
3545 | /* we dissect if it is not dissected before or it is a non-fragment pdu (between two multisegment pdus) */
3546 | pinfo->can_desegment = 2;
3547 | pinfo->desegment_offset = 0;
3548 | pinfo->desegment_len = 0;
3549 |
3550 | call_dissector_only(subdissector_handle, tvb_new_subset_length(tvb, offset, datalen),
3551 | pinfo, subdissector_tree, subdissector_data);
3552 |
3553 | if (pinfo->desegment_len) {
3554 | DISSECTOR_ASSERT_HINT(pinfo->desegment_len != DESEGMENT_UNTIL_FIN, "Subdissector MUST NOT "((void) ((pinfo->desegment_len != 0x0ffffffe) ? (void)0 : ( proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3556, "pinfo->desegment_len != 0x0ffffffe" , "Subdissector MUST NOT " "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to " " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined." ))))
3555 | "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to "((void) ((pinfo->desegment_len != 0x0ffffffe) ? (void)0 : ( proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3556, "pinfo->desegment_len != 0x0ffffffe" , "Subdissector MUST NOT " "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to " " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined." ))))
3556 | " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined.")((void) ((pinfo->desegment_len != 0x0ffffffe) ? (void)0 : ( proto_report_dissector_bug("%s:%u: failed assertion \"%s\" (%s)" , "epan/reassemble.c", 3556, "pinfo->desegment_len != 0x0ffffffe" , "Subdissector MUST NOT " "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to " " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined." ))));
3557 | /* only happen during first scan */
3558 | DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo) && datalen == length)((void) ((!((pinfo)->fd->visited) && datalen == length) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\"" , "epan/reassemble.c", 3558, "!((pinfo)->fd->visited) && datalen == length" ))));
3559 | offset += pinfo->desegment_offset;
3560 | length -= pinfo->desegment_offset;
3561 | } else {
3562 | /* all remaining bytes are consumed by subdissector */
3563 | offset += datalen;
3564 | length -= datalen;
3565 | }
3566 | if (!PINFO_FD_VISITED(pinfo)((pinfo)->fd->visited)) {
3567 | reassembly_info->prev_deseg_len = pinfo->desegment_len;
3568 | }
3569 | } /* else all remaining bytes (BoMSP) belong to a new MSP */
3570 | DISSECTOR_ASSERT(length >= 0)((void) ((length >= 0) ? (void)0 : (proto_report_dissector_bug ("%s:%u: failed assertion \"%s\"", "epan/reassemble.c", 3570, "length >= 0"))));
3571 | }
3572 |
3573 | /* handling BoMSP */
3574 | if (length > 0) {
3575 | col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "[%s segment of a reassembled PDU] ", label);
3576 | if (!PINFO_FD_VISITED(pinfo)((pinfo)->fd->visited)) {
3577 | /* create a msp for current frame during first scan */
3578 | cur_msp = wmem_new0(wmem_file_scope(), multisegment_pdu_t)((multisegment_pdu_t*)wmem_alloc0((wmem_file_scope()), sizeof (multisegment_pdu_t)));
3579 | cur_msp->first_frame = cur_frame_num;
3580 | cur_msp->last_frame = UINT64_MAX(18446744073709551615UL);
3581 | cur_msp->start_offset_at_first_frame = offset;
3582 | cur_msp->length = length;
3583 | cur_msp->streaming_reassembly_id = reassembly_id = create_streaming_reassembly_id();
3584 | cur_msp->prev_msp = reassembly_info->last_msp;
3585 | reassembly_info->last_msp = cur_msp;
3586 | if (reassembly_info->multisegment_pdus == NULL((void*)0)) {
3587 | reassembly_info->multisegment_pdus = wmem_map_new(wmem_file_scope(), g_int64_hash, g_int64_equal);
3588 | }
3589 | frame_ptr = (uint64_t*)wmem_memdup(wmem_file_scope(), &cur_frame_num, sizeof(uint64_t));
3590 | wmem_map_insert(reassembly_info->multisegment_pdus, frame_ptr, cur_msp);
3591 | } else {
3592 | DISSECTOR_ASSERT(cur_msp && cur_msp->start_offset_at_first_frame == offset)((void) ((cur_msp && cur_msp->start_offset_at_first_frame == offset) ? (void)0 : (proto_report_dissector_bug("%s:%u: failed assertion \"%s\"" , "epan/reassemble.c", 3592, "cur_msp && cur_msp->start_offset_at_first_frame == offset" ))));
3593 | reassembly_id = cur_msp->streaming_reassembly_id;
3594 | }
3595 | /* add first fragment of the new MSP to reassembly table */
3596 | head = fragment_add(&streaming_reassembly_table, tvb, offset, pinfo, reassembly_id,
3597 | NULL((void*)0), 0, length, true1);
3598 |
3599 | if (head && frag_hf_items->hf_reassembled_in) {
3600 | proto_item_set_generated(
3601 | proto_tree_add_uint(segment_tree, *(frag_hf_items->hf_reassembled_in),
3602 | tvb, offset, length, head->reassembled_in)
3603 | );
3604 | }
3605 | proto_tree_add_bytes_format(segment_tree, hf_segment_data, tvb, offset, length,
3606 | NULL((void*)0), "%s Segment data (%u byte%s)", label, length, plurality(length, "", "s")((length) == 1 ? ("") : ("s")));
3607 | }
3608 |
3609 | pinfo->can_desegment = save_can_desegment;
3610 | pinfo->desegment_offset = save_desegment_offset;
3611 | pinfo->desegment_len = save_desegment_len;
3612 |
3613 | return orig_length;
3614 | }
3615 |
3616 | int
3617 | additional_bytes_expected_to_complete_reassembly(streaming_reassembly_info_t* reassembly_info)
3618 | {
3619 | return reassembly_info->prev_deseg_len;
3620 | }
3621 |
3622 | /*
3623 | * Editor modelines - https://www.wireshark.org/tools/modelines.html
3624 | *
3625 | * Local variables:
3626 | * c-basic-offset: 8
3627 | * tab-width: 8
3628 | * indent-tabs-mode: t
3629 | * End:
3630 | *
3631 | * vi: set shiftwidth=8 tabstop=8 noexpandtab:
3632 | * :indentSize=8:tabSize=8:noTabs=false:
3633 | */