Put
[metze/wireshark/wip.git] / epan / reassemble.c
1 /* reassemble.c
2  * Routines for {fragment,segment} reassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 <epan/reassemble.h>
34
35 #include <epan/emem.h>
36
37 #include <epan/dissectors/packet-dcerpc.h>
38
39 typedef struct _fragment_key {
40         address src;
41         address dst;
42         guint32 id;
43 } fragment_key;
44
45 typedef struct _dcerpc_fragment_key {
46         address src;
47         address dst;
48         guint32 id;
49         e_uuid_t act_id;
50 } dcerpc_fragment_key;
51
52 static GMemChunk *fragment_key_chunk = NULL;
53 static GMemChunk *fragment_data_chunk = NULL;
54 static int fragment_init_count = 200;
55
56 #define LINK_FRAG(fd_head,fd)                                   \
57         {       fragment_data *fd_i;                            \
58                 /* add fragment to list, keep list sorted */            \
59                 for(fd_i=(fd_head);fd_i->next;fd_i=fd_i->next){ \
60                         if( ((fd)->offset) < (fd_i->next->offset) )     \
61                                 break;                                  \
62                 }                                                       \
63                 (fd)->next=fd_i->next;                          \
64                 fd_i->next=(fd);                                        \
65         }
66
67 static gint
68 fragment_equal(gconstpointer k1, gconstpointer k2)
69 {
70         const fragment_key* key1 = (const fragment_key*) k1;
71         const fragment_key* key2 = (const fragment_key*) k2;
72
73         /*key.id is the first item to compare since item is most
74           likely to differ between sessions, thus shortcircuiting
75           the comparasion of addresses.
76         */
77         return ( ( (key1->id    == key2->id) &&
78                    (ADDRESSES_EQUAL(&key1->src, &key2->src)) &&
79                    (ADDRESSES_EQUAL(&key1->dst, &key2->dst))
80                  ) ?
81                  TRUE : FALSE);
82 }
83
84 static guint
85 fragment_hash(gconstpointer k)
86 {
87         const fragment_key* key = (const fragment_key*) k;
88         guint hash_val;
89 /*
90         int i;
91 */
92
93         hash_val = 0;
94
95 /*      More than likely: in most captures src and dst addresses are the
96         same, and would hash the same.
97         We only use id as the hash as an optimization.
98
99         for (i = 0; i < key->src.len; i++)
100                 hash_val += key->src.data[i];
101         for (i = 0; i < key->dst.len; i++)
102                 hash_val += key->dst.data[i];
103 */
104
105         hash_val += key->id;
106
107         return hash_val;
108 }
109
110 static gint
111 dcerpc_fragment_equal(gconstpointer k1, gconstpointer k2)
112 {
113         const dcerpc_fragment_key* key1 = (const dcerpc_fragment_key*) k1;
114         const dcerpc_fragment_key* key2 = (const dcerpc_fragment_key*) k2;
115
116         /*key.id is the first item to compare since item is most
117           likely to differ between sessions, thus shortcircuiting
118           the comparasion of addresses.
119         */
120         return (((key1->id == key2->id)
121               && (ADDRESSES_EQUAL(&key1->src, &key2->src))
122               && (ADDRESSES_EQUAL(&key1->dst, &key2->dst))
123               && (memcmp (&key1->act_id, &key2->act_id, sizeof (e_uuid_t)) == 0))
124              ? TRUE : FALSE);
125 }
126
127 static guint
128 dcerpc_fragment_hash(gconstpointer k)
129 {
130         const dcerpc_fragment_key* key = (const dcerpc_fragment_key*) k;
131         guint hash_val;
132
133         hash_val = 0;
134
135         hash_val += key->id;
136         hash_val += key->act_id.Data1;
137         hash_val += key->act_id.Data2 << 16;
138         hash_val += key->act_id.Data3;
139
140         return hash_val;
141 }
142
143 typedef struct _reassembled_key {
144         guint32 id;
145         guint32 frame;
146 } reassembled_key;
147
148 static gint
149 reassembled_equal(gconstpointer k1, gconstpointer k2)
150 {
151         const reassembled_key* key1 = (const reassembled_key*) k1;
152         const reassembled_key* key2 = (const reassembled_key*) k2;
153
154         /*
155          * We assume that the frame numbers are unlikely to be equal,
156          * so we check them first.
157          */
158         return key1->frame == key2->frame && key1->id == key2->id;
159 }
160
161 static guint
162 reassembled_hash(gconstpointer k)
163 {
164         const reassembled_key* key = (const reassembled_key*) k;
165
166         return key->frame;
167 }
168
169 /*
170  * For a fragment hash table entry, free the address data to which the key
171  * refers and the fragment data to which the value refers.
172  * (The actual key and value structures get freed by "reassemble_init()".)
173  */
174 static gboolean
175 free_all_fragments(gpointer key_arg, gpointer value, gpointer user_data _U_)
176 {
177         fragment_key *key = key_arg;
178         fragment_data *fd_head;
179
180         /*
181          * Grr.  I guess the theory here is that freeing
182          * something sure as heck modifies it, so you
183          * want to ban attempts to free it, but, alas,
184          * if we make the "data" field of an "address"
185          * structure not a "const", the compiler whines if
186          * we try to make it point into the data for a packet,
187          * as that's a "const" array (and should be, as dissectors
188          * shouldn't trash it).
189          *
190          * So we cast the complaint into oblivion, and rely on
191          * the fact that these addresses are known to have had
192          * their data mallocated, i.e. they don't point into,
193          * say, the middle of the data for a packet.
194          */
195         g_free((gpointer)key->src.data);
196         g_free((gpointer)key->dst.data);
197
198         for (fd_head = value; fd_head != NULL; fd_head = fd_head->next) {
199                 if(fd_head->data && !(fd_head->flags&FD_NOT_MALLOCED))
200                         g_free(fd_head->data);
201         }
202
203         return TRUE;
204 }
205
206 /*
207  * For a reassembled-packet hash table entry, free the fragment data
208  * to which the value refers.
209  * (The actual value structures get freed by "reassemble_init()".)
210  */
211 static gboolean
212 free_all_reassembled_fragments(gpointer key_arg _U_, gpointer value,
213                                gpointer user_data _U_)
214 {
215         fragment_data *fd_head;
216
217         for (fd_head = value; fd_head != NULL; fd_head = fd_head->next) {
218                 if(fd_head->data && !(fd_head->flags&FD_NOT_MALLOCED)) {
219                         g_free(fd_head->data);
220
221                         /*
222                          * A reassembled packet is inserted into the
223                          * hash table once for every frame that made
224                          * up the reassembled packet; clear the data
225                          * pointer so that we only free the data the
226                          * first time we see it.
227                          */
228                         fd_head->data = NULL;
229                 }
230         }
231
232         return TRUE;
233 }
234
235 /*
236  * Initialize a fragment table.
237  */
238 void
239 fragment_table_init(GHashTable **fragment_table)
240 {
241         if (*fragment_table != NULL) {
242                 /*
243                  * The fragment hash table exists.
244                  *
245                  * Remove all entries and free fragment data for
246                  * each entry.  (The key and value data is freed
247                  * by "reassemble_init()".)
248                  */
249                 g_hash_table_foreach_remove(*fragment_table,
250                                 free_all_fragments, NULL);
251         } else {
252                 /* The fragment table does not exist. Create it */
253                 *fragment_table = g_hash_table_new(fragment_hash,
254                                 fragment_equal);
255         }
256 }
257
258 void
259 dcerpc_fragment_table_init(GHashTable **fragment_table)
260 {
261        if (*fragment_table != NULL) {
262                /*
263                 * The fragment hash table exists.
264                 *
265                 * Remove all entries and free fragment data for
266                 * each entry.  (The key and value data is freed
267                 * by "reassemble_init()".)
268                 */
269                g_hash_table_foreach_remove(*fragment_table,
270                                free_all_fragments, NULL);
271        } else {
272                /* The fragment table does not exist. Create it */
273                *fragment_table = g_hash_table_new(dcerpc_fragment_hash,
274                                dcerpc_fragment_equal);
275        }
276 }
277
278 /*
279  * Initialize a reassembled-packet table.
280  */
281 void
282 reassembled_table_init(GHashTable **reassembled_table)
283 {
284         if (*reassembled_table != NULL) {
285                 /*
286                  * The reassembled-packet hash table exists.
287                  *
288                  * Remove all entries and free reassembled packet
289                  * data for each entry.  (The key data is freed
290                  * by "reassemble_init()".)
291                  */
292                 g_hash_table_foreach_remove(*reassembled_table,
293                                 free_all_reassembled_fragments, NULL);
294         } else {
295                 /* The fragment table does not exist. Create it */
296                 *reassembled_table = g_hash_table_new(reassembled_hash,
297                                 reassembled_equal);
298         }
299 }
300
301 /*
302  * Free up all space allocated for fragment keys and data and
303  * reassembled keys.
304  */
305 void
306 reassemble_init(void)
307 {
308         if (fragment_key_chunk != NULL)
309                 g_mem_chunk_destroy(fragment_key_chunk);
310         if (fragment_data_chunk != NULL)
311                 g_mem_chunk_destroy(fragment_data_chunk);
312         fragment_key_chunk = g_mem_chunk_new("fragment_key_chunk",
313             sizeof(fragment_key),
314             fragment_init_count * sizeof(fragment_key),
315             G_ALLOC_AND_FREE);
316         fragment_data_chunk = g_mem_chunk_new("fragment_data_chunk",
317             sizeof(fragment_data),
318             fragment_init_count * sizeof(fragment_data),
319             G_ALLOC_ONLY);
320 }
321
322 /* This function cleans up the stored state and removes the reassembly data and
323  * (with one exception) all allocated memory for matching reassembly.
324  *
325  * The exception is :
326  * If the PDU was already completely reassembled, then the buffer containing the
327  * reassembled data WILL NOT be free()d, and the pointer to that buffer will be
328  * returned.
329  * Othervise the function will return NULL.
330  *
331  * So, if you call fragment_delete and it returns non-NULL, YOU are responsible to
332  * g_free() that buffer.
333  */
334 unsigned char *
335 fragment_delete(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
336 {
337         fragment_data *fd_head, *fd;
338         fragment_key key;
339         unsigned char *data=NULL;
340
341         /* create key to search hash with */
342         key.src = pinfo->src;
343         key.dst = pinfo->dst;
344         key.id  = id;
345
346         fd_head = g_hash_table_lookup(fragment_table, &key);
347
348         if(fd_head==NULL){
349                 /* We do not recognize this as a PDU we have seen before. return*/
350                 return NULL;
351         }
352
353         data=fd_head->data;
354         /* loop over all partial fragments and free any buffers */
355         for(fd=fd_head->next;fd;){
356                 fragment_data *tmp_fd;
357                 tmp_fd=fd->next;
358
359                 if( !(fd->flags&FD_NOT_MALLOCED) )
360                         g_free(fd->data);
361                 g_mem_chunk_free(fragment_data_chunk, fd);
362                 fd=tmp_fd;
363         }
364         g_mem_chunk_free(fragment_data_chunk, fd_head);
365         g_hash_table_remove(fragment_table, &key);
366
367         return data;
368 }
369
370 /* This function is used to check if there is partial or completed reassembly state
371  * matching this packet. I.e. Are there reassembly going on or not for this packet?
372  */
373 fragment_data *
374 fragment_get(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
375 {
376         fragment_data *fd_head;
377         fragment_key key;
378
379         /* create key to search hash with */
380         key.src = pinfo->src;
381         key.dst = pinfo->dst;
382         key.id  = id;
383
384         fd_head = g_hash_table_lookup(fragment_table, &key);
385
386         return fd_head;
387 }
388
389 /* id *must* be the frame number for this to work! */
390 fragment_data *
391 fragment_get_reassembled(packet_info *pinfo, guint32 id, GHashTable *reassembled_table)
392 {
393         fragment_data *fd_head;
394         reassembled_key key;
395
396         /* create key to search hash with */
397         key.frame = id;
398         key.id = id;
399         fd_head = g_hash_table_lookup(reassembled_table, &key);
400
401         return fd_head;
402 }
403
404 fragment_data *
405 fragment_get_reassembled_id(packet_info *pinfo, guint32 id, GHashTable *reassembled_table)
406 {
407         fragment_data *fd_head;
408         reassembled_key key;
409
410         /* create key to search hash with */
411         key.frame = pinfo->fd->num;
412         key.id = id;
413         fd_head = g_hash_table_lookup(reassembled_table, &key);
414
415         return fd_head;
416 }
417
418 /* This function can be used to explicitely set the total length (if known)
419  * for reassembly of a PDU.
420  * This is useful for reassembly of PDUs where one may have the total length specified
421  * in the first fragment instead of as for, say, IPv4 where a flag indicates which
422  * is the last fragment.
423  *
424  * Such protocols might fragment_add with a more_frags==TRUE for every fragment
425  * and just tell the reassembly engine the expected total length of the reassembled data
426  * using fragment_set_tot_len immediately after doing fragment_add for the first packet.
427  *
428  * note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment.
429  * i.e. since the block numbers start at 0, if we specify tot_len==2, that
430  * actually means we want to defragment 3 blocks, block 0, 1 and 2.
431  */
432 void
433 fragment_set_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table,
434                      guint32 tot_len)
435 {
436         fragment_data *fd_head;
437         fragment_key key;
438
439         /* create key to search hash with */
440         key.src = pinfo->src;
441         key.dst = pinfo->dst;
442         key.id  = id;
443
444         fd_head = g_hash_table_lookup(fragment_table, &key);
445
446         if(fd_head){
447                 fd_head->datalen = tot_len;
448         }
449
450         return;
451 }
452
453 guint32
454 fragment_get_tot_len(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
455 {
456         fragment_data *fd_head;
457         fragment_key key;
458
459         /* create key to search hash with */
460         key.src = pinfo->src;
461         key.dst = pinfo->dst;
462         key.id  = id;
463
464         fd_head = g_hash_table_lookup(fragment_table, &key);
465
466         if(fd_head){
467                 return fd_head->datalen;
468         }
469
470         return 0;
471 }
472
473
474 /* This function will set the partial reassembly flag for a fh.
475    When this function is called, the fh MUST already exist, i.e.
476    the fh MUST be created by the initial call to fragment_add() before
477    this function is called.
478    Also note that this function MUST be called to indicate a fh will be
479    extended (increase the already stored data)
480 */
481
482 void
483 fragment_set_partial_reassembly(packet_info *pinfo, guint32 id, GHashTable *fragment_table)
484 {
485         fragment_data *fd_head;
486         fragment_key key;
487
488         /* create key to search hash with */
489         key.src = pinfo->src;
490         key.dst = pinfo->dst;
491         key.id  = id;
492
493         fd_head = g_hash_table_lookup(fragment_table, &key);
494
495         /*
496          * XXX - why not do all the stuff done early in "fragment_add_work()",
497          * turning off FD_DEFRAGMENTED and pointing the fragments' data
498          * pointers to the appropriate part of the already-reassembled
499          * data, and clearing the data length and "reassembled in" frame
500          * number, here?  We currently have a hack in the TCP dissector
501          * not to set the "reassembled in" value if the "partial reassembly"
502          * flag is set, so that in the first pass through the packets
503          * we don't falsely set a packet as reassembled in that packet
504          * if the dissector decided that even more reassembly was needed.
505          */
506         if(fd_head){
507                 fd_head->flags |= FD_PARTIAL_REASSEMBLY;
508         }
509 }
510
511 /*
512  * This function gets rid of an entry from a fragment table, given
513  * a pointer to the key for that entry; it also frees up the key
514  * and the addresses in it.
515  */
516 static void
517 fragment_unhash(GHashTable *fragment_table, fragment_key *key)
518 {
519         /*
520          * Remove the entry from the fragment table.
521          */
522         g_hash_table_remove(fragment_table, key);
523
524         /*
525          * Free up the copies of the addresses from the old key.
526          */
527         g_free((gpointer)key->src.data);
528         g_free((gpointer)key->dst.data);
529
530         /*
531          * Free the key itself.
532          */
533         g_mem_chunk_free(fragment_key_chunk, key);
534 }
535
536 /*
537  * This function adds fragment_data structure to a reassembled-packet
538  * hash table, using the frame numbers of each of the frames from
539  * which it was reassembled as keys, and sets the "reassembled_in"
540  * frame number.
541  */
542 static void
543 fragment_reassembled(fragment_data *fd_head, packet_info *pinfo,
544              GHashTable *reassembled_table, guint32 id)
545 {
546         reassembled_key *new_key;
547         fragment_data *fd;
548
549         if (fd_head->next == NULL) {
550                 /*
551                  * This was not fragmented, so there's no fragment
552                  * table; just hash it using the current frame number.
553                  */
554                 new_key = se_alloc(sizeof(reassembled_key));
555                 new_key->frame = pinfo->fd->num;
556                 new_key->id = id;
557                 g_hash_table_insert(reassembled_table, new_key, fd_head);
558         } else {
559                 /*
560                  * Hash it with the frame numbers for all the frames.
561                  */
562                 for (fd = fd_head->next; fd != NULL; fd = fd->next){
563                         new_key = se_alloc(sizeof(reassembled_key));
564                         new_key->frame = fd->frame;
565                         new_key->id = id;
566                         g_hash_table_insert(reassembled_table, new_key,
567                             fd_head);
568                 }
569         }
570         fd_head->flags |= FD_DEFRAGMENTED;
571         fd_head->reassembled_in = pinfo->fd->num;
572 }
573
574 /*
575  * This function adds a new fragment to the fragment hash table.
576  * If this is the first fragment seen for this datagram, a new entry
577  * is created in the hash table, otherwise this fragment is just added
578  * to the linked list of fragments for this packet.
579  * The list of fragments for a specific datagram is kept sorted for
580  * easier handling.
581  *
582  * Returns a pointer to the head of the fragment data list if we have all the
583  * fragments, NULL otherwise.
584  *
585  * This function assumes frag_offset being a byte offset into the defragment
586  * packet.
587  *
588  * 01-2002
589  * Once the fh is defragmented (= FD_DEFRAGMENTED set), it can be
590  * extended using the FD_PARTIAL_REASSEMBLY flag. This flag should be set
591  * using fragment_set_partial_reassembly() before calling fragment_add
592  * with the new fragment. FD_TOOLONGFRAGMENT and FD_MULTIPLETAILS flags
593  * are lowered when a new extension process is started.
594  */
595 static gboolean
596 fragment_add_work(fragment_data *fd_head, tvbuff_t *tvb, int offset,
597              packet_info *pinfo, guint32 frag_offset,
598              guint32 frag_data_len, gboolean more_frags)
599 {
600         fragment_data *fd;
601         fragment_data *fd_i;
602         guint32 max, dfpos;
603         unsigned char *old_data;
604
605         /* create new fd describing this fragment */
606         fd = g_mem_chunk_alloc(fragment_data_chunk);
607         fd->next = NULL;
608         fd->flags = 0;
609         fd->frame = pinfo->fd->num;
610         fd->offset = frag_offset;
611         fd->len  = frag_data_len;
612         fd->data = NULL;
613
614         /*
615          * If it was already defragmented and this new fragment goes beyond
616          * data limits, set flag in already empty fds & point old fds to malloc'ed data.
617          */
618         if(fd_head->flags & FD_DEFRAGMENTED && (frag_offset+frag_data_len) >= fd_head->datalen &&
619                 fd_head->flags & FD_PARTIAL_REASSEMBLY){
620                 for(fd_i=fd_head->next; fd_i; fd_i=fd_i->next){
621                         if( !fd_i->data ) {
622                                 fd_i->data = fd_head->data + fd_i->offset;
623                                 fd_i->flags |= FD_NOT_MALLOCED;
624                         }
625                         fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
626                 }
627                 fd_head->flags ^= FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY;
628                 fd_head->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
629                 fd_head->datalen=0;
630                 fd_head->reassembled_in=0;
631         }
632
633         if (!more_frags) {
634                 /*
635                  * This is the tail fragment in the sequence.
636                  */
637                 if (fd_head->datalen) {
638                         /* ok we have already seen other tails for this packet
639                          * it might be a duplicate.
640                          */
641                         if (fd_head->datalen != (fd->offset + fd->len) ){
642                                 /* Oops, this tail indicates a different packet
643                                  * len than the previous ones. Somethings wrong
644                                  */
645                                 fd->flags      |= FD_MULTIPLETAILS;
646                                 fd_head->flags |= FD_MULTIPLETAILS;
647                         }
648                 } else {
649                         /* this was the first tail fragment, now we know the
650                          * length of the packet
651                          */
652                         fd_head->datalen = fd->offset + fd->len;
653                 }
654         }
655
656
657
658
659         /* If the packet is already defragmented, this MUST be an overlap.
660          * The entire defragmented packet is in fd_head->data
661          * Even if we have previously defragmented this packet, we still check
662          * check it. Someone might play overlap and TTL games.
663          */
664         if (fd_head->flags & FD_DEFRAGMENTED) {
665                 fd->flags      |= FD_OVERLAP;
666                 fd_head->flags |= FD_OVERLAP;
667                 /* make sure its not too long */
668                 if (fd->offset + fd->len > fd_head->datalen) {
669                         fd->flags      |= FD_TOOLONGFRAGMENT;
670                         fd_head->flags |= FD_TOOLONGFRAGMENT;
671                         LINK_FRAG(fd_head,fd);
672                         return TRUE;
673                 }
674                 /* make sure it doesnt conflict with previous data */
675                 if ( memcmp(fd_head->data+fd->offset,
676                         tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
677                         fd->flags      |= FD_OVERLAPCONFLICT;
678                         fd_head->flags |= FD_OVERLAPCONFLICT;
679                         LINK_FRAG(fd_head,fd);
680                         return TRUE;
681                 }
682                 /* it was just an overlap, link it and return */
683                 LINK_FRAG(fd_head,fd);
684                 return TRUE;
685         }
686
687
688
689         /* If we have reached this point, the packet is not defragmented yet.
690          * Save all payload in a buffer until we can defragment.
691          * XXX - what if we didn't capture the entire fragment due
692          * to a too-short snapshot length?
693          */
694         fd->data = g_malloc(fd->len);
695         tvb_memcpy(tvb, fd->data, offset, fd->len);
696         LINK_FRAG(fd_head,fd);
697
698
699         if( !(fd_head->datalen) ){
700                 /* if we dont know the datalen, there are still missing
701                  * packets. Cheaper than the check below.
702                  */
703                 return FALSE;
704         }
705
706
707         /*
708          * Check if we have received the entire fragment.
709          * This is easy since the list is sorted and the head is faked.
710          *
711          * First, we compute the amount of contiguous data that's
712          * available.  (The check for fd_i->offset <= max rules out
713          * fragments that don't start before or at the end of the
714          * previous fragment, i.e. fragments that have a gap between
715          * them and the previous fragment.)
716          */
717         max = 0;
718         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
719                 if ( ((fd_i->offset)<=max) &&
720                     ((fd_i->offset+fd_i->len)>max) ){
721                         max = fd_i->offset+fd_i->len;
722                 }
723         }
724
725         if (max < (fd_head->datalen)) {
726                 /*
727                  * The amount of contiguous data we have is less than the
728                  * amount of data we're trying to reassemble, so we haven't
729                  * received all packets yet.
730                  */
731                 return FALSE;
732         }
733
734
735         if (max > (fd_head->datalen)) {
736                 /*XXX not sure if current fd was the TOOLONG*/
737                 /*XXX is it fair to flag current fd*/
738                 /* oops, too long fragment detected */
739                 fd->flags      |= FD_TOOLONGFRAGMENT;
740                 fd_head->flags |= FD_TOOLONGFRAGMENT;
741         }
742
743         /* we have received an entire packet, defragment it and
744          * free all fragments
745          */
746         /* store old data just in case */
747         old_data=fd_head->data;
748         fd_head->data = g_malloc(max);
749
750         /* add all data fragments */
751         for (dfpos=0,fd_i=fd_head;fd_i;fd_i=fd_i->next) {
752                 if (fd_i->len) {
753                         if (fd_i->offset < dfpos) {
754                                 fd_i->flags    |= FD_OVERLAP;
755                                 fd_head->flags |= FD_OVERLAP;
756                                 if ( memcmp(fd_head->data+fd_i->offset,
757                                         fd_i->data,
758                                         MIN(fd_i->len,(dfpos-fd_i->offset))
759                                         ) ){
760                                         fd_i->flags    |= FD_OVERLAPCONFLICT;
761                                         fd_head->flags |= FD_OVERLAPCONFLICT;
762                                 }
763                         }
764                         /* dfpos is always >= than fd_i->offset */
765                         /* No gaps can exist here, max_loop(above) does this */
766                         /* XXX - true? Can we get fd_i->offset+fd-i->len */
767                         /* overflowing, for example? */
768                         if( fd_i->offset+fd_i->len > dfpos ) {
769                                 if (fd_i->offset+fd_i->len > max)
770                                         g_warning("Reassemble error in frame %u: offset %u + len %u > max %u",
771                                             pinfo->fd->num, fd_i->offset,
772                                             fd_i->len, max);
773                                 else if (dfpos < fd_i->offset)
774                                         g_warning("Reassemble error in frame %u: dfpos %u < offset %u",
775                                             pinfo->fd->num, dfpos, fd_i->offset);
776                                 else if (dfpos-fd_i->offset > fd_i->len)
777                                         g_warning("Reassemble error in frame %u: dfpos %u - offset %u > len %u",
778                                             pinfo->fd->num, dfpos, fd_i->offset,
779                                             fd_i->len);
780                                 else
781                                         memcpy(fd_head->data+dfpos,
782                                             fd_i->data+(dfpos-fd_i->offset),
783                                             fd_i->len-(dfpos-fd_i->offset));
784                         } else {
785                                 if (fd_i->offset+fd_i->len < fd_i->offset)
786                                         g_warning("Reassemble error in frame %u: offset %u + len %u < offset",
787                                             pinfo->fd->num, fd_i->offset,
788                                             fd_i->len);
789                         }
790                         if( fd_i->flags & FD_NOT_MALLOCED )
791                                 fd_i->flags &= ~FD_NOT_MALLOCED;
792                         else
793                                 g_free(fd_i->data);
794                         fd_i->data=NULL;
795
796                         dfpos=MAX(dfpos,(fd_i->offset+fd_i->len));
797                 }
798         }
799
800         if( old_data )
801                 g_free(old_data);
802         /* mark this packet as defragmented.
803            allows us to skip any trailing fragments */
804         fd_head->flags |= FD_DEFRAGMENTED;
805         fd_head->reassembled_in=pinfo->fd->num;
806
807         return TRUE;
808 }
809
810 static fragment_data *
811 fragment_add_common(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
812              GHashTable *fragment_table, guint32 frag_offset,
813              guint32 frag_data_len, gboolean more_frags,
814              gboolean check_already_added)
815 {
816         fragment_key key, *new_key;
817         fragment_data *fd_head;
818         fragment_data *fd_item;
819         gboolean already_added=pinfo->fd->flags.visited;
820
821
822         /* dissector shouldn't give us garbage tvb info */
823         DISSECTOR_ASSERT(tvb_bytes_exist(tvb, offset, frag_data_len));
824
825         /* create key to search hash with */
826         key.src = pinfo->src;
827         key.dst = pinfo->dst;
828         key.id  = id;
829
830         fd_head = g_hash_table_lookup(fragment_table, &key);
831
832 #if 0
833         /* debug output of associated fragments. */
834         /* leave it here for future debugging sessions */
835         if(strcmp(pinfo->current_proto, "DCERPC") == 0) {
836                 printf("proto:%s num:%u id:%u offset:%u len:%u more:%u visited:%u\n", 
837                         pinfo->current_proto, pinfo->fd->num, id, frag_offset, frag_data_len, more_frags, pinfo->fd->flags.visited);
838                 if(fd_head != NULL) {
839                         for(fd_item=fd_head->next;fd_item;fd_item=fd_item->next){
840                                 printf("fd_frame:%u fd_offset:%u len:%u datalen:%u\n", 
841                                         fd_item->frame, fd_item->offset, fd_item->len, fd_item->datalen);
842                         }
843                 }
844         }
845 #endif
846
847         /*
848          * "already_added" is true if "pinfo->fd->flags.visited" is true;
849          * if "pinfo->fd->flags.visited", this isn't the first pass, so
850          * we've already done all the reassembly and added all the
851          * fragments.
852          *
853          * If it's not true, but "check_already_added" is true, just check
854          * if we have seen this fragment before, i.e., if we have already
855          * added it to reassembly.
856          * That can be true even if "pinfo->fd->flags.visited" is false
857          * since we sometimes might call a subdissector multiple times.
858          * As an additional check, just make sure we have not already added 
859          * this frame to the reassembly list, if there is a reassembly list;
860          * note that the first item in the reassembly list is not a
861          * fragment, it's a data structure for the reassembled packet.
862          * We don't check it because its "frame" member isn't initialized
863          * to anything, and because it doesn't count in any case.
864          *
865          * And as another additional check, make sure the fragment offsets are
866          * the same, as otherwise we get into trouble if multiple fragments
867          * are in one PDU.
868          */
869         if (!already_added && check_already_added && fd_head != NULL) {
870                 for(fd_item=fd_head->next;fd_item;fd_item=fd_item->next){
871                         if(pinfo->fd->num==fd_item->frame && frag_offset==fd_item->offset){
872                                 already_added=TRUE;
873                         }
874                 }
875         }
876         /* have we already added this frame ?*/
877         if (already_added) {
878                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
879                         return fd_head;
880                 } else {
881                         return NULL;
882                 }
883         }
884
885         if (fd_head==NULL){
886                 /* not found, this must be the first snooped fragment for this
887                  * packet. Create list-head.
888                  */
889                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
890
891                 /* head/first structure in list only holds no other data than
892                  * 'datalen' then we don't have to change the head of the list
893                  * even if we want to keep it sorted
894                  */
895                 fd_head->next=NULL;
896                 fd_head->datalen=0;
897                 fd_head->offset=0;
898                 fd_head->len=0;
899                 fd_head->flags=0;
900                 fd_head->data=NULL;
901                 fd_head->reassembled_in=0;
902
903                 /*
904                  * We're going to use the key to insert the fragment,
905                  * so allocate a structure for it, and copy the
906                  * addresses, allocating new buffers for the address
907                  * data.
908                  */
909                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
910                 COPY_ADDRESS(&new_key->src, &key.src);
911                 COPY_ADDRESS(&new_key->dst, &key.dst);
912                 new_key->id = key.id;
913                 g_hash_table_insert(fragment_table, new_key, fd_head);
914         }
915
916         if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
917             frag_data_len, more_frags)) {
918                 /*
919                  * Reassembly is complete.
920                  */
921                 return fd_head;
922         } else {
923                 /*
924                  * Reassembly isn't complete.
925                  */
926                 return NULL;
927         }
928 }
929
930 fragment_data *
931 fragment_add(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
932              GHashTable *fragment_table, guint32 frag_offset,
933              guint32 frag_data_len, gboolean more_frags)
934 {
935         return fragment_add_common(tvb, offset, pinfo, id, fragment_table,
936             frag_offset, frag_data_len, more_frags, TRUE);
937 }
938
939 /*
940  * For use when you can have multiple fragments in the same frame added
941  * to the same reassembled PDU, e.g. with ONC RPC-over-TCP.
942  */
943 fragment_data *
944 fragment_add_multiple_ok(tvbuff_t *tvb, int offset, packet_info *pinfo,
945                          guint32 id, GHashTable *fragment_table,
946                          guint32 frag_offset, guint32 frag_data_len,
947                          gboolean more_frags)
948 {
949         return fragment_add_common(tvb, offset, pinfo, id, fragment_table,
950             frag_offset, frag_data_len, more_frags, FALSE);
951 }
952
953 fragment_data *
954 fragment_add_check(tvbuff_t *tvb, int offset, packet_info *pinfo,
955              guint32 id, GHashTable *fragment_table,
956              GHashTable *reassembled_table, guint32 frag_offset,
957              guint32 frag_data_len, gboolean more_frags)
958 {
959         reassembled_key reass_key;
960         fragment_key key, *new_key, *old_key;
961         gpointer orig_key, value;
962         fragment_data *fd_head;
963
964         /*
965          * If this isn't the first pass, look for this frame in the table
966          * of reassembled packets.
967          */
968         if (pinfo->fd->flags.visited) {
969                 reass_key.frame = pinfo->fd->num;
970                 reass_key.id = id;
971                 return g_hash_table_lookup(reassembled_table, &reass_key);
972         }
973
974         /* create key to search hash with */
975         key.src = pinfo->src;
976         key.dst = pinfo->dst;
977         key.id  = id;
978
979         if (!g_hash_table_lookup_extended(fragment_table, &key,
980                                           &orig_key, &value)) {
981                 /* not found, this must be the first snooped fragment for this
982                  * packet. Create list-head.
983                  */
984                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
985
986                 /* head/first structure in list only holds no other data than
987                  * 'datalen' then we don't have to change the head of the list
988                  * even if we want to keep it sorted
989                  */
990                 fd_head->next=NULL;
991                 fd_head->datalen=0;
992                 fd_head->offset=0;
993                 fd_head->len=0;
994                 fd_head->flags=0;
995                 fd_head->data=NULL;
996                 fd_head->reassembled_in=0;
997
998                 /*
999                  * We're going to use the key to insert the fragment,
1000                  * so allocate a structure for it, and copy the
1001                  * addresses, allocating new buffers for the address
1002                  * data.
1003                  */
1004                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
1005                 COPY_ADDRESS(&new_key->src, &key.src);
1006                 COPY_ADDRESS(&new_key->dst, &key.dst);
1007                 new_key->id = key.id;
1008                 g_hash_table_insert(fragment_table, new_key, fd_head);
1009
1010                 orig_key = new_key;     /* for unhashing it later */
1011         } else {
1012                 /*
1013                  * We found it.
1014                  */
1015                 fd_head = value;
1016         }
1017
1018         /*
1019          * If this is a short frame, then we can't, and don't, do
1020          * reassembly on it.  We just give up.
1021          */
1022         if (tvb_reported_length(tvb) > tvb_length(tvb))
1023                 return NULL;
1024
1025         if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
1026             frag_data_len, more_frags)) {
1027                 /*
1028                  * Reassembly is complete.
1029                  * Remove this from the table of in-progress
1030                  * reassemblies, add it to the table of
1031                  * reassembled packets, and return it.
1032                  */
1033
1034                 /*
1035                  * Remove this from the table of in-progress reassemblies,
1036                  * and free up any memory used for it in that table.
1037                  */
1038                 old_key = orig_key;
1039                 fragment_unhash(fragment_table, old_key);
1040
1041                 /*
1042                  * Add this item to the table of reassembled packets.
1043                  */
1044                 fragment_reassembled(fd_head, pinfo, reassembled_table, id);
1045                 return fd_head;
1046         } else {
1047                 /*
1048                  * Reassembly isn't complete.
1049                  */
1050                 return NULL;
1051         }
1052 }
1053
1054 /*
1055  * This function adds a new fragment to the entry for a reassembly
1056  * operation.
1057  *
1058  * The list of fragments for a specific datagram is kept sorted for
1059  * easier handling.
1060  *
1061  * Returns TRUE if we have all the fragments, FALSE otherwise.
1062  *
1063  * This function assumes frag_number being a block sequence number.
1064  * The bsn for the first block is 0.
1065  */
1066 static gboolean
1067 fragment_add_seq_work(fragment_data *fd_head, tvbuff_t *tvb, int offset,
1068              packet_info *pinfo, guint32 frag_number,
1069              guint32 frag_data_len, gboolean more_frags)
1070 {
1071         fragment_data *fd;
1072         fragment_data *fd_i;
1073         fragment_data *last_fd;
1074         guint32 max, dfpos, size;
1075
1076         /* create new fd describing this fragment */
1077         fd = g_mem_chunk_alloc(fragment_data_chunk);
1078         fd->next = NULL;
1079         fd->flags = 0;
1080         fd->frame = pinfo->fd->num;
1081         fd->offset = frag_number;
1082         fd->len  = frag_data_len;
1083         fd->data = NULL;
1084
1085         if (!more_frags) {
1086                 /*
1087                  * This is the tail fragment in the sequence.
1088                  */
1089                 if (fd_head->datalen) {
1090                         /* ok we have already seen other tails for this packet
1091                          * it might be a duplicate.
1092                          */
1093                         if (fd_head->datalen != fd->offset ){
1094                                 /* Oops, this tail indicates a different packet
1095                                  * len than the previous ones. Somethings wrong
1096                                  */
1097                                 fd->flags      |= FD_MULTIPLETAILS;
1098                                 fd_head->flags |= FD_MULTIPLETAILS;
1099                         }
1100                 } else {
1101                         /* this was the first tail fragment, now we know the
1102                          * sequence number of that fragment (which is NOT
1103                          * the length of the packet!)
1104                          */
1105                         fd_head->datalen = fd->offset;
1106                 }
1107         }
1108
1109         /* If the packet is already defragmented, this MUST be an overlap.
1110          * The entire defragmented packet is in fd_head->data
1111          * Even if we have previously defragmented this packet, we still check
1112          * check it. Someone might play overlap and TTL games.
1113          */
1114         if (fd_head->flags & FD_DEFRAGMENTED) {
1115                 fd->flags      |= FD_OVERLAP;
1116                 fd_head->flags |= FD_OVERLAP;
1117
1118                 /* make sure it's not past the end */
1119                 if (fd->offset > fd_head->datalen) {
1120                         /* new fragment comes after the end */
1121                         fd->flags      |= FD_TOOLONGFRAGMENT;
1122                         fd_head->flags |= FD_TOOLONGFRAGMENT;
1123                         LINK_FRAG(fd_head,fd);
1124                         return TRUE;
1125                 }
1126                 /* make sure it doesnt conflict with previous data */
1127                 dfpos=0;
1128                 last_fd=NULL;
1129                 for (fd_i=fd_head->next;fd_i && (fd_i->offset!=fd->offset);fd_i=fd_i->next) {
1130                   if (!last_fd || last_fd->offset!=fd_i->offset){
1131                     dfpos += fd_i->len;
1132                   }
1133                   last_fd=fd_i;
1134                 }
1135                 if(fd_i){
1136                         /* new fragment overlaps existing fragment */
1137                         if(fd_i->len!=fd->len){
1138                                 /*
1139                                  * They have different lengths; this
1140                                  * is definitely a conflict.
1141                                  */
1142                                 fd->flags      |= FD_OVERLAPCONFLICT;
1143                                 fd_head->flags |= FD_OVERLAPCONFLICT;
1144                                 LINK_FRAG(fd_head,fd);
1145                                 return TRUE;
1146                         }
1147                         DISSECTOR_ASSERT(fd_head->len >= dfpos + fd->len);
1148                         if ( memcmp(fd_head->data+dfpos,
1149                             tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
1150                                 /*
1151                                  * They have the same length, but the
1152                                  * data isn't the same.
1153                                  */
1154                                 fd->flags      |= FD_OVERLAPCONFLICT;
1155                                 fd_head->flags |= FD_OVERLAPCONFLICT;
1156                                 LINK_FRAG(fd_head,fd);
1157                                 return TRUE;
1158                         }
1159                         /* it was just an overlap, link it and return */
1160                         LINK_FRAG(fd_head,fd);
1161                         return TRUE;
1162                 } else {
1163                         /*
1164                          * New fragment doesn't overlap an existing
1165                          * fragment - there was presumably a gap in
1166                          * the sequence number space.
1167                          *
1168                          * XXX - what should we do here?  Is it always
1169                          * the case that there are no gaps, or are there
1170                          * protcols using sequence numbers where there
1171                          * can be gaps?
1172                          *
1173                          * If the former, the check below for having
1174                          * received all the fragments should check for
1175                          * holes in the sequence number space and for the
1176                          * first sequence number being 0.  If we do that,
1177                          * the only way we can get here is if this fragment
1178                          * is past the end of the sequence number space -
1179                          * but the check for "fd->offset > fd_head->datalen"
1180                          * would have caught that above, so it can't happen.
1181                          *
1182                          * If the latter, we don't have a good way of
1183                          * knowing whether reassembly is complete if we
1184                          * get packet out of order such that the "last"
1185                          * fragment doesn't show up last - but, unless
1186                          * in-order reliable delivery of fragments is
1187                          * guaranteed, an implementation of the protocol
1188                          * has no way of knowing whether reassembly is
1189                          * complete, either.
1190                          *
1191                          * For now, we just link the fragment in and
1192                          * return.
1193                          */
1194                         LINK_FRAG(fd_head,fd);
1195                         return TRUE;
1196                 }
1197         }
1198
1199         /* If we have reached this point, the packet is not defragmented yet.
1200          * Save all payload in a buffer until we can defragment.
1201          * XXX - what if we didn't capture the entire fragment due
1202          * to a too-short snapshot length?
1203          */
1204         /* check len, ther may be a fragment with 0 len, that is actually the tail */
1205         if (fd->len) {
1206                 fd->data = g_malloc(fd->len);
1207                 tvb_memcpy(tvb, fd->data, offset, fd->len);
1208         }
1209         LINK_FRAG(fd_head,fd);
1210
1211
1212         if( !(fd_head->datalen) ){
1213                 /* if we dont know the sequence number of the last fragment,
1214                  * there are definitely still missing packets. Cheaper than
1215                  * the check below.
1216                  */
1217                 return FALSE;
1218         }
1219
1220
1221         /* check if we have received the entire fragment
1222          * this is easy since the list is sorted and the head is faked.
1223          */
1224         max = 0;
1225         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1226           if ( fd_i->offset==max ){
1227             max++;
1228           }
1229         }
1230         /* max will now be datalen+1 if all fragments have been seen */
1231
1232         if (max <= fd_head->datalen) {
1233                 /* we have not received all packets yet */
1234                 return FALSE;
1235         }
1236
1237
1238         if (max > (fd_head->datalen+1)) {
1239                 /* oops, too long fragment detected */
1240                 fd->flags      |= FD_TOOLONGFRAGMENT;
1241                 fd_head->flags |= FD_TOOLONGFRAGMENT;
1242         }
1243
1244
1245         /* we have received an entire packet, defragment it and
1246          * free all fragments
1247          */
1248         size=0;
1249         last_fd=NULL;
1250         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1251           if(!last_fd || last_fd->offset!=fd_i->offset){
1252             size+=fd_i->len;
1253           }
1254           last_fd=fd_i;
1255         }
1256         fd_head->data = g_malloc(size);
1257         fd_head->len = size;            /* record size for caller       */
1258
1259         /* add all data fragments */
1260         dfpos = 0;
1261         last_fd=NULL;
1262         for (fd_i=fd_head->next;fd_i && fd_i->len + dfpos <= size;fd_i=fd_i->next) {
1263           if (fd_i->len) {
1264             if(!last_fd || last_fd->offset!=fd_i->offset){
1265               memcpy(fd_head->data+dfpos,fd_i->data,fd_i->len);
1266               dfpos += fd_i->len;
1267             } else {
1268               /* duplicate/retransmission/overlap */
1269               fd_i->flags    |= FD_OVERLAP;
1270               fd_head->flags |= FD_OVERLAP;
1271               if( (last_fd->len!=fd_i->datalen)
1272                   || memcmp(last_fd->data, fd_i->data, last_fd->len) ){
1273                         fd->flags      |= FD_OVERLAPCONFLICT;
1274                         fd_head->flags |= FD_OVERLAPCONFLICT;
1275               }
1276             }
1277           }
1278           last_fd=fd_i;
1279         }
1280
1281         /* we have defragmented the pdu, now free all fragments*/
1282         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1283           if(fd_i->data){
1284             g_free(fd_i->data);
1285             fd_i->data=NULL;
1286           }
1287         }
1288
1289         /* mark this packet as defragmented.
1290            allows us to skip any trailing fragments */
1291         fd_head->flags |= FD_DEFRAGMENTED;
1292         fd_head->reassembled_in=pinfo->fd->num;
1293
1294         return TRUE;
1295 }
1296
1297 /*
1298  * This function adds a new fragment to the fragment hash table.
1299  * If this is the first fragment seen for this datagram, a new entry
1300  * is created in the hash table, otherwise this fragment is just added
1301  * to the linked list of fragments for this packet.
1302  *
1303  * Returns a pointer to the head of the fragment data list if we have all the
1304  * fragments, NULL otherwise.
1305  *
1306  * This function assumes frag_number being a block sequence number.
1307  * The bsn for the first block is 0.
1308  */
1309 fragment_data *
1310 fragment_add_seq(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
1311              GHashTable *fragment_table, guint32 frag_number,
1312              guint32 frag_data_len, gboolean more_frags)
1313 {
1314         fragment_key key, *new_key;
1315         fragment_data *fd_head;
1316
1317         /* create key to search hash with */
1318         key.src = pinfo->src;
1319         key.dst = pinfo->dst;
1320         key.id  = id;
1321
1322         fd_head = g_hash_table_lookup(fragment_table, &key);
1323
1324         /* have we already seen this frame ?*/
1325         if (pinfo->fd->flags.visited) {
1326                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
1327                         return fd_head;
1328                 } else {
1329                         return NULL;
1330                 }
1331         }
1332
1333         if (fd_head==NULL){
1334                 /* not found, this must be the first snooped fragment for this
1335                  * packet. Create list-head.
1336                  */
1337                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1338
1339                 /* head/first structure in list only holds no other data than
1340                  * 'datalen' then we don't have to change the head of the list
1341                  * even if we want to keep it sorted
1342                  */
1343                 fd_head->next=NULL;
1344                 fd_head->datalen=0;
1345                 fd_head->offset=0;
1346                 fd_head->len=0;
1347                 fd_head->flags=FD_BLOCKSEQUENCE;
1348                 fd_head->data=NULL;
1349                 fd_head->reassembled_in=0;
1350
1351                 /*
1352                  * We're going to use the key to insert the fragment,
1353                  * so allocate a structure for it, and copy the
1354                  * addresses, allocating new buffers for the address
1355                  * data.
1356                  */
1357                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
1358                 COPY_ADDRESS(&new_key->src, &key.src);
1359                 COPY_ADDRESS(&new_key->dst, &key.dst);
1360                 new_key->id = key.id;
1361                 g_hash_table_insert(fragment_table, new_key, fd_head);
1362         }
1363
1364         if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1365                                   frag_number, frag_data_len, more_frags)) {
1366                 /*
1367                  * Reassembly is complete.
1368                  */
1369                 return fd_head;
1370         } else {
1371                 /*
1372                  * Reassembly isn't complete.
1373                  */
1374                 return NULL;
1375         }
1376 }
1377
1378 fragment_data *
1379 fragment_add_dcerpc_dg(tvbuff_t *tvb, int offset, packet_info *pinfo, guint32 id,
1380                     void *v_act_id,
1381                     GHashTable *fragment_table, guint32 frag_number,
1382                     guint32 frag_data_len, gboolean more_frags)
1383 {
1384        dcerpc_fragment_key key, *new_key;
1385        fragment_data *fd_head;
1386        e_uuid_t *act_id = (e_uuid_t *)v_act_id;
1387
1388        /* create key to search hash with */
1389        key.src = pinfo->src;
1390        key.dst = pinfo->dst;
1391        key.id  = id;
1392        key.act_id  = *act_id;
1393
1394        fd_head = g_hash_table_lookup(fragment_table, &key);
1395
1396        /* have we already seen this frame ?*/
1397        if (pinfo->fd->flags.visited) {
1398                if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
1399                        return fd_head;
1400                } else {
1401                        return NULL;
1402                }
1403        }
1404
1405        if (fd_head==NULL){
1406                /* not found, this must be the first snooped fragment for this
1407                  * packet. Create list-head.
1408                 */
1409                fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1410
1411                /* head/first structure in list only holds no other data than
1412                  * 'datalen' then we don't have to change the head of the list
1413                  * even if we want to keep it sorted
1414                  */
1415                fd_head->next=NULL;
1416                fd_head->datalen=0;
1417                fd_head->offset=0;
1418                fd_head->len=0;
1419                fd_head->flags=FD_BLOCKSEQUENCE;
1420                fd_head->data=NULL;
1421                fd_head->reassembled_in=0;
1422
1423                /*
1424                 * We're going to use the key to insert the fragment,
1425                 * so allocate a structure for it, and copy the
1426                 * addresses, allocating new buffers for the address
1427                 * data.
1428                 */
1429                new_key = se_alloc(sizeof(dcerpc_fragment_key));
1430                COPY_ADDRESS(&new_key->src, &key.src);
1431                COPY_ADDRESS(&new_key->dst, &key.dst);
1432                new_key->id = key.id;
1433                new_key->act_id = key.act_id;
1434                g_hash_table_insert(fragment_table, new_key, fd_head);
1435        }
1436
1437        if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1438                                  frag_number, frag_data_len, more_frags)) {
1439                /*
1440                 * Reassembly is complete.
1441                 */
1442                return fd_head;
1443        } else {
1444                /*
1445                 * Reassembly isn't complete.
1446                 */
1447                return NULL;
1448        }
1449 }
1450
1451 /*
1452  * This does the work for "fragment_add_seq_check()" and
1453  * "fragment_add_seq_next()".
1454  *
1455  * This function assumes frag_number being a block sequence number.
1456  * The bsn for the first block is 0.
1457  *
1458  * If "no_frag_number" is TRUE, it uses the next expected fragment number
1459  * as the fragment number if there is a reassembly in progress, otherwise
1460  * it uses 0.
1461  *
1462  * If "no_frag_number" is FALSE, it uses the "frag_number" argument as
1463  * the fragment number.
1464  *
1465  * If this is the first fragment seen for this datagram, a new
1466  * "fragment_data" structure is allocated to refer to the reassembled,
1467  * packet, and:
1468  *
1469  *      if "more_frags" is false and "frag_802_11_hack" is true, the
1470  *      structure is not added to the hash table, and not given any
1471  *      fragments to refer to, but is just returned - this is a special
1472  *      hack for the 802.11 dissector (see packet-80211.c);
1473  *
1474  *      otherwise, this fragment is added to the linked list of fragments
1475  *      for this packet, and the "fragment_data" structure is put into
1476  *      the hash table.
1477  *
1478  * Otherwise, this fragment is just added to the linked list of fragments
1479  * for this packet.
1480  *
1481  * If, after processing this fragment, we have all the fragments,
1482  * "fragment_add_seq_check_work()" removes that from the fragment hash
1483  * table if necessary and adds it to the table of reassembled fragments,
1484  * and returns a pointer to the head of the fragment list.
1485  *
1486  * If this is the first fragment we've seen, and "more_frags" is false
1487  * and "frag_802_11_hack" is true, "fragment_add_seq_check_work()" does
1488  * nothing to the fragment data list, and returns a pointer to the head
1489  * of that (empty) list.
1490  *
1491  * Otherwise, it returns NULL.
1492  *
1493  * XXX - Should we simply return NULL for zero-length fragments?
1494  */
1495 static fragment_data *
1496 fragment_add_seq_check_work(tvbuff_t *tvb, int offset, packet_info *pinfo,
1497              guint32 id, GHashTable *fragment_table,
1498              GHashTable *reassembled_table, guint32 frag_number,
1499              guint32 frag_data_len, gboolean more_frags,
1500              gboolean no_frag_number, gboolean frag_802_11_hack)
1501 {
1502         reassembled_key reass_key;
1503         fragment_key key, *new_key, *old_key;
1504         gpointer orig_key, value;
1505         fragment_data *fd_head, *fd;
1506
1507         /*
1508          * Have we already seen this frame?
1509          * If so, look for it in the table of reassembled packets.
1510          */
1511         if (pinfo->fd->flags.visited) {
1512                 reass_key.frame = pinfo->fd->num;
1513                 reass_key.id = id;
1514                 return g_hash_table_lookup(reassembled_table, &reass_key);
1515         }
1516
1517         /* create key to search hash with */
1518         key.src = pinfo->src;
1519         key.dst = pinfo->dst;
1520         key.id  = id;
1521
1522         if (!g_hash_table_lookup_extended(fragment_table, &key,
1523                                           &orig_key, &value)) {
1524                 /* not found, this must be the first snooped fragment for this
1525                  * packet. Create list-head.
1526                  */
1527                 fd_head=g_mem_chunk_alloc(fragment_data_chunk);
1528
1529                 /* head/first structure in list only holds no other data than
1530                  * 'datalen' then we don't have to change the head of the list
1531                  * even if we want to keep it sorted
1532                  */
1533                 fd_head->next=NULL;
1534                 fd_head->datalen=0;
1535                 fd_head->offset=0;
1536                 fd_head->len=0;
1537                 fd_head->flags=FD_BLOCKSEQUENCE;
1538                 fd_head->data=NULL;
1539                 fd_head->reassembled_in=0;
1540
1541                 if ((no_frag_number || frag_802_11_hack) && !more_frags) {
1542                         /*
1543                          * This is the last fragment for this packet, and
1544                          * is the only one we've seen.
1545                          *
1546                          * Either we don't have sequence numbers, in which
1547                          * case we assume this is the first fragment for
1548                          * this packet, or we're doing special 802.11
1549                          * processing, in which case we assume it's one
1550                          * of those reassembled packets with a non-zero
1551                          * fragment number (see packet-80211.c); just
1552                          * add the fragment to the table of reassembled
1553                          * packets, and return a pointer to the head of
1554                          * the list.
1555                          */
1556                         fragment_reassembled(fd_head, pinfo,
1557                                reassembled_table, id);
1558                         return fd_head;
1559                 }
1560
1561                 /*
1562                  * We're going to use the key to insert the fragment,
1563                  * so allocate a structure for it, and copy the
1564                  * addresses, allocating new buffers for the address
1565                  * data.
1566                  */
1567                 new_key = g_mem_chunk_alloc(fragment_key_chunk);
1568                 COPY_ADDRESS(&new_key->src, &key.src);
1569                 COPY_ADDRESS(&new_key->dst, &key.dst);
1570                 new_key->id = key.id;
1571                 g_hash_table_insert(fragment_table, new_key, fd_head);
1572
1573                 orig_key = new_key;     /* for unhashing it later */
1574
1575                 /*
1576                  * If we weren't given an initial fragment number,
1577                  * make it 0.
1578                  */
1579                 if (no_frag_number)
1580                         frag_number = 0;
1581         } else {
1582                 /*
1583                  * We found it.
1584                  */
1585                 fd_head = value;
1586
1587                 /*
1588                  * If we weren't given an initial fragment number,
1589                  * use the next expected fragment number as the fragment
1590                  * number for this fragment.
1591                  */
1592                 if (no_frag_number) {
1593                         for (fd = fd_head; fd != NULL; fd = fd->next) {
1594                                 if (fd->next == NULL)
1595                                         frag_number = fd->offset + 1;
1596                         }
1597                 }
1598         }
1599
1600         /*
1601          * If we don't have all the data that is in this fragment,
1602          * then we can't, and don't, do reassembly on it.
1603          *
1604          * If it's the first frame, handle it as an unfragmented packet.
1605          * Otherwise, just handle it as a fragment.
1606          *
1607          * If "more_frags" isn't set, we get rid of the entry in the
1608          * hash table for this reassembly, as we don't need it any more.
1609          */
1610         if (!tvb_bytes_exist(tvb, offset, frag_data_len)) {
1611                 if (!more_frags) {
1612                         /*
1613                          * Remove this from the table of in-progress
1614                          * reassemblies, and free up any memory used for
1615                          * it in that table.
1616                          */
1617                         old_key = orig_key;
1618                         fragment_unhash(fragment_table, old_key);
1619                 }
1620                 return frag_number == 0 ? fd_head : NULL;
1621         }
1622
1623         if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1624                                   frag_number, frag_data_len, more_frags)) {
1625                 /*
1626                  * Reassembly is complete.
1627                  * Remove this from the table of in-progress
1628                  * reassemblies, add it to the table of
1629                  * reassembled packets, and return it.
1630                  */
1631
1632                 /*
1633                  * Remove this from the table of in-progress reassemblies,
1634                  * and free up any memory used for it in that table.
1635                  */
1636                 old_key = orig_key;
1637                 fragment_unhash(fragment_table, old_key);
1638
1639                 /*
1640                  * Add this item to the table of reassembled packets.
1641                  */
1642                 fragment_reassembled(fd_head, pinfo, reassembled_table, id);
1643                 return fd_head;
1644         } else {
1645                 /*
1646                  * Reassembly isn't complete.
1647                  */
1648                 return NULL;
1649         }
1650 }
1651
1652 fragment_data *
1653 fragment_add_seq_check(tvbuff_t *tvb, int offset, packet_info *pinfo,
1654              guint32 id, GHashTable *fragment_table,
1655              GHashTable *reassembled_table, guint32 frag_number,
1656              guint32 frag_data_len, gboolean more_frags)
1657 {
1658         return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1659             fragment_table, reassembled_table, frag_number, frag_data_len,
1660             more_frags, FALSE, FALSE);
1661 }
1662
1663 fragment_data *
1664 fragment_add_seq_802_11(tvbuff_t *tvb, int offset, packet_info *pinfo,
1665              guint32 id, GHashTable *fragment_table,
1666              GHashTable *reassembled_table, guint32 frag_number,
1667              guint32 frag_data_len, gboolean more_frags)
1668 {
1669         return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1670             fragment_table, reassembled_table, frag_number, frag_data_len,
1671             more_frags, FALSE, TRUE);
1672 }
1673
1674 fragment_data *
1675 fragment_add_seq_next(tvbuff_t *tvb, int offset, packet_info *pinfo,
1676              guint32 id, GHashTable *fragment_table,
1677              GHashTable *reassembled_table, guint32 frag_data_len,
1678              gboolean more_frags)
1679 {
1680         return fragment_add_seq_check_work(tvb, offset, pinfo, id,
1681             fragment_table, reassembled_table, 0, frag_data_len,
1682             more_frags, TRUE, FALSE);
1683 }
1684
1685 /*
1686  * Process reassembled data; if we're on the frame in which the data
1687  * was reassembled, put the fragment information into the protocol
1688  * tree, and construct a tvbuff with the reassembled data, otherwise
1689  * just put a "reassembled in" item into the protocol tree.
1690  */
1691 tvbuff_t *
1692 process_reassembled_data(tvbuff_t *tvb, int offset, packet_info *pinfo,
1693     const char *name, fragment_data *fd_head, const fragment_items *fit,
1694     gboolean *update_col_infop, proto_tree *tree)
1695 {
1696         tvbuff_t *next_tvb;
1697         gboolean update_col_info;
1698         proto_item *frag_tree_item;
1699
1700         if (fd_head != NULL && pinfo->fd->num == fd_head->reassembled_in) {
1701                 /*
1702                  * OK, we've reassembled this.
1703                  * Is this something that's been reassembled from more
1704                  * than one fragment?
1705                  */
1706                 if (fd_head->next != NULL) {
1707                         /*
1708                          * Yes.
1709                          * Allocate a new tvbuff, referring to the
1710                          * reassembled payload.
1711                          */
1712                         if (fd_head->flags & FD_BLOCKSEQUENCE) {
1713                                 next_tvb = tvb_new_real_data(fd_head->data,
1714                                       fd_head->len, fd_head->len);
1715                         } else {
1716                                 next_tvb = tvb_new_real_data(fd_head->data,
1717                                       fd_head->datalen, fd_head->datalen);
1718                         }
1719
1720                         /*
1721                          * Add the tvbuff to the list of tvbuffs to which
1722                          * the tvbuff we were handed refers, so it'll get
1723                          * cleaned up when that tvbuff is cleaned up.
1724                          */
1725                         tvb_set_child_real_data_tvbuff(tvb, next_tvb);
1726
1727                         /* Add the defragmented data to the data source list. */
1728                         add_new_data_source(pinfo, next_tvb, name);
1729
1730                         /* show all fragments */
1731                         if (fd_head->flags & FD_BLOCKSEQUENCE) {
1732                                 update_col_info = !show_fragment_seq_tree(
1733                                     fd_head, fit,  tree, pinfo, next_tvb, &frag_tree_item);
1734                         } else {
1735                                 update_col_info = !show_fragment_tree(fd_head,
1736                                     fit, tree, pinfo, next_tvb, &frag_tree_item);
1737                         }
1738                 } else {
1739                         /*
1740                          * No.
1741                          * Return a tvbuff with the payload.
1742                          */
1743                         next_tvb = tvb_new_subset(tvb, offset, -1, -1);
1744                         pinfo->fragmented = FALSE;      /* one-fragment packet */
1745                         update_col_info = TRUE;
1746                 }
1747                 if (update_col_infop != NULL)
1748                         *update_col_infop = update_col_info;
1749         } else {
1750                 /*
1751                  * We don't have the complete reassembled payload, or this
1752                  * isn't the final frame of that payload.
1753                  */
1754                 next_tvb = NULL;
1755
1756                 /*
1757                  * If we know what frame this was reassembled in,
1758                  * and if there's a field to use for the number of
1759                  * the frame in which the packet was reassembled,
1760                  * add it to the protocol tree.
1761                  */
1762                 if (fd_head != NULL && fit->hf_reassembled_in != NULL) {
1763                         proto_tree_add_uint(tree,
1764                             *(fit->hf_reassembled_in), tvb,
1765                             0, 0, fd_head->reassembled_in);
1766                 }
1767         }
1768         return next_tvb;
1769 }
1770
1771 /*
1772  * Show a single fragment in a fragment subtree, and put information about
1773  * it in the top-level item for that subtree.
1774  */
1775 static void
1776 show_fragment(fragment_data *fd, int offset, const fragment_items *fit,
1777     proto_tree *ft, proto_item *fi, gboolean first_frag, tvbuff_t *tvb)
1778 {
1779         proto_item *fei=NULL;
1780         int hf;
1781
1782         if (first_frag)
1783                 proto_item_append_text(fi, " (%u byte%s): ", tvb_length(tvb),
1784                     plurality(tvb_length(tvb), "", "s"));
1785         else
1786                 proto_item_append_text(fi, ", ");
1787         proto_item_append_text(fi, "#%u(%u)", fd->frame, fd->len);
1788
1789         if (fd->flags & (FD_OVERLAPCONFLICT
1790                 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1791                 hf = *(fit->hf_fragment_error);
1792         } else {
1793                 hf = *(fit->hf_fragment);
1794         }
1795         if (fd->len == 0) {
1796                 fei = proto_tree_add_uint_format(ft, hf,
1797                         tvb, offset, fd->len,
1798                         fd->frame,
1799                         "Frame: %u (no data)",
1800                         fd->frame);
1801         } else {
1802                 fei = proto_tree_add_uint_format(ft, hf,
1803                         tvb, offset, fd->len,
1804                         fd->frame,
1805                         "Frame: %u, payload: %u-%u (%u byte%s)",
1806                         fd->frame,
1807                         offset,
1808                         offset+fd->len-1,
1809                         fd->len,
1810                         plurality(fd->len, "", "s"));
1811         }
1812         PROTO_ITEM_SET_GENERATED(fei);
1813         if (fd->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT
1814                 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1815                 /* this fragment has some flags set, create a subtree
1816                  * for it and display the flags.
1817                  */
1818                 proto_tree *fet=NULL;
1819
1820                 fet = proto_item_add_subtree(fei, *(fit->ett_fragment));
1821                 if (fd->flags&FD_OVERLAP) {
1822                         fei=proto_tree_add_boolean(fet,
1823                                 *(fit->hf_fragment_overlap),
1824                                 tvb, 0, 0,
1825                                 TRUE);
1826                         PROTO_ITEM_SET_GENERATED(fei);
1827                 }
1828                 if (fd->flags&FD_OVERLAPCONFLICT) {
1829                         fei=proto_tree_add_boolean(fet,
1830                                 *(fit->hf_fragment_overlap_conflict),
1831                                 tvb, 0, 0,
1832                                 TRUE);
1833                         PROTO_ITEM_SET_GENERATED(fei);
1834                 }
1835                 if (fd->flags&FD_MULTIPLETAILS) {
1836                         fei=proto_tree_add_boolean(fet,
1837                                 *(fit->hf_fragment_multiple_tails),
1838                                 tvb, 0, 0,
1839                                 TRUE);
1840                         PROTO_ITEM_SET_GENERATED(fei);
1841                 }
1842                 if (fd->flags&FD_TOOLONGFRAGMENT) {
1843                         fei=proto_tree_add_boolean(fet,
1844                                 *(fit->hf_fragment_too_long_fragment),
1845                                 tvb, 0, 0,
1846                                 TRUE);
1847                         PROTO_ITEM_SET_GENERATED(fei);
1848                 }
1849         }
1850 }
1851
1852 static gboolean
1853 show_fragment_errs_in_col(fragment_data *fd_head, const fragment_items *fit,
1854     packet_info *pinfo)
1855 {
1856         if (fd_head->flags & (FD_OVERLAPCONFLICT
1857                 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
1858                 if (check_col(pinfo->cinfo, COL_INFO)) {
1859                         col_add_fstr(pinfo->cinfo, COL_INFO,
1860                                 "[Illegal %s]", fit->tag);
1861                         return TRUE;
1862                 }
1863         }
1864
1865         return FALSE;
1866 }
1867
1868 /* This function will build the fragment subtree; it's for fragments
1869    reassembled with "fragment_add()".
1870
1871    It will return TRUE if there were fragmentation errors
1872    or FALSE if fragmentation was ok.
1873 */
1874 gboolean
1875 show_fragment_tree(fragment_data *fd_head, const fragment_items *fit,
1876     proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi)
1877 {
1878         fragment_data *fd;
1879         proto_tree *ft;
1880         gboolean first_frag;
1881
1882         /* It's not fragmented. */
1883         pinfo->fragmented = FALSE;
1884
1885         *fi = proto_tree_add_item(tree, *(fit->hf_fragments),
1886             tvb, 0, -1, FALSE);
1887         PROTO_ITEM_SET_GENERATED(*fi);
1888
1889         ft = proto_item_add_subtree(*fi, *(fit->ett_fragments));
1890         first_frag = TRUE;
1891         for (fd = fd_head->next; fd != NULL; fd = fd->next) {
1892                 show_fragment(fd, fd->offset, fit, ft, *fi, first_frag, tvb);
1893                 first_frag = FALSE;
1894         }
1895
1896         return show_fragment_errs_in_col(fd_head, fit, pinfo);
1897 }
1898
1899 /* This function will build the fragment subtree; it's for fragments
1900    reassembled with "fragment_add_seq()" or "fragment_add_seq_check()".
1901
1902    It will return TRUE if there were fragmentation errors
1903    or FALSE if fragmentation was ok.
1904 */
1905 gboolean
1906 show_fragment_seq_tree(fragment_data *fd_head, const fragment_items *fit,
1907     proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi)
1908 {
1909         guint32 offset, next_offset;
1910         fragment_data *fd, *last_fd;
1911         proto_tree *ft;
1912         gboolean first_frag;
1913
1914         /* It's not fragmented. */
1915         pinfo->fragmented = FALSE;
1916
1917         *fi = proto_tree_add_item(tree, *(fit->hf_fragments),
1918             tvb, 0, -1, FALSE);
1919         PROTO_ITEM_SET_GENERATED(*fi);
1920
1921         ft = proto_item_add_subtree(*fi, *(fit->ett_fragments));
1922         offset = 0;
1923         next_offset = 0;
1924         last_fd = NULL;
1925         first_frag = TRUE;
1926         for (fd = fd_head->next; fd != NULL; fd = fd->next){
1927                 if (last_fd == NULL || last_fd->offset != fd->offset) {
1928                         offset = next_offset;
1929                         next_offset += fd->len;
1930                 }
1931                 last_fd = fd;
1932                 show_fragment(fd, offset, fit, ft, *fi, first_frag, tvb);
1933                 first_frag = FALSE;
1934         }
1935
1936         return show_fragment_errs_in_col(fd_head, fit, pinfo);
1937 }