Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / reassemble.c
blob5444a7704d7ab43a428965399a9fea2ca351ebb2
1 /* reassemble.c
2 * Routines for {fragment,segment} reassembly
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
11 #include "config.h"
13 #include <string.h>
15 #include <epan/packet.h>
16 #include <epan/exceptions.h>
17 #include <epan/reassemble.h>
18 #include <epan/tvbuff-int.h>
20 #include <wsutil/str_util.h>
21 #include <wsutil/ws_assert.h>
24 * Functions for reassembly tables where the endpoint addresses, and a
25 * fragment ID, are used as the key.
27 typedef struct _fragment_addresses_key {
28 address src;
29 address dst;
30 uint32_t id;
31 } fragment_addresses_key;
33 GList* reassembly_table_list;
35 static unsigned
36 fragment_addresses_hash(const void *k)
38 const fragment_addresses_key* key = (const fragment_addresses_key*) k;
39 unsigned hash_val;
41 int i;
44 hash_val = 0;
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.
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];
56 hash_val += key->id;
58 return hash_val;
61 static int
62 fragment_addresses_equal(const void *k1, const void *k2)
64 const fragment_addresses_key* key1 = (const fragment_addresses_key*) k1;
65 const fragment_addresses_key* key2 = (const fragment_addresses_key*) k2;
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.
72 return (key1->id == key2->id) &&
73 (addresses_equal(&key1->src, &key2->src)) &&
74 (addresses_equal(&key1->dst, &key2->dst));
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.
82 static void *
83 fragment_addresses_temporary_key(const packet_info *pinfo, const uint32_t id,
84 const void *data _U_)
86 fragment_addresses_key *key = g_slice_new(fragment_addresses_key);
89 * Do a shallow copy of the addresses.
91 copy_address_shallow(&key->src, &pinfo->src);
92 copy_address_shallow(&key->dst, &pinfo->dst);
93 key->id = id;
95 return (void *)key;
99 * Create a fragment key for permanent use; it must point to persistent
100 * data, so that it can be used to add entries.
102 static void *
103 fragment_addresses_persistent_key(const packet_info *pinfo, const uint32_t id,
104 const void *data _U_)
106 fragment_addresses_key *key = g_slice_new(fragment_addresses_key);
109 * Do a deep copy of the addresses.
111 copy_address(&key->src, &pinfo->src);
112 copy_address(&key->dst, &pinfo->dst);
113 key->id = id;
115 return (void *)key;
118 static void
119 fragment_addresses_free_temporary_key(void *ptr)
121 fragment_addresses_key *key = (fragment_addresses_key *)ptr;
122 g_slice_free(fragment_addresses_key, key);
125 static void
126 fragment_addresses_free_persistent_key(void *ptr)
128 fragment_addresses_key *key = (fragment_addresses_key *)ptr;
130 if(key){
132 * Free up the copies of the addresses from the old key.
134 free_address(&key->src);
135 free_address(&key->dst);
137 g_slice_free(fragment_addresses_key, key);
141 const reassembly_table_functions
142 addresses_reassembly_table_functions = {
143 fragment_addresses_hash,
144 fragment_addresses_equal,
145 fragment_addresses_temporary_key,
146 fragment_addresses_persistent_key,
147 fragment_addresses_free_temporary_key,
148 fragment_addresses_free_persistent_key
152 * Functions for reassembly tables where the endpoint addresses and ports,
153 * and a fragment ID, are used as the key.
155 typedef struct _fragment_addresses_ports_key {
156 address src_addr;
157 address dst_addr;
158 uint32_t src_port;
159 uint32_t dst_port;
160 uint32_t id;
161 } fragment_addresses_ports_key;
163 static unsigned
164 fragment_addresses_ports_hash(const void *k)
166 const fragment_addresses_ports_key* key = (const fragment_addresses_ports_key*) k;
167 unsigned hash_val;
169 int i;
172 hash_val = 0;
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.
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;
186 hash_val += key->id;
188 return hash_val;
191 static int
192 fragment_addresses_ports_equal(const void *k1, const void *k2)
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;
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.
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);
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.
214 static void *
215 fragment_addresses_ports_temporary_key(const packet_info *pinfo, const uint32_t id,
216 const void *data _U_)
218 fragment_addresses_ports_key *key = g_slice_new(fragment_addresses_ports_key);
221 * Do a shallow copy of the addresses.
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;
229 return (void *)key;
233 * Create a fragment key for permanent use; it must point to persistent
234 * data, so that it can be used to add entries.
236 static void *
237 fragment_addresses_ports_persistent_key(const packet_info *pinfo,
238 const uint32_t id, const void *data _U_)
240 fragment_addresses_ports_key *key = g_slice_new(fragment_addresses_ports_key);
243 * Do a deep copy of the addresses.
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;
251 return (void *)key;
254 static void
255 fragment_addresses_ports_free_temporary_key(void *ptr)
257 fragment_addresses_ports_key *key = (fragment_addresses_ports_key *)ptr;
258 g_slice_free(fragment_addresses_ports_key, key);
261 static void
262 fragment_addresses_ports_free_persistent_key(void *ptr)
264 fragment_addresses_ports_key *key = (fragment_addresses_ports_key *)ptr;
266 if(key){
268 * Free up the copies of the addresses from the old key.
270 free_address(&key->src_addr);
271 free_address(&key->dst_addr);
273 g_slice_free(fragment_addresses_ports_key, key);
277 const reassembly_table_functions
278 addresses_ports_reassembly_table_functions = {
279 fragment_addresses_ports_hash,
280 fragment_addresses_ports_equal,
281 fragment_addresses_ports_temporary_key,
282 fragment_addresses_ports_persistent_key,
283 fragment_addresses_ports_free_temporary_key,
284 fragment_addresses_ports_free_persistent_key
287 typedef struct _reassembled_key {
288 uint32_t id;
289 uint32_t frame;
290 } reassembled_key;
292 static int
293 reassembled_equal(const void *k1, const void *k2)
295 const reassembled_key* key1 = (const reassembled_key*) k1;
296 const reassembled_key* key2 = (const reassembled_key*) k2;
299 * We assume that the frame numbers are unlikely to be equal,
300 * so we check them first.
302 return key1->frame == key2->frame && key1->id == key2->id;
305 static unsigned
306 reassembled_hash(const void *k)
308 const reassembled_key* key = (const reassembled_key*) k;
310 return key->frame;
313 static void
314 reassembled_key_free(void *ptr)
316 g_slice_free(reassembled_key, (reassembled_key *)ptr);
320 * For a fragment hash table entry, free the associated fragments.
321 * The entry value (fd_chain) is freed herein and the entry is freed
322 * when the key freeing routine is called (as a consequence of returning
323 * true from this function).
325 static gboolean
326 free_all_fragments(void *key_arg _U_, void *value, void *user_data _U_)
328 fragment_head *fd_head;
329 fragment_item *fd_i = NULL, *tmp_fd;
331 /* g_hash_table_new_full() was used to supply a function
332 * to free the key and anything to which it points
334 fd_head = (fragment_head *)value;
335 if (fd_head != NULL) {
336 fd_i = fd_head->next;
337 if(fd_head->tvb_data && !(fd_head->flags&FD_SUBSET_TVB))
338 tvb_free(fd_head->tvb_data);
339 g_slice_free(fragment_head, fd_head);
342 for (; fd_i != NULL; fd_i = tmp_fd) {
343 tmp_fd=fd_i->next;
345 if(fd_i->tvb_data && !(fd_i->flags&FD_SUBSET_TVB))
346 tvb_free(fd_i->tvb_data);
347 g_slice_free(fragment_item, fd_i);
350 return TRUE;
353 /* ------------------------- */
354 static fragment_head *new_head(const uint32_t flags)
356 fragment_head *fd_head;
357 /* If head/first structure in list only holds no other data than
358 * 'datalen' then we don't have to change the head of the list
359 * even if we want to keep it sorted
361 fd_head=g_slice_new0(fragment_head);
363 fd_head->flags=flags;
364 return fd_head;
368 * For a reassembled-packet hash table entry, free the fragment data
369 * to which the value refers. (The key is freed by reassembled_key_free.)
371 static void
372 free_fd_head(fragment_head *fd_head)
374 fragment_item *fd_i, *tmp;
376 if (fd_head->flags & FD_SUBSET_TVB)
377 fd_head->tvb_data = NULL;
378 if (fd_head->tvb_data)
379 tvb_free(fd_head->tvb_data);
380 for (fd_i = fd_head->next; fd_i; fd_i = tmp) {
381 tmp = fd_i->next;
382 if (fd_i->flags & FD_SUBSET_TVB)
383 fd_i->tvb_data = NULL;
384 if (fd_i->tvb_data) {
385 tvb_free(fd_i->tvb_data);
387 g_slice_free(fragment_item, fd_i);
389 g_slice_free(fragment_head, fd_head);
392 static void
393 unref_fd_head(void *data)
395 fragment_head *fd_head = (fragment_head *) data;
396 fd_head->ref_count--;
398 if (fd_head->ref_count == 0) {
399 free_fd_head(fd_head);
403 static void
404 reassembled_table_insert(GHashTable *reassembled_table, reassembled_key *key, fragment_head *fd_head)
406 fragment_head *old_fd_head;
407 fd_head->ref_count++;
408 if ((old_fd_head = g_hash_table_lookup(reassembled_table, key)) != NULL) {
409 if (old_fd_head->ref_count == 1) {
410 /* We're replacing the last entry in the reassembled
411 * table for an old reassembly. Does it have a tvb?
412 * We might still be using that tvb's memory for an
413 * address via set_address_tvb(). (See #19094.)
415 if (old_fd_head->tvb_data && fd_head->tvb_data) {
416 /* Free it when the new tvb is freed */
417 tvb_set_child_real_data_tvbuff(fd_head->tvb_data, old_fd_head->tvb_data);
419 /* XXX: Set the old data to NULL regardless. If we
420 * have old data but not new data, that is odd (we're
421 * replacing a reassembly with tvb data with something
422 * with no tvb data, possibly because a zero length or
423 * null tvb was passed into a defragment function,
424 * which is a dissector bug.)
425 * This leaks the tvb data if we couldn't add it to
426 * a new tvb's chain, but we might not be able to free
427 * it yet if set_address_tvb() was used.
429 old_fd_head->tvb_data = NULL;
432 g_hash_table_insert(reassembled_table, key, fd_head);
435 typedef struct register_reassembly_table {
436 reassembly_table *table;
437 const reassembly_table_functions *funcs;
438 } register_reassembly_table_t;
441 * Register a reassembly table.
443 void
444 reassembly_table_register(reassembly_table *table,
445 const reassembly_table_functions *funcs)
447 register_reassembly_table_t* reg_table;
449 DISSECTOR_ASSERT(table);
450 DISSECTOR_ASSERT(funcs);
452 reg_table = g_new(register_reassembly_table_t,1);
454 reg_table->table = table;
455 reg_table->funcs = funcs;
457 reassembly_table_list = g_list_prepend(reassembly_table_list, reg_table);
461 * Initialize a reassembly table, with specified functions.
463 void
464 reassembly_table_init(reassembly_table *table,
465 const reassembly_table_functions *funcs)
467 if (table->temporary_key_func == NULL)
468 table->temporary_key_func = funcs->temporary_key_func;
469 if (table->persistent_key_func == NULL)
470 table->persistent_key_func = funcs->persistent_key_func;
471 if (table->free_temporary_key_func == NULL)
472 table->free_temporary_key_func = funcs->free_temporary_key_func;
473 if (table->fragment_table != NULL) {
475 * The fragment hash table exists.
477 * Remove all entries and free fragment data for each entry.
479 * The keys, and anything to which they point, are freed by
480 * calling the table's key freeing function. The values
481 * are freed in free_all_fragments().
483 g_hash_table_foreach_remove(table->fragment_table,
484 free_all_fragments, NULL);
485 } else {
486 /* The fragment table does not exist. Create it */
487 table->fragment_table = g_hash_table_new_full(funcs->hash_func,
488 funcs->equal_func, funcs->free_persistent_key_func, NULL);
491 if (table->reassembled_table != NULL) {
493 * The reassembled-packet hash table exists.
495 * Remove all entries and free reassembled packet
496 * data and key for each entry.
498 g_hash_table_remove_all(table->reassembled_table);
499 } else {
500 /* The fragment table does not exist. Create it */
501 table->reassembled_table = g_hash_table_new_full(reassembled_hash,
502 reassembled_equal, reassembled_key_free, unref_fd_head);
507 * Destroy a reassembly table.
509 void
510 reassembly_table_destroy(reassembly_table *table)
513 * Clear the function pointers.
515 table->temporary_key_func = NULL;
516 table->persistent_key_func = NULL;
517 table->free_temporary_key_func = NULL;
518 if (table->fragment_table != NULL) {
520 * The fragment hash table exists.
522 * Remove all entries and free fragment data for each entry.
524 * The keys, and anything to which they point, are freed by
525 * calling the table's key freeing function. The values
526 * are freed in free_all_fragments().
528 g_hash_table_foreach_remove(table->fragment_table,
529 free_all_fragments, NULL);
532 * Now destroy the hash table.
534 g_hash_table_destroy(table->fragment_table);
535 table->fragment_table = NULL;
537 if (table->reassembled_table != NULL) {
539 * The reassembled-packet hash table exists.
541 * Remove all entries and free reassembled packet
542 * data and key for each entry.
545 g_hash_table_remove_all(table->reassembled_table);
548 * Now destroy the hash table.
550 g_hash_table_destroy(table->reassembled_table);
551 table->reassembled_table = NULL;
556 * Look up an fd_head in the fragment table, optionally returning the key
557 * for it.
559 static fragment_head *
560 lookup_fd_head(reassembly_table *table, const packet_info *pinfo,
561 const uint32_t id, const void *data, void * *orig_keyp)
563 void *key;
564 void *value;
566 /* Create key to search hash with */
567 key = table->temporary_key_func(pinfo, id, data);
570 * Look up the reassembly in the fragment table.
572 if (!g_hash_table_lookup_extended(table->fragment_table, key, orig_keyp,
573 &value))
574 value = NULL;
575 /* Free the key */
576 table->free_temporary_key_func(key);
578 return (fragment_head *)value;
582 * Insert an fd_head into the fragment table, and return the key used.
584 static void *
585 insert_fd_head(reassembly_table *table, fragment_head *fd_head,
586 const packet_info *pinfo, const uint32_t id, const void *data)
588 void *key;
591 * We're going to use the key to insert the fragment,
592 * so make a persistent version of it.
594 key = table->persistent_key_func(pinfo, id, data);
595 g_hash_table_insert(table->fragment_table, key, fd_head);
596 return key;
599 /* This function cleans up the stored state and removes the reassembly data and
600 * (with one exception) all allocated memory for matching reassembly.
602 * The exception is :
603 * If the PDU was already completely reassembled, then the tvbuff containing the
604 * reassembled data WILL NOT be free()d, and the pointer to that tvbuff will be
605 * returned.
606 * Othervise the function will return NULL.
608 * So, if you call fragment_delete and it returns non-NULL, YOU are responsible
609 * to tvb_free() that tvbuff.
611 tvbuff_t *
612 fragment_delete(reassembly_table *table, const packet_info *pinfo,
613 const uint32_t id, const void *data)
615 fragment_head *fd_head;
616 fragment_item *fd;
617 tvbuff_t *fd_tvb_data=NULL;
618 void *key;
620 fd_head = lookup_fd_head(table, pinfo, id, data, &key);
621 if(fd_head==NULL){
622 /* We do not recognize this as a PDU we have seen before. return */
623 return NULL;
626 fd_tvb_data=fd_head->tvb_data;
627 /* loop over all partial fragments and free any tvbuffs */
628 for(fd=fd_head->next;fd;){
629 fragment_item *tmp_fd;
630 tmp_fd=fd->next;
632 if (fd->tvb_data && !(fd->flags & FD_SUBSET_TVB))
633 tvb_free(fd->tvb_data);
634 g_slice_free(fragment_item, fd);
635 fd=tmp_fd;
637 g_slice_free(fragment_head, fd_head);
638 g_hash_table_remove(table->fragment_table, key);
640 return fd_tvb_data;
643 /* This function is used to check if there is partial or completed reassembly state
644 * matching this packet. I.e. Is there reassembly going on or not for this packet?
646 fragment_head *
647 fragment_get(reassembly_table *table, const packet_info *pinfo,
648 const uint32_t id, const void *data)
650 return lookup_fd_head(table, pinfo, id, data, NULL);
653 fragment_head *
654 fragment_get_reassembled_id(reassembly_table *table, const packet_info *pinfo,
655 const uint32_t id)
657 fragment_head *fd_head;
658 reassembled_key key;
660 /* create key to search hash with */
661 key.frame = pinfo->num;
662 key.id = id;
663 fd_head = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &key);
665 return fd_head;
668 /* To specify the offset for the fragment numbering, the first fragment is added with 0, and
669 * afterwards this offset is set. All additional calls to off_seq_check will calculate
670 * the number in sequence in regards to the offset */
671 void
672 fragment_add_seq_offset(reassembly_table *table, const packet_info *pinfo, const uint32_t id,
673 const void *data, const uint32_t fragment_offset)
675 fragment_head *fd_head;
677 fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
678 if (!fd_head)
679 return;
681 /* Resetting the offset is not allowed */
682 if ( fd_head->fragment_nr_offset != 0 )
683 return;
685 fd_head->fragment_nr_offset = fragment_offset;
688 static void
689 update_first_gap(fragment_head *fd_head, fragment_item *inserted, bool multi_insert)
691 uint32_t frag_end = inserted->offset + inserted->len;
692 fragment_item *iter;
693 uint32_t contiguous;
695 if (inserted->offset > fd_head->contiguous_len) {
696 /* first inserted node is after first gap */
697 return;
698 } else if (fd_head->first_gap == NULL) {
699 /* we haven't seen first fragment yet */
700 if (inserted->offset != 0) {
701 /* inserted node is not first fragment */
702 return;
704 contiguous = inserted->len;
705 iter = inserted;
706 } else {
707 contiguous = MAX(fd_head->contiguous_len, frag_end);
708 iter = multi_insert ? inserted : fd_head->first_gap;
711 while (iter->next) {
712 if (iter->next->offset > contiguous) {
713 break;
715 iter = iter->next;
716 contiguous = MAX(contiguous, iter->offset + iter->len);
719 /* iter is either pointing to last fragment before gap or tail */
720 fd_head->first_gap = iter;
721 fd_head->contiguous_len = contiguous;
725 * Keeping first gap and contiguous length in sync significantly speeds up
726 * LINK_FRAG() when fragments in capture file are mostly ordered. However, when
727 * fragments are removed from the list, the first gap can point to fragments
728 * that were either moved to another list or freed. Therefore when any fragment
729 * before first gap is removed, the first gap (and contiguous length) must be
730 * invalidated.
732 static void fragment_reset_first_gap(fragment_head *fd_head)
734 fd_head->first_gap = NULL;
735 fd_head->contiguous_len = 0;
736 if (fd_head->next) {
737 bool multi_insert = (fd_head->next->next != NULL);
738 update_first_gap(fd_head, fd_head->next, multi_insert);
743 * Determines whether list modification requires first gap reset. On entry
744 * modified is NULL if all elements were removed, otherwise it points to
745 * element (reachable from fd_head) whose next pointer was changed.
747 static void fragment_items_removed(fragment_head *fd_head, fragment_item *modified)
749 if ((fd_head->first_gap == modified) ||
750 ((modified != NULL) && (modified->offset > fd_head->contiguous_len))) {
751 /* Removed elements were after first gap */
752 return;
754 fragment_reset_first_gap(fd_head);
758 * For use with fragment_add (and not the fragment_add_seq functions).
759 * When the reassembled result is wrong (perhaps it needs to be extended), this
760 * function clears any previous reassembly result, allowing the new reassembled
761 * length to be set again.
763 static void
764 fragment_reset_defragmentation(fragment_head *fd_head)
766 /* Caller must ensure that this function is only called when
767 * defragmentation is safe to undo. */
768 DISSECTOR_ASSERT(fd_head->flags & FD_DEFRAGMENTED);
770 for (fragment_item *fd_i = fd_head->next; fd_i; fd_i = fd_i->next) {
771 if (!fd_i->tvb_data) {
772 fd_i->tvb_data = tvb_new_subset_remaining(fd_head->tvb_data, fd_i->offset);
773 fd_i->flags |= FD_SUBSET_TVB;
775 fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
777 fd_head->flags &= ~(FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY|FD_DATALEN_SET);
778 fd_head->flags &= ~(FD_TOOLONGFRAGMENT|FD_MULTIPLETAILS);
779 fd_head->datalen = 0;
780 fd_head->reassembled_in = 0;
781 fd_head->reas_in_layer_num = 0;
784 /* This function can be used to explicitly set the total length (if known)
785 * for reassembly of a PDU.
786 * This is useful for reassembly of PDUs where one may have the total length specified
787 * in the first fragment instead of as for, say, IPv4 where a flag indicates which
788 * is the last fragment.
790 * Such protocols might fragment_add with a more_frags==true for every fragment
791 * and just tell the reassembly engine the expected total length of the reassembled data
792 * using fragment_set_tot_len immediately after doing fragment_add for the first packet.
794 * Note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment.
795 * i.e. since the block numbers start at 0, if we specify tot_len==2, that
796 * actually means we want to defragment 3 blocks, block 0, 1 and 2.
798 void
799 fragment_set_tot_len(reassembly_table *table, const packet_info *pinfo,
800 const uint32_t id, const void *data, const uint32_t tot_len)
802 fragment_head *fd_head;
803 fragment_item *fd;
804 uint32_t max_offset = 0;
806 fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
807 if (!fd_head)
808 return;
810 /* If we're setting a block sequence number, verify that it
811 * doesn't conflict with values set by existing fragments.
812 * XXX - eliminate this check?
814 if (fd_head->flags & FD_BLOCKSEQUENCE) {
815 for (fd = fd_head->next; fd; fd = fd->next) {
816 if (fd->offset > max_offset) {
817 max_offset = fd->offset;
818 if (max_offset > tot_len) {
819 fd_head->error = "Bad total reassembly block count";
820 THROW_MESSAGE(ReassemblyError, fd_head->error);
826 if (fd_head->flags & FD_DEFRAGMENTED) {
827 if (max_offset != tot_len) {
828 fd_head->error = "Defragmented complete but total length not satisfied";
829 THROW_MESSAGE(ReassemblyError, fd_head->error);
833 /* We got this far so the value is sane. */
834 fd_head->datalen = tot_len;
835 fd_head->flags |= FD_DATALEN_SET;
838 void
839 fragment_reset_tot_len(reassembly_table *table, const packet_info *pinfo,
840 const uint32_t id, const void *data, const uint32_t tot_len)
842 fragment_head *fd_head;
844 fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
845 if (!fd_head)
846 return;
849 * If FD_PARTIAL_REASSEMBLY is set, it would make the next fragment_add
850 * call set the reassembled length based on the fragment offset and
851 * length. As the length is known now, be sure to disable that magic.
853 fd_head->flags &= ~FD_PARTIAL_REASSEMBLY;
855 /* If the length is already as expected, there is nothing else to do. */
856 if (tot_len == fd_head->datalen)
857 return;
859 if (fd_head->flags & FD_DEFRAGMENTED) {
861 * Fragments were reassembled before, clear it to allow
862 * increasing the reassembled length.
864 fragment_reset_defragmentation(fd_head);
867 fd_head->datalen = tot_len;
868 fd_head->flags |= FD_DATALEN_SET;
871 void
872 fragment_truncate(reassembly_table *table, const packet_info *pinfo,
873 const uint32_t id, const void *data, const uint32_t tot_len)
876 tvbuff_t *old_tvb_data;
877 fragment_head *fd_head;
879 fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
880 if (!fd_head)
881 return;
883 /* Caller must ensure that this function is only called when
884 * we are defragmented. */
885 DISSECTOR_ASSERT(fd_head->flags & FD_DEFRAGMENTED);
888 * If FD_PARTIAL_REASSEMBLY is set, it would make the next fragment_add
889 * call set the reassembled length based on the fragment offset and
890 * length. As the length is known now, be sure to disable that magic.
892 fd_head->flags &= ~FD_PARTIAL_REASSEMBLY;
894 /* If the length is already as expected, there is nothing else to do. */
895 if (tot_len == fd_head->datalen)
896 return;
898 DISSECTOR_ASSERT(fd_head->datalen > tot_len);
900 old_tvb_data=fd_head->tvb_data;
901 fd_head->tvb_data = tvb_clone_offset_len(old_tvb_data, 0, tot_len);
902 tvb_set_free_cb(fd_head->tvb_data, g_free);
904 if (old_tvb_data)
905 tvb_add_to_chain(fd_head->tvb_data, old_tvb_data);
906 fd_head->datalen = tot_len;
908 /* Keep the fragments before the split point, dividing any if
909 * necessary.
910 * XXX: In rare cases, there might be fragments marked as overlap that
911 * have data both before and after the split point, and which only
912 * overlap after the split point. In that case, after dividing the
913 * fragments the first part no longer overlap.
914 * However, at this point we can't test for overlap conflicts,
915 * so we'll just leave the overlap flags as-is.
917 fd_head->flags &= ~(FD_OVERLAP|FD_OVERLAPCONFLICT|FD_TOOLONGFRAGMENT|FD_MULTIPLETAILS);
918 fragment_item *fd_i, *prev_fd = NULL;
919 for (fd_i = fd_head->next; fd_i && (fd_i->offset < tot_len); fd_i = fd_i->next) {
920 fd_i->flags &= ~(FD_TOOLONGFRAGMENT|FD_MULTIPLETAILS);
921 /* Check for the split point occurring in the middle of the
922 * fragment. */
923 if (fd_i->offset + fd_i->len > tot_len) {
924 fd_i->len = tot_len - fd_i->offset;
926 fd_head->flags |= fd_i->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT);
927 prev_fd = fd_i;
929 /* Below should do nothing since this is already defragmented */
930 if (fd_i->flags & FD_SUBSET_TVB)
931 fd_i->flags &= ~FD_SUBSET_TVB;
932 else if (fd_i->tvb_data)
933 tvb_free(fd_i->tvb_data);
935 fd_i->tvb_data=NULL;
938 /* Remove all the other fragments, as they are past the split point. */
939 if (prev_fd) {
940 prev_fd->next = NULL;
941 } else {
942 fd_head->next = NULL;
944 fd_head->contiguous_len = MIN(fd_head->contiguous_len, tot_len);
945 fragment_items_removed(fd_head, prev_fd);
946 fragment_item *tmp_fd;
947 for (; fd_i; fd_i = tmp_fd) {
948 tmp_fd=fd_i->next;
950 if (fd_i->tvb_data && !(fd_i->flags & FD_SUBSET_TVB))
951 tvb_free(fd_i->tvb_data);
952 g_slice_free(fragment_item, fd_i);
956 uint32_t
957 fragment_get_tot_len(reassembly_table *table, const packet_info *pinfo,
958 const uint32_t id, const void *data)
960 fragment_head *fd_head;
962 fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
964 if(fd_head){
965 return fd_head->datalen;
968 return 0;
971 /* This function will set the partial reassembly flag for a fh.
972 When this function is called, the fh MUST already exist, i.e.
973 the fh MUST be created by the initial call to fragment_add() before
974 this function is called.
975 Also note that this function MUST be called to indicate a fh will be
976 extended (increase the already stored data)
979 void
980 fragment_set_partial_reassembly(reassembly_table *table,
981 const packet_info *pinfo, const uint32_t id,
982 const void *data)
984 fragment_head *fd_head;
986 fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
989 * XXX - why not do all the stuff done early in "fragment_add_work()",
990 * turning off FD_DEFRAGMENTED and pointing the fragments' data
991 * pointers to the appropriate part of the already-reassembled
992 * data, and clearing the data length and "reassembled in" frame
993 * number, here? We currently have a hack in the TCP dissector
994 * not to set the "reassembled in" value if the "partial reassembly"
995 * flag is set, so that in the first pass through the packets
996 * we don't falsely set a packet as reassembled in that packet
997 * if the dissector decided that even more reassembly was needed.
999 if(fd_head){
1000 fd_head->flags |= FD_PARTIAL_REASSEMBLY;
1005 * This function gets rid of an entry from a fragment table, given
1006 * a pointer to the key for that entry.
1008 * The key freeing routine will be called by g_hash_table_remove().
1010 static void
1011 fragment_unhash(reassembly_table *table, void *key)
1014 * Remove the entry from the fragment table.
1016 g_hash_table_remove(table->fragment_table, key);
1020 * This function adds fragment_head structure to a reassembled-packet
1021 * hash table, using the frame numbers of each of the frames from
1022 * which it was reassembled as keys, and sets the "reassembled_in"
1023 * frame number.
1025 static void
1026 fragment_reassembled(reassembly_table *table, fragment_head *fd_head,
1027 const packet_info *pinfo, const uint32_t id)
1029 reassembled_key *new_key;
1030 fragment_item *fd;
1032 fd_head->ref_count = 0;
1033 if (fd_head->next == NULL) {
1035 * This was not fragmented, so there's no fragment
1036 * table; just hash it using the current frame number.
1038 new_key = g_slice_new(reassembled_key);
1039 new_key->frame = pinfo->num;
1040 new_key->id = id;
1041 reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1042 } else {
1044 * Hash it with the frame numbers for all the frames.
1046 for (fd = fd_head->next; fd != NULL; fd = fd->next){
1047 new_key = g_slice_new(reassembled_key);
1048 new_key->frame = fd->frame;
1049 new_key->id = id;
1050 reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1053 fd_head->flags |= FD_DEFRAGMENTED;
1054 fd_head->reassembled_in = pinfo->num;
1055 fd_head->reas_in_layer_num = pinfo->curr_layer_num;
1059 * This function is a variant of the above for the single sequence
1060 * case, using id+offset (i.e., the original sequence number) for the id
1061 * in the key.
1063 static void
1064 fragment_reassembled_single(reassembly_table *table, fragment_head *fd_head,
1065 const packet_info *pinfo, const uint32_t id)
1067 reassembled_key *new_key;
1068 fragment_item *fd;
1070 fd_head->ref_count = 0;
1071 if (fd_head->next == NULL) {
1073 * This was not fragmented, so there's no fragment
1074 * table; just hash it using the current frame number.
1076 new_key = g_slice_new(reassembled_key);
1077 new_key->frame = pinfo->num;
1078 new_key->id = id;
1079 reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1080 } else {
1082 * Hash it with the frame numbers for all the frames.
1084 for (fd = fd_head->next; fd != NULL; fd = fd->next){
1085 new_key = g_slice_new(reassembled_key);
1086 new_key->frame = fd->frame;
1087 new_key->id = id + fd->offset;
1088 reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1091 fd_head->flags |= FD_DEFRAGMENTED;
1092 fd_head->reassembled_in = pinfo->num;
1093 fd_head->reas_in_layer_num = pinfo->curr_layer_num;
1096 static void
1097 LINK_FRAG(fragment_head *fd_head,fragment_item *fd)
1099 fragment_item *fd_i;
1101 /* add fragment to list, keep list sorted */
1102 if (fd_head->next == NULL || fd->offset < fd_head->next->offset) {
1103 /* New first fragment */
1104 fd->next = fd_head->next;
1105 fd_head->next = fd;
1106 } else {
1107 fd_i = fd_head->next;
1108 if (fd_head->first_gap != NULL) {
1109 if (fd->offset >= fd_head->first_gap->offset) {
1110 /* fragment is after first gap */
1111 fd_i = fd_head->first_gap;
1114 for(; fd_i->next; fd_i=fd_i->next) {
1115 if (fd->offset < fd_i->next->offset )
1116 break;
1118 fd->next = fd_i->next;
1119 fd_i->next = fd;
1122 update_first_gap(fd_head, fd, false);
1125 static void
1126 MERGE_FRAG(fragment_head *fd_head, fragment_item *fd)
1128 fragment_item *fd_i, *tmp, *inserted = fd;
1129 bool multi_insert;
1131 if (fd == NULL) return;
1133 multi_insert = (fd->next != NULL);
1135 if (fd_head->next == NULL) {
1136 fd_head->next = fd;
1137 update_first_gap(fd_head, fd, multi_insert);
1138 return;
1141 if ((fd_head->first_gap != NULL) &&
1142 (fd->offset >= fd_head->first_gap->offset)) {
1143 /* all new fragments go after first gap */
1144 fd_i = fd_head->first_gap;
1145 } else {
1146 /* at least one new fragment goes before first gap */
1147 if (fd->offset < fd_head->next->offset) {
1148 /* inserted fragment is new head, "swap" the lists */
1149 tmp = fd_head->next;
1150 fd_head->next = fd;
1151 fd = tmp;
1153 fd_i = fd_head->next;
1156 /* Traverse the list linked to fragment head ("main" list), checking if
1157 * fd pointer ("merge" list) should go before or after fd_i->next. Swap
1158 * fd_i->next ("main") and fd pointers ("merge") if "merge" list should
1159 * go before iterated element (fd_i). After the swap what formerly was
1160 * "merge" list essentially becomes part of "main" list (just detached
1161 * element, i.e. fd, is now head of new "merge list").
1163 for(; fd_i->next; fd_i=fd_i->next) {
1164 if (fd->offset < fd_i->next->offset) {
1165 tmp = fd_i->next;
1166 fd_i->next = fd;
1167 fd = tmp;
1170 /* Reached "main" list end, attach remaining elements */
1171 fd_i->next = fd;
1173 update_first_gap(fd_head, inserted, multi_insert);
1177 * This function adds a new fragment to the fragment hash table.
1178 * If this is the first fragment seen for this datagram, a new entry
1179 * is created in the hash table, otherwise this fragment is just added
1180 * to the linked list of fragments for this packet.
1181 * The list of fragments for a specific datagram is kept sorted for
1182 * easier handling.
1184 * Returns a pointer to the head of the fragment data list if we have all the
1185 * fragments, NULL otherwise.
1187 * This function assumes frag_offset being a byte offset into the defragment
1188 * packet.
1190 * 01-2002
1191 * Once the fh is defragmented (= FD_DEFRAGMENTED set), it can be
1192 * extended using the FD_PARTIAL_REASSEMBLY flag. This flag should be set
1193 * using fragment_set_partial_reassembly() before calling fragment_add
1194 * with the new fragment. FD_TOOLONGFRAGMENT and FD_MULTIPLETAILS flags
1195 * are lowered when a new extension process is started.
1197 static bool
1198 fragment_add_work(fragment_head *fd_head, tvbuff_t *tvb, const int offset,
1199 const packet_info *pinfo, const uint32_t frag_offset,
1200 const uint32_t frag_data_len, const bool more_frags,
1201 const uint32_t frag_frame, const bool allow_overlaps)
1203 fragment_item *fd;
1204 fragment_item *fd_i;
1205 uint32_t dfpos, fraglen, overlap;
1206 tvbuff_t *old_tvb_data;
1207 uint8_t *data;
1209 /* create new fd describing this fragment */
1210 fd = g_slice_new(fragment_item);
1211 fd->next = NULL;
1212 fd->flags = 0;
1213 fd->frame = frag_frame;
1214 fd->offset = frag_offset;
1215 fd->len = frag_data_len;
1216 fd->tvb_data = NULL;
1219 * Are we adding to an already-completed reassembly?
1221 if (fd_head->flags & FD_DEFRAGMENTED) {
1223 * Yes. Does this fragment go past the end of the results
1224 * of that reassembly?
1226 if (frag_offset + frag_data_len > fd_head->datalen) {
1228 * Yes. Have we been requested to continue reassembly?
1230 if (fd_head->flags & FD_PARTIAL_REASSEMBLY) {
1232 * Yes. Set flag in already empty fds &
1233 * point old fds to malloc'ed data.
1235 fragment_reset_defragmentation(fd_head);
1236 } else if (!allow_overlaps) {
1238 * No. Bail out since we have no idea what to
1239 * do with this fragment (and if we keep going
1240 * we'll run past the end of a buffer sooner
1241 * or later).
1243 g_slice_free(fragment_item, fd);
1246 * This is an attempt to add a fragment to a
1247 * reassembly that had already completed.
1248 * If it had no error, we don't want to
1249 * mark it with an error, and if it had an
1250 * error, we don't want to overwrite it, so
1251 * we don't set fd_head->error.
1253 if (frag_offset >= fd_head->datalen) {
1255 * The fragment starts past the end
1256 * of the reassembled data.
1258 THROW_MESSAGE(ReassemblyError, "New fragment past old data limits");
1259 } else {
1261 * The fragment starts before the end
1262 * of the reassembled data, but
1263 * runs past the end. That could
1264 * just be a retransmission with extra
1265 * data, but the calling dissector
1266 * didn't set FD_PARTIAL_REASSEMBLY
1267 * so it won't be handled correctly.
1269 * XXX: We could set FD_TOOLONGFRAGMENT
1270 * below instead.
1272 THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)");
1275 } else {
1277 * No. That means it overlaps the completed reassembly.
1278 * This is probably a retransmission and normal
1279 * behavior. (If not, it's because the dissector
1280 * doesn't handle reused sequence numbers correctly,
1281 * e.g. #10503). Handle below.
1286 /* Do this after we may have bailed out (above) so that we don't leave
1287 * fd_head->frame in a bad state if we do */
1288 if (fd->frame > fd_head->frame)
1289 fd_head->frame = fd->frame;
1291 if (!more_frags) {
1293 * This is the tail fragment in the sequence.
1295 if (fd_head->flags & FD_DATALEN_SET) {
1296 /* ok we have already seen other tails for this packet
1297 * it might be a duplicate.
1299 if (fd_head->datalen != (fd->offset + fd->len) ){
1300 /* Oops, this tail indicates a different packet
1301 * len than the previous ones. Something's wrong.
1303 fd->flags |= FD_MULTIPLETAILS;
1304 fd_head->flags |= FD_MULTIPLETAILS;
1306 } else {
1307 /* This was the first tail fragment; now we know
1308 * what the length of the packet should be.
1310 fd_head->datalen = fd->offset + fd->len;
1311 fd_head->flags |= FD_DATALEN_SET;
1317 /* If the packet is already defragmented, this MUST be an overlap.
1318 * The entire defragmented packet is in fd_head->data.
1319 * Even if we have previously defragmented this packet, we still
1320 * check it. Someone might play overlap and TTL games.
1322 if (fd_head->flags & FD_DEFRAGMENTED) {
1323 uint32_t end_offset = fd->offset + fd->len;
1324 fd->flags |= FD_OVERLAP;
1325 fd_head->flags |= FD_OVERLAP;
1326 /* make sure it's not too long */
1327 /* XXX: We probably don't call this, unlike the _seq()
1328 * functions, because we throw an exception above.
1330 if (end_offset > fd_head->datalen || end_offset < fd->offset || end_offset < fd->len) {
1331 fd->flags |= FD_TOOLONGFRAGMENT;
1332 fd_head->flags |= FD_TOOLONGFRAGMENT;
1334 /* make sure it doesn't conflict with previous data */
1335 else if ( tvb_memeql(fd_head->tvb_data, fd->offset,
1336 tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
1337 fd->flags |= FD_OVERLAPCONFLICT;
1338 fd_head->flags |= FD_OVERLAPCONFLICT;
1340 /* it was just an overlap, link it and return */
1341 LINK_FRAG(fd_head,fd);
1342 return true;
1347 /* If we have reached this point, the packet is not defragmented yet.
1348 * Save all payload in a buffer until we can defragment.
1350 if (!tvb_bytes_exist(tvb, offset, fd->len)) {
1351 g_slice_free(fragment_item, fd);
1352 THROW(BoundsError);
1354 fd->tvb_data = tvb_clone_offset_len(tvb, offset, fd->len);
1355 LINK_FRAG(fd_head,fd);
1358 if( !(fd_head->flags & FD_DATALEN_SET) ){
1359 /* if we don't know the datalen, there are still missing
1360 * packets. Cheaper than the check below.
1362 return false;
1365 /* Check if we have received the entire fragment. */
1366 if (fd_head->contiguous_len < fd_head->datalen) {
1368 * The amount of contiguous data we have is less than the
1369 * amount of data we're trying to reassemble, so we haven't
1370 * received all packets yet.
1372 return false;
1375 /* we have received an entire packet, defragment it and
1376 * free all fragments
1378 /* store old data just in case */
1379 old_tvb_data=fd_head->tvb_data;
1380 data = (uint8_t *) g_malloc(fd_head->datalen);
1381 fd_head->tvb_data = tvb_new_real_data(data, fd_head->datalen, fd_head->datalen);
1382 tvb_set_free_cb(fd_head->tvb_data, g_free);
1384 /* add all data fragments */
1385 for (dfpos=0,fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1386 if (fd_i->len) {
1388 * The contiguous length check above also
1389 * ensures that the only gaps that exist here
1390 * are ones where a fragment starts past the
1391 * end of the reassembled datagram, and there's
1392 * a gap between the previous fragment and
1393 * that fragment.
1395 * A "DESEGMENT_UNTIL_FIN" was involved wherein the
1396 * FIN packet had an offset less than the highest
1397 * fragment offset seen. [Seen from a fuzz-test:
1398 * bug #2470]).
1400 * Note that the "overlap" compare must only be
1401 * done for fragments with (offset+len) <= fd_head->datalen
1402 * and thus within the newly g_malloc'd buffer.
1405 if (fd_i->offset >= fd_head->datalen) {
1407 * Fragment starts after the end
1408 * of the reassembled packet.
1410 * This can happen if the length was
1411 * set after the offending fragment
1412 * was added to the reassembly.
1414 * Flag this fragment, but don't
1415 * try to extract any data from
1416 * it, as there's no place to put
1417 * it.
1419 * XXX - add different flag value
1420 * for this.
1422 fd_i->flags |= FD_TOOLONGFRAGMENT;
1423 fd_head->flags |= FD_TOOLONGFRAGMENT;
1424 } else if (fd_i->offset + fd_i->len < fd_i->offset) {
1425 /* Integer overflow, unhandled by rest of
1426 * code so error out. This check handles
1427 * all possible remaining overflows.
1429 fd_head->error = "offset + len < offset";
1430 } else if (!fd_i->tvb_data) {
1431 fd_head->error = "no data";
1432 } else {
1433 fraglen = fd_i->len;
1434 if (fd_i->offset + fraglen > fd_head->datalen) {
1436 * Fragment goes past the end
1437 * of the packet, as indicated
1438 * by the last fragment.
1440 * This can happen if the
1441 * length was set after the
1442 * offending fragment was
1443 * added to the reassembly.
1445 * Mark it as such, and only
1446 * copy from it what fits in
1447 * the packet.
1449 fd_i->flags |= FD_TOOLONGFRAGMENT;
1450 fd_head->flags |= FD_TOOLONGFRAGMENT;
1451 fraglen = fd_head->datalen - fd_i->offset;
1453 overlap = dfpos - fd_i->offset;
1454 /* Guaranteed to be >= 0, previous code
1455 * has checked for gaps. */
1456 if (overlap) {
1457 /* duplicate/retransmission/overlap */
1458 uint32_t cmp_len = MIN(fd_i->len,overlap);
1460 fd_i->flags |= FD_OVERLAP;
1461 fd_head->flags |= FD_OVERLAP;
1462 if ( memcmp(data + fd_i->offset,
1463 tvb_get_ptr(fd_i->tvb_data, 0, cmp_len),
1464 cmp_len)
1466 fd_i->flags |= FD_OVERLAPCONFLICT;
1467 fd_head->flags |= FD_OVERLAPCONFLICT;
1470 /* XXX: As in the fragment_add_seq funcs
1471 * like fragment_defragment_and_free() the
1472 * existing behavior does not overwrite
1473 * overlapping bytes even if there is a
1474 * conflict. It only adds new bytes.
1476 * Since we only add fragments to a reassembly
1477 * if the reassembly isn't complete, the most
1478 * common case for overlap conflicts is when
1479 * an earlier reassembly isn't fully contained
1480 * in the capture, and we've reused an
1481 * indentification number / wrapped around
1482 * offset sequence numbers much later in the
1483 * capture. In that case, we probably *do*
1484 * want to overwrite conflicting bytes, since
1485 * the earlier fragments didn't form a complete
1486 * reassembly and should be effectively thrown
1487 * out rather than mixed with the new ones?
1489 if (fd_i->offset + fraglen > dfpos) {
1490 memcpy(data+dfpos,
1491 tvb_get_ptr(fd_i->tvb_data, overlap, fraglen-overlap),
1492 fraglen-overlap);
1493 dfpos = fd_i->offset + fraglen;
1497 if (fd_i->flags & FD_SUBSET_TVB)
1498 fd_i->flags &= ~FD_SUBSET_TVB;
1499 else if (fd_i->tvb_data)
1500 tvb_free(fd_i->tvb_data);
1502 fd_i->tvb_data=NULL;
1506 if (old_tvb_data)
1507 tvb_add_to_chain(tvb, old_tvb_data);
1508 /* mark this packet as defragmented.
1509 allows us to skip any trailing fragments */
1510 fd_head->flags |= FD_DEFRAGMENTED;
1511 fd_head->reassembled_in=pinfo->num;
1512 fd_head->reas_in_layer_num = pinfo->curr_layer_num;
1514 /* we don't throw until here to avoid leaking old_data and others */
1515 if (fd_head->error) {
1516 THROW_MESSAGE(ReassemblyError, fd_head->error);
1519 return true;
1522 static fragment_head *
1523 fragment_add_common(reassembly_table *table, tvbuff_t *tvb, const int offset,
1524 const packet_info *pinfo, const uint32_t id,
1525 const void *data, const uint32_t frag_offset,
1526 const uint32_t frag_data_len, const bool more_frags,
1527 const bool check_already_added,
1528 const uint32_t frag_frame)
1530 fragment_head *fd_head;
1531 fragment_item *fd_item;
1532 bool already_added;
1536 * Dissector shouldn't give us garbage tvb info.
1538 * XXX - should this code take responsibility for preventing
1539 * reassembly if data is missing due to the packets being
1540 * sliced, rather than leaving it up to dissectors?
1542 DISSECTOR_ASSERT(tvb_bytes_exist(tvb, offset, frag_data_len));
1544 fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
1546 #if 0
1547 /* debug output of associated fragments. */
1548 /* leave it here for future debugging sessions */
1549 if(strcmp(pinfo->current_proto, "DCERPC") == 0) {
1550 printf("proto:%s num:%u id:%u offset:%u len:%u more:%u visited:%u\n",
1551 pinfo->current_proto, pinfo->num, id, frag_offset, frag_data_len, more_frags, pinfo->fd->visited);
1552 if(fd_head != NULL) {
1553 for(fd_item=fd_head->next;fd_item;fd_item=fd_item->next){
1554 printf("fd_frame:%u fd_offset:%u len:%u datalen:%u\n",
1555 fd_item->frame, fd_item->offset, fd_item->len, fd_item->datalen);
1559 #endif
1562 * Is this the first pass through the capture?
1564 if (!pinfo->fd->visited) {
1566 * Yes, so we could be doing reassembly. If
1567 * "check_already_added" is true, and fd_head is non-null,
1568 * meaning that this fragment would be added to an
1569 * in-progress reassembly, check if we have seen this
1570 * fragment before, i.e., if we have already added it to
1571 * that reassembly. That can be true even on the first pass
1572 * since we sometimes might call a subdissector multiple
1573 * times.
1575 * We check both the frame number and the fragment offset,
1576 * so that we support multiple fragments from the same
1577 * frame being added to the same reassembled PDU.
1579 if (check_already_added && fd_head != NULL) {
1581 * fd_head->frame is the maximum of the frame
1582 * numbers of all the fragments added to this
1583 * reassembly; if this frame is later than that
1584 * frame, we know it hasn't been added yet.
1586 if (frag_frame <= fd_head->frame) {
1587 already_added = false;
1589 * The first item in the reassembly list
1590 * is not a fragment, it's a data structure
1591 * for the reassembled packet, so we
1592 * start checking with the next item.
1594 for (fd_item = fd_head->next; fd_item;
1595 fd_item = fd_item->next) {
1596 if (frag_frame == fd_item->frame &&
1597 frag_offset == fd_item->offset) {
1598 already_added = true;
1599 break;
1602 if (already_added) {
1604 * Have we already finished
1605 * reassembling?
1607 if (fd_head->flags & FD_DEFRAGMENTED) {
1609 * Yes.
1610 * XXX - can this ever happen?
1612 THROW_MESSAGE(ReassemblyError,
1613 "Frame already added in first pass");
1614 } else {
1616 * No.
1618 return NULL;
1623 } else {
1625 * No, so we've already done all the reassembly and added
1626 * all the fragments. Do we have a reassembly and, if so,
1627 * have we finished reassembling?
1629 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
1631 * Yes. This is probably being done after the
1632 * first pass, and we've already done the work
1633 * on the first pass.
1635 * If the reassembly got a fatal error, throw that
1636 * error again.
1638 if (fd_head->error)
1639 THROW_MESSAGE(ReassemblyError, fd_head->error);
1642 * Is it later in the capture than all of the
1643 * fragments in the reassembly?
1645 if (frag_frame > fd_head->frame) {
1647 * Yes, so report this as a problem,
1648 * possibly a retransmission.
1650 THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)");
1654 * Does this fragment go past the end of the
1655 * results of that reassembly?
1657 if (frag_offset + frag_data_len > fd_head->datalen) {
1659 * Yes.
1661 if (frag_offset >= fd_head->datalen) {
1663 * The fragment starts past the
1664 * end of the reassembled data.
1666 THROW_MESSAGE(ReassemblyError, "New fragment past old data limits");
1667 } else {
1669 * The fragment starts before the end
1670 * of the reassembled data, but
1671 * runs past the end. That could
1672 * just be a retransmission.
1674 THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)");
1678 return fd_head;
1679 } else {
1681 * No.
1683 return NULL;
1687 if (fd_head==NULL){
1688 /* not found, this must be the first snooped fragment for this
1689 * packet. Create list-head.
1691 fd_head = new_head(0);
1694 * Insert it into the hash table.
1696 insert_fd_head(table, fd_head, pinfo, id, data);
1699 if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
1700 frag_data_len, more_frags, frag_frame, false)) {
1702 * Reassembly is complete.
1704 return fd_head;
1705 } else {
1707 * Reassembly isn't complete.
1709 return NULL;
1713 fragment_head *
1714 fragment_add(reassembly_table *table, tvbuff_t *tvb, const int offset,
1715 const packet_info *pinfo, const uint32_t id, const void *data,
1716 const uint32_t frag_offset, const uint32_t frag_data_len,
1717 const bool more_frags)
1719 return fragment_add_common(table, tvb, offset, pinfo, id, data,
1720 frag_offset, frag_data_len, more_frags, true, pinfo->num);
1724 * For use when you can have multiple fragments in the same frame added
1725 * to the same reassembled PDU, e.g. with ONC RPC-over-TCP.
1727 fragment_head *
1728 fragment_add_multiple_ok(reassembly_table *table, tvbuff_t *tvb,
1729 const int offset, const packet_info *pinfo,
1730 const uint32_t id, const void *data,
1731 const uint32_t frag_offset,
1732 const uint32_t frag_data_len, const bool more_frags)
1734 return fragment_add_common(table, tvb, offset, pinfo, id, data,
1735 frag_offset, frag_data_len, more_frags, false, pinfo->num);
1739 * For use in protocols like TCP when you are adding an out of order segment
1740 * that arrived in an earlier frame because the correct fragment id could not
1741 * be determined until later. By allowing fd->frame to be different than
1742 * pinfo->num, show_fragment_tree will display the correct fragment numbers.
1744 * Note that pinfo is still used to set reassembled_in if we have all the
1745 * fragments, so that results on subsequent passes can be the same as the
1746 * first pass.
1748 fragment_head *
1749 fragment_add_out_of_order(reassembly_table *table, tvbuff_t *tvb,
1750 const int offset, const packet_info *pinfo,
1751 const uint32_t id, const void *data,
1752 const uint32_t frag_offset,
1753 const uint32_t frag_data_len,
1754 const bool more_frags, const uint32_t frag_frame)
1756 return fragment_add_common(table, tvb, offset, pinfo, id, data,
1757 frag_offset, frag_data_len, more_frags, true, frag_frame);
1760 fragment_head *
1761 fragment_add_check_with_fallback(reassembly_table *table, tvbuff_t *tvb, const int offset,
1762 const packet_info *pinfo, const uint32_t id,
1763 const void *data, const uint32_t frag_offset,
1764 const uint32_t frag_data_len, const bool more_frags,
1765 const uint32_t fallback_frame)
1767 reassembled_key reass_key;
1768 fragment_head *fd_head;
1769 void *orig_key;
1770 bool late_retransmission = false;
1773 * If this isn't the first pass, look for this frame in the table
1774 * of reassembled packets.
1776 if (pinfo->fd->visited) {
1777 reass_key.frame = pinfo->num;
1778 reass_key.id = id;
1779 return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
1782 /* Looks up a key in the GHashTable, returning the original key and the associated value
1783 * and a bool which is true if the key was found. This is useful if you need to free
1784 * the memory allocated for the original key, for example before calling g_hash_table_remove()
1786 fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
1787 if ((fd_head == NULL) && (fallback_frame != pinfo->num)) {
1788 /* Check if there is completed reassembly reachable from fallback frame */
1789 reass_key.frame = fallback_frame;
1790 reass_key.id = id;
1791 fd_head = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
1792 if (fd_head != NULL) {
1793 /* Found completely reassembled packet, hash it with current frame number */
1794 reassembled_key *new_key = g_slice_new(reassembled_key);
1795 new_key->frame = pinfo->num;
1796 new_key->id = id;
1797 reassembled_table_insert(table->reassembled_table, new_key, fd_head);
1798 late_retransmission = true;
1801 if (fd_head == NULL) {
1802 /* not found, this must be the first snooped fragment for this
1803 * packet. Create list-head.
1805 fd_head = new_head(0);
1808 * Save the key, for unhashing it later.
1810 orig_key = insert_fd_head(table, fd_head, pinfo, id, data);
1814 * If this is a short frame, then we can't, and don't, do
1815 * reassembly on it. We just give up.
1817 if (!tvb_bytes_exist(tvb, offset, frag_data_len)) {
1818 return NULL;
1821 if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
1822 frag_data_len, more_frags, pinfo->num, late_retransmission)) {
1823 /* Nothing left to do if it was a late retransmission */
1824 if (late_retransmission) {
1825 return fd_head;
1828 * Reassembly is complete.
1829 * Remove this from the table of in-progress
1830 * reassemblies, add it to the table of
1831 * reassembled packets, and return it.
1835 * Remove this from the table of in-progress reassemblies,
1836 * and free up any memory used for it in that table.
1838 fragment_unhash(table, orig_key);
1841 * Add this item to the table of reassembled packets.
1843 fragment_reassembled(table, fd_head, pinfo, id);
1844 return fd_head;
1845 } else {
1847 * Reassembly isn't complete.
1849 return NULL;
1853 fragment_head *
1854 fragment_add_check(reassembly_table *table, tvbuff_t *tvb, const int offset,
1855 const packet_info *pinfo, const uint32_t id,
1856 const void *data, const uint32_t frag_offset,
1857 const uint32_t frag_data_len, const bool more_frags)
1859 return fragment_add_check_with_fallback(table, tvb, offset, pinfo, id, data,
1860 frag_offset, frag_data_len, more_frags, pinfo->num);
1863 static void
1864 fragment_defragment_and_free (fragment_head *fd_head, const packet_info *pinfo)
1866 fragment_item *fd_i = NULL;
1867 fragment_item *last_fd = NULL;
1868 uint32_t dfpos = 0, size = 0;
1869 tvbuff_t *old_tvb_data = NULL;
1870 uint8_t *data;
1872 for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1873 if(!last_fd || last_fd->offset!=fd_i->offset){
1874 size+=fd_i->len;
1876 last_fd=fd_i;
1879 /* store old data in case the fd_i->data pointers refer to it */
1880 old_tvb_data=fd_head->tvb_data;
1881 data = (uint8_t *) g_malloc(size);
1882 fd_head->tvb_data = tvb_new_real_data(data, size, size);
1883 tvb_set_free_cb(fd_head->tvb_data, g_free);
1884 fd_head->len = size; /* record size for caller */
1886 /* add all data fragments */
1887 last_fd=NULL;
1888 for (fd_i=fd_head->next; fd_i; fd_i=fd_i->next) {
1889 if (fd_i->len) {
1890 if(!last_fd || last_fd->offset != fd_i->offset) {
1891 /* First fragment or in-sequence fragment */
1892 memcpy(data+dfpos, tvb_get_ptr(fd_i->tvb_data, 0, fd_i->len), fd_i->len);
1893 dfpos += fd_i->len;
1894 } else {
1895 /* duplicate/retransmission/overlap */
1896 fd_i->flags |= FD_OVERLAP;
1897 fd_head->flags |= FD_OVERLAP;
1898 if(last_fd->len != fd_i->len
1899 || tvb_memeql(last_fd->tvb_data, 0, tvb_get_ptr(fd_i->tvb_data, 0, last_fd->len), last_fd->len) ) {
1900 fd_i->flags |= FD_OVERLAPCONFLICT;
1901 fd_head->flags |= FD_OVERLAPCONFLICT;
1905 last_fd=fd_i;
1908 /* we have defragmented the pdu, now free all fragments*/
1909 for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1910 if (fd_i->flags & FD_SUBSET_TVB)
1911 fd_i->flags &= ~FD_SUBSET_TVB;
1912 else if (fd_i->tvb_data)
1913 tvb_free(fd_i->tvb_data);
1914 fd_i->tvb_data=NULL;
1916 if (old_tvb_data)
1917 tvb_free(old_tvb_data);
1919 /* mark this packet as defragmented.
1920 * allows us to skip any trailing fragments.
1922 fd_head->flags |= FD_DEFRAGMENTED;
1923 fd_head->reassembled_in=pinfo->num;
1924 fd_head->reas_in_layer_num = pinfo->curr_layer_num;
1928 * This function adds a new fragment to the entry for a reassembly
1929 * operation.
1931 * The list of fragments for a specific datagram is kept sorted for
1932 * easier handling.
1934 * Returns true if we have all the fragments, false otherwise.
1936 * This function assumes frag_number being a block sequence number.
1937 * The bsn for the first block is 0.
1939 static bool
1940 fragment_add_seq_work(fragment_head *fd_head, tvbuff_t *tvb, const int offset,
1941 const packet_info *pinfo, const uint32_t frag_number,
1942 const uint32_t frag_data_len, const bool more_frags)
1944 fragment_item *fd;
1945 fragment_item *fd_i;
1946 fragment_item *last_fd;
1947 uint32_t max, dfpos;
1948 uint32_t frag_number_work;
1950 /* Enables the use of fragment sequence numbers, which do not start with 0 */
1951 frag_number_work = frag_number;
1952 if ( fd_head->fragment_nr_offset != 0 )
1953 if ( frag_number_work >= fd_head->fragment_nr_offset )
1954 frag_number_work = frag_number - fd_head->fragment_nr_offset;
1956 /* if the partial reassembly flag has been set, and we are extending
1957 * the pdu, un-reassemble the pdu. This means pointing old fds to malloc'ed data.
1959 if(fd_head->flags & FD_DEFRAGMENTED && frag_number_work >= fd_head->datalen &&
1960 fd_head->flags & FD_PARTIAL_REASSEMBLY){
1961 uint32_t lastdfpos = 0;
1962 dfpos = 0;
1963 for(fd_i=fd_head->next; fd_i; fd_i=fd_i->next){
1964 if( !fd_i->tvb_data ) {
1965 if( fd_i->flags & FD_OVERLAP ) {
1966 /* this is a duplicate of the previous
1967 * fragment. */
1968 fd_i->tvb_data = tvb_new_subset_remaining(fd_head->tvb_data, lastdfpos);
1969 } else {
1970 fd_i->tvb_data = tvb_new_subset_remaining(fd_head->tvb_data, dfpos);
1971 lastdfpos = dfpos;
1972 dfpos += fd_i->len;
1974 fd_i->flags |= FD_SUBSET_TVB;
1976 fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
1978 fd_head->flags &= ~(FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY|FD_DATALEN_SET);
1979 fd_head->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
1980 fd_head->datalen=0;
1981 fd_head->reassembled_in=0;
1982 fd_head->reas_in_layer_num = 0;
1986 /* create new fd describing this fragment */
1987 fd = g_slice_new(fragment_item);
1988 fd->next = NULL;
1989 fd->flags = 0;
1990 fd->frame = pinfo->num;
1991 fd->offset = frag_number_work;
1992 fd->len = frag_data_len;
1993 fd->tvb_data = NULL;
1995 /* fd_head->frame is the maximum of the frame numbers of all the
1996 * fragments added to the reassembly. */
1997 if (fd->frame > fd_head->frame)
1998 fd_head->frame = fd->frame;
2000 if (!more_frags) {
2002 * This is the tail fragment in the sequence.
2004 if (fd_head->flags&FD_DATALEN_SET) {
2005 /* ok we have already seen other tails for this packet
2006 * it might be a duplicate.
2008 if (fd_head->datalen != fd->offset ){
2009 /* Oops, this tail indicates a different packet
2010 * len than the previous ones. Something's wrong.
2012 fd->flags |= FD_MULTIPLETAILS;
2013 fd_head->flags |= FD_MULTIPLETAILS;
2015 } else {
2016 /* this was the first tail fragment, now we know the
2017 * sequence number of that fragment (which is NOT
2018 * the length of the packet!)
2020 fd_head->datalen = fd->offset;
2021 fd_head->flags |= FD_DATALEN_SET;
2025 /* If the packet is already defragmented, this MUST be an overlap.
2026 * The entire defragmented packet is in fd_head->data
2027 * Even if we have previously defragmented this packet, we still check
2028 * check it. Someone might play overlap and TTL games.
2030 if (fd_head->flags & FD_DEFRAGMENTED) {
2031 fd->flags |= FD_OVERLAP;
2032 fd_head->flags |= FD_OVERLAP;
2034 /* make sure it's not past the end */
2035 if (fd->offset > fd_head->datalen) {
2036 /* new fragment comes after the end */
2037 fd->flags |= FD_TOOLONGFRAGMENT;
2038 fd_head->flags |= FD_TOOLONGFRAGMENT;
2039 LINK_FRAG(fd_head,fd);
2040 return true;
2042 /* make sure it doesn't conflict with previous data */
2043 dfpos=0;
2044 last_fd=NULL;
2045 for (fd_i=fd_head->next;fd_i && (fd_i->offset!=fd->offset);fd_i=fd_i->next) {
2046 if (!last_fd || last_fd->offset!=fd_i->offset){
2047 dfpos += fd_i->len;
2049 last_fd=fd_i;
2051 if(fd_i){
2052 /* new fragment overlaps existing fragment */
2053 if(fd_i->len!=fd->len){
2055 * They have different lengths; this
2056 * is definitely a conflict.
2058 fd->flags |= FD_OVERLAPCONFLICT;
2059 fd_head->flags |= FD_OVERLAPCONFLICT;
2060 LINK_FRAG(fd_head,fd);
2061 return true;
2063 DISSECTOR_ASSERT(fd_head->len >= dfpos + fd->len);
2064 if (tvb_memeql(fd_head->tvb_data, dfpos,
2065 tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
2067 * They have the same length, but the
2068 * data isn't the same.
2070 fd->flags |= FD_OVERLAPCONFLICT;
2071 fd_head->flags |= FD_OVERLAPCONFLICT;
2072 LINK_FRAG(fd_head,fd);
2073 return true;
2075 /* it was just an overlap, link it and return */
2076 LINK_FRAG(fd_head,fd);
2077 return true;
2078 } else {
2080 * New fragment doesn't overlap an existing
2081 * fragment - there was presumably a gap in
2082 * the sequence number space.
2084 * XXX - what should we do here? Is it always
2085 * the case that there are no gaps, or are there
2086 * protcols using sequence numbers where there
2087 * can be gaps?
2089 * If the former, the check below for having
2090 * received all the fragments should check for
2091 * holes in the sequence number space and for the
2092 * first sequence number being 0. If we do that,
2093 * the only way we can get here is if this fragment
2094 * is past the end of the sequence number space -
2095 * but the check for "fd->offset > fd_head->datalen"
2096 * would have caught that above, so it can't happen.
2098 * If the latter, we don't have a good way of
2099 * knowing whether reassembly is complete if we
2100 * get packet out of order such that the "last"
2101 * fragment doesn't show up last - but, unless
2102 * in-order reliable delivery of fragments is
2103 * guaranteed, an implementation of the protocol
2104 * has no way of knowing whether reassembly is
2105 * complete, either.
2107 * For now, we just link the fragment in and
2108 * return.
2110 LINK_FRAG(fd_head,fd);
2111 return true;
2115 /* If we have reached this point, the packet is not defragmented yet.
2116 * Save all payload in a buffer until we can defragment.
2118 /* check len, there may be a fragment with 0 len, that is actually the tail */
2119 if (fd->len) {
2120 if (!tvb_bytes_exist(tvb, offset, fd->len)) {
2121 /* abort if we didn't capture the entire fragment due
2122 * to a too-short snapshot length */
2123 g_slice_free(fragment_item, fd);
2124 return false;
2127 fd->tvb_data = tvb_clone_offset_len(tvb, offset, fd->len);
2129 LINK_FRAG(fd_head,fd);
2132 if( !(fd_head->flags & FD_DATALEN_SET) ){
2133 /* if we don't know the sequence number of the last fragment,
2134 * there are definitely still missing packets. Cheaper than
2135 * the check below.
2137 return false;
2141 /* check if we have received the entire fragment
2142 * this is easy since the list is sorted and the head is faked.
2143 * common case the whole list is scanned.
2145 max = 0;
2146 for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
2147 if ( fd_i->offset==max ){
2148 max++;
2151 /* max will now be datalen+1 if all fragments have been seen */
2153 if (max <= fd_head->datalen) {
2154 /* we have not received all packets yet */
2155 return false;
2159 if (max > (fd_head->datalen+1)) {
2160 /* oops, too long fragment detected */
2161 fd->flags |= FD_TOOLONGFRAGMENT;
2162 fd_head->flags |= FD_TOOLONGFRAGMENT;
2166 /* we have received an entire packet, defragment it and
2167 * free all fragments
2169 fragment_defragment_and_free(fd_head, pinfo);
2171 return true;
2175 * This function adds a new fragment to the fragment hash table.
2176 * If this is the first fragment seen for this datagram, a new entry
2177 * is created in the hash table, otherwise this fragment is just added
2178 * to the linked list of fragments for this packet.
2180 * Returns a pointer to the head of the fragment data list if we have all the
2181 * fragments, NULL otherwise.
2183 * This function assumes frag_number being a block sequence number.
2184 * The bsn for the first block is 0.
2186 static fragment_head *
2187 fragment_add_seq_common(reassembly_table *table, tvbuff_t *tvb,
2188 const int offset, const packet_info *pinfo,
2189 const uint32_t id, const void *data,
2190 uint32_t frag_number, const uint32_t frag_data_len,
2191 const bool more_frags, const uint32_t flags,
2192 void * *orig_keyp)
2194 fragment_head *fd_head;
2195 void *orig_key;
2197 fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
2199 /* have we already seen this frame ?*/
2200 if (pinfo->fd->visited) {
2201 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
2202 if (orig_keyp != NULL)
2203 *orig_keyp = orig_key;
2204 return fd_head;
2205 } else {
2206 return NULL;
2210 if (fd_head==NULL){
2211 /* not found, this must be the first snooped fragment for this
2212 * packet. Create list-head.
2214 fd_head= new_head(FD_BLOCKSEQUENCE);
2216 if((flags & (REASSEMBLE_FLAGS_NO_FRAG_NUMBER|REASSEMBLE_FLAGS_802_11_HACK))
2217 && !more_frags) {
2219 * This is the last fragment for this packet, and
2220 * is the only one we've seen.
2222 * Either we don't have sequence numbers, in which
2223 * case we assume this is the first fragment for
2224 * this packet, or we're doing special 802.11
2225 * processing, in which case we assume it's one
2226 * of those reassembled packets with a non-zero
2227 * fragment number (see packet-80211.c); just
2228 * return a pointer to the head of the list;
2229 * fragment_add_seq_check will then add it to the table
2230 * of reassembled packets.
2232 if (orig_keyp != NULL)
2233 *orig_keyp = NULL;
2234 /* To save memory, we don't actually copy the
2235 * fragment from the tvbuff to the fragment, and in
2236 * process_reassembled_data just return back a subset
2237 * of the original tvbuff (which must be passed in).
2239 fd_head->len = frag_data_len;
2240 fd_head->reassembled_in=pinfo->num;
2241 fd_head->reas_in_layer_num = pinfo->curr_layer_num;
2242 return fd_head;
2245 orig_key = insert_fd_head(table, fd_head, pinfo, id, data);
2246 if (orig_keyp != NULL)
2247 *orig_keyp = orig_key;
2250 * If we weren't given an initial fragment number,
2251 * make it 0.
2253 if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER)
2254 frag_number = 0;
2255 } else {
2256 if (orig_keyp != NULL)
2257 *orig_keyp = orig_key;
2259 if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER) {
2260 fragment_item *fd;
2262 * If we weren't given an initial fragment number,
2263 * use the next expected fragment number as the fragment
2264 * number for this fragment.
2266 for (fd = fd_head->next; fd != NULL; fd = fd->next) {
2267 if (fd->next == NULL)
2268 frag_number = fd->offset + 1;
2273 if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
2274 frag_number, frag_data_len, more_frags)) {
2276 * Reassembly is complete.
2278 return fd_head;
2279 } else {
2281 * Reassembly isn't complete.
2283 return NULL;
2287 fragment_head *
2288 fragment_add_seq(reassembly_table *table, tvbuff_t *tvb, const int offset,
2289 const packet_info *pinfo, const uint32_t id, const void *data,
2290 const uint32_t frag_number, const uint32_t frag_data_len,
2291 const bool more_frags, const uint32_t flags)
2293 return fragment_add_seq_common(table, tvb, offset, pinfo, id, data,
2294 frag_number, frag_data_len,
2295 more_frags, flags, NULL);
2299 * This does the work for "fragment_add_seq_check()" and
2300 * "fragment_add_seq_next()".
2302 * This function assumes frag_number being a block sequence number.
2303 * The bsn for the first block is 0.
2305 * If REASSEMBLE_FLAGS_NO_FRAG_NUMBER, it uses the next expected fragment number
2306 * as the fragment number if there is a reassembly in progress, otherwise
2307 * it uses 0.
2309 * If not REASSEMBLE_FLAGS_NO_FRAG_NUMBER, it uses the "frag_number" argument as
2310 * the fragment number.
2312 * If this is the first fragment seen for this datagram, a new
2313 * "fragment_head" structure is allocated to refer to the reassembled
2314 * packet.
2316 * This fragment is added to the linked list of fragments for this packet.
2318 * If "more_frags" is false and REASSEMBLE_FLAGS_802_11_HACK (as the name
2319 * implies, a special hack for 802.11) or REASSEMBLE_FLAGS_NO_FRAG_NUMBER
2320 * (implying messages must be in order since there's no sequence number) are
2321 * set in "flags", then this (one element) list is returned.
2323 * If, after processing this fragment, we have all the fragments,
2324 * "fragment_add_seq_check_work()" removes that from the fragment hash
2325 * table if necessary and adds it to the table of reassembled fragments,
2326 * and returns a pointer to the head of the fragment list.
2328 * Otherwise, it returns NULL.
2330 * XXX - Should we simply return NULL for zero-length fragments?
2332 static fragment_head *
2333 fragment_add_seq_check_work(reassembly_table *table, tvbuff_t *tvb,
2334 const int offset, const packet_info *pinfo,
2335 const uint32_t id, const void *data,
2336 const uint32_t frag_number,
2337 const uint32_t frag_data_len,
2338 const bool more_frags, const uint32_t flags)
2340 reassembled_key reass_key;
2341 fragment_head *fd_head;
2342 void *orig_key;
2345 * Have we already seen this frame?
2346 * If so, look for it in the table of reassembled packets.
2348 if (pinfo->fd->visited) {
2349 reass_key.frame = pinfo->num;
2350 reass_key.id = id;
2351 return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
2354 fd_head = fragment_add_seq_common(table, tvb, offset, pinfo, id, data,
2355 frag_number, frag_data_len,
2356 more_frags,
2357 flags,
2358 &orig_key);
2359 if (fd_head) {
2361 * Reassembly is complete.
2363 * If this is in the table of in-progress reassemblies,
2364 * remove it from that table. (It could be that this
2365 * was the first and last fragment, so that no
2366 * reassembly was done.)
2368 if (orig_key != NULL)
2369 fragment_unhash(table, orig_key);
2372 * Add this item to the table of reassembled packets.
2374 fragment_reassembled(table, fd_head, pinfo, id);
2375 return fd_head;
2376 } else {
2378 * Reassembly isn't complete.
2380 return NULL;
2384 fragment_head *
2385 fragment_add_seq_check(reassembly_table *table, tvbuff_t *tvb, const int offset,
2386 const packet_info *pinfo, const uint32_t id,
2387 const void *data,
2388 const uint32_t frag_number, const uint32_t frag_data_len,
2389 const bool more_frags)
2391 return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2392 frag_number, frag_data_len,
2393 more_frags, 0);
2396 fragment_head *
2397 fragment_add_seq_802_11(reassembly_table *table, tvbuff_t *tvb,
2398 const int offset, const packet_info *pinfo,
2399 const uint32_t id, const void *data,
2400 const uint32_t frag_number, const uint32_t frag_data_len,
2401 const bool more_frags)
2403 return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2404 frag_number, frag_data_len,
2405 more_frags,
2406 REASSEMBLE_FLAGS_802_11_HACK);
2409 fragment_head *
2410 fragment_add_seq_next(reassembly_table *table, tvbuff_t *tvb, const int offset,
2411 const packet_info *pinfo, const uint32_t id,
2412 const void *data, const uint32_t frag_data_len,
2413 const bool more_frags)
2415 /* Use a dummy frag_number (0), it is ignored since
2416 * REASSEMBLE_FLAGS_NO_FRAG_NUMBER is set. */
2417 return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2418 0, frag_data_len, more_frags,
2419 REASSEMBLE_FLAGS_NO_FRAG_NUMBER);
2422 static void
2423 fragment_add_seq_single_move(reassembly_table *table, const packet_info *pinfo,
2424 const uint32_t id, const void *data,
2425 const uint32_t offset)
2427 fragment_head *fh, *new_fh;
2428 fragment_item *fd, *prev_fd;
2429 tvbuff_t *old_tvb_data;
2430 if (offset == 0) {
2431 return;
2433 fh = lookup_fd_head(table, pinfo, id, data, NULL);
2434 if (fh == NULL) {
2435 /* Shouldn't be called this way.
2436 * Probably wouldn't hurt to just create fh in this case. */
2437 ws_assert_not_reached();
2438 return;
2440 if (fh->flags & FD_DATALEN_SET && fh->datalen <= offset) {
2441 /* Don't take from past the end. <= because we don't
2442 * want to take a First fragment from the next one
2443 * either */
2444 return;
2446 new_fh = lookup_fd_head(table, pinfo, id+offset, data, NULL);
2447 if (new_fh != NULL) {
2448 /* Attach to the end of the sorted list. */
2449 prev_fd = NULL;
2450 for(fd = fh->next; fd != NULL; fd=fd->next) {
2451 prev_fd = fd;
2453 /* Don't take a reassembly starting with a First fragment. */
2454 fd = new_fh->next;
2455 if (fd && fd->offset != 0) {
2456 fragment_item *inserted = fd;
2457 bool multi_insert = (inserted->next != NULL);
2458 if (prev_fd) {
2459 prev_fd->next = fd;
2460 } else {
2461 fh->next = fd;
2463 for (; fd; fd=fd->next) {
2464 fd->offset += offset;
2465 if (fh->frame < fd->frame) {
2466 fh->frame = fd->frame;
2469 update_first_gap(fh, inserted, multi_insert);
2470 /* If previously found a Last fragment,
2471 * transfer that info to the new one. */
2472 if (new_fh->flags & FD_DATALEN_SET) {
2473 fh->flags |= FD_DATALEN_SET;
2474 fh->datalen = new_fh->datalen + offset;
2476 /* Now remove and delete */
2477 new_fh->next = NULL;
2478 old_tvb_data = fragment_delete(table, pinfo, id+offset, data);
2479 if (old_tvb_data)
2480 tvb_free(old_tvb_data);
2485 static fragment_head *
2486 fragment_add_seq_single_work(reassembly_table *table, tvbuff_t *tvb,
2487 const int offset, const packet_info *pinfo,
2488 const uint32_t id, const void* data,
2489 const uint32_t frag_data_len,
2490 const bool first, const bool last,
2491 const uint32_t max_frags, const uint32_t max_age,
2492 const uint32_t flags)
2494 reassembled_key reass_key;
2495 tvbuff_t *old_tvb_data;
2496 void *orig_key;
2497 fragment_head *fh, *new_fh;
2498 fragment_item *fd, *prev_fd;
2499 uint32_t frag_number, tmp_offset;
2500 /* Have we already seen this frame?
2501 * If so, look for it in the table of reassembled packets.
2502 * Note here we store in the reassembly table by the single sequence
2503 * number rather than the sequence number of the First fragment. */
2504 if (pinfo->fd->visited) {
2505 reass_key.frame = pinfo->num;
2506 reass_key.id = id;
2507 fh = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
2508 return fh;
2510 /* First let's figure out where we want to add our new fragment */
2511 fh = NULL;
2512 if (first) {
2513 frag_number = 0;
2514 fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL);
2515 if ((flags & REASSEMBLE_FLAGS_AGING) &&
2516 fh && ((fh->frame + max_age) < pinfo->num)) {
2517 old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data);
2518 if (old_tvb_data)
2519 tvb_free(old_tvb_data);
2520 fh = NULL;
2522 if (fh == NULL) {
2523 /* Not found. Create list-head. */
2524 fh = new_head(FD_BLOCKSEQUENCE);
2525 insert_fd_head(table, fh, pinfo, id-frag_number, data);
2527 /* As this is the first fragment, we might have added segments
2528 * for this reassembly to the previous one in-progress. */
2529 fd = NULL;
2530 for (frag_number=1; frag_number < max_frags; frag_number++) {
2531 new_fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL);
2532 if (new_fh != NULL) {
2533 prev_fd = NULL;
2534 new_fh->frame = 0;
2535 for (fd=new_fh->next; fd && fd->offset < frag_number; fd=fd->next) {
2536 prev_fd = fd;
2537 if (new_fh->frame < fd->frame) {
2538 new_fh->frame = fd->frame;
2541 if (prev_fd) {
2542 prev_fd->next = NULL;
2543 } else {
2544 new_fh->next = NULL;
2546 fragment_items_removed(new_fh, prev_fd);
2547 break;
2550 if (fd != NULL) {
2551 tmp_offset = 0;
2552 for (prev_fd = fd; prev_fd; prev_fd = prev_fd->next) {
2553 prev_fd->offset -= frag_number;
2554 tmp_offset = prev_fd->offset;
2555 if (fh->frame < prev_fd->frame) {
2556 fh->frame = prev_fd->frame;
2559 MERGE_FRAG(fh, fd);
2560 if (new_fh != NULL) {
2561 /* If we've moved a Last packet, change datalen.
2562 * Second part of this test prob. redundant? */
2563 if (new_fh->flags & FD_DATALEN_SET &&
2564 new_fh->datalen >= frag_number) {
2565 fh->flags |= FD_DATALEN_SET;
2566 fh->datalen = new_fh->datalen - frag_number;
2567 new_fh->flags &= ~FD_DATALEN_SET;
2568 new_fh->datalen = 0;
2570 /* If we've moved all the fragments,
2571 * delete the old head */
2572 if (new_fh->next == NULL) {
2573 old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data);
2574 if (old_tvb_data)
2575 tvb_free(old_tvb_data);
2577 } else {
2578 /* Look forward and take off the next (this is
2579 * necessary in some edge cases where max_frags
2580 * prevented some fragments from going on the
2581 * previous First, but they can go on this one. */
2582 fragment_add_seq_single_move(table, pinfo, id,
2583 data, tmp_offset);
2586 frag_number = 0; /* For the rest of the function */
2587 } else {
2588 for (frag_number=1; frag_number < max_frags; frag_number++) {
2589 fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL);
2590 if ((flags & REASSEMBLE_FLAGS_AGING) &&
2591 fh && ((fh->frame + max_age) < pinfo->num)) {
2592 old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data);
2593 if (old_tvb_data)
2594 tvb_free(old_tvb_data);
2595 fh = NULL;
2597 if (fh != NULL) {
2598 if (fh->flags & FD_DATALEN_SET &&
2599 fh->datalen < frag_number) {
2600 /* This fragment is after the Last
2601 * fragment, so must go after here. */
2602 fh = NULL;
2604 break;
2607 if (fh == NULL) { /* Didn't find location, use default */
2608 frag_number = 1;
2609 /* Already looked for frag_number 1, so just create */
2610 fh = new_head(FD_BLOCKSEQUENCE);
2611 insert_fd_head(table, fh, pinfo, id-frag_number, data);
2614 if (last) {
2615 /* Look for fragments past the end set by this Last fragment. */
2616 prev_fd = NULL;
2617 for (fd=fh->next; fd && fd->offset <= frag_number; fd=fd->next) {
2618 prev_fd = fd;
2620 /* fd is now all fragments offset > frag_number (the Last).
2621 * It shouldn't have a fragment with offset frag_number+1,
2622 * as that would be a First fragment not marked as such.
2623 * However, this can happen if we had unreassembled fragments
2624 * (missing, or at the start of the capture) and we've also
2625 * looped around on the sequence numbers. It can also happen
2626 * if bit errors mess up Last or First. */
2627 if (fd != NULL) {
2628 if (prev_fd) {
2629 prev_fd->next = NULL;
2630 } else {
2631 fh->next = NULL;
2633 fragment_items_removed(fh, prev_fd);
2634 fh->frame = 0;
2635 for (prev_fd=fh->next; prev_fd; prev_fd=prev_fd->next) {
2636 if (fh->frame < prev_fd->frame) {
2637 fh->frame = prev_fd->frame;
2640 while (fd && fd->offset == frag_number+1) {
2641 /* Definitely have bad data here. Best to
2642 * delete these and leave unreassembled. */
2643 fragment_item *tmp_fd;
2644 tmp_fd=fd->next;
2646 if (fd->tvb_data && !(fd->flags & FD_SUBSET_TVB))
2647 tvb_free(fd->tvb_data);
2648 g_slice_free(fragment_item, fd);
2649 fd=tmp_fd;
2652 if (fd != NULL) {
2653 /* Move these onto the next frame. */
2654 new_fh = lookup_fd_head(table, pinfo, id+1, data, NULL);
2655 if (new_fh==NULL) {
2656 /* Not found. Create list-head. */
2657 new_fh = new_head(FD_BLOCKSEQUENCE);
2658 insert_fd_head(table, new_fh, pinfo, id+1, data);
2660 tmp_offset = 0;
2661 for (prev_fd = fd; prev_fd; prev_fd = prev_fd->next) {
2662 prev_fd->offset -= (frag_number+1);
2663 tmp_offset = prev_fd->offset;
2664 if (new_fh->frame < fd->frame) {
2665 new_fh->frame = fd->frame;
2668 MERGE_FRAG(new_fh, fd);
2669 /* If we previously found a different Last fragment,
2670 * transfer that information to the new reassembly. */
2671 if (fh->flags & FD_DATALEN_SET &&
2672 fh->datalen > frag_number) {
2673 new_fh->flags |= FD_DATALEN_SET;
2674 new_fh->datalen = fh->datalen - (frag_number+1);
2675 fh->flags &= ~FD_DATALEN_SET;
2676 fh->datalen = 0;
2677 } else {
2678 /* Look forward and take off the next (this is
2679 * necessary in some edge cases where max_frags
2680 * prevented some fragments from going on the
2681 * previous First, but they can go on this one. */
2682 fragment_add_seq_single_move(table, pinfo, id+1,
2683 data, tmp_offset);
2686 } else {
2687 fragment_add_seq_single_move(table, pinfo, id-frag_number, data,
2688 frag_number+1);
2690 /* Having cleaned up everything, finally ready to add our new
2691 * fragment. Note that only this will ever complete a reassembly. */
2692 fh = fragment_add_seq_common(table, tvb, offset, pinfo,
2693 id-frag_number, data,
2694 frag_number, frag_data_len,
2695 !last, 0, &orig_key);
2696 if (fh) {
2698 * Reassembly is complete.
2700 * If this is in the table of in-progress reassemblies,
2701 * remove it from that table. (It could be that this
2702 * was the first and last fragment, so that no
2703 * reassembly was done.)
2705 if (orig_key != NULL)
2706 fragment_unhash(table, orig_key);
2709 * Add this item to the table of reassembled packets.
2711 fragment_reassembled_single(table, fh, pinfo, id-frag_number);
2712 return fh;
2713 } else {
2715 * Reassembly isn't complete.
2717 return NULL;
2721 fragment_head *
2722 fragment_add_seq_single(reassembly_table *table, tvbuff_t *tvb,
2723 const int offset, const packet_info *pinfo,
2724 const uint32_t id, const void* data,
2725 const uint32_t frag_data_len,
2726 const bool first, const bool last,
2727 const uint32_t max_frags)
2729 return fragment_add_seq_single_work(table, tvb, offset, pinfo,
2730 id, data, frag_data_len,
2731 first, last, max_frags, 0, 0);
2734 fragment_head *
2735 fragment_add_seq_single_aging(reassembly_table *table, tvbuff_t *tvb,
2736 const int offset, const packet_info *pinfo,
2737 const uint32_t id, const void* data,
2738 const uint32_t frag_data_len,
2739 const bool first, const bool last,
2740 const uint32_t max_frags, const uint32_t max_age)
2742 return fragment_add_seq_single_work(table, tvb, offset, pinfo,
2743 id, data, frag_data_len,
2744 first, last, max_frags, max_age,
2745 REASSEMBLE_FLAGS_AGING);
2748 void
2749 fragment_start_seq_check(reassembly_table *table, const packet_info *pinfo,
2750 const uint32_t id, const void *data,
2751 const uint32_t tot_len)
2753 fragment_head *fd_head;
2755 /* Have we already seen this frame ?*/
2756 if (pinfo->fd->visited) {
2757 return;
2760 /* Check if fragment data exists */
2761 fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
2763 if (fd_head == NULL) {
2764 /* Create list-head. */
2765 fd_head = g_slice_new(fragment_head);
2766 fd_head->next = NULL;
2767 fd_head->first_gap = NULL;
2768 fd_head->contiguous_len = 0;
2769 fd_head->frame = 0;
2770 fd_head->len = 0;
2771 fd_head->fragment_nr_offset = 0;
2772 fd_head->datalen = tot_len;
2773 fd_head->reassembled_in = 0;
2774 fd_head->reas_in_layer_num = 0;
2775 fd_head->flags = FD_BLOCKSEQUENCE|FD_DATALEN_SET;
2776 fd_head->tvb_data = NULL;
2777 fd_head->error = NULL;
2779 insert_fd_head(table, fd_head, pinfo, id, data);
2783 fragment_head *
2784 fragment_end_seq_next(reassembly_table *table, const packet_info *pinfo,
2785 const uint32_t id, const void *data)
2787 reassembled_key reass_key;
2788 reassembled_key *new_key;
2789 fragment_head *fd_head;
2790 fragment_item *fd;
2791 void *orig_key;
2792 uint32_t max_offset = 0;
2795 * Have we already seen this frame?
2796 * If so, look for it in the table of reassembled packets.
2798 if (pinfo->fd->visited) {
2799 reass_key.frame = pinfo->num;
2800 reass_key.id = id;
2801 return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
2804 fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
2806 if (fd_head) {
2807 for (fd = fd_head->next; fd; fd = fd->next) {
2808 if (fd->offset > max_offset) {
2809 max_offset = fd->offset;
2812 fd_head->datalen = max_offset;
2813 fd_head->flags |= FD_DATALEN_SET;
2815 fragment_defragment_and_free (fd_head, pinfo);
2818 * Remove this from the table of in-progress reassemblies,
2819 * and free up any memory used for it in that table.
2821 fragment_unhash(table, orig_key);
2824 * Add this item to the table of reassembled packets.
2826 fragment_reassembled(table, fd_head, pinfo, id);
2827 if (fd_head->next != NULL) {
2828 new_key = g_slice_new(reassembled_key);
2829 new_key->frame = pinfo->num;
2830 new_key->id = id;
2831 reassembled_table_insert(table->reassembled_table, new_key, fd_head);
2834 return fd_head;
2835 } else {
2837 * Fragment data not found.
2839 return NULL;
2844 * Process reassembled data; if we're on the frame in which the data
2845 * was reassembled, put the fragment information into the protocol
2846 * tree, and construct a tvbuff with the reassembled data, otherwise
2847 * just put a "reassembled in" item into the protocol tree.
2848 * offset from start of tvb, result up to end of tvb
2850 tvbuff_t *
2851 process_reassembled_data(tvbuff_t *tvb, const int offset, packet_info *pinfo,
2852 const char *name, fragment_head *fd_head, const fragment_items *fit,
2853 bool *update_col_infop, proto_tree *tree)
2855 tvbuff_t *next_tvb;
2856 bool update_col_info;
2857 proto_item *frag_tree_item;
2859 if (fd_head != NULL && pinfo->num == fd_head->reassembled_in && pinfo->curr_layer_num == fd_head->reas_in_layer_num) {
2861 * OK, we've reassembled this.
2862 * Is this something that's been reassembled from more
2863 * than one fragment?
2865 if (fd_head->next != NULL) {
2867 * Yes.
2868 * Allocate a new tvbuff, referring to the
2869 * reassembled payload, and set
2870 * the tvbuff to the list of tvbuffs to which
2871 * the tvbuff we were handed refers, so it'll get
2872 * cleaned up when that tvbuff is cleaned up.
2874 next_tvb = tvb_new_chain(tvb, fd_head->tvb_data);
2876 /* Add the defragmented data to the data source list. */
2877 add_new_data_source(pinfo, next_tvb, name);
2879 /* show all fragments */
2880 if (fd_head->flags & FD_BLOCKSEQUENCE) {
2881 update_col_info = !show_fragment_seq_tree(
2882 fd_head, fit, tree, pinfo, next_tvb, &frag_tree_item);
2883 } else {
2884 update_col_info = !show_fragment_tree(fd_head,
2885 fit, tree, pinfo, next_tvb, &frag_tree_item);
2887 } else {
2889 * No.
2890 * Return a tvbuff with the payload, a subset of the
2891 * tvbuff passed in. (The dissector SHOULD pass in
2892 * the correct tvbuff and offset.)
2894 int len;
2895 /* For FD_BLOCKSEQUENCE, len is the length in bytes,
2896 * datalen is the number of fragments.
2898 if (fd_head->flags & FD_BLOCKSEQUENCE) {
2899 len = fd_head->len;
2900 } else {
2901 // XXX Do the non-seq functions have this optimization?
2902 len = fd_head->datalen;
2904 next_tvb = tvb_new_subset_length(tvb, offset, len);
2905 pinfo->fragmented = false; /* one-fragment packet */
2906 update_col_info = true;
2908 if (update_col_infop != NULL)
2909 *update_col_infop = update_col_info;
2910 } else {
2912 * We don't have the complete reassembled payload, or this
2913 * isn't the final frame of that payload.
2915 next_tvb = NULL;
2918 * If we know what frame this was reassembled in,
2919 * and if there's a field to use for the number of
2920 * the frame in which the packet was reassembled,
2921 * add it to the protocol tree.
2923 if (fd_head != NULL && fit->hf_reassembled_in != NULL) {
2924 proto_item *fei = proto_tree_add_uint(tree,
2925 *(fit->hf_reassembled_in), tvb,
2926 0, 0, fd_head->reassembled_in);
2927 proto_item_set_generated(fei);
2930 return next_tvb;
2934 * Show a single fragment in a fragment subtree, and put information about
2935 * it in the top-level item for that subtree.
2937 static void
2938 show_fragment(fragment_item *fd, const int offset, const fragment_items *fit,
2939 proto_tree *ft, proto_item *fi, const bool first_frag,
2940 const uint32_t count, tvbuff_t *tvb, packet_info *pinfo)
2942 proto_item *fei=NULL;
2943 int hf;
2945 if (first_frag) {
2946 char *name;
2947 if (count == 1) {
2948 name = g_strdup(proto_registrar_get_name(*(fit->hf_fragment)));
2949 } else {
2950 name = g_strdup(proto_registrar_get_name(*(fit->hf_fragments)));
2952 proto_item_set_text(fi, "%u %s (%u byte%s): ", count, name, tvb_captured_length(tvb),
2953 plurality(tvb_captured_length(tvb), "", "s"));
2954 g_free(name);
2955 } else {
2956 proto_item_append_text(fi, ", ");
2958 proto_item_append_text(fi, "#%u(%u)", fd->frame, fd->len);
2960 if (fd->flags & (FD_OVERLAPCONFLICT
2961 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
2962 hf = *(fit->hf_fragment_error);
2963 } else {
2964 hf = *(fit->hf_fragment);
2966 if (fd->len == 0) {
2967 fei = proto_tree_add_uint_format(ft, hf,
2968 tvb, offset, fd->len,
2969 fd->frame,
2970 "Frame: %u (no data)",
2971 fd->frame);
2972 } else {
2973 fei = proto_tree_add_uint_format(ft, hf,
2974 tvb, offset, fd->len,
2975 fd->frame,
2976 "Frame: %u, payload: %u-%u (%u byte%s)",
2977 fd->frame,
2978 offset,
2979 offset+fd->len-1,
2980 fd->len,
2981 plurality(fd->len, "", "s"));
2983 proto_item_set_generated(fei);
2984 mark_frame_as_depended_upon(pinfo->fd, fd->frame);
2985 if (fd->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT
2986 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
2987 /* this fragment has some flags set, create a subtree
2988 * for it and display the flags.
2990 proto_tree *fet=NULL;
2992 fet = proto_item_add_subtree(fei, *(fit->ett_fragment));
2993 if (fd->flags&FD_OVERLAP) {
2994 fei=proto_tree_add_boolean(fet,
2995 *(fit->hf_fragment_overlap),
2996 tvb, 0, 0,
2997 true);
2998 proto_item_set_generated(fei);
3000 if (fd->flags&FD_OVERLAPCONFLICT) {
3001 fei=proto_tree_add_boolean(fet,
3002 *(fit->hf_fragment_overlap_conflict),
3003 tvb, 0, 0,
3004 true);
3005 proto_item_set_generated(fei);
3007 if (fd->flags&FD_MULTIPLETAILS) {
3008 fei=proto_tree_add_boolean(fet,
3009 *(fit->hf_fragment_multiple_tails),
3010 tvb, 0, 0,
3011 true);
3012 proto_item_set_generated(fei);
3014 if (fd->flags&FD_TOOLONGFRAGMENT) {
3015 fei=proto_tree_add_boolean(fet,
3016 *(fit->hf_fragment_too_long_fragment),
3017 tvb, 0, 0,
3018 true);
3019 proto_item_set_generated(fei);
3024 static bool
3025 show_fragment_errs_in_col(fragment_head *fd_head, const fragment_items *fit,
3026 packet_info *pinfo)
3028 if (fd_head->flags & (FD_OVERLAPCONFLICT
3029 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
3030 col_add_fstr(pinfo->cinfo, COL_INFO, "[Illegal %s]", fit->tag);
3031 return true;
3034 return false;
3037 /* This function will build the fragment subtree; it's for fragments
3038 reassembled with "fragment_add()".
3040 It will return true if there were fragmentation errors
3041 or false if fragmentation was ok.
3043 bool
3044 show_fragment_tree(fragment_head *fd_head, const fragment_items *fit,
3045 proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi)
3047 fragment_item *fd;
3048 proto_tree *ft;
3049 bool first_frag;
3050 uint32_t count = 0;
3051 /* It's not fragmented. */
3052 pinfo->fragmented = false;
3054 *fi = proto_tree_add_item(tree, *(fit->hf_fragments), tvb, 0, -1, ENC_NA);
3055 proto_item_set_generated(*fi);
3057 ft = proto_item_add_subtree(*fi, *(fit->ett_fragments));
3058 first_frag = true;
3059 for (fd = fd_head->next; fd != NULL; fd = fd->next) {
3060 count++;
3062 for (fd = fd_head->next; fd != NULL; fd = fd->next) {
3063 show_fragment(fd, fd->offset, fit, ft, *fi, first_frag, count, tvb, pinfo);
3064 first_frag = false;
3067 if (fit->hf_fragment_count) {
3068 proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_fragment_count),
3069 tvb, 0, 0, count);
3070 proto_item_set_generated(fli);
3073 if (fit->hf_reassembled_length) {
3074 proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_reassembled_length),
3075 tvb, 0, 0, tvb_captured_length (tvb));
3076 proto_item_set_generated(fli);
3079 if (fit->hf_reassembled_data) {
3080 proto_item *fli = proto_tree_add_item(ft, *(fit->hf_reassembled_data),
3081 tvb, 0, tvb_captured_length(tvb), ENC_NA);
3082 proto_item_set_generated(fli);
3085 return show_fragment_errs_in_col(fd_head, fit, pinfo);
3088 /* This function will build the fragment subtree; it's for fragments
3089 reassembled with "fragment_add_seq()" or "fragment_add_seq_check()".
3091 It will return true if there were fragmentation errors
3092 or false if fragmentation was ok.
3094 bool
3095 show_fragment_seq_tree(fragment_head *fd_head, const fragment_items *fit,
3096 proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi)
3098 uint32_t offset, next_offset, count = 0;
3099 fragment_item *fd, *last_fd;
3100 proto_tree *ft;
3101 bool first_frag;
3103 /* It's not fragmented. */
3104 pinfo->fragmented = false;
3106 *fi = proto_tree_add_item(tree, *(fit->hf_fragments), tvb, 0, -1, ENC_NA);
3107 proto_item_set_generated(*fi);
3109 ft = proto_item_add_subtree(*fi, *(fit->ett_fragments));
3110 offset = 0;
3111 next_offset = 0;
3112 last_fd = NULL;
3113 first_frag = true;
3114 for (fd = fd_head->next; fd != NULL; fd = fd->next){
3115 count++;
3117 for (fd = fd_head->next; fd != NULL; fd = fd->next){
3118 if (last_fd == NULL || last_fd->offset != fd->offset) {
3119 offset = next_offset;
3120 next_offset += fd->len;
3122 last_fd = fd;
3123 show_fragment(fd, offset, fit, ft, *fi, first_frag, count, tvb, pinfo);
3124 first_frag = false;
3127 if (fit->hf_fragment_count) {
3128 proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_fragment_count),
3129 tvb, 0, 0, count);
3130 proto_item_set_generated(fli);
3133 if (fit->hf_reassembled_length) {
3134 proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_reassembled_length),
3135 tvb, 0, 0, tvb_captured_length (tvb));
3136 proto_item_set_generated(fli);
3139 if (fit->hf_reassembled_data) {
3140 proto_item *fli = proto_tree_add_item(ft, *(fit->hf_reassembled_data),
3141 tvb, 0, tvb_captured_length(tvb), ENC_NA);
3142 proto_item_set_generated(fli);
3145 return show_fragment_errs_in_col(fd_head, fit, pinfo);
3148 static void
3149 reassembly_table_init_reg_table(void *p, void *user_data _U_)
3151 register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p;
3152 reassembly_table_init(reg_table->table, reg_table->funcs);
3155 static void
3156 reassembly_table_init_reg_tables(void)
3158 g_list_foreach(reassembly_table_list, reassembly_table_init_reg_table, NULL);
3161 static void
3162 reassembly_table_cleanup_reg_table(void *p, void *user_data _U_)
3164 register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p;
3165 reassembly_table_destroy(reg_table->table);
3168 static void
3169 reassembly_table_cleanup_reg_tables(void)
3171 g_list_foreach(reassembly_table_list, reassembly_table_cleanup_reg_table, NULL);
3174 void reassembly_tables_init(void)
3176 register_init_routine(&reassembly_table_init_reg_tables);
3177 register_cleanup_routine(&reassembly_table_cleanup_reg_tables);
3180 static void
3181 reassembly_table_free(void *p, void *user_data _U_)
3183 register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p;
3184 reassembly_table_destroy(reg_table->table);
3185 g_free(reg_table);
3188 void
3189 reassembly_table_cleanup(void)
3191 g_list_foreach(reassembly_table_list, reassembly_table_free, NULL);
3192 g_list_free(reassembly_table_list);
3195 /* One instance of this structure is created for each pdu that spans across
3196 * multiple segments. (MSP) */
3197 typedef struct _multisegment_pdu_t {
3198 uint64_t first_frame;
3199 uint64_t last_frame;
3200 unsigned start_offset_at_first_frame;
3201 unsigned end_offset_at_last_frame;
3202 int length; /* length of this MSP */
3203 uint32_t streaming_reassembly_id;
3204 /* pointer to previous multisegment_pdu */
3205 struct _multisegment_pdu_t* prev_msp;
3206 } multisegment_pdu_t;
3208 /* struct for keeping the reassembly information of each stream */
3209 struct streaming_reassembly_info_t {
3210 /* This map is keyed by frame num and keeps track of all MSPs for this
3211 * stream. Different frames will point to the same MSP if they contain
3212 * part data of this MSP. If a frame contains data that
3213 * belongs to two MSPs, it will point to the second MSP. */
3214 wmem_map_t* multisegment_pdus;
3215 /* This map is keyed by frame num and keeps track of the frag_offset
3216 * of the first byte of frames for fragment_add() after first scan. */
3217 wmem_map_t* frame_num_frag_offset_map;
3218 /* how many bytes the current uncompleted MSP still needs. (only valid for first scan) */
3219 int prev_deseg_len;
3220 /* the current uncompleted MSP (only valid for first scan) */
3221 multisegment_pdu_t* last_msp;
3224 static uint32_t
3225 create_streaming_reassembly_id(void)
3227 static uint32_t global_streaming_reassembly_id = 0;
3228 return ++global_streaming_reassembly_id;
3231 streaming_reassembly_info_t*
3232 streaming_reassembly_info_new(void)
3234 return wmem_new0(wmem_file_scope(), streaming_reassembly_info_t);
3237 /* Following is an example of ProtoA and ProtoB protocols from the declaration of this function in 'reassemble.h':
3239 * +------------------ A Multisegment PDU of ProtoB ----------------------+
3240 * | |
3241 * +--- ProtoA payload1 ---+ +- payload2 -+ +- Payload3 -+ +- Payload4 -+ +- ProtoA payload5 -+
3242 * | EoMSP | OmNFP | BoMSP | | MoMSP | | MoMSP | | MoMSP | | EoMSP | BoMSP |
3243 * +-------+-------+-------+ +------------+ +------------+ +------------+ +---------+---------+
3244 * | |
3245 * +----------------------------------------------------------------------+
3247 * For a ProtoA payload composed of EoMSP + OmNFP + BoMSP will call fragment_add() twice on EoMSP and BoMSP; and call
3248 * process_reassembled_data() once for generating tvb of a MSP to which EoMSP belongs; and call subdissector twice on
3249 * reassembled MSP of EoMSP and OmNFP + BoMSP. After that finds BoMSP is a beginning of a MSP at first scan.
3251 * The rules are:
3253 * - If a ProtoA payload contains EoMSP, we will need call fragment_add(), process_reassembled_data() and subdissector
3254 * once on it to end a MSP. (May run twice or more times at first scan, because subdissector may only return the
3255 * head length of message by pinfo->desegment_len. We need run second time for subdissector to determine the length
3256 * of entire message).
3258 * - If a ProtoA payload contains OmNFP, we will need only call subdissector once on it. The subdissector need dissect
3259 * all non-fragment PDUs in it. (no desegment_len should output)
3261 * - If a ProtoA payload contains BoMSP, we will need call subdissector once on BoMSP or OmNFP+BoMSP (because unknown
3262 * during first scan). The subdissector will output desegment_len (!= 0). Then we will call fragment_add()
3263 * with a new reassembly id on BoMSP for starting a new MSP.
3265 * - If a ProtoA payload only contains MoMSP (entire payload is part of a MSP), we will only call fragment_add() once
3266 * or twice (at first scan) on it. The subdissector will not be called.
3268 * In this implementation, only multisegment PDUs are recorded in multisegment_pdus map keyed by the numbers (uint64_t)
3269 * of frames belongs to MSPs. Each MSP in the map has a pointer referred to previous MSP, because we may need
3270 * two MSPs to dissect a ProtoA payload that contains EoMSP + BoMSP at the same time. The multisegment_pdus map is built
3271 * during first scan (pinfo->visited == false) with help of prev_deseg_len and last_msp fields of streaming_reassembly_info_t
3272 * for each direction of a ProtoA STREAM. The prev_deseg_len record how many bytes of subsequent ProtoA payloads belong to
3273 * previous PDU during first scan. The last_msp member of streaming_reassembly_info_t is always point to last MSP which
3274 * is created during scan previous or early ProtoA payloads. Since subdissector might return only the head length of entire
3275 * message (by pinfo->desegment_len) when there is not enough data to determine the message length, we need to reopen
3276 * reassembly fragments for adding more bytes during scanning the next ProtoA payload. We have to use fragment_add()
3277 * instead of fragment_add_check() or fragment_add_seq_next().
3279 * Read more: please refer to comments of the declaration of this function in 'reassemble.h'.
3282 reassemble_streaming_data_and_call_subdissector(
3283 tvbuff_t* tvb, packet_info* pinfo, unsigned offset, int length,
3284 proto_tree* segment_tree, proto_tree* reassembled_tree, reassembly_table streaming_reassembly_table,
3285 streaming_reassembly_info_t* reassembly_info, uint64_t cur_frame_num,
3286 dissector_handle_t subdissector_handle, proto_tree* subdissector_tree, void* subdissector_data,
3287 const char* label, const fragment_items* frag_hf_items, int hf_segment_data
3290 int orig_length = length;
3291 int datalen = 0;
3292 int bytes_belong_to_prev_msp = 0; /* bytes belong to previous MSP */
3293 uint32_t reassembly_id = 0, frag_offset = 0;
3294 fragment_head* head = NULL;
3295 bool need_more = false;
3296 bool found_BoMSP = false;
3297 multisegment_pdu_t* cur_msp = NULL, * prev_msp = NULL;
3298 uint16_t save_can_desegment;
3299 int save_desegment_offset;
3300 uint32_t save_desegment_len;
3301 uint64_t* frame_ptr;
3303 save_can_desegment = pinfo->can_desegment;
3304 save_desegment_offset = pinfo->desegment_offset;
3305 save_desegment_len = pinfo->desegment_len;
3307 /* calculate how many bytes of this payload belongs to previous MSP (EoMSP) */
3308 if (!PINFO_FD_VISITED(pinfo)) {
3309 /* this is first scan */
3310 if (reassembly_info->prev_deseg_len == DESEGMENT_ONE_MORE_SEGMENT) {
3311 /* assuming the entire tvb belongs to the previous MSP */
3312 bytes_belong_to_prev_msp = length;
3313 reassembly_info->prev_deseg_len = length;
3314 } else if (reassembly_info->prev_deseg_len > 0) {
3315 /* part or all of current payload belong to previous MSP */
3316 bytes_belong_to_prev_msp = MIN(reassembly_info->prev_deseg_len, length);
3317 reassembly_info->prev_deseg_len -= bytes_belong_to_prev_msp;
3318 need_more = (reassembly_info->prev_deseg_len > 0);
3319 } /* else { beginning of a new PDU (might be a NFP or MSP) } */
3321 if (bytes_belong_to_prev_msp > 0) {
3322 DISSECTOR_ASSERT(reassembly_info->last_msp != NULL);
3323 reassembly_id = reassembly_info->last_msp->streaming_reassembly_id;
3324 frag_offset = reassembly_info->last_msp->length;
3325 if (reassembly_info->frame_num_frag_offset_map == NULL) {
3326 reassembly_info->frame_num_frag_offset_map = wmem_map_new(wmem_file_scope(), g_int64_hash, g_int64_equal);
3328 frame_ptr = (uint64_t*)wmem_memdup(wmem_file_scope(), &cur_frame_num, sizeof(uint64_t));
3329 wmem_map_insert(reassembly_info->frame_num_frag_offset_map, frame_ptr, GUINT_TO_POINTER(frag_offset));
3330 /* This payload contains the data of previous msp, so we point to it. That may be overridden late. */
3331 wmem_map_insert(reassembly_info->multisegment_pdus, frame_ptr, reassembly_info->last_msp);
3333 } else {
3334 /* not first scan, use information of multisegment_pdus built during first scan */
3335 if (reassembly_info->multisegment_pdus) {
3336 cur_msp = (multisegment_pdu_t*)wmem_map_lookup(reassembly_info->multisegment_pdus, &cur_frame_num);
3338 if (cur_msp) {
3339 if (cur_msp->first_frame == cur_frame_num) {
3340 /* Current payload contains a beginning of a MSP. (BoMSP)
3341 * The cur_msp contains information about the beginning MSP.
3342 * If prev_msp is not null, that means this payload also contains
3343 * the last part of previous MSP. (EoMSP) */
3344 prev_msp = cur_msp->prev_msp;
3345 } else {
3346 /* Current payload is not a first frame of a MSP (not include BoMSP). */
3347 prev_msp = cur_msp;
3348 cur_msp = NULL;
3352 if (prev_msp && prev_msp->last_frame >= cur_frame_num) {
3353 if (prev_msp->last_frame == cur_frame_num) {
3354 /* this payload contains part of previous MSP (contains EoMSP) */
3355 bytes_belong_to_prev_msp = prev_msp->end_offset_at_last_frame - offset;
3356 } else { /* if (prev_msp->last_frame > cur_frame_num) */
3357 /* this payload all belongs to previous MSP */
3358 bytes_belong_to_prev_msp = length;
3359 need_more = true;
3361 reassembly_id = prev_msp->streaming_reassembly_id;
3363 if (reassembly_info->frame_num_frag_offset_map) {
3364 frag_offset = GPOINTER_TO_UINT(wmem_map_lookup(reassembly_info->frame_num_frag_offset_map, &cur_frame_num));
3368 /* handling EoMSP or MoMSP (entire payload being middle part of a MSP) */
3369 while (bytes_belong_to_prev_msp > 0) {
3370 tvbuff_t* reassembled_tvb = NULL;
3371 DISSECTOR_ASSERT(reassembly_id > 0);
3372 pinfo->can_desegment = 2; /* this will be decreased one while passing to subdissector */
3373 pinfo->desegment_offset = 0;
3374 pinfo->desegment_len = 0;
3376 head = fragment_add(&streaming_reassembly_table, tvb, offset, pinfo, reassembly_id, NULL,
3377 frag_offset, bytes_belong_to_prev_msp, need_more);
3379 if (head) {
3380 if (frag_hf_items->hf_reassembled_in) {
3381 proto_item_set_generated(
3382 proto_tree_add_uint(segment_tree, *(frag_hf_items->hf_reassembled_in), tvb, offset,
3383 bytes_belong_to_prev_msp, head->reassembled_in)
3387 if (!need_more) {
3388 reassembled_tvb = process_reassembled_data(tvb, offset, pinfo,
3389 wmem_strdup_printf(pinfo->pool, "Reassembled %s", label),
3390 head, frag_hf_items, NULL, reassembled_tree);
3394 proto_tree_add_bytes_format(segment_tree, hf_segment_data, tvb, offset,
3395 bytes_belong_to_prev_msp, NULL, "%s Segment data (%u byte%s)", label,
3396 bytes_belong_to_prev_msp, plurality(bytes_belong_to_prev_msp, "", "s"));
3398 if (reassembled_tvb) {
3399 /* normally, this stage will dissect one or more completed pdus */
3400 /* Note, don't call_dissector_with_data because sometime the pinfo->curr_layer_num will changed
3401 * after calling that will make reassembly failed! */
3402 call_dissector_only(subdissector_handle, reassembled_tvb, pinfo, subdissector_tree, subdissector_data);
3405 if (pinfo->desegment_len) {
3406 /* that must only happen during first scan the reassembly_info->prev_deseg_len might be only the
3407 * head length of entire message. */
3408 DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo));
3409 DISSECTOR_ASSERT_HINT(pinfo->desegment_len != DESEGMENT_UNTIL_FIN, "Subdissector MUST NOT "
3410 "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to "
3411 " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined.");
3413 if (pinfo->desegment_offset > 0) {
3414 DISSECTOR_ASSERT_HINT(pinfo->desegment_offset > reassembly_info->last_msp->length
3415 && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp,
3416 wmem_strdup_printf(pinfo->pool,
3417 "Subdissector MUST NOT set pinfo->desegment_offset(%d) in previous or next part of MSP, must between (%d, %d).",
3418 pinfo->desegment_offset, reassembly_info->last_msp->length, reassembly_info->last_msp->length + bytes_belong_to_prev_msp));
3420 /* shorten the bytes_belong_to_prev_msp and just truncate the reassembled tvb */
3421 bytes_belong_to_prev_msp = pinfo->desegment_offset - reassembly_info->last_msp->length;
3422 fragment_truncate(&streaming_reassembly_table, pinfo, reassembly_id, NULL, pinfo->desegment_offset);
3423 found_BoMSP = true;
3424 } else {
3425 if (pinfo->desegment_len == DESEGMENT_ONE_MORE_SEGMENT) {
3426 /* just need more bytes, all remaining bytes belongs to previous MSP (to run fragment_add again) */
3427 bytes_belong_to_prev_msp = length;
3430 /* Remove the data added by previous fragment_add(), and reopen fragments for adding more bytes. */
3431 fragment_truncate(&streaming_reassembly_table, pinfo, reassembly_id, NULL, reassembly_info->last_msp->length);
3432 fragment_set_partial_reassembly(&streaming_reassembly_table, pinfo, reassembly_id, NULL);
3434 reassembly_info->prev_deseg_len = bytes_belong_to_prev_msp + pinfo->desegment_len;
3435 bytes_belong_to_prev_msp = MIN(reassembly_info->prev_deseg_len, length);
3436 reassembly_info->prev_deseg_len -= bytes_belong_to_prev_msp;
3437 need_more = (reassembly_info->prev_deseg_len > 0);
3438 continue;
3442 if (pinfo->desegment_len == 0 || found_BoMSP) {
3443 /* We will arrive here, only when the MSP is defragmented and dissected or this
3444 * payload all belongs to previous MSP (only fragment_add() with need_more=true called)
3445 * or BoMSP is parsed while pinfo->desegment_offset > 0 and pinfo->desegment_len != 0
3447 offset += bytes_belong_to_prev_msp;
3448 length -= bytes_belong_to_prev_msp;
3449 DISSECTOR_ASSERT(length >= 0);
3450 if (!PINFO_FD_VISITED(pinfo)) {
3451 reassembly_info->last_msp->length += bytes_belong_to_prev_msp;
3454 if (!PINFO_FD_VISITED(pinfo) && reassembled_tvb) {
3455 /* completed current msp */
3456 reassembly_info->last_msp->last_frame = cur_frame_num;
3457 reassembly_info->last_msp->end_offset_at_last_frame = offset;
3458 reassembly_info->prev_deseg_len = pinfo->desegment_len;
3460 bytes_belong_to_prev_msp = 0; /* break */
3464 /* to find and handle OmNFP, and find BoMSP at first scan. */
3465 if (length > 0 && !found_BoMSP) {
3466 if (!PINFO_FD_VISITED(pinfo)) {
3467 /* It is first scan, to dissect remaining bytes to find whether it is OmNFP only, or BoMSP only or OmNFP + BoMSP. */
3468 datalen = length;
3469 DISSECTOR_ASSERT(cur_msp == NULL);
3470 } else {
3471 /* Not first scan */
3472 if (cur_msp) {
3473 /* There's a BoMSP. Let's calculate the length of OmNFP between EoMSP and BoMSP */
3474 datalen = cur_msp->start_offset_at_first_frame - offset; /* if result is zero that means no OmNFP */
3475 } else {
3476 /* This payload is not a beginning of MSP. The remaining bytes all belong to OmNFP without BoMSP */
3477 datalen = length;
3480 DISSECTOR_ASSERT(datalen >= 0);
3482 /* Dissect the remaining of this payload. If (datalen == 0) means remaining only have one BoMSP without OmNFP. */
3483 if (datalen > 0) {
3484 /* we dissect if it is not dissected before or it is a non-fragment pdu (between two multisegment pdus) */
3485 pinfo->can_desegment = 2;
3486 pinfo->desegment_offset = 0;
3487 pinfo->desegment_len = 0;
3489 call_dissector_only(subdissector_handle, tvb_new_subset_length(tvb, offset, datalen),
3490 pinfo, subdissector_tree, subdissector_data);
3492 if (pinfo->desegment_len) {
3493 DISSECTOR_ASSERT_HINT(pinfo->desegment_len != DESEGMENT_UNTIL_FIN, "Subdissector MUST NOT "
3494 "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to "
3495 " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined.");
3496 /* only happen during first scan */
3497 DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo) && datalen == length);
3498 offset += pinfo->desegment_offset;
3499 length -= pinfo->desegment_offset;
3500 } else {
3501 /* all remaining bytes are consumed by subdissector */
3502 offset += datalen;
3503 length -= datalen;
3505 if (!PINFO_FD_VISITED(pinfo)) {
3506 reassembly_info->prev_deseg_len = pinfo->desegment_len;
3508 } /* else all remaining bytes (BoMSP) belong to a new MSP */
3509 DISSECTOR_ASSERT(length >= 0);
3512 /* handling BoMSP */
3513 if (length > 0) {
3514 col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "[%s segment of a reassembled PDU] ", label);
3515 if (!PINFO_FD_VISITED(pinfo)) {
3516 /* create a msp for current frame during first scan */
3517 cur_msp = wmem_new0(wmem_file_scope(), multisegment_pdu_t);
3518 cur_msp->first_frame = cur_frame_num;
3519 cur_msp->last_frame = UINT64_MAX;
3520 cur_msp->start_offset_at_first_frame = offset;
3521 cur_msp->length = length;
3522 cur_msp->streaming_reassembly_id = reassembly_id = create_streaming_reassembly_id();
3523 cur_msp->prev_msp = reassembly_info->last_msp;
3524 reassembly_info->last_msp = cur_msp;
3525 if (reassembly_info->multisegment_pdus == NULL) {
3526 reassembly_info->multisegment_pdus = wmem_map_new(wmem_file_scope(), g_int64_hash, g_int64_equal);
3528 frame_ptr = (uint64_t*)wmem_memdup(wmem_file_scope(), &cur_frame_num, sizeof(uint64_t));
3529 wmem_map_insert(reassembly_info->multisegment_pdus, frame_ptr, cur_msp);
3530 } else {
3531 DISSECTOR_ASSERT(cur_msp && cur_msp->start_offset_at_first_frame == offset);
3532 reassembly_id = cur_msp->streaming_reassembly_id;
3534 /* add first fragment of the new MSP to reassembly table */
3535 head = fragment_add(&streaming_reassembly_table, tvb, offset, pinfo, reassembly_id,
3536 NULL, 0, length, true);
3538 if (head && frag_hf_items->hf_reassembled_in) {
3539 proto_item_set_generated(
3540 proto_tree_add_uint(segment_tree, *(frag_hf_items->hf_reassembled_in),
3541 tvb, offset, length, head->reassembled_in)
3544 proto_tree_add_bytes_format(segment_tree, hf_segment_data, tvb, offset, length,
3545 NULL, "%s Segment data (%u byte%s)", label, length, plurality(length, "", "s"));
3548 pinfo->can_desegment = save_can_desegment;
3549 pinfo->desegment_offset = save_desegment_offset;
3550 pinfo->desegment_len = save_desegment_len;
3552 return orig_length;
3556 additional_bytes_expected_to_complete_reassembly(streaming_reassembly_info_t* reassembly_info)
3558 return reassembly_info->prev_deseg_len;
3562 * Editor modelines - https://www.wireshark.org/tools/modelines.html
3564 * Local variables:
3565 * c-basic-offset: 8
3566 * tab-width: 8
3567 * indent-tabs-mode: t
3568 * End:
3570 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
3571 * :indentSize=8:tabSize=8:noTabs=false: