Fix up the overlap checks in the fragment_add_seq functions for
[obnox/wireshark/wip.git] / reassemble.c
1 /* reassemble.c
2  * Routines for {fragment,segment} reassembly
3  *
4  * $Id: reassemble.c,v 1.38 2003/04/30 22:13:05 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 /*
101  * XXX - we use the frame number as the key (we can't use the frame_data
102  * structure, as in Tethereal there's only one such structure).
103  *
104  * This won't work if there's more than one form of reassembly using
105  * the reassembled-packet hash tables going on in the frame, and two
106  * or more are using the same protocol and thus the same hash table.
107  *
108  * We could use the addresses, or the reassembly ID, to distinguish
109  * between the reassemblies, if necessary.
110  *
111  * Hopefully, we won't see anything perverse such as that (say, some
112  * form of IP-in-IP tunneling, with fragments of an IP datagram
113  * tunneled inside IP datagrams that are themselves fragmented).
114  */
115 static gint
116 reassembled_equal(gconstpointer k1, gconstpointer k2)
117 {
118         return ((guint32)k1 == (guint32)k2);
119 }
120
121 static guint
122 reassembled_hash(gconstpointer k)
123 {
124         return (guint32)k;
125 }
126
127 /*
128  * For a fragment hash table entry, free the address data to which the key
129  * refers and the fragment data to which the value refers.
130  * (The actual key and value structures get freed by "reassemble_init()".)
131  */
132 static gboolean
133 free_all_fragments(gpointer key_arg, gpointer value, gpointer user_data _U_)
134 {
135         fragment_key *key = key_arg;
136         fragment_data *fd_head;
137
138         /*
139          * Grr.  I guess the theory here is that freeing
140          * something sure as heck modifies it, so you
141          * want to ban attempts to free it, but, alas,
142          * if we make the "data" field of an "address"
143          * structure not a "const", the compiler whines if
144          * we try to make it point into the data for a packet,
145          * as that's a "const" array (and should be, as dissectors
146          * shouldn't trash it).
147          *
148          * So we cast the complaint into oblivion, and rely on
149          * the fact that these addresses are known to have had
150          * their data mallocated, i.e. they don't point into,
151          * say, the middle of the data for a packet.
152          */
153         g_free((gpointer)key->src.data);
154         g_free((gpointer)key->dst.data);
155
156         for (fd_head = value; fd_head != NULL; fd_head = fd_head->next) {
157                 if(fd_head->data && !(fd_head->flags&FD_NOT_MALLOCED))
158                         g_free(fd_head->data);
159         }
160
161         return TRUE;
162 }
163
164 /*
165  * For a reassembled-packet hash table entry, free the fragment data
166  * to which the value refers.
167  * (The actual value structures get freed by "reassemble_init()".)
168  */
169 static gboolean
170 free_all_reassembled_fragments(gpointer key_arg _U_, gpointer value,
171                                gpointer user_data _U_)
172 {
173         fragment_data *fd_head;
174
175         for (fd_head = value; fd_head != NULL; fd_head = fd_head->next) {
176                 if(fd_head->data && !(fd_head->flags&FD_NOT_MALLOCED)) {
177                         g_free(fd_head->data);
178
179                         /*
180                          * A reassembled packet is inserted into the
181                          * hash table once for every frame that made
182                          * up the reassembled packet; clear the data
183                          * pointer so that we only free the data the
184                          * first time we see it.
185                          */
186                         fd_head->data = NULL;
187                 }
188         }
189
190         return TRUE;
191 }
192
193 /*
194  * Initialize a fragment table.
195  */
196 void
197 fragment_table_init(GHashTable **fragment_table)
198 {
199         if (*fragment_table != NULL) {
200                 /*
201                  * The fragment hash table exists.
202                  *
203                  * Remove all entries and free fragment data for
204                  * each entry.  (The key and value data is freed
205                  * by "reassemble_init()".)
206                  */
207                 g_hash_table_foreach_remove(*fragment_table,
208                                 free_all_fragments, NULL);
209         } else {
210                 /* The fragment table does not exist. Create it */
211                 *fragment_table = g_hash_table_new(fragment_hash,
212                                 fragment_equal);
213         }
214 }
215
216 /*
217  * Initialize a reassembled-packet table.
218  */
219 void
220 reassembled_table_init(GHashTable **reassembled_table)
221 {
222         if (*reassembled_table != NULL) {
223                 /*
224                  * The reassembled-packet hash table exists.
225                  *
226                  * Remove all entries and free fragment data for
227                  * each entry.  (The key and value data is freed
228                  * by "reassemble_init()".)
229                  */
230                 g_hash_table_foreach_remove(*reassembled_table,
231                                 free_all_reassembled_fragments, NULL);
232         } else {
233                 /* The fragment table does not exist. Create it */
234                 *reassembled_table = g_hash_table_new(reassembled_hash,
235                                 reassembled_equal);
236         }
237 }
238
239 /*
240  * Free up all space allocated for fragment keys and data.
241  */
242 void
243 reassemble_init(void)
244 {
245         if (fragment_key_chunk != NULL)
246                 g_mem_chunk_destroy(fragment_key_chunk);
247         if (fragment_data_chunk != NULL)
248                 g_mem_chunk_destroy(fragment_data_chunk);
249         fragment_key_chunk = g_mem_chunk_new("fragment_key_chunk",
250             sizeof(fragment_key),
251             fragment_init_count * sizeof(fragment_key),
252             G_ALLOC_AND_FREE);
253         fragment_data_chunk = g_mem_chunk_new("fragment_data_chunk",
254             sizeof(fragment_data),
255             fragment_init_count * sizeof(fragment_data),
256             G_ALLOC_ONLY);
257
258 }
259
260 /* This function cleans up the stored state and removes the reassembly data and
261  * (with one exception) all allocated memory for matching reassembly.
262  *
263  * The exception is :
264  * If the PDU was already completely reassembled, then the buffer containing the
265  * reassembled data WILL NOT be free()d, and the pointer to that buffer will be
266  * returned.
267  * Othervise the function will return NULL.
268  *
269  * So, if you call fragment_delete and it returns non-NULL, YOU are responsible to
270  * g_free() that buffer.
271  */
272 unsigned char *
273 fragment_delete(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
274 {
275         fragment_data *fd_head, *fd;
276         fragment_key key;
277         unsigned char *data=NULL;
278
279         /* create key to search hash with */
280         key.src = pinfo->src;
281         key.dst = pinfo->dst;
282         key.id  = id;
283
284         fd_head = g_hash_table_lookup(fragment_table, &key);
285
286         if(fd_head==NULL){
287                 /* We do not recognize this as a PDU we have seen before. return*/
288                 return NULL;
289         }
290
291         data=fd_head->data;
292         /* loop over all partial fragments and free any buffers */
293         for(fd=fd_head->next;fd;){
294                 fragment_data *tmp_fd;
295                 tmp_fd=fd->next;
296
297                 if( !(fd->flags&FD_NOT_MALLOCED) )
298                         g_free(fd->data);
299                 g_mem_chunk_free(fragment_data_chunk, fd);
300                 fd=tmp_fd;
301         }
302         g_mem_chunk_free(fragment_data_chunk, fd_head);
303         g_hash_table_remove(fragment_table, &key);
304
305         return data;
306 }
307
308 /* This function is used to check if there is partial or completed reassembly state
309  * matching this packet. I.e. Are there reassembly going on or not for this packet?
310  */
311 fragment_data *
312 fragment_get(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
313 {
314         fragment_data *fd_head;
315         fragment_key key;
316
317         /* create key to search hash with */
318         key.src = pinfo->src;
319         key.dst = pinfo->dst;
320         key.id  = id;
321
322         fd_head = g_hash_table_lookup(fragment_table, &key);
323
324         return fd_head;
325 }
326
327 /* This function can be used to explicitely set the total length (if known)
328  * for reassembly of a PDU.
329  * This is useful for reassembly of PDUs where one may have the total length specified
330  * in the first fragment instead of as for, say, IPv4 where a flag indicates which
331  * is the last fragment.
332  *
333  * Such protocols might fragment_add with a more_frags==TRUE for every fragment
334  * and just tell the reassembly engine the expected total length of the reassembled data
335  * using fragment_set_tot_len immediately after doing fragment_add for the first packet.
336  *
337  * note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment.
338  * i.e. since the block numbers start at 0, if we specify tot_len==2, that
339  * actually means we want to defragment 3 blocks, block 0, 1 and 2.
340  */
341 void
342 fragment_set_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table,
343                      guint32 tot_len)
344 {
345         fragment_data *fd_head;
346         fragment_key key;
347
348         /* create key to search hash with */
349         key.src = pinfo->src;
350         key.dst = pinfo->dst;
351         key.id  = id;
352
353         fd_head = g_hash_table_lookup(fragment_table, &key);
354
355         if(fd_head){
356                 fd_head->datalen = tot_len;
357         }
358
359         return;
360 }
361
362 guint32
363 fragment_get_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
364 {
365         fragment_data *fd_head;
366         fragment_key key;
367
368         /* create key to search hash with */
369         key.src = pinfo->src;
370         key.dst = pinfo->dst;
371         key.id  = id;
372
373         fd_head = g_hash_table_lookup(fragment_table, &key);
374
375         if(fd_head){
376                 return fd_head->datalen;
377         }
378
379         return 0;
380 }
381
382
383 /* This function will set the partial reassembly flag for a fh.
384    When this function is called, the fh MUST already exist, i.e.
385    the fh MUST be created by the initial call to fragment_add() before
386    this function is called.
387    Also note that this function MUST be called to indicate a fh will be
388    extended (increase the already stored data)
389 */
390
391 void
392 fragment_set_partial_reassembly(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
393 {
394         fragment_data *fd_head;
395         fragment_key key;
396
397         /* create key to search hash with */
398         key.src = pinfo->src;
399         key.dst = pinfo->dst;
400         key.id  = id;
401
402         fd_head = g_hash_table_lookup(fragment_table, &key);
403
404         if(fd_head){
405                 fd_head->flags |= FD_PARTIAL_REASSEMBLY;
406         }
407 }
408
409 /*
410  * This function gets rid of an entry from a fragment table, given
411  * a pointer to the key for that entry; it also frees up the key
412  * and the addresses in it.
413  */
414 static void
415 fragment_unhash(GHashTable *fragment_table, fragment_key *key)
416 {
417         /*
418          * Free up the copies of the addresses from the old key.
419          */
420         g_free((gpointer)key->src.data);
421         g_free((gpointer)key->dst.data);
422
423         /*
424          * Remove the entry from the fragment table.
425          */
426         g_hash_table_remove(fragment_table, key);
427
428         /*
429          * Free the key itself.
430          */
431         g_mem_chunk_free(fragment_key_chunk, key);
432 }
433
434 /*
435  * This function adds fragment_data structure to a reassembled-packet
436  * hash table, using the frame numbers of each of the frames from
437  * which it was reassembled as keys, and sets the "reassembled_in"
438  * frame number.
439  */
440 void
441 fragment_reassembled(fragment_data *fd_head, packet_info *pinfo,
442              GHashTable *reassembled_table)
443 {
444         fragment_data *fd;
445
446         if (fd_head->next == NULL) {
447                 /*
448                  * This was not fragmented, so there's no fragment
449                  * table; just hash it using the current frame number.
450                  */
451                 g_hash_table_insert(reassembled_table, (gpointer)pinfo->fd->num,
452                     fd_head);
453         } else {
454                 /*
455                  * Hash it with the frame numbers for all the frames.
456                  */
457                 for (fd = fd_head->next; fd != NULL; fd = fd->next){
458                         g_hash_table_insert(reassembled_table,
459                             (gpointer)fd->frame, fd_head);
460                 }
461         }
462         fd_head->reassembled_in = pinfo->fd->num;
463 }
464
465 /*
466  * This function adds a new fragment to the fragment hash table.
467  * If this is the first fragment seen for this datagram, a new entry
468  * is created in the hash table, otherwise this fragment is just added
469  * to the linked list of fragments for this packet.
470  * The list of fragments for a specific datagram is kept sorted for
471  * easier handling.
472  *
473  * Returns a pointer to the head of the fragment data list if we have all the
474  * fragments, NULL otherwise.
475  *
476  * This function assumes frag_offset being a byte offset into the defragment
477  * packet.
478  *
479  * 01-2002
480  * Once the fh is defragmented (= FD_DEFRAGMENTED set), it can be
481  * extended using the FD_PARTIAL_REASSEMBLY flag. This flag should be set
482  * using fragment_set_partial_reassembly() before calling fragment_add
483  * with the new fragment. FD_TOOLONGFRAGMENT and FD_MULTIPLETAILS flags
484  * are lowered when a new extension process is started.
485  */
486 static gboolean
487 fragment_add_work(fragment_data *fd_head, tvbuff_t *tvb, int offset,
488              packet_info *pinfo, guint32 frag_offset,
489              guint32 frag_data_len, gboolean more_frags)
490 {
491         fragment_data *fd;
492         fragment_data *fd_i;
493         guint32 max, dfpos;
494         unsigned char *old_data;
495
496         /* create new fd describing this fragment */
497         fd = g_mem_chunk_alloc(fragment_data_chunk);
498         fd->next = NULL;
499         fd->flags = 0;
500         fd->frame = pinfo->fd->num;
501         fd->offset = frag_offset;
502         fd->len  = frag_data_len;
503         fd->data = NULL;
504
505         /*
506          * If it was already defragmented and this new fragment goes beyond
507          * data limits, set flag in already empty fds & point old fds to malloc'ed data.
508          */
509         if(fd_head->flags & FD_DEFRAGMENTED && (frag_offset+frag_data_len) >= fd_head->datalen &&
510                 fd_head->flags & FD_PARTIAL_REASSEMBLY){
511                 for(fd_i=fd_head->next; fd_i; fd_i=fd_i->next){
512                         if( !fd_i->data ) {
513                                 fd_i->data = fd_head->data + fd_i->offset;
514                                 fd_i->flags |= FD_NOT_MALLOCED;
515                         }
516                         fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
517                 }
518                 fd_head->flags ^= FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY;
519                 fd_head->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
520                 fd_head->datalen=0;
521         }
522
523         if (!more_frags) {
524                 /*
525                  * This is the tail fragment in the sequence.
526                  */
527                 if (fd_head->datalen) {
528                         /* ok we have already seen other tails for this packet
529                          * it might be a duplicate.
530                          */
531                         if (fd_head->datalen != (fd->offset + fd->len) ){
532                                 /* Oops, this tail indicates a different packet
533                                  * len than the previous ones. Somethings wrong
534                                  */
535                                 fd->flags      |= FD_MULTIPLETAILS;
536                                 fd_head->flags |= FD_MULTIPLETAILS;
537                         }
538                 } else {
539                         /* this was the first tail fragment, now we know the
540                          * length of the packet
541                          */
542                         fd_head->datalen = fd->offset + fd->len;
543                 }
544         }
545
546
547
548
549         /* If the packet is already defragmented, this MUST be an overlap.
550          * The entire defragmented packet is in fd_head->data
551          * Even if we have previously defragmented this packet, we still check
552          * check it. Someone might play overlap and TTL games.
553          */
554         if (fd_head->flags & FD_DEFRAGMENTED) {
555                 fd->flags      |= FD_OVERLAP;
556                 fd_head->flags |= FD_OVERLAP;
557                 /* make sure its not too long */
558                 if (fd->offset + fd->len > fd_head->datalen) {
559                         fd->flags      |= FD_TOOLONGFRAGMENT;
560                         fd_head->flags |= FD_TOOLONGFRAGMENT;
561                         LINK_FRAG(fd_head,fd);
562                         return TRUE;
563                 }
564                 /* make sure it doesnt conflict with previous data */
565                 if ( memcmp(fd_head->data+fd->offset,
566                         tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
567                         fd->flags      |= FD_OVERLAPCONFLICT;
568                         fd_head->flags |= FD_OVERLAPCONFLICT;
569                         LINK_FRAG(fd_head,fd);
570                         return TRUE;
571                 }
572                 /* it was just an overlap, link it and return */
573                 LINK_FRAG(fd_head,fd);
574                 return TRUE;
575         }
576
577
578
579         /* If we have reached this point, the packet is not defragmented yet.
580          * Save all payload in a buffer until we can defragment.
581          * XXX - what if we didn't capture the entire fragment due
582          * to a too-short snapshot length?
583          */
584         fd->data = g_malloc(fd->len);
585         tvb_memcpy(tvb, fd->data, offset, fd->len);
586         LINK_FRAG(fd_head,fd);
587
588
589         if( !(fd_head->datalen) ){
590                 /* if we dont know the datalen, there are still missing
591                  * packets. Cheaper than the check below.
592                  */
593                 return FALSE;
594         }
595
596
597         /* check if we have received the entire fragment
598          * this is easy since the list is sorted and the head is faked.
599          */
600         max = 0;
601         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
602                 if ( ((fd_i->offset)<=max) &&
603                     ((fd_i->offset+fd_i->len)>max) ){
604                         max = fd_i->offset+fd_i->len;
605                 }
606         }
607
608         if (max < (fd_head->datalen)) {
609                 /* we have not received all packets yet */
610                 return FALSE;
611         }
612
613
614         if (max > (fd_head->datalen)) {
615                 /*XXX not sure if current fd was the TOOLONG*/
616                 /*XXX is it fair to flag current fd*/
617                 /* oops, too long fragment detected */
618                 fd->flags      |= FD_TOOLONGFRAGMENT;
619                 fd_head->flags |= FD_TOOLONGFRAGMENT;
620         }
621
622
623         /* we have received an entire packet, defragment it and
624          * free all fragments
625          */
626         /* store old data just in case */
627         old_data=fd_head->data;
628         fd_head->data = g_malloc(max);
629
630         /* add all data fragments */
631         for (dfpos=0,fd_i=fd_head;fd_i;fd_i=fd_i->next) {
632                 if (fd_i->len) {
633                         if (fd_i->offset < dfpos) {
634                                 fd_i->flags    |= FD_OVERLAP;
635                                 fd_head->flags |= FD_OVERLAP;
636                                 if ( memcmp(fd_head->data+fd_i->offset,
637                                         fd_i->data,
638                                         MIN(fd_i->len,(dfpos-fd_i->offset))
639                                         ) ){
640                                         fd_i->flags    |= FD_OVERLAPCONFLICT;
641                                         fd_head->flags |= FD_OVERLAPCONFLICT;
642                                 }
643                         }
644                         /* dfpos is always >= than fd_i->offset */
645                         /* No gaps can exist here, max_loop(above) does this */
646                         if( fd_i->offset+fd_i->len > dfpos )
647                                 memcpy(fd_head->data+dfpos, fd_i->data+(dfpos-fd_i->offset),
648                                         fd_i->len-(dfpos-fd_i->offset));
649                         if( fd_i->flags & FD_NOT_MALLOCED )
650                                 fd_i->flags ^= FD_NOT_MALLOCED;
651                         else
652                                 g_free(fd_i->data);
653                         fd_i->data=NULL;
654
655                         dfpos=MAX(dfpos,(fd_i->offset+fd_i->len));
656                 }
657         }
658
659         if( old_data )
660                 g_free(old_data);
661         /* mark this packet as defragmented.
662            allows us to skip any trailing fragments */
663         fd_head->flags |= FD_DEFRAGMENTED;
664         fd_head->reassembled_in=pinfo->fd->num;
665
666         return TRUE;
667 }
668
669 fragment_data *
670 fragment_add_common(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
671              GHashTable *fragment_table, guint32 frag_offset,
672              guint32 frag_data_len, gboolean more_frags,
673              gboolean check_already_added)
674 {
675         fragment_key key, *new_key;
676         fragment_data *fd_head;
677         fragment_data *fd_item;
678         gboolean already_added=pinfo->fd->flags.visited;
679
680         /* create key to search hash with */
681         key.src = pinfo->src;
682         key.dst = pinfo->dst;
683         key.id  = id;
684
685         fd_head = g_hash_table_lookup(fragment_table, &key);
686
687         /*
688          * "already_added" is true if "pinfo->fd->flags.visited" is true;
689          * if "pinfo->fd->flags.visited", this isn't the first pass, so
690          * we've already done all the reassembly and added all the
691          * fragments.
692          *
693          * If it's not true, but "check_already_added" is true, just check
694          * if we have seen this fragment before, i.e., if we have already
695          * added it to reassembly.
696          * That can be true even if "pinfo->fd->flags.visited" is false
697          * since we sometimes might call a subdissector multiple times.
698          * As an additional check, just make sure we have not already added 
699          * this frame to the reassembly list, if there is a reassembly list;
700          * note that the first item in the reassembly list is not a
701          * fragment, it's a data structure for the reassembled packet.
702          * We don't check it because its "frame" member isn't initialized
703          * to anything, and because it doesn't count in any case.
704          */
705         if (!already_added && check_already_added && fd_head != NULL) {
706                 for(fd_item=fd_head->next;fd_item;fd_item=fd_item->next){
707                         if(pinfo->fd->num==fd_item->frame){
708                                 already_added=TRUE;
709                         }
710                 }
711         }
712         /* have we already added this frame ?*/
713         if (already_added) {
714                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
715                         return fd_head;
716                 } else {
717                         return NULL;
718                 }
719         }
720
721         if (fd_head==NULL){
722                 /* not found, this must be the first snooped fragment for this
723                  * packet. Create list-head.
724                  */
725                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
726
727                 /* head/first structure in list only holds no other data than
728                  * 'datalen' then we don't have to change the head of the list
729                  * even if we want to keep it sorted
730                  */
731                 fd_head->next=NULL;
732                 fd_head->datalen=0;
733                 fd_head->offset=0;
734                 fd_head->len=0;
735                 fd_head->flags=0;
736                 fd_head->data=NULL;
737                 fd_head->reassembled_in=0;
738
739                 /*
740                  * We're going to use the key to insert the fragment,
741                  * so allocate a structure for it, and copy the
742                  * addresses, allocating new buffers for the address
743                  * data.
744                  */
745                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
746                 COPY_ADDRESS(&new_key->src, &key.src);
747                 COPY_ADDRESS(&new_key->dst, &key.dst);
748                 new_key->id = key.id;
749                 g_hash_table_insert(fragment_table, new_key, fd_head);
750         }
751
752         if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
753             frag_data_len, more_frags)) {
754                 /*
755                  * Reassembly is complete.
756                  */
757                 return fd_head;
758         } else {
759                 /*
760                  * Reassembly isn't complete.
761                  */
762                 return NULL;
763         }
764 }
765
766 fragment_data *
767 fragment_add(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
768              GHashTable *fragment_table, guint32 frag_offset,
769              guint32 frag_data_len, gboolean more_frags)
770 {
771         return fragment_add_common(tvb, offset, pinfo, id, fragment_table,
772             frag_offset, frag_data_len, more_frags, TRUE);
773 }
774
775 /*
776  * For use when you can have multiple fragments in the same frame added
777  * to the same reassembled PDU, e.g. with ONC RPC-over-TCP.
778  */
779 fragment_data *
780 fragment_add_multiple_ok(tvbuff_t *tvb, int offset, packet_info *pinfo,
781                          guint32 id, GHashTable *fragment_table,
782                          guint32 frag_offset, guint32 frag_data_len,
783                          gboolean more_frags)
784 {
785         return fragment_add_common(tvb, offset, pinfo, id, fragment_table,
786             frag_offset, frag_data_len, more_frags, FALSE);
787 }
788
789 fragment_data *
790 fragment_add_check(tvbuff_t *tvb, int offset, packet_info *pinfo,
791              guint32 id, GHashTable *fragment_table,
792              GHashTable *reassembled_table, guint32 frag_offset,
793              guint32 frag_data_len, gboolean more_frags)
794 {
795         fragment_key key, *new_key, *old_key;
796         gpointer orig_key, value;
797         fragment_data *fd_head;
798
799         /*
800          * If this isn't the first pass, look for this frame in the table
801          * of reassembled packets.
802          */
803         if (pinfo->fd->flags.visited)
804                 return g_hash_table_lookup(reassembled_table,
805                     (gpointer)pinfo->fd->num);
806
807         /* create key to search hash with */
808         key.src = pinfo->src;
809         key.dst = pinfo->dst;
810         key.id  = id;
811
812         if (!g_hash_table_lookup_extended(fragment_table, &key,
813                                           &orig_key, &value)) {
814                 /* not found, this must be the first snooped fragment for this
815                  * packet. Create list-head.
816                  */
817                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
818
819                 /* head/first structure in list only holds no other data than
820                  * 'datalen' then we don't have to change the head of the list
821                  * even if we want to keep it sorted
822                  */
823                 fd_head->next=NULL;
824                 fd_head->datalen=0;
825                 fd_head->offset=0;
826                 fd_head->len=0;
827                 fd_head->flags=0;
828                 fd_head->data=NULL;
829                 fd_head->reassembled_in=0;
830
831                 /*
832                  * We're going to use the key to insert the fragment,
833                  * so allocate a structure for it, and copy the
834                  * addresses, allocating new buffers for the address
835                  * data.
836                  */
837                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
838                 COPY_ADDRESS(&new_key->src, &key.src);
839                 COPY_ADDRESS(&new_key->dst, &key.dst);
840                 new_key->id = key.id;
841                 g_hash_table_insert(fragment_table, new_key, fd_head);
842
843                 orig_key = new_key;     /* for unhashing it later */
844         } else {
845                 /*
846                  * We found it.
847                  */
848                 fd_head = value;
849         }
850
851         /*
852          * If this is a short frame, then we can't, and don't, do
853          * reassembly on it.  We just give up.
854          */
855         if (tvb_reported_length(tvb) > tvb_length(tvb))
856                 return NULL;
857
858         if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
859             frag_data_len, more_frags)) {
860                 /*
861                  * Reassembly is complete.
862                  * Remove this from the table of in-progress
863                  * reassemblies, add it to the table of
864                  * reassembled packets, and return it.
865                  */
866
867                 /*
868                  * Remove this from the table of in-progress reassemblies,
869                  * and free up any memory used for it in that table.
870                  */
871                 old_key = orig_key;
872                 fragment_unhash(fragment_table, old_key);
873
874                 /*
875                  * Add this item to the table of reassembled packets.
876                  */
877                 fragment_reassembled(fd_head, pinfo, reassembled_table);
878                 return fd_head;
879         } else {
880                 /*
881                  * Reassembly isn't complete.
882                  */
883                 return NULL;
884         }
885 }
886
887 /*
888  * This function adds a new fragment to the entry for a reassembly
889  * operation.
890  *
891  * The list of fragments for a specific datagram is kept sorted for
892  * easier handling.
893  *
894  * Returns TRUE if we have all the fragments, FALSE otherwise.
895  *
896  * This function assumes frag_number being a block sequence number.
897  * The bsn for the first block is 0.
898  */
899 static gboolean
900 fragment_add_seq_work(fragment_data *fd_head, tvbuff_t *tvb, int offset,
901              packet_info *pinfo, guint32 frag_number,
902              guint32 frag_data_len, gboolean more_frags)
903 {
904         fragment_data *fd;
905         fragment_data *fd_i;
906         fragment_data *last_fd;
907         guint32 max, dfpos, size;
908
909         /* create new fd describing this fragment */
910         fd = g_mem_chunk_alloc(fragment_data_chunk);
911         fd->next = NULL;
912         fd->flags = 0;
913         fd->frame = pinfo->fd->num;
914         fd->offset = frag_number;
915         fd->len  = frag_data_len;
916         fd->data = NULL;
917
918         if (!more_frags) {
919                 /*
920                  * This is the tail fragment in the sequence.
921                  */
922                 if (fd_head->datalen) {
923                         /* ok we have already seen other tails for this packet
924                          * it might be a duplicate.
925                          */
926                         if (fd_head->datalen != fd->offset ){
927                                 /* Oops, this tail indicates a different packet
928                                  * len than the previous ones. Somethings wrong
929                                  */
930                                 fd->flags      |= FD_MULTIPLETAILS;
931                                 fd_head->flags |= FD_MULTIPLETAILS;
932                         }
933                 } else {
934                         /* this was the first tail fragment, now we know the
935                          * sequence number of that fragment (which is NOT
936                          * the length of the packet!)
937                          */
938                         fd_head->datalen = fd->offset;
939                 }
940         }
941
942         /* If the packet is already defragmented, this MUST be an overlap.
943          * The entire defragmented packet is in fd_head->data
944          * Even if we have previously defragmented this packet, we still check
945          * check it. Someone might play overlap and TTL games.
946          */
947         if (fd_head->flags & FD_DEFRAGMENTED) {
948                 fd->flags      |= FD_OVERLAP;
949                 fd_head->flags |= FD_OVERLAP;
950
951                 /* make sure it's not past the end */
952                 if (fd->offset > fd_head->datalen) {
953                         /* new fragment comes after the end */
954                         fd->flags      |= FD_TOOLONGFRAGMENT;
955                         fd_head->flags |= FD_TOOLONGFRAGMENT;
956                         LINK_FRAG(fd_head,fd);
957                         return TRUE;
958                 }
959                 /* make sure it doesnt conflict with previous data */
960                 dfpos=0;
961                 last_fd=NULL;
962                 for (fd_i=fd_head->next;fd_i->offset!=fd->offset;fd_i=fd_i->next) {
963                   if (!last_fd || last_fd->offset!=fd_i->offset){
964                     dfpos += fd_i->len;
965                   }
966                   last_fd=fd_i;
967                 }
968                 if(fd_i){
969                         /* new fragment overlaps existing fragment */
970                         if(fd_i->len!=fd->len){
971                                 /*
972                                  * They have different lengths; this
973                                  * is definitely a conflict.
974                                  */
975                                 fd->flags      |= FD_OVERLAPCONFLICT;
976                                 fd_head->flags |= FD_OVERLAPCONFLICT;
977                                 LINK_FRAG(fd_head,fd);
978                                 return TRUE;
979                         }
980                         g_assert(fd_head->len >= dfpos + fd->len);
981                         if ( memcmp(fd_head->data+dfpos,
982                             tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
983                                 /*
984                                  * They have the same length, but the
985                                  * data isn't the same.
986                                  */
987                                 fd->flags      |= FD_OVERLAPCONFLICT;
988                                 fd_head->flags |= FD_OVERLAPCONFLICT;
989                                 LINK_FRAG(fd_head,fd);
990                                 return TRUE;
991                         }
992                         /* it was just an overlap, link it and return */
993                         LINK_FRAG(fd_head,fd);
994                         return TRUE;
995                 } else {
996                         /*
997                          * New fragment doesn't overlap an existing
998                          * fragment - there was presumably a gap in
999                          * the sequence number space.
1000                          *
1001                          * XXX - what should we do here?  Is it always
1002                          * the case that there are no gaps, or are there
1003                          * protcols using sequence numbers where there
1004                          * can be gaps?
1005                          *
1006                          * If the former, the check below for having
1007                          * received all the fragments should check for
1008                          * holes in the sequence number space and for the
1009                          * first sequence number being 0.  If we do that,
1010                          * the only way we can get here is if this fragment
1011                          * is past the end of the sequence number space -
1012                          * but the check for "fd->offset > fd_head->datalen"
1013                          * would have caught that above, so it can't happen.
1014                          *
1015                          * If the latter, we don't have a good way of
1016                          * knowing whether reassembly is complete if we
1017                          * get packet out of order such that the "last"
1018                          * fragment doesn't show up last - but, unless
1019                          * in-order reliable delivery of fragments is
1020                          * guaranteed, an implementation of the protocol
1021                          * has no way of knowing whether reassembly is
1022                          * complete, either.
1023                          *
1024                          * For now, we just link the fragment in and
1025                          * return.
1026                          */
1027                         LINK_FRAG(fd_head,fd);
1028                         return TRUE;
1029                 }
1030         }
1031
1032         /* If we have reached this point, the packet is not defragmented yet.
1033          * Save all payload in a buffer until we can defragment.
1034          * XXX - what if we didn't capture the entire fragment due
1035          * to a too-short snapshot length?
1036          */
1037         fd->data = g_malloc(fd->len);
1038         tvb_memcpy(tvb, fd->data, offset, fd->len);
1039         LINK_FRAG(fd_head,fd);
1040
1041
1042         if( !(fd_head->datalen) ){
1043                 /* if we dont know the sequence number of the last fragment,
1044                  * there are definitely still missing packets. Cheaper than
1045                  * the check below.
1046                  */
1047                 return FALSE;
1048         }
1049
1050
1051         /* check if we have received the entire fragment
1052          * this is easy since the list is sorted and the head is faked.
1053          */
1054         max = 0;
1055         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1056           if ( fd_i->offset==max ){
1057             max++;
1058           }
1059         }
1060         /* max will now be datalen+1 if all fragments have been seen */
1061
1062         if (max <= fd_head->datalen) {
1063                 /* we have not received all packets yet */
1064                 return FALSE;
1065         }
1066
1067
1068         if (max > (fd_head->datalen+1)) {
1069                 /* oops, too long fragment detected */
1070                 fd->flags      |= FD_TOOLONGFRAGMENT;
1071                 fd_head->flags |= FD_TOOLONGFRAGMENT;
1072         }
1073
1074
1075         /* we have received an entire packet, defragment it and
1076          * free all fragments
1077          */
1078         size=0;
1079         last_fd=NULL;
1080         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1081           if(!last_fd || last_fd->offset!=fd_i->offset){
1082             size+=fd_i->len;
1083           }
1084           last_fd=fd_i;
1085         }
1086         fd_head->data = g_malloc(size);
1087         fd_head->len = size;            /* record size for caller       */
1088
1089         /* add all data fragments */
1090         last_fd=NULL;
1091         for (dfpos=0,fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1092           if (fd_i->len) {
1093             if(!last_fd || last_fd->offset!=fd_i->offset){
1094               memcpy(fd_head->data+dfpos,fd_i->data,fd_i->len);
1095               dfpos += fd_i->len;
1096             } else {
1097               /* duplicate/retransmission/overlap */
1098               fd_i->flags    |= FD_OVERLAP;
1099               fd_head->flags |= FD_OVERLAP;
1100               if( (last_fd->len!=fd_i->datalen)
1101                   || memcmp(last_fd->data, fd_i->data, last_fd->len) ){
1102                         fd->flags      |= FD_OVERLAPCONFLICT;
1103                         fd_head->flags |= FD_OVERLAPCONFLICT;
1104               }
1105             }
1106             last_fd=fd_i;
1107           }
1108         }
1109
1110         /* we have defragmented the pdu, now free all fragments*/
1111         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1112           if(fd_i->data){
1113             g_free(fd_i->data);
1114             fd_i->data=NULL;
1115           }
1116         }
1117
1118         /* mark this packet as defragmented.
1119            allows us to skip any trailing fragments */
1120         fd_head->flags |= FD_DEFRAGMENTED;
1121         fd_head->reassembled_in=pinfo->fd->num;
1122
1123         return TRUE;
1124 }
1125
1126 /*
1127  * This function adds a new fragment to the fragment hash table.
1128  * If this is the first fragment seen for this datagram, a new entry
1129  * is created in the hash table, otherwise this fragment is just added
1130  * to the linked list of fragments for this packet.
1131  *
1132  * Returns a pointer to the head of the fragment data list if we have all the
1133  * fragments, NULL otherwise.
1134  *
1135  * This function assumes frag_number being a block sequence number.
1136  * The bsn for the first block is 0.
1137  */
1138 fragment_data *
1139 fragment_add_seq(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
1140              GHashTable *fragment_table, guint32 frag_number,
1141              guint32 frag_data_len, gboolean more_frags)
1142 {
1143         fragment_key key, *new_key;
1144         fragment_data *fd_head;
1145
1146         /* create key to search hash with */
1147         key.src = pinfo->src;
1148         key.dst = pinfo->dst;
1149         key.id  = id;
1150
1151         fd_head = g_hash_table_lookup(fragment_table, &key);
1152
1153         /* have we already seen this frame ?*/
1154         if (pinfo->fd->flags.visited) {
1155                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
1156                         return fd_head;
1157                 } else {
1158                         return NULL;
1159                 }
1160         }
1161
1162         if (fd_head==NULL){
1163                 /* not found, this must be the first snooped fragment for this
1164                  * packet. Create list-head.
1165                  */
1166                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1167
1168                 /* head/first structure in list only holds no other data than
1169                  * 'datalen' then we don't have to change the head of the list
1170                  * even if we want to keep it sorted
1171                  */
1172                 fd_head->next=NULL;
1173                 fd_head->datalen=0;
1174                 fd_head->offset=0;
1175                 fd_head->len=0;
1176                 fd_head->flags=FD_BLOCKSEQUENCE;
1177                 fd_head->data=NULL;
1178                 fd_head->reassembled_in=0;
1179
1180                 /*
1181                  * We're going to use the key to insert the fragment,
1182                  * so allocate a structure for it, and copy the
1183                  * addresses, allocating new buffers for the address
1184                  * data.
1185                  */
1186                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
1187                 COPY_ADDRESS(&new_key->src, &key.src);
1188                 COPY_ADDRESS(&new_key->dst, &key.dst);
1189                 new_key->id = key.id;
1190                 g_hash_table_insert(fragment_table, new_key, fd_head);
1191         }
1192
1193         if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1194                                   frag_number, frag_data_len, more_frags)) {
1195                 /*
1196                  * Reassembly is complete.
1197                  */
1198                 return fd_head;
1199         } else {
1200                 /*
1201                  * Reassembly isn't complete.
1202                  */
1203                 return NULL;
1204         }
1205 }
1206
1207 /*
1208  * This does the work for "fragment_add_seq_check()" and
1209  * "fragment_add_seq_next()".
1210  *
1211  * This function assumes frag_number being a block sequence number.
1212  * The bsn for the first block is 0.
1213  *
1214  * If "no_frag_number" is TRUE, it uses the next expected fragment number
1215  * as the fragment number if there is a reassembly in progress, otherwise
1216  * it uses 0.
1217  *
1218  * If "no_frag_number" is FALSE, it uses the "frag_number" argument as
1219  * the fragment number.
1220  *
1221  * If this is the first fragment seen for this datagram, a new
1222  * "fragment_data" structure is allocated to refer to the reassembled,
1223  * packet, and:
1224  *
1225  *      if "more_frags" is false, the structure is not added to
1226  *      the hash table, and not given any fragments to refer to,
1227  *      but is just returned;
1228  *
1229  *      if "more_frags" is true, this fragment is added to the linked
1230  *      list of fragments for this packet, and the "fragment_data"
1231  *      structure is put into the hash table.
1232  *
1233  * Otherwise, this fragment is just added to the linked list of fragments
1234  * for this packet.
1235  *
1236  * Returns a pointer to the head of the fragment data list, and removes
1237  * that from the fragment hash table if necessary and adds it to the
1238  * table of reassembled fragments, if we have all the fragments or if
1239  * this is the only fragment and "more_frags" is false, returns NULL
1240  * otherwise.
1241  */
1242 fragment_data *
1243 fragment_add_seq_check_work(tvbuff_t *tvb, int offset, packet_info *pinfo,
1244              guint32 id, GHashTable *fragment_table,
1245              GHashTable *reassembled_table, guint32 frag_number,
1246              guint32 frag_data_len, gboolean more_frags,
1247              gboolean no_frag_number)
1248 {
1249         fragment_key key, *new_key, *old_key;
1250         gpointer orig_key, value;
1251         fragment_data *fd_head, *fd;
1252
1253         /*
1254          * Have we already seen this frame?
1255          * If so, look for it in the table of reassembled packets.
1256          */
1257         if (pinfo->fd->flags.visited)
1258                 return g_hash_table_lookup(reassembled_table,
1259                     (gpointer)pinfo->fd->num);
1260
1261         /* create key to search hash with */
1262         key.src = pinfo->src;
1263         key.dst = pinfo->dst;
1264         key.id  = id;
1265
1266         if (!g_hash_table_lookup_extended(fragment_table, &key,
1267                                           &orig_key, &value)) {
1268                 /* not found, this must be the first snooped fragment for this
1269                  * packet. Create list-head.
1270                  */
1271                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1272
1273                 /* head/first structure in list only holds no other data than
1274                  * 'datalen' then we don't have to change the head of the list
1275                  * even if we want to keep it sorted
1276                  */
1277                 fd_head->next=NULL;
1278                 fd_head->datalen=0;
1279                 fd_head->offset=0;
1280                 fd_head->len=0;
1281                 fd_head->flags=FD_BLOCKSEQUENCE;
1282                 fd_head->data=NULL;
1283                 fd_head->reassembled_in=0;
1284
1285                 if (!more_frags) {
1286                         /*
1287                          * This is the last snooped fragment for this
1288                          * packet as well; that means it's the only
1289                          * fragment.  Just add it to the table of
1290                          * reassembled packets, and return it.
1291                          */
1292                         fragment_reassembled(fd_head, pinfo,
1293                                reassembled_table);
1294                         return fd_head;
1295                 }
1296
1297                 /*
1298                  * We're going to use the key to insert the fragment,
1299                  * so allocate a structure for it, and copy the
1300                  * addresses, allocating new buffers for the address
1301                  * data.
1302                  */
1303                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
1304                 COPY_ADDRESS(&new_key->src, &key.src);
1305                 COPY_ADDRESS(&new_key->dst, &key.dst);
1306                 new_key->id = key.id;
1307                 g_hash_table_insert(fragment_table, new_key, fd_head);
1308
1309                 orig_key = new_key;     /* for unhashing it later */
1310
1311                 /*
1312                  * If we weren't given an initial fragment number,
1313                  * make it 0.
1314                  */
1315                 if (no_frag_number)
1316                         frag_number = 0;
1317         } else {
1318                 /*
1319                  * We found it.
1320                  */
1321                 fd_head = value;
1322
1323                 /*
1324                  * If we weren't given an initial fragment number,
1325                  * use the next expected fragment number as the fragment
1326                  * number for this fragment.
1327                  */
1328                 if (no_frag_number) {
1329                         for (fd = fd_head; fd != NULL; fd = fd->next) {
1330                                 if (fd->next == NULL)
1331                                         frag_number = fd->offset + 1;
1332                         }
1333                 }
1334         }
1335
1336         /*
1337          * If we don't have all the data that is in this fragment,
1338          * then we can't, and don't, do reassembly on it.
1339          *
1340          * If it's the first frame, handle it as an unfragmented packet.
1341          * Otherwise, just handle it as a fragment.
1342          *
1343          * If "more_frags" isn't set, we get rid of the entry in the
1344          * hash table for this reassembly, as we don't need it any more.
1345          */
1346         if (!tvb_bytes_exist(tvb, offset, frag_data_len)) {
1347                 if (!more_frags) {
1348                         /*
1349                          * Remove this from the table of in-progress
1350                          * reassemblies, and free up any memory used for
1351                          * it in that table.
1352                          */
1353                         old_key = orig_key;
1354                         fragment_unhash(fragment_table, old_key);
1355                 }
1356                 return frag_number == 0 ? fd_head : NULL;
1357         }
1358
1359         if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1360                                   frag_number, frag_data_len, more_frags)) {
1361                 /*
1362                  * Reassembly is complete.
1363                  * Remove this from the table of in-progress
1364                  * reassemblies, add it to the table of
1365                  * reassembled packets, and return it.
1366                  */
1367
1368                 /*
1369                  * Remove this from the table of in-progress reassemblies,
1370                  * and free up any memory used for it in that table.
1371                  */
1372                 old_key = orig_key;
1373                 fragment_unhash(fragment_table, old_key);
1374
1375                 /*
1376                  * Add this item to the table of reassembled packets.
1377                  */
1378                 fragment_reassembled(fd_head, pinfo, reassembled_table);
1379                 return fd_head;
1380         } else {
1381                 /*
1382                  * Reassembly isn't complete.
1383                  */
1384                 return NULL;
1385         }
1386 }
1387
1388 fragment_data *
1389 fragment_add_seq_check(tvbuff_t *tvb, int offset, packet_info *pinfo,
1390              guint32 id, GHashTable *fragment_table,
1391              GHashTable *reassembled_table, guint32 frag_number,
1392              guint32 frag_data_len, gboolean more_frags)
1393 {
1394         return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1395             fragment_table, reassembled_table, frag_number, frag_data_len,
1396             more_frags, FALSE);
1397 }
1398
1399 fragment_data *
1400 fragment_add_seq_next(tvbuff_t *tvb, int offset, packet_info *pinfo,
1401              guint32 id, GHashTable *fragment_table,
1402              GHashTable *reassembled_table, guint32 frag_data_len,
1403              gboolean more_frags)
1404 {
1405         return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1406             fragment_table, reassembled_table, 0, frag_data_len,
1407             more_frags, TRUE);
1408 }
1409
1410 /*
1411  * Process reassembled data; if we're on the frame in which the data
1412  * was reassembled, put the fragment information into the protocol
1413  * tree, and construct a tvbuff with the reassembled data, otherwise
1414  * just put a "reassembled in" item into the protocol tree.
1415  */
1416 tvbuff_t *
1417 process_reassembled_data(tvbuff_t *tvb, packet_info *pinfo, char *name,
1418     fragment_data *fd_head, const fragment_items *fit,
1419     gboolean *update_col_infop, proto_tree *tree)
1420 {
1421         tvbuff_t *next_tvb;
1422         gboolean update_col_info;
1423
1424         if (pinfo->fd->num == fd_head->reassembled_in) {
1425                 /*
1426                  * OK, we have the complete reassembled payload.
1427                  * Allocate a new tvbuff, referring to the reassembled
1428                  * payload.
1429                  */
1430                 if (fd_head->flags & FD_BLOCKSEQUENCE) {
1431                         next_tvb = tvb_new_real_data(fd_head->data,
1432                               fd_head->len, fd_head->len);
1433                 } else {
1434                         next_tvb = tvb_new_real_data(fd_head->data,
1435                               fd_head->datalen, fd_head->datalen);
1436                 }
1437
1438                 /*
1439                  * Add the tvbuff to the list of tvbuffs to which
1440                  * the tvbuff we were handed refers, so it'll get
1441                  * cleaned up when that tvbuff is cleaned up.
1442                  */
1443                 tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1444
1445                 /* Add the defragmented data to the data source list. */
1446                 add_new_data_source(pinfo, next_tvb, name);
1447
1448                 /* show all fragments */
1449                 if (fd_head->flags & FD_BLOCKSEQUENCE) {
1450                         update_col_info = !show_fragment_seq_tree(fd_head,
1451                             fit,  tree, pinfo, next_tvb);
1452                 } else {
1453                         update_col_info = !show_fragment_tree(fd_head,
1454                             fit, tree, pinfo, next_tvb);
1455                 }
1456                 if (update_col_infop != NULL)
1457                         *update_col_infop = update_col_info;
1458         } else {
1459                 /* We don't have the complete reassembled payload. */
1460                 next_tvb = NULL;
1461
1462                 /*
1463                  * If there's a field to use for the number of
1464                  * the frame in which the packet was reassembled,
1465                  * add it to the protocol tree.
1466                  */
1467                 if (fit->hf_reassembled_in != NULL) {
1468                         proto_tree_add_uint(tree,
1469                             *(fit->hf_reassembled_in), tvb,
1470                             0, 0, fd_head->reassembled_in);
1471                 }
1472         }
1473         return next_tvb;
1474 }
1475
1476 /*
1477  * Show a single fragment in a fragment subtree.
1478  */
1479 static void
1480 show_fragment(fragment_data *fd, int offset, const fragment_items *fit,
1481     proto_tree *ft, tvbuff_t *tvb)
1482 {
1483         if (fd->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT
1484                 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1485                 /* this fragment has some flags set, create a subtree
1486                  * for it and display the flags.
1487                  */
1488                 proto_tree *fet=NULL;
1489                 proto_item *fei=NULL;
1490                 int hf;
1491
1492                 if (fd->flags & (FD_OVERLAPCONFLICT
1493                         |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1494                         hf = *(fit->hf_fragment_error);
1495                 } else {
1496                         hf = *(fit->hf_fragment);
1497                 }
1498                 fei = proto_tree_add_uint_format(ft, hf,
1499                         tvb, offset, fd->len,
1500                         fd->frame,
1501                         "Frame:%u payload:%u-%u",
1502                         fd->frame,
1503                         offset,
1504                         offset+fd->len-1);
1505                 fet = proto_item_add_subtree(fei, *(fit->ett_fragment));
1506                 if (fd->flags&FD_OVERLAP) {
1507                         proto_tree_add_boolean(fet,
1508                                 *(fit->hf_fragment_overlap),
1509                                 tvb, 0, 0,
1510                                 TRUE);
1511                 }
1512                 if (fd->flags&FD_OVERLAPCONFLICT) {
1513                         proto_tree_add_boolean(fet,
1514                                 *(fit->hf_fragment_overlap_conflict),
1515                                 tvb, 0, 0,
1516                                 TRUE);
1517                 }
1518                 if (fd->flags&FD_MULTIPLETAILS) {
1519                         proto_tree_add_boolean(fet,
1520                                 *(fit->hf_fragment_multiple_tails),
1521                                 tvb, 0, 0,
1522                                 TRUE);
1523                 }
1524                 if (fd->flags&FD_TOOLONGFRAGMENT) {
1525                         proto_tree_add_boolean(fet,
1526                                 *(fit->hf_fragment_too_long_fragment),
1527                                 tvb, 0, 0,
1528                                 TRUE);
1529                 }
1530         } else {
1531                 /* nothing of interest for this fragment */
1532                 proto_tree_add_uint_format(ft, *(fit->hf_fragment),
1533                         tvb, offset, fd->len,
1534                         fd->frame,
1535                         "Frame:%u payload:%u-%u",
1536                         fd->frame,
1537                         offset,
1538                         offset+fd->len-1
1539                 );
1540         }
1541 }
1542
1543 static gboolean
1544 show_fragment_errs_in_col(fragment_data *fd_head, const fragment_items *fit,
1545     packet_info *pinfo)
1546 {
1547         if (fd_head->flags & (FD_OVERLAPCONFLICT
1548                 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1549                 if (check_col(pinfo->cinfo, COL_INFO)) {
1550                         col_add_fstr(pinfo->cinfo, COL_INFO,
1551                                 "[Illegal %s]", fit->tag);
1552                         return TRUE;
1553                 }
1554         }
1555
1556         return FALSE;
1557 }
1558
1559 /* This function will build the fragment subtree; it's for fragments
1560    reassembled with "fragment_add()".
1561
1562    It will return TRUE if there were fragmentation errors
1563    or FALSE if fragmentation was ok.
1564 */
1565 gboolean
1566 show_fragment_tree(fragment_data *fd_head, const fragment_items *fit,
1567     proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb)
1568 {
1569         fragment_data *fd;
1570         proto_tree *ft;
1571         proto_item *fi;
1572
1573         /* It's not fragmented. */
1574         pinfo->fragmented = FALSE;
1575
1576         fi = proto_tree_add_item(tree, *(fit->hf_fragments),
1577             tvb, 0, -1, FALSE);
1578         ft = proto_item_add_subtree(fi, *(fit->ett_fragments));
1579         for (fd = fd_head->next; fd != NULL; fd = fd->next)
1580                 show_fragment(fd, fd->offset, fit, ft, tvb);
1581
1582         return show_fragment_errs_in_col(fd_head, fit, pinfo);
1583 }
1584
1585 /* This function will build the fragment subtree; it's for fragments
1586    reassembled with "fragment_add_seq()" or "fragment_add_seq_check()".
1587
1588    It will return TRUE if there were fragmentation errors
1589    or FALSE if fragmentation was ok.
1590 */
1591 gboolean
1592 show_fragment_seq_tree(fragment_data *fd_head, const fragment_items *fit,
1593     proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb)
1594 {
1595         guint32 offset, next_offset;
1596         fragment_data *fd, *last_fd;
1597         proto_tree *ft;
1598         proto_item *fi;
1599
1600         /* It's not fragmented. */
1601         pinfo->fragmented = FALSE;
1602
1603         fi = proto_tree_add_item(tree, *(fit->hf_fragments),
1604             tvb, 0, -1, FALSE);
1605         ft = proto_item_add_subtree(fi, *(fit->ett_fragments));
1606         offset = 0;
1607         next_offset = 0;
1608         last_fd = NULL;
1609         for (fd = fd_head->next; fd != NULL; fd = fd->next){
1610                 if (last_fd == NULL || last_fd->offset != fd->offset) {
1611                         offset = next_offset;
1612                         next_offset += fd->len;
1613                 }
1614                 last_fd = fd;
1615                 show_fragment(fd, offset, fit, ft, tvb);
1616         }
1617
1618         return show_fragment_errs_in_col(fd_head, fit, pinfo);
1619 }