/src/wireshark/epan/reassemble.c
Line | Count | Source |
1 | | /* reassemble.c |
2 | | * Routines for {fragment,segment} reassembly |
3 | | * |
4 | | * Wireshark - Network traffic analyzer |
5 | | * By Gerald Combs <gerald@wireshark.org> |
6 | | * Copyright 1998 Gerald Combs |
7 | | * |
8 | | * SPDX-License-Identifier: GPL-2.0-or-later |
9 | | */ |
10 | | |
11 | | #include "config.h" |
12 | | |
13 | | #include <string.h> |
14 | | |
15 | | #include <epan/packet.h> |
16 | | #include <epan/exceptions.h> |
17 | | #include <epan/reassemble.h> |
18 | | #include <epan/tvbuff-int.h> |
19 | | |
20 | | #include <wsutil/str_util.h> |
21 | | #include <wsutil/ws_assert.h> |
22 | | |
23 | | /* |
24 | | * Functions for reassembly tables where the endpoint addresses, and a |
25 | | * fragment ID, are used as the key. |
26 | | */ |
27 | | typedef struct _fragment_addresses_key { |
28 | | address src; |
29 | | address dst; |
30 | | uint32_t id; |
31 | | } fragment_addresses_key; |
32 | | |
33 | | static GList* reassembly_table_list; |
34 | | |
35 | | static unsigned |
36 | | fragment_addresses_hash(const void *k) |
37 | 33.9k | { |
38 | 33.9k | const fragment_addresses_key* key = (const fragment_addresses_key*) k; |
39 | 33.9k | unsigned hash_val; |
40 | | /* |
41 | | int i; |
42 | | */ |
43 | | |
44 | 33.9k | hash_val = 0; |
45 | | |
46 | | /* More than likely: in most captures src and dst addresses are the |
47 | | same, and would hash the same. |
48 | | We only use id as the hash as an optimization. |
49 | | |
50 | | for (i = 0; i < key->src.len; i++) |
51 | | hash_val += key->src.data[i]; |
52 | | for (i = 0; i < key->dst.len; i++) |
53 | | hash_val += key->dst.data[i]; |
54 | | */ |
55 | | |
56 | 33.9k | hash_val += key->id; |
57 | | |
58 | 33.9k | return hash_val; |
59 | 33.9k | } |
60 | | |
61 | | static int |
62 | | fragment_addresses_equal(const void *k1, const void *k2) |
63 | 333k | { |
64 | 333k | const fragment_addresses_key* key1 = (const fragment_addresses_key*) k1; |
65 | 333k | const fragment_addresses_key* key2 = (const fragment_addresses_key*) k2; |
66 | | |
67 | | /* |
68 | | * key.id is the first item to compare since it's the item most |
69 | | * likely to differ between sessions, thus short-circuiting |
70 | | * the comparison of addresses. |
71 | | */ |
72 | 333k | return (key1->id == key2->id) && |
73 | 209k | (addresses_equal(&key1->src, &key2->src)) && |
74 | 21.9k | (addresses_equal(&key1->dst, &key2->dst)); |
75 | 333k | } |
76 | | |
77 | | /* |
78 | | * Create a fragment key for temporary use; it can point to non- |
79 | | * persistent data, and so must only be used to look up and |
80 | | * delete entries, not to add them. |
81 | | */ |
82 | | static void * |
83 | | fragment_addresses_temporary_key(const packet_info *pinfo, const uint32_t id, |
84 | | const void *data _U_) |
85 | 28.0k | { |
86 | 28.0k | fragment_addresses_key *key = g_slice_new(fragment_addresses_key); |
87 | | |
88 | | /* |
89 | | * Do a shallow copy of the addresses. |
90 | | */ |
91 | 28.0k | copy_address_shallow(&key->src, &pinfo->src); |
92 | 28.0k | copy_address_shallow(&key->dst, &pinfo->dst); |
93 | 28.0k | key->id = id; |
94 | | |
95 | 28.0k | return (void *)key; |
96 | 28.0k | } |
97 | | |
98 | | /* |
99 | | * Create a fragment key for permanent use; it must point to persistent |
100 | | * data, so that it can be used to add entries. |
101 | | */ |
102 | | static void * |
103 | | fragment_addresses_persistent_key(const packet_info *pinfo, const uint32_t id, |
104 | | const void *data _U_) |
105 | 3.61k | { |
106 | 3.61k | fragment_addresses_key *key = g_slice_new(fragment_addresses_key); |
107 | | |
108 | | /* |
109 | | * Do a deep copy of the addresses. |
110 | | */ |
111 | 3.61k | copy_address(&key->src, &pinfo->src); |
112 | 3.61k | copy_address(&key->dst, &pinfo->dst); |
113 | 3.61k | key->id = id; |
114 | | |
115 | 3.61k | return (void *)key; |
116 | 3.61k | } |
117 | | |
118 | | static void |
119 | | fragment_addresses_free_temporary_key(void *ptr) |
120 | 28.0k | { |
121 | 28.0k | fragment_addresses_key *key = (fragment_addresses_key *)ptr; |
122 | 28.0k | g_slice_free(fragment_addresses_key, key); |
123 | 28.0k | } |
124 | | |
125 | | static void |
126 | | fragment_addresses_free_persistent_key(void *ptr) |
127 | 2.35k | { |
128 | 2.35k | fragment_addresses_key *key = (fragment_addresses_key *)ptr; |
129 | | |
130 | 2.35k | if(key){ |
131 | | /* |
132 | | * Free up the copies of the addresses from the old key. |
133 | | */ |
134 | 2.35k | free_address(&key->src); |
135 | 2.35k | free_address(&key->dst); |
136 | | |
137 | 2.35k | g_slice_free(fragment_addresses_key, key); |
138 | 2.35k | } |
139 | 2.35k | } |
140 | | |
141 | | const reassembly_table_functions |
142 | | addresses_reassembly_table_functions = { |
143 | | fragment_addresses_hash, |
144 | | fragment_addresses_equal, |
145 | | fragment_addresses_temporary_key, |
146 | | fragment_addresses_persistent_key, |
147 | | fragment_addresses_free_temporary_key, |
148 | | fragment_addresses_free_persistent_key |
149 | | }; |
150 | | |
151 | | /* |
152 | | * Functions for reassembly tables where the endpoint addresses and ports, |
153 | | * and a fragment ID, are used as the key. |
154 | | */ |
155 | | typedef struct _fragment_addresses_ports_key { |
156 | | address src_addr; |
157 | | address dst_addr; |
158 | | uint32_t src_port; |
159 | | uint32_t dst_port; |
160 | | uint32_t id; |
161 | | } fragment_addresses_ports_key; |
162 | | |
163 | | static unsigned |
164 | | fragment_addresses_ports_hash(const void *k) |
165 | 2.23k | { |
166 | 2.23k | const fragment_addresses_ports_key* key = (const fragment_addresses_ports_key*) k; |
167 | 2.23k | unsigned hash_val; |
168 | | /* |
169 | | int i; |
170 | | */ |
171 | | |
172 | 2.23k | hash_val = 0; |
173 | | |
174 | | /* More than likely: in most captures src and dst addresses and ports |
175 | | are the same, and would hash the same. |
176 | | We only use id as the hash as an optimization. |
177 | | |
178 | | for (i = 0; i < key->src.len; i++) |
179 | | hash_val += key->src_addr.data[i]; |
180 | | for (i = 0; i < key->dst.len; i++) |
181 | | hash_val += key->dst_addr.data[i]; |
182 | | hash_val += key->src_port; |
183 | | hash_val += key->dst_port; |
184 | | */ |
185 | | |
186 | 2.23k | hash_val += key->id; |
187 | | |
188 | 2.23k | return hash_val; |
189 | 2.23k | } |
190 | | |
191 | | static int |
192 | | fragment_addresses_ports_equal(const void *k1, const void *k2) |
193 | 28.0k | { |
194 | 28.0k | const fragment_addresses_ports_key* key1 = (const fragment_addresses_ports_key*) k1; |
195 | 28.0k | const fragment_addresses_ports_key* key2 = (const fragment_addresses_ports_key*) k2; |
196 | | |
197 | | /* |
198 | | * key.id is the first item to compare since it's the item most |
199 | | * likely to differ between sessions, thus short-circuiting |
200 | | * the comparison of addresses and ports. |
201 | | */ |
202 | 28.0k | return (key1->id == key2->id) && |
203 | 28.0k | (addresses_equal(&key1->src_addr, &key2->src_addr)) && |
204 | 2.58k | (addresses_equal(&key1->dst_addr, &key2->dst_addr)) && |
205 | 1.31k | (key1->src_port == key2->src_port) && |
206 | 984 | (key1->dst_port == key2->dst_port); |
207 | 28.0k | } |
208 | | |
209 | | /* |
210 | | * Create a fragment key for temporary use; it can point to non- |
211 | | * persistent data, and so must only be used to look up and |
212 | | * delete entries, not to add them. |
213 | | */ |
214 | | static void * |
215 | | fragment_addresses_ports_temporary_key(const packet_info *pinfo, const uint32_t id, |
216 | | const void *data _U_) |
217 | 1.68k | { |
218 | 1.68k | fragment_addresses_ports_key *key = g_slice_new(fragment_addresses_ports_key); |
219 | | |
220 | | /* |
221 | | * Do a shallow copy of the addresses. |
222 | | */ |
223 | 1.68k | copy_address_shallow(&key->src_addr, &pinfo->src); |
224 | 1.68k | copy_address_shallow(&key->dst_addr, &pinfo->dst); |
225 | 1.68k | key->src_port = pinfo->srcport; |
226 | 1.68k | key->dst_port = pinfo->destport; |
227 | 1.68k | key->id = id; |
228 | | |
229 | 1.68k | return (void *)key; |
230 | 1.68k | } |
231 | | |
232 | | /* |
233 | | * Create a fragment key for permanent use; it must point to persistent |
234 | | * data, so that it can be used to add entries. |
235 | | */ |
236 | | static void * |
237 | | fragment_addresses_ports_persistent_key(const packet_info *pinfo, |
238 | | const uint32_t id, const void *data _U_) |
239 | 512 | { |
240 | 512 | fragment_addresses_ports_key *key = g_slice_new(fragment_addresses_ports_key); |
241 | | |
242 | | /* |
243 | | * Do a deep copy of the addresses. |
244 | | */ |
245 | 512 | copy_address(&key->src_addr, &pinfo->src); |
246 | 512 | copy_address(&key->dst_addr, &pinfo->dst); |
247 | 512 | key->src_port = pinfo->srcport; |
248 | 512 | key->dst_port = pinfo->destport; |
249 | 512 | key->id = id; |
250 | | |
251 | 512 | return (void *)key; |
252 | 512 | } |
253 | | |
254 | | static void |
255 | | fragment_addresses_ports_free_temporary_key(void *ptr) |
256 | 1.68k | { |
257 | 1.68k | fragment_addresses_ports_key *key = (fragment_addresses_ports_key *)ptr; |
258 | 1.68k | g_slice_free(fragment_addresses_ports_key, key); |
259 | 1.68k | } |
260 | | |
261 | | static void |
262 | | fragment_addresses_ports_free_persistent_key(void *ptr) |
263 | 40 | { |
264 | 40 | fragment_addresses_ports_key *key = (fragment_addresses_ports_key *)ptr; |
265 | | |
266 | 40 | if(key){ |
267 | | /* |
268 | | * Free up the copies of the addresses from the old key. |
269 | | */ |
270 | 40 | free_address(&key->src_addr); |
271 | 40 | free_address(&key->dst_addr); |
272 | | |
273 | 40 | g_slice_free(fragment_addresses_ports_key, key); |
274 | 40 | } |
275 | 40 | } |
276 | | |
277 | | const reassembly_table_functions |
278 | | addresses_ports_reassembly_table_functions = { |
279 | | fragment_addresses_ports_hash, |
280 | | fragment_addresses_ports_equal, |
281 | | fragment_addresses_ports_temporary_key, |
282 | | fragment_addresses_ports_persistent_key, |
283 | | fragment_addresses_ports_free_temporary_key, |
284 | | fragment_addresses_ports_free_persistent_key |
285 | | }; |
286 | | |
287 | | typedef struct _reassembled_key { |
288 | | uint32_t id; |
289 | | uint32_t frame; |
290 | | } reassembled_key; |
291 | | |
292 | | static int |
293 | | reassembled_equal(const void *k1, const void *k2) |
294 | 31.3k | { |
295 | 31.3k | const reassembled_key* key1 = (const reassembled_key*) k1; |
296 | 31.3k | const reassembled_key* key2 = (const reassembled_key*) k2; |
297 | | |
298 | | /* |
299 | | * We assume that the frame numbers are unlikely to be equal, |
300 | | * so we check them first. |
301 | | */ |
302 | 31.3k | return key1->frame == key2->frame && key1->id == key2->id; |
303 | 31.3k | } |
304 | | |
305 | | static unsigned |
306 | | reassembled_hash(const void *k) |
307 | 19.9k | { |
308 | 19.9k | const reassembled_key* key = (const reassembled_key*) k; |
309 | | |
310 | 19.9k | return key->frame; |
311 | 19.9k | } |
312 | | |
313 | | static void |
314 | | reassembled_key_free(void *ptr) |
315 | 5.89k | { |
316 | 5.89k | g_slice_free(reassembled_key, (reassembled_key *)ptr); |
317 | 5.89k | } |
318 | | |
319 | | /* --------------fragment_item functions ----------- */ |
320 | | static fragment_item* |
321 | | new_fragment_item(uint32_t frame, uint32_t offset, uint32_t len) |
322 | 18.9k | { |
323 | 18.9k | fragment_item *fd; |
324 | | |
325 | 18.9k | fd = g_slice_new(fragment_item); |
326 | 18.9k | fd->next = NULL; |
327 | 18.9k | fd->flags = 0; |
328 | 18.9k | fd->frame = frame; |
329 | 18.9k | fd->offset = offset; |
330 | 18.9k | fd->len = len; |
331 | 18.9k | fd->tvb_data = NULL; |
332 | | |
333 | 18.9k | return fd; |
334 | 18.9k | } |
335 | | |
336 | | static void |
337 | | fragment_item_free_tvb(fragment_item *fd_i) |
338 | 8.59k | { |
339 | | /* If this is a subset of the tvb created for the head after |
340 | | * dissembly, don't free it (that would cause memory errors; |
341 | | * the parent will be freed later.) */ |
342 | 8.59k | if (fd_i->flags & FD_SUBSET_TVB) |
343 | 0 | fd_i->flags &= ~FD_SUBSET_TVB; |
344 | 8.59k | else if (fd_i->tvb_data) |
345 | 7.59k | tvb_free(fd_i->tvb_data); |
346 | | |
347 | 8.59k | fd_i->tvb_data=NULL; |
348 | 8.59k | } |
349 | | |
350 | | /* Returns the pointer to the next item so that the list can be freed. */ |
351 | | static fragment_item* |
352 | | fragment_item_free(fragment_item *fd_i) |
353 | 2.12k | { |
354 | 2.12k | fragment_item *fd_next = fd_i->next; |
355 | 2.12k | fragment_item_free_tvb(fd_i); |
356 | 2.12k | g_slice_free(fragment_item, fd_i); |
357 | 2.12k | return fd_next; |
358 | 2.12k | } |
359 | | |
360 | | /* ------------------------- */ |
361 | | static fragment_head *new_head(const uint32_t flags) |
362 | 7.05k | { |
363 | 7.05k | fragment_head *fd_head; |
364 | | /* If head/first structure in list only holds no other data than |
365 | | * 'datalen' then we don't have to change the head of the list |
366 | | * even if we want to keep it sorted |
367 | | */ |
368 | 7.05k | fd_head=g_slice_new0(fragment_head); |
369 | | |
370 | 7.05k | fd_head->flags=flags; |
371 | 7.05k | return fd_head; |
372 | 7.05k | } |
373 | | |
374 | | /* |
375 | | * For a reassembled-packet hash table entry, free the fragment data |
376 | | * to which the value refers. (The key is freed by reassembled_key_free.) |
377 | | */ |
378 | | static void |
379 | | free_fd_head(fragment_head *fd_head) |
380 | 952 | { |
381 | 952 | fragment_item *fd_i; |
382 | | |
383 | 952 | if (fd_head->flags & FD_SUBSET_TVB) |
384 | 0 | fd_head->tvb_data = NULL; |
385 | 952 | if (fd_head->tvb_data) |
386 | 0 | tvb_free(fd_head->tvb_data); |
387 | 952 | fd_i = fd_head->next; |
388 | 2.35k | while (fd_i != NULL) { |
389 | 1.40k | fd_i = fragment_item_free(fd_i); |
390 | 1.40k | } |
391 | 952 | g_slice_free(fragment_head, fd_head); |
392 | 952 | } |
393 | | |
394 | | static void |
395 | | unref_fd_head(void *data) |
396 | 5.89k | { |
397 | 5.89k | fragment_head *fd_head = (fragment_head *) data; |
398 | 5.89k | fd_head->ref_count--; |
399 | | |
400 | 5.89k | if (fd_head->ref_count == 0) { |
401 | 952 | free_fd_head(fd_head); |
402 | 952 | } |
403 | 5.89k | } |
404 | | |
405 | | /* |
406 | | * For a fragment hash table entry, free the associated fragments. |
407 | | * The entry value (fd_chain) is freed herein and the entry is freed |
408 | | * when the key freeing routine is called (as a consequence of returning |
409 | | * true from this function). |
410 | | */ |
411 | | static gboolean |
412 | | free_all_fragments(void *key_arg _U_, void *value, void *user_data _U_) |
413 | 0 | { |
414 | 0 | fragment_head *fd_head; |
415 | | |
416 | | /* g_hash_table_new_full() was used to supply a function |
417 | | * to free the key and anything to which it points |
418 | | */ |
419 | 0 | fd_head = (fragment_head *)value; |
420 | 0 | free_fd_head(fd_head); |
421 | |
|
422 | 0 | return TRUE; |
423 | 0 | } |
424 | | |
425 | | static void |
426 | | reassembled_table_insert(GHashTable *reassembled_table, reassembled_key *key, fragment_head *fd_head) |
427 | 9.89k | { |
428 | 9.89k | fragment_head *old_fd_head; |
429 | 9.89k | fd_head->ref_count++; |
430 | 9.89k | if ((old_fd_head = g_hash_table_lookup(reassembled_table, key)) != NULL) { |
431 | 5.89k | if (old_fd_head->ref_count == 1) { |
432 | | /* We're replacing the last entry in the reassembled |
433 | | * table for an old reassembly. Does it have a tvb? |
434 | | * We might still be using that tvb's memory for an |
435 | | * address via set_address_tvb(). (See #19094.) |
436 | | */ |
437 | 952 | if (old_fd_head->tvb_data && fd_head->tvb_data) { |
438 | | /* Free it when the new tvb is freed */ |
439 | 735 | tvb_set_child_real_data_tvbuff(fd_head->tvb_data, old_fd_head->tvb_data); |
440 | 735 | } |
441 | | /* XXX: Set the old data to NULL regardless. If we |
442 | | * have old data but not new data, that is odd (we're |
443 | | * replacing a reassembly with tvb data with something |
444 | | * with no tvb data, possibly because a zero length or |
445 | | * null tvb was passed into a defragment function, |
446 | | * which is a dissector bug.) |
447 | | * This leaks the tvb data if we couldn't add it to |
448 | | * a new tvb's chain, but we might not be able to free |
449 | | * it yet if set_address_tvb() was used. |
450 | | */ |
451 | 952 | old_fd_head->tvb_data = NULL; |
452 | 952 | } |
453 | 5.89k | } |
454 | 9.89k | g_hash_table_insert(reassembled_table, key, fd_head); |
455 | 9.89k | } |
456 | | |
457 | | typedef struct register_reassembly_table { |
458 | | reassembly_table *table; |
459 | | const reassembly_table_functions *funcs; |
460 | | } register_reassembly_table_t; |
461 | | |
462 | | /* |
463 | | * Register a reassembly table. |
464 | | */ |
465 | | void |
466 | | reassembly_table_register(reassembly_table *table, |
467 | | const reassembly_table_functions *funcs) |
468 | 2.04k | { |
469 | 2.04k | register_reassembly_table_t* reg_table; |
470 | | |
471 | 2.04k | DISSECTOR_ASSERT(table); |
472 | 2.04k | DISSECTOR_ASSERT(funcs); |
473 | | |
474 | 2.04k | reg_table = g_new(register_reassembly_table_t,1); |
475 | | |
476 | 2.04k | reg_table->table = table; |
477 | 2.04k | reg_table->funcs = funcs; |
478 | | |
479 | 2.04k | reassembly_table_list = g_list_prepend(reassembly_table_list, reg_table); |
480 | 2.04k | } |
481 | | |
482 | | /* |
483 | | * Initialize a reassembly table, with specified functions. |
484 | | */ |
485 | | void |
486 | | reassembly_table_init(reassembly_table *table, |
487 | | const reassembly_table_functions *funcs) |
488 | 2.17k | { |
489 | 2.17k | if (table->temporary_key_func == NULL) |
490 | 2.15k | table->temporary_key_func = funcs->temporary_key_func; |
491 | 2.17k | if (table->persistent_key_func == NULL) |
492 | 2.15k | table->persistent_key_func = funcs->persistent_key_func; |
493 | 2.17k | if (table->free_temporary_key_func == NULL) |
494 | 2.15k | table->free_temporary_key_func = funcs->free_temporary_key_func; |
495 | 2.17k | if (table->fragment_table != NULL) { |
496 | | /* |
497 | | * The fragment hash table exists. |
498 | | * |
499 | | * Remove all entries and free fragment data for each entry. |
500 | | * |
501 | | * The keys, and anything to which they point, are freed by |
502 | | * calling the table's key freeing function. The values |
503 | | * are freed in free_all_fragments(). |
504 | | */ |
505 | 14 | g_hash_table_foreach_remove(table->fragment_table, |
506 | 14 | free_all_fragments, NULL); |
507 | 2.15k | } else { |
508 | | /* The fragment table does not exist. Create it */ |
509 | 2.15k | table->fragment_table = g_hash_table_new_full(funcs->hash_func, |
510 | 2.15k | funcs->equal_func, funcs->free_persistent_key_func, NULL); |
511 | 2.15k | } |
512 | | |
513 | 2.17k | if (table->reassembled_table != NULL) { |
514 | | /* |
515 | | * The reassembled-packet hash table exists. |
516 | | * |
517 | | * Remove all entries and free reassembled packet |
518 | | * data and key for each entry. |
519 | | */ |
520 | 14 | g_hash_table_remove_all(table->reassembled_table); |
521 | 2.15k | } else { |
522 | | /* The fragment table does not exist. Create it */ |
523 | 2.15k | table->reassembled_table = g_hash_table_new_full(reassembled_hash, |
524 | 2.15k | reassembled_equal, reassembled_key_free, unref_fd_head); |
525 | 2.15k | } |
526 | 2.17k | } |
527 | | |
528 | | /* |
529 | | * Destroy a reassembly table. |
530 | | */ |
531 | | void |
532 | | reassembly_table_destroy(reassembly_table *table) |
533 | 0 | { |
534 | | /* |
535 | | * Clear the function pointers. |
536 | | */ |
537 | 0 | table->temporary_key_func = NULL; |
538 | 0 | table->persistent_key_func = NULL; |
539 | 0 | table->free_temporary_key_func = NULL; |
540 | 0 | if (table->fragment_table != NULL) { |
541 | | /* |
542 | | * The fragment hash table exists. |
543 | | * |
544 | | * Remove all entries and free fragment data for each entry. |
545 | | * |
546 | | * The keys, and anything to which they point, are freed by |
547 | | * calling the table's key freeing function. The values |
548 | | * are freed in free_all_fragments(). |
549 | | */ |
550 | 0 | g_hash_table_foreach_remove(table->fragment_table, |
551 | 0 | free_all_fragments, NULL); |
552 | | |
553 | | /* |
554 | | * Now destroy the hash table. |
555 | | */ |
556 | 0 | g_hash_table_destroy(table->fragment_table); |
557 | 0 | table->fragment_table = NULL; |
558 | 0 | } |
559 | 0 | if (table->reassembled_table != NULL) { |
560 | | /* |
561 | | * The reassembled-packet hash table exists. |
562 | | * |
563 | | * Remove all entries and free reassembled packet |
564 | | * data and key for each entry. |
565 | | */ |
566 | |
|
567 | 0 | g_hash_table_remove_all(table->reassembled_table); |
568 | | |
569 | | /* |
570 | | * Now destroy the hash table. |
571 | | */ |
572 | 0 | g_hash_table_destroy(table->reassembled_table); |
573 | 0 | table->reassembled_table = NULL; |
574 | 0 | } |
575 | 0 | } |
576 | | |
577 | | /* |
578 | | * Look up an fd_head in the fragment table, optionally returning the key |
579 | | * for it. |
580 | | */ |
581 | | static fragment_head * |
582 | | lookup_fd_head(reassembly_table *table, const packet_info *pinfo, |
583 | | const uint32_t id, const void *data, void * *orig_keyp) |
584 | 39.0k | { |
585 | 39.0k | void *key; |
586 | 39.0k | void *value; |
587 | | |
588 | | /* Create key to search hash with */ |
589 | 39.0k | key = table->temporary_key_func(pinfo, id, data); |
590 | | |
591 | | /* |
592 | | * Look up the reassembly in the fragment table. |
593 | | */ |
594 | 39.0k | if (!g_hash_table_lookup_extended(table->fragment_table, key, orig_keyp, |
595 | 39.0k | &value)) |
596 | 18.2k | value = NULL; |
597 | | /* Free the key */ |
598 | 39.0k | table->free_temporary_key_func(key); |
599 | | |
600 | 39.0k | return (fragment_head *)value; |
601 | 39.0k | } |
602 | | |
603 | | /* |
604 | | * Insert an fd_head into the fragment table, and return the key used. |
605 | | */ |
606 | | static void * |
607 | | insert_fd_head(reassembly_table *table, fragment_head *fd_head, |
608 | | const packet_info *pinfo, const uint32_t id, const void *data) |
609 | 5.66k | { |
610 | 5.66k | void *key; |
611 | | |
612 | | /* |
613 | | * We're going to use the key to insert the fragment, |
614 | | * so make a persistent version of it. |
615 | | */ |
616 | 5.66k | key = table->persistent_key_func(pinfo, id, data); |
617 | 5.66k | g_hash_table_insert(table->fragment_table, key, fd_head); |
618 | 5.66k | return key; |
619 | 5.66k | } |
620 | | |
621 | | /* This function cleans up the stored state and removes the reassembly data and |
622 | | * (with one exception) all allocated memory for matching reassembly. |
623 | | * |
624 | | * The exception is : |
625 | | * If the PDU was already completely reassembled, then the tvbuff containing the |
626 | | * reassembled data WILL NOT be free()d, and the pointer to that tvbuff will be |
627 | | * returned. |
628 | | * Othervise the function will return NULL. |
629 | | * |
630 | | * So, if you call fragment_delete and it returns non-NULL, YOU are responsible |
631 | | * to tvb_free() that tvbuff. |
632 | | */ |
633 | | tvbuff_t * |
634 | | fragment_delete(reassembly_table *table, const packet_info *pinfo, |
635 | | const uint32_t id, const void *data) |
636 | 559 | { |
637 | 559 | fragment_head *fd_head; |
638 | 559 | fragment_item *fd; |
639 | 559 | tvbuff_t *fd_tvb_data=NULL; |
640 | 559 | void *key; |
641 | | |
642 | 559 | fd_head = lookup_fd_head(table, pinfo, id, data, &key); |
643 | 559 | if(fd_head==NULL){ |
644 | | /* We do not recognize this as a PDU we have seen before. return */ |
645 | 0 | return NULL; |
646 | 0 | } |
647 | | |
648 | 559 | fd_tvb_data=fd_head->tvb_data; |
649 | | /* loop over all partial fragments and free any tvbuffs */ |
650 | 559 | fd = fd_head->next; |
651 | 911 | while (fd != NULL) { |
652 | 352 | fd = fragment_item_free(fd); |
653 | 352 | } |
654 | 559 | g_slice_free(fragment_head, fd_head); |
655 | 559 | g_hash_table_remove(table->fragment_table, key); |
656 | | |
657 | 559 | return fd_tvb_data; |
658 | 559 | } |
659 | | |
660 | | /* This function is used to check if there is partial or completed reassembly state |
661 | | * matching this packet. I.e. Is there reassembly going on or not for this packet? |
662 | | */ |
663 | | fragment_head * |
664 | | fragment_get(reassembly_table *table, const packet_info *pinfo, |
665 | | const uint32_t id, const void *data) |
666 | 1.82k | { |
667 | 1.82k | return lookup_fd_head(table, pinfo, id, data, NULL); |
668 | 1.82k | } |
669 | | |
670 | | fragment_head * |
671 | | fragment_get_reassembled_id(reassembly_table *table, const packet_info *pinfo, |
672 | | const uint32_t id) |
673 | 162 | { |
674 | 162 | fragment_head *fd_head; |
675 | 162 | reassembled_key key; |
676 | | |
677 | | /* create key to search hash with */ |
678 | 162 | key.frame = pinfo->num; |
679 | 162 | key.id = id; |
680 | 162 | fd_head = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &key); |
681 | | |
682 | 162 | return fd_head; |
683 | 162 | } |
684 | | |
685 | | /* To specify the offset for the fragment numbering, the first fragment is added with 0, and |
686 | | * afterwards this offset is set. All additional calls to off_seq_check will calculate |
687 | | * the number in sequence in regards to the offset */ |
688 | | void |
689 | | fragment_add_seq_offset(reassembly_table *table, const packet_info *pinfo, const uint32_t id, |
690 | | const void *data, const uint32_t fragment_offset) |
691 | 6 | { |
692 | 6 | fragment_head *fd_head; |
693 | | |
694 | 6 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL); |
695 | 6 | if (!fd_head) |
696 | 1 | return; |
697 | | |
698 | | /* Resetting the offset is not allowed */ |
699 | 5 | if ( fd_head->fragment_nr_offset != 0 ) |
700 | 0 | return; |
701 | | |
702 | 5 | fd_head->fragment_nr_offset = fragment_offset; |
703 | 5 | } |
704 | | |
705 | | static void |
706 | | update_first_gap(fragment_head *fd_head, fragment_item *inserted, bool multi_insert) |
707 | 19.6k | { |
708 | 19.6k | uint32_t frag_end = inserted->offset + inserted->len; |
709 | 19.6k | fragment_item *iter; |
710 | 19.6k | uint32_t contiguous; |
711 | | |
712 | 19.6k | if (inserted->offset > fd_head->contiguous_len) { |
713 | | /* first inserted node is after first gap */ |
714 | 6.65k | return; |
715 | 12.9k | } else if (fd_head->first_gap == NULL) { |
716 | | /* we haven't seen first fragment yet */ |
717 | 4.16k | if (inserted->offset != 0) { |
718 | | /* inserted node is not first fragment */ |
719 | 0 | return; |
720 | 0 | } |
721 | 4.16k | contiguous = inserted->len; |
722 | 4.16k | iter = inserted; |
723 | 8.81k | } else { |
724 | 8.81k | contiguous = MAX(fd_head->contiguous_len, frag_end); |
725 | 8.81k | iter = multi_insert ? inserted : fd_head->first_gap; |
726 | 8.81k | } |
727 | | |
728 | 21.3k | while (iter->next) { |
729 | 12.0k | if (iter->next->offset > contiguous) { |
730 | 3.64k | break; |
731 | 3.64k | } |
732 | 8.36k | iter = iter->next; |
733 | 8.36k | contiguous = MAX(contiguous, iter->offset + iter->len); |
734 | 8.36k | } |
735 | | |
736 | | /* iter is either pointing to last fragment before gap or tail */ |
737 | 12.9k | fd_head->first_gap = iter; |
738 | 12.9k | fd_head->contiguous_len = contiguous; |
739 | 12.9k | } |
740 | | |
741 | | /* |
742 | | * Keeping first gap and contiguous length in sync significantly speeds up |
743 | | * LINK_FRAG() when fragments in capture file are mostly ordered. However, when |
744 | | * fragments are removed from the list, the first gap can point to fragments |
745 | | * that were either moved to another list or freed. Therefore when any fragment |
746 | | * before first gap is removed, the first gap (and contiguous length) must be |
747 | | * invalidated. |
748 | | */ |
749 | | static void fragment_reset_first_gap(fragment_head *fd_head) |
750 | 234 | { |
751 | 234 | fd_head->first_gap = NULL; |
752 | 234 | fd_head->contiguous_len = 0; |
753 | 234 | if (fd_head->next) { |
754 | 234 | bool multi_insert = (fd_head->next->next != NULL); |
755 | 234 | update_first_gap(fd_head, fd_head->next, multi_insert); |
756 | 234 | } |
757 | 234 | } |
758 | | |
759 | | /* |
760 | | * Determines whether list modification requires first gap reset. On entry |
761 | | * modified is NULL if all elements were removed, otherwise it points to |
762 | | * element (reachable from fd_head) whose next pointer was changed. |
763 | | */ |
764 | | static void fragment_items_removed(fragment_head *fd_head, fragment_item *modified) |
765 | 848 | { |
766 | 848 | if ((fd_head->first_gap == modified) || |
767 | 614 | ((modified != NULL) && (modified->offset > fd_head->contiguous_len))) { |
768 | | /* Removed elements were after first gap */ |
769 | 614 | return; |
770 | 614 | } |
771 | 234 | fragment_reset_first_gap(fd_head); |
772 | 234 | } |
773 | | |
774 | | /* |
775 | | * For use with fragment_add (and not the fragment_add_seq functions). |
776 | | * When the reassembled result is wrong (perhaps it needs to be extended), this |
777 | | * function clears any previous reassembly result, allowing the new reassembled |
778 | | * length to be set again. |
779 | | */ |
780 | | static void |
781 | | fragment_reset_defragmentation(fragment_head *fd_head) |
782 | 22 | { |
783 | | /* Caller must ensure that this function is only called when |
784 | | * defragmentation is safe to undo. */ |
785 | 22 | DISSECTOR_ASSERT(fd_head->flags & FD_DEFRAGMENTED); |
786 | | |
787 | 22 | fd_head->flags &= ~(FD_DEFRAGMENTED|FD_PARTIAL_REASSEMBLY|FD_DATALEN_SET); |
788 | | /* We have to clear TOOLONGFRAGMENT and MULTIPLETAILS because they |
789 | | * might change when extending the reassembly. If those flags weren't |
790 | | * set on the head, they're not set on any item. */ |
791 | 22 | if (fd_head->flags & (FD_TOOLONGFRAGMENT|FD_MULTIPLETAILS)) { |
792 | 0 | for (fragment_item *fd_i = fd_head->next; fd_i; fd_i = fd_i->next) { |
793 | 0 | fd_i->flags &= (~FD_TOOLONGFRAGMENT) & (~FD_MULTIPLETAILS); |
794 | 0 | } |
795 | 0 | fd_head->flags &= ~(FD_TOOLONGFRAGMENT|FD_MULTIPLETAILS); |
796 | 0 | } |
797 | 22 | fd_head->datalen = 0; |
798 | 22 | fd_head->reassembled_in = 0; |
799 | 22 | fd_head->reas_in_layer_num = 0; |
800 | 22 | } |
801 | | |
802 | | /* This function can be used to explicitly set the total length (if known) |
803 | | * for reassembly of a PDU. |
804 | | * This is useful for reassembly of PDUs where one may have the total length specified |
805 | | * in the first fragment instead of as for, say, IPv4 where a flag indicates which |
806 | | * is the last fragment. |
807 | | * |
808 | | * Such protocols might fragment_add with a more_frags==true for every fragment |
809 | | * and just tell the reassembly engine the expected total length of the reassembled data |
810 | | * using fragment_set_tot_len immediately after doing fragment_add for the first packet. |
811 | | * |
812 | | * Note that for FD_BLOCKSEQUENCE tot_len is the index for the tail fragment. |
813 | | * i.e. since the block numbers start at 0, if we specify tot_len==2, that |
814 | | * actually means we want to defragment 3 blocks, block 0, 1 and 2. |
815 | | */ |
816 | | void |
817 | | fragment_set_tot_len(reassembly_table *table, const packet_info *pinfo, |
818 | | const uint32_t id, const void *data, const uint32_t tot_len) |
819 | 166 | { |
820 | 166 | fragment_head *fd_head; |
821 | 166 | fragment_item *fd; |
822 | 166 | uint32_t max_offset = 0; |
823 | | |
824 | 166 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL); |
825 | 166 | if (!fd_head) |
826 | 1 | return; |
827 | | |
828 | | /* If we're setting a block sequence number, verify that it |
829 | | * doesn't conflict with values set by existing fragments. |
830 | | * XXX - eliminate this check? |
831 | | */ |
832 | 165 | if (fd_head->flags & FD_BLOCKSEQUENCE) { |
833 | 62 | for (fd = fd_head->next; fd; fd = fd->next) { |
834 | 37 | if (fd->offset > max_offset) { |
835 | 10 | max_offset = fd->offset; |
836 | 10 | if (max_offset > tot_len) { |
837 | 3 | fd_head->error = "Bad total reassembly block count"; |
838 | 3 | THROW_MESSAGE(ReassemblyError, fd_head->error); |
839 | 3 | } |
840 | 10 | } |
841 | 37 | } |
842 | 25 | } |
843 | | |
844 | 165 | if (fd_head->flags & FD_DEFRAGMENTED) { |
845 | 1 | if (max_offset != tot_len) { |
846 | 1 | fd_head->error = "Defragmented complete but total length not satisfied"; |
847 | 1 | THROW_MESSAGE(ReassemblyError, fd_head->error); |
848 | 1 | } |
849 | 1 | } |
850 | | |
851 | | /* We got this far so the value is sane. */ |
852 | 165 | fd_head->datalen = tot_len; |
853 | 165 | fd_head->flags |= FD_DATALEN_SET; |
854 | 165 | } |
855 | | |
856 | | void |
857 | | fragment_reset_tot_len(reassembly_table *table, const packet_info *pinfo, |
858 | | const uint32_t id, const void *data, const uint32_t tot_len) |
859 | 0 | { |
860 | 0 | fragment_head *fd_head; |
861 | |
|
862 | 0 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL); |
863 | 0 | if (!fd_head) |
864 | 0 | return; |
865 | | |
866 | | /* |
867 | | * If FD_PARTIAL_REASSEMBLY is set, it would make the next fragment_add |
868 | | * call set the reassembled length based on the fragment offset and |
869 | | * length. As the length is known now, be sure to disable that magic. |
870 | | */ |
871 | 0 | fd_head->flags &= ~FD_PARTIAL_REASSEMBLY; |
872 | | |
873 | | /* If the length is already as expected, there is nothing else to do. */ |
874 | 0 | if (tot_len == fd_head->datalen) |
875 | 0 | return; |
876 | | |
877 | 0 | if (fd_head->flags & FD_DEFRAGMENTED) { |
878 | | /* |
879 | | * Fragments were reassembled before, clear it to allow |
880 | | * increasing the reassembled length. |
881 | | */ |
882 | 0 | fragment_reset_defragmentation(fd_head); |
883 | 0 | } |
884 | |
|
885 | 0 | fd_head->datalen = tot_len; |
886 | 0 | fd_head->flags |= FD_DATALEN_SET; |
887 | 0 | } |
888 | | |
889 | | void |
890 | | fragment_truncate(reassembly_table *table, const packet_info *pinfo, |
891 | | const uint32_t id, const void *data, const uint32_t tot_len) |
892 | | |
893 | 22 | { |
894 | 22 | tvbuff_t *old_tvb_data; |
895 | 22 | fragment_head *fd_head; |
896 | | |
897 | 22 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL); |
898 | 22 | if (!fd_head) |
899 | 0 | return; |
900 | | |
901 | | /* Caller must ensure that this function is only called when |
902 | | * we are defragmented. */ |
903 | 22 | DISSECTOR_ASSERT(fd_head->flags & FD_DEFRAGMENTED); |
904 | | |
905 | | /* |
906 | | * If FD_PARTIAL_REASSEMBLY is set, it would make the next fragment_add |
907 | | * call set the reassembled length based on the fragment offset and |
908 | | * length. As the length is known now, be sure to disable that magic. |
909 | | */ |
910 | 22 | fd_head->flags &= ~FD_PARTIAL_REASSEMBLY; |
911 | | |
912 | | /* If the length is already as expected, there is nothing else to do. */ |
913 | 22 | if (tot_len == fd_head->datalen) |
914 | 0 | return; |
915 | | |
916 | 22 | DISSECTOR_ASSERT(fd_head->datalen > tot_len); |
917 | | |
918 | 22 | old_tvb_data=fd_head->tvb_data; |
919 | 22 | fd_head->tvb_data = tvb_clone_offset_len(old_tvb_data, 0, tot_len); |
920 | 22 | tvb_set_free_cb(fd_head->tvb_data, g_free); |
921 | | |
922 | 22 | if (old_tvb_data) |
923 | 22 | tvb_add_to_chain(fd_head->tvb_data, old_tvb_data); |
924 | 22 | fd_head->datalen = tot_len; |
925 | | |
926 | | /* Keep the fragments before the split point, dividing any if |
927 | | * necessary. |
928 | | * XXX: In rare cases, there might be fragments marked as overlap that |
929 | | * have data both before and after the split point, and which only |
930 | | * overlap after the split point. In that case, after dividing the |
931 | | * fragments the first part no longer overlap. |
932 | | * However, at this point we can't test for overlap conflicts, |
933 | | * so we'll just leave the overlap flags as-is. |
934 | | */ |
935 | 22 | fd_head->flags &= ~(FD_OVERLAP|FD_OVERLAPCONFLICT|FD_TOOLONGFRAGMENT|FD_MULTIPLETAILS); |
936 | 22 | fragment_item *fd_i, *prev_fd = NULL; |
937 | 61 | for (fd_i = fd_head->next; fd_i && (fd_i->offset < tot_len); fd_i = fd_i->next) { |
938 | 39 | fd_i->flags &= ~(FD_TOOLONGFRAGMENT|FD_MULTIPLETAILS); |
939 | | /* Check for the split point occurring in the middle of the |
940 | | * fragment. */ |
941 | 39 | if (fd_i->offset + fd_i->len > tot_len) { |
942 | 0 | fd_i->len = tot_len - fd_i->offset; |
943 | 0 | } |
944 | 39 | fd_head->flags |= fd_i->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT); |
945 | 39 | prev_fd = fd_i; |
946 | | |
947 | | /* Below should do nothing since this is already defragmented */ |
948 | 39 | fragment_item_free_tvb(fd_i); |
949 | 39 | } |
950 | | |
951 | | /* Remove all the other fragments, as they are past the split point. */ |
952 | 22 | if (prev_fd) { |
953 | 22 | prev_fd->next = NULL; |
954 | 22 | } else { |
955 | 0 | fd_head->next = NULL; |
956 | 0 | } |
957 | 22 | fd_head->contiguous_len = MIN(fd_head->contiguous_len, tot_len); |
958 | 22 | fragment_items_removed(fd_head, prev_fd); |
959 | 44 | while (fd_i != NULL) { |
960 | 22 | fd_i = fragment_item_free(fd_i); |
961 | 22 | } |
962 | 22 | } |
963 | | |
964 | | uint32_t |
965 | | fragment_get_tot_len(reassembly_table *table, const packet_info *pinfo, |
966 | | const uint32_t id, const void *data) |
967 | 272 | { |
968 | 272 | fragment_head *fd_head; |
969 | | |
970 | 272 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL); |
971 | | |
972 | 272 | if(fd_head){ |
973 | 257 | return fd_head->datalen; |
974 | 257 | } |
975 | | |
976 | 15 | return 0; |
977 | 272 | } |
978 | | |
979 | | /* This function will set the partial reassembly flag for a fh. |
980 | | When this function is called, the fh MUST already exist, i.e. |
981 | | the fh MUST be created by the initial call to fragment_add() before |
982 | | this function is called. |
983 | | Also note that this function MUST be called to indicate a fh will be |
984 | | extended (increase the already stored data) |
985 | | */ |
986 | | |
987 | | void |
988 | | fragment_set_partial_reassembly(reassembly_table *table, |
989 | | const packet_info *pinfo, const uint32_t id, |
990 | | const void *data) |
991 | 22 | { |
992 | 22 | fragment_head *fd_head; |
993 | | |
994 | 22 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL); |
995 | | |
996 | | /* |
997 | | * XXX - why not do all the stuff done early in "fragment_add_work()", |
998 | | * turning off FD_DEFRAGMENTED and pointing the fragments' data |
999 | | * pointers to the appropriate part of the already-reassembled |
1000 | | * data, and clearing the data length and "reassembled in" frame |
1001 | | * number, here? We currently have a hack in the TCP dissector |
1002 | | * not to set the "reassembled in" value if the "partial reassembly" |
1003 | | * flag is set, so that in the first pass through the packets |
1004 | | * we don't falsely set a packet as reassembled in that packet |
1005 | | * if the dissector decided that even more reassembly was needed. |
1006 | | */ |
1007 | 22 | if(fd_head){ |
1008 | 22 | fd_head->flags |= FD_PARTIAL_REASSEMBLY; |
1009 | 22 | } |
1010 | 22 | } |
1011 | | |
1012 | | /* |
1013 | | * This function gets rid of an entry from a fragment table, given |
1014 | | * a pointer to the key for that entry. |
1015 | | * |
1016 | | * The key freeing routine will be called by g_hash_table_remove(). |
1017 | | */ |
1018 | | static void |
1019 | | fragment_unhash(reassembly_table *table, void *key) |
1020 | 2.75k | { |
1021 | | /* |
1022 | | * Remove the entry from the fragment table. |
1023 | | */ |
1024 | 2.75k | g_hash_table_remove(table->fragment_table, key); |
1025 | 2.75k | } |
1026 | | |
1027 | | /* |
1028 | | * This function adds fragment_head structure to a reassembled-packet |
1029 | | * hash table, using the frame numbers of each of the frames from |
1030 | | * which it was reassembled as keys, and sets the "reassembled_in" |
1031 | | * frame number. |
1032 | | */ |
1033 | | static void |
1034 | | fragment_reassembled(reassembly_table *table, fragment_head *fd_head, |
1035 | | const packet_info *pinfo, const uint32_t id) |
1036 | 3.35k | { |
1037 | 3.35k | reassembled_key *new_key; |
1038 | 3.35k | fragment_item *fd; |
1039 | | |
1040 | 3.35k | fd_head->ref_count = 0; |
1041 | 3.35k | if (fd_head->next == NULL) { |
1042 | | /* |
1043 | | * This was not fragmented, so there's no fragment |
1044 | | * table; just hash it using the current frame number. |
1045 | | */ |
1046 | 1.38k | new_key = g_slice_new(reassembled_key); |
1047 | 1.38k | new_key->frame = pinfo->num; |
1048 | 1.38k | new_key->id = id; |
1049 | 1.38k | reassembled_table_insert(table->reassembled_table, new_key, fd_head); |
1050 | 1.96k | } else { |
1051 | | /* |
1052 | | * Hash it with the frame numbers for all the frames. |
1053 | | */ |
1054 | 8.94k | for (fd = fd_head->next; fd != NULL; fd = fd->next){ |
1055 | 6.97k | new_key = g_slice_new(reassembled_key); |
1056 | 6.97k | new_key->frame = fd->frame; |
1057 | 6.97k | new_key->id = id; |
1058 | 6.97k | reassembled_table_insert(table->reassembled_table, new_key, fd_head); |
1059 | 6.97k | } |
1060 | 1.96k | } |
1061 | 3.35k | fd_head->flags |= FD_DEFRAGMENTED; |
1062 | 3.35k | fd_head->reassembled_in = pinfo->num; |
1063 | 3.35k | fd_head->reas_in_layer_num = pinfo->curr_layer_num; |
1064 | 3.35k | } |
1065 | | |
1066 | | /* |
1067 | | * This function is a variant of the above for the single sequence |
1068 | | * case, using id+offset (i.e., the original sequence number) for the id |
1069 | | * in the key. |
1070 | | */ |
1071 | | static void |
1072 | | fragment_reassembled_single(reassembly_table *table, fragment_head *fd_head, |
1073 | | const packet_info *pinfo, const uint32_t id) |
1074 | 787 | { |
1075 | 787 | reassembled_key *new_key; |
1076 | 787 | fragment_item *fd; |
1077 | | |
1078 | 787 | fd_head->ref_count = 0; |
1079 | 787 | if (fd_head->next == NULL) { |
1080 | | /* |
1081 | | * This was not fragmented, so there's no fragment |
1082 | | * table; just hash it using the current frame number. |
1083 | | */ |
1084 | 0 | new_key = g_slice_new(reassembled_key); |
1085 | 0 | new_key->frame = pinfo->num; |
1086 | 0 | new_key->id = id; |
1087 | 0 | reassembled_table_insert(table->reassembled_table, new_key, fd_head); |
1088 | 787 | } else { |
1089 | | /* |
1090 | | * Hash it with the frame numbers for all the frames. |
1091 | | */ |
1092 | 2.31k | for (fd = fd_head->next; fd != NULL; fd = fd->next){ |
1093 | 1.53k | new_key = g_slice_new(reassembled_key); |
1094 | 1.53k | new_key->frame = fd->frame; |
1095 | 1.53k | new_key->id = id + fd->offset; |
1096 | 1.53k | reassembled_table_insert(table->reassembled_table, new_key, fd_head); |
1097 | 1.53k | } |
1098 | 787 | } |
1099 | 787 | fd_head->flags |= FD_DEFRAGMENTED; |
1100 | 787 | fd_head->reassembled_in = pinfo->num; |
1101 | 787 | fd_head->reas_in_layer_num = pinfo->curr_layer_num; |
1102 | 787 | } |
1103 | | |
1104 | | static void |
1105 | | LINK_FRAG(fragment_head *fd_head,fragment_item *fd) |
1106 | 18.8k | { |
1107 | 18.8k | fragment_item *fd_i; |
1108 | | |
1109 | | /* add fragment to list, keep list sorted */ |
1110 | | /* It is important that new fragments are added *after* any |
1111 | | * fragments with the same offset (as currently done.) */ |
1112 | 18.8k | if (fd_head->next == NULL || fd->offset < fd_head->next->offset) { |
1113 | | /* New first fragment */ |
1114 | 5.49k | fd->next = fd_head->next; |
1115 | 5.49k | fd_head->next = fd; |
1116 | 13.3k | } else { |
1117 | 13.3k | fd_i = fd_head->next; |
1118 | 13.3k | if (fd_head->first_gap != NULL) { |
1119 | 9.70k | if (fd->offset >= fd_head->first_gap->offset) { |
1120 | | /* fragment is after first gap */ |
1121 | 6.73k | fd_i = fd_head->first_gap; |
1122 | 6.73k | } |
1123 | 9.70k | } |
1124 | 2.20M | for(; fd_i->next; fd_i=fd_i->next) { |
1125 | 2.19M | if (fd->offset < fd_i->next->offset ) |
1126 | 5.98k | break; |
1127 | 2.19M | } |
1128 | 13.3k | fd->next = fd_i->next; |
1129 | 13.3k | fd_i->next = fd; |
1130 | 13.3k | } |
1131 | | |
1132 | 18.8k | update_first_gap(fd_head, fd, false); |
1133 | 18.8k | } |
1134 | | |
1135 | | static void |
1136 | | MERGE_FRAG(fragment_head *fd_head, fragment_item *fd) |
1137 | 432 | { |
1138 | 432 | fragment_item *fd_i, *tmp, *inserted = fd; |
1139 | 432 | bool multi_insert; |
1140 | | |
1141 | 432 | if (fd == NULL) return; |
1142 | | |
1143 | 432 | multi_insert = (fd->next != NULL); |
1144 | | |
1145 | 432 | if (fd_head->next == NULL) { |
1146 | 287 | fd_head->next = fd; |
1147 | 287 | update_first_gap(fd_head, fd, multi_insert); |
1148 | 287 | return; |
1149 | 287 | } |
1150 | | |
1151 | 145 | if ((fd_head->first_gap != NULL) && |
1152 | 120 | (fd->offset >= fd_head->first_gap->offset)) { |
1153 | | /* all new fragments go after first gap */ |
1154 | 51 | fd_i = fd_head->first_gap; |
1155 | 94 | } else { |
1156 | | /* at least one new fragment goes before first gap */ |
1157 | 94 | if (fd->offset < fd_head->next->offset) { |
1158 | | /* inserted fragment is new head, "swap" the lists */ |
1159 | 11 | tmp = fd_head->next; |
1160 | 11 | fd_head->next = fd; |
1161 | 11 | fd = tmp; |
1162 | 11 | } |
1163 | 94 | fd_i = fd_head->next; |
1164 | 94 | } |
1165 | | |
1166 | | /* Traverse the list linked to fragment head ("main" list), checking if |
1167 | | * fd pointer ("merge" list) should go before or after fd_i->next. Swap |
1168 | | * fd_i->next ("main") and fd pointers ("merge") if "merge" list should |
1169 | | * go before iterated element (fd_i). After the swap what formerly was |
1170 | | * "merge" list essentially becomes part of "main" list (just detached |
1171 | | * element, i.e. fd, is now head of new "merge list"). |
1172 | | */ |
1173 | 674 | for(; fd_i->next; fd_i=fd_i->next) { |
1174 | 529 | if (fd->offset < fd_i->next->offset) { |
1175 | 75 | tmp = fd_i->next; |
1176 | 75 | fd_i->next = fd; |
1177 | 75 | fd = tmp; |
1178 | 75 | } |
1179 | 529 | } |
1180 | | /* Reached "main" list end, attach remaining elements */ |
1181 | 145 | fd_i->next = fd; |
1182 | | |
1183 | 145 | update_first_gap(fd_head, inserted, multi_insert); |
1184 | 145 | } |
1185 | | |
1186 | | /* |
1187 | | * This function adds a new fragment to the fragment hash table. |
1188 | | * If this is the first fragment seen for this datagram, a new entry |
1189 | | * is created in the hash table, otherwise this fragment is just added |
1190 | | * to the linked list of fragments for this packet. |
1191 | | * The list of fragments for a specific datagram is kept sorted for |
1192 | | * easier handling. |
1193 | | * |
1194 | | * Returns a pointer to the head of the fragment data list if we have all the |
1195 | | * fragments, NULL otherwise. |
1196 | | * |
1197 | | * This function assumes frag_offset being a byte offset into the defragment |
1198 | | * packet. |
1199 | | * |
1200 | | * 01-2002 |
1201 | | * Once the fh is defragmented (= FD_DEFRAGMENTED set), it can be |
1202 | | * extended using the FD_PARTIAL_REASSEMBLY flag. This flag should be set |
1203 | | * using fragment_set_partial_reassembly() before calling fragment_add |
1204 | | * with the new fragment. FD_TOOLONGFRAGMENT and FD_MULTIPLETAILS flags |
1205 | | * are lowered when a new extension process is started. |
1206 | | */ |
1207 | | static bool |
1208 | | fragment_add_work(fragment_head *fd_head, tvbuff_t *tvb, const int offset, |
1209 | | const packet_info *pinfo, const uint32_t frag_offset, |
1210 | | const uint32_t frag_data_len, const bool more_frags, |
1211 | | const uint32_t frag_frame, const bool allow_overlaps) |
1212 | 9.65k | { |
1213 | 9.65k | fragment_item *fd; |
1214 | 9.65k | fragment_item *fd_i; |
1215 | 9.65k | uint32_t dfpos, fraglen, overlap; |
1216 | 9.65k | tvbuff_t *old_tvb_data; |
1217 | 9.65k | uint8_t *data; |
1218 | | |
1219 | | /* create new fd describing this fragment */ |
1220 | 9.65k | fd = new_fragment_item(frag_frame, frag_offset, frag_data_len); |
1221 | | |
1222 | | /* |
1223 | | * Are we adding to an already-completed reassembly? |
1224 | | */ |
1225 | 9.65k | if (fd_head->flags & FD_DEFRAGMENTED) { |
1226 | | /* |
1227 | | * Yes. Does this fragment go past the end of the results |
1228 | | * of that reassembly? |
1229 | | */ |
1230 | 22 | if (frag_offset + frag_data_len > fd_head->datalen) { |
1231 | | /* |
1232 | | * Yes. Have we been requested to continue reassembly? |
1233 | | */ |
1234 | 22 | if (fd_head->flags & FD_PARTIAL_REASSEMBLY) { |
1235 | | /* |
1236 | | * Yes. Set flag in already empty fds & |
1237 | | * point old fds to malloc'ed data. |
1238 | | */ |
1239 | 22 | fragment_reset_defragmentation(fd_head); |
1240 | 22 | } else if (!allow_overlaps) { |
1241 | | /* |
1242 | | * No. Bail out since we have no idea what to |
1243 | | * do with this fragment (and if we keep going |
1244 | | * we'll run past the end of a buffer sooner |
1245 | | * or later). |
1246 | | */ |
1247 | 0 | g_slice_free(fragment_item, fd); |
1248 | | |
1249 | | /* |
1250 | | * This is an attempt to add a fragment to a |
1251 | | * reassembly that had already completed. |
1252 | | * If it had no error, we don't want to |
1253 | | * mark it with an error, and if it had an |
1254 | | * error, we don't want to overwrite it, so |
1255 | | * we don't set fd_head->error. |
1256 | | */ |
1257 | 0 | if (frag_offset >= fd_head->datalen) { |
1258 | | /* |
1259 | | * The fragment starts past the end |
1260 | | * of the reassembled data. |
1261 | | */ |
1262 | 0 | THROW_MESSAGE(ReassemblyError, "New fragment past old data limits"); |
1263 | 0 | } else { |
1264 | | /* |
1265 | | * The fragment starts before the end |
1266 | | * of the reassembled data, but |
1267 | | * runs past the end. That could |
1268 | | * just be a retransmission with extra |
1269 | | * data, but the calling dissector |
1270 | | * didn't set FD_PARTIAL_REASSEMBLY |
1271 | | * so it won't be handled correctly. |
1272 | | * |
1273 | | * XXX: We could set FD_TOOLONGFRAGMENT |
1274 | | * below instead. |
1275 | | */ |
1276 | 0 | THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)"); |
1277 | 0 | } |
1278 | 0 | } |
1279 | 22 | } else { |
1280 | | /* |
1281 | | * No. That means it overlaps the completed reassembly. |
1282 | | * This is probably a retransmission and normal |
1283 | | * behavior. (If not, it's because the dissector |
1284 | | * doesn't handle reused sequence numbers correctly, |
1285 | | * e.g. #10503). Handle below. |
1286 | | */ |
1287 | 0 | } |
1288 | 22 | } |
1289 | | |
1290 | | /* Do this after we may have bailed out (above) so that we don't leave |
1291 | | * fd_head->frame in a bad state if we do */ |
1292 | 9.65k | if (fd->frame > fd_head->frame) |
1293 | 3.41k | fd_head->frame = fd->frame; |
1294 | | |
1295 | 9.65k | if (!more_frags) { |
1296 | | /* |
1297 | | * This is the tail fragment in the sequence. |
1298 | | */ |
1299 | 1.80k | if (fd_head->flags & FD_DATALEN_SET) { |
1300 | | /* ok we have already seen other tails for this packet |
1301 | | * it might be a duplicate. |
1302 | | */ |
1303 | 658 | if (fd_head->datalen != (fd->offset + fd->len) ){ |
1304 | | /* Oops, this tail indicates a different packet |
1305 | | * len than the previous ones. Something's wrong. |
1306 | | */ |
1307 | 555 | fd->flags |= FD_MULTIPLETAILS; |
1308 | 555 | fd_head->flags |= FD_MULTIPLETAILS; |
1309 | 555 | } |
1310 | 1.14k | } else { |
1311 | | /* This was the first tail fragment; now we know |
1312 | | * what the length of the packet should be. |
1313 | | */ |
1314 | 1.14k | fd_head->datalen = fd->offset + fd->len; |
1315 | 1.14k | fd_head->flags |= FD_DATALEN_SET; |
1316 | 1.14k | } |
1317 | 1.80k | } |
1318 | | |
1319 | | |
1320 | | |
1321 | | /* If the packet is already defragmented, this MUST be an overlap. |
1322 | | * The entire defragmented packet is in fd_head->data. |
1323 | | * Even if we have previously defragmented this packet, we still |
1324 | | * check it. Someone might play overlap and TTL games. |
1325 | | */ |
1326 | 9.65k | if (fd_head->flags & FD_DEFRAGMENTED) { |
1327 | 0 | uint32_t end_offset = fd->offset + fd->len; |
1328 | 0 | fd->flags |= FD_OVERLAP|FD_DEFRAGMENTED; |
1329 | 0 | fd_head->flags |= FD_OVERLAP; |
1330 | | /* make sure it's not too long */ |
1331 | | /* XXX: We probably don't call this, unlike the _seq() |
1332 | | * functions, because we throw an exception above. |
1333 | | */ |
1334 | 0 | if (end_offset > fd_head->datalen || end_offset < fd->offset || end_offset < fd->len) { |
1335 | 0 | fd->flags |= FD_TOOLONGFRAGMENT; |
1336 | 0 | fd_head->flags |= FD_TOOLONGFRAGMENT; |
1337 | 0 | } |
1338 | | /* make sure it doesn't conflict with previous data */ |
1339 | 0 | else if ( tvb_memeql(fd_head->tvb_data, fd->offset, |
1340 | 0 | tvb_get_ptr(tvb,offset,fd->len),fd->len) ){ |
1341 | 0 | fd->flags |= FD_OVERLAPCONFLICT; |
1342 | 0 | fd_head->flags |= FD_OVERLAPCONFLICT; |
1343 | 0 | } |
1344 | | /* it was just an overlap, link it and return */ |
1345 | 0 | LINK_FRAG(fd_head,fd); |
1346 | 0 | return true; |
1347 | 0 | } |
1348 | | |
1349 | | |
1350 | | |
1351 | | /* If we have reached this point, the packet is not defragmented yet. |
1352 | | * Save all payload in a buffer until we can defragment. |
1353 | | */ |
1354 | 9.65k | if (!tvb_bytes_exist(tvb, offset, fd->len)) { |
1355 | 0 | g_slice_free(fragment_item, fd); |
1356 | 0 | THROW(BoundsError); |
1357 | 0 | } |
1358 | 9.65k | fd->tvb_data = tvb_clone_offset_len(tvb, offset, fd->len); |
1359 | 9.65k | LINK_FRAG(fd_head,fd); |
1360 | | |
1361 | | |
1362 | 9.65k | if( !(fd_head->flags & FD_DATALEN_SET) ){ |
1363 | | /* if we don't know the datalen, there are still missing |
1364 | | * packets. Cheaper than the check below. |
1365 | | */ |
1366 | 4.54k | return false; |
1367 | 4.54k | } |
1368 | | |
1369 | | /* Check if we have received the entire fragment. */ |
1370 | 5.10k | if (fd_head->contiguous_len < fd_head->datalen) { |
1371 | | /* |
1372 | | * The amount of contiguous data we have is less than the |
1373 | | * amount of data we're trying to reassemble, so we haven't |
1374 | | * received all packets yet. |
1375 | | */ |
1376 | 4.10k | return false; |
1377 | 4.10k | } |
1378 | | |
1379 | | /* we have received an entire packet, defragment it and |
1380 | | * free all fragments |
1381 | | */ |
1382 | | /* store old data just in case */ |
1383 | 1.00k | old_tvb_data=fd_head->tvb_data; |
1384 | 1.00k | data = (uint8_t *) g_malloc(fd_head->datalen); |
1385 | 1.00k | fd_head->tvb_data = tvb_new_real_data(data, fd_head->datalen, fd_head->datalen); |
1386 | 1.00k | tvb_set_free_cb(fd_head->tvb_data, g_free); |
1387 | | |
1388 | 1.00k | dfpos = old_tvb_data ? tvb_captured_length(old_tvb_data) : 0; |
1389 | 1.00k | if (dfpos) { |
1390 | 17 | memcpy(data, tvb_get_ptr(old_tvb_data, 0, dfpos), MIN(fd_head->datalen, dfpos)); |
1391 | 17 | } |
1392 | | /* add all data fragments that have not already been added, i.e., |
1393 | | * if the defragmentation was reset after partial reassembly, |
1394 | | * but we have to check the previously added ones as well for |
1395 | | * TOOLONGFRAGMENT as the datalen has changed. */ |
1396 | 5.28k | for (fd_i=fd_head->next;fd_i;fd_i=fd_i->next) { |
1397 | 4.27k | if (fd_i->len) { |
1398 | | /* |
1399 | | * The contiguous length check above also |
1400 | | * ensures that the only gaps that exist here |
1401 | | * are ones where a fragment starts past the |
1402 | | * end of the reassembled datagram, and there's |
1403 | | * a gap between the previous fragment and |
1404 | | * that fragment. |
1405 | | * |
1406 | | * A "DESEGMENT_UNTIL_FIN" was involved wherein the |
1407 | | * FIN packet had an offset less than the highest |
1408 | | * fragment offset seen. [Seen from a fuzz-test: |
1409 | | * bug #2470]). |
1410 | | * |
1411 | | * Note that the "overlap" compare must only be |
1412 | | * done for fragments with (offset+len) <= fd_head->datalen |
1413 | | * and thus within the newly g_malloc'd buffer. |
1414 | | */ |
1415 | | |
1416 | 2.16k | if (fd_i->offset >= fd_head->datalen) { |
1417 | | /* |
1418 | | * Fragment starts after the end |
1419 | | * of the reassembled packet. |
1420 | | * |
1421 | | * This can happen if the length was |
1422 | | * set after the offending fragment |
1423 | | * was added to the reassembly. |
1424 | | * |
1425 | | * Flag this fragment, but don't |
1426 | | * try to extract any data from |
1427 | | * it, as there's no place to put |
1428 | | * it. |
1429 | | * |
1430 | | * XXX - add different flag value |
1431 | | * for this. |
1432 | | */ |
1433 | 208 | fd_i->flags |= FD_TOOLONGFRAGMENT; |
1434 | 208 | fd_head->flags |= FD_TOOLONGFRAGMENT; |
1435 | 1.95k | } else if (fd_i->offset + fd_i->len < fd_i->offset) { |
1436 | | /* Integer overflow, unhandled by rest of |
1437 | | * code so error out. This check handles |
1438 | | * all possible remaining overflows. |
1439 | | */ |
1440 | 0 | fd_head->error = "offset + len < offset"; |
1441 | 1.95k | } else { |
1442 | 1.95k | fraglen = fd_i->len; |
1443 | 1.95k | if (fd_i->offset + fraglen > fd_head->datalen) { |
1444 | | /* |
1445 | | * Fragment goes past the end |
1446 | | * of the packet, as indicated |
1447 | | * by the last fragment. |
1448 | | * |
1449 | | * This can happen if the |
1450 | | * length was set after the |
1451 | | * offending fragment was |
1452 | | * added to the reassembly. |
1453 | | * |
1454 | | * Mark it as such, and only |
1455 | | * copy from it what fits in |
1456 | | * the packet. |
1457 | | */ |
1458 | 92 | fd_i->flags |= FD_TOOLONGFRAGMENT; |
1459 | 92 | fd_head->flags |= FD_TOOLONGFRAGMENT; |
1460 | 92 | fraglen = fd_head->datalen - fd_i->offset; |
1461 | 92 | } |
1462 | 1.95k | if (fd_i->flags & FD_DEFRAGMENTED) { |
1463 | | /* If we already added the item the |
1464 | | * previous time, we're done. */ |
1465 | 30 | continue; |
1466 | 30 | } |
1467 | 1.92k | if (!fd_i->tvb_data) { |
1468 | | /* We check this here because |
1469 | | * previously added items now |
1470 | | * have no data (not an error). */ |
1471 | 0 | fd_head->error = "no data"; |
1472 | 0 | continue; |
1473 | 0 | } |
1474 | 1.92k | overlap = 0; |
1475 | 1.92k | if (fd_i->offset < dfpos) { |
1476 | | /* The new item's begins before the |
1477 | | * existing end. How much overlap? */ |
1478 | 633 | overlap = dfpos - fd_i->offset; |
1479 | | /* duplicate/retransmission/overlap */ |
1480 | 633 | uint32_t cmp_len = MIN(fd_i->len,overlap); |
1481 | | |
1482 | 633 | fd_i->flags |= FD_OVERLAP; |
1483 | 633 | fd_head->flags |= FD_OVERLAP; |
1484 | 633 | if ( memcmp(data + fd_i->offset, |
1485 | 633 | tvb_get_ptr(fd_i->tvb_data, 0, cmp_len), |
1486 | 633 | cmp_len) |
1487 | 633 | ) { |
1488 | 461 | fd_i->flags |= FD_OVERLAPCONFLICT; |
1489 | 461 | fd_head->flags |= FD_OVERLAPCONFLICT; |
1490 | 461 | } |
1491 | 633 | } |
1492 | | /* XXX: As in the fragment_add_seq funcs |
1493 | | * like fragment_defragment_and_free() the |
1494 | | * existing behavior does not overwrite |
1495 | | * overlapping bytes even if there is a |
1496 | | * conflict. It only adds new bytes. |
1497 | | * |
1498 | | * Since we only add fragments to a reassembly |
1499 | | * if the reassembly isn't complete, the most |
1500 | | * common case for overlap conflicts is when |
1501 | | * an earlier reassembly isn't fully contained |
1502 | | * in the capture, and we've reused an |
1503 | | * identification number / wrapped around |
1504 | | * offset sequence numbers much later in the |
1505 | | * capture. In that case, we probably *do* |
1506 | | * want to overwrite conflicting bytes, since |
1507 | | * the earlier fragments didn't form a complete |
1508 | | * reassembly and should be effectively thrown |
1509 | | * out rather than mixed with the new ones? |
1510 | | */ |
1511 | 1.92k | if (fd_i->offset + fraglen > dfpos) { |
1512 | 1.31k | memcpy(data+dfpos, |
1513 | 1.31k | tvb_get_ptr(fd_i->tvb_data, overlap, fraglen-overlap), |
1514 | 1.31k | fraglen-overlap); |
1515 | 1.31k | dfpos = fd_i->offset + fraglen; |
1516 | 1.31k | } |
1517 | 1.92k | } |
1518 | | /* Mark that this fragment as used and clear data. */ |
1519 | 2.13k | fd_i->flags |= FD_DEFRAGMENTED; |
1520 | 2.13k | fragment_item_free_tvb(fd_i); |
1521 | 2.13k | } |
1522 | 4.27k | } |
1523 | | |
1524 | 1.00k | if (old_tvb_data) |
1525 | 17 | tvb_add_to_chain(tvb, old_tvb_data); |
1526 | | /* mark this packet as defragmented. |
1527 | | allows us to skip any trailing fragments */ |
1528 | 1.00k | fd_head->flags |= FD_DEFRAGMENTED; |
1529 | 1.00k | fd_head->reassembled_in=pinfo->num; |
1530 | 1.00k | fd_head->reas_in_layer_num = pinfo->curr_layer_num; |
1531 | | |
1532 | | /* we don't throw until here to avoid leaking old_data and others */ |
1533 | 1.00k | if (fd_head->error) { |
1534 | 0 | THROW_MESSAGE(ReassemblyError, fd_head->error); |
1535 | 0 | } |
1536 | | |
1537 | 1.00k | return true; |
1538 | 5.10k | } |
1539 | | |
1540 | | static fragment_head * |
1541 | | fragment_add_common(reassembly_table *table, tvbuff_t *tvb, const int offset, |
1542 | | const packet_info *pinfo, const uint32_t id, |
1543 | | const void *data, const uint32_t frag_offset, |
1544 | | const uint32_t frag_data_len, const bool more_frags, |
1545 | | const bool check_already_added, |
1546 | | const uint32_t frag_frame) |
1547 | 602 | { |
1548 | 602 | fragment_head *fd_head; |
1549 | 602 | fragment_item *fd_item; |
1550 | 602 | bool already_added; |
1551 | | |
1552 | | |
1553 | | /* |
1554 | | * Dissector shouldn't give us garbage tvb info. |
1555 | | * |
1556 | | * XXX - should this code take responsibility for preventing |
1557 | | * reassembly if data is missing due to the packets being |
1558 | | * sliced, rather than leaving it up to dissectors? |
1559 | | */ |
1560 | 602 | DISSECTOR_ASSERT(tvb_bytes_exist(tvb, offset, frag_data_len)); |
1561 | | |
1562 | 602 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL); |
1563 | | |
1564 | | #if 0 |
1565 | | /* debug output of associated fragments. */ |
1566 | | /* leave it here for future debugging sessions */ |
1567 | | if(strcmp(pinfo->current_proto, "DCERPC") == 0) { |
1568 | | printf("proto:%s num:%u id:%u offset:%u len:%u more:%u visited:%u\n", |
1569 | | pinfo->current_proto, pinfo->num, id, frag_offset, frag_data_len, more_frags, pinfo->fd->visited); |
1570 | | if(fd_head != NULL) { |
1571 | | for(fd_item=fd_head->next;fd_item;fd_item=fd_item->next){ |
1572 | | printf("fd_frame:%u fd_offset:%u len:%u datalen:%u\n", |
1573 | | fd_item->frame, fd_item->offset, fd_item->len, fd_item->datalen); |
1574 | | } |
1575 | | } |
1576 | | } |
1577 | | #endif |
1578 | | |
1579 | | /* |
1580 | | * Is this the first pass through the capture? |
1581 | | */ |
1582 | 602 | if (!pinfo->fd->visited) { |
1583 | | /* |
1584 | | * Yes, so we could be doing reassembly. If |
1585 | | * "check_already_added" is true, and fd_head is non-null, |
1586 | | * meaning that this fragment would be added to an |
1587 | | * in-progress reassembly, check if we have seen this |
1588 | | * fragment before, i.e., if we have already added it to |
1589 | | * that reassembly. That can be true even on the first pass |
1590 | | * since we sometimes might call a subdissector multiple |
1591 | | * times. |
1592 | | * |
1593 | | * We check both the frame number and the fragment offset, |
1594 | | * so that we support multiple fragments from the same |
1595 | | * frame being added to the same reassembled PDU. |
1596 | | */ |
1597 | 602 | if (check_already_added && fd_head != NULL) { |
1598 | | /* |
1599 | | * fd_head->frame is the maximum of the frame |
1600 | | * numbers of all the fragments added to this |
1601 | | * reassembly; if this frame is later than that |
1602 | | * frame, we know it hasn't been added yet. |
1603 | | */ |
1604 | 373 | if (frag_frame <= fd_head->frame) { |
1605 | 173 | already_added = false; |
1606 | | /* |
1607 | | * The first item in the reassembly list |
1608 | | * is not a fragment, it's a data structure |
1609 | | * for the reassembled packet, so we |
1610 | | * start checking with the next item. |
1611 | | */ |
1612 | 363 | for (fd_item = fd_head->next; fd_item; |
1613 | 283 | fd_item = fd_item->next) { |
1614 | 283 | if (frag_frame == fd_item->frame && |
1615 | 191 | frag_offset == fd_item->offset) { |
1616 | 93 | already_added = true; |
1617 | 93 | break; |
1618 | 93 | } |
1619 | 283 | } |
1620 | 173 | if (already_added) { |
1621 | | /* |
1622 | | * Have we already finished |
1623 | | * reassembling? |
1624 | | */ |
1625 | 93 | if (fd_head->flags & FD_DEFRAGMENTED) { |
1626 | | /* |
1627 | | * Yes. |
1628 | | * XXX - can this ever happen? |
1629 | | */ |
1630 | 24 | THROW_MESSAGE(ReassemblyError, |
1631 | 24 | "Frame already added in first pass"); |
1632 | 69 | } else { |
1633 | | /* |
1634 | | * No. |
1635 | | */ |
1636 | 69 | return NULL; |
1637 | 69 | } |
1638 | 93 | } |
1639 | 173 | } |
1640 | 373 | } |
1641 | 602 | } else { |
1642 | | /* |
1643 | | * No, so we've already done all the reassembly and added |
1644 | | * all the fragments. Do we have a reassembly and, if so, |
1645 | | * have we finished reassembling? |
1646 | | */ |
1647 | 0 | if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) { |
1648 | | /* |
1649 | | * Yes. This is probably being done after the |
1650 | | * first pass, and we've already done the work |
1651 | | * on the first pass. |
1652 | | * |
1653 | | * If the reassembly got a fatal error, throw that |
1654 | | * error again. |
1655 | | */ |
1656 | 0 | if (fd_head->error) |
1657 | 0 | THROW_MESSAGE(ReassemblyError, fd_head->error); |
1658 | | |
1659 | | /* |
1660 | | * Is it later in the capture than all of the |
1661 | | * fragments in the reassembly? |
1662 | | */ |
1663 | 0 | if (frag_frame > fd_head->frame) { |
1664 | | /* |
1665 | | * Yes, so report this as a problem, |
1666 | | * possibly a retransmission. |
1667 | | */ |
1668 | 0 | THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)"); |
1669 | 0 | } |
1670 | | |
1671 | | /* |
1672 | | * Does this fragment go past the end of the |
1673 | | * results of that reassembly? |
1674 | | */ |
1675 | 0 | if (frag_offset + frag_data_len > fd_head->datalen) { |
1676 | | /* |
1677 | | * Yes. |
1678 | | */ |
1679 | 0 | if (frag_offset >= fd_head->datalen) { |
1680 | | /* |
1681 | | * The fragment starts past the |
1682 | | * end of the reassembled data. |
1683 | | */ |
1684 | 0 | THROW_MESSAGE(ReassemblyError, "New fragment past old data limits"); |
1685 | 0 | } else { |
1686 | | /* |
1687 | | * The fragment starts before the end |
1688 | | * of the reassembled data, but |
1689 | | * runs past the end. That could |
1690 | | * just be a retransmission. |
1691 | | */ |
1692 | 0 | THROW_MESSAGE(ReassemblyError, "New fragment overlaps old data (retransmission?)"); |
1693 | 0 | } |
1694 | 0 | } |
1695 | |
|
1696 | 0 | return fd_head; |
1697 | 0 | } else { |
1698 | | /* |
1699 | | * No. |
1700 | | */ |
1701 | 0 | return NULL; |
1702 | 0 | } |
1703 | 0 | } |
1704 | | |
1705 | 533 | if (fd_head==NULL){ |
1706 | | /* not found, this must be the first snooped fragment for this |
1707 | | * packet. Create list-head. |
1708 | | */ |
1709 | 229 | fd_head = new_head(0); |
1710 | | |
1711 | | /* |
1712 | | * Insert it into the hash table. |
1713 | | */ |
1714 | 229 | insert_fd_head(table, fd_head, pinfo, id, data); |
1715 | 229 | } |
1716 | | |
1717 | 533 | if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset, |
1718 | 533 | frag_data_len, more_frags, frag_frame, false)) { |
1719 | | /* |
1720 | | * Reassembly is complete. |
1721 | | */ |
1722 | 77 | return fd_head; |
1723 | 456 | } else { |
1724 | | /* |
1725 | | * Reassembly isn't complete. |
1726 | | */ |
1727 | 456 | return NULL; |
1728 | 456 | } |
1729 | 533 | } |
1730 | | |
1731 | | fragment_head * |
1732 | | fragment_add(reassembly_table *table, tvbuff_t *tvb, const int offset, |
1733 | | const packet_info *pinfo, const uint32_t id, const void *data, |
1734 | | const uint32_t frag_offset, const uint32_t frag_data_len, |
1735 | | const bool more_frags) |
1736 | 602 | { |
1737 | 602 | return fragment_add_common(table, tvb, offset, pinfo, id, data, |
1738 | 602 | frag_offset, frag_data_len, more_frags, true, pinfo->num); |
1739 | 602 | } |
1740 | | |
1741 | | /* |
1742 | | * For use when you can have multiple fragments in the same frame added |
1743 | | * to the same reassembled PDU, e.g. with ONC RPC-over-TCP. |
1744 | | */ |
1745 | | fragment_head * |
1746 | | fragment_add_multiple_ok(reassembly_table *table, tvbuff_t *tvb, |
1747 | | const int offset, const packet_info *pinfo, |
1748 | | const uint32_t id, const void *data, |
1749 | | const uint32_t frag_offset, |
1750 | | const uint32_t frag_data_len, const bool more_frags) |
1751 | 0 | { |
1752 | 0 | return fragment_add_common(table, tvb, offset, pinfo, id, data, |
1753 | 0 | frag_offset, frag_data_len, more_frags, false, pinfo->num); |
1754 | 0 | } |
1755 | | |
1756 | | /* |
1757 | | * For use in protocols like TCP when you are adding an out of order segment |
1758 | | * that arrived in an earlier frame because the correct fragment id could not |
1759 | | * be determined until later. By allowing fd->frame to be different than |
1760 | | * pinfo->num, show_fragment_tree will display the correct fragment numbers. |
1761 | | * |
1762 | | * Note that pinfo is still used to set reassembled_in if we have all the |
1763 | | * fragments, so that results on subsequent passes can be the same as the |
1764 | | * first pass. |
1765 | | */ |
1766 | | fragment_head * |
1767 | | fragment_add_out_of_order(reassembly_table *table, tvbuff_t *tvb, |
1768 | | const int offset, const packet_info *pinfo, |
1769 | | const uint32_t id, const void *data, |
1770 | | const uint32_t frag_offset, |
1771 | | const uint32_t frag_data_len, |
1772 | | const bool more_frags, const uint32_t frag_frame) |
1773 | 0 | { |
1774 | 0 | return fragment_add_common(table, tvb, offset, pinfo, id, data, |
1775 | 0 | frag_offset, frag_data_len, more_frags, true, frag_frame); |
1776 | 0 | } |
1777 | | |
1778 | | fragment_head * |
1779 | | fragment_add_check_with_fallback(reassembly_table *table, tvbuff_t *tvb, const int offset, |
1780 | | const packet_info *pinfo, const uint32_t id, |
1781 | | const void *data, const uint32_t frag_offset, |
1782 | | const uint32_t frag_data_len, const bool more_frags, |
1783 | | const uint32_t fallback_frame) |
1784 | 9.25k | { |
1785 | 9.25k | reassembled_key reass_key; |
1786 | 9.25k | fragment_head *fd_head; |
1787 | 9.25k | void *orig_key; |
1788 | 9.25k | bool late_retransmission = false; |
1789 | | |
1790 | | /* |
1791 | | * If this isn't the first pass, look for this frame in the table |
1792 | | * of reassembled packets. |
1793 | | */ |
1794 | 9.25k | if (pinfo->fd->visited) { |
1795 | 0 | reass_key.frame = pinfo->num; |
1796 | 0 | reass_key.id = id; |
1797 | 0 | return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key); |
1798 | 0 | } |
1799 | | |
1800 | | /* Looks up a key in the GHashTable, returning the original key and the associated value |
1801 | | * and a bool which is true if the key was found. This is useful if you need to free |
1802 | | * the memory allocated for the original key, for example before calling g_hash_table_remove() |
1803 | | */ |
1804 | 9.25k | fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key); |
1805 | 9.25k | if ((fd_head == NULL) && (fallback_frame != pinfo->num)) { |
1806 | | /* Check if there is completed reassembly reachable from fallback frame */ |
1807 | 0 | reass_key.frame = fallback_frame; |
1808 | 0 | reass_key.id = id; |
1809 | 0 | fd_head = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key); |
1810 | 0 | if (fd_head != NULL) { |
1811 | | /* Found completely reassembled packet, hash it with current frame number */ |
1812 | 0 | reassembled_key *new_key = g_slice_new(reassembled_key); |
1813 | 0 | new_key->frame = pinfo->num; |
1814 | 0 | new_key->id = id; |
1815 | 0 | reassembled_table_insert(table->reassembled_table, new_key, fd_head); |
1816 | 0 | late_retransmission = true; |
1817 | 0 | } |
1818 | 0 | } |
1819 | 9.25k | if (fd_head == NULL) { |
1820 | | /* not found, this must be the first snooped fragment for this |
1821 | | * packet. Create list-head. |
1822 | | */ |
1823 | 1.52k | fd_head = new_head(0); |
1824 | | |
1825 | | /* |
1826 | | * Save the key, for unhashing it later. |
1827 | | */ |
1828 | 1.52k | orig_key = insert_fd_head(table, fd_head, pinfo, id, data); |
1829 | 1.52k | } |
1830 | | |
1831 | | /* |
1832 | | * If this is a short frame, then we can't, and don't, do |
1833 | | * reassembly on it. We just give up. |
1834 | | */ |
1835 | 9.25k | if (!tvb_bytes_exist(tvb, offset, frag_data_len)) { |
1836 | 115 | return NULL; |
1837 | 115 | } |
1838 | | |
1839 | 9.14k | if (fragment_add_work(fd_head, tvb, offset, pinfo, frag_offset, |
1840 | 9.14k | frag_data_len, more_frags, pinfo->num, late_retransmission)) { |
1841 | | /* Nothing left to do if it was a late retransmission */ |
1842 | 927 | if (late_retransmission) { |
1843 | 0 | return fd_head; |
1844 | 0 | } |
1845 | | /* |
1846 | | * Reassembly is complete. |
1847 | | * Remove this from the table of in-progress |
1848 | | * reassemblies, add it to the table of |
1849 | | * reassembled packets, and return it. |
1850 | | */ |
1851 | | |
1852 | | /* |
1853 | | * Remove this from the table of in-progress reassemblies, |
1854 | | * and free up any memory used for it in that table. |
1855 | | */ |
1856 | 927 | fragment_unhash(table, orig_key); |
1857 | | |
1858 | | /* |
1859 | | * Add this item to the table of reassembled packets. |
1860 | | */ |
1861 | 927 | fragment_reassembled(table, fd_head, pinfo, id); |
1862 | 927 | return fd_head; |
1863 | 8.21k | } else { |
1864 | | /* |
1865 | | * Reassembly isn't complete. |
1866 | | */ |
1867 | 8.21k | return NULL; |
1868 | 8.21k | } |
1869 | 9.14k | } |
1870 | | |
1871 | | fragment_head * |
1872 | | fragment_add_check(reassembly_table *table, tvbuff_t *tvb, const int offset, |
1873 | | const packet_info *pinfo, const uint32_t id, |
1874 | | const void *data, const uint32_t frag_offset, |
1875 | | const uint32_t frag_data_len, const bool more_frags) |
1876 | 9.25k | { |
1877 | 9.25k | return fragment_add_check_with_fallback(table, tvb, offset, pinfo, id, data, |
1878 | 9.25k | frag_offset, frag_data_len, more_frags, pinfo->num); |
1879 | 9.25k | } |
1880 | | |
1881 | | static void |
1882 | | fragment_defragment_and_free (fragment_head *fd_head, const packet_info *pinfo) |
1883 | 1.83k | { |
1884 | 1.83k | fragment_item *fd_i = NULL; |
1885 | 1.83k | fragment_item *last_fd = NULL; |
1886 | 1.83k | uint32_t dfpos = 0, old_dfpos = 0, size = 0; |
1887 | 1.83k | tvbuff_t *old_tvb_data = NULL; |
1888 | 1.83k | uint8_t *data; |
1889 | | |
1890 | 6.30k | for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) { |
1891 | 4.47k | if(!last_fd || last_fd->offset!=fd_i->offset){ |
1892 | 3.73k | size+=fd_i->len; |
1893 | 3.73k | } |
1894 | 4.47k | last_fd=fd_i; |
1895 | 4.47k | } |
1896 | | |
1897 | | /* store old data in case the fd_i->data pointers refer to it */ |
1898 | 1.83k | old_tvb_data=fd_head->tvb_data; |
1899 | 1.83k | data = (uint8_t *) g_malloc(size); |
1900 | 1.83k | fd_head->tvb_data = tvb_new_real_data(data, size, size); |
1901 | 1.83k | tvb_set_free_cb(fd_head->tvb_data, g_free); |
1902 | 1.83k | fd_head->len = size; /* record size for caller */ |
1903 | | |
1904 | 1.83k | if (old_tvb_data) { |
1905 | 0 | dfpos = tvb_captured_length(old_tvb_data); |
1906 | 0 | memcpy(data, tvb_get_ptr(old_tvb_data, 0, dfpos), MIN(size, dfpos)); |
1907 | 0 | } |
1908 | | |
1909 | | /* add all data fragments */ |
1910 | 1.83k | last_fd=NULL; |
1911 | 1.83k | dfpos = 0; |
1912 | 6.30k | for (fd_i=fd_head->next; fd_i; fd_i=fd_i->next) { |
1913 | 4.47k | if (fd_i->len) { |
1914 | 4.29k | if(!last_fd || last_fd->offset != fd_i->offset) { |
1915 | | /* First fragment or in-sequence fragment */ |
1916 | 3.57k | if (!(fd_i->flags & FD_DEFRAGMENTED)) { |
1917 | | /* Already copied on the first pass */ |
1918 | 3.57k | memcpy(data+dfpos, tvb_get_ptr(fd_i->tvb_data, 0, fd_i->len), fd_i->len); |
1919 | 3.57k | } |
1920 | | /* But we need the position for overlap calculation of new fragments */ |
1921 | 3.57k | old_dfpos = dfpos; |
1922 | 3.57k | dfpos += fd_i->len; |
1923 | 3.57k | } else if (!(fd_i->flags & FD_DEFRAGMENTED)){ |
1924 | | /* duplicate/retransmission/overlap */ |
1925 | | /* Note that overlaps of old fragments were already calculated. */ |
1926 | 721 | fd_i->flags |= FD_OVERLAP; |
1927 | 721 | fd_head->flags |= FD_OVERLAP; |
1928 | 721 | if((old_dfpos + fd_i->len != dfpos) |
1929 | 596 | || tvb_memeql(fd_i->tvb_data, 0, data+old_dfpos, fd_i->len) ) { |
1930 | 596 | fd_i->flags |= FD_OVERLAPCONFLICT; |
1931 | 596 | fd_head->flags |= FD_OVERLAPCONFLICT; |
1932 | 596 | } |
1933 | 721 | } |
1934 | 4.29k | fragment_item_free_tvb(fd_i); |
1935 | 4.29k | fd_i->flags |= FD_DEFRAGMENTED; |
1936 | 4.29k | } |
1937 | 4.47k | last_fd=fd_i; |
1938 | 4.47k | } |
1939 | | |
1940 | 1.83k | if (old_tvb_data) |
1941 | 0 | tvb_free(old_tvb_data); |
1942 | | |
1943 | | /* mark this packet as defragmented. |
1944 | | * allows us to skip any trailing fragments. |
1945 | | */ |
1946 | 1.83k | fd_head->flags |= FD_DEFRAGMENTED; |
1947 | 1.83k | fd_head->reassembled_in=pinfo->num; |
1948 | 1.83k | fd_head->reas_in_layer_num = pinfo->curr_layer_num; |
1949 | 1.83k | } |
1950 | | |
1951 | | /* |
1952 | | * This function adds a new fragment to the entry for a reassembly |
1953 | | * operation. |
1954 | | * |
1955 | | * The list of fragments for a specific datagram is kept sorted for |
1956 | | * easier handling. |
1957 | | * |
1958 | | * Returns true if we have all the fragments, false otherwise. |
1959 | | * |
1960 | | * This function assumes frag_number being a block sequence number. |
1961 | | * The bsn for the first block is 0. |
1962 | | */ |
1963 | | static bool |
1964 | | fragment_add_seq_work(fragment_head *fd_head, tvbuff_t *tvb, const int offset, |
1965 | | const packet_info *pinfo, const uint32_t frag_number, |
1966 | | const uint32_t frag_data_len, const bool more_frags) |
1967 | 9.31k | { |
1968 | 9.31k | fragment_item *fd; |
1969 | 9.31k | fragment_item *fd_i; |
1970 | 9.31k | fragment_item *last_fd; |
1971 | 9.31k | uint32_t max, dfpos; |
1972 | 9.31k | uint32_t frag_number_work; |
1973 | | |
1974 | | /* Enables the use of fragment sequence numbers, which do not start with 0 */ |
1975 | 9.31k | frag_number_work = frag_number; |
1976 | 9.31k | if ( fd_head->fragment_nr_offset != 0 ) |
1977 | 0 | if ( frag_number_work >= fd_head->fragment_nr_offset ) |
1978 | 0 | frag_number_work = frag_number - fd_head->fragment_nr_offset; |
1979 | | |
1980 | | /* if the partial reassembly flag has been set, and we are extending |
1981 | | * the pdu, un-reassemble the pdu. This means pointing old fds to malloc'ed data. |
1982 | | */ |
1983 | 9.31k | if(fd_head->flags & FD_DEFRAGMENTED && frag_number_work >= fd_head->datalen && |
1984 | 22 | fd_head->flags & FD_PARTIAL_REASSEMBLY){ |
1985 | |
|
1986 | 0 | fragment_reset_defragmentation(fd_head); |
1987 | 0 | } |
1988 | | |
1989 | | |
1990 | | /* create new fd describing this fragment */ |
1991 | 9.31k | fd = new_fragment_item(pinfo->num, frag_number_work, frag_data_len); |
1992 | | |
1993 | | /* fd_head->frame is the maximum of the frame numbers of all the |
1994 | | * fragments added to the reassembly. */ |
1995 | 9.31k | if (fd->frame > fd_head->frame) |
1996 | 5.23k | fd_head->frame = fd->frame; |
1997 | | |
1998 | 9.31k | if (!more_frags) { |
1999 | | /* |
2000 | | * This is the tail fragment in the sequence. |
2001 | | */ |
2002 | 4.10k | if (fd_head->flags&FD_DATALEN_SET) { |
2003 | | /* ok we have already seen other tails for this packet |
2004 | | * it might be a duplicate. |
2005 | | */ |
2006 | 1.60k | if (fd_head->datalen != fd->offset ){ |
2007 | | /* Oops, this tail indicates a different packet |
2008 | | * len than the previous ones. Something's wrong. |
2009 | | */ |
2010 | 260 | fd->flags |= FD_MULTIPLETAILS; |
2011 | 260 | fd_head->flags |= FD_MULTIPLETAILS; |
2012 | 260 | } |
2013 | 2.50k | } else { |
2014 | | /* this was the first tail fragment, now we know the |
2015 | | * sequence number of that fragment (which is NOT |
2016 | | * the length of the packet!) |
2017 | | */ |
2018 | 2.50k | fd_head->datalen = fd->offset; |
2019 | 2.50k | fd_head->flags |= FD_DATALEN_SET; |
2020 | 2.50k | } |
2021 | 4.10k | } |
2022 | | |
2023 | | /* If the packet is already defragmented, this MUST be an overlap. |
2024 | | * The entire defragmented packet is in fd_head->data |
2025 | | * Even if we have previously defragmented this packet, we still check |
2026 | | * check it. Someone might play overlap and TTL games. |
2027 | | */ |
2028 | 9.31k | if (fd_head->flags & FD_DEFRAGMENTED) { |
2029 | 44 | fd->flags |= FD_OVERLAP|FD_DEFRAGMENTED; |
2030 | 44 | fd_head->flags |= FD_OVERLAP; |
2031 | | |
2032 | | /* make sure it's not past the end */ |
2033 | 44 | if (fd->offset > fd_head->datalen) { |
2034 | | /* new fragment comes after the end */ |
2035 | 4 | fd->flags |= FD_TOOLONGFRAGMENT; |
2036 | 4 | fd_head->flags |= FD_TOOLONGFRAGMENT; |
2037 | 4 | LINK_FRAG(fd_head,fd); |
2038 | 4 | return true; |
2039 | 4 | } |
2040 | | /* make sure it doesn't conflict with previous data */ |
2041 | 40 | dfpos=0; |
2042 | 40 | last_fd=NULL; |
2043 | 149 | for (fd_i=fd_head->next;fd_i && (fd_i->offset!=fd->offset);fd_i=fd_i->next) { |
2044 | 109 | if (!last_fd || last_fd->offset!=fd_i->offset){ |
2045 | 24 | dfpos += fd_i->len; |
2046 | 24 | } |
2047 | 109 | last_fd=fd_i; |
2048 | 109 | } |
2049 | 40 | if(fd_i){ |
2050 | | /* new fragment overlaps existing fragment */ |
2051 | 40 | if(fd_i->len!=fd->len){ |
2052 | | /* |
2053 | | * They have different lengths; this |
2054 | | * is definitely a conflict. |
2055 | | */ |
2056 | 20 | fd->flags |= FD_OVERLAPCONFLICT; |
2057 | 20 | fd_head->flags |= FD_OVERLAPCONFLICT; |
2058 | 20 | LINK_FRAG(fd_head,fd); |
2059 | 20 | return true; |
2060 | 20 | } |
2061 | 20 | DISSECTOR_ASSERT(fd_head->len >= dfpos + fd->len); |
2062 | 20 | if (tvb_memeql(fd_head->tvb_data, dfpos, |
2063 | 20 | tvb_get_ptr(tvb,offset,fd->len),fd->len) ){ |
2064 | | /* |
2065 | | * They have the same length, but the |
2066 | | * data isn't the same. |
2067 | | */ |
2068 | 4 | fd->flags |= FD_OVERLAPCONFLICT; |
2069 | 4 | fd_head->flags |= FD_OVERLAPCONFLICT; |
2070 | 4 | LINK_FRAG(fd_head,fd); |
2071 | 4 | return true; |
2072 | 4 | } |
2073 | | /* it was just an overlap, link it and return */ |
2074 | 16 | LINK_FRAG(fd_head,fd); |
2075 | 16 | return true; |
2076 | 20 | } else { |
2077 | | /* |
2078 | | * New fragment doesn't overlap an existing |
2079 | | * fragment - there was presumably a gap in |
2080 | | * the sequence number space. |
2081 | | * |
2082 | | * XXX - what should we do here? Is it always |
2083 | | * the case that there are no gaps, or are there |
2084 | | * protcols using sequence numbers where there |
2085 | | * can be gaps? |
2086 | | * |
2087 | | * If the former, the check below for having |
2088 | | * received all the fragments should check for |
2089 | | * holes in the sequence number space and for the |
2090 | | * first sequence number being 0. If we do that, |
2091 | | * the only way we can get here is if this fragment |
2092 | | * is past the end of the sequence number space - |
2093 | | * but the check for "fd->offset > fd_head->datalen" |
2094 | | * would have caught that above, so it can't happen. |
2095 | | * |
2096 | | * If the latter, we don't have a good way of |
2097 | | * knowing whether reassembly is complete if we |
2098 | | * get packet out of order such that the "last" |
2099 | | * fragment doesn't show up last - but, unless |
2100 | | * in-order reliable delivery of fragments is |
2101 | | * guaranteed, an implementation of the protocol |
2102 | | * has no way of knowing whether reassembly is |
2103 | | * complete, either. |
2104 | | * |
2105 | | * For now, we just link the fragment in and |
2106 | | * return. |
2107 | | */ |
2108 | 0 | LINK_FRAG(fd_head,fd); |
2109 | 0 | return true; |
2110 | 0 | } |
2111 | 40 | } |
2112 | | |
2113 | | /* If we have reached this point, the packet is not defragmented yet. |
2114 | | * Save all payload in a buffer until we can defragment. |
2115 | | */ |
2116 | | /* check len, there may be a fragment with 0 len, that is actually the tail */ |
2117 | 9.27k | if (fd->len) { |
2118 | 8.89k | if (!tvb_bytes_exist(tvb, offset, fd->len)) { |
2119 | | /* abort if we didn't capture the entire fragment due |
2120 | | * to a too-short snapshot length */ |
2121 | 129 | g_slice_free(fragment_item, fd); |
2122 | 129 | return false; |
2123 | 129 | } |
2124 | | |
2125 | 8.76k | fd->tvb_data = tvb_clone_offset_len(tvb, offset, fd->len); |
2126 | 8.76k | } |
2127 | 9.14k | LINK_FRAG(fd_head,fd); |
2128 | | |
2129 | | |
2130 | 9.14k | if( !(fd_head->flags & FD_DATALEN_SET) ){ |
2131 | | /* if we don't know the sequence number of the last fragment, |
2132 | | * there are definitely still missing packets. Cheaper than |
2133 | | * the check below. |
2134 | | */ |
2135 | 4.72k | return false; |
2136 | 4.72k | } |
2137 | | |
2138 | | |
2139 | | /* check if we have received the entire fragment |
2140 | | * this is easy since the list is sorted and the head is faked. |
2141 | | * common case the whole list is scanned. |
2142 | | */ |
2143 | 4.42k | max = 0; |
2144 | 27.6k | for(fd_i=fd_head->next;fd_i;fd_i=fd_i->next) { |
2145 | 23.1k | if ( fd_i->offset==max ){ |
2146 | 4.43k | max++; |
2147 | 4.43k | } |
2148 | 23.1k | } |
2149 | | /* max will now be datalen+1 if all fragments have been seen */ |
2150 | | |
2151 | 4.42k | if (max <= fd_head->datalen) { |
2152 | | /* we have not received all packets yet */ |
2153 | 2.58k | return false; |
2154 | 2.58k | } |
2155 | | |
2156 | | |
2157 | 1.83k | if (max > (fd_head->datalen+1)) { |
2158 | | /* oops, too long fragment detected */ |
2159 | 2 | fd->flags |= FD_TOOLONGFRAGMENT; |
2160 | 2 | fd_head->flags |= FD_TOOLONGFRAGMENT; |
2161 | 2 | } |
2162 | | |
2163 | | |
2164 | | /* we have received an entire packet, defragment it and |
2165 | | * free all fragments |
2166 | | */ |
2167 | 1.83k | fragment_defragment_and_free(fd_head, pinfo); |
2168 | | |
2169 | 1.83k | return true; |
2170 | 4.42k | } |
2171 | | |
2172 | | /* |
2173 | | * This function adds a new fragment to the fragment hash table. |
2174 | | * If this is the first fragment seen for this datagram, a new entry |
2175 | | * is created in the hash table, otherwise this fragment is just added |
2176 | | * to the linked list of fragments for this packet. |
2177 | | * |
2178 | | * Returns a pointer to the head of the fragment data list if we have all the |
2179 | | * fragments, NULL otherwise. |
2180 | | * |
2181 | | * This function assumes frag_number being a block sequence number. |
2182 | | * The bsn for the first block is 0. |
2183 | | */ |
2184 | | static fragment_head * |
2185 | | fragment_add_seq_common(reassembly_table *table, tvbuff_t *tvb, |
2186 | | const int offset, const packet_info *pinfo, |
2187 | | const uint32_t id, const void *data, |
2188 | | uint32_t frag_number, const uint32_t frag_data_len, |
2189 | | const bool more_frags, const uint32_t flags, |
2190 | | void * *orig_keyp) |
2191 | 10.7k | { |
2192 | 10.7k | fragment_head *fd_head; |
2193 | 10.7k | void *orig_key; |
2194 | | |
2195 | 10.7k | fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key); |
2196 | | |
2197 | | /* have we already seen this frame ?*/ |
2198 | 10.7k | if (pinfo->fd->visited) { |
2199 | 0 | if (fd_head != NULL && fd_head->flags & FD_DEFRAGMENTED) { |
2200 | 0 | if (orig_keyp != NULL) |
2201 | 0 | *orig_keyp = orig_key; |
2202 | 0 | return fd_head; |
2203 | 0 | } else { |
2204 | 0 | return NULL; |
2205 | 0 | } |
2206 | 0 | } |
2207 | | |
2208 | 10.7k | if (fd_head==NULL){ |
2209 | | /* not found, this must be the first snooped fragment for this |
2210 | | * packet. Create list-head. |
2211 | | */ |
2212 | 3.47k | fd_head = new_head(FD_BLOCKSEQUENCE); |
2213 | | |
2214 | 3.47k | if((flags & (REASSEMBLE_FLAGS_NO_FRAG_NUMBER|REASSEMBLE_FLAGS_802_11_HACK)) |
2215 | 2.76k | && !more_frags) { |
2216 | | /* |
2217 | | * This is the last fragment for this packet, and |
2218 | | * is the only one we've seen. |
2219 | | * |
2220 | | * Either we don't have sequence numbers, in which |
2221 | | * case we assume this is the first fragment for |
2222 | | * this packet, or we're doing special 802.11 |
2223 | | * processing, in which case we assume it's one |
2224 | | * of those reassembled packets with a non-zero |
2225 | | * fragment number (see packet-80211.c); just |
2226 | | * return a pointer to the head of the list; |
2227 | | * fragment_add_seq_check will then add it to the table |
2228 | | * of reassembled packets. |
2229 | | */ |
2230 | 1.38k | if (orig_keyp != NULL) |
2231 | 1.38k | *orig_keyp = NULL; |
2232 | | /* To save memory, we don't actually copy the |
2233 | | * fragment from the tvbuff to the fragment, and in |
2234 | | * process_reassembled_data just return back a subset |
2235 | | * of the original tvbuff (which must be passed in). |
2236 | | */ |
2237 | 1.38k | fd_head->len = frag_data_len; |
2238 | 1.38k | fd_head->reassembled_in=pinfo->num; |
2239 | 1.38k | fd_head->reas_in_layer_num = pinfo->curr_layer_num; |
2240 | 1.38k | return fd_head; |
2241 | 1.38k | } |
2242 | | |
2243 | 2.08k | orig_key = insert_fd_head(table, fd_head, pinfo, id, data); |
2244 | 2.08k | if (orig_keyp != NULL) |
2245 | 1.98k | *orig_keyp = orig_key; |
2246 | | |
2247 | | /* |
2248 | | * If we weren't given an initial fragment number, |
2249 | | * make it 0. |
2250 | | */ |
2251 | 2.08k | if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER) |
2252 | 1.37k | frag_number = 0; |
2253 | 7.22k | } else { |
2254 | 7.22k | if (orig_keyp != NULL) |
2255 | 6.98k | *orig_keyp = orig_key; |
2256 | | |
2257 | 7.22k | if (flags & REASSEMBLE_FLAGS_NO_FRAG_NUMBER) { |
2258 | 2.63k | fragment_item *fd; |
2259 | | /* |
2260 | | * If we weren't given an initial fragment number, |
2261 | | * use the next expected fragment number as the fragment |
2262 | | * number for this fragment. |
2263 | | */ |
2264 | 14.2k | for (fd = fd_head->next; fd != NULL; fd = fd->next) { |
2265 | 11.6k | if (fd->next == NULL) |
2266 | 2.61k | frag_number = fd->offset + 1; |
2267 | 11.6k | } |
2268 | 2.63k | } |
2269 | 7.22k | } |
2270 | | |
2271 | 9.31k | if (fragment_add_seq_work(fd_head, tvb, offset, pinfo, |
2272 | 9.31k | frag_number, frag_data_len, more_frags)) { |
2273 | | /* |
2274 | | * Reassembly is complete. |
2275 | | */ |
2276 | 1.87k | return fd_head; |
2277 | 7.43k | } else { |
2278 | | /* |
2279 | | * Reassembly isn't complete. |
2280 | | */ |
2281 | 7.43k | return NULL; |
2282 | 7.43k | } |
2283 | 9.31k | } |
2284 | | |
2285 | | fragment_head * |
2286 | | fragment_add_seq(reassembly_table *table, tvbuff_t *tvb, const int offset, |
2287 | | const packet_info *pinfo, const uint32_t id, const void *data, |
2288 | | const uint32_t frag_number, const uint32_t frag_data_len, |
2289 | | const bool more_frags, const uint32_t flags) |
2290 | 351 | { |
2291 | 351 | return fragment_add_seq_common(table, tvb, offset, pinfo, id, data, |
2292 | 351 | frag_number, frag_data_len, |
2293 | 351 | more_frags, flags, NULL); |
2294 | 351 | } |
2295 | | |
2296 | | /* |
2297 | | * This does the work for "fragment_add_seq_check()" and |
2298 | | * "fragment_add_seq_next()". |
2299 | | * |
2300 | | * This function assumes frag_number being a block sequence number. |
2301 | | * The bsn for the first block is 0. |
2302 | | * |
2303 | | * If REASSEMBLE_FLAGS_NO_FRAG_NUMBER, it uses the next expected fragment number |
2304 | | * as the fragment number if there is a reassembly in progress, otherwise |
2305 | | * it uses 0. |
2306 | | * |
2307 | | * If not REASSEMBLE_FLAGS_NO_FRAG_NUMBER, it uses the "frag_number" argument as |
2308 | | * the fragment number. |
2309 | | * |
2310 | | * If this is the first fragment seen for this datagram, a new |
2311 | | * "fragment_head" structure is allocated to refer to the reassembled |
2312 | | * packet. |
2313 | | * |
2314 | | * This fragment is added to the linked list of fragments for this packet. |
2315 | | * |
2316 | | * If "more_frags" is false and REASSEMBLE_FLAGS_802_11_HACK (as the name |
2317 | | * implies, a special hack for 802.11) or REASSEMBLE_FLAGS_NO_FRAG_NUMBER |
2318 | | * (implying messages must be in order since there's no sequence number) are |
2319 | | * set in "flags", then this (one element) list is returned. |
2320 | | * |
2321 | | * If, after processing this fragment, we have all the fragments, |
2322 | | * "fragment_add_seq_check_work()" removes that from the fragment hash |
2323 | | * table if necessary and adds it to the table of reassembled fragments, |
2324 | | * and returns a pointer to the head of the fragment list. |
2325 | | * |
2326 | | * Otherwise, it returns NULL. |
2327 | | * |
2328 | | * XXX - Should we simply return NULL for zero-length fragments? |
2329 | | */ |
2330 | | static fragment_head * |
2331 | | fragment_add_seq_check_work(reassembly_table *table, tvbuff_t *tvb, |
2332 | | const int offset, const packet_info *pinfo, |
2333 | | const uint32_t id, const void *data, |
2334 | | const uint32_t frag_number, |
2335 | | const uint32_t frag_data_len, |
2336 | | const bool more_frags, const uint32_t flags) |
2337 | 6.73k | { |
2338 | 6.73k | reassembled_key reass_key; |
2339 | 6.73k | fragment_head *fd_head; |
2340 | 6.73k | void *orig_key; |
2341 | | |
2342 | | /* |
2343 | | * Have we already seen this frame? |
2344 | | * If so, look for it in the table of reassembled packets. |
2345 | | */ |
2346 | 6.73k | if (pinfo->fd->visited) { |
2347 | 0 | reass_key.frame = pinfo->num; |
2348 | 0 | reass_key.id = id; |
2349 | 0 | return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key); |
2350 | 0 | } |
2351 | | |
2352 | 6.73k | fd_head = fragment_add_seq_common(table, tvb, offset, pinfo, id, data, |
2353 | 6.73k | frag_number, frag_data_len, |
2354 | 6.73k | more_frags, |
2355 | 6.73k | flags, |
2356 | 6.73k | &orig_key); |
2357 | 6.73k | if (fd_head) { |
2358 | | /* |
2359 | | * Reassembly is complete. |
2360 | | * |
2361 | | * If this is in the table of in-progress reassemblies, |
2362 | | * remove it from that table. (It could be that this |
2363 | | * was the first and last fragment, so that no |
2364 | | * reassembly was done.) |
2365 | | */ |
2366 | 2.42k | if (orig_key != NULL) |
2367 | 1.03k | fragment_unhash(table, orig_key); |
2368 | | |
2369 | | /* |
2370 | | * Add this item to the table of reassembled packets. |
2371 | | */ |
2372 | 2.42k | fragment_reassembled(table, fd_head, pinfo, id); |
2373 | 2.42k | return fd_head; |
2374 | 4.30k | } else { |
2375 | | /* |
2376 | | * Reassembly isn't complete. |
2377 | | */ |
2378 | 4.30k | return NULL; |
2379 | 4.30k | } |
2380 | 6.73k | } |
2381 | | |
2382 | | fragment_head * |
2383 | | fragment_add_seq_check(reassembly_table *table, tvbuff_t *tvb, const int offset, |
2384 | | const packet_info *pinfo, const uint32_t id, |
2385 | | const void *data, |
2386 | | const uint32_t frag_number, const uint32_t frag_data_len, |
2387 | | const bool more_frags) |
2388 | 1.33k | { |
2389 | 1.33k | return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data, |
2390 | 1.33k | frag_number, frag_data_len, |
2391 | 1.33k | more_frags, 0); |
2392 | 1.33k | } |
2393 | | |
2394 | | fragment_head * |
2395 | | fragment_add_seq_802_11(reassembly_table *table, tvbuff_t *tvb, |
2396 | | const int offset, const packet_info *pinfo, |
2397 | | const uint32_t id, const void *data, |
2398 | | const uint32_t frag_number, const uint32_t frag_data_len, |
2399 | | const bool more_frags) |
2400 | 0 | { |
2401 | 0 | return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data, |
2402 | 0 | frag_number, frag_data_len, |
2403 | 0 | more_frags, |
2404 | 0 | REASSEMBLE_FLAGS_802_11_HACK); |
2405 | 0 | } |
2406 | | |
2407 | | fragment_head * |
2408 | | fragment_add_seq_next(reassembly_table *table, tvbuff_t *tvb, const int offset, |
2409 | | const packet_info *pinfo, const uint32_t id, |
2410 | | const void *data, const uint32_t frag_data_len, |
2411 | | const bool more_frags) |
2412 | 5.39k | { |
2413 | | /* Use a dummy frag_number (0), it is ignored since |
2414 | | * REASSEMBLE_FLAGS_NO_FRAG_NUMBER is set. */ |
2415 | 5.39k | return fragment_add_seq_check_work(table, tvb, offset, pinfo, id, data, |
2416 | 5.39k | 0, frag_data_len, more_frags, |
2417 | 5.39k | REASSEMBLE_FLAGS_NO_FRAG_NUMBER); |
2418 | 5.39k | } |
2419 | | |
2420 | | static void |
2421 | | fragment_add_seq_single_move(reassembly_table *table, const packet_info *pinfo, |
2422 | | const uint32_t id, const void *data, |
2423 | | const uint32_t offset) |
2424 | 1.36k | { |
2425 | 1.36k | fragment_head *fh, *new_fh; |
2426 | 1.36k | fragment_item *fd, *prev_fd; |
2427 | 1.36k | tvbuff_t *old_tvb_data; |
2428 | 1.36k | if (offset == 0) { |
2429 | 0 | return; |
2430 | 0 | } |
2431 | 1.36k | fh = lookup_fd_head(table, pinfo, id, data, NULL); |
2432 | 1.36k | if (fh == NULL) { |
2433 | | /* Shouldn't be called this way. |
2434 | | * Probably wouldn't hurt to just create fh in this case. */ |
2435 | 0 | ws_assert_not_reached(); |
2436 | 0 | return; |
2437 | 0 | } |
2438 | 1.36k | if (fh->flags & FD_DATALEN_SET && fh->datalen <= offset) { |
2439 | | /* Don't take from past the end. <= because we don't |
2440 | | * want to take a First fragment from the next one |
2441 | | * either */ |
2442 | 21 | return; |
2443 | 21 | } |
2444 | 1.34k | new_fh = lookup_fd_head(table, pinfo, id+offset, data, NULL); |
2445 | 1.34k | if (new_fh != NULL) { |
2446 | | /* Attach to the end of the sorted list. */ |
2447 | 247 | prev_fd = NULL; |
2448 | 3.99k | for(fd = fh->next; fd != NULL; fd=fd->next) { |
2449 | 3.74k | prev_fd = fd; |
2450 | 3.74k | } |
2451 | | /* Don't take a reassembly starting with a First fragment. */ |
2452 | 247 | fd = new_fh->next; |
2453 | 247 | if (fd && fd->offset != 0) { |
2454 | 132 | fragment_item *inserted = fd; |
2455 | 132 | bool multi_insert = (inserted->next != NULL); |
2456 | 132 | if (prev_fd) { |
2457 | 30 | prev_fd->next = fd; |
2458 | 102 | } else { |
2459 | 102 | fh->next = fd; |
2460 | 102 | } |
2461 | 1.02k | for (; fd; fd=fd->next) { |
2462 | 890 | fd->offset += offset; |
2463 | 890 | if (fh->frame < fd->frame) { |
2464 | 179 | fh->frame = fd->frame; |
2465 | 179 | } |
2466 | 890 | } |
2467 | 132 | update_first_gap(fh, inserted, multi_insert); |
2468 | | /* If previously found a Last fragment, |
2469 | | * transfer that info to the new one. */ |
2470 | 132 | if (new_fh->flags & FD_DATALEN_SET) { |
2471 | 126 | fh->flags |= FD_DATALEN_SET; |
2472 | 126 | fh->datalen = new_fh->datalen + offset; |
2473 | 126 | } |
2474 | | /* Now remove and delete */ |
2475 | 132 | new_fh->next = NULL; |
2476 | 132 | old_tvb_data = fragment_delete(table, pinfo, id+offset, data); |
2477 | 132 | if (old_tvb_data) |
2478 | 0 | tvb_free(old_tvb_data); |
2479 | 132 | } |
2480 | 247 | } |
2481 | 1.34k | } |
2482 | | |
2483 | | static fragment_head * |
2484 | | fragment_add_seq_single_work(reassembly_table *table, tvbuff_t *tvb, |
2485 | | const int offset, const packet_info *pinfo, |
2486 | | const uint32_t id, const void* data, |
2487 | | const uint32_t frag_data_len, |
2488 | | const bool first, const bool last, |
2489 | | const uint32_t max_frags, const uint32_t max_age, |
2490 | | const uint32_t flags) |
2491 | 3.62k | { |
2492 | 3.62k | reassembled_key reass_key; |
2493 | 3.62k | tvbuff_t *old_tvb_data; |
2494 | 3.62k | void *orig_key; |
2495 | 3.62k | fragment_head *fh, *new_fh; |
2496 | 3.62k | fragment_item *fd, *prev_fd; |
2497 | 3.62k | uint32_t frag_number, tmp_offset; |
2498 | | /* Have we already seen this frame? |
2499 | | * If so, look for it in the table of reassembled packets. |
2500 | | * Note here we store in the reassembly table by the single sequence |
2501 | | * number rather than the sequence number of the First fragment. */ |
2502 | 3.62k | if (pinfo->fd->visited) { |
2503 | 0 | reass_key.frame = pinfo->num; |
2504 | 0 | reass_key.id = id; |
2505 | 0 | fh = (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key); |
2506 | 0 | return fh; |
2507 | 0 | } |
2508 | | /* First let's figure out where we want to add our new fragment */ |
2509 | 3.62k | fh = NULL; |
2510 | 3.62k | if (first) { |
2511 | 1.43k | frag_number = 0; |
2512 | 1.43k | fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL); |
2513 | 1.43k | if ((flags & REASSEMBLE_FLAGS_AGING) && |
2514 | 1.43k | fh && ((fh->frame + max_age) < pinfo->num)) { |
2515 | 38 | old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data); |
2516 | 38 | if (old_tvb_data) |
2517 | 0 | tvb_free(old_tvb_data); |
2518 | 38 | fh = NULL; |
2519 | 38 | } |
2520 | 1.43k | if (fh == NULL) { |
2521 | | /* Not found. Create list-head. */ |
2522 | 862 | fh = new_head(FD_BLOCKSEQUENCE); |
2523 | 862 | insert_fd_head(table, fh, pinfo, id-frag_number, data); |
2524 | 862 | } |
2525 | | /* As this is the first fragment, we might have added segments |
2526 | | * for this reassembly to the previous one in-progress. */ |
2527 | 1.43k | fd = NULL; |
2528 | 6.11k | for (frag_number=1; frag_number < max_frags; frag_number++) { |
2529 | 5.22k | new_fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL); |
2530 | 5.22k | if (new_fh != NULL) { |
2531 | 547 | prev_fd = NULL; |
2532 | 547 | new_fh->frame = 0; |
2533 | 3.70k | for (fd=new_fh->next; fd && fd->offset < frag_number; fd=fd->next) { |
2534 | 3.15k | prev_fd = fd; |
2535 | 3.15k | if (new_fh->frame < fd->frame) { |
2536 | 1.08k | new_fh->frame = fd->frame; |
2537 | 1.08k | } |
2538 | 3.15k | } |
2539 | 547 | if (prev_fd) { |
2540 | 281 | prev_fd->next = NULL; |
2541 | 281 | } else { |
2542 | 266 | new_fh->next = NULL; |
2543 | 266 | } |
2544 | 547 | fragment_items_removed(new_fh, prev_fd); |
2545 | 547 | break; |
2546 | 547 | } |
2547 | 5.22k | } |
2548 | 1.43k | if (fd != NULL) { |
2549 | 321 | tmp_offset = 0; |
2550 | 1.53k | for (prev_fd = fd; prev_fd; prev_fd = prev_fd->next) { |
2551 | 1.21k | prev_fd->offset -= frag_number; |
2552 | 1.21k | tmp_offset = prev_fd->offset; |
2553 | 1.21k | if (fh->frame < prev_fd->frame) { |
2554 | 243 | fh->frame = prev_fd->frame; |
2555 | 243 | } |
2556 | 1.21k | } |
2557 | 321 | MERGE_FRAG(fh, fd); |
2558 | 321 | if (new_fh != NULL) { |
2559 | | /* If we've moved a Last packet, change datalen. |
2560 | | * Second part of this test prob. redundant? */ |
2561 | 321 | if (new_fh->flags & FD_DATALEN_SET && |
2562 | 174 | new_fh->datalen >= frag_number) { |
2563 | 174 | fh->flags |= FD_DATALEN_SET; |
2564 | 174 | fh->datalen = new_fh->datalen - frag_number; |
2565 | 174 | new_fh->flags &= ~FD_DATALEN_SET; |
2566 | 174 | new_fh->datalen = 0; |
2567 | 174 | } |
2568 | | /* If we've moved all the fragments, |
2569 | | * delete the old head */ |
2570 | 321 | if (new_fh->next == NULL) { |
2571 | 266 | old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data); |
2572 | 266 | if (old_tvb_data) |
2573 | 0 | tvb_free(old_tvb_data); |
2574 | 266 | } |
2575 | 321 | } else { |
2576 | | /* Look forward and take off the next (this is |
2577 | | * necessary in some edge cases where max_frags |
2578 | | * prevented some fragments from going on the |
2579 | | * previous First, but they can go on this one. */ |
2580 | 0 | fragment_add_seq_single_move(table, pinfo, id, |
2581 | 0 | data, tmp_offset); |
2582 | 0 | } |
2583 | 321 | } |
2584 | 1.43k | frag_number = 0; /* For the rest of the function */ |
2585 | 2.18k | } else { |
2586 | 6.90k | for (frag_number=1; frag_number < max_frags; frag_number++) { |
2587 | 6.11k | fh = lookup_fd_head(table, pinfo, id-frag_number, data, NULL); |
2588 | 6.11k | if ((flags & REASSEMBLE_FLAGS_AGING) && |
2589 | 6.11k | fh && ((fh->frame + max_age) < pinfo->num)) { |
2590 | 123 | old_tvb_data = fragment_delete(table, pinfo, id-frag_number, data); |
2591 | 123 | if (old_tvb_data) |
2592 | 0 | tvb_free(old_tvb_data); |
2593 | 123 | fh = NULL; |
2594 | 123 | } |
2595 | 6.11k | if (fh != NULL) { |
2596 | 1.39k | if (fh->flags & FD_DATALEN_SET && |
2597 | 1.05k | fh->datalen < frag_number) { |
2598 | | /* This fragment is after the Last |
2599 | | * fragment, so must go after here. */ |
2600 | 70 | fh = NULL; |
2601 | 70 | } |
2602 | 1.39k | break; |
2603 | 1.39k | } |
2604 | 6.11k | } |
2605 | 2.18k | if (fh == NULL) { /* Didn't find location, use default */ |
2606 | 868 | frag_number = 1; |
2607 | | /* Already looked for frag_number 1, so just create */ |
2608 | 868 | fh = new_head(FD_BLOCKSEQUENCE); |
2609 | 868 | insert_fd_head(table, fh, pinfo, id-frag_number, data); |
2610 | 868 | } |
2611 | 2.18k | } |
2612 | 3.62k | if (last) { |
2613 | | /* Look for fragments past the end set by this Last fragment. */ |
2614 | 2.26k | prev_fd = NULL; |
2615 | 11.5k | for (fd=fh->next; fd && fd->offset <= frag_number; fd=fd->next) { |
2616 | 9.27k | prev_fd = fd; |
2617 | 9.27k | } |
2618 | | /* fd is now all fragments offset > frag_number (the Last). |
2619 | | * It shouldn't have a fragment with offset frag_number+1, |
2620 | | * as that would be a First fragment not marked as such. |
2621 | | * However, this can happen if we had unreassembled fragments |
2622 | | * (missing, or at the start of the capture) and we've also |
2623 | | * looped around on the sequence numbers. It can also happen |
2624 | | * if bit errors mess up Last or First. */ |
2625 | 2.26k | if (fd != NULL) { |
2626 | 279 | if (prev_fd) { |
2627 | 231 | prev_fd->next = NULL; |
2628 | 231 | } else { |
2629 | 48 | fh->next = NULL; |
2630 | 48 | } |
2631 | 279 | fragment_items_removed(fh, prev_fd); |
2632 | 279 | fh->frame = 0; |
2633 | 1.31k | for (prev_fd=fh->next; prev_fd; prev_fd=prev_fd->next) { |
2634 | 1.03k | if (fh->frame < prev_fd->frame) { |
2635 | 342 | fh->frame = prev_fd->frame; |
2636 | 342 | } |
2637 | 1.03k | } |
2638 | 629 | while (fd && fd->offset == frag_number+1) { |
2639 | | /* Definitely have bad data here. Best to |
2640 | | * delete these and leave unreassembled. */ |
2641 | 350 | fd = fragment_item_free(fd); |
2642 | 350 | } |
2643 | 279 | } |
2644 | 2.26k | if (fd != NULL) { |
2645 | | /* Move these onto the next frame. */ |
2646 | 111 | new_fh = lookup_fd_head(table, pinfo, id+1, data, NULL); |
2647 | 111 | if (new_fh==NULL) { |
2648 | | /* Not found. Create list-head. */ |
2649 | 97 | new_fh = new_head(FD_BLOCKSEQUENCE); |
2650 | 97 | insert_fd_head(table, new_fh, pinfo, id+1, data); |
2651 | 97 | } |
2652 | 111 | tmp_offset = 0; |
2653 | 888 | for (prev_fd = fd; prev_fd; prev_fd = prev_fd->next) { |
2654 | 777 | prev_fd->offset -= (frag_number+1); |
2655 | 777 | tmp_offset = prev_fd->offset; |
2656 | 777 | if (new_fh->frame < fd->frame) { |
2657 | 97 | new_fh->frame = fd->frame; |
2658 | 97 | } |
2659 | 777 | } |
2660 | 111 | MERGE_FRAG(new_fh, fd); |
2661 | | /* If we previously found a different Last fragment, |
2662 | | * transfer that information to the new reassembly. */ |
2663 | 111 | if (fh->flags & FD_DATALEN_SET && |
2664 | 104 | fh->datalen > frag_number) { |
2665 | 104 | new_fh->flags |= FD_DATALEN_SET; |
2666 | 104 | new_fh->datalen = fh->datalen - (frag_number+1); |
2667 | 104 | fh->flags &= ~FD_DATALEN_SET; |
2668 | 104 | fh->datalen = 0; |
2669 | 104 | } else { |
2670 | | /* Look forward and take off the next (this is |
2671 | | * necessary in some edge cases where max_frags |
2672 | | * prevented some fragments from going on the |
2673 | | * previous First, but they can go on this one. */ |
2674 | 7 | fragment_add_seq_single_move(table, pinfo, id+1, |
2675 | 7 | data, tmp_offset); |
2676 | 7 | } |
2677 | 111 | } |
2678 | 2.26k | } else { |
2679 | 1.36k | fragment_add_seq_single_move(table, pinfo, id-frag_number, data, |
2680 | 1.36k | frag_number+1); |
2681 | 1.36k | } |
2682 | | /* Having cleaned up everything, finally ready to add our new |
2683 | | * fragment. Note that only this will ever complete a reassembly. */ |
2684 | 3.62k | fh = fragment_add_seq_common(table, tvb, offset, pinfo, |
2685 | 3.62k | id-frag_number, data, |
2686 | 3.62k | frag_number, frag_data_len, |
2687 | 3.62k | !last, 0, &orig_key); |
2688 | 3.62k | if (fh) { |
2689 | | /* |
2690 | | * Reassembly is complete. |
2691 | | * |
2692 | | * If this is in the table of in-progress reassemblies, |
2693 | | * remove it from that table. (It could be that this |
2694 | | * was the first and last fragment, so that no |
2695 | | * reassembly was done.) |
2696 | | */ |
2697 | 787 | if (orig_key != NULL) |
2698 | 787 | fragment_unhash(table, orig_key); |
2699 | | |
2700 | | /* |
2701 | | * Add this item to the table of reassembled packets. |
2702 | | */ |
2703 | 787 | fragment_reassembled_single(table, fh, pinfo, id-frag_number); |
2704 | 787 | return fh; |
2705 | 2.83k | } else { |
2706 | | /* |
2707 | | * Reassembly isn't complete. |
2708 | | */ |
2709 | 2.83k | return NULL; |
2710 | 2.83k | } |
2711 | 3.62k | } |
2712 | | |
2713 | | fragment_head * |
2714 | | fragment_add_seq_single(reassembly_table *table, tvbuff_t *tvb, |
2715 | | const int offset, const packet_info *pinfo, |
2716 | | const uint32_t id, const void* data, |
2717 | | const uint32_t frag_data_len, |
2718 | | const bool first, const bool last, |
2719 | | const uint32_t max_frags) |
2720 | 0 | { |
2721 | 0 | return fragment_add_seq_single_work(table, tvb, offset, pinfo, |
2722 | 0 | id, data, frag_data_len, |
2723 | 0 | first, last, max_frags, 0, 0); |
2724 | 0 | } |
2725 | | |
2726 | | fragment_head * |
2727 | | fragment_add_seq_single_aging(reassembly_table *table, tvbuff_t *tvb, |
2728 | | const int offset, const packet_info *pinfo, |
2729 | | const uint32_t id, const void* data, |
2730 | | const uint32_t frag_data_len, |
2731 | | const bool first, const bool last, |
2732 | | const uint32_t max_frags, const uint32_t max_age) |
2733 | 3.62k | { |
2734 | 3.62k | return fragment_add_seq_single_work(table, tvb, offset, pinfo, |
2735 | 3.62k | id, data, frag_data_len, |
2736 | 3.62k | first, last, max_frags, max_age, |
2737 | 3.62k | REASSEMBLE_FLAGS_AGING); |
2738 | 3.62k | } |
2739 | | |
2740 | | void |
2741 | | fragment_start_seq_check(reassembly_table *table, const packet_info *pinfo, |
2742 | | const uint32_t id, const void *data, |
2743 | | const uint32_t tot_len) |
2744 | 0 | { |
2745 | 0 | fragment_head *fd_head; |
2746 | | |
2747 | | /* Have we already seen this frame ?*/ |
2748 | 0 | if (pinfo->fd->visited) { |
2749 | 0 | return; |
2750 | 0 | } |
2751 | | |
2752 | | /* Check if fragment data exists */ |
2753 | 0 | fd_head = lookup_fd_head(table, pinfo, id, data, NULL); |
2754 | |
|
2755 | 0 | if (fd_head == NULL) { |
2756 | | /* Create list-head. */ |
2757 | 0 | fd_head = new_head(FD_BLOCKSEQUENCE|FD_DATALEN_SET); |
2758 | 0 | fd_head->datalen = tot_len; |
2759 | |
|
2760 | 0 | insert_fd_head(table, fd_head, pinfo, id, data); |
2761 | 0 | } |
2762 | 0 | } |
2763 | | |
2764 | | fragment_head * |
2765 | | fragment_end_seq_next(reassembly_table *table, const packet_info *pinfo, |
2766 | | const uint32_t id, const void *data) |
2767 | 1 | { |
2768 | 1 | reassembled_key reass_key; |
2769 | 1 | reassembled_key *new_key; |
2770 | 1 | fragment_head *fd_head; |
2771 | 1 | fragment_item *fd; |
2772 | 1 | void *orig_key; |
2773 | 1 | uint32_t max_offset = 0; |
2774 | | |
2775 | | /* |
2776 | | * Have we already seen this frame? |
2777 | | * If so, look for it in the table of reassembled packets. |
2778 | | */ |
2779 | 1 | if (pinfo->fd->visited) { |
2780 | 0 | reass_key.frame = pinfo->num; |
2781 | 0 | reass_key.id = id; |
2782 | 0 | return (fragment_head *)g_hash_table_lookup(table->reassembled_table, &reass_key); |
2783 | 0 | } |
2784 | | |
2785 | 1 | fd_head = lookup_fd_head(table, pinfo, id, data, &orig_key); |
2786 | | |
2787 | 1 | if (fd_head) { |
2788 | 2 | for (fd = fd_head->next; fd; fd = fd->next) { |
2789 | 1 | if (fd->offset > max_offset) { |
2790 | 0 | max_offset = fd->offset; |
2791 | 0 | } |
2792 | 1 | } |
2793 | 1 | fd_head->datalen = max_offset; |
2794 | 1 | fd_head->flags |= FD_DATALEN_SET; |
2795 | | |
2796 | 1 | fragment_defragment_and_free (fd_head, pinfo); |
2797 | | |
2798 | | /* |
2799 | | * Remove this from the table of in-progress reassemblies, |
2800 | | * and free up any memory used for it in that table. |
2801 | | */ |
2802 | 1 | fragment_unhash(table, orig_key); |
2803 | | |
2804 | | /* |
2805 | | * Add this item to the table of reassembled packets. |
2806 | | */ |
2807 | 1 | fragment_reassembled(table, fd_head, pinfo, id); |
2808 | 1 | if (fd_head->next != NULL) { |
2809 | 1 | new_key = g_slice_new(reassembled_key); |
2810 | 1 | new_key->frame = pinfo->num; |
2811 | 1 | new_key->id = id; |
2812 | 1 | reassembled_table_insert(table->reassembled_table, new_key, fd_head); |
2813 | 1 | } |
2814 | | |
2815 | 1 | return fd_head; |
2816 | 1 | } else { |
2817 | | /* |
2818 | | * Fragment data not found. |
2819 | | */ |
2820 | 0 | return NULL; |
2821 | 0 | } |
2822 | 1 | } |
2823 | | |
2824 | | /* |
2825 | | * Process reassembled data; if we're on the frame in which the data |
2826 | | * was reassembled, put the fragment information into the protocol |
2827 | | * tree, and construct a tvbuff with the reassembled data, otherwise |
2828 | | * just put a "reassembled in" item into the protocol tree. |
2829 | | * offset from start of tvb, result up to end of tvb |
2830 | | */ |
2831 | | tvbuff_t * |
2832 | | process_reassembled_data(tvbuff_t *tvb, const int offset, packet_info *pinfo, |
2833 | | const char *name, fragment_head *fd_head, const fragment_items *fit, |
2834 | | bool *update_col_infop, proto_tree *tree) |
2835 | 8.93k | { |
2836 | 8.93k | tvbuff_t *next_tvb; |
2837 | 8.93k | bool update_col_info; |
2838 | 8.93k | proto_item *frag_tree_item; |
2839 | | |
2840 | 8.93k | if (fd_head != NULL && pinfo->num == fd_head->reassembled_in && pinfo->curr_layer_num == fd_head->reas_in_layer_num) { |
2841 | | /* |
2842 | | * OK, we've reassembled this. |
2843 | | * Is this something that's been reassembled from more |
2844 | | * than one fragment? |
2845 | | */ |
2846 | 2.95k | if (fd_head->next != NULL) { |
2847 | | /* |
2848 | | * Yes. |
2849 | | * Allocate a new tvbuff, referring to the |
2850 | | * reassembled payload, and set |
2851 | | * the tvbuff to the list of tvbuffs to which |
2852 | | * the tvbuff we were handed refers, so it'll get |
2853 | | * cleaned up when that tvbuff is cleaned up. |
2854 | | */ |
2855 | 2.37k | next_tvb = tvb_new_chain(tvb, fd_head->tvb_data); |
2856 | | |
2857 | | /* Add the defragmented data to the data source list. */ |
2858 | 2.37k | add_new_data_source(pinfo, next_tvb, name); |
2859 | | |
2860 | | /* show all fragments */ |
2861 | 2.37k | if (fd_head->flags & FD_BLOCKSEQUENCE) { |
2862 | 1.72k | update_col_info = !show_fragment_seq_tree( |
2863 | 1.72k | fd_head, fit, tree, pinfo, next_tvb, &frag_tree_item); |
2864 | 1.72k | } else { |
2865 | 644 | update_col_info = !show_fragment_tree(fd_head, |
2866 | 644 | fit, tree, pinfo, next_tvb, &frag_tree_item); |
2867 | 644 | } |
2868 | 2.37k | } else { |
2869 | | /* |
2870 | | * No. |
2871 | | * Return a tvbuff with the payload, a subset of the |
2872 | | * tvbuff passed in. (The dissector SHOULD pass in |
2873 | | * the correct tvbuff and offset.) |
2874 | | */ |
2875 | 582 | int len; |
2876 | | /* For FD_BLOCKSEQUENCE, len is the length in bytes, |
2877 | | * datalen is the number of fragments. |
2878 | | */ |
2879 | 582 | if (fd_head->flags & FD_BLOCKSEQUENCE) { |
2880 | 582 | len = fd_head->len; |
2881 | 582 | } else { |
2882 | | // XXX Do the non-seq functions have this optimization? |
2883 | 0 | len = fd_head->datalen; |
2884 | 0 | } |
2885 | 582 | next_tvb = tvb_new_subset_length(tvb, offset, len); |
2886 | 582 | pinfo->fragmented = false; /* one-fragment packet */ |
2887 | 582 | update_col_info = true; |
2888 | 582 | } |
2889 | 2.95k | if (update_col_infop != NULL) |
2890 | 7 | *update_col_infop = update_col_info; |
2891 | 5.97k | } else { |
2892 | | /* |
2893 | | * We don't have the complete reassembled payload, or this |
2894 | | * isn't the final frame of that payload. |
2895 | | */ |
2896 | 5.97k | next_tvb = NULL; |
2897 | | |
2898 | | /* |
2899 | | * If we know what frame this was reassembled in, |
2900 | | * and if there's a field to use for the number of |
2901 | | * the frame in which the packet was reassembled, |
2902 | | * add it to the protocol tree. |
2903 | | */ |
2904 | 5.97k | if (fd_head != NULL && fit->hf_reassembled_in != NULL) { |
2905 | 9 | proto_item *fei = proto_tree_add_uint(tree, |
2906 | 9 | *(fit->hf_reassembled_in), tvb, |
2907 | 9 | 0, 0, fd_head->reassembled_in); |
2908 | 9 | proto_item_set_generated(fei); |
2909 | 9 | } |
2910 | 5.97k | } |
2911 | 8.93k | return next_tvb; |
2912 | 8.93k | } |
2913 | | |
2914 | | /* |
2915 | | * Show a single fragment in a fragment subtree, and put information about |
2916 | | * it in the top-level item for that subtree. |
2917 | | */ |
2918 | | static void |
2919 | | show_fragment(fragment_item *fd, const int offset, const fragment_items *fit, |
2920 | | proto_tree *ft, proto_item *fi, const bool first_frag, |
2921 | | const uint32_t count, tvbuff_t *tvb, packet_info *pinfo) |
2922 | 7.26k | { |
2923 | 7.26k | proto_item *fei=NULL; |
2924 | 7.26k | int hf; |
2925 | | |
2926 | 7.26k | if (first_frag) { |
2927 | 2.47k | char *name; |
2928 | 2.47k | if (count == 1) { |
2929 | 624 | name = g_strdup(proto_registrar_get_name(*(fit->hf_fragment))); |
2930 | 1.85k | } else { |
2931 | 1.85k | name = g_strdup(proto_registrar_get_name(*(fit->hf_fragments))); |
2932 | 1.85k | } |
2933 | 2.47k | proto_item_set_text(fi, "%u %s (%u byte%s): ", count, name, tvb_captured_length(tvb), |
2934 | 2.47k | plurality(tvb_captured_length(tvb), "", "s")); |
2935 | 2.47k | g_free(name); |
2936 | 4.79k | } else { |
2937 | 4.79k | proto_item_append_text(fi, ", "); |
2938 | 4.79k | } |
2939 | 7.26k | proto_item_append_text(fi, "#%u(%u)", fd->frame, fd->len); |
2940 | | |
2941 | 7.26k | if (fd->flags & (FD_OVERLAPCONFLICT |
2942 | 7.26k | |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) { |
2943 | 1.23k | hf = *(fit->hf_fragment_error); |
2944 | 6.03k | } else { |
2945 | 6.03k | hf = *(fit->hf_fragment); |
2946 | 6.03k | } |
2947 | 7.26k | if (fd->len == 0) { |
2948 | 1.21k | fei = proto_tree_add_uint_format(ft, hf, |
2949 | 1.21k | tvb, offset, fd->len, |
2950 | 1.21k | fd->frame, |
2951 | 1.21k | "Frame: %u (no data)", |
2952 | 1.21k | fd->frame); |
2953 | 6.05k | } else { |
2954 | 6.05k | fei = proto_tree_add_uint_format(ft, hf, |
2955 | 6.05k | tvb, offset, fd->len, |
2956 | 6.05k | fd->frame, |
2957 | 6.05k | "Frame: %u, payload: %u-%u (%u byte%s)", |
2958 | 6.05k | fd->frame, |
2959 | 6.05k | offset, |
2960 | 6.05k | offset+fd->len-1, |
2961 | 6.05k | fd->len, |
2962 | 6.05k | plurality(fd->len, "", "s")); |
2963 | 6.05k | } |
2964 | 7.26k | proto_item_set_generated(fei); |
2965 | 7.26k | mark_frame_as_depended_upon(pinfo->fd, fd->frame); |
2966 | 7.26k | if (fd->flags & (FD_OVERLAP|FD_OVERLAPCONFLICT |
2967 | 7.26k | |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) { |
2968 | | /* this fragment has some flags set, create a subtree |
2969 | | * for it and display the flags. |
2970 | | */ |
2971 | 1.51k | proto_tree *fet=NULL; |
2972 | | |
2973 | 1.51k | fet = proto_item_add_subtree(fei, *(fit->ett_fragment)); |
2974 | 1.51k | if (fd->flags&FD_OVERLAP) { |
2975 | 1.25k | fei=proto_tree_add_boolean(fet, |
2976 | 1.25k | *(fit->hf_fragment_overlap), |
2977 | 1.25k | tvb, 0, 0, |
2978 | 1.25k | true); |
2979 | 1.25k | proto_item_set_generated(fei); |
2980 | 1.25k | } |
2981 | 1.51k | if (fd->flags&FD_OVERLAPCONFLICT) { |
2982 | 968 | fei=proto_tree_add_boolean(fet, |
2983 | 968 | *(fit->hf_fragment_overlap_conflict), |
2984 | 968 | tvb, 0, 0, |
2985 | 968 | true); |
2986 | 968 | proto_item_set_generated(fei); |
2987 | 968 | } |
2988 | 1.51k | if (fd->flags&FD_MULTIPLETAILS) { |
2989 | 259 | fei=proto_tree_add_boolean(fet, |
2990 | 259 | *(fit->hf_fragment_multiple_tails), |
2991 | 259 | tvb, 0, 0, |
2992 | 259 | true); |
2993 | 259 | proto_item_set_generated(fei); |
2994 | 259 | } |
2995 | 1.51k | if (fd->flags&FD_TOOLONGFRAGMENT) { |
2996 | 123 | fei=proto_tree_add_boolean(fet, |
2997 | 123 | *(fit->hf_fragment_too_long_fragment), |
2998 | 123 | tvb, 0, 0, |
2999 | 123 | true); |
3000 | 123 | proto_item_set_generated(fei); |
3001 | 123 | } |
3002 | 1.51k | } |
3003 | 7.26k | } |
3004 | | |
3005 | | static bool |
3006 | | show_fragment_errs_in_col(fragment_head *fd_head, const fragment_items *fit, |
3007 | | packet_info *pinfo) |
3008 | 2.47k | { |
3009 | 2.47k | if (fd_head->flags & (FD_OVERLAPCONFLICT |
3010 | 2.47k | |FD_MULTIPLETAILS|FD_TOOLONGFRAGMENT) ) { |
3011 | 288 | col_add_fstr(pinfo->cinfo, COL_INFO, "[Illegal %s]", fit->tag); |
3012 | 288 | return true; |
3013 | 288 | } |
3014 | | |
3015 | 2.18k | return false; |
3016 | 2.47k | } |
3017 | | |
3018 | | /* This function will build the fragment subtree; it's for fragments |
3019 | | reassembled with "fragment_add()". |
3020 | | |
3021 | | It will return true if there were fragmentation errors |
3022 | | or false if fragmentation was ok. |
3023 | | */ |
3024 | | bool |
3025 | | show_fragment_tree(fragment_head *fd_head, const fragment_items *fit, |
3026 | | proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi) |
3027 | 644 | { |
3028 | 644 | fragment_item *fd; |
3029 | 644 | proto_tree *ft; |
3030 | 644 | bool first_frag; |
3031 | 644 | uint32_t count = 0; |
3032 | | /* It's not fragmented. */ |
3033 | 644 | pinfo->fragmented = false; |
3034 | | |
3035 | 644 | *fi = proto_tree_add_item(tree, *(fit->hf_fragments), tvb, 0, -1, ENC_NA); |
3036 | 644 | proto_item_set_generated(*fi); |
3037 | | |
3038 | 644 | ft = proto_item_add_subtree(*fi, *(fit->ett_fragments)); |
3039 | 644 | first_frag = true; |
3040 | 3.45k | for (fd = fd_head->next; fd != NULL; fd = fd->next) { |
3041 | 2.80k | count++; |
3042 | 2.80k | } |
3043 | 3.45k | for (fd = fd_head->next; fd != NULL; fd = fd->next) { |
3044 | 2.80k | show_fragment(fd, fd->offset, fit, ft, *fi, first_frag, count, tvb, pinfo); |
3045 | 2.80k | first_frag = false; |
3046 | 2.80k | } |
3047 | | |
3048 | 644 | if (fit->hf_fragment_count) { |
3049 | 644 | proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_fragment_count), |
3050 | 644 | tvb, 0, 0, count); |
3051 | 644 | proto_item_set_generated(fli); |
3052 | 644 | } |
3053 | | |
3054 | 644 | if (fit->hf_reassembled_length) { |
3055 | 644 | proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_reassembled_length), |
3056 | 644 | tvb, 0, 0, tvb_captured_length (tvb)); |
3057 | 644 | proto_item_set_generated(fli); |
3058 | 644 | } |
3059 | | |
3060 | 644 | if (fit->hf_reassembled_data) { |
3061 | 1 | proto_item *fli = proto_tree_add_item(ft, *(fit->hf_reassembled_data), |
3062 | 1 | tvb, 0, tvb_captured_length(tvb), ENC_NA); |
3063 | 1 | proto_item_set_generated(fli); |
3064 | 1 | } |
3065 | | |
3066 | 644 | return show_fragment_errs_in_col(fd_head, fit, pinfo); |
3067 | 644 | } |
3068 | | |
3069 | | /* This function will build the fragment subtree; it's for fragments |
3070 | | reassembled with "fragment_add_seq()" or "fragment_add_seq_check()". |
3071 | | |
3072 | | It will return true if there were fragmentation errors |
3073 | | or false if fragmentation was ok. |
3074 | | */ |
3075 | | bool |
3076 | | show_fragment_seq_tree(fragment_head *fd_head, const fragment_items *fit, |
3077 | | proto_tree *tree, packet_info *pinfo, tvbuff_t *tvb, proto_item **fi) |
3078 | 1.83k | { |
3079 | 1.83k | uint32_t offset, next_offset, count = 0; |
3080 | 1.83k | fragment_item *fd, *last_fd; |
3081 | 1.83k | proto_tree *ft; |
3082 | 1.83k | bool first_frag; |
3083 | | |
3084 | | /* It's not fragmented. */ |
3085 | 1.83k | pinfo->fragmented = false; |
3086 | | |
3087 | 1.83k | *fi = proto_tree_add_item(tree, *(fit->hf_fragments), tvb, 0, -1, ENC_NA); |
3088 | 1.83k | proto_item_set_generated(*fi); |
3089 | | |
3090 | 1.83k | ft = proto_item_add_subtree(*fi, *(fit->ett_fragments)); |
3091 | 1.83k | offset = 0; |
3092 | 1.83k | next_offset = 0; |
3093 | 1.83k | last_fd = NULL; |
3094 | 1.83k | first_frag = true; |
3095 | 6.29k | for (fd = fd_head->next; fd != NULL; fd = fd->next){ |
3096 | 4.46k | count++; |
3097 | 4.46k | } |
3098 | 6.29k | for (fd = fd_head->next; fd != NULL; fd = fd->next){ |
3099 | 4.46k | if (last_fd == NULL || last_fd->offset != fd->offset) { |
3100 | 3.72k | offset = next_offset; |
3101 | 3.72k | next_offset += fd->len; |
3102 | 3.72k | } |
3103 | 4.46k | last_fd = fd; |
3104 | 4.46k | show_fragment(fd, offset, fit, ft, *fi, first_frag, count, tvb, pinfo); |
3105 | 4.46k | first_frag = false; |
3106 | 4.46k | } |
3107 | | |
3108 | 1.83k | if (fit->hf_fragment_count) { |
3109 | 1.83k | proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_fragment_count), |
3110 | 1.83k | tvb, 0, 0, count); |
3111 | 1.83k | proto_item_set_generated(fli); |
3112 | 1.83k | } |
3113 | | |
3114 | 1.83k | if (fit->hf_reassembled_length) { |
3115 | 1.83k | proto_item *fli = proto_tree_add_uint(ft, *(fit->hf_reassembled_length), |
3116 | 1.83k | tvb, 0, 0, tvb_captured_length (tvb)); |
3117 | 1.83k | proto_item_set_generated(fli); |
3118 | 1.83k | } |
3119 | | |
3120 | 1.83k | if (fit->hf_reassembled_data) { |
3121 | 31 | proto_item *fli = proto_tree_add_item(ft, *(fit->hf_reassembled_data), |
3122 | 31 | tvb, 0, tvb_captured_length(tvb), ENC_NA); |
3123 | 31 | proto_item_set_generated(fli); |
3124 | 31 | } |
3125 | | |
3126 | 1.83k | return show_fragment_errs_in_col(fd_head, fit, pinfo); |
3127 | 1.83k | } |
3128 | | |
3129 | | static void |
3130 | | reassembly_table_init_reg_table(void *p, void *user_data _U_) |
3131 | 2.04k | { |
3132 | 2.04k | register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p; |
3133 | 2.04k | reassembly_table_init(reg_table->table, reg_table->funcs); |
3134 | 2.04k | } |
3135 | | |
3136 | | static void |
3137 | | reassembly_table_init_reg_tables(void) |
3138 | 14 | { |
3139 | 14 | g_list_foreach(reassembly_table_list, reassembly_table_init_reg_table, NULL); |
3140 | 14 | } |
3141 | | |
3142 | | static void |
3143 | | reassembly_table_cleanup_reg_table(void *p, void *user_data _U_) |
3144 | 0 | { |
3145 | 0 | register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p; |
3146 | 0 | reassembly_table_destroy(reg_table->table); |
3147 | 0 | } |
3148 | | |
3149 | | static void |
3150 | | reassembly_table_cleanup_reg_tables(void) |
3151 | 0 | { |
3152 | 0 | g_list_foreach(reassembly_table_list, reassembly_table_cleanup_reg_table, NULL); |
3153 | 0 | } |
3154 | | |
3155 | | void reassembly_tables_init(void) |
3156 | 14 | { |
3157 | 14 | register_init_routine(&reassembly_table_init_reg_tables); |
3158 | 14 | register_cleanup_routine(&reassembly_table_cleanup_reg_tables); |
3159 | 14 | } |
3160 | | |
3161 | | static void |
3162 | | reassembly_table_free(void *p, void *user_data _U_) |
3163 | 0 | { |
3164 | 0 | register_reassembly_table_t* reg_table = (register_reassembly_table_t*)p; |
3165 | 0 | reassembly_table_destroy(reg_table->table); |
3166 | 0 | g_free(reg_table); |
3167 | 0 | } |
3168 | | |
3169 | | void |
3170 | | reassembly_table_cleanup(void) |
3171 | 0 | { |
3172 | 0 | g_list_foreach(reassembly_table_list, reassembly_table_free, NULL); |
3173 | 0 | g_list_free(reassembly_table_list); |
3174 | 0 | } |
3175 | | |
3176 | | /* One instance of this structure is created for each pdu that spans across |
3177 | | * multiple segments. (MSP) */ |
3178 | | typedef struct _multisegment_pdu_t { |
3179 | | uint64_t first_frame; |
3180 | | uint64_t last_frame; |
3181 | | unsigned start_offset_at_first_frame; |
3182 | | unsigned end_offset_at_last_frame; |
3183 | | int length; /* length of this MSP */ |
3184 | | uint32_t streaming_reassembly_id; |
3185 | | /* pointer to previous multisegment_pdu */ |
3186 | | struct _multisegment_pdu_t* prev_msp; |
3187 | | } multisegment_pdu_t; |
3188 | | |
3189 | | /* struct for keeping the reassembly information of each stream */ |
3190 | | struct streaming_reassembly_info_t { |
3191 | | /* This map is keyed by frame num and keeps track of all MSPs for this |
3192 | | * stream. Different frames will point to the same MSP if they contain |
3193 | | * part data of this MSP. If a frame contains data that |
3194 | | * belongs to two MSPs, it will point to the second MSP. */ |
3195 | | wmem_map_t* multisegment_pdus; |
3196 | | /* This map is keyed by frame num and keeps track of the frag_offset |
3197 | | * of the first byte of frames for fragment_add() after first scan. */ |
3198 | | wmem_map_t* frame_num_frag_offset_map; |
3199 | | /* how many bytes the current uncompleted MSP still needs. (only valid for first scan) */ |
3200 | | int prev_deseg_len; |
3201 | | /* the current uncompleted MSP (only valid for first scan) */ |
3202 | | multisegment_pdu_t* last_msp; |
3203 | | }; |
3204 | | |
3205 | | static uint32_t |
3206 | | create_streaming_reassembly_id(void) |
3207 | 26 | { |
3208 | 26 | static uint32_t global_streaming_reassembly_id = 0; |
3209 | 26 | return ++global_streaming_reassembly_id; |
3210 | 26 | } |
3211 | | |
3212 | | streaming_reassembly_info_t* |
3213 | | streaming_reassembly_info_new(void) |
3214 | 19 | { |
3215 | 19 | return wmem_new0(wmem_file_scope(), streaming_reassembly_info_t); |
3216 | 19 | } |
3217 | | |
3218 | | /* Following is an example of ProtoA and ProtoB protocols from the declaration of this function in 'reassemble.h': |
3219 | | * |
3220 | | * +------------------ A Multisegment PDU of ProtoB ----------------------+ |
3221 | | * | | |
3222 | | * +--- ProtoA payload1 ---+ +- payload2 -+ +- Payload3 -+ +- Payload4 -+ +- ProtoA payload5 -+ |
3223 | | * | EoMSP | OmNFP | BoMSP | | MoMSP | | MoMSP | | MoMSP | | EoMSP | BoMSP | |
3224 | | * +-------+-------+-------+ +------------+ +------------+ +------------+ +---------+---------+ |
3225 | | * | | |
3226 | | * +----------------------------------------------------------------------+ |
3227 | | * |
3228 | | * For a ProtoA payload composed of EoMSP + OmNFP + BoMSP will call fragment_add() twice on EoMSP and BoMSP; and call |
3229 | | * process_reassembled_data() once for generating tvb of a MSP to which EoMSP belongs; and call subdissector twice on |
3230 | | * reassembled MSP of EoMSP and OmNFP + BoMSP. After that finds BoMSP is a beginning of a MSP at first scan. |
3231 | | * |
3232 | | * The rules are: |
3233 | | * |
3234 | | * - If a ProtoA payload contains EoMSP, we will need call fragment_add(), process_reassembled_data() and subdissector |
3235 | | * once on it to end a MSP. (May run twice or more times at first scan, because subdissector may only return the |
3236 | | * head length of message by pinfo->desegment_len. We need run second time for subdissector to determine the length |
3237 | | * of entire message). |
3238 | | * |
3239 | | * - If a ProtoA payload contains OmNFP, we will need only call subdissector once on it. The subdissector need dissect |
3240 | | * all non-fragment PDUs in it. (no desegment_len should output) |
3241 | | * |
3242 | | * - If a ProtoA payload contains BoMSP, we will need call subdissector once on BoMSP or OmNFP+BoMSP (because unknown |
3243 | | * during first scan). The subdissector will output desegment_len (!= 0). Then we will call fragment_add() |
3244 | | * with a new reassembly id on BoMSP for starting a new MSP. |
3245 | | * |
3246 | | * - If a ProtoA payload only contains MoMSP (entire payload is part of a MSP), we will only call fragment_add() once |
3247 | | * or twice (at first scan) on it. The subdissector will not be called. |
3248 | | * |
3249 | | * In this implementation, only multisegment PDUs are recorded in multisegment_pdus map keyed by the numbers (uint64_t) |
3250 | | * of frames belongs to MSPs. Each MSP in the map has a pointer referred to previous MSP, because we may need |
3251 | | * two MSPs to dissect a ProtoA payload that contains EoMSP + BoMSP at the same time. The multisegment_pdus map is built |
3252 | | * during first scan (pinfo->visited == false) with help of prev_deseg_len and last_msp fields of streaming_reassembly_info_t |
3253 | | * for each direction of a ProtoA STREAM. The prev_deseg_len record how many bytes of subsequent ProtoA payloads belong to |
3254 | | * previous PDU during first scan. The last_msp member of streaming_reassembly_info_t is always point to last MSP which |
3255 | | * is created during scan previous or early ProtoA payloads. Since subdissector might return only the head length of entire |
3256 | | * message (by pinfo->desegment_len) when there is not enough data to determine the message length, we need to reopen |
3257 | | * reassembly fragments for adding more bytes during scanning the next ProtoA payload. We have to use fragment_add() |
3258 | | * instead of fragment_add_check() or fragment_add_seq_next(). |
3259 | | * |
3260 | | * Read more: please refer to comments of the declaration of this function in 'reassemble.h'. |
3261 | | */ |
3262 | | int |
3263 | | reassemble_streaming_data_and_call_subdissector( |
3264 | | tvbuff_t* tvb, packet_info* pinfo, unsigned offset, int length, |
3265 | | proto_tree* segment_tree, proto_tree* reassembled_tree, reassembly_table streaming_reassembly_table, |
3266 | | streaming_reassembly_info_t* reassembly_info, uint64_t cur_frame_num, |
3267 | | dissector_handle_t subdissector_handle, proto_tree* subdissector_tree, void* subdissector_data, |
3268 | | const char* label, const fragment_items* frag_hf_items, int hf_segment_data |
3269 | | ) |
3270 | 80 | { |
3271 | 80 | int orig_length = length; |
3272 | 80 | int datalen = 0; |
3273 | 80 | int bytes_belong_to_prev_msp = 0; /* bytes belong to previous MSP */ |
3274 | 80 | uint32_t reassembly_id = 0, frag_offset = 0; |
3275 | 80 | fragment_head* head = NULL; |
3276 | 80 | bool need_more = false; |
3277 | 80 | bool found_BoMSP = false; |
3278 | 80 | multisegment_pdu_t* cur_msp = NULL, * prev_msp = NULL; |
3279 | 80 | uint16_t save_can_desegment; |
3280 | 80 | int save_desegment_offset; |
3281 | 80 | uint32_t save_desegment_len; |
3282 | 80 | uint64_t* frame_ptr; |
3283 | | |
3284 | 80 | save_can_desegment = pinfo->can_desegment; |
3285 | 80 | save_desegment_offset = pinfo->desegment_offset; |
3286 | 80 | save_desegment_len = pinfo->desegment_len; |
3287 | | |
3288 | | /* calculate how many bytes of this payload belongs to previous MSP (EoMSP) */ |
3289 | 80 | if (!PINFO_FD_VISITED(pinfo)) { |
3290 | | /* this is first scan */ |
3291 | 80 | if (reassembly_info->prev_deseg_len == DESEGMENT_ONE_MORE_SEGMENT) { |
3292 | | /* assuming the entire tvb belongs to the previous MSP */ |
3293 | 39 | bytes_belong_to_prev_msp = length; |
3294 | 39 | reassembly_info->prev_deseg_len = length; |
3295 | 41 | } else if (reassembly_info->prev_deseg_len > 0) { |
3296 | | /* part or all of current payload belong to previous MSP */ |
3297 | 8 | bytes_belong_to_prev_msp = MIN(reassembly_info->prev_deseg_len, length); |
3298 | 8 | reassembly_info->prev_deseg_len -= bytes_belong_to_prev_msp; |
3299 | 8 | need_more = (reassembly_info->prev_deseg_len > 0); |
3300 | 8 | } /* else { beginning of a new PDU (might be a NFP or MSP) } */ |
3301 | | |
3302 | 80 | if (bytes_belong_to_prev_msp > 0) { |
3303 | 47 | DISSECTOR_ASSERT(reassembly_info->last_msp != NULL); |
3304 | 47 | reassembly_id = reassembly_info->last_msp->streaming_reassembly_id; |
3305 | 47 | frag_offset = reassembly_info->last_msp->length; |
3306 | 47 | if (reassembly_info->frame_num_frag_offset_map == NULL) { |
3307 | 7 | reassembly_info->frame_num_frag_offset_map = wmem_map_new(wmem_file_scope(), g_int64_hash, g_int64_equal); |
3308 | 7 | } |
3309 | 47 | frame_ptr = (uint64_t*)wmem_memdup(wmem_file_scope(), &cur_frame_num, sizeof(uint64_t)); |
3310 | 47 | wmem_map_insert(reassembly_info->frame_num_frag_offset_map, frame_ptr, GUINT_TO_POINTER(frag_offset)); |
3311 | | /* This payload contains the data of previous msp, so we point to it. That may be overridden late. */ |
3312 | 47 | wmem_map_insert(reassembly_info->multisegment_pdus, frame_ptr, reassembly_info->last_msp); |
3313 | 47 | } |
3314 | 80 | } else { |
3315 | | /* not first scan, use information of multisegment_pdus built during first scan */ |
3316 | 0 | if (reassembly_info->multisegment_pdus) { |
3317 | 0 | cur_msp = (multisegment_pdu_t*)wmem_map_lookup(reassembly_info->multisegment_pdus, &cur_frame_num); |
3318 | 0 | } |
3319 | 0 | if (cur_msp) { |
3320 | 0 | if (cur_msp->first_frame == cur_frame_num) { |
3321 | | /* Current payload contains a beginning of a MSP. (BoMSP) |
3322 | | * The cur_msp contains information about the beginning MSP. |
3323 | | * If prev_msp is not null, that means this payload also contains |
3324 | | * the last part of previous MSP. (EoMSP) */ |
3325 | 0 | prev_msp = cur_msp->prev_msp; |
3326 | 0 | } else { |
3327 | | /* Current payload is not a first frame of a MSP (not include BoMSP). */ |
3328 | 0 | prev_msp = cur_msp; |
3329 | 0 | cur_msp = NULL; |
3330 | 0 | } |
3331 | 0 | } |
3332 | |
|
3333 | 0 | if (prev_msp && prev_msp->last_frame >= cur_frame_num) { |
3334 | 0 | if (prev_msp->last_frame == cur_frame_num) { |
3335 | | /* this payload contains part of previous MSP (contains EoMSP) */ |
3336 | 0 | bytes_belong_to_prev_msp = prev_msp->end_offset_at_last_frame - offset; |
3337 | 0 | } else { /* if (prev_msp->last_frame > cur_frame_num) */ |
3338 | | /* this payload all belongs to previous MSP */ |
3339 | 0 | bytes_belong_to_prev_msp = length; |
3340 | 0 | need_more = true; |
3341 | 0 | } |
3342 | 0 | reassembly_id = prev_msp->streaming_reassembly_id; |
3343 | 0 | } |
3344 | 0 | if (reassembly_info->frame_num_frag_offset_map) { |
3345 | 0 | frag_offset = GPOINTER_TO_UINT(wmem_map_lookup(reassembly_info->frame_num_frag_offset_map, &cur_frame_num)); |
3346 | 0 | } |
3347 | 0 | } |
3348 | | |
3349 | | /* handling EoMSP or MoMSP (entire payload being middle part of a MSP) */ |
3350 | 149 | while (bytes_belong_to_prev_msp > 0) { |
3351 | 69 | tvbuff_t* reassembled_tvb = NULL; |
3352 | 69 | DISSECTOR_ASSERT(reassembly_id > 0); |
3353 | 69 | pinfo->can_desegment = 2; /* this will be decreased one while passing to subdissector */ |
3354 | 69 | pinfo->desegment_offset = 0; |
3355 | 69 | pinfo->desegment_len = 0; |
3356 | | |
3357 | 69 | head = fragment_add(&streaming_reassembly_table, tvb, offset, pinfo, reassembly_id, NULL, |
3358 | 69 | frag_offset, bytes_belong_to_prev_msp, need_more); |
3359 | | |
3360 | 69 | if (head) { |
3361 | 31 | if (frag_hf_items->hf_reassembled_in) { |
3362 | 31 | proto_item_set_generated( |
3363 | 31 | proto_tree_add_uint(segment_tree, *(frag_hf_items->hf_reassembled_in), tvb, offset, |
3364 | 31 | bytes_belong_to_prev_msp, head->reassembled_in) |
3365 | 31 | ); |
3366 | 31 | } |
3367 | | |
3368 | 31 | if (!need_more) { |
3369 | 31 | reassembled_tvb = process_reassembled_data(tvb, offset, pinfo, |
3370 | 31 | wmem_strdup_printf(pinfo->pool, "Reassembled %s", label), |
3371 | 31 | head, frag_hf_items, NULL, reassembled_tree); |
3372 | 31 | } |
3373 | 31 | } |
3374 | | |
3375 | 69 | proto_tree_add_bytes_format(segment_tree, hf_segment_data, tvb, offset, |
3376 | 69 | bytes_belong_to_prev_msp, NULL, "%s Segment data (%u byte%s)", label, |
3377 | 69 | bytes_belong_to_prev_msp, plurality(bytes_belong_to_prev_msp, "", "s")); |
3378 | | |
3379 | 69 | if (reassembled_tvb) { |
3380 | | /* normally, this stage will dissect one or more completed pdus */ |
3381 | | /* Note, don't call_dissector_with_data because sometime the pinfo->curr_layer_num will changed |
3382 | | * after calling that will make reassembly failed! */ |
3383 | 31 | call_dissector_only(subdissector_handle, reassembled_tvb, pinfo, subdissector_tree, subdissector_data); |
3384 | 31 | } |
3385 | | |
3386 | 69 | if (pinfo->desegment_len) { |
3387 | | /* that must only happen during first scan the reassembly_info->prev_deseg_len might be only the |
3388 | | * head length of entire message. */ |
3389 | 22 | DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo)); |
3390 | 22 | DISSECTOR_ASSERT_HINT(pinfo->desegment_len != DESEGMENT_UNTIL_FIN, "Subdissector MUST NOT " |
3391 | 22 | "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to " |
3392 | 22 | " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined."); |
3393 | | |
3394 | 22 | if (pinfo->desegment_offset > 0) { |
3395 | 0 | DISSECTOR_ASSERT_HINT(pinfo->desegment_offset > reassembly_info->last_msp->length |
3396 | 0 | && pinfo->desegment_offset < reassembly_info->last_msp->length + bytes_belong_to_prev_msp, |
3397 | 0 | wmem_strdup_printf(pinfo->pool, |
3398 | 0 | "Subdissector MUST NOT set pinfo->desegment_offset(%d) in previous or next part of MSP, must between (%d, %d).", |
3399 | 0 | pinfo->desegment_offset, reassembly_info->last_msp->length, reassembly_info->last_msp->length + bytes_belong_to_prev_msp)); |
3400 | | |
3401 | | /* shorten the bytes_belong_to_prev_msp and just truncate the reassembled tvb */ |
3402 | 0 | bytes_belong_to_prev_msp = pinfo->desegment_offset - reassembly_info->last_msp->length; |
3403 | 0 | fragment_truncate(&streaming_reassembly_table, pinfo, reassembly_id, NULL, pinfo->desegment_offset); |
3404 | 0 | found_BoMSP = true; |
3405 | 22 | } else { |
3406 | 22 | if (pinfo->desegment_len == DESEGMENT_ONE_MORE_SEGMENT) { |
3407 | | /* just need more bytes, all remaining bytes belongs to previous MSP (to run fragment_add again) */ |
3408 | 21 | bytes_belong_to_prev_msp = length; |
3409 | 21 | } |
3410 | | |
3411 | | /* Remove the data added by previous fragment_add(), and reopen fragments for adding more bytes. */ |
3412 | 22 | fragment_truncate(&streaming_reassembly_table, pinfo, reassembly_id, NULL, reassembly_info->last_msp->length); |
3413 | 22 | fragment_set_partial_reassembly(&streaming_reassembly_table, pinfo, reassembly_id, NULL); |
3414 | | |
3415 | 22 | reassembly_info->prev_deseg_len = bytes_belong_to_prev_msp + pinfo->desegment_len; |
3416 | 22 | bytes_belong_to_prev_msp = MIN(reassembly_info->prev_deseg_len, length); |
3417 | 22 | reassembly_info->prev_deseg_len -= bytes_belong_to_prev_msp; |
3418 | 22 | need_more = (reassembly_info->prev_deseg_len > 0); |
3419 | 22 | continue; |
3420 | 22 | } |
3421 | 22 | } |
3422 | | |
3423 | 47 | if (pinfo->desegment_len == 0 || found_BoMSP) { |
3424 | | /* We will arrive here, only when the MSP is defragmented and dissected or this |
3425 | | * payload all belongs to previous MSP (only fragment_add() with need_more=true called) |
3426 | | * or BoMSP is parsed while pinfo->desegment_offset > 0 and pinfo->desegment_len != 0 |
3427 | | */ |
3428 | 47 | offset += bytes_belong_to_prev_msp; |
3429 | 47 | length -= bytes_belong_to_prev_msp; |
3430 | 47 | DISSECTOR_ASSERT(length >= 0); |
3431 | 47 | if (!PINFO_FD_VISITED(pinfo)) { |
3432 | 47 | reassembly_info->last_msp->length += bytes_belong_to_prev_msp; |
3433 | 47 | } |
3434 | | |
3435 | 47 | if (!PINFO_FD_VISITED(pinfo) && reassembled_tvb) { |
3436 | | /* completed current msp */ |
3437 | 9 | reassembly_info->last_msp->last_frame = cur_frame_num; |
3438 | 9 | reassembly_info->last_msp->end_offset_at_last_frame = offset; |
3439 | 9 | reassembly_info->prev_deseg_len = pinfo->desegment_len; |
3440 | 9 | } |
3441 | 47 | bytes_belong_to_prev_msp = 0; /* break */ |
3442 | 47 | } |
3443 | 47 | } |
3444 | | |
3445 | | /* to find and handle OmNFP, and find BoMSP at first scan. */ |
3446 | 80 | if (length > 0 && !found_BoMSP) { |
3447 | 33 | if (!PINFO_FD_VISITED(pinfo)) { |
3448 | | /* It is first scan, to dissect remaining bytes to find whether it is OmNFP only, or BoMSP only or OmNFP + BoMSP. */ |
3449 | 33 | datalen = length; |
3450 | 33 | DISSECTOR_ASSERT(cur_msp == NULL); |
3451 | 33 | } else { |
3452 | | /* Not first scan */ |
3453 | 0 | if (cur_msp) { |
3454 | | /* There's a BoMSP. Let's calculate the length of OmNFP between EoMSP and BoMSP */ |
3455 | 0 | datalen = cur_msp->start_offset_at_first_frame - offset; /* if result is zero that means no OmNFP */ |
3456 | 0 | } else { |
3457 | | /* This payload is not a beginning of MSP. The remaining bytes all belong to OmNFP without BoMSP */ |
3458 | 0 | datalen = length; |
3459 | 0 | } |
3460 | 0 | } |
3461 | 33 | DISSECTOR_ASSERT(datalen >= 0); |
3462 | | |
3463 | | /* Dissect the remaining of this payload. If (datalen == 0) means remaining only have one BoMSP without OmNFP. */ |
3464 | 33 | if (datalen > 0) { |
3465 | | /* we dissect if it is not dissected before or it is a non-fragment pdu (between two multisegment pdus) */ |
3466 | 33 | pinfo->can_desegment = 2; |
3467 | 33 | pinfo->desegment_offset = 0; |
3468 | 33 | pinfo->desegment_len = 0; |
3469 | | |
3470 | 33 | call_dissector_only(subdissector_handle, tvb_new_subset_length(tvb, offset, datalen), |
3471 | 33 | pinfo, subdissector_tree, subdissector_data); |
3472 | | |
3473 | 33 | if (pinfo->desegment_len) { |
3474 | 26 | DISSECTOR_ASSERT_HINT(pinfo->desegment_len != DESEGMENT_UNTIL_FIN, "Subdissector MUST NOT " |
3475 | 26 | "set pinfo->desegment_len to DESEGMENT_UNTIL_FIN. Instead, it can set pinfo->desegment_len to " |
3476 | 26 | " DESEGMENT_ONE_MORE_SEGMENT or the length of head if the length of entire message is not able to be determined."); |
3477 | | /* only happen during first scan */ |
3478 | 26 | DISSECTOR_ASSERT(!PINFO_FD_VISITED(pinfo) && datalen == length); |
3479 | 26 | offset += pinfo->desegment_offset; |
3480 | 26 | length -= pinfo->desegment_offset; |
3481 | 26 | } else { |
3482 | | /* all remaining bytes are consumed by subdissector */ |
3483 | 7 | offset += datalen; |
3484 | 7 | length -= datalen; |
3485 | 7 | } |
3486 | 33 | if (!PINFO_FD_VISITED(pinfo)) { |
3487 | 33 | reassembly_info->prev_deseg_len = pinfo->desegment_len; |
3488 | 33 | } |
3489 | 33 | } /* else all remaining bytes (BoMSP) belong to a new MSP */ |
3490 | 33 | DISSECTOR_ASSERT(length >= 0); |
3491 | 33 | } |
3492 | | |
3493 | | /* handling BoMSP */ |
3494 | 80 | if (length > 0) { |
3495 | 26 | col_append_sep_fstr(pinfo->cinfo, COL_INFO, " ", "[%s segment of a reassembled PDU] ", label); |
3496 | 26 | if (!PINFO_FD_VISITED(pinfo)) { |
3497 | | /* create a msp for current frame during first scan */ |
3498 | 26 | cur_msp = wmem_new0(wmem_file_scope(), multisegment_pdu_t); |
3499 | 26 | cur_msp->first_frame = cur_frame_num; |
3500 | 26 | cur_msp->last_frame = UINT64_MAX; |
3501 | 26 | cur_msp->start_offset_at_first_frame = offset; |
3502 | 26 | cur_msp->length = length; |
3503 | 26 | cur_msp->streaming_reassembly_id = reassembly_id = create_streaming_reassembly_id(); |
3504 | 26 | cur_msp->prev_msp = reassembly_info->last_msp; |
3505 | 26 | reassembly_info->last_msp = cur_msp; |
3506 | 26 | if (reassembly_info->multisegment_pdus == NULL) { |
3507 | 13 | reassembly_info->multisegment_pdus = wmem_map_new(wmem_file_scope(), g_int64_hash, g_int64_equal); |
3508 | 13 | } |
3509 | 26 | frame_ptr = (uint64_t*)wmem_memdup(wmem_file_scope(), &cur_frame_num, sizeof(uint64_t)); |
3510 | 26 | wmem_map_insert(reassembly_info->multisegment_pdus, frame_ptr, cur_msp); |
3511 | 26 | } else { |
3512 | 0 | DISSECTOR_ASSERT(cur_msp && cur_msp->start_offset_at_first_frame == offset); |
3513 | 0 | reassembly_id = cur_msp->streaming_reassembly_id; |
3514 | 0 | } |
3515 | | /* add first fragment of the new MSP to reassembly table */ |
3516 | 26 | head = fragment_add(&streaming_reassembly_table, tvb, offset, pinfo, reassembly_id, |
3517 | 26 | NULL, 0, length, true); |
3518 | | |
3519 | 26 | if (head && frag_hf_items->hf_reassembled_in) { |
3520 | 0 | proto_item_set_generated( |
3521 | 0 | proto_tree_add_uint(segment_tree, *(frag_hf_items->hf_reassembled_in), |
3522 | 0 | tvb, offset, length, head->reassembled_in) |
3523 | 0 | ); |
3524 | 0 | } |
3525 | 26 | proto_tree_add_bytes_format(segment_tree, hf_segment_data, tvb, offset, length, |
3526 | 26 | NULL, "%s Segment data (%u byte%s)", label, length, plurality(length, "", "s")); |
3527 | 26 | } |
3528 | | |
3529 | 80 | pinfo->can_desegment = save_can_desegment; |
3530 | 80 | pinfo->desegment_offset = save_desegment_offset; |
3531 | 80 | pinfo->desegment_len = save_desegment_len; |
3532 | | |
3533 | 80 | return orig_length; |
3534 | 80 | } |
3535 | | |
3536 | | int |
3537 | | additional_bytes_expected_to_complete_reassembly(streaming_reassembly_info_t* reassembly_info) |
3538 | 0 | { |
3539 | 0 | return reassembly_info->prev_deseg_len; |
3540 | 0 | } |
3541 | | |
3542 | | /* |
3543 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
3544 | | * |
3545 | | * Local variables: |
3546 | | * c-basic-offset: 8 |
3547 | | * tab-width: 8 |
3548 | | * indent-tabs-mode: t |
3549 | | * End: |
3550 | | * |
3551 | | * vi: set shiftwidth=8 tabstop=8 noexpandtab: |
3552 | | * :indentSize=8:tabSize=8:noTabs=false: |
3553 | | */ |