Fix some places where "Ethereal" wasn't capitalized and boldfaced.
[obnox/wireshark/wip.git] / reassemble.c
1 /* reassemble.c
2  * Routines for {fragment,segment} reassembly
3  *
4  * $Id: reassemble.c,v 1.39 2003/06/04 05:41:37 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <string.h>
30
31 #include <epan/packet.h>
32
33 #include "reassemble.h"
34
35
36 typedef struct _fragment_key {
37         address src;
38         address dst;
39         guint32 id;
40 } fragment_key;
41
42 static GMemChunk *fragment_key_chunk = NULL;
43 static GMemChunk *fragment_data_chunk = NULL;
44 static int fragment_init_count = 200;
45
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) )     \
51                                 break;                                  \
52                 }                                                       \
53                 (fd)->next=fd_i->next;                          \
54                 fd_i->next=(fd);                                        \
55         }
56
57 static gint
58 fragment_equal(gconstpointer k1, gconstpointer k2)
59 {
60         const fragment_key* key1 = (const fragment_key*) k1;
61         const fragment_key* key2 = (const fragment_key*) k2;
62
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.
66         */
67         return ( ( (key1->id    == key2->id) &&
68                    (ADDRESSES_EQUAL(&key1->src, &key2->src)) &&
69                    (ADDRESSES_EQUAL(&key1->dst, &key2->dst))
70                  ) ?
71                  TRUE : FALSE);
72 }
73
74 static guint
75 fragment_hash(gconstpointer k)
76 {
77         const fragment_key* key = (const fragment_key*) k;
78         guint hash_val;
79 /*
80         int i;
81 */
82
83         hash_val = 0;
84
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.
88
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];
93 */
94
95         hash_val += key->id;
96
97         return hash_val;
98 }
99
100 typedef struct _reassembled_key {
101         guint32 id;
102         guint32 frame;
103 } reassembled_key;
104
105 static GMemChunk *reassembled_key_chunk = NULL;
106
107 static gint
108 reassembled_equal(gconstpointer k1, gconstpointer k2)
109 {
110         const reassembled_key* key1 = (const reassembled_key*) k1;
111         const reassembled_key* key2 = (const reassembled_key*) k2;
112
113         /*
114          * We assume that the frame numbers are unlikely to be equal,
115          * so we check them first.
116          */
117         return key1->frame == key2->frame && key1->id == key2->id;
118 }
119
120 static guint
121 reassembled_hash(gconstpointer k)
122 {
123         const reassembled_key* key = (const reassembled_key*) k;
124
125         return key->frame;
126 }
127
128 /*
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()".)
132  */
133 static gboolean
134 free_all_fragments(gpointer key_arg, gpointer value, gpointer user_data _U_)
135 {
136         fragment_key *key = key_arg;
137         fragment_data *fd_head;
138
139         /*
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).
148          *
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.
153          */
154         g_free((gpointer)key->src.data);
155         g_free((gpointer)key->dst.data);
156
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);
160         }
161
162         return TRUE;
163 }
164
165 /*
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()".)
169  */
170 static gboolean
171 free_all_reassembled_fragments(gpointer key_arg _U_, gpointer value,
172                                gpointer user_data _U_)
173 {
174         fragment_data *fd_head;
175
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);
179
180                         /*
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.
186                          */
187                         fd_head->data = NULL;
188                 }
189         }
190
191         return TRUE;
192 }
193
194 /*
195  * Initialize a fragment table.
196  */
197 void
198 fragment_table_init(GHashTable **fragment_table)
199 {
200         if (*fragment_table != NULL) {
201                 /*
202                  * The fragment hash table exists.
203                  *
204                  * Remove all entries and free fragment data for
205                  * each entry.  (The key and value data is freed
206                  * by "reassemble_init()".)
207                  */
208                 g_hash_table_foreach_remove(*fragment_table,
209                                 free_all_fragments, NULL);
210         } else {
211                 /* The fragment table does not exist. Create it */
212                 *fragment_table = g_hash_table_new(fragment_hash,
213                                 fragment_equal);
214         }
215 }
216
217 /*
218  * Initialize a reassembled-packet table.
219  */
220 void
221 reassembled_table_init(GHashTable **reassembled_table)
222 {
223         if (*reassembled_table != NULL) {
224                 /*
225                  * The reassembled-packet hash table exists.
226                  *
227                  * Remove all entries and free reassembled packet
228                  * data for each entry.  (The key data is freed
229                  * by "reassemble_init()".)
230                  */
231                 g_hash_table_foreach_remove(*reassembled_table,
232                                 free_all_reassembled_fragments, NULL);
233         } else {
234                 /* The fragment table does not exist. Create it */
235                 *reassembled_table = g_hash_table_new(reassembled_hash,
236                                 reassembled_equal);
237         }
238 }
239
240 /*
241  * Free up all space allocated for fragment keys and data and
242  * reassembled keys.
243  */
244 void
245 reassemble_init(void)
246 {
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),
256             G_ALLOC_AND_FREE);
257         fragment_data_chunk = g_mem_chunk_new("fragment_data_chunk",
258             sizeof(fragment_data),
259             fragment_init_count * sizeof(fragment_data),
260             G_ALLOC_ONLY);
261         reassembled_key_chunk = g_mem_chunk_new("reassembled_key_chunk",
262             sizeof(reassembled_key),
263             fragment_init_count * sizeof(reassembled_key),
264             G_ALLOC_AND_FREE);
265 }
266
267 /* This function cleans up the stored state and removes the reassembly data and
268  * (with one exception) all allocated memory for matching reassembly.
269  *
270  * The exception is :
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
273  * returned.
274  * Othervise the function will return NULL.
275  *
276  * So, if you call fragment_delete and it returns non-NULL, YOU are responsible to
277  * g_free() that buffer.
278  */
279 unsigned char *
280 fragment_delete(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
281 {
282         fragment_data *fd_head, *fd;
283         fragment_key key;
284         unsigned char *data=NULL;
285
286         /* create key to search hash with */
287         key.src = pinfo->src;
288         key.dst = pinfo->dst;
289         key.id  = id;
290
291         fd_head = g_hash_table_lookup(fragment_table, &key);
292
293         if(fd_head==NULL){
294                 /* We do not recognize this as a PDU we have seen before. return*/
295                 return NULL;
296         }
297
298         data=fd_head->data;
299         /* loop over all partial fragments and free any buffers */
300         for(fd=fd_head->next;fd;){
301                 fragment_data *tmp_fd;
302                 tmp_fd=fd->next;
303
304                 if( !(fd->flags&FD_NOT_MALLOCED) )
305                         g_free(fd->data);
306                 g_mem_chunk_free(fragment_data_chunk, fd);
307                 fd=tmp_fd;
308         }
309         g_mem_chunk_free(fragment_data_chunk, fd_head);
310         g_hash_table_remove(fragment_table, &key);
311
312         return data;
313 }
314
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?
317  */
318 fragment_data *
319 fragment_get(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
320 {
321         fragment_data *fd_head;
322         fragment_key key;
323
324         /* create key to search hash with */
325         key.src = pinfo->src;
326         key.dst = pinfo->dst;
327         key.id  = id;
328
329         fd_head = g_hash_table_lookup(fragment_table, &key);
330
331         return fd_head;
332 }
333
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.
339  *
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.
343  *
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.
347  */
348 void
349 fragment_set_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table,
350                      guint32 tot_len)
351 {
352         fragment_data *fd_head;
353         fragment_key key;
354
355         /* create key to search hash with */
356         key.src = pinfo->src;
357         key.dst = pinfo->dst;
358         key.id  = id;
359
360         fd_head = g_hash_table_lookup(fragment_table, &key);
361
362         if(fd_head){
363                 fd_head->datalen = tot_len;
364         }
365
366         return;
367 }
368
369 guint32
370 fragment_get_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
371 {
372         fragment_data *fd_head;
373         fragment_key key;
374
375         /* create key to search hash with */
376         key.src = pinfo->src;
377         key.dst = pinfo->dst;
378         key.id  = id;
379
380         fd_head = g_hash_table_lookup(fragment_table, &key);
381
382         if(fd_head){
383                 return fd_head->datalen;
384         }
385
386         return 0;
387 }
388
389
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)
396 */
397
398 void
399 fragment_set_partial_reassembly(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
400 {
401         fragment_data *fd_head;
402         fragment_key key;
403
404         /* create key to search hash with */
405         key.src = pinfo->src;
406         key.dst = pinfo->dst;
407         key.id  = id;
408
409         fd_head = g_hash_table_lookup(fragment_table, &key);
410
411         if(fd_head){
412                 fd_head->flags |= FD_PARTIAL_REASSEMBLY;
413         }
414 }
415
416 /*
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.
420  */
421 static void
422 fragment_unhash(GHashTable *fragment_table, fragment_key *key)
423 {
424         /*
425          * Free up the copies of the addresses from the old key.
426          */
427         g_free((gpointer)key->src.data);
428         g_free((gpointer)key->dst.data);
429
430         /*
431          * Remove the entry from the fragment table.
432          */
433         g_hash_table_remove(fragment_table, key);
434
435         /*
436          * Free the key itself.
437          */
438         g_mem_chunk_free(fragment_key_chunk, key);
439 }
440
441 /*
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"
445  * frame number.
446  */
447 static void
448 fragment_reassembled(fragment_data *fd_head, packet_info *pinfo,
449              GHashTable *reassembled_table, guint32 id)
450 {
451         reassembled_key *new_key;
452         fragment_data *fd;
453
454         if (fd_head->next == NULL) {
455                 /*
456                  * This was not fragmented, so there's no fragment
457                  * table; just hash it using the current frame number.
458                  */
459                 new_key = g_mem_chunk_alloc(reassembled_key_chunk);
460                 new_key->frame = pinfo->fd->num;
461                 new_key->id = id;
462                 g_hash_table_insert(reassembled_table, new_key, fd_head);
463         } else {
464                 /*
465                  * Hash it with the frame numbers for all the frames.
466                  */
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;
470                         new_key->id = id;
471                         g_hash_table_insert(reassembled_table, new_key,
472                             fd_head);
473                 }
474         }
475         fd_head->reassembled_in = pinfo->fd->num;
476 }
477
478 /*
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
484  * easier handling.
485  *
486  * Returns a pointer to the head of the fragment data list if we have all the
487  * fragments, NULL otherwise.
488  *
489  * This function assumes frag_offset being a byte offset into the defragment
490  * packet.
491  *
492  * 01-2002
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.
498  */
499 static gboolean
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)
503 {
504         fragment_data *fd;
505         fragment_data *fd_i;
506         guint32 max, dfpos;
507         unsigned char *old_data;
508
509         /* create new fd describing this fragment */
510         fd = g_mem_chunk_alloc(fragment_data_chunk);
511         fd->next = NULL;
512         fd->flags = 0;
513         fd->frame = pinfo->fd->num;
514         fd->offset = frag_offset;
515         fd->len  = frag_data_len;
516         fd->data = NULL;
517
518         /*
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.
521          */
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){
525                         if( !fd_i->data ) {
526                                 fd_i->data = fd_head->data + fd_i->offset;
527                                 fd_i->flags |= FD_NOT_MALLOCED;
528                         }
529                         fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
530                 }
531                 fd_head->flags ^= FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY;
532                 fd_head->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
533                 fd_head->datalen=0;
534         }
535
536         if (!more_frags) {
537                 /*
538                  * This is the tail fragment in the sequence.
539                  */
540                 if (fd_head->datalen) {
541                         /* ok we have already seen other tails for this packet
542                          * it might be a duplicate.
543                          */
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
547                                  */
548                                 fd->flags      |= FD_MULTIPLETAILS;
549                                 fd_head->flags |= FD_MULTIPLETAILS;
550                         }
551                 } else {
552                         /* this was the first tail fragment, now we know the
553                          * length of the packet
554                          */
555                         fd_head->datalen = fd->offset + fd->len;
556                 }
557         }
558
559
560
561
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.
566          */
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);
575                         return TRUE;
576                 }
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);
583                         return TRUE;
584                 }
585                 /* it was just an overlap, link it and return */
586                 LINK_FRAG(fd_head,fd);
587                 return TRUE;
588         }
589
590
591
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?
596          */
597         fd->data = g_malloc(fd->len);
598         tvb_memcpy(tvb, fd->data, offset, fd->len);
599         LINK_FRAG(fd_head,fd);
600
601
602         if( !(fd_head->datalen) ){
603                 /* if we dont know the datalen, there are still missing
604                  * packets. Cheaper than the check below.
605                  */
606                 return FALSE;
607         }
608
609
610         /* check if we have received the entire fragment
611          * this is easy since the list is sorted and the head is faked.
612          */
613         max = 0;
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;
618                 }
619         }
620
621         if (max < (fd_head->datalen)) {
622                 /* we have not received all packets yet */
623                 return FALSE;
624         }
625
626
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;
633         }
634
635
636         /* we have received an entire packet, defragment it and
637          * free all fragments
638          */
639         /* store old data just in case */
640         old_data=fd_head->data;
641         fd_head->data = g_malloc(max);
642
643         /* add all data fragments */
644         for (dfpos=0,fd_i=fd_head;fd_i;fd_i=fd_i->next) {
645                 if (fd_i->len) {
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,
650                                         fd_i->data,
651                                         MIN(fd_i->len,(dfpos-fd_i->offset))
652                                         ) ){
653                                         fd_i->flags    |= FD_OVERLAPCONFLICT;
654                                         fd_head->flags |= FD_OVERLAPCONFLICT;
655                                 }
656                         }
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;
664                         else
665                                 g_free(fd_i->data);
666                         fd_i->data=NULL;
667
668                         dfpos=MAX(dfpos,(fd_i->offset+fd_i->len));
669                 }
670         }
671
672         if( old_data )
673                 g_free(old_data);
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;
678
679         return TRUE;
680 }
681
682 fragment_data *
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)
687 {
688         fragment_key key, *new_key;
689         fragment_data *fd_head;
690         fragment_data *fd_item;
691         gboolean already_added=pinfo->fd->flags.visited;
692
693         /* create key to search hash with */
694         key.src = pinfo->src;
695         key.dst = pinfo->dst;
696         key.id  = id;
697
698         fd_head = g_hash_table_lookup(fragment_table, &key);
699
700         /*
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
704          * fragments.
705          *
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.
717          */
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){
721                                 already_added=TRUE;
722                         }
723                 }
724         }
725         /* have we already added this frame ?*/
726         if (already_added) {
727                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
728                         return fd_head;
729                 } else {
730                         return NULL;
731                 }
732         }
733
734         if (fd_head==NULL){
735                 /* not found, this must be the first snooped fragment for this
736                  * packet. Create list-head.
737                  */
738                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
739
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
743                  */
744                 fd_head->next=NULL;
745                 fd_head->datalen=0;
746                 fd_head->offset=0;
747                 fd_head->len=0;
748                 fd_head->flags=0;
749                 fd_head->data=NULL;
750                 fd_head->reassembled_in=0;
751
752                 /*
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
756                  * data.
757                  */
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);
763         }
764
765         if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
766             frag_data_len, more_frags)) {
767                 /*
768                  * Reassembly is complete.
769                  */
770                 return fd_head;
771         } else {
772                 /*
773                  * Reassembly isn't complete.
774                  */
775                 return NULL;
776         }
777 }
778
779 fragment_data *
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)
783 {
784         return fragment_add_common(tvb, offset, pinfo, id, fragment_table,
785             frag_offset, frag_data_len, more_frags, TRUE);
786 }
787
788 /*
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.
791  */
792 fragment_data *
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,
796                          gboolean more_frags)
797 {
798         return fragment_add_common(tvb, offset, pinfo, id, fragment_table,
799             frag_offset, frag_data_len, more_frags, FALSE);
800 }
801
802 fragment_data *
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)
807 {
808         reassembled_key reass_key;
809         fragment_key key, *new_key, *old_key;
810         gpointer orig_key, value;
811         fragment_data *fd_head;
812
813         /*
814          * If this isn't the first pass, look for this frame in the table
815          * of reassembled packets.
816          */
817         if (pinfo->fd->flags.visited) {
818                 reass_key.frame = pinfo->fd->num;
819                 reass_key.id = id;
820                 return g_hash_table_lookup(reassembled_table, &reass_key);
821         }
822
823         /* create key to search hash with */
824         key.src = pinfo->src;
825         key.dst = pinfo->dst;
826         key.id  = id;
827
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.
832                  */
833                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
834
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
838                  */
839                 fd_head->next=NULL;
840                 fd_head->datalen=0;
841                 fd_head->offset=0;
842                 fd_head->len=0;
843                 fd_head->flags=0;
844                 fd_head->data=NULL;
845                 fd_head->reassembled_in=0;
846
847                 /*
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
851                  * data.
852                  */
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);
858
859                 orig_key = new_key;     /* for unhashing it later */
860         } else {
861                 /*
862                  * We found it.
863                  */
864                 fd_head = value;
865         }
866
867         /*
868          * If this is a short frame, then we can't, and don't, do
869          * reassembly on it.  We just give up.
870          */
871         if (tvb_reported_length(tvb) > tvb_length(tvb))
872                 return NULL;
873
874         if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
875             frag_data_len, more_frags)) {
876                 /*
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.
881                  */
882
883                 /*
884                  * Remove this from the table of in-progress reassemblies,
885                  * and free up any memory used for it in that table.
886                  */
887                 old_key = orig_key;
888                 fragment_unhash(fragment_table, old_key);
889
890                 /*
891                  * Add this item to the table of reassembled packets.
892                  */
893                 fragment_reassembled(fd_head, pinfo, reassembled_table, id);
894                 return fd_head;
895         } else {
896                 /*
897                  * Reassembly isn't complete.
898                  */
899                 return NULL;
900         }
901 }
902
903 /*
904  * This function adds a new fragment to the entry for a reassembly
905  * operation.
906  *
907  * The list of fragments for a specific datagram is kept sorted for
908  * easier handling.
909  *
910  * Returns TRUE if we have all the fragments, FALSE otherwise.
911  *
912  * This function assumes frag_number being a block sequence number.
913  * The bsn for the first block is 0.
914  */
915 static gboolean
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)
919 {
920         fragment_data *fd;
921         fragment_data *fd_i;
922         fragment_data *last_fd;
923         guint32 max, dfpos, size;
924
925         /* create new fd describing this fragment */
926         fd = g_mem_chunk_alloc(fragment_data_chunk);
927         fd->next = NULL;
928         fd->flags = 0;
929         fd->frame = pinfo->fd->num;
930         fd->offset = frag_number;
931         fd->len  = frag_data_len;
932         fd->data = NULL;
933
934         if (!more_frags) {
935                 /*
936                  * This is the tail fragment in the sequence.
937                  */
938                 if (fd_head->datalen) {
939                         /* ok we have already seen other tails for this packet
940                          * it might be a duplicate.
941                          */
942                         if (fd_head->datalen != fd->offset ){
943                                 /* Oops, this tail indicates a different packet
944                                  * len than the previous ones. Somethings wrong
945                                  */
946                                 fd->flags      |= FD_MULTIPLETAILS;
947                                 fd_head->flags |= FD_MULTIPLETAILS;
948                         }
949                 } else {
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!)
953                          */
954                         fd_head->datalen = fd->offset;
955                 }
956         }
957
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.
962          */
963         if (fd_head->flags & FD_DEFRAGMENTED) {
964                 fd->flags      |= FD_OVERLAP;
965                 fd_head->flags |= FD_OVERLAP;
966
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);
973                         return TRUE;
974                 }
975                 /* make sure it doesnt conflict with previous data */
976                 dfpos=0;
977                 last_fd=NULL;
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){
980                     dfpos += fd_i->len;
981                   }
982                   last_fd=fd_i;
983                 }
984                 if(fd_i){
985                         /* new fragment overlaps existing fragment */
986                         if(fd_i->len!=fd->len){
987                                 /*
988                                  * They have different lengths; this
989                                  * is definitely a conflict.
990                                  */
991                                 fd->flags      |= FD_OVERLAPCONFLICT;
992                                 fd_head->flags |= FD_OVERLAPCONFLICT;
993                                 LINK_FRAG(fd_head,fd);
994                                 return TRUE;
995                         }
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) ){
999                                 /*
1000                                  * They have the same length, but the
1001                                  * data isn't the same.
1002                                  */
1003                                 fd->flags      |= FD_OVERLAPCONFLICT;
1004                                 fd_head->flags |= FD_OVERLAPCONFLICT;
1005                                 LINK_FRAG(fd_head,fd);
1006                                 return TRUE;
1007                         }
1008                         /* it was just an overlap, link it and return */
1009                         LINK_FRAG(fd_head,fd);
1010                         return TRUE;
1011                 } else {
1012                         /*
1013                          * New fragment doesn't overlap an existing
1014                          * fragment - there was presumably a gap in
1015                          * the sequence number space.
1016                          *
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
1020                          * can be gaps?
1021                          *
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.
1030                          *
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
1038                          * complete, either.
1039                          *
1040                          * For now, we just link the fragment in and
1041                          * return.
1042                          */
1043                         LINK_FRAG(fd_head,fd);
1044                         return TRUE;
1045                 }
1046         }
1047
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?
1052          */
1053         fd->data = g_malloc(fd->len);
1054         tvb_memcpy(tvb, fd->data, offset, fd->len);
1055         LINK_FRAG(fd_head,fd);
1056
1057
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
1061                  * the check below.
1062                  */
1063                 return FALSE;
1064         }
1065
1066
1067         /* check if we have received the entire fragment
1068          * this is easy since the list is sorted and the head is faked.
1069          */
1070         max = 0;
1071         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1072           if ( fd_i->offset==max ){
1073             max++;
1074           }
1075         }
1076         /* max will now be datalen+1 if all fragments have been seen */
1077
1078         if (max <= fd_head->datalen) {
1079                 /* we have not received all packets yet */
1080                 return FALSE;
1081         }
1082
1083
1084         if (max > (fd_head->datalen+1)) {
1085                 /* oops, too long fragment detected */
1086                 fd->flags      |= FD_TOOLONGFRAGMENT;
1087                 fd_head->flags |= FD_TOOLONGFRAGMENT;
1088         }
1089
1090
1091         /* we have received an entire packet, defragment it and
1092          * free all fragments
1093          */
1094         size=0;
1095         last_fd=NULL;
1096         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1097           if(!last_fd || last_fd->offset!=fd_i->offset){
1098             size+=fd_i->len;
1099           }
1100           last_fd=fd_i;
1101         }
1102         fd_head->data = g_malloc(size);
1103         fd_head->len = size;            /* record size for caller       */
1104
1105         /* add all data fragments */
1106         last_fd=NULL;
1107         for (dfpos=0,fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1108           if (fd_i->len) {
1109             if(!last_fd || last_fd->offset!=fd_i->offset){
1110               memcpy(fd_head->data+dfpos,fd_i->data,fd_i->len);
1111               dfpos += fd_i->len;
1112             } else {
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;
1120               }
1121             }
1122             last_fd=fd_i;
1123           }
1124         }
1125
1126         /* we have defragmented the pdu, now free all fragments*/
1127         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1128           if(fd_i->data){
1129             g_free(fd_i->data);
1130             fd_i->data=NULL;
1131           }
1132         }
1133
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;
1138
1139         return TRUE;
1140 }
1141
1142 /*
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.
1147  *
1148  * Returns a pointer to the head of the fragment data list if we have all the
1149  * fragments, NULL otherwise.
1150  *
1151  * This function assumes frag_number being a block sequence number.
1152  * The bsn for the first block is 0.
1153  */
1154 fragment_data *
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)
1158 {
1159         fragment_key key, *new_key;
1160         fragment_data *fd_head;
1161
1162         /* create key to search hash with */
1163         key.src = pinfo->src;
1164         key.dst = pinfo->dst;
1165         key.id  = id;
1166
1167         fd_head = g_hash_table_lookup(fragment_table, &key);
1168
1169         /* have we already seen this frame ?*/
1170         if (pinfo->fd->flags.visited) {
1171                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
1172                         return fd_head;
1173                 } else {
1174                         return NULL;
1175                 }
1176         }
1177
1178         if (fd_head==NULL){
1179                 /* not found, this must be the first snooped fragment for this
1180                  * packet. Create list-head.
1181                  */
1182                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1183
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
1187                  */
1188                 fd_head->next=NULL;
1189                 fd_head->datalen=0;
1190                 fd_head->offset=0;
1191                 fd_head->len=0;
1192                 fd_head->flags=FD_BLOCKSEQUENCE;
1193                 fd_head->data=NULL;
1194                 fd_head->reassembled_in=0;
1195
1196                 /*
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
1200                  * data.
1201                  */
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);
1207         }
1208
1209         if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1210                                   frag_number, frag_data_len, more_frags)) {
1211                 /*
1212                  * Reassembly is complete.
1213                  */
1214                 return fd_head;
1215         } else {
1216                 /*
1217                  * Reassembly isn't complete.
1218                  */
1219                 return NULL;
1220         }
1221 }
1222
1223 /*
1224  * This does the work for "fragment_add_seq_check()" and
1225  * "fragment_add_seq_next()".
1226  *
1227  * This function assumes frag_number being a block sequence number.
1228  * The bsn for the first block is 0.
1229  *
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
1232  * it uses 0.
1233  *
1234  * If "no_frag_number" is FALSE, it uses the "frag_number" argument as
1235  * the fragment number.
1236  *
1237  * If this is the first fragment seen for this datagram, a new
1238  * "fragment_data" structure is allocated to refer to the reassembled,
1239  * packet, and:
1240  *
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;
1244  *
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.
1248  *
1249  * Otherwise, this fragment is just added to the linked list of fragments
1250  * for this packet.
1251  *
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
1256  * otherwise.
1257  */
1258 fragment_data *
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)
1264 {
1265         reassembled_key reass_key;
1266         fragment_key key, *new_key, *old_key;
1267         gpointer orig_key, value;
1268         fragment_data *fd_head, *fd;
1269
1270         /*
1271          * Have we already seen this frame?
1272          * If so, look for it in the table of reassembled packets.
1273          */
1274         if (pinfo->fd->flags.visited) {
1275                 reass_key.frame = pinfo->fd->num;
1276                 reass_key.id = id;
1277                 return g_hash_table_lookup(reassembled_table, &reass_key);
1278         }
1279
1280         /* create key to search hash with */
1281         key.src = pinfo->src;
1282         key.dst = pinfo->dst;
1283         key.id  = id;
1284
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.
1289                  */
1290                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1291
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
1295                  */
1296                 fd_head->next=NULL;
1297                 fd_head->datalen=0;
1298                 fd_head->offset=0;
1299                 fd_head->len=0;
1300                 fd_head->flags=FD_BLOCKSEQUENCE;
1301                 fd_head->data=NULL;
1302                 fd_head->reassembled_in=0;
1303
1304                 if (!more_frags) {
1305                         /*
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.
1310                          */
1311                         fragment_reassembled(fd_head, pinfo,
1312                                reassembled_table, id);
1313                         return fd_head;
1314                 }
1315
1316                 /*
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
1320                  * data.
1321                  */
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);
1327
1328                 orig_key = new_key;     /* for unhashing it later */
1329
1330                 /*
1331                  * If we weren't given an initial fragment number,
1332                  * make it 0.
1333                  */
1334                 if (no_frag_number)
1335                         frag_number = 0;
1336         } else {
1337                 /*
1338                  * We found it.
1339                  */
1340                 fd_head = value;
1341
1342                 /*
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.
1346                  */
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;
1351                         }
1352                 }
1353         }
1354
1355         /*
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.
1358          *
1359          * If it's the first frame, handle it as an unfragmented packet.
1360          * Otherwise, just handle it as a fragment.
1361          *
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.
1364          */
1365         if (!tvb_bytes_exist(tvb, offset, frag_data_len)) {
1366                 if (!more_frags) {
1367                         /*
1368                          * Remove this from the table of in-progress
1369                          * reassemblies, and free up any memory used for
1370                          * it in that table.
1371                          */
1372                         old_key = orig_key;
1373                         fragment_unhash(fragment_table, old_key);
1374                 }
1375                 return frag_number == 0 ? fd_head : NULL;
1376         }
1377
1378         if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1379                                   frag_number, frag_data_len, more_frags)) {
1380                 /*
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.
1385                  */
1386
1387                 /*
1388                  * Remove this from the table of in-progress reassemblies,
1389                  * and free up any memory used for it in that table.
1390                  */
1391                 old_key = orig_key;
1392                 fragment_unhash(fragment_table, old_key);
1393
1394                 /*
1395                  * Add this item to the table of reassembled packets.
1396                  */
1397                 fragment_reassembled(fd_head, pinfo, reassembled_table, id);
1398                 return fd_head;
1399         } else {
1400                 /*
1401                  * Reassembly isn't complete.
1402                  */
1403                 return NULL;
1404         }
1405 }
1406
1407 fragment_data *
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)
1412 {
1413         return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1414             fragment_table, reassembled_table, frag_number, frag_data_len,
1415             more_frags, FALSE);
1416 }
1417
1418 fragment_data *
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)
1423 {
1424         return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1425             fragment_table, reassembled_table, 0, frag_data_len,
1426             more_frags, TRUE);
1427 }
1428
1429 /*
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.
1434  */
1435 tvbuff_t *
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)
1439 {
1440         tvbuff_t *next_tvb;
1441         gboolean update_col_info;
1442
1443         if (pinfo->fd->num == fd_head->reassembled_in) {
1444                 /*
1445                  * OK, we have the complete reassembled payload.
1446                  * Allocate a new tvbuff, referring to the reassembled
1447                  * payload.
1448                  */
1449                 if (fd_head->flags & FD_BLOCKSEQUENCE) {
1450                         next_tvb = tvb_new_real_data(fd_head->data,
1451                               fd_head->len, fd_head->len);
1452                 } else {
1453                         next_tvb = tvb_new_real_data(fd_head->data,
1454                               fd_head->datalen, fd_head->datalen);
1455                 }
1456
1457                 /*
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.
1461                  */
1462                 tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1463
1464                 /* Add the defragmented data to the data source list. */
1465                 add_new_data_source(pinfo, next_tvb, name);
1466
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);
1471                 } else {
1472                         update_col_info = !show_fragment_tree(fd_head,
1473                             fit, tree, pinfo, next_tvb);
1474                 }
1475                 if (update_col_infop != NULL)
1476                         *update_col_infop = update_col_info;
1477         } else {
1478                 /* We don't have the complete reassembled payload. */
1479                 next_tvb = NULL;
1480
1481                 /*
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.
1485                  */
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);
1490                 }
1491         }
1492         return next_tvb;
1493 }
1494
1495 /*
1496  * Show a single fragment in a fragment subtree.
1497  */
1498 static void
1499 show_fragment(fragment_data *fd, int offset, const fragment_items *fit,
1500     proto_tree *ft, tvbuff_t *tvb)
1501 {
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.
1506                  */
1507                 proto_tree *fet=NULL;
1508                 proto_item *fei=NULL;
1509                 int hf;
1510
1511                 if (fd->flags & (FD_OVERLAPCONFLICT
1512                         |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1513                         hf = *(fit->hf_fragment_error);
1514                 } else {
1515                         hf = *(fit->hf_fragment);
1516                 }
1517                 fei = proto_tree_add_uint_format(ft, hf,
1518                         tvb, offset, fd->len,
1519                         fd->frame,
1520                         "Frame:%u payload:%u-%u",
1521                         fd->frame,
1522                         offset,
1523                         offset+fd->len-1);
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),
1528                                 tvb, 0, 0,
1529                                 TRUE);
1530                 }
1531                 if (fd->flags&FD_OVERLAPCONFLICT) {
1532                         proto_tree_add_boolean(fet,
1533                                 *(fit->hf_fragment_overlap_conflict),
1534                                 tvb, 0, 0,
1535                                 TRUE);
1536                 }
1537                 if (fd->flags&FD_MULTIPLETAILS) {
1538                         proto_tree_add_boolean(fet,
1539                                 *(fit->hf_fragment_multiple_tails),
1540                                 tvb, 0, 0,
1541                                 TRUE);
1542                 }
1543                 if (fd->flags&FD_TOOLONGFRAGMENT) {
1544                         proto_tree_add_boolean(fet,
1545                                 *(fit->hf_fragment_too_long_fragment),
1546                                 tvb, 0, 0,
1547                                 TRUE);
1548                 }
1549         } else {
1550                 /* nothing of interest for this fragment */
1551                 proto_tree_add_uint_format(ft, *(fit->hf_fragment),
1552                         tvb, offset, fd->len,
1553                         fd->frame,
1554                         "Frame:%u payload:%u-%u",
1555                         fd->frame,
1556                         offset,
1557                         offset+fd->len-1
1558                 );
1559         }
1560 }
1561
1562 static gboolean
1563 show_fragment_errs_in_col(fragment_data *fd_head, const fragment_items *fit,
1564     packet_info *pinfo)
1565 {
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);
1571                         return TRUE;
1572                 }
1573         }
1574
1575         return FALSE;
1576 }
1577
1578 /* This function will build the fragment subtree; it's for fragments
1579    reassembled with "fragment_add()".
1580
1581    It will return TRUE if there were fragmentation errors
1582    or FALSE if fragmentation was ok.
1583 */
1584 gboolean
1585 show_fragment_tree(fragment_data *fd_head, const fragment_items *fit,
1586     proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb)
1587 {
1588         fragment_data *fd;
1589         proto_tree *ft;
1590         proto_item *fi;
1591
1592         /* It's not fragmented. */
1593         pinfo->fragmented = FALSE;
1594
1595         fi = proto_tree_add_item(tree, *(fit->hf_fragments),
1596             tvb, 0, -1, FALSE);
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);
1600
1601         return show_fragment_errs_in_col(fd_head, fit, pinfo);
1602 }
1603
1604 /* This function will build the fragment subtree; it's for fragments
1605    reassembled with "fragment_add_seq()" or "fragment_add_seq_check()".
1606
1607    It will return TRUE if there were fragmentation errors
1608    or FALSE if fragmentation was ok.
1609 */
1610 gboolean
1611 show_fragment_seq_tree(fragment_data *fd_head, const fragment_items *fit,
1612     proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb)
1613 {
1614         guint32 offset, next_offset;
1615         fragment_data *fd, *last_fd;
1616         proto_tree *ft;
1617         proto_item *fi;
1618
1619         /* It's not fragmented. */
1620         pinfo->fragmented = FALSE;
1621
1622         fi = proto_tree_add_item(tree, *(fit->hf_fragments),
1623             tvb, 0, -1, FALSE);
1624         ft = proto_item_add_subtree(fi, *(fit->ett_fragments));
1625         offset = 0;
1626         next_offset = 0;
1627         last_fd = NULL;
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;
1632                 }
1633                 last_fd = fd;
1634                 show_fragment(fd, offset, fit, ft, tvb);
1635         }
1636
1637         return show_fragment_errs_in_col(fd_head, fit, pinfo);
1638 }