(Trivial) Fix spellling in a comment.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28
29 #include <epan/packet.h>
30 #include <epan/exceptions.h>
31 #include <epan/emem.h>
32 #include <epan/reassemble.h>
33 #include <epan/tvbuff-int.h>
34
35 /*
36  * Functions for reassembly tables where the endpoint addresses, and a
37  * fragment ID, are used as the key.
38  */
39 typedef struct _fragment_addresses_key {
40         address src;
41         address dst;
42         guint32 id;
43 } fragment_addresses_key;
44
45 static guint
46 fragment_addresses_hash(gconstpointer k)
47 {
48         const fragment_addresses_key* key = (const fragment_addresses_key*) k;
49         guint hash_val;
50 /*
51         int i;
52 */
53
54         hash_val = 0;
55
56 /*      More than likely: in most captures src and dst addresses are the
57         same, and would hash the same.
58         We only use id as the hash as an optimization.
59
60         for (i = 0; i < key->src.len; i++)
61                 hash_val += key->src.data[i];
62         for (i = 0; i < key->dst.len; i++)
63                 hash_val += key->dst.data[i];
64 */
65
66         hash_val += key->id;
67
68         return hash_val;
69 }
70
71 static gint
72 fragment_addresses_equal(gconstpointer k1, gconstpointer k2)
73 {
74         const fragment_addresses_key* key1 = (const fragment_addresses_key*) k1;
75         const fragment_addresses_key* key2 = (const fragment_addresses_key*) k2;
76
77         /*
78          * key.id is the first item to compare since it's the item most
79          * likely to differ between sessions, thus short-circuiting
80          * the comparison of addresses.
81          */
82         return (key1->id == key2->id) &&
83                (ADDRESSES_EQUAL(&key1->src, &key2->src)) &&
84                (ADDRESSES_EQUAL(&key1->dst, &key2->dst));
85 }
86
87 /*
88  * Create a fragment key for temporary use; it can point to non-
89  * persistent data, and so must only be used to look up and
90  * delete entries, not to add them.
91  */
92 static gpointer
93 fragment_addresses_temporary_key(const packet_info *pinfo, const guint32 id,
94                                  const void *data _U_)
95 {
96         fragment_addresses_key *key = g_slice_new(fragment_addresses_key);
97
98         /*
99          * Do a shallow copy of the addresses.
100          */
101         key->src = pinfo->src;
102         key->dst = pinfo->dst;
103         key->id = id;
104
105         return (gpointer)key;
106 }
107
108 /*
109  * Create a fragment key for permanent use; it must point to persistent
110  * data, so that it can be used to add entries.
111  */
112 static gpointer
113 fragment_addresses_persistent_key(const packet_info *pinfo, const guint32 id,
114                                   const void *data _U_)
115 {
116         fragment_addresses_key *key = g_slice_new(fragment_addresses_key);
117
118         /*
119          * Do a deep copy of the addresses.
120          */
121         COPY_ADDRESS(&key->src, &pinfo->src);
122         COPY_ADDRESS(&key->dst, &pinfo->dst);
123         key->id = id;
124
125         return (gpointer)key;
126 }
127
128 static void
129 fragment_addresses_free_temporary_key(gpointer ptr)
130 {
131         fragment_addresses_key *key = (fragment_addresses_key *)ptr;
132
133         if(key)
134                 g_slice_free(fragment_addresses_key, key);
135 }
136
137 static void
138 fragment_addresses_free_persistent_key(gpointer ptr)
139 {
140         fragment_addresses_key *key = (fragment_addresses_key *)ptr;
141
142         if(key){
143                 /*
144                  * Free up the copies of the addresses from the old key.
145                  */
146                 g_free((gpointer)key->src.data);
147                 g_free((gpointer)key->dst.data);
148
149                 g_slice_free(fragment_addresses_key, key);
150         }
151 }
152
153 const reassembly_table_functions
154 addresses_reassembly_table_functions = {
155         fragment_addresses_hash,
156         fragment_addresses_equal,
157         fragment_addresses_temporary_key,
158         fragment_addresses_persistent_key,
159         fragment_addresses_free_temporary_key,
160         fragment_addresses_free_persistent_key
161 };
162
163 /*
164  * Functions for reassembly tables where the endpoint addresses and ports,
165  * and a fragment ID, are used as the key.
166  */
167 typedef struct _fragment_addresses_ports_key {
168         address src_addr;
169         address dst_addr;
170         guint32 src_port;
171         guint32 dst_port;
172         guint32 id;
173 } fragment_addresses_ports_key;
174
175 static guint
176 fragment_addresses_ports_hash(gconstpointer k)
177 {
178         const fragment_addresses_ports_key* key = (const fragment_addresses_ports_key*) k;
179         guint hash_val;
180 /*
181         int i;
182 */
183
184         hash_val = 0;
185
186 /*      More than likely: in most captures src and dst addresses and ports
187         are the same, and would hash the same.
188         We only use id as the hash as an optimization.
189
190         for (i = 0; i < key->src.len; i++)
191                 hash_val += key->src_addr.data[i];
192         for (i = 0; i < key->dst.len; i++)
193                 hash_val += key->dst_addr.data[i];
194         hash_val += key->src_port;
195         hash_val += key->dst_port;
196 */
197
198         hash_val += key->id;
199
200         return hash_val;
201 }
202
203 static gint
204 fragment_addresses_ports_equal(gconstpointer k1, gconstpointer k2)
205 {
206         const fragment_addresses_ports_key* key1 = (const fragment_addresses_ports_key*) k1;
207         const fragment_addresses_ports_key* key2 = (const fragment_addresses_ports_key*) k2;
208
209         /*
210          * key.id is the first item to compare since it's the item most
211          * likely to differ between sessions, thus short-circuiting
212          * the comparison of addresses and ports.
213          */
214         return (key1->id == key2->id) &&
215                (ADDRESSES_EQUAL(&key1->src_addr, &key2->src_addr)) &&
216                (ADDRESSES_EQUAL(&key1->dst_addr, &key2->dst_addr)) &&
217                (key1->src_port == key2->src_port) &&
218                (key1->dst_port == key2->dst_port);
219 }
220
221 /*
222  * Create a fragment key for temporary use; it can point to non-
223  * persistent data, and so must only be used to look up and
224  * delete entries, not to add them.
225  */
226 static gpointer
227 fragment_addresses_ports_temporary_key(const packet_info *pinfo, const guint32 id,
228                                        const void *data _U_)
229 {
230         fragment_addresses_ports_key *key = g_slice_new(fragment_addresses_ports_key);
231
232         /*
233          * Do a shallow copy of the addresses.
234          */
235         key->src_addr = pinfo->src;
236         key->dst_addr = pinfo->dst;
237         key->src_port = pinfo->srcport;
238         key->dst_port = pinfo->destport;
239         key->id = id;
240
241         return (gpointer)key;
242 }
243
244 /*
245  * Create a fragment key for permanent use; it must point to persistent
246  * data, so that it can be used to add entries.
247  */
248 static gpointer
249 fragment_addresses_ports_persistent_key(const packet_info *pinfo,
250                                         const guint32 id, const void *data _U_)
251 {
252         fragment_addresses_ports_key *key = g_slice_new(fragment_addresses_ports_key);
253
254         /*
255          * Do a deep copy of the addresses.
256          */
257         COPY_ADDRESS(&key->src_addr, &pinfo->src);
258         COPY_ADDRESS(&key->dst_addr, &pinfo->dst);
259         key->src_port = pinfo->srcport;
260         key->dst_port = pinfo->destport;
261         key->id = id;
262
263         return (gpointer)key;
264 }
265
266 static void
267 fragment_addresses_ports_free_temporary_key(gpointer ptr)
268 {
269         fragment_addresses_ports_key *key = (fragment_addresses_ports_key *)ptr;
270
271         if(key)
272                 g_slice_free(fragment_addresses_ports_key, key);
273 }
274
275 static void
276 fragment_addresses_ports_free_persistent_key(gpointer ptr)
277 {
278         fragment_addresses_ports_key *key = (fragment_addresses_ports_key *)ptr;
279
280         if(key){
281                 /*
282                  * Free up the copies of the addresses from the old key.
283                  */
284                 g_free((gpointer)key->src_addr.data);
285                 g_free((gpointer)key->dst_addr.data);
286
287                 g_slice_free(fragment_addresses_ports_key, key);
288         }
289 }
290
291 const reassembly_table_functions
292 addresses_ports_reassembly_table_functions = {
293         fragment_addresses_ports_hash,
294         fragment_addresses_ports_equal,
295         fragment_addresses_ports_temporary_key,
296         fragment_addresses_ports_persistent_key,
297         fragment_addresses_ports_free_temporary_key,
298         fragment_addresses_ports_free_persistent_key
299 };
300
301 typedef struct _reassembled_key {
302         guint32 id;
303         guint32 frame;
304 } reassembled_key;
305
306 static gint
307 reassembled_equal(gconstpointer k1, gconstpointer k2)
308 {
309         const reassembled_key* key1 = (const reassembled_key*) k1;
310         const reassembled_key* key2 = (const reassembled_key*) k2;
311
312         /*
313          * We assume that the frame numbers are unlikely to be equal,
314          * so we check them first.
315          */
316         return key1->frame == key2->frame && key1->id == key2->id;
317 }
318
319 static guint
320 reassembled_hash(gconstpointer k)
321 {
322         const reassembled_key* key = (const reassembled_key*) k;
323
324         return key->frame;
325 }
326
327 /*
328  * For a fragment hash table entry, free the associated fragments.
329  * The entry value (fd_chain) is freed herein and the entry is freed
330  * when the key freeing routine is called (as a consequence of returning
331  * TRUE from this function).
332  */
333 static gboolean
334 free_all_fragments(gpointer key_arg _U_, gpointer value, gpointer user_data _U_)
335 {
336         fragment_head *fd_head;
337         fragment_item *tmp_fd;
338
339         /* g_hash_table_new_full() was used to supply a function
340          * to free the key and anything to which it points
341          */
342         for (fd_head = (fragment_head *)value; fd_head != NULL; fd_head = tmp_fd) {
343                 tmp_fd=fd_head->next;
344
345                 if(fd_head->tvb_data && !(fd_head->flags&FD_SUBSET_TVB))
346                         tvb_free(fd_head->tvb_data);
347                 g_slice_free(fragment_item, fd_head);
348         }
349
350         return TRUE;
351 }
352
353 /* ------------------------- */
354 static fragment_head *new_head(const guint32 flags)
355 {
356         fragment_head *fd_head;
357         /* If head/first structure in list only holds no other data than
358         * 'datalen' then we don't have to change the head of the list
359         * even if we want to keep it sorted
360         */
361         fd_head=g_slice_new0(fragment_head);
362
363         fd_head->flags=flags;
364         return fd_head;
365 }
366
367 #define FD_VISITED_FREE 0xffff
368
369 /*
370  * For a reassembled-packet hash table entry, free the fragment data
371  * to which the value refers and also the key itself.
372  */
373 static gboolean
374 free_all_reassembled_fragments(gpointer key_arg, gpointer value,
375                                    gpointer user_data)
376 {
377         GPtrArray *allocated_fragments = (GPtrArray *) user_data;
378         fragment_head *fd_head;
379
380         for (fd_head = (fragment_head *)value; fd_head != NULL; fd_head = fd_head->next) {
381                 /*
382                  * A reassembled packet is inserted into the
383                  * hash table once for every frame that made
384                  * up the reassembled packet; add first seen
385                  * fragments to array and later free them in
386                  * free_fragments()
387                  */
388                 if (fd_head->flags != FD_VISITED_FREE) {
389                         if (fd_head->flags & FD_SUBSET_TVB)
390                                 fd_head->tvb_data = NULL;
391                         g_ptr_array_add(allocated_fragments, fd_head);
392                         fd_head->flags = FD_VISITED_FREE;
393                 }
394         }
395
396         g_slice_free(reassembled_key, (reassembled_key *)key_arg);
397
398         return TRUE;
399 }
400
401 static void
402 free_fragments(gpointer data, gpointer user_data _U_)
403 {
404         fragment_item *fd_head = (fragment_item *) data;
405
406         if (fd_head->tvb_data)
407                 tvb_free(fd_head->tvb_data);
408         g_slice_free(fragment_item, fd_head);
409 }
410
411 /*
412  * Initialize a reassembly table, with specified functions.
413  */
414 void
415 reassembly_table_init(reassembly_table *table,
416                       const reassembly_table_functions *funcs)
417 {
418         if (table->temporary_key_func == NULL)
419                 table->temporary_key_func = funcs->temporary_key_func;
420         if (table->persistent_key_func == NULL)
421                 table->persistent_key_func = funcs->persistent_key_func;
422         if (table->free_temporary_key_func == NULL)
423                 table->free_temporary_key_func = funcs->free_temporary_key_func;
424         if (table->fragment_table != NULL) {
425                 /*
426                  * The fragment hash table exists.
427                  *
428                  * Remove all entries and free fragment data for each entry.
429                  *
430                  * The keys, and anything to which they point, are freed by
431                  * calling the table's key freeing function.  The values
432                  * are freed in free_all_fragments().
433                  */
434                 g_hash_table_foreach_remove(table->fragment_table,
435                                             free_all_fragments, NULL);
436         } else {
437                 /* The fragment table does not exist. Create it */
438                 table->fragment_table = g_hash_table_new_full(funcs->hash_func,
439                     funcs->equal_func, funcs->free_persistent_key_func, NULL);
440         }
441
442         if (table->reassembled_table != NULL) {
443                 GPtrArray *allocated_fragments;
444
445                 /*
446                  * The reassembled-packet hash table exists.
447                  *
448                  * Remove all entries and free reassembled packet
449                  * data and key for each entry.
450                  */
451
452                 allocated_fragments = g_ptr_array_new();
453                 g_hash_table_foreach_remove(table->reassembled_table,
454                                 free_all_reassembled_fragments, allocated_fragments);
455
456                 g_ptr_array_foreach(allocated_fragments, free_fragments, NULL);
457                 g_ptr_array_free(allocated_fragments, TRUE);
458         } else {
459                 /* The fragment table does not exist. Create it */
460                 table->reassembled_table = g_hash_table_new(reassembled_hash,
461                     reassembled_equal);
462         }
463 }
464
465 /*
466  * Destroy a reassembly table.
467  */
468 void
469 reassembly_table_destroy(reassembly_table *table)
470 {
471         /*
472          * Clear the function pointers.
473          */
474         table->temporary_key_func = NULL;
475         table->persistent_key_func = NULL;
476         table->free_temporary_key_func = NULL;
477         if (table->fragment_table != NULL) {
478                 /*
479                  * The fragment hash table exists.
480                  *
481                  * Remove all entries and free fragment data for each entry.
482                  *
483                  * The keys, and anything to which they point, are freed by
484                  * calling the table's key freeing function.  The values
485                  * are freed in free_all_fragments().
486                  */
487                 g_hash_table_foreach_remove(table->fragment_table,
488                                             free_all_fragments, NULL);
489
490                 /*
491                  * Now destroy the hash table.
492                  */
493                 g_hash_table_destroy(table->fragment_table);
494                 table->fragment_table = NULL;
495         }
496         if (table->reassembled_table != NULL) {
497                 GPtrArray *allocated_fragments;
498
499                 /*
500                  * The reassembled-packet hash table exists.
501                  *
502                  * Remove all entries and free reassembled packet
503                  * data and key for each entry.
504                  */
505
506                 allocated_fragments = g_ptr_array_new();
507                 g_hash_table_foreach_remove(table->reassembled_table,
508                                 free_all_reassembled_fragments, allocated_fragments);
509
510                 g_ptr_array_foreach(allocated_fragments, free_fragments, NULL);
511                 g_ptr_array_free(allocated_fragments, TRUE);
512
513                 /*
514                  * Now destroy the hash table.
515                  */
516                 g_hash_table_destroy(table->reassembled_table);
517                 table->reassembled_table = NULL;
518         }
519 }
520
521 /*
522  * Look up an fd_head in the fragment table, optionally returning the key
523  * for it.
524  */
525 static fragment_head *
526 lookup_fd_head(reassembly_table *table, const packet_info *pinfo,
527                const guint32 id, const void *data, gpointer *orig_keyp)
528 {
529         gpointer key;
530         gpointer value;
531
532         /* Create key to search hash with */
533         key = table->temporary_key_func(pinfo, id, data);
534
535         /*
536          * Look up the reassembly in the fragment table.
537          */
538         if (!g_hash_table_lookup_extended(table->fragment_table, key, orig_keyp,
539                                           &value))
540                 value = NULL;
541         /* Free the key */
542         table->free_temporary_key_func(key);
543
544         return (fragment_head *)value;
545 }
546
547 /*
548  * Insert an fd_head into the fragment table, and return the key used.
549  */
550 static gpointer
551 insert_fd_head(reassembly_table *table, fragment_head *fd_head,
552                const packet_info *pinfo, const guint32 id, const void *data)
553 {
554         gpointer key;
555
556         /*
557          * We're going to use the key to insert the fragment,
558          * so make a persistent version of it.
559          */
560         key = table->persistent_key_func(pinfo, id, data);
561         g_hash_table_insert(table->fragment_table, key, fd_head);
562         return key;
563 }
564
565 /* This function cleans up the stored state and removes the reassembly data and
566  * (with one exception) all allocated memory for matching reassembly.
567  *
568  * The exception is :
569  * If the PDU was already completely reassembled, then the tvbuff containing the
570  * reassembled data WILL NOT be free()d, and the pointer to that tvbuff will be
571  * returned.
572  * Othervise the function will return NULL.
573  *
574  * So, if you call fragment_delete and it returns non-NULL, YOU are responsible
575  * to tvb_free() that tvbuff.
576  */
577 tvbuff_t *
578 fragment_delete(reassembly_table *table, const packet_info *pinfo,
579                 const guint32 id, const void *data)
580 {
581         fragment_head *fd_head;
582         fragment_item *fd;
583         tvbuff_t *fd_tvb_data=NULL;
584         gpointer key;
585
586         fd_head = lookup_fd_head(table, pinfo, id, data, &key);
587         if(fd_head==NULL){
588                 /* We do not recognize this as a PDU we have seen before. return */
589                 return NULL;
590         }
591
592         fd_tvb_data=fd_head->tvb_data; 
593         /* loop over all partial fragments and free any tvbuffs */
594         for(fd=fd_head->next;fd;){
595                 fragment_item *tmp_fd;
596                 tmp_fd=fd->next;
597
598                 if (fd->tvb_data && !(fd->flags & FD_SUBSET_TVB))
599                         tvb_free(fd->tvb_data);
600                 g_slice_free(fragment_item, fd);
601                 fd=tmp_fd;
602         }
603         g_slice_free(fragment_head, fd_head);
604         g_hash_table_remove(table->fragment_table, key);
605
606         return fd_tvb_data;
607 }
608
609 /* This function is used to check if there is partial or completed reassembly state
610  * matching this packet. I.e. Is there reassembly going on or not for this packet?
611  */
612 fragment_head *
613 fragment_get(reassembly_table *table, const packet_info *pinfo,
614              const guint32 id, const void *data)
615 {
616         return lookup_fd_head(table, pinfo, id, data, NULL);
617 }
618
619 /* id *must* be the frame number for this to work! */
620 fragment_head *
621 fragment_get_reassembled(reassembly_table *table, const guint32 id)
622 {
623         fragment_head *fd_head;
624         reassembled_key key;
625
626         /* create key to search hash with */
627         key.frame = id;
628         key.id = id;
629         fd_head = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &key);
630
631         return fd_head;
632 }
633
634 fragment_head *
635 fragment_get_reassembled_id(reassembly_table *table, const packet_info *pinfo,
636                             const guint32 id)
637 {
638         fragment_head *fd_head;
639         reassembled_key key;
640
641         /* create key to search hash with */
642         key.frame = pinfo->fd->num;
643         key.id = id;
644         fd_head = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &key);
645
646         return fd_head;
647 }
648
649 /* To specify the offset for the fragment numbering, the first fragment is added with 0, and
650  * afterwards this offset is set. All additional calls to off_seq_check will calculate
651  * the number in sequence in regards to the offset */
652 void
653 fragment_add_seq_offset(reassembly_table *table, const packet_info *pinfo, const guint32 id,
654                 const void *data, const guint32 fragment_offset)
655 {
656         fragment_head *fd_head;
657
658         fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
659         if (!fd_head)
660                 return;
661
662         /* Reseting the offset is not allowed */
663         if ( fd_head->fragment_nr_offset != 0 )
664                 return;
665
666         fd_head->fragment_nr_offset = fragment_offset;
667 }
668
669 /* This function can be used to explicitly set the total length (if known)
670  * for reassembly of a PDU.
671  * This is useful for reassembly of PDUs where one may have the total length specified
672  * in the first fragment instead of as for, say, IPv4 where a flag indicates which
673  * is the last fragment.
674  *
675  * Such protocols might fragment_add with a more_frags==TRUE for every fragment
676  * and just tell the reassembly engine the expected total length of the reassembled data
677  * using fragment_set_tot_len immediately after doing fragment_add for the first packet.
678  *
679  * Note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment.
680  * i.e. since the block numbers start at 0, if we specify tot_len==2, that
681  * actually means we want to defragment 3 blocks, block 0, 1 and 2.
682  */
683 void
684 fragment_set_tot_len(reassembly_table *table, const packet_info *pinfo,
685                      const guint32 id, const void *data, const guint32 tot_len)
686 {
687         fragment_head *fd_head;
688         fragment_item *fd;
689         guint32        max_offset = 0;
690
691         fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
692         if (!fd_head)
693                 return;
694
695         /* If we're setting a block sequence number, verify that it
696          * doesn't conflict with values set by existing fragments.
697          * XXX - eliminate this check?
698          */
699         fd = fd_head;
700         if (fd_head->flags & FD_BLOCKSEQUENCE) {
701                 while (fd) {
702                         if (fd->offset > max_offset) {
703                                 max_offset = fd->offset;
704                                 if (max_offset > tot_len) {
705                                         fd_head->error = "Bad total reassembly block count";
706                                         THROW_MESSAGE(ReassemblyError, fd_head->error);
707                                 }
708                         }
709                         fd = fd->next;
710                 }
711         }
712
713         if (fd_head->flags & FD_DEFRAGMENTED) {
714                 if (max_offset != tot_len) {
715                         fd_head->error = "Defragmented complete but total length not satisfied";
716                         THROW_MESSAGE(ReassemblyError, fd_head->error);
717                 }
718         }
719
720         /* We got this far so the value is sane. */
721         fd_head->datalen = tot_len;
722         fd_head->flags |= FD_DATALEN_SET;
723 }
724
725 guint32
726 fragment_get_tot_len(reassembly_table *table, const packet_info *pinfo,
727                      const guint32 id, const void *data)
728 {
729         fragment_head *fd_head;
730
731         fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
732
733         if(fd_head){
734                 return fd_head->datalen;
735         }
736
737         return 0;
738 }
739
740
741 /* This function will set the partial reassembly flag for a fh.
742    When this function is called, the fh MUST already exist, i.e.
743    the fh MUST be created by the initial call to fragment_add() before
744    this function is called.
745    Also note that this function MUST be called to indicate a fh will be
746    extended (increase the already stored data)
747 */
748
749 void
750 fragment_set_partial_reassembly(reassembly_table *table,
751                                 const packet_info *pinfo, const guint32 id,
752                                 const void *data)
753 {
754         fragment_head *fd_head;
755
756         fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
757
758         /*
759          * XXX - why not do all the stuff done early in "fragment_add_work()",
760          * turning off FD_DEFRAGMENTED and pointing the fragments' data
761          * pointers to the appropriate part of the already-reassembled
762          * data, and clearing the data length and "reassembled in" frame
763          * number, here?  We currently have a hack in the TCP dissector
764          * not to set the "reassembled in" value if the "partial reassembly"
765          * flag is set, so that in the first pass through the packets
766          * we don't falsely set a packet as reassembled in that packet
767          * if the dissector decided that even more reassembly was needed.
768          */
769         if(fd_head){
770                 fd_head->flags |= FD_PARTIAL_REASSEMBLY;
771         }
772 }
773
774 /*
775  * This function gets rid of an entry from a fragment table, given
776  * a pointer to the key for that entry.
777  *
778  * The key freeing routine will be called by g_hash_table_remove().
779  */
780 static void
781 fragment_unhash(reassembly_table *table, gpointer key)
782 {
783         /*
784          * Remove the entry from the fragment table.
785          */
786         g_hash_table_remove(table->fragment_table, key);
787 }
788
789 /*
790  * This function adds fragment_head structure to a reassembled-packet
791  * hash table, using the frame numbers of each of the frames from
792  * which it was reassembled as keys, and sets the "reassembled_in"
793  * frame number.
794  */
795 static void
796 fragment_reassembled(reassembly_table *table, fragment_head *fd_head,
797                      const packet_info *pinfo, const guint32 id)
798 {
799         reassembled_key *new_key;
800         fragment_item *fd;
801
802         if (fd_head->next == NULL) {
803                 /*
804                  * This was not fragmented, so there's no fragment
805                  * table; just hash it using the current frame number.
806                  */
807                 new_key = g_slice_new(reassembled_key);
808                 new_key->frame = pinfo->fd->num;
809                 new_key->id = id;
810                 g_hash_table_insert(table->reassembled_table, new_key, fd_head);
811         } else {
812                 /*
813                  * Hash it with the frame numbers for all the frames.
814                  */
815                 for (fd = fd_head->next; fd != NULL; fd = fd->next){
816                         new_key = g_slice_new(reassembled_key);
817                         new_key->frame = fd->frame;
818                         new_key->id = id;
819                         g_hash_table_insert(table->reassembled_table, new_key,
820                                 fd_head);
821                 }
822         }
823         fd_head->flags |= FD_DEFRAGMENTED;
824         fd_head->reassembled_in = pinfo->fd->num;
825 }
826
827 static void
828 LINK_FRAG(fragment_head *fd_head,fragment_item *fd)
829 {
830         fragment_item *fd_i;
831
832         /* add fragment to list, keep list sorted */
833         for(fd_i= fd_head; fd_i->next;fd_i=fd_i->next) {
834                 if (fd->offset < fd_i->next->offset )
835                         break;
836         }
837         fd->next=fd_i->next;
838         fd_i->next=fd;
839 }
840
841 /*
842  * This function adds a new fragment to the fragment hash table.
843  * If this is the first fragment seen for this datagram, a new entry
844  * is created in the hash table, otherwise this fragment is just added
845  * to the linked list of fragments for this packet.
846  * The list of fragments for a specific datagram is kept sorted for
847  * easier handling.
848  *
849  * Returns a pointer to the head of the fragment data list if we have all the
850  * fragments, NULL otherwise.
851  *
852  * This function assumes frag_offset being a byte offset into the defragment
853  * packet.
854  *
855  * 01-2002
856  * Once the fh is defragmented (= FD_DEFRAGMENTED set), it can be
857  * extended using the FD_PARTIAL_REASSEMBLY flag. This flag should be set
858  * using fragment_set_partial_reassembly() before calling fragment_add
859  * with the new fragment. FD_TOOLONGFRAGMENT and FD_MULTIPLETAILS flags
860  * are lowered when a new extension process is started.
861  */
862 static gboolean
863 fragment_add_work(fragment_head *fd_head, tvbuff_t *tvb, const int offset,
864                  const packet_info *pinfo, const guint32 frag_offset,
865                  const guint32 frag_data_len, const gboolean more_frags)
866 {
867         fragment_item *fd;
868         fragment_item *fd_i;
869         guint32 max, dfpos, fraglen;
870         tvbuff_t *old_tvb_data;
871         guint8 *data;
872
873         /* create new fd describing this fragment */
874         fd = g_slice_new(fragment_item);
875         fd->next = NULL;
876         fd->flags = 0;
877         fd->frame = pinfo->fd->num;
878         fd->offset = frag_offset;
879         fd->fragment_nr_offset = 0; /* will only be used with sequence */
880         fd->len  = frag_data_len;
881         fd->tvb_data = NULL;
882         fd->error = NULL;
883
884         /*
885          * Are we adding to an already-completed reassembly?
886          */
887         if (fd_head->flags & FD_DEFRAGMENTED) {
888                 /*
889                  * Yes.  Does this fragment go past the end of the results
890                  * of that reassembly?
891                  * XXX - shouldn't this be ">"?  If frag_offset + frag_data_len
892                  * == fd_head->datalen, this overlaps the end of the
893                  * reassembly, but doesn't go past it, right?
894                  */
895                 if (frag_offset + frag_data_len >= fd_head->datalen) {
896                         /*
897                          * Yes.  Have we been requested to continue reassembly?
898                          */
899                         if (fd_head->flags & FD_PARTIAL_REASSEMBLY) {
900                                 /*
901                                  * Yes.  Set flag in already empty fds &
902                                  * point old fds to malloc'ed data.
903                                  */
904                                 for(fd_i=fd_head->next; fd_i; fd_i=fd_i->next){
905                                         if( !fd_i->tvb_data ) {
906                                                 fd_i->tvb_data = tvb_new_subset_remaining(fd_head->tvb_data, fd_i->offset);
907                                                 fd_i->flags |= FD_SUBSET_TVB;
908                                         }
909                                         fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
910                                 }
911                                 fd_head->flags &= ~(FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY|FD_DATALEN_SET);
912                                 fd_head->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
913                                 fd_head->datalen=0;
914                                 fd_head->reassembled_in=0;
915                         } else {
916                                 /*
917                                  * No.  Bail out since we have no idea what to
918                                  * do with this fragment (and if we keep going
919                                  * we'll run past the end of a buffer sooner
920                                  * or later).
921                                  */
922                                 g_slice_free(fragment_item, fd);
923                                  
924                                 /*
925                                  * This is an attempt to add a fragment to a
926                                  * reassembly that had already completed.
927                                  * If it had no error, we don't want to
928                                  * mark it with an error, and if it had an
929                                  * error, we don't want to overwrite it, so
930                                  * we don't set fd_head->error.
931                                  */
932                                 if (frag_offset >= fd_head->datalen) {
933                                         /*
934                                          * The fragment starts past the end
935                                          * of the reassembled data.
936                                          */
937                                         THROW_MESSAGE(ReassemblyError, "New fragment past old data limits");
938                                 } else {
939                                         /*
940                                          * The fragment starts before the end
941                                          * of the reassembled data, but
942                                          * runs past the end.  That could
943                                          * just be a retransmission.
944                                          */
945                                         THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)");
946                                 }
947                         }
948                 } else {
949                         /*
950                          * No.  That means it still overlaps that, so report
951                          * this as a problem, possibly a retransmission.
952                          */
953                         g_slice_free(fragment_item, fd);
954                         THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)");
955                 }
956         }
957
958         /* Do this after we may have bailed out (above) so that we don't leave
959          * fd_head->frame in a bad state if we do */
960         if (fd->frame > fd_head->frame)
961                 fd_head->frame = fd->frame;
962
963         if (!more_frags) {
964                 /*
965                  * This is the tail fragment in the sequence.
966                  */
967                 if (fd_head->flags & FD_DATALEN_SET) {
968                         /* ok we have already seen other tails for this packet
969                          * it might be a duplicate.
970                          */
971                         if (fd_head->datalen != (fd->offset + fd->len) ){
972                                 /* Oops, this tail indicates a different packet
973                                  * len than the previous ones. Something's wrong.
974                                  */
975                                 fd->flags          |= FD_MULTIPLETAILS;
976                                 fd_head->flags |= FD_MULTIPLETAILS;
977                         }
978                 } else {
979                         /* This was the first tail fragment; now we know
980                          * what the length of the packet should be.
981                          */
982                         fd_head->datalen = fd->offset + fd->len;
983                         fd_head->flags |= FD_DATALEN_SET;
984                 }
985         }
986
987
988
989         /* If the packet is already defragmented, this MUST be an overlap.
990          * The entire defragmented packet is in fd_head->data.
991          * Even if we have previously defragmented this packet, we still
992          * check it. Someone might play overlap and TTL games.
993          */
994         if (fd_head->flags & FD_DEFRAGMENTED) {
995                 guint32 end_offset = fd->offset + fd->len;
996                 fd->flags          |= FD_OVERLAP;
997                 fd_head->flags |= FD_OVERLAP;
998                 /* make sure it's not too long */
999                 if (end_offset > fd_head->datalen || end_offset < fd->offset || end_offset < fd->len) {
1000                         fd->flags          |= FD_TOOLONGFRAGMENT;
1001                         fd_head->flags |= FD_TOOLONGFRAGMENT;
1002                 }
1003                 /* make sure it doesn't conflict with previous data */
1004                 else if ( tvb_memeql(fd_head->tvb_data, fd->offset,
1005                         tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
1006                         fd->flags          |= FD_OVERLAPCONFLICT;
1007                         fd_head->flags |= FD_OVERLAPCONFLICT;
1008                 }
1009                 /* it was just an overlap, link it and return */
1010                 LINK_FRAG(fd_head,fd);
1011                 return TRUE;
1012         }
1013
1014
1015
1016         /* If we have reached this point, the packet is not defragmented yet.
1017          * Save all payload in a buffer until we can defragment.
1018          * XXX - what if we didn't capture the entire fragment due
1019          * to a too-short snapshot length?
1020          */
1021         fd->tvb_data = tvb_clone_offset_len(tvb, offset, fd->len);
1022         LINK_FRAG(fd_head,fd);
1023
1024
1025         if( !(fd_head->flags & FD_DATALEN_SET) ){
1026                 /* if we dont know the datalen, there are still missing
1027                  * packets. Cheaper than the check below.
1028                  */
1029                 return FALSE;
1030         }
1031
1032
1033         /*
1034          * Check if we have received the entire fragment.
1035          * This is easy since the list is sorted and the head is faked.
1036          *
1037          * First, we compute the amount of contiguous data that's
1038          * available.  (The check for fd_i->offset <= max rules out
1039          * fragments that don't start before or at the end of the
1040          * previous fragment, i.e. fragments that have a gap between
1041          * them and the previous fragment.)
1042          */
1043         max = 0;
1044         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1045                 if ( ((fd_i->offset)<=max) &&
1046                         ((fd_i->offset+fd_i->len)>max) ){
1047                         max = fd_i->offset+fd_i->len;
1048                 }
1049         }
1050
1051         if (max < (fd_head->datalen)) {
1052                 /*
1053                  * The amount of contiguous data we have is less than the
1054                  * amount of data we're trying to reassemble, so we haven't
1055                  * received all packets yet.
1056                  */
1057                 return FALSE;
1058         }
1059
1060         /* we have received an entire packet, defragment it and
1061          * free all fragments
1062          */
1063         /* store old data just in case */
1064         old_tvb_data=fd_head->tvb_data;
1065         data = (guint8 *) g_malloc(fd_head->datalen);
1066         fd_head->tvb_data = tvb_new_real_data(data, fd_head->datalen, fd_head->datalen);
1067         tvb_set_free_cb(fd_head->tvb_data, g_free);
1068
1069         /* add all data fragments */
1070         for (dfpos=0,fd_i=fd_head;fd_i;fd_i=fd_i->next) {
1071                 if (fd_i->len) {
1072                         /*
1073                          * The loop above that calculates max also
1074                          * ensures that the only gaps that exist here
1075                          * are ones where a fragment starts past the
1076                          * end of the reassembled datagram, and there's
1077                          * a gap between the previous fragment and
1078                          * that fragment.
1079                          *
1080                          * A "DESEGMENT_UNTIL_FIN" was involved wherein the
1081                          * FIN packet had an offset less than the highest
1082                          * fragment offset seen. [Seen from a fuzz-test:
1083                          * bug #2470]).
1084                          *
1085                          * Note that the "overlap" compare must only be
1086                          * done for fragments with (offset+len) <= fd_head->datalen
1087                          * and thus within the newly g_malloc'd buffer.
1088                          */
1089                         if (fd_i->offset + fd_i->len > dfpos) {
1090                                 if (fd_i->offset >= fd_head->datalen) {
1091                                         /*
1092                                          * Fragment starts after the end
1093                                          * of the reassembled packet.
1094                                          *
1095                                          * This can happen if the length was
1096                                          * set after the offending fragment
1097                                          * was added to the reassembly.
1098                                          *
1099                                          * Flag this fragment, but don't
1100                                          * try to extract any data from
1101                                          * it, as there's no place to put
1102                                          * it.
1103                                          *
1104                                          * XXX - add different flag value
1105                                          * for this.
1106                                          */
1107                                         fd_i->flags    |= FD_TOOLONGFRAGMENT;
1108                                         fd_head->flags |= FD_TOOLONGFRAGMENT;
1109                                 } else if (dfpos < fd_i->offset) {
1110                                         /*
1111                                          * XXX - can this happen?  We've
1112                                          * already rejected fragments that
1113                                          * start past the end of the
1114                                          * reassembled datagram, and
1115                                          * the loop that calculated max
1116                                          * should have ruled out gaps,
1117                                          * but could fd_i->offset +
1118                                          * fd_i->len overflow?
1119                                          */
1120                                         fd_head->error = "dfpos < offset";
1121                                 } else if (dfpos - fd_i->offset > fd_i->len)
1122                                         fd_head->error = "dfpos - offset > len";
1123                                 else if (!fd_head->tvb_data)
1124                                         fd_head->error = "no data";
1125                                 else {
1126                                         fraglen = fd_i->len;
1127                                         if (fd_i->offset + fraglen > fd_head->datalen) {
1128                                                 /*
1129                                                  * Fragment goes past the end
1130                                                  * of the packet, as indicated
1131                                                  * by the last fragment.
1132                                                  *
1133                                                  * This can happen if the
1134                                                  * length was set after the
1135                                                  * offending fragment was
1136                                                  * added to the reassembly.
1137                                                  *
1138                                                  * Mark it as such, and only
1139                                                  * copy from it what fits in
1140                                                  * the packet.
1141                                                  */
1142                                                 fd_i->flags    |= FD_TOOLONGFRAGMENT;
1143                                                 fd_head->flags |= FD_TOOLONGFRAGMENT;
1144                                                 fraglen = fd_head->datalen - fd_i->offset;
1145                                         }
1146                                         if (fd_i->offset < dfpos) {
1147                                                 guint32 cmp_len = MIN(fd_i->len,(dfpos-fd_i->offset));
1148
1149                                                 fd_i->flags    |= FD_OVERLAP;
1150                                                 fd_head->flags |= FD_OVERLAP;
1151                                                 if ( memcmp(data + fd_i->offset,
1152                                                                 tvb_get_ptr(fd_i->tvb_data, 0, cmp_len),
1153                                                                 cmp_len)
1154                                                                  ) {
1155                                                         fd_i->flags    |= FD_OVERLAPCONFLICT;
1156                                                         fd_head->flags |= FD_OVERLAPCONFLICT;
1157                                                 }
1158                                         }
1159                                         if (fraglen < dfpos - fd_i->offset) {
1160                                                 /*
1161                                                  * XXX - can this happen?
1162                                                  */
1163                                                 fd_head->error = "fraglen < dfpos - offset";
1164                                         } else {
1165                                                 memcpy(data+dfpos,
1166                                                         tvb_get_ptr(fd_i->tvb_data, (dfpos-fd_i->offset), fraglen-(dfpos-fd_i->offset)),
1167                                                         fraglen-(dfpos-fd_i->offset));
1168                                                 dfpos=MAX(dfpos, (fd_i->offset + fraglen));
1169                                         }
1170                                 }
1171                         } else {
1172                                 if (fd_i->offset + fd_i->len < fd_i->offset) {
1173                                         /* Integer overflow? */
1174                                         fd_head->error = "offset + len < offset";
1175                                 }
1176                         }
1177
1178                         if (fd_i->flags & FD_SUBSET_TVB)
1179                                 fd_i->flags &= ~FD_SUBSET_TVB;
1180                         else if (fd_i->tvb_data)
1181                                 tvb_free(fd_i->tvb_data);
1182
1183                         fd_i->tvb_data=NULL;
1184                 }
1185         }
1186
1187         if (old_tvb_data)
1188                 tvb_add_to_chain(tvb, old_tvb_data);
1189         /* mark this packet as defragmented.
1190            allows us to skip any trailing fragments */
1191         fd_head->flags |= FD_DEFRAGMENTED;
1192         fd_head->reassembled_in=pinfo->fd->num;
1193
1194         /* we don't throw until here to avoid leaking old_data and others */
1195         if (fd_head->error) {
1196                 THROW_MESSAGE(ReassemblyError, fd_head->error);
1197         }
1198
1199         return TRUE;
1200 }
1201
1202 static fragment_head *
1203 fragment_add_common(reassembly_table *table, tvbuff_t *tvb, const int offset,
1204                     const packet_info *pinfo, const guint32 id,
1205                     const void *data, const guint32 frag_offset,
1206                     const guint32 frag_data_len, const gboolean more_frags,
1207                     const gboolean check_already_added)
1208 {
1209         fragment_head *fd_head;
1210         fragment_item *fd_item;
1211         gboolean already_added;
1212
1213
1214         /* dissector shouldn't give us garbage tvb info */
1215         DISSECTOR_ASSERT(tvb_bytes_exist(tvb, offset, frag_data_len));
1216
1217         fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
1218
1219 #if 0
1220         /* debug output of associated fragments. */
1221         /* leave it here for future debugging sessions */
1222         if(strcmp(pinfo->current_proto, "DCERPC") == 0) {
1223                 printf("proto:%s num:%u id:%u offset:%u len:%u more:%u visited:%u\n",
1224                         pinfo->current_proto, pinfo->fd->num, id, frag_offset, frag_data_len, more_frags, pinfo->fd->flags.visited);
1225                 if(fd_head != NULL) {
1226                         for(fd_item=fd_head->next;fd_item;fd_item=fd_item->next){
1227                                 printf("fd_frame:%u fd_offset:%u len:%u datalen:%u\n",
1228                                         fd_item->frame, fd_item->offset, fd_item->len, fd_item->datalen);
1229                         }
1230                 }
1231         }
1232 #endif
1233
1234         /*
1235          * Is this the first pass through the capture?
1236          */
1237         if (!pinfo->fd->flags.visited) {
1238                 /*
1239                  * Yes, so we could be doing reassembly.  If
1240                  * "check_already_added" is true, and fd_head is non-null,
1241                  * meaning that this fragment would be added to an
1242                  * in-progress reassembly, check if we have seen this
1243                  * fragment before, i.e., if we have already added it to
1244                  * that reassembly. That can be true even on the first pass
1245                  * since we sometimes might call a subdissector multiple
1246                  * times.
1247                  *
1248                  * We check both the frame number and the fragment offset,
1249                  * so that we support multiple fragments from the same
1250                  * frame being added to the same reassembled PDU.
1251                  */
1252                 if (check_already_added && fd_head != NULL) {
1253                         /*
1254                          * fd_head->frame is the maximum of the frame
1255                          * numbers of all the fragments added to this
1256                          * reassembly; if this frame is later than that
1257                          * frame, we know it hasn't been added yet.
1258                          */
1259                         if (pinfo->fd->num <= fd_head->frame) {
1260                                 already_added = FALSE;
1261                                 /*
1262                                  * The first item in the reassembly list
1263                                  * is not a fragment, it's a data structure
1264                                  * for the reassembled packet, so we
1265                                  * start checking with the next item.
1266                                  */
1267                                 for (fd_item = fd_head->next; fd_item;
1268                                     fd_item = fd_item->next) {
1269                                         if (pinfo->fd->num == fd_item->frame &&
1270                                             frag_offset == fd_item->offset) {
1271                                                 already_added = TRUE;
1272                                                 break;
1273                                         }
1274                                 }
1275                                 if (already_added) {
1276                                         /*
1277                                          * Have we already finished
1278                                          * reassembling?
1279                                          */
1280                                         if (fd_head->flags & FD_DEFRAGMENTED) {
1281                                                 /*
1282                                                  * Yes.
1283                                                  * XXX - can this ever happen?
1284                                                  */
1285                                                 THROW_MESSAGE(ReassemblyError,
1286                                                     "Frame already added in first pass");
1287                                         } else {
1288                                                 /*
1289                                                  * No.
1290                                                  */
1291                                                 return NULL;
1292                                         }
1293                                 }
1294                         }
1295                 }
1296         } else {
1297                 /*
1298                  * No, so we've already done all the reassembly and added
1299                  * all the fragments.  Do we have a reassembly and, if so,
1300                  * have we finished reassembling?
1301                  */
1302                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
1303                         /*
1304                          * Yes.  This is probably being done after the
1305                          * first pass, and we've already done the work
1306                          * on the first pass.
1307                          *
1308                          * If the reassembly got a fatal error, throw that
1309                          * error again.
1310                          */
1311                         if (fd_head->error)
1312                                 THROW_MESSAGE(ReassemblyError, fd_head->error);
1313
1314                         /*
1315                          * Is it later in the capture than all of the
1316                          * fragments in the reassembly?
1317                          */
1318                         if (pinfo->fd->num > fd_head->frame) {
1319                                 /*
1320                                  * Yes, so report this as a problem,
1321                                  * possibly a retransmission.
1322                                  */
1323                                 THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)");
1324                         }
1325
1326                         /*
1327                          * Does this fragment go past the end of the
1328                          * results of that reassembly?
1329                          */
1330                         if (frag_offset + frag_data_len > fd_head->datalen) {
1331                                 /*
1332                                  * Yes.
1333                                  */
1334                                 if (frag_offset >= fd_head->datalen) {
1335                                         /*
1336                                          * The fragment starts past the
1337                                          * end of the reassembled data.
1338                                          */
1339                                         THROW_MESSAGE(ReassemblyError, "New fragment past old data limits");
1340                                 } else {
1341                                         /*
1342                                          * The fragment starts before the end
1343                                          * of the reassembled data, but
1344                                          * runs past the end.  That could
1345                                          * just be a retransmission.
1346                                          */
1347                                         THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)");
1348                                 }
1349                         }
1350
1351                         return fd_head;
1352                 } else {
1353                         /*
1354                          * No.
1355                          */
1356                         return NULL;
1357                 }
1358         }
1359
1360         if (fd_head==NULL){
1361                 /* not found, this must be the first snooped fragment for this
1362                  * packet. Create list-head.
1363                  */
1364                 fd_head = new_head(0);
1365
1366                 /*
1367                  * Insert it into the hash table.
1368                  */
1369                 insert_fd_head(table, fd_head, pinfo, id, data);
1370         }
1371
1372         if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
1373                 frag_data_len, more_frags)) {
1374                 /*
1375                  * Reassembly is complete.
1376                  */
1377                 return fd_head;
1378         } else {
1379                 /*
1380                  * Reassembly isn't complete.
1381                  */
1382                 return NULL;
1383         }
1384 }
1385
1386 fragment_head *
1387 fragment_add(reassembly_table *table, tvbuff_t *tvb, const int offset,
1388              const packet_info *pinfo, const guint32 id, const void *data,
1389              const guint32 frag_offset, const guint32 frag_data_len,
1390              const gboolean more_frags)
1391 {
1392         return fragment_add_common(table, tvb, offset, pinfo, id, data,
1393                 frag_offset, frag_data_len, more_frags, TRUE);
1394 }
1395
1396 /*
1397  * For use when you can have multiple fragments in the same frame added
1398  * to the same reassembled PDU, e.g. with ONC RPC-over-TCP.
1399  */
1400 fragment_head *
1401 fragment_add_multiple_ok(reassembly_table *table, tvbuff_t *tvb,
1402                          const int offset, const packet_info *pinfo,
1403                          const guint32 id, const void *data,
1404                          const guint32 frag_offset,
1405                          const guint32 frag_data_len, const gboolean more_frags)
1406 {
1407         return fragment_add_common(table, tvb, offset, pinfo, id, data,
1408                 frag_offset, frag_data_len, more_frags, FALSE);
1409 }
1410
1411 fragment_head *
1412 fragment_add_check(reassembly_table *table, tvbuff_t *tvb, const int offset,
1413                    const packet_info *pinfo, const guint32 id,
1414                    const void *data, const guint32 frag_offset,
1415                    const guint32 frag_data_len, const gboolean more_frags)
1416 {
1417         reassembled_key reass_key;
1418         fragment_head *fd_head;
1419         gpointer orig_key;
1420
1421         /*
1422          * If this isn't the first pass, look for this frame in the table
1423          * of reassembled packets.
1424          */
1425         if (pinfo->fd->flags.visited) {
1426                 reass_key.frame = pinfo->fd->num;
1427                 reass_key.id = id;
1428                 return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
1429         }
1430
1431         /* Looks up a key in the GHashTable, returning the original key and the associated value
1432          * and a gboolean which is TRUE if the key was found. This is useful if you need to free
1433          * the memory allocated for the original key, for example before calling g_hash_table_remove()
1434          */
1435         fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
1436         if (fd_head == NULL) {
1437                 /* not found, this must be the first snooped fragment for this
1438                  * packet. Create list-head.
1439                  */
1440                 fd_head = new_head(0);
1441
1442                 /*
1443                  * Save the key, for unhashing it later.
1444                  */
1445                 orig_key = insert_fd_head(table, fd_head, pinfo, id, data);
1446         }
1447
1448         /*
1449          * If this is a short frame, then we can't, and don't, do
1450          * reassembly on it.  We just give up.
1451          */
1452         if (tvb_reported_length(tvb) > tvb_length(tvb))
1453                 return NULL;
1454
1455         if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset,
1456                 frag_data_len, more_frags)) {
1457                 /*
1458                  * Reassembly is complete.
1459                  * Remove this from the table of in-progress
1460                  * reassemblies, add it to the table of
1461                  * reassembled packets, and return it.
1462                  */
1463
1464                 /*
1465                  * Remove this from the table of in-progress reassemblies,
1466                  * and free up any memory used for it in that table.
1467                  */
1468                 fragment_unhash(table, orig_key);
1469
1470                 /*
1471                  * Add this item to the table of reassembled packets.
1472                  */
1473                 fragment_reassembled(table, fd_head, pinfo, id);
1474                 return fd_head;
1475         } else {
1476                 /*
1477                  * Reassembly isn't complete.
1478                  */
1479                 return NULL;
1480         }
1481 }
1482
1483 static void
1484 fragment_defragment_and_free (fragment_head *fd_head, const packet_info *pinfo)
1485 {
1486         fragment_item *fd_i = NULL;
1487         fragment_item *last_fd = NULL;
1488         guint32  dfpos = 0, size = 0;
1489         tvbuff_t *old_tvb_data = NULL;
1490         guint8 *data;
1491
1492         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1493                 if(!last_fd || last_fd->offset!=fd_i->offset){
1494                         size+=fd_i->len;
1495                 }
1496                 last_fd=fd_i;
1497         }
1498
1499         /* store old data in case the fd_i->data pointers refer to it */
1500         old_tvb_data=fd_head->tvb_data;
1501         data = (guint8 *) g_malloc(size);
1502         fd_head->tvb_data = tvb_new_real_data(data, size, size);
1503         tvb_set_free_cb(fd_head->tvb_data, g_free);
1504         fd_head->len = size;            /* record size for caller       */
1505
1506         /* add all data fragments */
1507         last_fd=NULL;
1508         for (fd_i=fd_head->next; fd_i; fd_i=fd_i->next) {
1509                 if (fd_i->len) {
1510                         if(!last_fd || last_fd->offset != fd_i->offset) {
1511                                 /* First fragment or in-sequence fragment */
1512                                 memcpy(data+dfpos, tvb_get_ptr(fd_i->tvb_data, 0, fd_i->len), fd_i->len);
1513                                 dfpos += fd_i->len;
1514                         } else {
1515                                 /* duplicate/retransmission/overlap */
1516                                 fd_i->flags    |= FD_OVERLAP;
1517                                 fd_head->flags |= FD_OVERLAP;
1518                                 if(last_fd->len != fd_i->len
1519                                    || tvb_memeql(last_fd->tvb_data, 0, tvb_get_ptr(fd_i->tvb_data, 0, last_fd->len), last_fd->len) ) {
1520                                         fd_i->flags    |= FD_OVERLAPCONFLICT;
1521                                         fd_head->flags |= FD_OVERLAPCONFLICT;
1522                                 }
1523                         }
1524                 }
1525                 last_fd=fd_i;
1526         }
1527
1528         /* we have defragmented the pdu, now free all fragments*/
1529         for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1530                 if (fd_i->flags & FD_SUBSET_TVB)
1531                         fd_i->flags &= ~FD_SUBSET_TVB;
1532                 else if (fd_i->tvb_data)
1533                         tvb_free(fd_i->tvb_data);
1534                 fd_i->tvb_data=NULL;
1535         }
1536         if (old_tvb_data)
1537                 tvb_free(old_tvb_data);
1538
1539         /* mark this packet as defragmented.
1540          * allows us to skip any trailing fragments.
1541          */
1542         fd_head->flags |= FD_DEFRAGMENTED;
1543         fd_head->reassembled_in=pinfo->fd->num;
1544 }
1545
1546 /*
1547  * This function adds a new fragment to the entry for a reassembly
1548  * operation.
1549  *
1550  * The list of fragments for a specific datagram is kept sorted for
1551  * easier handling.
1552  *
1553  * Returns TRUE if we have all the fragments, FALSE otherwise.
1554  *
1555  * This function assumes frag_number being a block sequence number.
1556  * The bsn for the first block is 0.
1557  */
1558 static gboolean
1559 fragment_add_seq_work(fragment_head *fd_head, tvbuff_t *tvb, const int offset,
1560                  const packet_info *pinfo, const guint32 frag_number,
1561                  const guint32 frag_data_len, const gboolean more_frags)
1562 {
1563         fragment_item *fd;
1564         fragment_item *fd_i;
1565         fragment_item *last_fd;
1566         guint32 max, dfpos;
1567         guint32 frag_number_work;
1568
1569         /* Enables the use of fragment sequence numbers, which do not start with 0 */
1570         frag_number_work = frag_number;
1571         if ( fd_head->fragment_nr_offset != 0 )
1572                 if ( frag_number_work >= fd_head->fragment_nr_offset )
1573                         frag_number_work = frag_number - fd_head->fragment_nr_offset;
1574
1575         /* if the partial reassembly flag has been set, and we are extending
1576          * the pdu, un-reassemble the pdu. This means pointing old fds to malloc'ed data.
1577          */
1578         if(fd_head->flags & FD_DEFRAGMENTED && frag_number_work >= fd_head->datalen &&
1579                 fd_head->flags & FD_PARTIAL_REASSEMBLY){
1580                 guint32 lastdfpos = 0;
1581                 dfpos = 0;
1582                 for(fd_i=fd_head->next; fd_i; fd_i=fd_i->next){
1583                         if( !fd_i->tvb_data ) {
1584                                 if( fd_i->flags & FD_OVERLAP ) {
1585                                         /* this is a duplicate of the previous
1586                                          * fragment. */
1587                                         fd_i->tvb_data = tvb_new_subset_remaining(fd_head->tvb_data, lastdfpos);
1588                                 } else {
1589                                         fd_i->tvb_data = tvb_new_subset_remaining(fd_head->tvb_data, dfpos);
1590                                         lastdfpos = dfpos;
1591                                         dfpos += fd_i->len;
1592                                 }
1593                                 fd_i->flags |= FD_SUBSET_TVB;
1594                         }
1595                         fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
1596                 }
1597                 fd_head->flags &= ~(FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY|FD_DATALEN_SET);
1598                 fd_head->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS);
1599                 fd_head->datalen=0;
1600                 fd_head->reassembled_in=0;
1601         }
1602
1603
1604         /* create new fd describing this fragment */
1605         fd = g_slice_new(fragment_item);
1606         fd->next = NULL;
1607         fd->flags = 0;
1608         fd->frame = pinfo->fd->num;
1609         fd->offset = frag_number_work;
1610         fd->len  = frag_data_len;
1611         fd->tvb_data = NULL;
1612         fd->error = NULL;
1613
1614         if (!more_frags) {
1615                 /*
1616                  * This is the tail fragment in the sequence.
1617                  */
1618                 if (fd_head->flags&FD_DATALEN_SET) {
1619                         /* ok we have already seen other tails for this packet
1620                          * it might be a duplicate.
1621                          */
1622                         if (fd_head->datalen != fd->offset ){
1623                                 /* Oops, this tail indicates a different packet
1624                                  * len than the previous ones. Something's wrong.
1625                                  */
1626                                 fd->flags       |= FD_MULTIPLETAILS;
1627                                 fd_head->flags  |= FD_MULTIPLETAILS;
1628                         }
1629                 } else {
1630                         /* this was the first tail fragment, now we know the
1631                          * sequence number of that fragment (which is NOT
1632                          * the length of the packet!)
1633                          */
1634                         fd_head->datalen = fd->offset;
1635                         fd_head->flags |= FD_DATALEN_SET;
1636                 }
1637         }
1638
1639         /* If the packet is already defragmented, this MUST be an overlap.
1640          * The entire defragmented packet is in fd_head->data
1641          * Even if we have previously defragmented this packet, we still check
1642          * check it. Someone might play overlap and TTL games.
1643          */
1644         if (fd_head->flags & FD_DEFRAGMENTED) {
1645                 fd->flags       |= FD_OVERLAP;
1646                 fd_head->flags  |= FD_OVERLAP;
1647
1648                 /* make sure it's not past the end */
1649                 if (fd->offset > fd_head->datalen) {
1650                         /* new fragment comes after the end */
1651                         fd->flags       |= FD_TOOLONGFRAGMENT;
1652                         fd_head->flags  |= FD_TOOLONGFRAGMENT;
1653                         LINK_FRAG(fd_head,fd);
1654                         return TRUE;
1655                 }
1656                 /* make sure it doesn't conflict with previous data */
1657                 dfpos=0;
1658                 last_fd=NULL;
1659                 for (fd_i=fd_head->next;fd_i && (fd_i->offset!=fd->offset);fd_i=fd_i->next) {
1660                   if (!last_fd || last_fd->offset!=fd_i->offset){
1661                         dfpos += fd_i->len;
1662                   }
1663                   last_fd=fd_i;
1664                 }
1665                 if(fd_i){
1666                         /* new fragment overlaps existing fragment */
1667                         if(fd_i->len!=fd->len){
1668                                 /*
1669                                  * They have different lengths; this
1670                                  * is definitely a conflict.
1671                                  */
1672                                 fd->flags       |= FD_OVERLAPCONFLICT;
1673                                 fd_head->flags  |= FD_OVERLAPCONFLICT;
1674                                 LINK_FRAG(fd_head,fd);
1675                                 return TRUE;
1676                         }
1677                         DISSECTOR_ASSERT(fd_head->len >= dfpos + fd->len);
1678                         if (tvb_memeql(fd_head->tvb_data, dfpos,
1679                                 tvb_get_ptr(tvb,offset,fd->len),fd->len) ){
1680                                 /*
1681                                  * They have the same length, but the
1682                                  * data isn't the same.
1683                                  */
1684                                 fd->flags       |= FD_OVERLAPCONFLICT;
1685                                 fd_head->flags  |= FD_OVERLAPCONFLICT;
1686                                 LINK_FRAG(fd_head,fd);
1687                                 return TRUE;
1688                         }
1689                         /* it was just an overlap, link it and return */
1690                         LINK_FRAG(fd_head,fd);
1691                         return TRUE;
1692                 } else {
1693                         /*
1694                          * New fragment doesn't overlap an existing
1695                          * fragment - there was presumably a gap in
1696                          * the sequence number space.
1697                          *
1698                          * XXX - what should we do here?  Is it always
1699                          * the case that there are no gaps, or are there
1700                          * protcols using sequence numbers where there
1701                          * can be gaps?
1702                          *
1703                          * If the former, the check below for having
1704                          * received all the fragments should check for
1705                          * holes in the sequence number space and for the
1706                          * first sequence number being 0.  If we do that,
1707                          * the only way we can get here is if this fragment
1708                          * is past the end of the sequence number space -
1709                          * but the check for "fd->offset > fd_head->datalen"
1710                          * would have caught that above, so it can't happen.
1711                          *
1712                          * If the latter, we don't have a good way of
1713                          * knowing whether reassembly is complete if we
1714                          * get packet out of order such that the "last"
1715                          * fragment doesn't show up last - but, unless
1716                          * in-order reliable delivery of fragments is
1717                          * guaranteed, an implementation of the protocol
1718                          * has no way of knowing whether reassembly is
1719                          * complete, either.
1720                          *
1721                          * For now, we just link the fragment in and
1722                          * return.
1723                          */
1724                         LINK_FRAG(fd_head,fd);
1725                         return TRUE;
1726                 }
1727         }
1728
1729         /* If we have reached this point, the packet is not defragmented yet.
1730          * Save all payload in a buffer until we can defragment.
1731          * XXX - what if we didn't capture the entire fragment due
1732          * to a too-short snapshot length?
1733          */
1734         /* check len, there may be a fragment with 0 len, that is actually the tail */
1735         if (fd->len) {
1736                 fd->tvb_data = tvb_clone_offset_len(tvb, offset, fd->len);
1737         }
1738         LINK_FRAG(fd_head,fd);
1739
1740
1741         if( !(fd_head->flags & FD_DATALEN_SET) ){
1742                 /* if we dont know the sequence number of the last fragment,
1743                  * there are definitely still missing packets. Cheaper than
1744                  * the check below.
1745                  */
1746                 return FALSE;
1747         }
1748
1749
1750         /* check if we have received the entire fragment
1751          * this is easy since the list is sorted and the head is faked.
1752          * common case the whole list is scanned.
1753          */
1754         max = 0;
1755         for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) {
1756           if ( fd_i->offset==max ){
1757                 max++;
1758           }
1759         }
1760         /* max will now be datalen+1 if all fragments have been seen */
1761
1762         if (max <= fd_head->datalen) {
1763                 /* we have not received all packets yet */
1764                 return FALSE;
1765         }
1766
1767
1768         if (max > (fd_head->datalen+1)) {
1769                 /* oops, too long fragment detected */
1770                 fd->flags       |= FD_TOOLONGFRAGMENT;
1771                 fd_head->flags  |= FD_TOOLONGFRAGMENT;
1772         }
1773
1774
1775         /* we have received an entire packet, defragment it and
1776          * free all fragments
1777          */
1778         fragment_defragment_and_free(fd_head, pinfo);
1779
1780         return TRUE;
1781 }
1782
1783 /*
1784  * This function adds a new fragment to the fragment hash table.
1785  * If this is the first fragment seen for this datagram, a new entry
1786  * is created in the hash table, otherwise this fragment is just added
1787  * to the linked list of fragments for this packet.
1788  *
1789  * Returns a pointer to the head of the fragment data list if we have all the
1790  * fragments, NULL otherwise.
1791  *
1792  * This function assumes frag_number being a block sequence number.
1793  * The bsn for the first block is 0.
1794  */
1795 static fragment_head *
1796 fragment_add_seq_common(reassembly_table *table, tvbuff_t *tvb,
1797                         const int offset, const packet_info *pinfo,
1798                         const guint32 id, const void *data, 
1799                         guint32 frag_number, const guint32 frag_data_len,
1800                         const gboolean more_frags, const guint32 flags,
1801                         gpointer *orig_keyp)
1802 {
1803         fragment_head *fd_head;
1804         gpointer orig_key;
1805
1806         fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
1807
1808         /* have we already seen this frame ?*/
1809         if (pinfo->fd->flags.visited) {
1810                 if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) {
1811                         if (orig_keyp != NULL)
1812                                 *orig_keyp = orig_key;
1813                         return fd_head;
1814                 } else {
1815                         return NULL;
1816                 }
1817         }
1818
1819         if (fd_head==NULL){
1820                 /* not found, this must be the first snooped fragment for this
1821                  * packet. Create list-head.
1822                  */
1823                 fd_head= new_head(FD_BLOCKSEQUENCE);
1824
1825                 if((flags & (REASSEMBLE_FLAGS_NO_FRAG_NUMBER|REASSEMBLE_FLAGS_802_11_HACK))
1826                    && !more_frags) {
1827                         /*
1828                          * This is the last fragment for this packet, and
1829                          * is the only one we've seen.
1830                          *
1831                          * Either we don't have sequence numbers, in which
1832                          * case we assume this is the first fragment for
1833                          * this packet, or we're doing special 802.11
1834                          * processing, in which case we assume it's one
1835                          * of those reassembled packets with a non-zero
1836                          * fragment number (see packet-80211.c); just
1837                          * return a pointer to the head of the list;
1838                          * fragment_add_seq_check will then add it to the table
1839                          * of reassembled packets.
1840                          */
1841                         if (orig_keyp != NULL)
1842                                 *orig_keyp = NULL;
1843                         fd_head->reassembled_in=pinfo->fd->num;
1844                         return fd_head;
1845                 }
1846
1847                 orig_key = insert_fd_head(table, fd_head, pinfo, id, data);
1848                 if (orig_keyp != NULL)
1849                         *orig_keyp = orig_key;
1850
1851                 /*
1852                  * If we weren't given an initial fragment number,
1853                  * make it 0.
1854                  */
1855                 if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER)
1856                         frag_number = 0;
1857         } else {
1858                 if (orig_keyp != NULL)
1859                         *orig_keyp = orig_key;
1860
1861                 if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER) {
1862                         fragment_item *fd;
1863                         /*
1864                          * If we weren't given an initial fragment number,
1865                          * use the next expected fragment number as the fragment
1866                          * number for this fragment.
1867                          */
1868                         for (fd = fd_head; fd != NULL; fd = fd->next) {
1869                                 if (fd->next == NULL)
1870                                         frag_number = fd->offset + 1;
1871                         }
1872                 }
1873         }
1874
1875         /*
1876          * XXX I've copied this over from the old separate
1877          * fragment_add_seq_check_work, but I'm not convinced it's doing the
1878          * right thing -- rav
1879          *
1880          * If we don't have all the data that is in this fragment,
1881          * then we can't, and don't, do reassembly on it.
1882          *
1883          * If it's the first frame, handle it as an unfragmented packet.
1884          * Otherwise, just handle it as a fragment.
1885          *
1886          * If "more_frags" isn't set, we get rid of the entry in the
1887          * hash table for this reassembly, as we don't need it any more.
1888          */
1889         if ((flags & REASSEMBLE_FLAGS_CHECK_DATA_PRESENT) &&
1890                 !tvb_bytes_exist(tvb, offset, frag_data_len)) {
1891                 fd_head -> flags |= FD_DATA_NOT_PRESENT;
1892                 if (frag_number == 0) {
1893                         return fd_head;
1894                 }
1895                 else {
1896                         if (!more_frags) {
1897                                 /*
1898                                  * Remove this from the table of in-progress
1899                                  * reassemblies, and free up any memory used for
1900                                  * it in that table.
1901                                  */
1902                                 fragment_unhash(table, *orig_keyp);
1903                                 free_all_fragments(NULL, fd_head, NULL);
1904                         }
1905                         return NULL;
1906                 }
1907         }
1908
1909         if (fragment_add_seq_work(fd_head, tvb, offset, pinfo,
1910                                   frag_number, frag_data_len, more_frags)) {
1911                 /*
1912                  * Reassembly is complete.
1913                  */
1914                 return fd_head;
1915         } else {
1916                 /*
1917                  * Reassembly isn't complete.
1918                  */
1919                 return NULL;
1920         }
1921 }
1922
1923 fragment_head *
1924 fragment_add_seq(reassembly_table *table, tvbuff_t *tvb, const int offset,
1925                  const packet_info *pinfo, const guint32 id, const void *data, 
1926                  const guint32 frag_number, const guint32 frag_data_len,
1927                  const gboolean more_frags, const guint32 flags)
1928 {
1929         return fragment_add_seq_common(table, tvb, offset, pinfo, id, data,
1930                                        frag_number, frag_data_len,
1931                                        more_frags, flags, NULL);
1932 }
1933
1934 /*
1935  * This does the work for "fragment_add_seq_check()" and
1936  * "fragment_add_seq_next()".
1937  *
1938  * This function assumes frag_number being a block sequence number.
1939  * The bsn for the first block is 0.
1940  *
1941  * If "no_frag_number" is TRUE, it uses the next expected fragment number
1942  * as the fragment number if there is a reassembly in progress, otherwise
1943  * it uses 0.
1944  *
1945  * If "no_frag_number" is FALSE, it uses the "frag_number" argument as
1946  * the fragment number.
1947  *
1948  * If this is the first fragment seen for this datagram, a new
1949  * "fragment_head" structure is allocated to refer to the reassembled
1950  * packet.
1951  *
1952  * This fragment is added to the linked list of fragments for this packet.
1953  *
1954  * If "more_frags" is false and REASSEMBLE_FLAGS_802_11_HACK (as the name
1955  * implies, a special hack for 802.11) or REASSEMBLE_FLAGS_NO_FRAG_NUMBER
1956  * (implying messages must be in order since there's no sequence number) are
1957  * set in "flags", then this (one element) list is returned.
1958  *
1959  * If, after processing this fragment, we have all the fragments,
1960  * "fragment_add_seq_check_work()" removes that from the fragment hash
1961  * table if necessary and adds it to the table of reassembled fragments,
1962  * and returns a pointer to the head of the fragment list.
1963  *
1964  * Otherwise, it returns NULL.
1965  *
1966  * XXX - Should we simply return NULL for zero-length fragments?
1967  */
1968 static fragment_head *
1969 fragment_add_seq_check_work(reassembly_table *table, tvbuff_t *tvb,
1970                             const int offset, const packet_info *pinfo,
1971                             const guint32 id, const void *data, 
1972                             const guint32 frag_number,
1973                             const guint32 frag_data_len,
1974                             const gboolean more_frags, const guint32 flags)
1975 {
1976         reassembled_key reass_key;
1977         fragment_head *fd_head;
1978         gpointer orig_key;
1979
1980         /*
1981          * Have we already seen this frame?
1982          * If so, look for it in the table of reassembled packets.
1983          */
1984         if (pinfo->fd->flags.visited) {
1985                 reass_key.frame = pinfo->fd->num;
1986                 reass_key.id = id;
1987                 return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
1988         }
1989
1990         fd_head = fragment_add_seq_common(table, tvb, offset, pinfo, id, data, 
1991                                           frag_number, frag_data_len,
1992                                           more_frags,
1993                                           flags|REASSEMBLE_FLAGS_CHECK_DATA_PRESENT,
1994                                           &orig_key);
1995         if (fd_head) {
1996                 if(fd_head->flags & FD_DATA_NOT_PRESENT) {
1997                         /* this is the first fragment of a datagram with
1998                          * truncated fragments. Don't move it to the
1999                          * reassembled table. */
2000                         return fd_head;
2001                 }
2002
2003                 /*
2004                  * Reassembly is complete.
2005                  *
2006                  * If this is in the table of in-progress reassemblies,
2007                  * remove it from that table.  (It could be that this
2008                  * was the first and last fragment, so that no
2009                  * reassembly was done.)
2010                  */
2011                 if (orig_key != NULL)
2012                         fragment_unhash(table, orig_key);
2013
2014                 /*
2015                  * Add this item to the table of reassembled packets.
2016                  */
2017                 fragment_reassembled(table, fd_head, pinfo, id);
2018                 return fd_head;
2019         } else {
2020                 /*
2021                  * Reassembly isn't complete.
2022                  */
2023                 return NULL;
2024         }
2025 }
2026
2027 fragment_head *
2028 fragment_add_seq_check(reassembly_table *table, tvbuff_t *tvb, const int offset,
2029                        const packet_info *pinfo, const guint32 id,
2030                        const void *data,
2031                        const guint32 frag_number, const guint32 frag_data_len,
2032                        const gboolean more_frags)
2033 {
2034         return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2035                                            frag_number, frag_data_len,
2036                                            more_frags, 0);
2037 }
2038
2039 fragment_head *
2040 fragment_add_seq_802_11(reassembly_table *table, tvbuff_t *tvb,
2041                         const int offset, const packet_info *pinfo,
2042                         const guint32 id, const void *data,
2043                         const guint32 frag_number, const guint32 frag_data_len,
2044                         const gboolean more_frags)
2045 {
2046         return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2047                                            frag_number, frag_data_len,
2048                                            more_frags,
2049                                            REASSEMBLE_FLAGS_802_11_HACK);
2050 }
2051
2052 fragment_head *
2053 fragment_add_seq_next(reassembly_table *table, tvbuff_t *tvb, const int offset,
2054                       const packet_info *pinfo, const guint32 id,
2055                       const void *data, const guint32 frag_data_len,
2056                       const gboolean more_frags)
2057 {
2058         return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data,
2059                                            0, frag_data_len, more_frags,
2060                                            REASSEMBLE_FLAGS_NO_FRAG_NUMBER);
2061 }
2062
2063 void
2064 fragment_start_seq_check(reassembly_table *table, const packet_info *pinfo,
2065                          const guint32 id, const void *data, 
2066                          const guint32 tot_len)
2067 {
2068         fragment_head *fd_head;
2069
2070         /* Have we already seen this frame ?*/
2071         if (pinfo->fd->flags.visited) {
2072                 return;
2073         }
2074
2075         /* Check if fragment data exists */
2076         fd_head = lookup_fd_head(table, pinfo, id, data, NULL);
2077
2078         if (fd_head == NULL) {
2079                 /* Create list-head. */
2080                 fd_head = g_slice_new(fragment_head);
2081                 fd_head->next = NULL;
2082                 fd_head->datalen = tot_len;
2083                 fd_head->offset = 0;
2084                 fd_head->fragment_nr_offset = 0;
2085                 fd_head->len = 0;
2086                 fd_head->flags = FD_BLOCKSEQUENCE|FD_DATALEN_SET;
2087                 fd_head->tvb_data = NULL;
2088                 fd_head->reassembled_in = 0;
2089                 fd_head->error = NULL;
2090
2091                 insert_fd_head(table, fd_head, pinfo, id, data);
2092         }
2093 }
2094
2095 fragment_head *
2096 fragment_end_seq_next(reassembly_table *table, const packet_info *pinfo,
2097                       const guint32 id, const void *data)
2098 {
2099         reassembled_key reass_key;
2100         reassembled_key *new_key;
2101         fragment_head *fd_head;
2102         gpointer orig_key;
2103
2104         /*
2105          * Have we already seen this frame?
2106          * If so, look for it in the table of reassembled packets.
2107          */
2108         if (pinfo->fd->flags.visited) {
2109                 reass_key.frame = pinfo->fd->num;
2110                 reass_key.id = id;
2111                 return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key);
2112         }
2113
2114         fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key);
2115
2116         if (fd_head) {
2117                 if (fd_head->flags & FD_DATA_NOT_PRESENT) {
2118                         /* No data added */
2119                         return NULL;
2120                 }
2121
2122                 fd_head->datalen = fd_head->offset;
2123                 fd_head->flags |= FD_DATALEN_SET;
2124
2125                 fragment_defragment_and_free (fd_head, pinfo);
2126
2127                 /*
2128                  * Remove this from the table of in-progress reassemblies,
2129                  * and free up any memory used for it in that table.
2130                  */
2131                 fragment_unhash(table, orig_key);
2132
2133                 /*
2134                  * Add this item to the table of reassembled packets.
2135                  */
2136                 fragment_reassembled(table, fd_head, pinfo, id);
2137                 if (fd_head->next != NULL) {
2138                         new_key = g_slice_new(reassembled_key);
2139                         new_key->frame = pinfo->fd->num;
2140                         new_key->id = id;
2141                         g_hash_table_insert(table->reassembled_table, new_key, fd_head);
2142                 }
2143
2144                 return fd_head;
2145         } else {
2146                 /*
2147                  * Fragment data not found.
2148                  */
2149                 return NULL;
2150         }
2151 }
2152
2153 /*
2154  * Process reassembled data; if we're on the frame in which the data
2155  * was reassembled, put the fragment information into the protocol
2156  * tree, and construct a tvbuff with the reassembled data, otherwise
2157  * just put a "reassembled in" item into the protocol tree.
2158  */
2159 tvbuff_t *
2160 process_reassembled_data(tvbuff_t *tvb, const int offset, packet_info *pinfo,
2161         const char *name, fragment_head *fd_head, const fragment_items *fit,
2162         gboolean *update_col_infop, proto_tree *tree)
2163 {
2164         tvbuff_t *next_tvb;
2165         gboolean update_col_info;
2166         proto_item *frag_tree_item;
2167
2168         if (fd_head != NULL && pinfo->fd->num == fd_head->reassembled_in) {
2169                 /*
2170                  * OK, we've reassembled this.
2171                  * Is this something that's been reassembled from more
2172                  * than one fragment?
2173                  */
2174                 if (fd_head->next != NULL) {
2175                         /*
2176                          * Yes.
2177                          * Allocate a new tvbuff, referring to the
2178                          * reassembled payload, and set
2179                          * the tvbuff to the list of tvbuffs to which
2180                          * the tvbuff we were handed refers, so it'll get
2181                          * cleaned up when that tvbuff is cleaned up.
2182                          */
2183                         next_tvb = tvb_new_chain(tvb, fd_head->tvb_data);
2184
2185                         /* Add the defragmented data to the data source list. */
2186                         add_new_data_source(pinfo, next_tvb, name);
2187
2188                         /* show all fragments */
2189                         if (fd_head->flags & FD_BLOCKSEQUENCE) {
2190                                 update_col_info = !show_fragment_seq_tree(
2191                                         fd_head, fit,  tree, pinfo, next_tvb, &frag_tree_item);
2192                         } else {
2193                                 update_col_info = !show_fragment_tree(fd_head,
2194                                         fit, tree, pinfo, next_tvb, &frag_tree_item);
2195                         }
2196                 } else {
2197                         /*
2198                          * No.
2199                          * Return a tvbuff with the payload.
2200                          */
2201                         next_tvb = tvb_new_subset_remaining(tvb, offset);
2202                         pinfo->fragmented = FALSE;      /* one-fragment packet */
2203                         update_col_info = TRUE;
2204                 }
2205                 if (update_col_infop != NULL)
2206                         *update_col_infop = update_col_info;
2207         } else {
2208                 /*
2209                  * We don't have the complete reassembled payload, or this
2210                  * isn't the final frame of that payload.
2211                  */
2212                 next_tvb = NULL;
2213
2214                 /*
2215                  * If we know what frame this was reassembled in,
2216                  * and if there's a field to use for the number of
2217                  * the frame in which the packet was reassembled,
2218                  * add it to the protocol tree.
2219                  */
2220                 if (fd_head != NULL && fit->hf_reassembled_in != NULL) {
2221                         proto_tree_add_uint(tree,
2222                                 *(fit->hf_reassembled_in), tvb,
2223                                 0, 0, fd_head->reassembled_in);
2224                 }
2225         }
2226         return next_tvb;
2227 }
2228
2229 /*
2230  * Show a single fragment in a fragment subtree, and put information about
2231  * it in the top-level item for that subtree.
2232  */
2233 static void
2234 show_fragment(fragment_item *fd, const int offset, const fragment_items *fit,
2235         proto_tree *ft, proto_item *fi, const gboolean first_frag,
2236         const guint32 count, tvbuff_t *tvb, packet_info *pinfo)
2237 {
2238         proto_item *fei=NULL;
2239         int hf;
2240
2241         if (first_frag) {
2242                 gchar *name;
2243                 if (count == 1) {
2244                         name = g_strdup(proto_registrar_get_name(*(fit->hf_fragment)));
2245                 } else {
2246                         name = g_strdup(proto_registrar_get_name(*(fit->hf_fragments)));
2247                 }
2248                 proto_item_set_text(fi, "%u %s (%u byte%s): ", count, name, tvb_length(tvb),
2249                                     plurality(tvb_length(tvb), "", "s"));
2250                 g_free(name);
2251         } else {
2252                 proto_item_append_text(fi, ", ");
2253         }
2254         proto_item_append_text(fi, "#%u(%u)", fd->frame, fd->len);
2255
2256         if (fd->flags & (FD_OVERLAPCONFLICT
2257                 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
2258                 hf = *(fit->hf_fragment_error);
2259         } else {
2260                 hf = *(fit->hf_fragment);
2261         }
2262         if (fd->len == 0) {
2263                 fei = proto_tree_add_uint_format(ft, hf,
2264                         tvb, offset, fd->len,
2265                         fd->frame,
2266                         "Frame: %u (no data)",
2267                         fd->frame);
2268         } else {
2269                 fei = proto_tree_add_uint_format(ft, hf,
2270                         tvb, offset, fd->len,
2271                         fd->frame,
2272                         "Frame: %u, payload: %u-%u (%u byte%s)",
2273                         fd->frame,
2274                         offset,
2275                         offset+fd->len-1,
2276                         fd->len,
2277                         plurality(fd->len, "", "s"));
2278         }
2279         PROTO_ITEM_SET_GENERATED(fei);
2280         mark_frame_as_depended_upon(pinfo, fd->frame);
2281         if (fd->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT
2282                 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
2283                 /* this fragment has some flags set, create a subtree
2284                  * for it and display the flags.
2285                  */
2286                 proto_tree *fet=NULL;
2287
2288                 fet = proto_item_add_subtree(fei, *(fit->ett_fragment));
2289                 if (fd->flags&FD_OVERLAP) {
2290                         fei=proto_tree_add_boolean(fet,
2291                                 *(fit->hf_fragment_overlap),
2292                                 tvb, 0, 0,
2293                                 TRUE);
2294                         PROTO_ITEM_SET_GENERATED(fei);
2295                 }
2296                 if (fd->flags&FD_OVERLAPCONFLICT) {
2297                         fei=proto_tree_add_boolean(fet,
2298                                 *(fit->hf_fragment_overlap_conflict),
2299                                 tvb, 0, 0,
2300                                 TRUE);
2301                         PROTO_ITEM_SET_GENERATED(fei);
2302                 }
2303                 if (fd->flags&FD_MULTIPLETAILS) {
2304                         fei=proto_tree_add_boolean(fet,
2305                                 *(fit->hf_fragment_multiple_tails),
2306                                 tvb, 0, 0,
2307                                 TRUE);
2308                         PROTO_ITEM_SET_GENERATED(fei);
2309                 }
2310                 if (fd->flags&FD_TOOLONGFRAGMENT) {
2311                         fei=proto_tree_add_boolean(fet,
2312                                 *(fit->hf_fragment_too_long_fragment),
2313                                 tvb, 0, 0,
2314                                 TRUE);
2315                         PROTO_ITEM_SET_GENERATED(fei);
2316                 }
2317         }
2318 }
2319
2320 static gboolean
2321 show_fragment_errs_in_col(fragment_head *fd_head, const fragment_items *fit,
2322         packet_info *pinfo)
2323 {
2324         if (fd_head->flags & (FD_OVERLAPCONFLICT
2325                 |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) {
2326                 col_add_fstr(pinfo->cinfo, COL_INFO, "[Illegal %s]", fit->tag);
2327                 return TRUE;
2328         }
2329
2330         return FALSE;
2331 }
2332
2333 /* This function will build the fragment subtree; it's for fragments
2334    reassembled with "fragment_add()".
2335
2336    It will return TRUE if there were fragmentation errors
2337    or FALSE if fragmentation was ok.
2338 */
2339 gboolean
2340 show_fragment_tree(fragment_head *fd_head, const fragment_items *fit,
2341         proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi)
2342 {
2343         fragment_item *fd;
2344         proto_tree *ft;
2345         gboolean first_frag;
2346         guint32 count = 0;
2347         /* It's not fragmented. */
2348         pinfo->fragmented = FALSE;
2349
2350         *fi = proto_tree_add_item(tree, *(fit->hf_fragments), tvb, 0, -1, ENC_NA);
2351         PROTO_ITEM_SET_GENERATED(*fi);
2352
2353         ft = proto_item_add_subtree(*fi, *(fit->ett_fragments));
2354         first_frag = TRUE;
2355         for (fd = fd_head->next; fd != NULL; fd = fd->next) {
2356                 count++;
2357         }
2358         for (fd = fd_head->next; fd != NULL; fd = fd->next) {
2359                 show_fragment(fd, fd->offset, fit, ft, *fi, first_frag, count, tvb, pinfo);
2360                 first_frag = FALSE;
2361         }
2362
2363         if (fit->hf_fragment_count) {
2364                 proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_fragment_count),
2365                                                       tvb, 0, 0, count);
2366                 PROTO_ITEM_SET_GENERATED(fli);
2367         }
2368
2369         if (fit->hf_reassembled_length) {
2370                 proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_reassembled_length),
2371                                                       tvb, 0, 0, tvb_length (tvb));
2372                 PROTO_ITEM_SET_GENERATED(fli);
2373         }
2374
2375         if (fit->hf_reassembled_data) {
2376                 proto_item *fli = proto_tree_add_item(ft, *(fit->hf_reassembled_data),
2377                                                       tvb, 0, tvb_length(tvb), ENC_NA);
2378                 PROTO_ITEM_SET_GENERATED(fli);
2379         }
2380
2381         return show_fragment_errs_in_col(fd_head, fit, pinfo);
2382 }
2383
2384 /* This function will build the fragment subtree; it's for fragments
2385    reassembled with "fragment_add_seq()" or "fragment_add_seq_check()".
2386
2387    It will return TRUE if there were fragmentation errors
2388    or FALSE if fragmentation was ok.
2389 */
2390 gboolean
2391 show_fragment_seq_tree(fragment_head *fd_head, const fragment_items *fit,
2392         proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi)
2393 {
2394         guint32 offset, next_offset, count = 0;
2395         fragment_item *fd, *last_fd;
2396         proto_tree *ft;
2397         gboolean first_frag;
2398
2399         /* It's not fragmented. */
2400         pinfo->fragmented = FALSE;
2401
2402         *fi = proto_tree_add_item(tree, *(fit->hf_fragments), tvb, 0, -1, ENC_NA);
2403         PROTO_ITEM_SET_GENERATED(*fi);
2404
2405         ft = proto_item_add_subtree(*fi, *(fit->ett_fragments));
2406         offset = 0;
2407         next_offset = 0;
2408         last_fd = NULL;
2409         first_frag = TRUE;
2410         for (fd = fd_head->next; fd != NULL; fd = fd->next){
2411                 count++;
2412         }
2413         for (fd = fd_head->next; fd != NULL; fd = fd->next){
2414                 if (last_fd == NULL || last_fd->offset != fd->offset) {
2415                         offset = next_offset;
2416                         next_offset += fd->len;
2417                 }
2418                 last_fd = fd;
2419                 show_fragment(fd, offset, fit, ft, *fi, first_frag, count, tvb, pinfo);
2420                 first_frag = FALSE;
2421         }
2422
2423         if (fit->hf_fragment_count) {
2424                 proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_fragment_count),
2425                                                       tvb, 0, 0, count);
2426                 PROTO_ITEM_SET_GENERATED(fli);
2427         }
2428
2429         if (fit->hf_reassembled_length) {
2430                 proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_reassembled_length),
2431                                                       tvb, 0, 0, tvb_length (tvb));
2432                 PROTO_ITEM_SET_GENERATED(fli);
2433         }
2434
2435         if (fit->hf_reassembled_data) {
2436                 proto_item *fli = proto_tree_add_item(ft, *(fit->hf_reassembled_data),
2437                                                       tvb, 0, tvb_length(tvb), ENC_NA);
2438                 PROTO_ITEM_SET_GENERATED(fli);
2439         }
2440
2441         return show_fragment_errs_in_col(fd_head, fit, pinfo);
2442 }
2443
2444 /*
2445  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
2446  *
2447  * Local variables:
2448  * c-basic-offset: 8
2449  * tab-width: 8
2450  * indent-tabs-mode: t
2451  * End:
2452  *
2453  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2454  * :indentSize=8:tabSize=8:noTabs=false:
2455  */