Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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