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