2 * Routines for {fragment,segment} reassembly
4 * $Id: reassemble.c,v 1.39 2003/06/04 05:41:37 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <epan/packet.h>
33 #include "reassemble.h"
36 typedef struct _fragment_key {
42 static GMemChunk *fragment_key_chunk = NULL;
43 static GMemChunk *fragment_data_chunk = NULL;
44 static int fragment_init_count = 200;
46 #define LINK_FRAG(fd_head,fd) \
47 { fragment_data *fd_i; \
48 /* add fragment to list, keep list sorted */ \
49 for(fd_i=(fd_head);fd_i->next;fd_i=fd_i->next){ \
50 if( ((fd)->offset) < (fd_i->next->offset) ) \
53 (fd)->next=fd_i->next; \
58 fragment_equal(gconstpointer k1, gconstpointer k2)
60 const fragment_key* key1 = (const fragment_key*) k1;
61 const fragment_key* key2 = (const fragment_key*) k2;
63 /*key.id is the first item to compare since item is most
64 likely to differ between sessions, thus shortcircuiting
65 the comparasion of addresses.
67 return ( ( (key1->id == key2->id) &&
68 (ADDRESSES_EQUAL(&key1->src, &key2->src)) &&
69 (ADDRESSES_EQUAL(&key1->dst, &key2->dst))
75 fragment_hash(gconstpointer k)
77 const fragment_key* key = (const fragment_key*) k;
85 /* More than likely: in most captures src and dst addresses are the
86 same, and would hash the same.
87 We only use id as the hash as an optimization.
89 for (i = 0; i < key->src.len; i++)
90 hash_val += key->src.data[i];
91 for (i = 0; i < key->dst.len; i++)
92 hash_val += key->dst.data[i];
100 typedef struct _reassembled_key {
105 static GMemChunk *reassembled_key_chunk = NULL;
108 reassembled_equal(gconstpointer k1, gconstpointer k2)
110 const reassembled_key* key1 = (const reassembled_key*) k1;
111 const reassembled_key* key2 = (const reassembled_key*) k2;
114 * We assume that the frame numbers are unlikely to be equal,
115 * so we check them first.
117 return key1->frame == key2->frame && key1->id == key2->id;
121 reassembled_hash(gconstpointer k)
123 const reassembled_key* key = (const reassembled_key*) k;
129 * For a fragment hash table entry, free the address data to which the key
130 * refers and the fragment data to which the value refers.
131 * (The actual key and value structures get freed by "reassemble_init()".)
134 free_all_fragments(gpointer key_arg, gpointer value, gpointer user_data _U_)
136 fragment_key *key = key_arg;
137 fragment_data *fd_head;
140 * Grr. I guess the theory here is that freeing
141 * something sure as heck modifies it, so you
142 * want to ban attempts to free it, but, alas,
143 * if we make the "data" field of an "address"
144 * structure not a "const", the compiler whines if
145 * we try to make it point into the data for a packet,
146 * as that's a "const" array (and should be, as dissectors
147 * shouldn't trash it).
149 * So we cast the complaint into oblivion, and rely on
150 * the fact that these addresses are known to have had
151 * their data mallocated, i.e. they don't point into,
152 * say, the middle of the data for a packet.
154 g_free((gpointer)key->src.data);
155 g_free((gpointer)key->dst.data);
157 for (fd_head = value; fd_head != NULL; fd_head = fd_head->next) {
158 if(fd_head->data && !(fd_head->flags&FD_NOT_MALLOCED))
159 g_free(fd_head->data);
166 * For a reassembled-packet hash table entry, free the fragment data
167 * to which the value refers.
168 * (The actual value structures get freed by "reassemble_init()".)
171 free_all_reassembled_fragments(gpointer key_arg _U_, gpointer value,
172 gpointer user_data _U_)
174 fragment_data *fd_head;
176 for (fd_head = value; fd_head != NULL; fd_head = fd_head->next) {
177 if(fd_head->data && !(fd_head->flags&FD_NOT_MALLOCED)) {
178 g_free(fd_head->data);
181 * A reassembled packet is inserted into the
182 * hash table once for every frame that made
183 * up the reassembled packet; clear the data
184 * pointer so that we only free the data the
185 * first time we see it.
187 fd_head->data = NULL;
195 * Initialize a fragment table.
198 fragment_table_init(GHashTable **fragment_table)
200 if (*fragment_table != NULL) {
202 * The fragment hash table exists.
204 * Remove all entries and free fragment data for
205 * each entry. (The key and value data is freed
206 * by "reassemble_init()".)
208 g_hash_table_foreach_remove(*fragment_table,
209 free_all_fragments, NULL);
211 /* The fragment table does not exist. Create it */
212 *fragment_table = g_hash_table_new(fragment_hash,
218 * Initialize a reassembled-packet table.
221 reassembled_table_init(GHashTable **reassembled_table)
223 if (*reassembled_table != NULL) {
225 * The reassembled-packet hash table exists.
227 * Remove all entries and free reassembled packet
228 * data for each entry. (The key data is freed
229 * by "reassemble_init()".)
231 g_hash_table_foreach_remove(*reassembled_table,
232 free_all_reassembled_fragments, NULL);
234 /* The fragment table does not exist. Create it */
235 *reassembled_table = g_hash_table_new(reassembled_hash,
241 * Free up all space allocated for fragment keys and data and
245 reassemble_init(void)
247 if (fragment_key_chunk != NULL)
248 g_mem_chunk_destroy(fragment_key_chunk);
249 if (fragment_data_chunk != NULL)
250 g_mem_chunk_destroy(fragment_data_chunk);
251 if (reassembled_key_chunk != NULL)
252 g_mem_chunk_destroy(reassembled_key_chunk);
253 fragment_key_chunk = g_mem_chunk_new("fragment_key_chunk",
254 sizeof(fragment_key),
255 fragment_init_count * sizeof(fragment_key),
257 fragment_data_chunk = g_mem_chunk_new("fragment_data_chunk",
258 sizeof(fragment_data),
259 fragment_init_count * sizeof(fragment_data),
261 reassembled_key_chunk = g_mem_chunk_new("reassembled_key_chunk",
262 sizeof(reassembled_key),
263 fragment_init_count * sizeof(reassembled_key),
267 /* This function cleans up the stored state and removes the reassembly data and
268 * (with one exception) all allocated memory for matching reassembly.
271 * If the PDU was already completely reassembled, then the buffer containing the
272 * reassembled data WILL NOT be free()d, and the pointer to that buffer will be
274 * Othervise the function will return NULL.
276 * So, if you call fragment_delete and it returns non-NULL, YOU are responsible to
277 * g_free() that buffer.
280 fragment_delete(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
282 fragment_data *fd_head, *fd;
284 unsigned char *data=NULL;
286 /* create key to search hash with */
287 key.src = pinfo->src;
288 key.dst = pinfo->dst;
291 fd_head = g_hash_table_lookup(fragment_table, &key);
294 /* We do not recognize this as a PDU we have seen before. return*/
299 /* loop over all partial fragments and free any buffers */
300 for(fd=fd_head->next;fd;){
301 fragment_data *tmp_fd;
304 if( !(fd->flags&FD_NOT_MALLOCED) )
306 g_mem_chunk_free(fragment_data_chunk, fd);
309 g_mem_chunk_free(fragment_data_chunk, fd_head);
310 g_hash_table_remove(fragment_table, &key);
315 /* This function is used to check if there is partial or completed reassembly state
316 * matching this packet. I.e. Are there reassembly going on or not for this packet?
319 fragment_get(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
321 fragment_data *fd_head;
324 /* create key to search hash with */
325 key.src = pinfo->src;
326 key.dst = pinfo->dst;
329 fd_head = g_hash_table_lookup(fragment_table, &key);
334 /* This function can be used to explicitely set the total length (if known)
335 * for reassembly of a PDU.
336 * This is useful for reassembly of PDUs where one may have the total length specified
337 * in the first fragment instead of as for, say, IPv4 where a flag indicates which
338 * is the last fragment.
340 * Such protocols might fragment_add with a more_frags==TRUE for every fragment
341 * and just tell the reassembly engine the expected total length of the reassembled data
342 * using fragment_set_tot_len immediately after doing fragment_add for the first packet.
344 * note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment.
345 * i.e. since the block numbers start at 0, if we specify tot_len==2, that
346 * actually means we want to defragment 3 blocks, block 0, 1 and 2.
349 fragment_set_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table,
352 fragment_data *fd_head;
355 /* create key to search hash with */
356 key.src = pinfo->src;
357 key.dst = pinfo->dst;
360 fd_head = g_hash_table_lookup(fragment_table, &key);
363 fd_head->datalen = tot_len;
370 fragment_get_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
372 fragment_data *fd_head;
375 /* create key to search hash with */
376 key.src = pinfo->src;
377 key.dst = pinfo->dst;
380 fd_head = g_hash_table_lookup(fragment_table, &key);
383 return fd_head->datalen;
390 /* This function will set the partial reassembly flag for a fh.
391 When this function is called, the fh MUST already exist, i.e.
392 the fh MUST be created by the initial call to fragment_add() before
393 this function is called.
394 Also note that this function MUST be called to indicate a fh will be
395 extended (increase the already stored data)
399 fragment_set_partial_reassembly(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
401 fragment_data *fd_head;
404 /* create key to search hash with */
405 key.src = pinfo->src;
406 key.dst = pinfo->dst;
409 fd_head = g_hash_table_lookup(fragment_table, &key);
412 fd_head->flags |= FD_PARTIAL_REASSEMBLY;
417 * This function gets rid of an entry from a fragment table, given
418 * a pointer to the key for that entry; it also frees up the key
419 * and the addresses in it.
422 fragment_unhash(GHashTable *fragment_table, fragment_key *key)
425 * Free up the copies of the addresses from the old key.
427 g_free((gpointer)key->src.data);
428 g_free((gpointer)key->dst.data);
431 * Remove the entry from the fragment table.
433 g_hash_table_remove(fragment_table, key);
436 * Free the key itself.
438 g_mem_chunk_free(fragment_key_chunk, key);
442 * This function adds fragment_data structure to a reassembled-packet
443 * hash table, using the frame numbers of each of the frames from
444 * which it was reassembled as keys, and sets the "reassembled_in"
448 fragment_reassembled(fragment_data *fd_head, packet_info *pinfo,
449 GHashTable *reassembled_table, guint32 id)
451 reassembled_key *new_key;
454 if (fd_head->next == NULL) {
456 * This was not fragmented, so there's no fragment
457 * table; just hash it using the current frame number.
459 new_key = g_mem_chunk_alloc(reassembled_key_chunk);
460 new_key->frame = pinfo->fd->num;
462 g_hash_table_insert(reassembled_table, new_key, fd_head);
465 * Hash it with the frame numbers for all the frames.
467 for (fd = fd_head->next; fd != NULL; fd = fd->next){
468 new_key = g_mem_chunk_alloc(reassembled_key_chunk);
469 new_key->frame = fd->frame;
471 g_hash_table_insert(reassembled_table, new_key,
475 fd_head->reassembled_in = pinfo->fd->num;
479 * This function adds a new fragment to the fragment hash table.
480 * If this is the first fragment seen for this datagram, a new entry
481 * is created in the hash table, otherwise this fragment is just added
482 * to the linked list of fragments for this packet.
483 * The list of fragments for a specific datagram is kept sorted for
486 * Returns a pointer to the head of the fragment data list if we have all the
487 * fragments, NULL otherwise.
489 * This function assumes frag_offset being a byte offset into the defragment
493 * Once the fh is defragmented (= FD_DEFRAGMENTED set), it can be
494 * extended using the FD_PARTIAL_REASSEMBLY flag. This flag should be set
495 * using fragment_set_partial_reassembly() before calling fragment_add
496 * with the new fragment. FD_TOOLONGFRAGMENT and FD_MULTIPLETAILS flags
497 * are lowered when a new extension process is started.
500 fragment_add_work(fragment_data *fd_head, tvbuff_t *tvb, int offset,
501 packet_info *pinfo, guint32 frag_offset,
502 guint32 frag_data_len, gboolean more_frags)
507 unsigned char *old_data;
509 /* create new fd describing this fragment */
510 fd = g_mem_chunk_alloc(fragment_data_chunk);
513 fd->frame = pinfo->fd->num;
514 fd->offset = frag_offset;
515 fd->len = frag_data_len;
519 * If it was already defragmented and this new fragment goes beyond
520 * data limits, set flag in already empty fds & point old fds to malloc'ed data.
522 if(fd_head->flags & FD_DEFRAGMENTED && (frag_offset+frag_data_len) >= fd_head->datalen &&
523 fd_head->flags & FD_PARTIAL_REASSEMBLY){
524 for(fd_i=fd_head->next; fd_i; fd_i=fd_i->next){
526 fd_i->data = fd_head->data + fd_i->offset;
527 fd_i->flags |= FD_NOT_MALLOCED;
529 fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
531 fd_head->flags ^= FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY;
532 fd_head->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
538 * This is the tail fragment in the sequence.
540 if (fd_head->datalen) {
541 /* ok we have already seen other tails for this packet
542 * it might be a duplicate.
544 if (fd_head->datalen != (fd->offset + fd->len) ){
545 /* Oops, this tail indicates a different packet
546 * len than the previous ones. Somethings wrong
548 fd->flags |= FD_MULTIPLETAILS;
549 fd_head->flags |= FD_MULTIPLETAILS;
552 /* this was the first tail fragment, now we know the
553 * length of the packet
555 fd_head->datalen = fd->offset + fd->len;
562 /* If the packet is already defragmented, this MUST be an overlap.
563 * The entire defragmented packet is in fd_head->data
564 * Even if we have previously defragmented this packet, we still check
565 * check it. Someone might play overlap and TTL games.
567 if (fd_head->flags & FD_DEFRAGMENTED) {
568 fd->flags |= FD_OVERLAP;
569 fd_head->flags |= FD_OVERLAP;
570 /* make sure its not too long */
571 if (fd->offset + fd->len > fd_head->datalen) {
572 fd->flags |= FD_TOOLONGFRAGMENT;
573 fd_head->flags |= FD_TOOLONGFRAGMENT;
574 LINK_FRAG(fd_head,fd);
577 /* make sure it doesnt conflict with previous data */
578 if ( memcmp(fd_head->data+fd->offset,
579 tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
580 fd->flags |= FD_OVERLAPCONFLICT;
581 fd_head->flags |= FD_OVERLAPCONFLICT;
582 LINK_FRAG(fd_head,fd);
585 /* it was just an overlap, link it and return */
586 LINK_FRAG(fd_head,fd);
592 /* If we have reached this point, the packet is not defragmented yet.
593 * Save all payload in a buffer until we can defragment.
594 * XXX - what if we didn't capture the entire fragment due
595 * to a too-short snapshot length?
597 fd->data = g_malloc(fd->len);
598 tvb_memcpy(tvb, fd->data, offset, fd->len);
599 LINK_FRAG(fd_head,fd);
602 if( !(fd_head->datalen) ){
603 /* if we dont know the datalen, there are still missing
604 * packets. Cheaper than the check below.
610 /* check if we have received the entire fragment
611 * this is easy since the list is sorted and the head is faked.
614 for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
615 if ( ((fd_i->offset)<=max) &&
616 ((fd_i->offset+fd_i->len)>max) ){
617 max = fd_i->offset+fd_i->len;
621 if (max < (fd_head->datalen)) {
622 /* we have not received all packets yet */
627 if (max > (fd_head->datalen)) {
628 /*XXX not sure if current fd was the TOOLONG*/
629 /*XXX is it fair to flag current fd*/
630 /* oops, too long fragment detected */
631 fd->flags |= FD_TOOLONGFRAGMENT;
632 fd_head->flags |= FD_TOOLONGFRAGMENT;
636 /* we have received an entire packet, defragment it and
639 /* store old data just in case */
640 old_data=fd_head->data;
641 fd_head->data = g_malloc(max);
643 /* add all data fragments */
644 for (dfpos=0,fd_i=fd_head;fd_i;fd_i=fd_i->next) {
646 if (fd_i->offset < dfpos) {
647 fd_i->flags |= FD_OVERLAP;
648 fd_head->flags |= FD_OVERLAP;
649 if ( memcmp(fd_head->data+fd_i->offset,
651 MIN(fd_i->len,(dfpos-fd_i->offset))
653 fd_i->flags |= FD_OVERLAPCONFLICT;
654 fd_head->flags |= FD_OVERLAPCONFLICT;
657 /* dfpos is always >= than fd_i->offset */
658 /* No gaps can exist here, max_loop(above) does this */
659 if( fd_i->offset+fd_i->len > dfpos )
660 memcpy(fd_head->data+dfpos, fd_i->data+(dfpos-fd_i->offset),
661 fd_i->len-(dfpos-fd_i->offset));
662 if( fd_i->flags & FD_NOT_MALLOCED )
663 fd_i->flags ^= FD_NOT_MALLOCED;
668 dfpos=MAX(dfpos,(fd_i->offset+fd_i->len));
674 /* mark this packet as defragmented.
675 allows us to skip any trailing fragments */
676 fd_head->flags |= FD_DEFRAGMENTED;
677 fd_head->reassembled_in=pinfo->fd->num;
683 fragment_add_common(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
684 GHashTable *fragment_table, guint32 frag_offset,
685 guint32 frag_data_len, gboolean more_frags,
686 gboolean check_already_added)
688 fragment_key key, *new_key;
689 fragment_data *fd_head;
690 fragment_data *fd_item;
691 gboolean already_added=pinfo->fd->flags.visited;
693 /* create key to search hash with */
694 key.src = pinfo->src;
695 key.dst = pinfo->dst;
698 fd_head = g_hash_table_lookup(fragment_table, &key);
701 * "already_added" is true if "pinfo->fd->flags.visited" is true;
702 * if "pinfo->fd->flags.visited", this isn't the first pass, so
703 * we've already done all the reassembly and added all the
706 * If it's not true, but "check_already_added" is true, just check
707 * if we have seen this fragment before, i.e., if we have already
708 * added it to reassembly.
709 * That can be true even if "pinfo->fd->flags.visited" is false
710 * since we sometimes might call a subdissector multiple times.
711 * As an additional check, just make sure we have not already added
712 * this frame to the reassembly list, if there is a reassembly list;
713 * note that the first item in the reassembly list is not a
714 * fragment, it's a data structure for the reassembled packet.
715 * We don't check it because its "frame" member isn't initialized
716 * to anything, and because it doesn't count in any case.
718 if (!already_added && check_already_added && fd_head != NULL) {
719 for(fd_item=fd_head->next;fd_item;fd_item=fd_item->next){
720 if(pinfo->fd->num==fd_item->frame){
725 /* have we already added this frame ?*/
727 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
735 /* not found, this must be the first snooped fragment for this
736 * packet. Create list-head.
738 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
740 /* head/first structure in list only holds no other data than
741 * 'datalen' then we don't have to change the head of the list
742 * even if we want to keep it sorted
750 fd_head->reassembled_in=0;
753 * We're going to use the key to insert the fragment,
754 * so allocate a structure for it, and copy the
755 * addresses, allocating new buffers for the address
758 new_key = g_mem_chunk_alloc(fragment_key_chunk);
759 COPY_ADDRESS(&new_key->src, &key.src);
760 COPY_ADDRESS(&new_key->dst, &key.dst);
761 new_key->id = key.id;
762 g_hash_table_insert(fragment_table, new_key, fd_head);
765 if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
766 frag_data_len, more_frags)) {
768 * Reassembly is complete.
773 * Reassembly isn't complete.
780 fragment_add(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
781 GHashTable *fragment_table, guint32 frag_offset,
782 guint32 frag_data_len, gboolean more_frags)
784 return fragment_add_common(tvb, offset, pinfo, id, fragment_table,
785 frag_offset, frag_data_len, more_frags, TRUE);
789 * For use when you can have multiple fragments in the same frame added
790 * to the same reassembled PDU, e.g. with ONC RPC-over-TCP.
793 fragment_add_multiple_ok(tvbuff_t *tvb, int offset, packet_info *pinfo,
794 guint32 id, GHashTable *fragment_table,
795 guint32 frag_offset, guint32 frag_data_len,
798 return fragment_add_common(tvb, offset, pinfo, id, fragment_table,
799 frag_offset, frag_data_len, more_frags, FALSE);
803 fragment_add_check(tvbuff_t *tvb, int offset, packet_info *pinfo,
804 guint32 id, GHashTable *fragment_table,
805 GHashTable *reassembled_table, guint32 frag_offset,
806 guint32 frag_data_len, gboolean more_frags)
808 reassembled_key reass_key;
809 fragment_key key, *new_key, *old_key;
810 gpointer orig_key, value;
811 fragment_data *fd_head;
814 * If this isn't the first pass, look for this frame in the table
815 * of reassembled packets.
817 if (pinfo->fd->flags.visited) {
818 reass_key.frame = pinfo->fd->num;
820 return g_hash_table_lookup(reassembled_table, &reass_key);
823 /* create key to search hash with */
824 key.src = pinfo->src;
825 key.dst = pinfo->dst;
828 if (!g_hash_table_lookup_extended(fragment_table, &key,
829 &orig_key, &value)) {
830 /* not found, this must be the first snooped fragment for this
831 * packet. Create list-head.
833 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
835 /* head/first structure in list only holds no other data than
836 * 'datalen' then we don't have to change the head of the list
837 * even if we want to keep it sorted
845 fd_head->reassembled_in=0;
848 * We're going to use the key to insert the fragment,
849 * so allocate a structure for it, and copy the
850 * addresses, allocating new buffers for the address
853 new_key = g_mem_chunk_alloc(fragment_key_chunk);
854 COPY_ADDRESS(&new_key->src, &key.src);
855 COPY_ADDRESS(&new_key->dst, &key.dst);
856 new_key->id = key.id;
857 g_hash_table_insert(fragment_table, new_key, fd_head);
859 orig_key = new_key; /* for unhashing it later */
868 * If this is a short frame, then we can't, and don't, do
869 * reassembly on it. We just give up.
871 if (tvb_reported_length(tvb) > tvb_length(tvb))
874 if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
875 frag_data_len, more_frags)) {
877 * Reassembly is complete.
878 * Remove this from the table of in-progress
879 * reassemblies, add it to the table of
880 * reassembled packets, and return it.
884 * Remove this from the table of in-progress reassemblies,
885 * and free up any memory used for it in that table.
888 fragment_unhash(fragment_table, old_key);
891 * Add this item to the table of reassembled packets.
893 fragment_reassembled(fd_head, pinfo, reassembled_table, id);
897 * Reassembly isn't complete.
904 * This function adds a new fragment to the entry for a reassembly
907 * The list of fragments for a specific datagram is kept sorted for
910 * Returns TRUE if we have all the fragments, FALSE otherwise.
912 * This function assumes frag_number being a block sequence number.
913 * The bsn for the first block is 0.
916 fragment_add_seq_work(fragment_data *fd_head, tvbuff_t *tvb, int offset,
917 packet_info *pinfo, guint32 frag_number,
918 guint32 frag_data_len, gboolean more_frags)
922 fragment_data *last_fd;
923 guint32 max, dfpos, size;
925 /* create new fd describing this fragment */
926 fd = g_mem_chunk_alloc(fragment_data_chunk);
929 fd->frame = pinfo->fd->num;
930 fd->offset = frag_number;
931 fd->len = frag_data_len;
936 * This is the tail fragment in the sequence.
938 if (fd_head->datalen) {
939 /* ok we have already seen other tails for this packet
940 * it might be a duplicate.
942 if (fd_head->datalen != fd->offset ){
943 /* Oops, this tail indicates a different packet
944 * len than the previous ones. Somethings wrong
946 fd->flags |= FD_MULTIPLETAILS;
947 fd_head->flags |= FD_MULTIPLETAILS;
950 /* this was the first tail fragment, now we know the
951 * sequence number of that fragment (which is NOT
952 * the length of the packet!)
954 fd_head->datalen = fd->offset;
958 /* If the packet is already defragmented, this MUST be an overlap.
959 * The entire defragmented packet is in fd_head->data
960 * Even if we have previously defragmented this packet, we still check
961 * check it. Someone might play overlap and TTL games.
963 if (fd_head->flags & FD_DEFRAGMENTED) {
964 fd->flags |= FD_OVERLAP;
965 fd_head->flags |= FD_OVERLAP;
967 /* make sure it's not past the end */
968 if (fd->offset > fd_head->datalen) {
969 /* new fragment comes after the end */
970 fd->flags |= FD_TOOLONGFRAGMENT;
971 fd_head->flags |= FD_TOOLONGFRAGMENT;
972 LINK_FRAG(fd_head,fd);
975 /* make sure it doesnt conflict with previous data */
978 for (fd_i=fd_head->next;fd_i->offset!=fd->offset;fd_i=fd_i->next) {
979 if (!last_fd || last_fd->offset!=fd_i->offset){
985 /* new fragment overlaps existing fragment */
986 if(fd_i->len!=fd->len){
988 * They have different lengths; this
989 * is definitely a conflict.
991 fd->flags |= FD_OVERLAPCONFLICT;
992 fd_head->flags |= FD_OVERLAPCONFLICT;
993 LINK_FRAG(fd_head,fd);
996 g_assert(fd_head->len >= dfpos + fd->len);
997 if ( memcmp(fd_head->data+dfpos,
998 tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
1000 * They have the same length, but the
1001 * data isn't the same.
1003 fd->flags |= FD_OVERLAPCONFLICT;
1004 fd_head->flags |= FD_OVERLAPCONFLICT;
1005 LINK_FRAG(fd_head,fd);
1008 /* it was just an overlap, link it and return */
1009 LINK_FRAG(fd_head,fd);
1013 * New fragment doesn't overlap an existing
1014 * fragment - there was presumably a gap in
1015 * the sequence number space.
1017 * XXX - what should we do here? Is it always
1018 * the case that there are no gaps, or are there
1019 * protcols using sequence numbers where there
1022 * If the former, the check below for having
1023 * received all the fragments should check for
1024 * holes in the sequence number space and for the
1025 * first sequence number being 0. If we do that,
1026 * the only way we can get here is if this fragment
1027 * is past the end of the sequence number space -
1028 * but the check for "fd->offset > fd_head->datalen"
1029 * would have caught that above, so it can't happen.
1031 * If the latter, we don't have a good way of
1032 * knowing whether reassembly is complete if we
1033 * get packet out of order such that the "last"
1034 * fragment doesn't show up last - but, unless
1035 * in-order reliable delivery of fragments is
1036 * guaranteed, an implementation of the protocol
1037 * has no way of knowing whether reassembly is
1040 * For now, we just link the fragment in and
1043 LINK_FRAG(fd_head,fd);
1048 /* If we have reached this point, the packet is not defragmented yet.
1049 * Save all payload in a buffer until we can defragment.
1050 * XXX - what if we didn't capture the entire fragment due
1051 * to a too-short snapshot length?
1053 fd->data = g_malloc(fd->len);
1054 tvb_memcpy(tvb, fd->data, offset, fd->len);
1055 LINK_FRAG(fd_head,fd);
1058 if( !(fd_head->datalen) ){
1059 /* if we dont know the sequence number of the last fragment,
1060 * there are definitely still missing packets. Cheaper than
1067 /* check if we have received the entire fragment
1068 * this is easy since the list is sorted and the head is faked.
1071 for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1072 if ( fd_i->offset==max ){
1076 /* max will now be datalen+1 if all fragments have been seen */
1078 if (max <= fd_head->datalen) {
1079 /* we have not received all packets yet */
1084 if (max > (fd_head->datalen+1)) {
1085 /* oops, too long fragment detected */
1086 fd->flags |= FD_TOOLONGFRAGMENT;
1087 fd_head->flags |= FD_TOOLONGFRAGMENT;
1091 /* we have received an entire packet, defragment it and
1092 * free all fragments
1096 for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1097 if(!last_fd || last_fd->offset!=fd_i->offset){
1102 fd_head->data = g_malloc(size);
1103 fd_head->len = size; /* record size for caller */
1105 /* add all data fragments */
1107 for (dfpos=0,fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1109 if(!last_fd || last_fd->offset!=fd_i->offset){
1110 memcpy(fd_head->data+dfpos,fd_i->data,fd_i->len);
1113 /* duplicate/retransmission/overlap */
1114 fd_i->flags |= FD_OVERLAP;
1115 fd_head->flags |= FD_OVERLAP;
1116 if( (last_fd->len!=fd_i->datalen)
1117 || memcmp(last_fd->data, fd_i->data, last_fd->len) ){
1118 fd->flags |= FD_OVERLAPCONFLICT;
1119 fd_head->flags |= FD_OVERLAPCONFLICT;
1126 /* we have defragmented the pdu, now free all fragments*/
1127 for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1134 /* mark this packet as defragmented.
1135 allows us to skip any trailing fragments */
1136 fd_head->flags |= FD_DEFRAGMENTED;
1137 fd_head->reassembled_in=pinfo->fd->num;
1143 * This function adds a new fragment to the fragment hash table.
1144 * If this is the first fragment seen for this datagram, a new entry
1145 * is created in the hash table, otherwise this fragment is just added
1146 * to the linked list of fragments for this packet.
1148 * Returns a pointer to the head of the fragment data list if we have all the
1149 * fragments, NULL otherwise.
1151 * This function assumes frag_number being a block sequence number.
1152 * The bsn for the first block is 0.
1155 fragment_add_seq(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
1156 GHashTable *fragment_table, guint32 frag_number,
1157 guint32 frag_data_len, gboolean more_frags)
1159 fragment_key key, *new_key;
1160 fragment_data *fd_head;
1162 /* create key to search hash with */
1163 key.src = pinfo->src;
1164 key.dst = pinfo->dst;
1167 fd_head = g_hash_table_lookup(fragment_table, &key);
1169 /* have we already seen this frame ?*/
1170 if (pinfo->fd->flags.visited) {
1171 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
1179 /* not found, this must be the first snooped fragment for this
1180 * packet. Create list-head.
1182 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1184 /* head/first structure in list only holds no other data than
1185 * 'datalen' then we don't have to change the head of the list
1186 * even if we want to keep it sorted
1192 fd_head->flags=FD_BLOCKSEQUENCE;
1194 fd_head->reassembled_in=0;
1197 * We're going to use the key to insert the fragment,
1198 * so allocate a structure for it, and copy the
1199 * addresses, allocating new buffers for the address
1202 new_key = g_mem_chunk_alloc(fragment_key_chunk);
1203 COPY_ADDRESS(&new_key->src, &key.src);
1204 COPY_ADDRESS(&new_key->dst, &key.dst);
1205 new_key->id = key.id;
1206 g_hash_table_insert(fragment_table, new_key, fd_head);
1209 if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1210 frag_number, frag_data_len, more_frags)) {
1212 * Reassembly is complete.
1217 * Reassembly isn't complete.
1224 * This does the work for "fragment_add_seq_check()" and
1225 * "fragment_add_seq_next()".
1227 * This function assumes frag_number being a block sequence number.
1228 * The bsn for the first block is 0.
1230 * If "no_frag_number" is TRUE, it uses the next expected fragment number
1231 * as the fragment number if there is a reassembly in progress, otherwise
1234 * If "no_frag_number" is FALSE, it uses the "frag_number" argument as
1235 * the fragment number.
1237 * If this is the first fragment seen for this datagram, a new
1238 * "fragment_data" structure is allocated to refer to the reassembled,
1241 * if "more_frags" is false, the structure is not added to
1242 * the hash table, and not given any fragments to refer to,
1243 * but is just returned;
1245 * if "more_frags" is true, this fragment is added to the linked
1246 * list of fragments for this packet, and the "fragment_data"
1247 * structure is put into the hash table.
1249 * Otherwise, this fragment is just added to the linked list of fragments
1252 * Returns a pointer to the head of the fragment data list, and removes
1253 * that from the fragment hash table if necessary and adds it to the
1254 * table of reassembled fragments, if we have all the fragments or if
1255 * this is the only fragment and "more_frags" is false, returns NULL
1259 fragment_add_seq_check_work(tvbuff_t *tvb, int offset, packet_info *pinfo,
1260 guint32 id, GHashTable *fragment_table,
1261 GHashTable *reassembled_table, guint32 frag_number,
1262 guint32 frag_data_len, gboolean more_frags,
1263 gboolean no_frag_number)
1265 reassembled_key reass_key;
1266 fragment_key key, *new_key, *old_key;
1267 gpointer orig_key, value;
1268 fragment_data *fd_head, *fd;
1271 * Have we already seen this frame?
1272 * If so, look for it in the table of reassembled packets.
1274 if (pinfo->fd->flags.visited) {
1275 reass_key.frame = pinfo->fd->num;
1277 return g_hash_table_lookup(reassembled_table, &reass_key);
1280 /* create key to search hash with */
1281 key.src = pinfo->src;
1282 key.dst = pinfo->dst;
1285 if (!g_hash_table_lookup_extended(fragment_table, &key,
1286 &orig_key, &value)) {
1287 /* not found, this must be the first snooped fragment for this
1288 * packet. Create list-head.
1290 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1292 /* head/first structure in list only holds no other data than
1293 * 'datalen' then we don't have to change the head of the list
1294 * even if we want to keep it sorted
1300 fd_head->flags=FD_BLOCKSEQUENCE;
1302 fd_head->reassembled_in=0;
1306 * This is the last snooped fragment for this
1307 * packet as well; that means it's the only
1308 * fragment. Just add it to the table of
1309 * reassembled packets, and return it.
1311 fragment_reassembled(fd_head, pinfo,
1312 reassembled_table, id);
1317 * We're going to use the key to insert the fragment,
1318 * so allocate a structure for it, and copy the
1319 * addresses, allocating new buffers for the address
1322 new_key = g_mem_chunk_alloc(fragment_key_chunk);
1323 COPY_ADDRESS(&new_key->src, &key.src);
1324 COPY_ADDRESS(&new_key->dst, &key.dst);
1325 new_key->id = key.id;
1326 g_hash_table_insert(fragment_table, new_key, fd_head);
1328 orig_key = new_key; /* for unhashing it later */
1331 * If we weren't given an initial fragment number,
1343 * If we weren't given an initial fragment number,
1344 * use the next expected fragment number as the fragment
1345 * number for this fragment.
1347 if (no_frag_number) {
1348 for (fd = fd_head; fd != NULL; fd = fd->next) {
1349 if (fd->next == NULL)
1350 frag_number = fd->offset + 1;
1356 * If we don't have all the data that is in this fragment,
1357 * then we can't, and don't, do reassembly on it.
1359 * If it's the first frame, handle it as an unfragmented packet.
1360 * Otherwise, just handle it as a fragment.
1362 * If "more_frags" isn't set, we get rid of the entry in the
1363 * hash table for this reassembly, as we don't need it any more.
1365 if (!tvb_bytes_exist(tvb, offset, frag_data_len)) {
1368 * Remove this from the table of in-progress
1369 * reassemblies, and free up any memory used for
1373 fragment_unhash(fragment_table, old_key);
1375 return frag_number == 0 ? fd_head : NULL;
1378 if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1379 frag_number, frag_data_len, more_frags)) {
1381 * Reassembly is complete.
1382 * Remove this from the table of in-progress
1383 * reassemblies, add it to the table of
1384 * reassembled packets, and return it.
1388 * Remove this from the table of in-progress reassemblies,
1389 * and free up any memory used for it in that table.
1392 fragment_unhash(fragment_table, old_key);
1395 * Add this item to the table of reassembled packets.
1397 fragment_reassembled(fd_head, pinfo, reassembled_table, id);
1401 * Reassembly isn't complete.
1408 fragment_add_seq_check(tvbuff_t *tvb, int offset, packet_info *pinfo,
1409 guint32 id, GHashTable *fragment_table,
1410 GHashTable *reassembled_table, guint32 frag_number,
1411 guint32 frag_data_len, gboolean more_frags)
1413 return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1414 fragment_table, reassembled_table, frag_number, frag_data_len,
1419 fragment_add_seq_next(tvbuff_t *tvb, int offset, packet_info *pinfo,
1420 guint32 id, GHashTable *fragment_table,
1421 GHashTable *reassembled_table, guint32 frag_data_len,
1422 gboolean more_frags)
1424 return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1425 fragment_table, reassembled_table, 0, frag_data_len,
1430 * Process reassembled data; if we're on the frame in which the data
1431 * was reassembled, put the fragment information into the protocol
1432 * tree, and construct a tvbuff with the reassembled data, otherwise
1433 * just put a "reassembled in" item into the protocol tree.
1436 process_reassembled_data(tvbuff_t *tvb, packet_info *pinfo, char *name,
1437 fragment_data *fd_head, const fragment_items *fit,
1438 gboolean *update_col_infop, proto_tree *tree)
1441 gboolean update_col_info;
1443 if (pinfo->fd->num == fd_head->reassembled_in) {
1445 * OK, we have the complete reassembled payload.
1446 * Allocate a new tvbuff, referring to the reassembled
1449 if (fd_head->flags & FD_BLOCKSEQUENCE) {
1450 next_tvb = tvb_new_real_data(fd_head->data,
1451 fd_head->len, fd_head->len);
1453 next_tvb = tvb_new_real_data(fd_head->data,
1454 fd_head->datalen, fd_head->datalen);
1458 * Add the tvbuff to the list of tvbuffs to which
1459 * the tvbuff we were handed refers, so it'll get
1460 * cleaned up when that tvbuff is cleaned up.
1462 tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1464 /* Add the defragmented data to the data source list. */
1465 add_new_data_source(pinfo, next_tvb, name);
1467 /* show all fragments */
1468 if (fd_head->flags & FD_BLOCKSEQUENCE) {
1469 update_col_info = !show_fragment_seq_tree(fd_head,
1470 fit, tree, pinfo, next_tvb);
1472 update_col_info = !show_fragment_tree(fd_head,
1473 fit, tree, pinfo, next_tvb);
1475 if (update_col_infop != NULL)
1476 *update_col_infop = update_col_info;
1478 /* We don't have the complete reassembled payload. */
1482 * If there's a field to use for the number of
1483 * the frame in which the packet was reassembled,
1484 * add it to the protocol tree.
1486 if (fit->hf_reassembled_in != NULL) {
1487 proto_tree_add_uint(tree,
1488 *(fit->hf_reassembled_in), tvb,
1489 0, 0, fd_head->reassembled_in);
1496 * Show a single fragment in a fragment subtree.
1499 show_fragment(fragment_data *fd, int offset, const fragment_items *fit,
1500 proto_tree *ft, tvbuff_t *tvb)
1502 if (fd->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT
1503 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1504 /* this fragment has some flags set, create a subtree
1505 * for it and display the flags.
1507 proto_tree *fet=NULL;
1508 proto_item *fei=NULL;
1511 if (fd->flags & (FD_OVERLAPCONFLICT
1512 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1513 hf = *(fit->hf_fragment_error);
1515 hf = *(fit->hf_fragment);
1517 fei = proto_tree_add_uint_format(ft, hf,
1518 tvb, offset, fd->len,
1520 "Frame:%u payload:%u-%u",
1524 fet = proto_item_add_subtree(fei, *(fit->ett_fragment));
1525 if (fd->flags&FD_OVERLAP) {
1526 proto_tree_add_boolean(fet,
1527 *(fit->hf_fragment_overlap),
1531 if (fd->flags&FD_OVERLAPCONFLICT) {
1532 proto_tree_add_boolean(fet,
1533 *(fit->hf_fragment_overlap_conflict),
1537 if (fd->flags&FD_MULTIPLETAILS) {
1538 proto_tree_add_boolean(fet,
1539 *(fit->hf_fragment_multiple_tails),
1543 if (fd->flags&FD_TOOLONGFRAGMENT) {
1544 proto_tree_add_boolean(fet,
1545 *(fit->hf_fragment_too_long_fragment),
1550 /* nothing of interest for this fragment */
1551 proto_tree_add_uint_format(ft, *(fit->hf_fragment),
1552 tvb, offset, fd->len,
1554 "Frame:%u payload:%u-%u",
1563 show_fragment_errs_in_col(fragment_data *fd_head, const fragment_items *fit,
1566 if (fd_head->flags & (FD_OVERLAPCONFLICT
1567 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1568 if (check_col(pinfo->cinfo, COL_INFO)) {
1569 col_add_fstr(pinfo->cinfo, COL_INFO,
1570 "[Illegal %s]", fit->tag);
1578 /* This function will build the fragment subtree; it's for fragments
1579 reassembled with "fragment_add()".
1581 It will return TRUE if there were fragmentation errors
1582 or FALSE if fragmentation was ok.
1585 show_fragment_tree(fragment_data *fd_head, const fragment_items *fit,
1586 proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb)
1592 /* It's not fragmented. */
1593 pinfo->fragmented = FALSE;
1595 fi = proto_tree_add_item(tree, *(fit->hf_fragments),
1597 ft = proto_item_add_subtree(fi, *(fit->ett_fragments));
1598 for (fd = fd_head->next; fd != NULL; fd = fd->next)
1599 show_fragment(fd, fd->offset, fit, ft, tvb);
1601 return show_fragment_errs_in_col(fd_head, fit, pinfo);
1604 /* This function will build the fragment subtree; it's for fragments
1605 reassembled with "fragment_add_seq()" or "fragment_add_seq_check()".
1607 It will return TRUE if there were fragmentation errors
1608 or FALSE if fragmentation was ok.
1611 show_fragment_seq_tree(fragment_data *fd_head, const fragment_items *fit,
1612 proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb)
1614 guint32 offset, next_offset;
1615 fragment_data *fd, *last_fd;
1619 /* It's not fragmented. */
1620 pinfo->fragmented = FALSE;
1622 fi = proto_tree_add_item(tree, *(fit->hf_fragments),
1624 ft = proto_item_add_subtree(fi, *(fit->ett_fragments));
1628 for (fd = fd_head->next; fd != NULL; fd = fd->next){
1629 if (last_fd == NULL || last_fd->offset != fd->offset) {
1630 offset = next_offset;
1631 next_offset += fd->len;
1634 show_fragment(fd, offset, fit, ft, tvb);
1637 return show_fragment_errs_in_col(fd_head, fit, pinfo);