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
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
|
/* pcapng.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
*
* File format support for pcapng file format
* Copyright (c) 2007 by Ulf Lamping <ulf.lamping@web.de>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/* File format specification:
* https://github.com/pcapng/pcapng
* Related Wiki page:
* https://gitlab.com/wireshark/wireshark/-/wikis/Development/PcapNg
*/
#include "config.h"
#define WS_LOG_DOMAIN LOG_DOMAIN_WIRETAP
#include "wtap_opttypes.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <wsutil/wslog.h>
#include <wsutil/strtoi.h>
#include <wsutil/glib-compat.h>
#include <wsutil/ws_assert.h>
#include <wsutil/ws_roundup.h>
#include <wsutil/unicode-utils.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "required_file_handlers.h"
#include "pcap-common.h"
#include "pcap-encap.h"
#include "pcapng.h"
#include "pcapng_module.h"
#include "secrets-types.h"
#define ROUND_TO_4BYTE(len) WS_ROUNDUP_4(len)
static gboolean
pcapng_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err,
gchar **err_info, gint64 *data_offset);
static gboolean
pcapng_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static void
pcapng_close(wtap *wth);
static gboolean
pcapng_encap_is_ft_specific(int encap);
static gboolean
pcapng_write_if_descr_block(wtap_dumper *wdh, wtap_block_t int_data, int *err);
/*
* Minimum block size = size of block header + size of block trailer.
*/
#define MIN_BLOCK_SIZE ((guint32)(sizeof(pcapng_block_header_t) + sizeof(guint32)))
/*
* Minimum SHB size = minimum block size + size of fixed length portion of SHB.
*/
#define MIN_SHB_SIZE ((guint32)(MIN_BLOCK_SIZE + sizeof(pcapng_section_header_block_t)))
/* pcapng: packet block file encoding (obsolete) */
typedef struct pcapng_packet_block_s {
guint16 interface_id;
guint16 drops_count;
guint32 timestamp_high;
guint32 timestamp_low;
guint32 captured_len;
guint32 packet_len;
/* ... Packet Data ... */
/* ... Padding ... */
/* ... Options ... */
} pcapng_packet_block_t;
/*
* Minimum PB size = minimum block size + size of fixed length portion of PB.
*/
#define MIN_PB_SIZE ((guint32)(MIN_BLOCK_SIZE + sizeof(pcapng_packet_block_t)))
/* pcapng: enhanced packet block file encoding */
typedef struct pcapng_enhanced_packet_block_s {
guint32 interface_id;
guint32 timestamp_high;
guint32 timestamp_low;
guint32 captured_len;
guint32 packet_len;
/* ... Packet Data ... */
/* ... Padding ... */
/* ... Options ... */
} pcapng_enhanced_packet_block_t;
/*
* Minimum EPB size = minimum block size + size of fixed length portion of EPB.
*/
#define MIN_EPB_SIZE ((guint32)(MIN_BLOCK_SIZE + sizeof(pcapng_enhanced_packet_block_t)))
/* pcapng: simple packet block file encoding */
typedef struct pcapng_simple_packet_block_s {
guint32 packet_len;
/* ... Packet Data ... */
/* ... Padding ... */
} pcapng_simple_packet_block_t;
/*
* Minimum SPB size = minimum block size + size of fixed length portion of SPB.
*/
#define MIN_SPB_SIZE ((guint32)(MIN_BLOCK_SIZE + sizeof(pcapng_simple_packet_block_t)))
/* pcapng: name resolution block file encoding */
typedef struct pcapng_name_resolution_block_s {
guint16 record_type;
guint16 record_len;
/* ... Record ... */
} pcapng_name_resolution_block_t;
/*
* Minimum NRB size = minimum block size + size of smallest NRB record
* (there must at least be an "end of records" record).
*/
#define MIN_NRB_SIZE ((guint32)(MIN_BLOCK_SIZE + sizeof(pcapng_name_resolution_block_t)))
/* pcapng: custom block file encoding */
typedef struct pcapng_custom_block_s {
guint32 pen;
/* Custom data and options */
} pcapng_custom_block_t;
/*
* Minimum CB size = minimum block size + size of fixed length portion of CB.
*/
#define MIN_CB_SIZE ((guint32)(MIN_BLOCK_SIZE + sizeof(pcapng_custom_block_t)))
/*
* Minimum ISB size = minimum block size + size of fixed length portion of ISB.
*/
#define MIN_ISB_SIZE ((guint32)(MIN_BLOCK_SIZE + sizeof(pcapng_interface_statistics_block_t)))
/*
* Minimum Sysdig size = minimum block size + packed size of sysdig_event_phdr.
* Minimum Sysdig event v2 header size = minimum block size + packed size of sysdig_event_v2_phdr (which, in addition
* to sysdig_event_phdr, includes the nparams 32bit value).
*/
#define SYSDIG_EVENT_HEADER_SIZE ((16 + 64 + 64 + 32 + 16)/8) /* CPU ID + TS + TID + Event len + Event type */
#define MIN_SYSDIG_EVENT_SIZE ((guint32)(MIN_BLOCK_SIZE + SYSDIG_EVENT_HEADER_SIZE))
#define SYSDIG_EVENT_V2_HEADER_SIZE ((16 + 64 + 64 + 32 + 16 + 32)/8) /* CPU ID + TS + TID + Event len + Event type + nparams */
#define MIN_SYSDIG_EVENT_V2_SIZE ((guint32)(MIN_BLOCK_SIZE + SYSDIG_EVENT_V2_HEADER_SIZE))
/*
* We require __REALTIME_TIMESTAMP in the Journal Export Format reader in
* order to set each packet timestamp. Require it here as well, although
* it's not strictly necessary.
*/
#define SDJ__REALTIME_TIMESTAMP "__REALTIME_TIMESTAMP="
#define MIN_SYSTEMD_JOURNAL_EXPORT_ENTRY_SIZE 23 // "__REALTIME_TIMESTAMP=0\n"
#define MIN_SYSTEMD_JOURNAL_EXPORT_BLOCK_SIZE ((guint32)(MIN_SYSTEMD_JOURNAL_EXPORT_ENTRY_SIZE + MIN_BLOCK_SIZE))
/* pcapng: common option header file encoding for every option type */
typedef struct pcapng_option_header_s {
guint16 option_code;
guint16 option_length;
/* ... x bytes Option Body ... */
/* ... Padding ... */
} pcapng_option_header_t;
struct pcapng_option {
guint16 type;
guint16 value_length;
};
/* Option codes: 16-bit field */
#define OPT_EPB_FLAGS 0x0002
#define OPT_EPB_HASH 0x0003
#define OPT_EPB_DROPCOUNT 0x0004
#define OPT_EPB_PACKETID 0x0005
#define OPT_EPB_QUEUE 0x0006
#define OPT_EPB_VERDICT 0x0007
#define OPT_NRB_DNSNAME 0x0002
#define OPT_NRB_DNSV4ADDR 0x0003
#define OPT_NRB_DNSV6ADDR 0x0004
/* MSBit of option code means "local type" */
#define OPT_LOCAL_FLAG 0x8000
/* OPT_EPB_VERDICT sub-types */
#define OPT_VERDICT_TYPE_HW 0
#define OPT_VERDICT_TYPE_TC 1
#define OPT_VERDICT_TYPE_XDP 2
/* OPT_EPB_HASH sub-types */
#define OPT_HASH_2COMP 0
#define OPT_HASH_XOR 1
#define OPT_HASH_CRC32 2
#define OPT_HASH_MD5 3
#define OPT_HASH_SHA1 4
#define OPT_HASH_TOEPLITZ 5
/*
* In order to keep from trying to allocate large chunks of memory,
* which could either fail or, even if it succeeds, chew up so much
* address space or memory+backing store as not to leave room for
* anything else, we impose upper limits on the size of blocks we're
* willing to handle.
*
* We pick a limit of an EPB with a maximum-sized D-Bus packet and 128 KiB
* worth of options; we use the maximum D-Bus packet size as that's larger
* than the maximum packet size for other link-layer types, and the maximum
* packet size for other link-layer types is currently small enough that
* the resulting block size would be less than the previous 16 MiB limit.
*/
#define MAX_BLOCK_SIZE (MIN_EPB_SIZE + WTAP_MAX_PACKET_SIZE_DBUS + 131072)
/* Note: many of the defined structures for block data are defined in wtap.h */
/* Packet data - used for both Enhanced Packet Block and the obsolete Packet Block data */
typedef struct wtapng_packet_s {
/* mandatory */
guint32 ts_high; /* seconds since 1.1.1970 */
guint32 ts_low; /* fraction of seconds, depends on if_tsresol */
guint32 cap_len; /* data length in the file */
guint32 packet_len; /* data length on the wire */
guint32 interface_id; /* identifier of the interface. */
guint16 drops_count; /* drops count, only valid for packet block */
/* 0xffff if information no available */
/* pack_hash */
/* XXX - put the packet data / pseudo_header here as well? */
} wtapng_packet_t;
/* Simple Packet data */
typedef struct wtapng_simple_packet_s {
/* mandatory */
guint32 cap_len; /* data length in the file */
guint32 packet_len; /* data length on the wire */
/* XXX - put the packet data / pseudo_header here as well? */
} wtapng_simple_packet_t;
/* Interface data in private struct */
typedef struct interface_info_s {
int wtap_encap;
guint32 snap_len;
guint64 time_units_per_second;
int tsprecision;
gint64 tsoffset;
int fcslen;
} interface_info_t;
typedef struct {
guint current_section_number; /**< Section number of the current section being read sequentially */
GArray *sections; /**< Sections found in the capture file. */
} pcapng_t;
/*
* Table for plugins to handle particular block types.
*
* A handler has a "read" routine and a "write" routine.
*
* A "read" routine returns a block as a libwiretap record, filling
* in the wtap_rec structure with the appropriate record type and
* other information, and filling in the supplied Buffer with
* data for which there's no place in the wtap_rec structure.
*
* A "write" routine takes a libwiretap record and Buffer and writes
* out a block.
*/
typedef struct {
block_reader reader;
block_writer writer;
} block_handler;
static GHashTable *block_handlers;
void
register_pcapng_block_type_handler(guint block_type, block_reader reader,
block_writer writer)
{
block_handler *handler;
/*
* Is this a known block type?
*/
switch (block_type) {
case BLOCK_TYPE_SHB:
case BLOCK_TYPE_IDB:
case BLOCK_TYPE_PB:
case BLOCK_TYPE_SPB:
case BLOCK_TYPE_NRB:
case BLOCK_TYPE_ISB:
case BLOCK_TYPE_EPB:
case BLOCK_TYPE_DSB:
case BLOCK_TYPE_CB_COPY:
case BLOCK_TYPE_CB_NO_COPY:
case BLOCK_TYPE_SYSDIG_MI:
case BLOCK_TYPE_SYSDIG_PL_V1:
case BLOCK_TYPE_SYSDIG_FDL_V1:
case BLOCK_TYPE_SYSDIG_EVENT:
case BLOCK_TYPE_SYSDIG_IL_V1:
case BLOCK_TYPE_SYSDIG_UL_V1:
case BLOCK_TYPE_SYSDIG_PL_V2:
case BLOCK_TYPE_SYSDIG_EVF:
case BLOCK_TYPE_SYSDIG_PL_V3:
case BLOCK_TYPE_SYSDIG_PL_V4:
case BLOCK_TYPE_SYSDIG_PL_V5:
case BLOCK_TYPE_SYSDIG_PL_V6:
case BLOCK_TYPE_SYSDIG_PL_V7:
case BLOCK_TYPE_SYSDIG_PL_V8:
case BLOCK_TYPE_SYSDIG_PL_V9:
case BLOCK_TYPE_SYSDIG_EVENT_V2:
case BLOCK_TYPE_SYSDIG_EVF_V2:
case BLOCK_TYPE_SYSDIG_FDL_V2:
case BLOCK_TYPE_SYSDIG_IL_V2:
case BLOCK_TYPE_SYSDIG_UL_V2:
case BLOCK_TYPE_SYSTEMD_JOURNAL_EXPORT:
/*
* Yes; we already handle it, and don't allow a replacement to
* be registered (if there's a bug in our code, or there's
* something we don't handle in that block, submit a change
* to the main Wireshark source).
*/
ws_warning("Attempt to register plugin for block type 0x%08x not allowed",
block_type);
return;
case BLOCK_TYPE_IRIG_TS:
case BLOCK_TYPE_ARINC_429:
/*
* Yes, and we don't already handle it. Allow a plugin to
* handle it.
*
* (But why not submit the plugin source to Wireshark?)
*/
break;
default:
/*
* No; is it a local block type?
*/
if (!(block_type & 0x80000000)) {
/*
* No; don't allow a plugin to be registered for it, as
* the block type needs to be registered before it's used.
*/
ws_warning("Attempt to register plugin for reserved block type 0x%08x not allowed",
block_type);
return;
}
/*
* Yes; allow the registration.
*/
break;
}
if (block_handlers == NULL) {
/*
* Create the table of block handlers.
*
* XXX - there's no "g_uint_hash()" or "g_uint_equal()",
* so we use "g_direct_hash()" and "g_direct_equal()".
*/
block_handlers = g_hash_table_new_full(g_direct_hash,
g_direct_equal,
NULL, g_free);
}
handler = g_new(block_handler, 1);
handler->reader = reader;
handler->writer = writer;
g_hash_table_insert(block_handlers, GUINT_TO_POINTER(block_type),
handler);
}
/*
* Tables for plugins to handle particular options for particular block
* types.
*
* An option has three handler routines:
*
* An option parser, used when reading an option from a file:
*
* The option parser is passed an indication of whether this section
* of the file is byte-swapped, the length of the option, the data of
* the option, a pointer to an error code, and a pointer to a pointer
* variable for an error string.
*
* It checks whether the length and option are valid, and, if they
* aren't, returns FALSE, setting the error code to the appropriate
* error (normally WTAP_ERR_BAD_FILE) and the error string to an
* appropriate string indicating the problem.
*
* Otherwise, if this section of the file is byte-swapped, it byte-swaps
* multi-byte numerical values, so that it's in the host byte order.
*
* An option sizer, used when writing an option to a file:
*
* The option sizer is passed the option identifier for the option
* and a wtap_optval_t * that points to the data for the option.
*
* It calculates how many bytes the option's data requires, not
* including any padding bytes, and returns that value.
*
* An option writer, used when writing an option to a file:
*
* The option writer is passed a wtap_dumper * to which the
* option data should be written, the option identifier for
* the option, a wtap_optval_t * that points to the data for
* the option, and an int * into which an error code should
* be stored if an error occurs when writing the option.
*
* It returns a gboolean value of TRUE if the attempt to
* write the option succeeds and FALSE if the attempt to
* write the option gets an error.
*/
/*
* Block types indices in the table of tables of option handlers.
*
* Block types are not guaranteed to be sequential, so we map the
* block types we support to a sequential set. Furthermore, all
* packet block types have the same set of options.
*/
#define BT_INDEX_SHB 0
#define BT_INDEX_IDB 1
#define BT_INDEX_PBS 2 /* all packet blocks */
#define BT_INDEX_NRB 3
#define BT_INDEX_ISB 4
#define BT_INDEX_EVT 5
#define BT_INDEX_DSB 6
#define NUM_BT_INDICES 7
typedef struct {
option_parser parser;
option_sizer sizer;
option_writer writer;
} option_handler;
static GHashTable *option_handlers[NUM_BT_INDICES];
/* Return whether this block type is handled interally, or
* if it is returned to the caller in pcapng_read().
* This is used by pcapng_open() to decide if it can process
* the block.
* Note that for block types that are registered from plugins,
* we don't know the true answer without actually reading the block,
* or even if there is a fixed answer for all blocks of that type,
* so we err on the side of not processing.
*/
static bool
get_block_type_internal(unsigned block_type)
{
switch (block_type) {
case BLOCK_TYPE_SHB:
case BLOCK_TYPE_IDB:
case BLOCK_TYPE_NRB:
case BLOCK_TYPE_DSB:
case BLOCK_TYPE_ISB: /* XXX: ISBs should probably not be internal. */
case BLOCK_TYPE_SYSDIG_MI:
case BLOCK_TYPE_SYSDIG_PL_V1:
case BLOCK_TYPE_SYSDIG_FDL_V1:
case BLOCK_TYPE_SYSDIG_IL_V1:
case BLOCK_TYPE_SYSDIG_UL_V1:
case BLOCK_TYPE_SYSDIG_PL_V2:
case BLOCK_TYPE_SYSDIG_PL_V3:
case BLOCK_TYPE_SYSDIG_PL_V4:
case BLOCK_TYPE_SYSDIG_PL_V5:
case BLOCK_TYPE_SYSDIG_PL_V6:
case BLOCK_TYPE_SYSDIG_PL_V7:
case BLOCK_TYPE_SYSDIG_PL_V8:
case BLOCK_TYPE_SYSDIG_PL_V9:
case BLOCK_TYPE_SYSDIG_FDL_V2:
case BLOCK_TYPE_SYSDIG_IL_V2:
case BLOCK_TYPE_SYSDIG_UL_V2:
return true;
case BLOCK_TYPE_PB:
case BLOCK_TYPE_EPB:
case BLOCK_TYPE_SPB:
return false;
case BLOCK_TYPE_CB_COPY:
case BLOCK_TYPE_CB_NO_COPY:
case BLOCK_TYPE_SYSDIG_EVENT:
case BLOCK_TYPE_SYSDIG_EVENT_V2:
case BLOCK_TYPE_SYSDIG_EVENT_V2_LARGE:
case BLOCK_TYPE_SYSTEMD_JOURNAL_EXPORT:
return false;
default:
#ifdef HAVE_PLUGINS
/*
* Do we have a handler for this block type?
*/
if (block_handlers != NULL &&
(g_hash_table_lookup(block_handlers, GUINT_TO_POINTER(block_type))) != NULL) {
/* Yes. We don't know if the handler sets this block internal
* or needs to return it to the pcap_read() caller without
* reading it. Since this is called by pcap_open(), play it
* safe and tell pcap_open() to stop processing blocks.
* (XXX: Maybe the block type handler registration interface
* should include some way of indicating whether blocks are
* handled internally, which should hopefully be the same
* for all blocks of a type.)
*/
return FALSE;
}
#endif
return TRUE;
}
return FALSE;
}
static gboolean
get_block_type_index(guint block_type, guint *bt_index)
{
ws_assert(bt_index);
switch (block_type) {
case BLOCK_TYPE_SHB:
*bt_index = BT_INDEX_SHB;
break;
case BLOCK_TYPE_IDB:
*bt_index = BT_INDEX_IDB;
break;
case BLOCK_TYPE_PB:
case BLOCK_TYPE_EPB:
case BLOCK_TYPE_SPB:
*bt_index = BT_INDEX_PBS;
break;
case BLOCK_TYPE_NRB:
*bt_index = BT_INDEX_NRB;
break;
case BLOCK_TYPE_ISB:
*bt_index = BT_INDEX_ISB;
break;
case BLOCK_TYPE_SYSDIG_EVENT:
case BLOCK_TYPE_SYSDIG_EVENT_V2:
case BLOCK_TYPE_SYSDIG_EVENT_V2_LARGE:
case BLOCK_TYPE_SYSDIG_MI:
case BLOCK_TYPE_SYSDIG_PL_V1:
case BLOCK_TYPE_SYSDIG_FDL_V1:
case BLOCK_TYPE_SYSDIG_IL_V1:
case BLOCK_TYPE_SYSDIG_UL_V1:
case BLOCK_TYPE_SYSDIG_PL_V2:
case BLOCK_TYPE_SYSDIG_PL_V3:
case BLOCK_TYPE_SYSDIG_PL_V4:
case BLOCK_TYPE_SYSDIG_PL_V5:
case BLOCK_TYPE_SYSDIG_PL_V6:
case BLOCK_TYPE_SYSDIG_PL_V7:
case BLOCK_TYPE_SYSDIG_PL_V8:
case BLOCK_TYPE_SYSDIG_PL_V9:
case BLOCK_TYPE_SYSDIG_FDL_V2:
case BLOCK_TYPE_SYSDIG_IL_V2:
case BLOCK_TYPE_SYSDIG_UL_V2:
*bt_index = BT_INDEX_EVT;
break;
case BLOCK_TYPE_DSB:
*bt_index = BT_INDEX_DSB;
break;
default:
/*
* This is a block type we don't process; either we ignore it,
* in which case the options don't get processed, or there's
* a plugin routine to handle it, in which case that routine
* will do the option processing itself.
*
* XXX - report an error?
*/
return FALSE;
}
return TRUE;
}
void
register_pcapng_option_handler(guint block_type, guint option_code,
option_parser parser,
option_sizer sizer,
option_writer writer)
{
guint bt_index;
option_handler *handler;
if (!get_block_type_index(block_type, &bt_index))
return;
if (option_handlers[bt_index] == NULL) {
/*
* Create the table of option handlers for this block type.
*
* XXX - there's no "g_uint_hash()" or "g_uint_equal()",
* so we use "g_direct_hash()" and "g_direct_equal()".
*/
option_handlers[bt_index] = g_hash_table_new_full(g_direct_hash,
g_direct_equal,
NULL, g_free);
}
handler = g_new(option_handler, 1);
handler->parser = parser;
handler->sizer = sizer;
handler->writer = writer;
g_hash_table_insert(option_handlers[bt_index],
GUINT_TO_POINTER(option_code), handler);
}
void
pcapng_process_uint8_option(wtapng_block_t *wblock,
guint16 option_code, guint16 option_length,
const guint8 *option_content)
{
if (option_length == 1) {
/*
* If this option can appear only once in a block, this call
* will fail on the second and later occurrences of the option;
* we silently ignore the failure.
*/
wtap_block_add_uint8_option(wblock->block, option_code, option_content[0]);
}
}
void
pcapng_process_uint32_option(wtapng_block_t *wblock,
const section_info_t *section_info,
pcapng_opt_byte_order_e byte_order,
guint16 option_code, guint16 option_length,
const guint8 *option_content)
{
guint32 uint32;
if (option_length == 4) {
/* Don't cast a guint8 * into a guint32 *--the
* guint8 * may not point to something that's
* aligned correctly.
*
* XXX - options are aligned on 32-bit boundaries, so, while
* it may be true that 64-bit options aren't guaranteed to be
* aligned on 64-bit bounaries, it shouldn't be true that 32-bit
* options aren't guaranteed to be aligned on 32-bit boundaries.
*/
memcpy(&uint32, option_content, sizeof(guint32));
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
if (section_info->byte_swapped) {
uint32 = GUINT32_SWAP_LE_BE(uint32);
}
break;
case OPT_BIG_ENDIAN:
uint32 = GUINT32_FROM_BE(uint32);
break;
case OPT_LITTLE_ENDIAN:
uint32 = GUINT32_FROM_LE(uint32);
break;
default:
/*
* This should not happen - this is called by pcapng_process_options(),
* which returns an error for an invalid byte_order argument, and
* otherwise passes the known-to-be-valid byte_order argument to
* us.
*
* Just ignore the option.
*/
return;
}
/*
* If this option can appear only once in a block, this call
* will fail on the second and later occurrences of the option;
* we silently ignore the failure.
*/
wtap_block_add_uint32_option(wblock->block, option_code, uint32);
}
}
void
pcapng_process_timestamp_option(wtapng_block_t *wblock,
const section_info_t *section_info,
pcapng_opt_byte_order_e byte_order,
guint16 option_code, guint16 option_length,
const guint8 *option_content)
{
if (option_length == 8) {
guint32 high, low;
guint64 timestamp;
/* Don't cast a guint8 * into a guint32 *--the
* guint8 * may not point to something that's
* aligned correctly.
*/
memcpy(&high, option_content, sizeof(guint32));
memcpy(&low, option_content + sizeof(guint32), sizeof(guint32));
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
if (section_info->byte_swapped) {
high = GUINT32_SWAP_LE_BE(high);
low = GUINT32_SWAP_LE_BE(low);
}
break;
case OPT_BIG_ENDIAN:
high = GUINT32_FROM_BE(high);
low = GUINT32_FROM_BE(low);
break;
case OPT_LITTLE_ENDIAN:
high = GUINT32_FROM_LE(high);
low = GUINT32_FROM_LE(low);
break;
default:
/*
* This should not happen - this is called by pcapng_process_options(),
* which returns an error for an invalid byte_order argument, and
* otherwise passes the known-to-be-valid byte_order argument to
* us.
*
* Just ignore the option.
*/
return;
}
timestamp = (guint64)high;
timestamp <<= 32;
timestamp += (guint64)low;
/*
* If this option can appear only once in a block, this call
* will fail on the second and later occurrences of the option;
* we silently ignore the failure.
*/
wtap_block_add_uint64_option(wblock->block, option_code, timestamp);
}
}
void
pcapng_process_uint64_option(wtapng_block_t *wblock,
const section_info_t *section_info,
pcapng_opt_byte_order_e byte_order,
guint16 option_code, guint16 option_length,
const guint8 *option_content)
{
guint64 uint64;
if (option_length == 8) {
/* Don't cast a guint8 * into a guint64 *--the
* guint8 * may not point to something that's
* aligned correctly.
*/
memcpy(&uint64, option_content, sizeof(guint64));
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
if (section_info->byte_swapped) {
uint64 = GUINT64_SWAP_LE_BE(uint64);
}
break;
case OPT_BIG_ENDIAN:
uint64 = GUINT64_FROM_BE(uint64);
break;
case OPT_LITTLE_ENDIAN:
uint64 = GUINT64_FROM_LE(uint64);
break;
default:
/*
* This should not happen - this is called by pcapng_process_options(),
* which returns an error for an invalid byte_order argument, and
* otherwise passes the known-to-be-valid byte_order argument to
* us.
*
* Just ignore the option.
*/
return;
}
/*
* If this option can appear only once in a block, this call
* will fail on the second and later occurrences of the option;
* we silently ignore the failure.
*/
wtap_block_add_uint64_option(wblock->block, option_code, uint64);
}
}
void
pcapng_process_int64_option(wtapng_block_t *wblock,
const section_info_t *section_info,
pcapng_opt_byte_order_e byte_order,
guint16 option_code, guint16 option_length,
const guint8 *option_content)
{
gint64 int64;
if (option_length == 8) {
/* Don't cast a gint8 * into a gint64 *--the
* guint8 * may not point to something that's
* aligned correctly.
*/
memcpy(&int64, option_content, sizeof(gint64));
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
if (section_info->byte_swapped) {
int64 = GUINT64_SWAP_LE_BE(int64);
}
break;
case OPT_BIG_ENDIAN:
int64 = GUINT64_FROM_BE(int64);
break;
case OPT_LITTLE_ENDIAN:
int64 = GUINT64_FROM_LE(int64);
break;
default:
/*
* This should not happen - this is called by pcapng_process_options(),
* which returns an error for an invalid byte_order argument, and
* otherwise passes the known-to-be-valid byte_order argument to
* us.
*
* Just ignore the option.
*/
return;
}
/*
* If this option can appear only once in a block, this call
* will fail on the second and later occurrences of the option;
* we silently ignore the failure.
*/
wtap_block_add_int64_option(wblock->block, option_code, int64);
}
}
void
pcapng_process_string_option(wtapng_block_t *wblock, guint16 option_code,
guint16 option_length, const guint8 *option_content)
{
const char *opt = (const char *)option_content;
size_t optlen = option_length;
char *str;
/* Validate UTF-8 encoding. */
str = ws_utf8_make_valid(NULL, opt, optlen);
wtap_block_add_string_option_owned(wblock->block, option_code, str);
}
void
pcapng_process_bytes_option(wtapng_block_t *wblock, guint16 option_code,
guint16 option_length, const guint8 *option_content)
{
wtap_block_add_bytes_option(wblock->block, option_code, (const char *)option_content, option_length);
}
static gboolean
pcapng_process_nflx_custom_option(wtapng_block_t *wblock,
section_info_t *section_info,
const guint8 *value, guint16 length)
{
struct nflx_dumpinfo dumpinfo;
guint32 type, version;
gint64 dumptime, temp;
if (length < 4) {
ws_debug("Length = %u too small", length);
return FALSE;
}
memcpy(&type, value, sizeof(guint32));
type = GUINT32_FROM_LE(type);
value += 4;
length -= 4;
ws_debug("Handling type = %u, payload of length = %u", type, length);
switch (type) {
case NFLX_OPT_TYPE_VERSION:
if (length == sizeof(guint32)) {
memcpy(&version, value, sizeof(guint32));
version = GUINT32_FROM_LE(version);
ws_debug("BBLog version: %u", version);
section_info->bblog_version = version;
} else {
ws_debug("BBLog version parameter has strange length: %u", length);
}
break;
case NFLX_OPT_TYPE_TCPINFO:
ws_debug("BBLog tcpinfo of length: %u", length);
if (wblock->type == BLOCK_TYPE_CB_COPY) {
ws_buffer_assure_space(wblock->frame_buffer, length);
wblock->rec->rec_header.custom_block_header.length = length + 4;
memcpy(ws_buffer_start_ptr(wblock->frame_buffer), value, length);
memcpy(&temp, value, sizeof(guint64));
temp = GUINT64_FROM_LE(temp);
wblock->rec->ts.secs = section_info->bblog_offset_tv_sec + temp;
memcpy(&temp, value + sizeof(guint64), sizeof(guint64));
temp = GUINT64_FROM_LE(temp);
wblock->rec->ts.nsecs = (guint32)(section_info->bblog_offset_tv_usec + temp) * 1000;
if (wblock->rec->ts.nsecs >= 1000000000) {
wblock->rec->ts.secs += 1;
wblock->rec->ts.nsecs -= 1000000000;
}
wblock->rec->presence_flags = WTAP_HAS_TS;
wblock->internal = FALSE;
}
break;
case NFLX_OPT_TYPE_DUMPINFO:
if (length == sizeof(struct nflx_dumpinfo)) {
memcpy(&dumpinfo, value, sizeof(struct nflx_dumpinfo));
section_info->bblog_offset_tv_sec = GUINT64_FROM_LE(dumpinfo.tlh_offset_tv_sec);
section_info->bblog_offset_tv_usec = GUINT64_FROM_LE(dumpinfo.tlh_offset_tv_usec);
ws_debug("BBLog dumpinfo time offset: %" PRIu64, section_info->bblog_offset_tv_sec);
} else {
ws_debug("BBLog dumpinfo parameter has strange length: %u", length);
}
break;
case NFLX_OPT_TYPE_DUMPTIME:
if (length == sizeof(gint64)) {
memcpy(&dumptime, value, sizeof(gint64));
dumptime = GINT64_FROM_LE(dumptime);
ws_debug("BBLog dumpinfo time offset: %" PRIu64, dumptime);
} else {
ws_debug("BBLog dumptime parameter has strange length: %u", length);
}
break;
case NFLX_OPT_TYPE_STACKNAME:
if (length >= 2) {
ws_debug("BBLog stack name: %.*s(%u)", length - 1, value + 1, *(guint8 *)value);
} else {
ws_debug("BBLog stack name has strange length: %u)", length);
}
break;
default:
ws_debug("Unknown type: %u, length: %u", type, length);
break;
}
return wtap_block_add_nflx_custom_option(wblock->block, type, value, length) == WTAP_OPTTYPE_SUCCESS;
}
static gboolean
pcapng_process_custom_option(wtapng_block_t *wblock,
section_info_t *section_info,
guint16 option_code, guint16 option_length,
const guint8 *option_content,
pcapng_opt_byte_order_e byte_order,
int *err, gchar **err_info)
{
guint32 pen;
gboolean ret;
if (option_length < 4) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: option length (%d) too small for custom option",
option_length);
return FALSE;
}
memcpy(&pen, option_content, sizeof(guint32));
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
if (section_info->byte_swapped) {
pen = GUINT32_SWAP_LE_BE(pen);
}
break;
case OPT_BIG_ENDIAN:
pen = GUINT32_FROM_BE(pen);
break;
case OPT_LITTLE_ENDIAN:
pen = GUINT32_FROM_LE(pen);
break;
default:
/*
* This should not happen - this is called by pcapng_process_options(),
* which returns an error for an invalid byte_order argument, and
* otherwise passes the known-to-be-valid byte_order argument to
* us.
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("pcapng: invalid byte order %d passed to pcapng_process_custom_option()",
byte_order);
return FALSE;
}
switch (pen) {
case PEN_NFLX:
ret = pcapng_process_nflx_custom_option(wblock, section_info, option_content + 4, option_length - 4);
break;
default:
ret = wtap_block_add_custom_option(wblock->block, option_code, pen, option_content + 4, option_length - 4) == WTAP_OPTTYPE_SUCCESS;
ws_debug("Custom option type %u (0x%04x) with unknown pen %u with custom data of length %u", option_code, option_code, pen, option_length - 4);
break;
}
ws_debug("returning %d", ret);
return ret;
}
#ifdef HAVE_PLUGINS
static gboolean
pcapng_process_unhandled_option(wtapng_block_t *wblock,
guint bt_index,
const section_info_t *section_info,
guint16 option_code, guint16 option_length,
const guint8 *option_content,
int *err, gchar **err_info)
{
option_handler *handler;
/*
* Do we have a handler for this packet block option code?
*/
if (option_handlers[bt_index] != NULL &&
(handler = (option_handler *)g_hash_table_lookup(option_handlers[bt_index],
GUINT_TO_POINTER((guint)option_code))) != NULL) {
/* Yes - call the handler. */
if (!handler->parser(wblock->block, section_info->byte_swapped,
option_length, option_content, err, err_info))
/* XXX - free anything? */
return FALSE;
}
return TRUE;
}
#else
static gboolean
pcapng_process_unhandled_option(wtapng_block_t *wblock _U_,
guint bt_index _U_,
const section_info_t *section_info _U_,
guint16 option_code _U_, guint16 option_length _U_,
const guint8 *option_content _U_,
int *err _U_, gchar **err_info _U_)
{
return TRUE;
}
#endif
gboolean
pcapng_process_options(FILE_T fh, wtapng_block_t *wblock,
section_info_t *section_info,
guint opt_cont_buf_len,
gboolean (*process_option)(wtapng_block_t *,
const section_info_t *,
guint16, guint16,
const guint8 *,
int *, gchar **),
pcapng_opt_byte_order_e byte_order,
int *err, gchar **err_info)
{
guint8 *option_content; /* Allocate as large as the options block */
guint opt_bytes_remaining;
const guint8 *option_ptr;
const pcapng_option_header_t *oh;
guint16 option_code, option_length;
guint rounded_option_length;
ws_debug("Options %u bytes", opt_cont_buf_len);
if (opt_cont_buf_len == 0) {
/* No options, so nothing to do */
return TRUE;
}
/* Allocate enough memory to hold all options */
option_content = (guint8 *)g_try_malloc(opt_cont_buf_len);
if (option_content == NULL) {
*err = ENOMEM; /* we assume we're out of memory */
return FALSE;
}
/* Read all the options into the buffer */
if (!wtap_read_bytes(fh, option_content, opt_cont_buf_len, err, err_info)) {
ws_debug("failed to read options");
g_free(option_content);
return FALSE;
}
/*
* Now process them.
* option_ptr starts out aligned on at least a 4-byte boundary, as
* that's what g_try_malloc() gives us, and each option is padded
* to a length that's a multiple of 4 bytes, so it remains aligned.
*/
option_ptr = &option_content[0];
opt_bytes_remaining = opt_cont_buf_len;
while (opt_bytes_remaining != 0) {
/* Get option header. */
oh = (const pcapng_option_header_t *)(const void *)option_ptr;
/* Sanity check: don't run past the end of the options. */
if (sizeof (*oh) > opt_bytes_remaining) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: Not enough data for option header");
g_free(option_content);
return FALSE;
}
option_code = oh->option_code;
option_length = oh->option_length;
switch (byte_order) {
case OPT_SECTION_BYTE_ORDER:
if (section_info->byte_swapped) {
option_code = GUINT16_SWAP_LE_BE(option_code);
option_length = GUINT16_SWAP_LE_BE(option_length);
}
break;
case OPT_BIG_ENDIAN:
option_code = GUINT16_FROM_BE(option_code);
option_length = GUINT16_FROM_BE(option_length);
break;
case OPT_LITTLE_ENDIAN:
option_code = GUINT16_FROM_LE(option_code);
option_length = GUINT16_FROM_LE(option_length);
break;
default:
/* Don't do that. */
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("pcapng: invalid byte order %d passed to pcapng_process_options()",
byte_order);
return FALSE;
}
option_ptr += sizeof (*oh); /* 4 bytes, so it remains aligned */
opt_bytes_remaining -= sizeof (*oh);
/* Round up option length to a multiple of 4. */
rounded_option_length = ROUND_TO_4BYTE(option_length);
/* Sanity check: don't run past the end of the options. */
if (rounded_option_length > opt_bytes_remaining) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: Not enough data to handle option of length %u",
option_length);
g_free(option_content);
return FALSE;
}
switch (option_code) {
case(OPT_EOFOPT): /* opt_endofopt */
if (opt_bytes_remaining != 0) {
ws_debug("%u bytes after opt_endofopt", opt_bytes_remaining);
}
/* padding should be ok here, just get out of this */
opt_bytes_remaining = rounded_option_length;
break;
case(OPT_COMMENT):
pcapng_process_string_option(wblock, option_code, option_length,
option_ptr);
break;
case(OPT_CUSTOM_STR_COPY):
case(OPT_CUSTOM_BIN_COPY):
case(OPT_CUSTOM_STR_NO_COPY):
case(OPT_CUSTOM_BIN_NO_COPY):
if (!pcapng_process_custom_option(wblock, section_info,
option_code, option_length,
option_ptr,
byte_order,
err, err_info)) {
g_free(option_content);
return FALSE;
}
break;
default:
if (process_option == NULL ||
!(*process_option)(wblock, (const section_info_t *)section_info, option_code,
option_length, option_ptr,
err, err_info)) {
g_free(option_content);
return FALSE;
}
}
option_ptr += rounded_option_length; /* multiple of 4 bytes, so it remains aligned */
opt_bytes_remaining -= rounded_option_length;
}
g_free(option_content);
return TRUE;
}
typedef enum {
PCAPNG_BLOCK_OK,
PCAPNG_BLOCK_NOT_SHB,
PCAPNG_BLOCK_ERROR
} block_return_val;
static gboolean
pcapng_process_section_header_block_option(wtapng_block_t *wblock,
const section_info_t *section_info,
guint16 option_code,
guint16 option_length,
const guint8 *option_content,
int *err, gchar **err_info)
{
/*
* Handle option content.
*
* ***DO NOT*** add any items to this table that are not
* standardized option codes in either section 3.5 "Options"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-options
*
* or in the list of options in section 4.1 "Section Header Block"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-section-header-block
*
* All option codes in this switch statement here must be listed
* in one of those places as standardized option types.
*/
switch (option_code) {
case(OPT_SHB_HARDWARE):
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
case(OPT_SHB_OS):
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
case(OPT_SHB_USERAPPL):
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
default:
if (!pcapng_process_unhandled_option(wblock, BT_INDEX_SHB,
section_info, option_code,
option_length, option_content,
err, err_info))
return FALSE;
break;
}
return TRUE;
}
static block_return_val
pcapng_read_section_header_block(FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
gboolean byte_swapped;
guint16 version_major;
guint16 version_minor;
guint opt_cont_buf_len;
pcapng_section_header_block_t shb;
wtapng_section_mandatory_t* section_data;
/* read fixed-length part of the block */
if (!wtap_read_bytes(fh, &shb, sizeof shb, err, err_info)) {
/*
* Even if this is just a short read, report it as an error.
* It *is* a read error except when we're doing an open, in
* which case it's a "this isn't a pcapng file" indication.
* The open code will call us directly, and treat a short
* read error as such an indication.
*/
return PCAPNG_BLOCK_ERROR;
}
/* is the magic number one we expect? */
switch (shb.magic) {
case(0x1A2B3C4D):
/* this seems pcapng with correct byte order */
byte_swapped = FALSE;
version_major = shb.version_major;
version_minor = shb.version_minor;
ws_debug("SHB (our byte order) V%u.%u, len %u",
version_major, version_minor, bh->block_total_length);
break;
case(0x4D3C2B1A):
/* this seems pcapng with swapped byte order */
byte_swapped = TRUE;
version_major = GUINT16_SWAP_LE_BE(shb.version_major);
version_minor = GUINT16_SWAP_LE_BE(shb.version_minor);
/* tweak the block length to meet current swapping that we know now */
bh->block_total_length = GUINT32_SWAP_LE_BE(bh->block_total_length);
ws_debug("SHB (byte-swapped) V%u.%u, len %u",
version_major, version_minor, bh->block_total_length);
break;
default:
/* Not a "pcapng" magic number we know about. */
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: unknown byte-order magic number 0x%08x", shb.magic);
/*
* See above comment about PCAPNG_BLOCK_NOT_SHB.
*/
return PCAPNG_BLOCK_NOT_SHB;
}
/*
* Add padding bytes to the block total length.
*
* See the comment in pcapng_read_block() for a long discussion
* of this.
*/
bh->block_total_length = ROUND_TO_4BYTE(bh->block_total_length);
/*
* Is this block long enough to be an SHB?
*/
if (bh->block_total_length < MIN_SHB_SIZE) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an SHB is less than the minimum SHB size %u",
bh->block_total_length, MIN_SHB_SIZE);
return PCAPNG_BLOCK_ERROR;
}
/* OK, at this point we assume it's a pcapng file.
Don't try to allocate memory for a huge number of options, as
that might fail and, even if it succeeds, it might not leave
any address space or memory+backing store for anything else.
We do that by imposing a maximum block size of MAX_BLOCK_SIZE.
We check for this *after* checking the SHB for its byte
order magic number, so that non-pcapng files are less
likely to be treated as bad pcapng files. */
if (bh->block_total_length > MAX_BLOCK_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u is too large (> %u)",
bh->block_total_length, MAX_BLOCK_SIZE);
return PCAPNG_BLOCK_ERROR;
}
/* Currently only SHB versions 1.0 and 1.2 are supported;
version 1.2 is treated as being the same as version 1.0.
See the current version of the pcapng specification.
Version 1.2 is written by some programs that write additional
block types (which can be read by any code that handles them,
regarless of whether the minor version if 0 or 2, so that's
not a reason to change the minor version number).
XXX - the pcapng specification says that readers should
just ignore sections with an unsupported version number;
presumably they can also report an error if they skip
all the way to the end of the file without finding
any versions that they support. */
if (!(version_major == 1 &&
(version_minor == 0 || version_minor == 2))) {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("pcapng: unknown SHB version %u.%u",
version_major, version_minor);
return PCAPNG_BLOCK_ERROR;
}
memset(section_info, 0, sizeof(section_info_t));
section_info->byte_swapped = byte_swapped;
section_info->version_major = version_major;
section_info->version_minor = version_minor;
/*
* Set wblock->block to a newly-allocated section header block.
*/
wblock->block = wtap_block_create(WTAP_BLOCK_SECTION);
/*
* Set the mandatory values for the block.
*/
section_data = (wtapng_section_mandatory_t*)wtap_block_get_mandatory_data(wblock->block);
/* 64bit section_length (currently unused) */
if (section_info->byte_swapped) {
section_data->section_length = GUINT64_SWAP_LE_BE(shb.section_length);
} else {
section_data->section_length = shb.section_length;
}
/* Options */
opt_cont_buf_len = bh->block_total_length - MIN_SHB_SIZE;
if (!pcapng_process_options(fh, wblock, section_info, opt_cont_buf_len,
pcapng_process_section_header_block_option,
OPT_SECTION_BYTE_ORDER, err, err_info))
return PCAPNG_BLOCK_ERROR;
/*
* We don't return these to the caller in pcapng_read().
*/
wblock->internal = TRUE;
return PCAPNG_BLOCK_OK;
}
static gboolean
pcapng_process_if_descr_block_option(wtapng_block_t *wblock,
const section_info_t *section_info,
guint16 option_code,
guint16 option_length,
const guint8 *option_content,
int *err, gchar **err_info)
{
if_filter_opt_t if_filter;
/*
* Handle option content.
*
* ***DO NOT*** add any items to this table that are not
* standardized option codes in either section 3.5 "Options"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-options
*
* or in the list of options in section 4.1 "Section Header Block"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-section-header-block
*
* All option codes in this switch statement here must be listed
* in one of those places as standardized option types.
*/
switch (option_code) {
case(OPT_IDB_NAME): /* if_name */
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
case(OPT_IDB_DESCRIPTION): /* if_description */
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
case(OPT_IDB_SPEED): /* if_speed */
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_IDB_TSRESOL): /* if_tsresol */
pcapng_process_uint8_option(wblock, option_code, option_length,
option_content);
break;
/*
* if_tzone 10 Time zone for GMT support (TODO: specify better). TODO: give a good example
*/
case(OPT_IDB_FILTER): /* if_filter */
if (option_length < 1) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block verdict option length %u is < 1",
option_length);
/* XXX - free anything? */
return FALSE;
}
/* The first byte of the Option Data keeps a code of the filter used (e.g. if this is a libpcap string,
* or BPF bytecode.
*/
if (option_content[0] == 0) {
if_filter.type = if_filter_pcap;
if_filter.data.filter_str = g_strndup((char *)option_content+1, option_length-1);
ws_debug("filter_str %s option_length %u",
if_filter.data.filter_str, option_length);
/* Fails with multiple options; we silently ignore the failure */
wtap_block_add_if_filter_option(wblock->block, option_code, &if_filter);
g_free(if_filter.data.filter_str);
} else if (option_content[0] == 1) {
/*
* XXX - byte-swap the code and k fields
* of each instruction as needed!
*
* XXX - what if option_length-1 is not a
* multiple of the size of a BPF instruction?
*/
guint num_insns;
const guint8 *insn_in;
if_filter.type = if_filter_bpf;
num_insns = (option_length-1)/8;
insn_in = option_content+1;
if_filter.data.bpf_prog.bpf_prog_len = num_insns;
if_filter.data.bpf_prog.bpf_prog = g_new(wtap_bpf_insn_t, num_insns);
for (guint i = 0; i < num_insns; i++) {
wtap_bpf_insn_t *insn = &if_filter.data.bpf_prog.bpf_prog[i];
memcpy(&insn->code, insn_in, 2);
if (section_info->byte_swapped)
insn->code = GUINT16_SWAP_LE_BE(insn->code);
insn_in += 2;
memcpy(&insn->jt, insn_in, 1);
insn_in += 1;
memcpy(&insn->jf, insn_in, 1);
insn_in += 1;
memcpy(&insn->k, insn_in, 4);
if (section_info->byte_swapped)
insn->k = GUINT32_SWAP_LE_BE(insn->k);
insn_in += 4;
}
/* Fails with multiple options; we silently ignore the failure */
wtap_block_add_if_filter_option(wblock->block, option_code, &if_filter);
g_free(if_filter.data.bpf_prog.bpf_prog);
}
break;
case(OPT_IDB_OS): /* if_os */
/*
* if_os 12 A UTF-8 string containing the name of the operating system of the machine in which this interface is installed.
* This can be different from the same information that can be contained by the Section Header Block (Section 3.1 (Section Header Block (mandatory)))
* because the capture can have been done on a remote machine. "Windows XP SP2" / "openSUSE 10.2" / ...
*/
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
case(OPT_IDB_FCSLEN): /* if_fcslen */
pcapng_process_uint8_option(wblock, option_code, option_length,
option_content);
break;
case(OPT_IDB_HARDWARE): /* if_hardware */
pcapng_process_string_option(wblock, option_code, option_length,
option_content);
break;
/* TODO: process these! */
case(OPT_IDB_IP4ADDR):
/*
* Interface network address and netmask. This option can be
* repeated multiple times within the same Interface
* Description Block when multiple IPv4 addresses are assigned
* to the interface. 192 168 1 1 255 255 255 0
*/
break;
case(OPT_IDB_IP6ADDR):
/*
* Interface network address and prefix length (stored in the
* last byte). This option can be repeated multiple times
* within the same Interface Description Block when multiple
* IPv6 addresses are assigned to the interface.
* 2001:0db8:85a3:08d3:1319:8a2e:0370:7344/64 is written (in
* hex) as "20 01 0d b8 85 a3 08 d3 13 19 8a 2e 03 70 73 44
* 40"
*/
break;
case(OPT_IDB_MACADDR):
/*
* Interface Hardware MAC address (48 bits). 00 01 02 03 04 05
*/
break;
case(OPT_IDB_EUIADDR):
/*
* Interface Hardware EUI address (64 bits), if available.
* TODO: give a good example
*/
break;
case(OPT_IDB_TZONE):
/*
* Time zone for GMT support. This option has never been
* specified in greater detail and, unless it were to identify
* something such as an IANA time zone database timezone,
* would be insufficient for converting between UTC and local
* time. Therefore, it SHOULD NOT be used; instead, the
* if_iana_tzname option SHOULD be used if time zone
* information is to be specified.
*
* Given that, we don't do anything with it.
*/
break;
case(OPT_IDB_TSOFFSET):
/*
* A 64-bit integer value that specifies an offset (in
* seconds) that must be added to the timestamp of each packet
* to obtain the absolute timestamp of a packet. If this optio
* is not present, an offset of 0 is assumed (i.e., timestamps
* in blocks are absolute timestamps.)
*/
pcapng_process_int64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
default:
if (!pcapng_process_unhandled_option(wblock, BT_INDEX_IDB,
section_info, option_code,
option_length, option_content,
err, err_info))
return FALSE;
break;
}
return TRUE;
}
/* "Interface Description Block" */
static gboolean
pcapng_read_if_descr_block(wtap *wth, FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
wtapng_block_t *wblock, int *err, gchar **err_info)
{
/* Default time stamp resolution is 10^6 */
guint64 time_units_per_second = 1000000;
int tsprecision = 6;
guint opt_cont_buf_len;
pcapng_interface_description_block_t idb;
wtapng_if_descr_mandatory_t* if_descr_mand;
guint link_type;
guint8 if_tsresol;
/*
* Is this block long enough to be an IDB?
*/
if (bh->block_total_length < MIN_IDB_SIZE) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an IDB is less than the minimum IDB size %u",
bh->block_total_length, MIN_IDB_SIZE);
return FALSE;
}
/* read block content */
if (!wtap_read_bytes(fh, &idb, sizeof idb, err, err_info)) {
ws_debug("failed to read IDB");
return FALSE;
}
/*
* Set wblock->block to a newly-allocated interface ID and information
* block.
*/
wblock->block = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO);
/*
* Set the mandatory values for the block.
*/
if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(wblock->block);
if (section_info->byte_swapped) {
link_type = GUINT16_SWAP_LE_BE(idb.linktype);
if_descr_mand->snap_len = GUINT32_SWAP_LE_BE(idb.snaplen);
} else {
link_type = idb.linktype;
if_descr_mand->snap_len = idb.snaplen;
}
if_descr_mand->wtap_encap = wtap_pcap_encap_to_wtap_encap(link_type);
ws_debug("IDB link_type %u (%s), snap %u",
link_type,
wtap_encap_description(if_descr_mand->wtap_encap),
if_descr_mand->snap_len);
if (if_descr_mand->snap_len > wtap_max_snaplen_for_encap(if_descr_mand->wtap_encap)) {
/*
* We do not use this value, maybe we should check the
* snap_len of the packets against it. For now, only warn.
*/
ws_debug("snapshot length %u unrealistic.",
if_descr_mand->snap_len);
/*if_descr_mand->snap_len = WTAP_MAX_PACKET_SIZE_STANDARD;*/
}
/* Options */
opt_cont_buf_len = bh->block_total_length - MIN_IDB_SIZE;
if (!pcapng_process_options(fh, wblock, section_info, opt_cont_buf_len,
pcapng_process_if_descr_block_option,
OPT_SECTION_BYTE_ORDER, err, err_info))
return FALSE;
/*
* Did we get a time stamp precision option?
*/
if (wtap_block_get_uint8_option_value(wblock->block, OPT_IDB_TSRESOL,
&if_tsresol) == WTAP_OPTTYPE_SUCCESS) {
/*
* Yes. Set time_units_per_second appropriately.
*/
guint8 exponent;
exponent = (guint8)(if_tsresol & 0x7f);
if (if_tsresol & 0x80) {
/*
* 2^63 fits in a 64-bit unsigned number; 2^64 does not.
*
* ((2^64-1)/(2^63) is about 1.99, so, in practice, that
* fine a time stamp resolution works only if you start
* capturing at the Unix/POSIX epoch and capture for about
* 1.9 seconds, so the maximum useful power-of-2 exponent
* in a pcapng file is less than 63.)
*/
if (exponent > 63) {
/*
* Time units per second won't fit in a 64-bit integer,
* so Wireshark's current code can't read the file.
*/
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("pcapng: IDB power-of-2 time stamp resolution %u > 63",
exponent);
return FALSE;
}
/* 2^exponent */
time_units_per_second = G_GUINT64_CONSTANT(1) << exponent;
/*
* Set the display precision to a value large enough to
* show the fractional time units we get, so that we
* don't display more digits than are justified.
*
* (That's also used as the base-10 if_tsresol value we use
* if we write this file as a pcapng file. Yes, that means
* that we won't write out the exact value we read in.
*
* Dealing with base-2 time stamps is a bit of a mess,
* thanks to humans counting with their fingers rather
* than their hands, and it applies to mroe files than
* pcapng files, e.g. ERF files.)
*/
if (time_units_per_second >= 1000000000)
tsprecision = WTAP_TSPREC_NSEC;
else if (time_units_per_second >= 100000000)
tsprecision = WTAP_TSPREC_10_NSEC;
else if (time_units_per_second >= 10000000)
tsprecision = WTAP_TSPREC_100_NSEC;
else if (time_units_per_second >= 1000000)
tsprecision = WTAP_TSPREC_USEC;
else if (time_units_per_second >= 100000)
tsprecision = WTAP_TSPREC_10_USEC;
else if (time_units_per_second >= 10000)
tsprecision = WTAP_TSPREC_100_USEC;
else if (time_units_per_second >= 1000)
tsprecision = WTAP_TSPREC_MSEC;
else if (time_units_per_second >= 100)
tsprecision = WTAP_TSPREC_10_MSEC;
else if (time_units_per_second >= 10)
tsprecision = WTAP_TSPREC_100_MSEC;
else
tsprecision = WTAP_TSPREC_SEC;
} else {
/*
* 10^19 fits in a 64-bit unsigned number; 10^20 does not.
*
* ((2^64-1)/(10^19) is about 1.84, so, in practice, that
* fine a time stamp resolution works only if you start
* capturing at the Unix/POSIX epoch and capture for about
* 1.8 seconds, so the maximum useful power-of-10 exponent
* in a pcapng file is less than 19.)
*/
guint64 result;
if (exponent > 19) {
/*
* Time units per second won't fit in a 64-bit integer,
* so Wireshark's current code can't read the file.
*/
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("pcapng: IDB power-of-10 time stamp resolution %u > 19",
exponent);
return FALSE;
}
/* 10^exponent */
result = 1;
for (guint i = 0; i < exponent; i++) {
result *= 10U;
}
time_units_per_second = result;
/*
* Set the display precision to min(exponent, WS_TSPREC_MAX),
* so that we don't display more digits than are justified.
* (That's also used as the base-10 if_tsresol value we use
* if we write this file as a pcapng file.)
*/
if (exponent <= WS_TSPREC_MAX) {
tsprecision = exponent;
} else {
tsprecision = WS_TSPREC_MAX;
}
}
if (time_units_per_second > (((guint64)1) << 32)) {
ws_debug("time conversion might be inaccurate");
}
}
/*
* Set the time units per second for this interface.
*/
if_descr_mand->time_units_per_second = time_units_per_second;
/*
* Set the number of digits of precision to display (and the
* number to use for this interface if saving to a pcapng
* file).
*/
if_descr_mand->tsprecision = tsprecision;
/*
* If the per-file encapsulation isn't known, set it to this
* interface's encapsulation.
*
* If it *is* known, and it isn't this interface's encapsulation,
* set it to WTAP_ENCAP_PER_PACKET, as this file doesn't
* have a single encapsulation for all interfaces in the file,
* so it probably doesn't have a single encapsulation for all
* packets in the file.
*/
if (wth->file_encap == WTAP_ENCAP_NONE) {
wth->file_encap = if_descr_mand->wtap_encap;
} else {
if (wth->file_encap != if_descr_mand->wtap_encap) {
wth->file_encap = WTAP_ENCAP_PER_PACKET;
}
}
/*
* The same applies to the per-file time stamp resolution.
*/
if (wth->file_tsprec == WTAP_TSPREC_UNKNOWN) {
wth->file_tsprec = if_descr_mand->tsprecision;
} else {
if (wth->file_tsprec != if_descr_mand->tsprecision) {
wth->file_tsprec = WTAP_TSPREC_PER_PACKET;
}
}
/*
* We don't return these to the caller in pcapng_read().
*/
wblock->internal = TRUE;
return TRUE;
}
static gboolean
pcapng_read_decryption_secrets_block(FILE_T fh, pcapng_block_header_t *bh,
const section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
guint to_read;
pcapng_decryption_secrets_block_t dsb;
wtapng_dsb_mandatory_t *dsb_mand;
/* read block content */
if (!wtap_read_bytes(fh, &dsb, sizeof(dsb), err, err_info)) {
ws_debug("failed to read DSB");
return FALSE;
}
/*
* Set wblock->block to a newly-allocated decryption secrets block.
*/
wblock->block = wtap_block_create(WTAP_BLOCK_DECRYPTION_SECRETS);
/*
* Set the mandatory values for the block.
*/
dsb_mand = (wtapng_dsb_mandatory_t *)wtap_block_get_mandatory_data(wblock->block);
if (section_info->byte_swapped) {
dsb_mand->secrets_type = GUINT32_SWAP_LE_BE(dsb.secrets_type);
dsb_mand->secrets_len = GUINT32_SWAP_LE_BE(dsb.secrets_len);
} else {
dsb_mand->secrets_type = dsb.secrets_type;
dsb_mand->secrets_len = dsb.secrets_len;
}
/* Sanity check: assume the secrets are not larger than 1 GiB */
if (dsb_mand->secrets_len > 1024 * 1024 * 1024) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: secrets block is too large: %u", dsb_mand->secrets_len);
return FALSE;
}
dsb_mand->secrets_data = (guint8 *)g_malloc0(dsb_mand->secrets_len);
if (!wtap_read_bytes(fh, dsb_mand->secrets_data, dsb_mand->secrets_len, err, err_info)) {
ws_debug("failed to read DSB");
return FALSE;
}
/* Skip past padding and discard options (not supported yet). */
to_read = bh->block_total_length - MIN_DSB_SIZE - dsb_mand->secrets_len;
if (!wtap_read_bytes(fh, NULL, to_read, err, err_info)) {
ws_debug("failed to read DSB options");
return FALSE;
}
/*
* We don't return these to the caller in pcapng_read().
*/
wblock->internal = TRUE;
return TRUE;
}
static bool
pcapng_read_sysdig_meta_event_block(FILE_T fh, pcapng_block_header_t *bh,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
guint to_read;
wtapng_sysdig_mev_mandatory_t *mev_mand;
/*
* Set wblock->block to a newly-allocated Sysdig meta event block.
*/
wblock->block = wtap_block_create(WTAP_BLOCK_SYSDIG_META_EVENT);
/*
* Set the mandatory values for the block.
*/
mev_mand = (wtapng_sysdig_mev_mandatory_t *)wtap_block_get_mandatory_data(wblock->block);
mev_mand->mev_type = bh->block_type;
mev_mand->mev_data_len = bh->block_total_length -
(int)sizeof(pcapng_block_header_t) -
(int)sizeof(bh->block_total_length);
/* Sanity check: assume event data can't be larger than 1 GiB */
if (mev_mand->mev_data_len > 1024 * 1024 * 1024) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: Sysdig mev block is too large: %u", mev_mand->mev_data_len);
return false;
}
mev_mand->mev_data = (uint8_t *)g_malloc(mev_mand->mev_data_len);
if (!wtap_read_bytes(fh, mev_mand->mev_data, mev_mand->mev_data_len, err, err_info)) {
ws_debug("failed to read Sysdig mev");
return false;
}
/* Skip past padding and discard options (not supported yet). */
to_read = bh->block_total_length - MIN_BLOCK_SIZE - mev_mand->mev_data_len;
if (!wtap_read_bytes(fh, NULL, to_read, err, err_info)) {
ws_debug("failed to read Sysdig mev options");
return FALSE;
}
/*
* We don't return these to the caller in pcapng_read().
*/
wblock->internal = true;
return true;
}
static gboolean
pcapng_process_packet_block_option(wtapng_block_t *wblock,
const section_info_t *section_info,
guint16 option_code,
guint16 option_length,
const guint8 *option_content,
int *err, gchar **err_info)
{
guint64 tmp64;
packet_verdict_opt_t packet_verdict;
packet_hash_opt_t packet_hash;
/*
* Handle option content.
*
* ***DO NOT*** add any items to this table that are not
* standardized option codes in either section 3.5 "Options"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-options
*
* or in the list of options in section 4.3 "Enhanced Packet Block"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-enhanced-packet-block
*
* All option codes in this switch statement here must be listed
* in one of those places as standardized option types.
*/
switch (option_code) {
case(OPT_EPB_FLAGS):
if (option_length != 4) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block flags option length %u is not 4",
option_length);
/* XXX - free anything? */
return FALSE;
}
pcapng_process_uint32_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_EPB_HASH):
if (option_length < 1) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block hash option length %u is < 1",
option_length);
/* XXX - free anything? */
return FALSE;
}
packet_hash.type = option_content[0];
packet_hash.hash_bytes =
g_byte_array_new_take((guint8 *)g_memdup2(&option_content[1],
option_length - 1),
option_length - 1);
wtap_block_add_packet_hash_option(wblock->block, option_code, &packet_hash);
wtap_packet_hash_free(&packet_hash);
ws_debug("hash type %u, data len %u",
option_content[0], option_length - 1);
break;
case(OPT_EPB_DROPCOUNT):
if (option_length != 8) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block drop count option length %u is not 8",
option_length);
/* XXX - free anything? */
return FALSE;
}
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_EPB_PACKETID):
if (option_length != 8) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block packet id option length %u is not 8",
option_length);
/* XXX - free anything? */
return FALSE;
}
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_EPB_QUEUE):
if (option_length != 4) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block queue option length %u is not 4",
option_length);
/* XXX - free anything? */
return FALSE;
}
pcapng_process_uint32_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_EPB_VERDICT):
if (option_length < 1) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block verdict option length %u is < 1",
option_length);
/* XXX - free anything? */
return FALSE;
}
switch (option_content[0]) {
case(OPT_VERDICT_TYPE_HW):
packet_verdict.type = packet_verdict_hardware;
packet_verdict.data.verdict_bytes =
g_byte_array_new_take((guint8 *)g_memdup2(&option_content[1],
option_length - 1),
option_length - 1);
break;
case(OPT_VERDICT_TYPE_TC):
if (option_length != 9) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block TC verdict option length %u is != 9",
option_length);
/* XXX - free anything? */
return FALSE;
}
/* Don't cast a guint8 * into a guint64 *--the
* guint8 * may not point to something that's
* aligned correctly.
*/
memcpy(&tmp64, &option_content[1], sizeof(guint64));
if (section_info->byte_swapped)
tmp64 = GUINT64_SWAP_LE_BE(tmp64);
packet_verdict.type = packet_verdict_linux_ebpf_tc;
packet_verdict.data.verdict_linux_ebpf_tc = tmp64;
break;
case(OPT_VERDICT_TYPE_XDP):
if (option_length != 9) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: packet block XDP verdict option length %u is != 9",
option_length);
/* XXX - free anything? */
return FALSE;
}
/* Don't cast a guint8 * into a guint64 *--the
* guint8 * may not point to something that's
* aligned correctly.
*/
memcpy(&tmp64, &option_content[1], sizeof(guint64));
if (section_info->byte_swapped)
tmp64 = GUINT64_SWAP_LE_BE(tmp64);
packet_verdict.type = packet_verdict_linux_ebpf_xdp;
packet_verdict.data.verdict_linux_ebpf_xdp = tmp64;
break;
default:
/* Silently ignore unknown verdict types */
return TRUE;
}
wtap_block_add_packet_verdict_option(wblock->block, option_code, &packet_verdict);
wtap_packet_verdict_free(&packet_verdict);
ws_debug("verdict type %u, data len %u",
option_content[0], option_length - 1);
break;
default:
if (!pcapng_process_unhandled_option(wblock, BT_INDEX_PBS,
section_info, option_code,
option_length, option_content,
err, err_info))
return FALSE;
break;
}
return TRUE;
}
static gboolean
pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info, gboolean enhanced)
{
guint block_read;
guint opt_cont_buf_len;
pcapng_enhanced_packet_block_t epb;
pcapng_packet_block_t pb;
wtapng_packet_t packet;
guint32 padding;
guint32 flags;
guint64 tmp64;
interface_info_t iface_info;
guint64 ts;
int pseudo_header_len;
int fcslen;
wblock->block = wtap_block_create(WTAP_BLOCK_PACKET);
/* "(Enhanced) Packet Block" read fixed part */
if (enhanced) {
/*
* Is this block long enough to be an EPB?
*/
if (bh->block_total_length < MIN_EPB_SIZE) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an EPB is less than the minimum EPB size %u",
bh->block_total_length, MIN_EPB_SIZE);
return FALSE;
}
if (!wtap_read_bytes(fh, &epb, sizeof epb, err, err_info)) {
ws_debug("failed to read packet data");
return FALSE;
}
block_read = (guint)sizeof epb;
if (section_info->byte_swapped) {
packet.interface_id = GUINT32_SWAP_LE_BE(epb.interface_id);
packet.drops_count = 0xFFFF; /* invalid */
packet.ts_high = GUINT32_SWAP_LE_BE(epb.timestamp_high);
packet.ts_low = GUINT32_SWAP_LE_BE(epb.timestamp_low);
packet.cap_len = GUINT32_SWAP_LE_BE(epb.captured_len);
packet.packet_len = GUINT32_SWAP_LE_BE(epb.packet_len);
} else {
packet.interface_id = epb.interface_id;
packet.drops_count = 0xFFFF; /* invalid */
packet.ts_high = epb.timestamp_high;
packet.ts_low = epb.timestamp_low;
packet.cap_len = epb.captured_len;
packet.packet_len = epb.packet_len;
}
ws_debug("EPB on interface_id %d, cap_len %d, packet_len %d",
packet.interface_id, packet.cap_len, packet.packet_len);
} else {
/*
* Is this block long enough to be a PB?
*/
if (bh->block_total_length < MIN_PB_SIZE) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of a PB is less than the minimum PB size %u",
bh->block_total_length, MIN_PB_SIZE);
return FALSE;
}
if (!wtap_read_bytes(fh, &pb, sizeof pb, err, err_info)) {
ws_debug("failed to read packet data");
return FALSE;
}
block_read = (guint)sizeof pb;
if (section_info->byte_swapped) {
packet.interface_id = GUINT16_SWAP_LE_BE(pb.interface_id);
packet.drops_count = GUINT16_SWAP_LE_BE(pb.drops_count);
packet.ts_high = GUINT32_SWAP_LE_BE(pb.timestamp_high);
packet.ts_low = GUINT32_SWAP_LE_BE(pb.timestamp_low);
packet.cap_len = GUINT32_SWAP_LE_BE(pb.captured_len);
packet.packet_len = GUINT32_SWAP_LE_BE(pb.packet_len);
} else {
packet.interface_id = pb.interface_id;
packet.drops_count = pb.drops_count;
packet.ts_high = pb.timestamp_high;
packet.ts_low = pb.timestamp_low;
packet.cap_len = pb.captured_len;
packet.packet_len = pb.packet_len;
}
ws_debug("PB on interface_id %d, cap_len %d, packet_len %d",
packet.interface_id, packet.cap_len, packet.packet_len);
}
/*
* How much padding is there at the end of the packet data?
*/
if ((packet.cap_len % 4) != 0)
padding = 4 - (packet.cap_len % 4);
else
padding = 0;
/*
* Is this block long enough to hold the packet data?
*/
if (enhanced) {
if (bh->block_total_length <
MIN_EPB_SIZE + packet.cap_len + padding) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an EPB is too small for %u bytes of packet data",
bh->block_total_length, packet.cap_len);
return FALSE;
}
} else {
if (bh->block_total_length <
MIN_PB_SIZE + packet.cap_len + padding) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of a PB is too small for %u bytes of packet data",
bh->block_total_length, packet.cap_len);
return FALSE;
}
}
ws_debug("packet data: packet_len %u captured_len %u interface_id %u",
packet.packet_len,
packet.cap_len,
packet.interface_id);
if (packet.interface_id >= section_info->interfaces->len) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: interface index %u is not less than section interface count %u",
packet.interface_id,
section_info->interfaces->len);
return FALSE;
}
iface_info = g_array_index(section_info->interfaces, interface_info_t,
packet.interface_id);
if (packet.cap_len > wtap_max_snaplen_for_encap(iface_info.wtap_encap)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: cap_len %u is larger than %u",
packet.cap_len,
wtap_max_snaplen_for_encap(iface_info.wtap_encap));
return FALSE;
}
wblock->rec->rec_type = REC_TYPE_PACKET;
wblock->rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID;
ws_debug("encapsulation = %d (%s), pseudo header size = %d.",
iface_info.wtap_encap,
wtap_encap_description(iface_info.wtap_encap),
pcap_get_phdr_size(iface_info.wtap_encap, &wblock->rec->rec_header.packet_header.pseudo_header));
wblock->rec->rec_header.packet_header.interface_id = packet.interface_id;
wblock->rec->rec_header.packet_header.pkt_encap = iface_info.wtap_encap;
wblock->rec->tsprec = iface_info.tsprecision;
memset((void *)&wblock->rec->rec_header.packet_header.pseudo_header, 0, sizeof(union wtap_pseudo_header));
pseudo_header_len = pcap_process_pseudo_header(fh,
FALSE, /* not a Nokia pcap - not a pcap at all */
iface_info.wtap_encap,
packet.cap_len,
wblock->rec,
err,
err_info);
if (pseudo_header_len < 0) {
return FALSE;
}
block_read += pseudo_header_len;
wblock->rec->rec_header.packet_header.caplen = packet.cap_len - pseudo_header_len;
wblock->rec->rec_header.packet_header.len = packet.packet_len - pseudo_header_len;
/* Combine the two 32-bit pieces of the timestamp into one 64-bit value */
ts = (((guint64)packet.ts_high) << 32) | ((guint64)packet.ts_low);
/* Convert it to seconds and nanoseconds. */
wblock->rec->ts.secs = (time_t)(ts / iface_info.time_units_per_second);
wblock->rec->ts.nsecs = (int)(((ts % iface_info.time_units_per_second) * 1000000000) / iface_info.time_units_per_second);
/* Add the time stamp offset. */
wblock->rec->ts.secs = (time_t)(wblock->rec->ts.secs + iface_info.tsoffset);
/* "(Enhanced) Packet Block" read capture data */
if (!wtap_read_packet_bytes(fh, wblock->frame_buffer,
packet.cap_len - pseudo_header_len, err, err_info))
return FALSE;
block_read += packet.cap_len - pseudo_header_len;
/* jump over potential padding bytes at end of the packet data */
if (padding != 0) {
if (!wtap_read_bytes(fh, NULL, padding, err, err_info))
return FALSE;
block_read += padding;
}
/* FCS length default */
fcslen = iface_info.fcslen;
/* Options */
opt_cont_buf_len = bh->block_total_length -
(int)sizeof(pcapng_block_header_t) -
block_read - /* fixed and variable part, including padding */
(int)sizeof(bh->block_total_length);
if (!pcapng_process_options(fh, wblock, section_info, opt_cont_buf_len,
pcapng_process_packet_block_option,
OPT_SECTION_BYTE_ORDER, err, err_info))
return FALSE;
/*
* Did we get a packet flags option?
*/
if (WTAP_OPTTYPE_SUCCESS == wtap_block_get_uint32_option_value(wblock->block, OPT_PKT_FLAGS, &flags)) {
if (PACK_FLAGS_FCS_LENGTH(flags) != 0) {
/*
* The FCS length is present, but in units of octets, not
* bits; convert it to bits.
*/
fcslen = PACK_FLAGS_FCS_LENGTH(flags)*8;
}
}
/*
* How about a drop_count option? If not, set it from other sources
*/
if (WTAP_OPTTYPE_SUCCESS != wtap_block_get_uint64_option_value(wblock->block, OPT_PKT_DROPCOUNT, &tmp64) && packet.drops_count != 0xFFFF) {
wtap_block_add_uint64_option(wblock->block, OPT_PKT_DROPCOUNT, (guint64)packet.drops_count);
}
pcap_read_post_process(FALSE, iface_info.wtap_encap,
wblock->rec, ws_buffer_start_ptr(wblock->frame_buffer),
section_info->byte_swapped, fcslen);
/*
* We return these to the caller in pcapng_read().
*/
wblock->internal = FALSE;
/*
* We want dissectors (particularly packet_frame) to be able to
* access packet comments and whatnot that are in the block. wblock->block
* will be unref'd by pcapng_seek_read(), so move the block to where
* dissectors can find it.
*/
wblock->rec->block = wblock->block;
wblock->block = NULL;
return TRUE;
}
static gboolean
pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh,
const section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
interface_info_t iface_info;
pcapng_simple_packet_block_t spb;
wtapng_simple_packet_t simple_packet;
guint32 padding;
int pseudo_header_len;
/*
* Is this block long enough to be an SPB?
*/
if (bh->block_total_length < MIN_SPB_SIZE) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an SPB is less than the minimum SPB size %u",
bh->block_total_length, MIN_SPB_SIZE);
return FALSE;
}
/* "Simple Packet Block" read fixed part */
if (!wtap_read_bytes(fh, &spb, sizeof spb, err, err_info)) {
ws_debug("failed to read packet data");
return FALSE;
}
if (0 >= section_info->interfaces->len) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("pcapng: SPB appeared before any IDBs in the section");
return FALSE;
}
iface_info = g_array_index(section_info->interfaces, interface_info_t, 0);
if (section_info->byte_swapped) {
simple_packet.packet_len = GUINT32_SWAP_LE_BE(spb.packet_len);
} else {
simple_packet.packet_len = spb.packet_len;
}
/*
* The captured length is not a field in the SPB; it can be
* calculated as the minimum of the snapshot length from the
* IDB and the packet length, as per the pcapng spec. An IDB
* snapshot length of 0 means no limit.
*/
simple_packet.cap_len = simple_packet.packet_len;
if (simple_packet.cap_len > iface_info.snap_len && iface_info.snap_len != 0)
simple_packet.cap_len = iface_info.snap_len;
/*
* How much padding is there at the end of the packet data?
*/
if ((simple_packet.cap_len % 4) != 0)
padding = 4 - (simple_packet.cap_len % 4);
else
padding = 0;
/*
* Is this block long enough to hold the packet data?
*/
if (bh->block_total_length < MIN_SPB_SIZE + simple_packet.cap_len + padding) {
/*
* No. That means that the problem is with the packet
* length; the snapshot length can be bigger than the amount
* of packet data in the block, as it's a *maximum* length,
* not a *minimum* length.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an SPB is too small for %u bytes of packet data",
bh->block_total_length, simple_packet.packet_len);
return FALSE;
}
if (simple_packet.cap_len > wtap_max_snaplen_for_encap(iface_info.wtap_encap)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: cap_len %u is larger than %u",
simple_packet.cap_len,
wtap_max_snaplen_for_encap(iface_info.wtap_encap));
return FALSE;
}
ws_debug("packet data: packet_len %u",
simple_packet.packet_len);
ws_debug("Need to read pseudo header of size %d",
pcap_get_phdr_size(iface_info.wtap_encap, &wblock->rec->rec_header.packet_header.pseudo_header));
/* No time stamp in a simple packet block; no options, either */
wblock->rec->rec_type = REC_TYPE_PACKET;
wblock->rec->presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID;
wblock->rec->rec_header.packet_header.interface_id = 0;
wblock->rec->rec_header.packet_header.pkt_encap = iface_info.wtap_encap;
wblock->rec->tsprec = iface_info.tsprecision;
wblock->rec->ts.secs = 0;
wblock->rec->ts.nsecs = 0;
wblock->rec->rec_header.packet_header.interface_id = 0;
memset((void *)&wblock->rec->rec_header.packet_header.pseudo_header, 0, sizeof(union wtap_pseudo_header));
pseudo_header_len = pcap_process_pseudo_header(fh,
FALSE,
iface_info.wtap_encap,
simple_packet.cap_len,
wblock->rec,
err,
err_info);
if (pseudo_header_len < 0) {
return FALSE;
}
wblock->rec->rec_header.packet_header.caplen = simple_packet.cap_len - pseudo_header_len;
wblock->rec->rec_header.packet_header.len = simple_packet.packet_len - pseudo_header_len;
memset((void *)&wblock->rec->rec_header.packet_header.pseudo_header, 0, sizeof(union wtap_pseudo_header));
/* "Simple Packet Block" read capture data */
if (!wtap_read_packet_bytes(fh, wblock->frame_buffer,
simple_packet.cap_len, err, err_info))
return FALSE;
/* jump over potential padding bytes at end of the packet data */
if ((simple_packet.cap_len % 4) != 0) {
if (!wtap_read_bytes(fh, NULL, 4 - (simple_packet.cap_len % 4), err, err_info))
return FALSE;
}
pcap_read_post_process(FALSE, iface_info.wtap_encap,
wblock->rec, ws_buffer_start_ptr(wblock->frame_buffer),
section_info->byte_swapped, iface_info.fcslen);
/*
* We return these to the caller in pcapng_read().
*/
wblock->internal = FALSE;
return TRUE;
}
#define NRES_ENDOFRECORD 0
#define NRES_IP4RECORD 1
#define NRES_IP6RECORD 2
#define PADDING4(x) ((((x + 3) >> 2) << 2) - x)
/* IPv6 + MAXNAMELEN */
#define INITIAL_NRB_REC_SIZE (16 + 64)
/*
* Find the end of the NUL-terminated name the beginning of which is pointed
* to by p; record_len is the number of bytes remaining in the record.
*
* Return the length of the name, including the terminating NUL.
*
* If we don't find a terminating NUL, return -1 and set *err and
* *err_info appropriately.
*/
static int
name_resolution_block_find_name_end(const char *p, guint record_len, int *err,
gchar **err_info)
{
int namelen;
namelen = 0;
for (;;) {
if (record_len == 0) {
/*
* We ran out of bytes in the record without
* finding a NUL.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("pcapng: NRB record has non-null-terminated host name");
return -1;
}
if (*p == '\0')
break; /* that's the terminating NUL */
p++;
record_len--;
namelen++; /* count this byte */
}
/* Include the NUL in the name length. */
return namelen + 1;
}
static gboolean
pcapng_process_name_resolution_block_option(wtapng_block_t *wblock,
const section_info_t *section_info,
guint16 option_code,
guint16 option_length,
const guint8 *option_content,
int *err, gchar **err_info)
{
/*
* Handle option content.
*
* ***DO NOT*** add any items to this table that are not
* standardized option codes in either section 3.5 "Options"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-options
*
* or in the list of options in section 4.1 "Section Header Block"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-section-header-block
*
* All option codes in this switch statement here must be listed
* in one of those places as standardized option types.
*/
switch (option_code) {
/* TODO:
* ns_dnsname 2
* ns_dnsIP4addr 3
* ns_dnsIP6addr 4
*/
default:
if (!pcapng_process_unhandled_option(wblock, BT_INDEX_NRB,
section_info, option_code,
option_length, option_content,
err, err_info))
return FALSE;
break;
}
return TRUE;
}
static gboolean
pcapng_read_name_resolution_block(FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
int block_read;
int to_read;
pcapng_name_resolution_block_t nrb;
Buffer nrb_rec;
guint32 v4_addr;
guint record_len, opt_cont_buf_len;
char *namep;
int namelen;
wtapng_nrb_mandatory_t *nrb_mand;
/*
* Is this block long enough to be an NRB?
*/
if (bh->block_total_length < MIN_NRB_SIZE) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an NRB is less than the minimum NRB size %u",
bh->block_total_length, MIN_NRB_SIZE);
return FALSE;
}
to_read = bh->block_total_length - 8 - 4; /* We have read the header and should not read the final block_total_length */
ws_debug("total %d bytes", bh->block_total_length);
/* Ensure we have a name resolution block */
if (wblock->block == NULL) {
wblock->block = wtap_block_create(WTAP_BLOCK_NAME_RESOLUTION);
}
/*
* Set the mandatory values for the block.
*/
nrb_mand = (wtapng_nrb_mandatory_t *)wtap_block_get_mandatory_data(wblock->block);
/*
* Start out with a buffer big enough for an IPv6 address and one
* 64-byte name; we'll make the buffer bigger if necessary.
*/
ws_buffer_init(&nrb_rec, INITIAL_NRB_REC_SIZE);
block_read = 0;
while (block_read < to_read) {
/*
* There must be at least one record's worth of data
* here.
*/
if ((size_t)(to_read - block_read) < sizeof nrb) {
ws_buffer_free(&nrb_rec);
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: %d bytes left in the block < NRB record header size %u",
to_read - block_read,
(guint)sizeof nrb);
return FALSE;
}
if (!wtap_read_bytes(fh, &nrb, sizeof nrb, err, err_info)) {
ws_buffer_free(&nrb_rec);
ws_debug("failed to read record header");
return FALSE;
}
block_read += (int)sizeof nrb;
if (section_info->byte_swapped) {
nrb.record_type = GUINT16_SWAP_LE_BE(nrb.record_type);
nrb.record_len = GUINT16_SWAP_LE_BE(nrb.record_len);
}
if (to_read - block_read < nrb.record_len + PADDING4(nrb.record_len)) {
ws_buffer_free(&nrb_rec);
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: %d bytes left in the block < NRB record length + padding %u",
to_read - block_read,
nrb.record_len + PADDING4(nrb.record_len));
return FALSE;
}
switch (nrb.record_type) {
case NRES_ENDOFRECORD:
/* There shouldn't be any more data - but there MAY be options */
goto read_options;
break;
case NRES_IP4RECORD:
/*
* The smallest possible record must have
* a 4-byte IPv4 address, hence a minimum
* of 4 bytes.
*
* (The pcapng spec really indicates
* that it must be at least 5 bytes,
* as there must be at least one name,
* and it really must be at least 6
* bytes, as the name mustn't be null,
* but there's no need to fail if there
* aren't any names at all, and we
* should report a null name as such.)
*/
if (nrb.record_len < 4) {
ws_buffer_free(&nrb_rec);
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: NRB record length for IPv4 record %u < minimum length 4",
nrb.record_len);
return FALSE;
}
ws_buffer_assure_space(&nrb_rec, nrb.record_len);
if (!wtap_read_bytes(fh, ws_buffer_start_ptr(&nrb_rec),
nrb.record_len, err, err_info)) {
ws_buffer_free(&nrb_rec);
ws_debug("failed to read IPv4 record data");
return FALSE;
}
block_read += nrb.record_len;
/*
* Scan through all the names in
* the record and add them.
*/
memcpy(&v4_addr,
ws_buffer_start_ptr(&nrb_rec), 4);
/* IPv4 address is in big-endian order in the file always, which is how we store
it internally as well, so don't byte-swap it */
for (namep = (char *)ws_buffer_start_ptr(&nrb_rec) + 4, record_len = nrb.record_len - 4;
record_len != 0;
namep += namelen, record_len -= namelen) {
/*
* Scan forward for a null
* byte.
*/
namelen = name_resolution_block_find_name_end(namep, record_len, err, err_info);
if (namelen == -1) {
ws_buffer_free(&nrb_rec);
return FALSE; /* fail */
}
hashipv4_t *tp = g_new0(hashipv4_t, 1);
tp->addr = v4_addr;
(void) g_strlcpy(tp->name, namep, MAXNAMELEN);
nrb_mand->ipv4_addr_list = g_list_prepend(nrb_mand->ipv4_addr_list, tp);
}
if (!wtap_read_bytes(fh, NULL, PADDING4(nrb.record_len), err, err_info)) {
ws_buffer_free(&nrb_rec);
return FALSE;
}
block_read += PADDING4(nrb.record_len);
break;
case NRES_IP6RECORD:
/*
* The smallest possible record must have
* a 16-byte IPv6 address, hence a minimum
* of 16 bytes.
*
* (The pcapng spec really indicates
* that it must be at least 17 bytes,
* as there must be at least one name,
* and it really must be at least 18
* bytes, as the name mustn't be null,
* but there's no need to fail if there
* aren't any names at all, and we
* should report a null name as such.)
*/
if (nrb.record_len < 16) {
ws_buffer_free(&nrb_rec);
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: NRB record length for IPv6 record %u < minimum length 16",
nrb.record_len);
return FALSE;
}
if (to_read < nrb.record_len) {
ws_buffer_free(&nrb_rec);
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: NRB record length for IPv6 record %u > remaining data in NRB",
nrb.record_len);
return FALSE;
}
ws_buffer_assure_space(&nrb_rec, nrb.record_len);
if (!wtap_read_bytes(fh, ws_buffer_start_ptr(&nrb_rec),
nrb.record_len, err, err_info)) {
ws_buffer_free(&nrb_rec);
return FALSE;
}
block_read += nrb.record_len;
for (namep = (char *)ws_buffer_start_ptr(&nrb_rec) + 16, record_len = nrb.record_len - 16;
record_len != 0;
namep += namelen, record_len -= namelen) {
/*
* Scan forward for a null
* byte.
*/
namelen = name_resolution_block_find_name_end(namep, record_len, err, err_info);
if (namelen == -1) {
ws_buffer_free(&nrb_rec);
return FALSE; /* fail */
}
hashipv6_t *tp = g_new0(hashipv6_t, 1);
memcpy(tp->addr, ws_buffer_start_ptr(&nrb_rec), sizeof tp->addr);
(void) g_strlcpy(tp->name, namep, MAXNAMELEN);
nrb_mand->ipv6_addr_list = g_list_prepend(nrb_mand->ipv6_addr_list, tp);
}
if (!wtap_read_bytes(fh, NULL, PADDING4(nrb.record_len), err, err_info)) {
ws_buffer_free(&nrb_rec);
return FALSE;
}
block_read += PADDING4(nrb.record_len);
break;
default:
ws_debug("unknown record type 0x%x", nrb.record_type);
if (!wtap_read_bytes(fh, NULL, nrb.record_len + PADDING4(nrb.record_len), err, err_info)) {
ws_buffer_free(&nrb_rec);
return FALSE;
}
block_read += nrb.record_len + PADDING4(nrb.record_len);
break;
}
}
read_options:
to_read -= block_read;
/* Options */
opt_cont_buf_len = to_read;
if (!pcapng_process_options(fh, wblock, section_info, opt_cont_buf_len,
pcapng_process_name_resolution_block_option,
OPT_SECTION_BYTE_ORDER, err, err_info))
return FALSE;
ws_buffer_free(&nrb_rec);
/*
* We don't return these to the caller in pcapng_read().
*/
wblock->internal = TRUE;
return TRUE;
}
static gboolean
pcapng_process_interface_statistics_block_option(wtapng_block_t *wblock,
const section_info_t *section_info,
guint16 option_code,
guint16 option_length,
const guint8 *option_content,
int *err, gchar **err_info)
{
/*
* Handle option content.
*
* ***DO NOT*** add any items to this table that are not
* standardized option codes in either section 3.5 "Options"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-options
*
* or in the list of options in section 4.1 "Section Header Block"
* of the current pcapng spec, at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html#name-section-header-block
*
* All option codes in this switch statement here must be listed
* in one of those places as standardized option types.
*/
switch (option_code) {
case(OPT_ISB_STARTTIME): /* isb_starttime */
pcapng_process_timestamp_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_ISB_ENDTIME): /* isb_endtime */
pcapng_process_timestamp_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_ISB_IFRECV): /* isb_ifrecv */
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_ISB_IFDROP): /* isb_ifdrop */
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_ISB_FILTERACCEPT): /* isb_filteraccept 6 */
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_ISB_OSDROP): /* isb_osdrop 7 */
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
case(OPT_ISB_USRDELIV): /* isb_usrdeliv 8 */
pcapng_process_uint64_option(wblock, section_info,
OPT_SECTION_BYTE_ORDER,
option_code, option_length,
option_content);
break;
default:
if (!pcapng_process_unhandled_option(wblock, BT_INDEX_ISB,
section_info, option_code,
option_length, option_content,
err, err_info))
return FALSE;
break;
}
return TRUE;
}
static gboolean
pcapng_read_interface_statistics_block(FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
guint opt_cont_buf_len;
pcapng_interface_statistics_block_t isb;
wtapng_if_stats_mandatory_t* if_stats_mand;
/*
* Is this block long enough to be an ISB?
*/
if (bh->block_total_length < MIN_ISB_SIZE) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an ISB is too small (< %u)",
bh->block_total_length, MIN_ISB_SIZE);
return FALSE;
}
/* "Interface Statistics Block" read fixed part */
if (!wtap_read_bytes(fh, &isb, sizeof isb, err, err_info)) {
ws_debug("failed to read packet data");
return FALSE;
}
/*
* Set wblock->block to a newly-allocated interface statistics block.
*/
wblock->block = wtap_block_create(WTAP_BLOCK_IF_STATISTICS);
/*
* Set the mandatory values for the block.
*/
if_stats_mand = (wtapng_if_stats_mandatory_t*)wtap_block_get_mandatory_data(wblock->block);
if (section_info->byte_swapped) {
if_stats_mand->interface_id = GUINT32_SWAP_LE_BE(isb.interface_id);
if_stats_mand->ts_high = GUINT32_SWAP_LE_BE(isb.timestamp_high);
if_stats_mand->ts_low = GUINT32_SWAP_LE_BE(isb.timestamp_low);
} else {
if_stats_mand->interface_id = isb.interface_id;
if_stats_mand->ts_high = isb.timestamp_high;
if_stats_mand->ts_low = isb.timestamp_low;
}
ws_debug("interface_id %u", if_stats_mand->interface_id);
/* Options */
opt_cont_buf_len = bh->block_total_length -
(MIN_BLOCK_SIZE + (guint)sizeof isb); /* fixed and variable part, including padding */
if (!pcapng_process_options(fh, wblock, section_info, opt_cont_buf_len,
pcapng_process_interface_statistics_block_option,
OPT_SECTION_BYTE_ORDER, err, err_info))
return FALSE;
/*
* We don't return these to the caller in pcapng_read().
*/
wblock->internal = TRUE;
return TRUE;
}
#define NFLX_BLOCK_TYPE_EVENT 1
#define NFLX_BLOCK_TYPE_SKIP 2
typedef struct pcapng_nflx_custom_block_s {
guint32 nflx_type;
} pcapng_nflx_custom_block_t;
#define MIN_NFLX_CB_SIZE ((guint32)(MIN_CB_SIZE + sizeof(pcapng_nflx_custom_block_t)))
static gboolean
pcapng_read_nflx_custom_block(FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
pcapng_nflx_custom_block_t nflx_cb;
guint opt_cont_buf_len;
guint32 type, skipped;
if (bh->block_total_length < MIN_NFLX_CB_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of a Netflix CB is too small (< %u)",
bh->block_total_length, MIN_NFLX_CB_SIZE);
return FALSE;
}
wblock->rec->rec_type = REC_TYPE_CUSTOM_BLOCK;
wblock->rec->rec_header.custom_block_header.pen = PEN_NFLX;
/* "NFLX Custom Block" read fixed part */
if (!wtap_read_bytes(fh, &nflx_cb, sizeof nflx_cb, err, err_info)) {
ws_debug("Failed to read nflx type");
return FALSE;
}
type = GUINT32_FROM_LE(nflx_cb.nflx_type);
ws_debug("BBLog type: %u", type);
switch (type) {
case NFLX_BLOCK_TYPE_EVENT:
/*
* The fixed-length portion is MIN_NFLX_CB_SIZE bytes.
* We already know we have that much data in the block.
*/
wblock->rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.type = BBLOG_TYPE_EVENT_BLOCK;
opt_cont_buf_len = bh->block_total_length - MIN_NFLX_CB_SIZE;
ws_debug("event");
break;
case NFLX_BLOCK_TYPE_SKIP:
/*
* The fixed-length portion is MIN_NFLX_CB_SIZE bytes plus a
* 32-bit value.
*
* Make sure we have that much data in the block.
*/
if (bh->block_total_length < MIN_NFLX_CB_SIZE + (guint32)sizeof(guint32)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of a Netflix skip CB is too small (< %u)",
bh->block_total_length,
MIN_NFLX_CB_SIZE + (guint32)sizeof(guint32));
return FALSE;
}
if (!wtap_read_bytes(fh, &skipped, sizeof(guint32), err, err_info)) {
ws_debug("Failed to read skipped");
return FALSE;
}
wblock->rec->presence_flags = 0;
wblock->rec->rec_header.custom_block_header.length = 4;
wblock->rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.type = BBLOG_TYPE_SKIPPED_BLOCK;
wblock->rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.skipped = GUINT32_FROM_LE(skipped);
wblock->internal = FALSE;
opt_cont_buf_len = bh->block_total_length - MIN_NFLX_CB_SIZE - sizeof(guint32);
ws_debug("skipped: %u", wblock->rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.skipped);
break;
default:
ws_debug("Unknown type %u", type);
return FALSE;
}
/* Options */
if (!pcapng_process_options(fh, wblock, section_info, opt_cont_buf_len,
NULL, OPT_LITTLE_ENDIAN, err, err_info))
return FALSE;
return TRUE;
}
static gboolean
pcapng_handle_generic_custom_block(FILE_T fh, pcapng_block_header_t *bh,
guint32 pen, wtapng_block_t *wblock,
int *err, gchar **err_info)
{
guint to_read;
ws_debug("unknown pen %u", pen);
if (bh->block_total_length % 4) {
to_read = bh->block_total_length + 4 - (bh->block_total_length % 4);
} else {
to_read = bh->block_total_length;
}
to_read -= MIN_CB_SIZE;
wblock->rec->rec_type = REC_TYPE_CUSTOM_BLOCK;
wblock->rec->presence_flags = 0;
wblock->rec->rec_header.custom_block_header.length = bh->block_total_length - MIN_CB_SIZE;
wblock->rec->rec_header.custom_block_header.pen = pen;
wblock->rec->rec_header.custom_block_header.copy_allowed = (bh->block_type == BLOCK_TYPE_CB_COPY);
if (!wtap_read_packet_bytes(fh, wblock->frame_buffer, to_read, err, err_info)) {
return FALSE;
}
/*
* We return these to the caller in pcapng_read().
*/
wblock->internal = FALSE;
return TRUE;
}
static gboolean
pcapng_read_custom_block(FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
pcapng_custom_block_t cb;
guint32 pen;
/* Is this block long enough to be an CB? */
if (bh->block_total_length < MIN_CB_SIZE) {
/*
* No.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of a CB is too small (< %u)",
bh->block_total_length, MIN_CB_SIZE);
return FALSE;
}
wblock->block = wtap_block_create(WTAP_BLOCK_CUSTOM);
/* Custom block read fixed part */
if (!wtap_read_bytes(fh, &cb, sizeof cb, err, err_info)) {
ws_debug("failed to read pen");
return FALSE;
}
if (section_info->byte_swapped) {
pen = GUINT32_SWAP_LE_BE(cb.pen);
} else {
pen = cb.pen;
}
ws_debug("pen %u, custom data and option length %u", pen, bh->block_total_length - MIN_CB_SIZE);
switch (pen) {
case PEN_NFLX:
if (!pcapng_read_nflx_custom_block(fh, bh, section_info, wblock, err, err_info))
return FALSE;
break;
default:
if (!pcapng_handle_generic_custom_block(fh, bh, pen, wblock, err, err_info)) {
return FALSE;
}
break;
}
wblock->rec->block = wblock->block;
wblock->block = NULL;
wblock->internal = FALSE;
return TRUE;
}
static gboolean
pcapng_read_sysdig_event_block(FILE_T fh, pcapng_block_header_t *bh,
const section_info_t *section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
unsigned block_read;
guint16 cpu_id;
guint64 wire_ts;
guint64 ts;
guint64 thread_id;
guint32 event_len;
guint16 event_type;
guint32 nparams = 0;
guint min_event_size;
switch (bh->block_type) {
case BLOCK_TYPE_SYSDIG_EVENT_V2_LARGE:
case BLOCK_TYPE_SYSDIG_EVENT_V2:
min_event_size = MIN_SYSDIG_EVENT_V2_SIZE;
break;
default:
min_event_size = MIN_SYSDIG_EVENT_SIZE;
break;
}
if (bh->block_total_length < min_event_size) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of a Sysdig event block is too small (< %u)",
bh->block_total_length, min_event_size);
return FALSE;
}
wblock->rec->rec_type = REC_TYPE_SYSCALL;
wblock->rec->rec_header.syscall_header.record_type = bh->block_type;
wblock->rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN /*|WTAP_HAS_INTERFACE_ID */;
wblock->rec->tsprec = WTAP_TSPREC_NSEC;
if (!wtap_read_bytes(fh, &cpu_id, sizeof cpu_id, err, err_info)) {
ws_debug("failed to read sysdig event cpu id");
return FALSE;
}
if (!wtap_read_bytes(fh, &wire_ts, sizeof wire_ts, err, err_info)) {
ws_debug("failed to read sysdig event timestamp");
return FALSE;
}
if (!wtap_read_bytes(fh, &thread_id, sizeof thread_id, err, err_info)) {
ws_debug("failed to read sysdig event thread id");
return FALSE;
}
if (!wtap_read_bytes(fh, &event_len, sizeof event_len, err, err_info)) {
ws_debug("failed to read sysdig event length");
return FALSE;
}
if (!wtap_read_bytes(fh, &event_type, sizeof event_type, err, err_info)) {
ws_debug("failed to read sysdig event type");
return FALSE;
}
if (bh->block_type == BLOCK_TYPE_SYSDIG_EVENT_V2 || bh->block_type == BLOCK_TYPE_SYSDIG_EVENT_V2_LARGE) {
if (!wtap_read_bytes(fh, &nparams, sizeof nparams, err, err_info)) {
ws_debug("failed to read sysdig number of parameters");
return FALSE;
}
}
wblock->rec->rec_header.syscall_header.byte_order = G_BYTE_ORDER;
/* XXX Use Gxxx_FROM_LE macros instead? */
if (section_info->byte_swapped) {
wblock->rec->rec_header.syscall_header.byte_order =
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
G_BIG_ENDIAN;
#else
G_LITTLE_ENDIAN;
#endif
wblock->rec->rec_header.syscall_header.cpu_id = GUINT16_SWAP_LE_BE(cpu_id);
ts = GUINT64_SWAP_LE_BE(wire_ts);
wblock->rec->rec_header.syscall_header.thread_id = GUINT64_SWAP_LE_BE(thread_id);
wblock->rec->rec_header.syscall_header.event_len = GUINT32_SWAP_LE_BE(event_len);
wblock->rec->rec_header.syscall_header.event_type = GUINT16_SWAP_LE_BE(event_type);
wblock->rec->rec_header.syscall_header.nparams = GUINT32_SWAP_LE_BE(nparams);
} else {
wblock->rec->rec_header.syscall_header.cpu_id = cpu_id;
ts = wire_ts;
wblock->rec->rec_header.syscall_header.thread_id = thread_id;
wblock->rec->rec_header.syscall_header.event_len = event_len;
wblock->rec->rec_header.syscall_header.event_type = event_type;
wblock->rec->rec_header.syscall_header.nparams = nparams;
}
wblock->rec->ts.secs = (time_t) (ts / 1000000000);
wblock->rec->ts.nsecs = (int) (ts % 1000000000);
block_read = bh->block_total_length - min_event_size;
wblock->rec->rec_header.syscall_header.event_filelen = block_read;
/* "Sysdig Event Block" read event data */
if (!wtap_read_packet_bytes(fh, wblock->frame_buffer,
block_read, err, err_info))
return FALSE;
/* XXX Read comment? */
/*
* We return these to the caller in pcapng_read().
*/
wblock->internal = FALSE;
return TRUE;
}
static gboolean
pcapng_read_systemd_journal_export_block(wtap *wth, FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn _U_, wtapng_block_t *wblock, int *err, gchar **err_info)
{
guint32 entry_length;
guint64 rt_ts;
gboolean have_ts = FALSE;
if (bh->block_total_length < MIN_SYSTEMD_JOURNAL_EXPORT_BLOCK_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of a systemd journal export block is too small (< %u)",
bh->block_total_length, MIN_SYSTEMD_JOURNAL_EXPORT_BLOCK_SIZE);
return FALSE;
}
entry_length = bh->block_total_length - MIN_BLOCK_SIZE;
/* Includes padding bytes. */
if (!wtap_read_packet_bytes(fh, wblock->frame_buffer,
entry_length, err, err_info)) {
return FALSE;
}
/*
* We don't have memmem available everywhere, so we get to add space for
* a trailing \0 for strstr below.
*/
ws_buffer_assure_space(wblock->frame_buffer, entry_length+1);
gchar *buf_ptr = (gchar *) ws_buffer_start_ptr(wblock->frame_buffer);
while (entry_length > 0 && buf_ptr[entry_length-1] == '\0') {
entry_length--;
}
if (entry_length < MIN_SYSTEMD_JOURNAL_EXPORT_ENTRY_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: entry length %u is too small (< %u)",
bh->block_total_length, MIN_SYSTEMD_JOURNAL_EXPORT_ENTRY_SIZE);
return FALSE;
}
ws_debug("entry_length %u", entry_length);
size_t rt_ts_len = strlen(SDJ__REALTIME_TIMESTAMP);
buf_ptr[entry_length] = '\0';
char *ts_pos = strstr(buf_ptr, SDJ__REALTIME_TIMESTAMP);
if (!ts_pos) {
ws_debug("no timestamp");
} else if (ts_pos+rt_ts_len >= (char *) buf_ptr+entry_length) {
ws_debug("timestamp past end of buffer");
} else {
const char *ts_end;
have_ts = ws_strtou64(ts_pos+rt_ts_len, &ts_end, &rt_ts);
if (!have_ts) {
ws_debug("invalid timestamp");
}
}
wblock->rec->rec_type = REC_TYPE_SYSTEMD_JOURNAL_EXPORT;
wblock->rec->rec_header.systemd_journal_export_header.record_len = entry_length;
wblock->rec->presence_flags = WTAP_HAS_CAP_LEN;
if (have_ts) {
wblock->rec->presence_flags |= WTAP_HAS_TS;
wblock->rec->tsprec = WTAP_TSPREC_USEC;
wblock->rec->ts.secs = (time_t) (rt_ts / 1000000);
wblock->rec->ts.nsecs = (rt_ts % 1000000) * 1000;
}
/*
* We return these to the caller in pcapng_read().
*/
wblock->internal = FALSE;
if (wth->file_encap == WTAP_ENCAP_NONE) {
/*
* Nothing (most notably an IDB) has set a file encap at this point.
* Do so here.
* XXX Should we set WTAP_ENCAP_SYSTEMD_JOURNAL if appropriate?
*/
wth->file_encap = WTAP_ENCAP_PER_PACKET;
}
return TRUE;
}
static gboolean
pcapng_read_unknown_block(FILE_T fh, pcapng_block_header_t *bh,
#ifdef HAVE_PLUGINS
const section_info_t *section_info,
#else
const section_info_t *section_info _U_,
#endif
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
guint32 block_read;
#ifdef HAVE_PLUGINS
block_handler *handler;
#endif
if (bh->block_total_length < MIN_BLOCK_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u of an unknown block type is less than the minimum block size %u",
bh->block_total_length, MIN_BLOCK_SIZE);
return FALSE;
}
block_read = bh->block_total_length - MIN_BLOCK_SIZE;
#ifdef HAVE_PLUGINS
/*
* Do we have a handler for this block type?
*/
if (block_handlers != NULL &&
(handler = (block_handler *)g_hash_table_lookup(block_handlers,
GUINT_TO_POINTER(bh->block_type))) != NULL) {
/* Yes - call it to read this block type. */
if (!handler->reader(fh, block_read, section_info->byte_swapped, wblock,
err, err_info))
return FALSE;
} else
#endif
{
/* No. Skip over this unknown block. */
if (!wtap_read_bytes(fh, NULL, block_read, err, err_info)) {
return FALSE;
}
/*
* We're skipping this, so we won't return these to the caller
* in pcapng_read().
*/
wblock->internal = TRUE;
}
return TRUE;
}
static gboolean
pcapng_read_and_check_block_trailer(FILE_T fh, pcapng_block_header_t *bh,
section_info_t *section_info,
int *err, gchar **err_info)
{
guint32 block_total_length;
/* sanity check: first and second block lengths must match */
if (!wtap_read_bytes(fh, &block_total_length, sizeof block_total_length,
err, err_info)) {
ws_debug("couldn't read second block length");
return FALSE;
}
if (section_info->byte_swapped)
block_total_length = GUINT32_SWAP_LE_BE(block_total_length);
/*
* According to the pcapng spec, this should equal the block total
* length value at the beginning of the block, which MUST (in the
* IANA sense) be a multiple of 4.
*
* We round the value at the beginning of the block to a multiple
* of 4, so do so with this value as well. This *does* mean that
* the two values, if they're not both multiples of 4, can differ
* and this code won't detect that, but we're already not detecting
* non-multiple-of-4 total lengths.
*/
block_total_length = ROUND_TO_4BYTE(block_total_length);
if (block_total_length != bh->block_total_length) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block lengths (first %u and second %u) don't match",
bh->block_total_length, block_total_length);
return FALSE;
}
return TRUE;
}
static gboolean
pcapng_read_block(wtap *wth, FILE_T fh, pcapng_t *pn,
section_info_t *section_info,
section_info_t *new_section_info,
wtapng_block_t *wblock,
int *err, gchar **err_info)
{
block_return_val ret;
pcapng_block_header_t bh;
wblock->block = NULL;
/* Try to read the (next) block header */
if (!wtap_read_bytes_or_eof(fh, &bh, sizeof bh, err, err_info)) {
ws_debug("wtap_read_bytes_or_eof() failed, err = %d.", *err);
return FALSE;
}
/*
* SHBs have to be treated differently from other blocks, because
* the byte order of the fields in the block can only be determined
* by looking at the byte-order magic number inside the block, not
* by using the byte order of the section to which it belongs, as
* it is the block that *defines* the byte order of the section to
* which it belongs.
*/
if (bh.block_type == BLOCK_TYPE_SHB) {
/*
* BLOCK_TYPE_SHB has the same value regardless of byte order,
* so we don't need to byte-swap it.
*
* We *might* need to byte-swap the total length, but we
* can't determine whether we do until we look inside the
* block and find the byte-order magic number, so we rely
* on pcapng_read_section_header_block() to do that and
* to swap the total length (as it needs to get the total
* length in the right byte order in order to read the
* entire block).
*/
wblock->type = bh.block_type;
ws_debug("block_type BLOCK_TYPE_SHB (0x%08x)", bh.block_type);
/*
* Fill in the section_info_t passed to us for use when
* there's a new SHB; don't overwrite the existing SHB,
* if there is one.
*/
ret = pcapng_read_section_header_block(fh, &bh, new_section_info,
wblock, err, err_info);
if (ret != PCAPNG_BLOCK_OK) {
return FALSE;
}
/*
* This is the current section; use its byte order, not that
* of the section pointed to by section_info (which could be
* null).
*/
section_info = new_section_info;
} else {
/*
* Not an SHB.
*/
if (section_info->byte_swapped) {
bh.block_type = GUINT32_SWAP_LE_BE(bh.block_type);
bh.block_total_length = GUINT32_SWAP_LE_BE(bh.block_total_length);
}
/*
* Add padding bytes to the block total length.
* (The "block total length" fields of some example files
* don't contain the packet data padding bytes!)
*
* For all block types currently defined in the pcapng
* specification, the portion of the block that precedes
* the options is, if necessary, padded to be a multiple
* of 4 octets, the header of an option is 4 octets long,
* and the value of an option is also padded to be a
* multiple of 4 octets, so the total length of a block
* is always a multiple of 4 octets.
*
* If you have defined a block where that is not true, you
* have violated the pcapng specification - hwere it says
* that "[The value fo the Block Total Length] MUST be a
* multiple of 4.", with MUST as described in BCP 14 (RFC 2119/
* RFC 8174).
*
* Therefore, if adjusting the block total length causes the
* code to read your block type not to work, that's your
* problem. It's bad enough that some blocks were written
* out with the block total length not including the padding.
* (Please note that libpcap is less forgiving that we are;
* it reports an error if the block total length isn't a
* multiple of 4.)
*/
bh.block_total_length = ROUND_TO_4BYTE(bh.block_total_length);
wblock->type = bh.block_type;
ws_noisy("block_type 0x%08x", bh.block_type);
/* Don't try to allocate memory for a huge number of options, as
that might fail and, even if it succeeds, it might not leave
any address space or memory+backing store for anything else.
We do that by imposing a maximum block size of MAX_BLOCK_SIZE. */
if (bh.block_total_length > MAX_BLOCK_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pcapng: total block length %u is too large (> %u)",
bh.block_total_length, MAX_BLOCK_SIZE);
return FALSE;
}
/*
* ***DO NOT*** add any items to this table that are not
* standardized block types in the current pcapng spec at
*
* https://pcapng.github.io/pcapng/draft-ietf-opsawg-pcapng.html
*
* All block types in this switch statement here must be
* listed there as standardized block types, ideally with
* a description.
*/
switch (bh.block_type) {
case(BLOCK_TYPE_IDB):
if (!pcapng_read_if_descr_block(wth, fh, &bh, section_info, wblock, err, err_info))
return FALSE;
break;
case(BLOCK_TYPE_PB):
if (!pcapng_read_packet_block(fh, &bh, section_info, wblock, err, err_info, FALSE))
return FALSE;
break;
case(BLOCK_TYPE_SPB):
if (!pcapng_read_simple_packet_block(fh, &bh, section_info, wblock, err, err_info))
return FALSE;
break;
case(BLOCK_TYPE_EPB):
if (!pcapng_read_packet_block(fh, &bh, section_info, wblock, err, err_info, TRUE))
return FALSE;
break;
case(BLOCK_TYPE_NRB):
if (!pcapng_read_name_resolution_block(fh, &bh, section_info, wblock, err, err_info))
return FALSE;
break;
case(BLOCK_TYPE_ISB):
if (!pcapng_read_interface_statistics_block(fh, &bh, section_info, wblock, err, err_info))
return FALSE;
break;
case(BLOCK_TYPE_DSB):
if (!pcapng_read_decryption_secrets_block(fh, &bh, section_info, wblock, err, err_info))
return FALSE;
break;
case BLOCK_TYPE_SYSDIG_MI:
case BLOCK_TYPE_SYSDIG_PL_V1:
case BLOCK_TYPE_SYSDIG_FDL_V1:
case BLOCK_TYPE_SYSDIG_IL_V1:
case BLOCK_TYPE_SYSDIG_UL_V1:
case BLOCK_TYPE_SYSDIG_PL_V2:
case BLOCK_TYPE_SYSDIG_PL_V3:
case BLOCK_TYPE_SYSDIG_PL_V4:
case BLOCK_TYPE_SYSDIG_PL_V5:
case BLOCK_TYPE_SYSDIG_PL_V6:
case BLOCK_TYPE_SYSDIG_PL_V7:
case BLOCK_TYPE_SYSDIG_PL_V8:
case BLOCK_TYPE_SYSDIG_PL_V9:
case BLOCK_TYPE_SYSDIG_FDL_V2:
case BLOCK_TYPE_SYSDIG_IL_V2:
case BLOCK_TYPE_SYSDIG_UL_V2:
if (!pcapng_read_sysdig_meta_event_block(fh, &bh, wblock, err, err_info))
return FALSE;
break;
case(BLOCK_TYPE_CB_COPY):
case(BLOCK_TYPE_CB_NO_COPY):
if (!pcapng_read_custom_block(fh, &bh, section_info, wblock, err, err_info))
return FALSE;
break;
case(BLOCK_TYPE_SYSDIG_EVENT):
case(BLOCK_TYPE_SYSDIG_EVENT_V2):
case(BLOCK_TYPE_SYSDIG_EVENT_V2_LARGE):
/* case(BLOCK_TYPE_SYSDIG_EVF): */
if (!pcapng_read_sysdig_event_block(fh, &bh, section_info, wblock, err, err_info))
return FALSE;
break;
case(BLOCK_TYPE_SYSTEMD_JOURNAL_EXPORT):
if (!pcapng_read_systemd_journal_export_block(wth, fh, &bh, pn, wblock, err, err_info))
return FALSE;
break;
default:
ws_debug("Unknown block_type: 0x%08x (block ignored), block total length %d",
bh.block_type, bh.block_total_length);
if (!pcapng_read_unknown_block(fh, &bh, section_info, wblock, err, err_info))
return FALSE;
break;
}
}
/*
* Read and check the block trailer.
*/
if (!pcapng_read_and_check_block_trailer(fh, &bh, section_info, err, err_info)) {
/* Not readable or not valid. */
return FALSE;
}
return TRUE;
}
/* Process an IDB that we've just read. The contents of wblock are copied as needed. */
static void
pcapng_process_idb(wtap *wth, section_info_t *section_info,
wtapng_block_t *wblock)
{
wtap_block_t int_data = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO);
interface_info_t iface_info;
wtapng_if_descr_mandatory_t *if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data),
*wblock_if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(wblock->block);
guint8 if_fcslen;
wtap_block_copy(int_data, wblock->block);
/* XXX if_tsoffset; opt 14 A 64 bits integer value that specifies an offset (in seconds)...*/
/* Interface statistics */
if_descr_mand->num_stat_entries = 0;
if_descr_mand->interface_statistics = NULL;
wtap_add_idb(wth, int_data);
iface_info.wtap_encap = wblock_if_descr_mand->wtap_encap;
iface_info.snap_len = wblock_if_descr_mand->snap_len;
iface_info.time_units_per_second = wblock_if_descr_mand->time_units_per_second;
iface_info.tsprecision = wblock_if_descr_mand->tsprecision;
/*
* Did we get an FCS length option?
*/
if (wtap_block_get_uint8_option_value(wblock->block, OPT_IDB_FCSLEN,
&if_fcslen) == WTAP_OPTTYPE_SUCCESS) {
/*
* Yes.
*/
iface_info.fcslen = if_fcslen;
} else {
/*
* No. Mark the FCS length as unknown.
*/
iface_info.fcslen = -1;
}
/*
* Did we get a time stamp offset option?
*/
if (wtap_block_get_int64_option_value(wblock->block, OPT_IDB_TSOFFSET,
&iface_info.tsoffset) == WTAP_OPTTYPE_SUCCESS) {
/*
* Yes.
*
* Remove the option, as the time stamps we provide will be
* absolute time stamps, with the offset added in, so it will
* appear as if there were no such option.
*/
wtap_block_remove_option(wblock->block, OPT_IDB_TSOFFSET);
} else {
/*
* No. Default to 0, meahing that time stamps in the file are
* absolute time stamps.
*/
iface_info.tsoffset = 0;
}
g_array_append_val(section_info->interfaces, iface_info);
}
/* Process an NRB that we have just read. */
static void
pcapng_process_nrb(wtap *wth, wtapng_block_t *wblock)
{
wtapng_process_nrb(wth, wblock->block);
if (wth->nrbs == NULL) {
wth->nrbs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
}
/* Store NRB such that it can be saved by the dumper. */
g_array_append_val(wth->nrbs, wblock->block);
}
/* Process a DSB that we have just read. */
static void
pcapng_process_dsb(wtap *wth, wtapng_block_t *wblock)
{
wtapng_process_dsb(wth, wblock->block);
/* Store DSB such that it can be saved by the dumper. */
g_array_append_val(wth->dsbs, wblock->block);
}
/* Process a Sysdig meta event block that we have just read. */
static void
pcapng_process_sysdig_mev(wtap *wth, wtapng_block_t *wblock)
{
// XXX add wtapng_process_sysdig_meb(wth, wblock->block);
/* Store meta event such that it can be saved by the dumper. */
g_array_append_val(wth->sysdig_meta_events, wblock->block);
}
static void
pcapng_process_internal_block(wtap *wth, pcapng_t *pcapng, section_info_t *current_section, section_info_t new_section, wtapng_block_t *wblock, const gint64 *data_offset)
{
wtap_block_t wtapng_if_descr;
wtap_block_t if_stats;
wtapng_if_stats_mandatory_t *if_stats_mand_block, *if_stats_mand;
wtapng_if_descr_mandatory_t *wtapng_if_descr_mand;
switch (wblock->type) {
case(BLOCK_TYPE_SHB):
ws_debug("another section header block");
/*
* Add this SHB to the table of SHBs.
*/
g_array_append_val(wth->shb_hdrs, wblock->block);
/*
* Update the current section number, and add
* the updated section_info_t to the array of
* section_info_t's for this file.
*/
pcapng->current_section_number++;
new_section.interfaces = g_array_new(FALSE, FALSE, sizeof(interface_info_t));
new_section.shb_off = *data_offset;
g_array_append_val(pcapng->sections, new_section);
break;
case(BLOCK_TYPE_IDB):
/* A new interface */
ws_debug("block type BLOCK_TYPE_IDB");
pcapng_process_idb(wth, current_section, wblock);
wtap_block_unref(wblock->block);
break;
case(BLOCK_TYPE_DSB):
/* Decryption secrets. */
ws_debug("block type BLOCK_TYPE_DSB");
pcapng_process_dsb(wth, wblock);
/* Do not free wblock->block, it is consumed by pcapng_process_dsb */
break;
case(BLOCK_TYPE_NRB):
/* More name resolution entries */
ws_debug("block type BLOCK_TYPE_NRB");
pcapng_process_nrb(wth, wblock);
/* Do not free wblock->block, it is consumed by pcapng_process_nrb */
break;
case(BLOCK_TYPE_ISB):
/*
* Another interface statistics report
*
* XXX - given that they're reports, we should be
* supplying them in read calls, and displaying them
* in the "packet" list, so you can see what the
* statistics were *at the time when the report was
* made*.
*
* The statistics from the *last* ISB could be displayed
* in the summary, but if there are packets after the
* last ISB, that could be misleading.
*
* If we only display them if that ISB has an isb_endtime
* option, which *should* only appear when capturing ended
* on that interface (so there should be no more packet
* blocks or ISBs for that interface after that point,
* that would be the best way of showing "summary"
* statistics.
*/
ws_debug("block type BLOCK_TYPE_ISB");
if_stats_mand_block = (wtapng_if_stats_mandatory_t*)wtap_block_get_mandatory_data(wblock->block);
if (wth->interface_data->len <= if_stats_mand_block->interface_id) {
ws_debug("BLOCK_TYPE_ISB wblock.if_stats.interface_id %u >= number_of_interfaces",
if_stats_mand_block->interface_id);
} else {
/* Get the interface description */
wtapng_if_descr = g_array_index(wth->interface_data, wtap_block_t, if_stats_mand_block->interface_id);
wtapng_if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(wtapng_if_descr);
if (wtapng_if_descr_mand->num_stat_entries == 0) {
/* First ISB found, no previous entry */
ws_debug("block type BLOCK_TYPE_ISB. First ISB found, no previous entry");
wtapng_if_descr_mand->interface_statistics = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
}
if_stats = wtap_block_create(WTAP_BLOCK_IF_STATISTICS);
if_stats_mand = (wtapng_if_stats_mandatory_t*)wtap_block_get_mandatory_data(if_stats);
if_stats_mand->interface_id = if_stats_mand_block->interface_id;
if_stats_mand->ts_high = if_stats_mand_block->ts_high;
if_stats_mand->ts_low = if_stats_mand_block->ts_low;
wtap_block_copy(if_stats, wblock->block);
g_array_append_val(wtapng_if_descr_mand->interface_statistics, if_stats);
wtapng_if_descr_mand->num_stat_entries++;
}
wtap_block_unref(wblock->block);
break;
case BLOCK_TYPE_SYSDIG_MI:
case BLOCK_TYPE_SYSDIG_PL_V1:
case BLOCK_TYPE_SYSDIG_FDL_V1:
case BLOCK_TYPE_SYSDIG_IL_V1:
case BLOCK_TYPE_SYSDIG_UL_V1:
case BLOCK_TYPE_SYSDIG_PL_V2:
case BLOCK_TYPE_SYSDIG_PL_V3:
case BLOCK_TYPE_SYSDIG_PL_V4:
case BLOCK_TYPE_SYSDIG_PL_V5:
case BLOCK_TYPE_SYSDIG_PL_V6:
case BLOCK_TYPE_SYSDIG_PL_V7:
case BLOCK_TYPE_SYSDIG_PL_V8:
case BLOCK_TYPE_SYSDIG_PL_V9:
case BLOCK_TYPE_SYSDIG_FDL_V2:
case BLOCK_TYPE_SYSDIG_IL_V2:
case BLOCK_TYPE_SYSDIG_UL_V2:
/* Decryption secrets. */
ws_debug("block type Sysdig meta event");
pcapng_process_sysdig_mev(wth, wblock);
/* Do not free wblock->block, it is consumed by pcapng_process_sysdig_meb */
break;
default:
/* XXX - improve handling of "unknown" blocks */
ws_debug("Unknown block type 0x%08x", wblock->type);
break;
}
}
/* classic wtap: open capture file */
wtap_open_return_val
pcapng_open(wtap *wth, int *err, gchar **err_info)
{
wtapng_block_t wblock;
pcapng_t *pcapng;
pcapng_block_header_t bh;
gint64 saved_offset;
section_info_t first_section, new_section, *current_section;
ws_debug("opening file");
/*
* Read first block.
*
* First, try to read the block header.
*/
if (!wtap_read_bytes_or_eof(wth->fh, &bh, sizeof bh, err, err_info)) {
ws_debug("wtap_read_bytes_or_eof() failed, err = %d.", *err);
if (*err == 0 || *err == WTAP_ERR_SHORT_READ) {
/*
* Short read or EOF.
*
* We're reading this as part of an open, so
* the file is too short to be a pcapng file.
*/
*err = 0;
g_free(*err_info);
*err_info = NULL;
return WTAP_OPEN_NOT_MINE;
}
return WTAP_OPEN_ERROR;
}
/*
* If this is a pcapng file, the first block must be a
* Section Header Block.
*/
if (bh.block_type != BLOCK_TYPE_SHB) {
/*
* Not an SHB, so this isn't a pcapng file.
*
* XXX - check for damage from transferring a file
* between Windows and UN*X as text rather than
* binary data?
*/
ws_debug("first block type 0x%08x not SHB", bh.block_type);
return WTAP_OPEN_NOT_MINE;
}
ws_debug("got an SHB");
/*
* Now try to read the block body, filling in the section_info_t
* for the first section.
*/
wblock.type = bh.block_type;
wblock.block = NULL;
/* we don't expect any packet blocks yet */
wblock.frame_buffer = NULL;
wblock.rec = NULL;
switch (pcapng_read_section_header_block(wth->fh, &bh, &first_section,
&wblock, err, err_info)) {
case PCAPNG_BLOCK_OK:
/* No problem */
break;
case PCAPNG_BLOCK_NOT_SHB:
/* This doesn't look like an SHB, so this isn't a pcapng file. */
wtap_block_unref(wblock.block);
*err = 0;
g_free(*err_info);
*err_info = NULL;
return WTAP_OPEN_NOT_MINE;
case PCAPNG_BLOCK_ERROR:
wtap_block_unref(wblock.block);
if (*err == WTAP_ERR_SHORT_READ) {
/*
* Short read.
*
* We're reading this as part of an open, so
* the file is too short to be a pcapng file.
*/
*err = 0;
g_free(*err_info);
*err_info = NULL;
return WTAP_OPEN_NOT_MINE;
}
/* An I/O error. */
return WTAP_OPEN_ERROR;
}
/*
* Read and check the block trailer.
*/
if (!pcapng_read_and_check_block_trailer(wth->fh, &bh, &first_section, err, err_info)) {
/* Not readable or not valid. */
wtap_block_unref(wblock.block);
return WTAP_OPEN_ERROR;
}
/*
* At this point, we've decided this is a pcapng file, not
* some other type of file, so we can't return WTAP_OPEN_NOT_MINE
* past this point.
*
* Copy the SHB that we just read to the first entry in the table of
* SHBs for this file.
*/
wtap_block_copy(g_array_index(wth->shb_hdrs, wtap_block_t, 0), wblock.block);
wtap_block_unref(wblock.block);
wblock.block = NULL;
wth->file_encap = WTAP_ENCAP_NONE;
wth->snapshot_length = 0;
wth->file_tsprec = WTAP_TSPREC_UNKNOWN;
pcapng = g_new(pcapng_t, 1);
wth->priv = (void *)pcapng;
/*
* We're currently processing the first section; as this is written
* in C, that's section 0. :-)
*/
pcapng->current_section_number = 0;
/*
* Create the array of interfaces for the first section.
*/
first_section.interfaces = g_array_new(FALSE, FALSE, sizeof(interface_info_t));
/*
* The first section is at the very beginning of the file.
*/
first_section.shb_off = 0;
/*
* Allocate the sections table with space reserved for the first
* section, and add that section.
*/
pcapng->sections = g_array_sized_new(FALSE, FALSE, sizeof(section_info_t), 1);
g_array_append_val(pcapng->sections, first_section);
wth->subtype_read = pcapng_read;
wth->subtype_seek_read = pcapng_seek_read;
wth->subtype_close = pcapng_close;
wth->file_type_subtype = pcapng_file_type_subtype;
/* Always initialize the lists of Decryption Secret Blocks, Name
* Resolution Blocks, and Sysdig meta event blocks such that a
* wtap_dumper can refer to them right after opening the capture
* file. */
wth->dsbs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
wth->nrbs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
wth->sysdig_meta_events = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
/* Most other capture types (such as pcap) support a single link-layer
* type, indicated in the header, and don't support WTAP_ENCAP_PER_PACKET.
* Most programs that write such capture files want to know the link-layer
* type when initially opening the destination file, and (unlike Wireshark)
* don't want to read the entire source file to find all the link-layer
* types before writing (particularly if reading from a pipe or FIFO.)
*
* In support of this, read all the internally-processed, non packet
* blocks that appear before the first packet block (EPB or SPB).
*
* Note that such programs will still have issues when trying to read
* a pcapng that has a new link-layer type in an IDB in the middle of
* the file, as they will discover in the middle that no, they can't
* successfully write the output file as desired.
*/
while (1) {
/* peek at next block */
/* Try to read the (next) block header */
saved_offset = file_tell(wth->fh);
if (!wtap_read_bytes_or_eof(wth->fh, &bh, sizeof bh, err, err_info)) {
if (*err == 0) {
/* EOF */
ws_debug("No more blocks available...");
break;
}
ws_debug("Check for more initial blocks, wtap_read_bytes_or_eof() failed, err = %d.",
*err);
return WTAP_OPEN_ERROR;
}
/* go back to where we were */
file_seek(wth->fh, saved_offset, SEEK_SET, err);
/*
* Get a pointer to the current section's section_info_t.
*/
current_section = &g_array_index(pcapng->sections, section_info_t,
pcapng->current_section_number);
if (current_section->byte_swapped) {
bh.block_type = GUINT32_SWAP_LE_BE(bh.block_type);
}
ws_debug("Check for more initial internal blocks, block_type 0x%08x",
bh.block_type);
if (!get_block_type_internal(bh.block_type)) {
break; /* Next block has to be returned in pcap_read */
}
/* Note that some custom block types, unlike packet blocks,
* don't need to be preceded by an IDB and so theoretically
* we could skip past them here. However, then there's no good
* way to both later return those blocks in pcap_read() and
* ensure that we don't read and process the IDBs (and other
* internal block types) a second time.
*
* pcapng_read_systemd_journal_export_block() sets the file level
* link-layer type if it's still UNKNOWN. We could do the same here
* for it and possibly other types based on block type, even without
* reading them.
*/
if (!pcapng_read_block(wth, wth->fh, pcapng, current_section,
&new_section, &wblock, err, err_info)) {
wtap_block_unref(wblock.block);
if (*err == 0) {
ws_debug("No more initial blocks available...");
break;
} else {
ws_debug("couldn't read block");
return WTAP_OPEN_ERROR;
}
}
pcapng_process_internal_block(wth, pcapng, current_section, new_section, &wblock, &saved_offset);
ws_debug("Read IDB number_of_interfaces %u, wtap_encap %i",
wth->interface_data->len, wth->file_encap);
}
return WTAP_OPEN_MINE;
}
/* classic wtap: read packet */
static gboolean
pcapng_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err,
gchar **err_info, gint64 *data_offset)
{
pcapng_t *pcapng = (pcapng_t *)wth->priv;
section_info_t *current_section, new_section;
wtapng_block_t wblock;
wblock.frame_buffer = buf;
wblock.rec = rec;
/* read next block */
while (1) {
*data_offset = file_tell(wth->fh);
ws_noisy("data_offset is %" PRId64, *data_offset);
/*
* Get the section_info_t for the current section.
*/
current_section = &g_array_index(pcapng->sections, section_info_t,
pcapng->current_section_number);
/*
* Read the next block.
*/
if (!pcapng_read_block(wth, wth->fh, pcapng, current_section,
&new_section, &wblock, err, err_info)) {
ws_noisy("data_offset is finally %" PRId64, *data_offset);
ws_debug("couldn't read packet block");
wtap_block_unref(wblock.block);
return FALSE;
}
if (!wblock.internal) {
/*
* This is a block type we return to the caller to process.
*/
ws_noisy("rec_type %u", wblock.rec->rec_type);
break;
}
/*
* This is a block type we process internally, rather than
* returning it for the caller to process.
*/
pcapng_process_internal_block(wth, pcapng, current_section, new_section, &wblock, data_offset);
}
/*ws_debug("Read length: %u Packet length: %u", bytes_read, rec->rec_header.packet_header.caplen);*/
ws_noisy("data_offset is finally %" PRId64, *data_offset);
/* Provide the section number */
rec->presence_flags |= WTAP_HAS_SECTION_NUMBER;
rec->section_number = pcapng->current_section_number;
return TRUE;
}
/* classic wtap: seek to file position and read packet */
static gboolean
pcapng_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info)
{
pcapng_t *pcapng = (pcapng_t *)wth->priv;
section_info_t *section_info, new_section;
wtapng_block_t wblock;
/* seek to the right file position */
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) < 0) {
return FALSE; /* Seek error */
}
ws_noisy("reading at offset %" PRIu64, seek_off);
/*
* Find the section_info_t for the section in which this block
* appears.
*
* First, make sure we have at least one section; if we don't, that's
* an internal error.
*/
ws_assert(pcapng->sections->len >= 1);
/*
* Now scan backwards through the array to find the first section
* that begins at or before the offset of the block we're reading.
*
* Yes, that's O(n) in the number of blocks, but we're unlikely to
* have many blocks and pretty unlikely to have more than one.
*/
guint section_number = pcapng->sections->len - 1;
for (;;) {
section_info = &g_array_index(pcapng->sections, section_info_t,
section_number);
if (section_info->shb_off <= seek_off)
break;
/*
* If that's section 0, something's wrong; that section should
* have an offset of 0.
*/
ws_assert(section_number != 0);
section_number--;
}
wblock.frame_buffer = buf;
wblock.rec = rec;
/* read the block */
if (!pcapng_read_block(wth, wth->random_fh, pcapng, section_info,
&new_section, &wblock, err, err_info)) {
ws_debug("couldn't read packet block (err=%d).", *err);
wtap_block_unref(wblock.block);
return FALSE;
}
/* block must not be one we process internally rather than supplying */
if (wblock.internal) {
ws_debug("block type 0x%08x is not one we return",
wblock.type);
wtap_block_unref(wblock.block);
return FALSE;
}
wtap_block_unref(wblock.block);
/* Provide the section number */
rec->presence_flags |= WTAP_HAS_SECTION_NUMBER;
rec->section_number = section_number;
return TRUE;
}
/* classic wtap: close capture file */
static void
pcapng_close(wtap *wth)
{
pcapng_t *pcapng = (pcapng_t *)wth->priv;
ws_debug("closing file");
/*
* Free up the interfaces tables for all the sections.
*/
for (guint i = 0; i < pcapng->sections->len; i++) {
section_info_t *section_info = &g_array_index(pcapng->sections,
section_info_t, i);
g_array_free(section_info->interfaces, TRUE);
}
g_array_free(pcapng->sections, TRUE);
}
typedef guint32 (*compute_option_size_func)(wtap_block_t, guint, wtap_opttype_e, wtap_optval_t*);
typedef struct compute_options_size_t
{
guint32 size;
compute_option_size_func compute_option_size;
} compute_options_size_t;
static guint32 pcapng_compute_string_option_size(wtap_optval_t *optval)
{
guint32 size = 0, pad;
size = (guint32)strlen(optval->stringval) & 0xffff;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
size += pad;
return size;
}
#if 0
static guint32 pcapng_compute_bytes_option_size(wtap_optval_t *optval)
{
guint32 size = 0, pad;
size = (guint32)g_bytes_get_size(optval->byteval) & 0xffff;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
size += pad;
return size;
}
#endif
static guint32 pcapng_compute_if_filter_option_size(wtap_optval_t *optval)
{
if_filter_opt_t* filter = &optval->if_filterval;
guint32 size;
guint32 pad;
if (filter->type == if_filter_pcap) {
size = (guint32)(strlen(filter->data.filter_str) + 1) & 0xffff;
} else if (filter->type == if_filter_bpf) {
size = (guint32)((filter->data.bpf_prog.bpf_prog_len * 8) + 1) & 0xffff;
} else {
/* Unknown type; don't write it */
size = 0;
}
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
size += pad;
return size;
}
static guint32 pcapng_compute_custom_option_size(wtap_optval_t *optval)
{
size_t size, pad;
/* PEN */
size = sizeof(guint32);
switch (optval->custom_opt.pen) {
case PEN_NFLX:
/* NFLX type */
size += sizeof(guint32);
size += optval->custom_opt.data.nflx_data.custom_data_len;
break;
default:
size += optval->custom_opt.data.generic_data.custom_data_len;
break;
}
if (size > 65535) {
size = 65535;
}
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
size += pad;
return (guint32)size;
}
static guint32 pcapng_compute_packet_hash_option_size(wtap_optval_t *optval)
{
packet_hash_opt_t* hash = &optval->packet_hash;
guint32 size;
switch (hash->type) {
case OPT_HASH_CRC32:
size = 4;
break;
case OPT_HASH_MD5:
size = 16;
break;
case OPT_HASH_SHA1:
size = 20;
break;
case OPT_HASH_TOEPLITZ:
size = 4;
break;
default:
/* 2COMP and XOR size not defined in standard (yet) */
size = hash->hash_bytes->len;
break;
}
/* XXX - What if the size of the hash bytes doesn't match the
* expected size? We can:
* 1) Return 0, and omit it when writing
* 2) Return hash_bytes->len, and write it out exactly as we have it
* 3) Return the correct size here, and when writing err or possibly
* truncate.
*/
/* Account for the size of the algorithm type field. */
size += 1;
#if 0
/* compute_block_option_size() handles padding. */
uint32_t pad;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
size += pad;
#endif
return size;
}
static guint32 pcapng_compute_packet_verdict_option_size(wtap_optval_t *optval)
{
packet_verdict_opt_t* verdict = &optval->packet_verdictval;
guint32 size;
switch (verdict->type) {
case packet_verdict_hardware:
size = verdict->data.verdict_bytes->len;
break;
case packet_verdict_linux_ebpf_tc:
size = 8;
break;
case packet_verdict_linux_ebpf_xdp:
size = 8;
break;
default:
size = 0;
break;
}
/* Account for the type octet */
if (size) {
size += 1;
}
#if 0
/* compute_block_option_size() handles padding. */
uint32_t pad;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
size += pad;
#endif
return size;
}
static gboolean
compute_block_option_size(wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type, wtap_optval_t *optval, void *user_data)
{
compute_options_size_t* options_size = (compute_options_size_t*)user_data;
guint32 size = 0;
/*
* Process the option IDs that are the same for all block types here;
* call the block-type-specific compute_size function for others.
*/
switch(option_id)
{
case OPT_COMMENT:
size = pcapng_compute_string_option_size(optval);
break;
case OPT_CUSTOM_STR_COPY:
case OPT_CUSTOM_BIN_COPY:
size = pcapng_compute_custom_option_size(optval);
break;
case OPT_CUSTOM_STR_NO_COPY:
case OPT_CUSTOM_BIN_NO_COPY:
/*
* Do not count these, as they're not supposed to be copied to
* new files.
*
* XXX - what if we're writing out a file that's *not* based on
* another file, so that we're *not* copying it from that file?
*/
break;
default:
/* Block-type dependent; call the callback. */
size = (*options_size->compute_option_size)(block, option_id, option_type, optval);
break;
}
/*
* Are we writing this option?
*/
/*
* XXX: The option length field is 16 bits. If size > 65535 (how?
* was the block was obtained from some format other than pcapng?),
* are we going to silently omit the option (in which case we shouldn't
* add the size here), or err out when writing it (in which case
* it's probably fine to add the size or not?) Adding it here and
* then omitting it when writing, as some of the routines do, means
* creating a corrupt file.
*/
if (size != 0) {
/*
* Yes. Add the size of the option header to the size of the
* option data.
*/
options_size->size += 4;
/* Now add the size of the option value. */
options_size->size += size;
/* Add optional padding to 32 bits */
if ((size & 0x03) != 0)
{
options_size->size += 4 - (size & 0x03);
}
}
return TRUE; /* we always succeed */
}
static guint32
compute_options_size(wtap_block_t block, compute_option_size_func compute_option_size)
{
compute_options_size_t compute_options_size;
/*
* Compute the total size of all the options in the block.
* This always suceeds, so we don't check the return value.
*/
compute_options_size.size = 0;
compute_options_size.compute_option_size = compute_option_size;
wtap_block_foreach_option(block, compute_block_option_size, &compute_options_size);
/* Are we writing any options? */
if (compute_options_size.size != 0) {
/* Yes, add the size of the End-of-options tag. */
compute_options_size.size += 4;
}
return compute_options_size.size;
}
static guint32 compute_shb_option_size(wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t* optval)
{
guint32 size;
switch(option_id)
{
case OPT_SHB_HARDWARE:
case OPT_SHB_OS:
case OPT_SHB_USERAPPL:
size = pcapng_compute_string_option_size(optval);
break;
default:
/* Unknown options - size by datatype? */
size = 0;
break;
}
return size;
}
typedef gboolean (*write_option_func)(wtap_dumper *, wtap_block_t, guint, wtap_opttype_e, wtap_optval_t*, int*);
typedef struct write_options_t
{
wtap_dumper *wdh;
int *err;
write_option_func write_option;
}
write_options_t;
static gboolean pcapng_write_option_eofopt(wtap_dumper *wdh, int *err)
{
struct pcapng_option_header option_hdr;
/* Write end of options */
option_hdr.type = OPT_EOFOPT;
option_hdr.value_length = 0;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
return TRUE;
}
static gboolean pcapng_write_uint8_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
const guint32 zero_pad = 0;
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)1;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &optval->uint8val, 1, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &zero_pad, 3, err))
return FALSE;
return TRUE;
}
static gboolean pcapng_write_uint32_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)4;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &optval->uint32val, 4, err))
return FALSE;
return TRUE;
}
static gboolean pcapng_write_uint64_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)8;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &optval->uint64val, 8, err))
return FALSE;
return TRUE;
}
static gboolean pcapng_write_timestamp_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
guint32 high, low;
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)8;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
high = (guint32)(optval->uint64val >> 32);
low = (guint32)(optval->uint64val >> 0);
if (!wtap_dump_file_write(wdh, &high, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &low, 4, err))
return FALSE;
return TRUE;
}
static gboolean pcapng_write_string_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
size_t size = strlen(optval->stringval);
const guint32 zero_pad = 0;
guint32 pad;
if (size == 0)
return TRUE;
if (size > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return TRUE;
}
/* String options don't consider pad bytes part of the length */
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)size;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, optval->stringval, size, err))
return FALSE;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
/* write padding (if any) */
if (pad != 0) {
if (!wtap_dump_file_write(wdh, &zero_pad, pad, err))
return FALSE;
}
return TRUE;
}
#if 0
static gboolean pcapng_write_bytes_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
size_t size = g_bytes_get_size(optval->byteval);
const guint32 zero_pad = 0;
guint32 pad;
if (size == 0)
return TRUE;
if (size > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return TRUE;
}
/* Bytes options don't consider pad bytes part of the length */
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)size;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, optval->stringval, size, err))
return FALSE;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
/* write padding (if any) */
if (pad != 0) {
if (!wtap_dump_file_write(wdh, &zero_pad, pad, err))
return FALSE;
}
return TRUE;
}
static gboolean pcapng_write_ipv4_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)4;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &optval->ipv4val, 1, err))
return FALSE;
return TRUE;
}
static gboolean pcapng_write_ipv6_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)IPv6_ADDR_SIZE;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &optval->ipv6val.bytes, IPv6_ADDR_SIZE, err))
return FALSE;
return TRUE;
}
#endif
static gboolean pcapng_write_if_filter_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
if_filter_opt_t* filter = &optval->if_filterval;
guint32 size, pad;
guint8 filter_type;
size_t filter_data_len;
struct pcapng_option_header option_hdr;
const guint32 zero_pad = 0;
switch (filter->type) {
case if_filter_pcap:
filter_type = 0; /* pcap filter string */
filter_data_len = strlen(filter->data.filter_str);
if (filter_data_len > 65534) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return TRUE;
}
break;
case if_filter_bpf:
filter_type = 1; /* BPF filter program */
filter_data_len = filter->data.bpf_prog.bpf_prog_len*8;
if (filter_data_len > 65528) {
/*
* Too big to fit in the option. (The filter length
* must be a multiple of 8, as that's the length
* of a BPF instruction.) Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return TRUE;
}
break;
default:
/* Unknown filter type; don't write anything. */
return TRUE;
}
size = (guint32)(filter_data_len + 1);
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
option_hdr.type = option_id;
option_hdr.value_length = size;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
/* Write the filter type */
if (!wtap_dump_file_write(wdh, &filter_type, 1, err))
return FALSE;
switch (filter->type) {
case if_filter_pcap:
/* Write the filter string */
if (!wtap_dump_file_write(wdh, filter->data.filter_str, filter_data_len, err))
return FALSE;
break;
case if_filter_bpf:
if (!wtap_dump_file_write(wdh, filter->data.bpf_prog.bpf_prog, filter_data_len, err))
return FALSE;
break;
default:
ws_assert_not_reached();
return TRUE;
}
/* write padding (if any) */
if (pad != 0) {
if (!wtap_dump_file_write(wdh, &zero_pad, pad, err))
return FALSE;
}
return TRUE;
}
static gboolean pcapng_write_custom_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
struct pcapng_option_header option_hdr;
gsize pad;
gsize size;
const guint32 zero_pad = 0;
guint32 pen, type;
gboolean use_little_endian;
if ((option_id == OPT_CUSTOM_STR_NO_COPY) ||
(option_id == OPT_CUSTOM_BIN_NO_COPY))
return TRUE;
ws_debug("PEN %d", optval->custom_opt.pen);
switch (optval->custom_opt.pen) {
case PEN_NFLX:
size = sizeof(guint32) + sizeof(guint32) + optval->custom_opt.data.nflx_data.custom_data_len;
use_little_endian = optval->custom_opt.data.nflx_data.use_little_endian;
break;
default:
size = sizeof(guint32) + optval->custom_opt.data.generic_data.custom_data_len;
use_little_endian = false;
break;
}
ws_debug("use_little_endian %d", use_little_endian);
if (size > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return TRUE;
}
/* write option header */
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)size;
if (use_little_endian) {
option_hdr.type = GUINT16_TO_LE(option_hdr.type);
option_hdr.value_length = GUINT16_TO_LE(option_hdr.value_length);
}
if (!wtap_dump_file_write(wdh, &option_hdr, sizeof(struct pcapng_option_header), err))
return FALSE;
/* write PEN */
pen = optval->custom_opt.pen;
if (use_little_endian) {
pen = GUINT32_TO_LE(pen);
}
if (!wtap_dump_file_write(wdh, &pen, sizeof(guint32), err))
return FALSE;
switch (optval->custom_opt.pen) {
case PEN_NFLX:
/* write NFLX type */
type = GUINT32_TO_LE(optval->custom_opt.data.nflx_data.type);
ws_debug("type=%d", type);
if (!wtap_dump_file_write(wdh, &type, sizeof(guint32), err))
return FALSE;
/* write custom data */
if (!wtap_dump_file_write(wdh, optval->custom_opt.data.nflx_data.custom_data, optval->custom_opt.data.nflx_data.custom_data_len, err)) {
return FALSE;
}
break;
default:
/* write custom data */
if (!wtap_dump_file_write(wdh, optval->custom_opt.data.generic_data.custom_data, optval->custom_opt.data.generic_data.custom_data_len, err)) {
return FALSE;
}
break;
}
/* write padding (if any) */
if (size % 4 != 0) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
if (pad != 0) {
if (!wtap_dump_file_write(wdh, &zero_pad, pad, err)) {
return FALSE;
}
}
ws_debug("Wrote custom option: type %u, length %u", option_hdr.type, option_hdr.value_length);
return TRUE;
}
static gboolean pcapng_write_packet_verdict_option(wtap_dumper *wdh, guint option_id, wtap_optval_t *optval, int *err)
{
packet_verdict_opt_t* verdict = &optval->packet_verdictval;
struct pcapng_option_header option_hdr;
guint8 type;
size_t size;
const guint32 zero_pad = 0;
guint32 pad;
switch (verdict->type) {
case packet_verdict_hardware:
/* Include type octet */
size = verdict->data.verdict_bytes->len + 1;
if (size > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return TRUE;
}
option_hdr.type = option_id;
option_hdr.value_length = (guint16)size;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
type = packet_verdict_hardware;
if (!wtap_dump_file_write(wdh, &type, sizeof(guint8), err))
return FALSE;
if (!wtap_dump_file_write(wdh, verdict->data.verdict_bytes->data,
verdict->data.verdict_bytes->len, err))
return FALSE;
break;
case packet_verdict_linux_ebpf_tc:
size = 9;
option_hdr.type = option_id;
option_hdr.value_length = (guint16)size;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
type = packet_verdict_linux_ebpf_tc;
if (!wtap_dump_file_write(wdh, &type, sizeof(guint8), err))
return FALSE;
if (!wtap_dump_file_write(wdh, &verdict->data.verdict_linux_ebpf_tc,
sizeof(guint64), err))
return FALSE;
break;
case packet_verdict_linux_ebpf_xdp:
size = 9;
option_hdr.type = option_id;
option_hdr.value_length = (guint16)size;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
type = packet_verdict_linux_ebpf_xdp;
if (!wtap_dump_file_write(wdh, &type, sizeof(guint8), err))
return FALSE;
if (!wtap_dump_file_write(wdh, &verdict->data.verdict_linux_ebpf_xdp,
sizeof(guint64), err))
return FALSE;
break;
default:
/* Unknown - don't write it out. */
return TRUE;
}
/* write padding (if any) */
if ((size % 4)) {
pad = 4 - (size % 4);
if (!wtap_dump_file_write(wdh, &zero_pad, pad, err))
return FALSE;
}
return TRUE;
}
static gboolean pcapng_write_packet_hash_option(wtap_dumper *wdh, unsigned option_id, wtap_optval_t *optval, int *err)
{
packet_hash_opt_t* hash = &optval->packet_hash;
struct pcapng_option_header option_hdr;
uint8_t type;
size_t size;
const uint32_t zero_pad = 0;
uint32_t pad;
size = pcapng_compute_packet_hash_option_size(optval);
if (size > 65535) {
/*
* Too big to fit in the option.
* Don't write anything.
*
* XXX - truncate it? Report an error?
*/
return TRUE;
}
if (size > hash->hash_bytes->len + 1) {
/*
* We don't have enough bytes to write.
* pcapng_compute_packet_hash_option_size() should return 0 if
* we want to silently omit the option instead, or should return
* the length if we want to blindly copy it.
* XXX - Is this the best error type?
*/
*err = WTAP_ERR_UNWRITABLE_REC_DATA;
return FALSE;
}
type = hash->type;
option_hdr.type = option_id;
/* Include type byte */
option_hdr.value_length = (uint16_t)size;
if (!wtap_dump_file_write(wdh, &option_hdr, 4, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &type, sizeof(uint8_t), err))
return FALSE;
if (!wtap_dump_file_write(wdh, hash->hash_bytes->data, size - 1,
err))
return FALSE;
/* write padding (if any) */
if ((size % 4)) {
pad = 4 - (size % 4);
if (!wtap_dump_file_write(wdh, &zero_pad, pad, err))
return FALSE;
}
return TRUE;
}
static gboolean write_block_option(wtap_block_t block, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval, void* user_data)
{
write_options_t* options = (write_options_t*)user_data;
/*
* Process the option IDs that are the same for all block types here;
* call the block-type-specific compute_size function for others.
*/
switch(option_id)
{
case OPT_COMMENT:
if (!pcapng_write_string_option(options->wdh, option_id, optval, options->err))
return FALSE;
break;
case OPT_CUSTOM_STR_COPY:
case OPT_CUSTOM_BIN_COPY:
if (!pcapng_write_custom_option(options->wdh, option_id, optval, options->err))
return FALSE;
break;
case OPT_CUSTOM_STR_NO_COPY:
case OPT_CUSTOM_BIN_NO_COPY:
/*
* Do not write these, as they're not supposed to be copied to
* new files.
*
* XXX - what if we're writing out a file that's *not* based on
* another file, so that we're *not* copying it from that file?
*/
break;
default:
/* Block-type dependent; call the callback, if we have one. */
if (options->write_option != NULL &&
!(*options->write_option)(options->wdh, block, option_id, option_type, optval, options->err))
return FALSE;
break;
}
return TRUE;
}
static gboolean
write_options(wtap_dumper *wdh, wtap_block_t block, write_option_func write_option, int *err)
{
write_options_t options;
options.wdh = wdh;
options.err = err;
options.write_option = write_option;
if (!wtap_block_foreach_option(block, write_block_option, &options))
return FALSE;
/* Write end of options */
return pcapng_write_option_eofopt(wdh, err);
}
static gboolean write_wtap_shb_option(wtap_dumper *wdh, wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval, int *err)
{
switch(option_id)
{
case OPT_SHB_HARDWARE:
case OPT_SHB_OS:
case OPT_SHB_USERAPPL:
if (!pcapng_write_string_option(wdh, option_id, optval, err))
return FALSE;
break;
default:
/* Unknown options - write by datatype? */
break;
}
return TRUE; /* success */
}
/* Write a section header block.
* If we don't have a section block header already, create a default
* one with no options.
*/
static gboolean
pcapng_write_section_header_block(wtap_dumper *wdh, int *err)
{
pcapng_block_header_t bh;
pcapng_section_header_block_t shb;
guint32 options_size;
wtap_block_t wdh_shb = NULL;
if (wdh->shb_hdrs && (wdh->shb_hdrs->len > 0)) {
wdh_shb = g_array_index(wdh->shb_hdrs, wtap_block_t, 0);
}
bh.block_total_length = (guint32)(sizeof(bh) + sizeof(shb) + 4);
options_size = 0;
if (wdh_shb) {
ws_debug("Have shb_hdr");
/* Compute size of all the options */
options_size = compute_options_size(wdh_shb, compute_shb_option_size);
bh.block_total_length += options_size;
}
ws_debug("Total len %u", bh.block_total_length);
/* write block header */
bh.block_type = BLOCK_TYPE_SHB;
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
/* write block fixed content */
shb.magic = 0x1A2B3C4D;
shb.version_major = 1;
shb.version_minor = 0;
if (wdh_shb) {
wtapng_section_mandatory_t* section_data = (wtapng_section_mandatory_t*)wtap_block_get_mandatory_data(wdh_shb);
shb.section_length = section_data->section_length;
} else {
shb.section_length = -1;
}
if (!wtap_dump_file_write(wdh, &shb, sizeof shb, err))
return FALSE;
if (wdh_shb) {
/* Write options, if we have any */
if (options_size != 0) {
if (!write_options(wdh, wdh_shb, write_wtap_shb_option, err))
return FALSE;
}
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
return TRUE;
}
/* options defined in Section 2.5 (Options)
* Name Code Length Description
* opt_comment 1 variable A UTF-8 string containing a comment that is associated to the current block.
*
* Enhanced Packet Block options
* epb_flags 2 4 A flags word containing link-layer information. A complete specification of
* the allowed flags can be found in Appendix A (Packet Block Flags Word).
* epb_hash 3 variable This option contains a hash of the packet. The first byte specifies the hashing algorithm,
* while the following bytes contain the actual hash, whose size depends on the hashing algorithm,
* and hence from the value in the first bit. The hashing algorithm can be: 2s complement
* (algorithm byte = 0, size=XXX), XOR (algorithm byte = 1, size=XXX), CRC32 (algorithm byte = 2, size = 4),
* MD-5 (algorithm byte = 3, size=XXX), SHA-1 (algorithm byte = 4, size=XXX).
* The hash covers only the packet, not the header added by the capture driver:
* this gives the possibility to calculate it inside the network card.
* The hash allows easier comparison/merging of different capture files, and reliable data transfer between the
* data acquisition system and the capture library.
* epb_dropcount 4 8 A 64bit integer value specifying the number of packets lost (by the interface and the operating system)
* between this packet and the preceding one.
* epb_packetid 5 8 The epb_packetid option is a 64-bit unsigned integer that
* uniquely identifies the packet. If the same packet is seen
* by multiple interfaces and there is a way for the capture
* application to correlate them, the same epb_packetid value
* must be used. An example could be a router that captures
* packets on all its interfaces in both directions. When a
* packet hits interface A on ingress, an EPB entry gets
* created, TTL gets decremented, and right before it egresses
* on interface B another EPB entry gets created in the trace
* file. In this case, two packets are in the capture file,
* which are not identical but the epb_packetid can be used to
* correlate them.
* epb_queue 6 4 The epb_queue option is a 32-bit unsigned integer that
* identifies on which queue of the interface the specific
* packet was received.
* epb_verdict 7 variable The epb_verdict option stores a verdict of the packet. The
* verdict indicates what would be done with the packet after
* processing it. For example, a firewall could drop the
* packet. This verdict can be set by various components, i.e.
* Hardware, Linux's eBPF TC or XDP framework, etc. etc. The
* first octet specifies the verdict type, while the following
* octets contain the actual verdict data, whose size depends on
* the verdict type, and hence from the value in the first
* octet. The verdict type can be: Hardware (type octet = 0,
* size = variable), Linux_eBPF_TC (type octet = 1, size = 8
* (64-bit unsigned integer), value = TC_ACT_* as defined in the
* Linux pck_cls.h include), Linux_eBPF_XDP (type octet = 2,
* size = 8 (64-bit unsigned integer), value = xdp_action as
* defined in the Linux pbf.h include).
* opt_endofopt 0 0 It delimits the end of the optional fields. This block cannot be repeated within a given list of options.
*/
static guint32
compute_epb_option_size(wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t* optval)
{
guint32 size;
switch(option_id)
{
case OPT_EPB_FLAGS:
size = 4;
break;
case OPT_EPB_DROPCOUNT:
size = 8;
break;
case OPT_EPB_PACKETID:
size = 8;
break;
case OPT_EPB_QUEUE:
size = 4;
break;
case OPT_EPB_VERDICT:
size = pcapng_compute_packet_verdict_option_size(optval);
break;
case OPT_EPB_HASH:
size = pcapng_compute_packet_hash_option_size(optval);
break;
default:
/* Unknown options - size by datatype? */
size = 0;
break;
}
return size;
}
static gboolean write_wtap_epb_option(wtap_dumper *wdh, wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval, int *err)
{
switch(option_id)
{
case OPT_PKT_FLAGS:
if (!pcapng_write_uint32_option(wdh, OPT_EPB_FLAGS, optval, err))
return FALSE;
break;
case OPT_PKT_DROPCOUNT:
if (!pcapng_write_uint64_option(wdh, OPT_EPB_DROPCOUNT, optval, err))
return FALSE;
break;
case OPT_PKT_PACKETID:
if (!pcapng_write_uint64_option(wdh, OPT_EPB_PACKETID, optval, err))
return FALSE;
break;
case OPT_PKT_QUEUE:
if (!pcapng_write_uint32_option(wdh, OPT_EPB_QUEUE, optval, err))
return FALSE;
break;
case OPT_PKT_VERDICT:
if (!pcapng_write_packet_verdict_option(wdh, OPT_EPB_VERDICT, optval,
err))
return false;
break;
case OPT_PKT_HASH:
if (!pcapng_write_packet_hash_option(wdh, OPT_EPB_HASH, optval,
err))
return false;
break;
default:
/* Unknown options - write by datatype? */
break;
}
return TRUE; /* success */
}
static gboolean
pcapng_write_enhanced_packet_block(wtap_dumper *wdh, const wtap_rec *rec,
const guint8 *pd, int *err, gchar **err_info)
{
const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
pcapng_block_header_t bh;
pcapng_enhanced_packet_block_t epb;
guint32 options_size = 0;
guint64 ts;
const guint32 zero_pad = 0;
guint32 pad_len;
guint32 phdr_len;
guint32 options_total_length = 0;
wtap_block_t int_data;
wtapng_if_descr_mandatory_t *int_data_mand;
/* Don't write anything we're not willing to read. */
if (rec->rec_header.packet_header.caplen > wtap_max_snaplen_for_encap(wdh->file_encap)) {
*err = WTAP_ERR_PACKET_TOO_LARGE;
return FALSE;
}
phdr_len = (guint32)pcap_get_phdr_size(rec->rec_header.packet_header.pkt_encap, pseudo_header);
if ((phdr_len + rec->rec_header.packet_header.caplen) % 4) {
pad_len = 4 - ((phdr_len + rec->rec_header.packet_header.caplen) % 4);
} else {
pad_len = 0;
}
if (rec->block != NULL) {
/* Compute size of all the options */
options_size = compute_options_size(rec->block, compute_epb_option_size);
}
/*
* Check the interface ID. Do this before writing the header,
* in case we need to add a new IDB.
*/
if (rec->presence_flags & WTAP_HAS_INTERFACE_ID)
epb.interface_id = rec->rec_header.packet_header.interface_id;
else {
/*
* The source isn't sending us IDBs. See if we already have a
* matching interface, and use it if so.
*/
for (epb.interface_id = 0; epb.interface_id < wdh->interface_data->len; ++epb.interface_id) {
int_data = g_array_index(wdh->interface_data, wtap_block_t,
epb.interface_id);
int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data);
if (int_data_mand->wtap_encap == rec->rec_header.packet_header.pkt_encap) {
if (int_data_mand->tsprecision == rec->tsprec || (!(rec->presence_flags & WTAP_HAS_TS))) {
break;
}
}
}
if (epb.interface_id == wdh->interface_data->len) {
/*
* We don't have a matching IDB. Generate a new one
* and write it to the file.
*/
int_data = wtap_rec_generate_idb(rec);
g_array_append_val(wdh->interface_data, int_data);
if (!pcapng_write_if_descr_block(wdh, int_data, err)) {
return FALSE;
}
}
}
if (epb.interface_id >= wdh->interface_data->len) {
/*
* Our caller is doing something bad.
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("pcapng: epb.interface_id (%u) >= wdh->interface_data->len (%u)",
epb.interface_id, wdh->interface_data->len);
return FALSE;
}
int_data = g_array_index(wdh->interface_data, wtap_block_t,
epb.interface_id);
int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data);
if (int_data_mand->wtap_encap != rec->rec_header.packet_header.pkt_encap) {
/*
* Our caller is doing something bad.
*/
*err = WTAP_ERR_INTERNAL;
*err_info = ws_strdup_printf("pcapng: interface %u encap %d != packet encap %d",
epb.interface_id,
int_data_mand->wtap_encap,
rec->rec_header.packet_header.pkt_encap);
return FALSE;
}
/* write (enhanced) packet block header */
bh.block_type = BLOCK_TYPE_EPB;
bh.block_total_length = (guint32)sizeof(bh) + (guint32)sizeof(epb) + phdr_len + rec->rec_header.packet_header.caplen + pad_len + options_total_length + options_size + 4;
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
/* write block fixed content */
/* Calculate the time stamp as a 64-bit integer. */
ts = ((guint64)rec->ts.secs) * int_data_mand->time_units_per_second +
(((guint64)rec->ts.nsecs) * int_data_mand->time_units_per_second) / 1000000000;
/*
* Split the 64-bit timestamp into two 32-bit pieces, using
* the time stamp resolution for the interface.
*/
epb.timestamp_high = (guint32)(ts >> 32);
epb.timestamp_low = (guint32)ts;
epb.captured_len = rec->rec_header.packet_header.caplen + phdr_len;
epb.packet_len = rec->rec_header.packet_header.len + phdr_len;
if (!wtap_dump_file_write(wdh, &epb, sizeof epb, err))
return FALSE;
/* write pseudo header */
if (!pcap_write_phdr(wdh, rec->rec_header.packet_header.pkt_encap, pseudo_header, err)) {
return FALSE;
}
/* write packet data */
if (!wtap_dump_file_write(wdh, pd, rec->rec_header.packet_header.caplen, err))
return FALSE;
/* write padding (if any) */
if (pad_len != 0) {
if (!wtap_dump_file_write(wdh, &zero_pad, pad_len, err))
return FALSE;
}
/* Write options, if we have any */
if (options_size != 0) {
if (!write_options(wdh, rec->block, write_wtap_epb_option, err))
return FALSE;
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
return TRUE;
}
static gboolean
pcapng_write_sysdig_event_block(wtap_dumper *wdh, const wtap_rec *rec,
const guint8 *pd, int *err)
{
pcapng_block_header_t bh;
const guint32 zero_pad = 0;
guint32 pad_len;
#if 0
gboolean have_options = FALSE;
struct pcapng_option option_hdr;
guint32 comment_len = 0, comment_pad_len = 0;
#endif
guint32 options_total_length = 0;
guint16 cpu_id;
guint64 hdr_ts;
guint64 ts;
guint64 thread_id;
guint32 event_len;
guint16 event_type;
/* Don't write anything we're not willing to read. */
if (rec->rec_header.syscall_header.event_filelen > WTAP_MAX_PACKET_SIZE_STANDARD) {
*err = WTAP_ERR_PACKET_TOO_LARGE;
return FALSE;
}
if (rec->rec_header.syscall_header.event_filelen % 4) {
pad_len = 4 - (rec->rec_header.syscall_header.event_filelen % 4);
} else {
pad_len = 0;
}
#if 0
/* Check if we should write comment option */
if (rec->opt_comment) {
have_options = TRUE;
comment_len = (guint32)strlen(rec->opt_comment) & 0xffff;
if ((comment_len % 4)) {
comment_pad_len = 4 - (comment_len % 4);
} else {
comment_pad_len = 0;
}
options_total_length = options_total_length + comment_len + comment_pad_len + 4 /* comment options tag */ ;
}
if (have_options) {
/* End-of options tag */
options_total_length += 4;
}
#endif
/* write sysdig event block header */
bh.block_type = BLOCK_TYPE_SYSDIG_EVENT;
bh.block_total_length = (guint32)sizeof(bh) + SYSDIG_EVENT_HEADER_SIZE + rec->rec_header.syscall_header.event_filelen + pad_len + options_total_length + 4;
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
/* Sysdig is always LE? */
cpu_id = GUINT16_TO_LE(rec->rec_header.syscall_header.cpu_id);
hdr_ts = (((guint64)rec->ts.secs) * 1000000000) + rec->ts.nsecs;
ts = GUINT64_TO_LE(hdr_ts);
thread_id = GUINT64_TO_LE(rec->rec_header.syscall_header.thread_id);
event_len = GUINT32_TO_LE(rec->rec_header.syscall_header.event_len);
event_type = GUINT16_TO_LE(rec->rec_header.syscall_header.event_type);
if (!wtap_dump_file_write(wdh, &cpu_id, sizeof cpu_id, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &ts, sizeof ts, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &thread_id, sizeof thread_id, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &event_len, sizeof event_len, err))
return FALSE;
if (!wtap_dump_file_write(wdh, &event_type, sizeof event_type, err))
return FALSE;
/* write event data */
if (!wtap_dump_file_write(wdh, pd, rec->rec_header.syscall_header.event_filelen, err))
return FALSE;
/* write padding (if any) */
if (pad_len != 0) {
if (!wtap_dump_file_write(wdh, &zero_pad, pad_len, err))
return FALSE;
}
/* XXX Write comment? */
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
return TRUE;
}
static gboolean
pcapng_write_systemd_journal_export_block(wtap_dumper *wdh, const wtap_rec *rec,
const guint8 *pd, int *err)
{
pcapng_block_header_t bh;
const guint32 zero_pad = 0;
guint32 pad_len;
/* Don't write anything we're not willing to read. */
if (rec->rec_header.systemd_journal_export_header.record_len > WTAP_MAX_PACKET_SIZE_STANDARD) {
*err = WTAP_ERR_PACKET_TOO_LARGE;
return FALSE;
}
if (rec->rec_header.systemd_journal_export_header.record_len % 4) {
pad_len = 4 - (rec->rec_header.systemd_journal_export_header.record_len % 4);
} else {
pad_len = 0;
}
/* write systemd journal export block header */
bh.block_type = BLOCK_TYPE_SYSTEMD_JOURNAL_EXPORT;
bh.block_total_length = (guint32)sizeof(bh) + rec->rec_header.systemd_journal_export_header.record_len + pad_len + 4;
ws_debug("writing %u bytes, %u padded",
rec->rec_header.systemd_journal_export_header.record_len,
bh.block_total_length);
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
/* write entry data */
if (!wtap_dump_file_write(wdh, pd, rec->rec_header.systemd_journal_export_header.record_len, err))
return FALSE;
/* write padding (if any) */
if (pad_len != 0) {
if (!wtap_dump_file_write(wdh, &zero_pad, pad_len, err))
return FALSE;
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
return TRUE;
}
static gboolean
pcapng_write_custom_block(wtap_dumper *wdh, const wtap_rec *rec,
const guint8 *pd, int *err)
{
pcapng_block_header_t bh;
pcapng_custom_block_t cb;
const guint32 zero_pad = 0;
guint32 pad_len;
/* Don't write anything we are not supposed to. */
if (!rec->rec_header.custom_block_header.copy_allowed) {
return TRUE;
}
/* Don't write anything we're not willing to read. */
if (rec->rec_header.custom_block_header.length > WTAP_MAX_PACKET_SIZE_STANDARD) {
*err = WTAP_ERR_PACKET_TOO_LARGE;
return FALSE;
}
if (rec->rec_header.custom_block_header.length % 4) {
pad_len = 4 - (rec->rec_header.custom_block_header.length % 4);
} else {
pad_len = 0;
}
/* write block header */
bh.block_type = BLOCK_TYPE_CB_COPY;
bh.block_total_length = (guint32)sizeof(bh) + (guint32)sizeof(cb) + rec->rec_header.custom_block_header.length + pad_len + 4;
ws_debug("writing %u bytes, %u padded, PEN %u",
rec->rec_header.custom_block_header.length,
bh.block_total_length, rec->rec_header.custom_block_header.pen);
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err)) {
return FALSE;
}
/* write custom block header */
cb.pen = rec->rec_header.custom_block_header.pen;
if (!wtap_dump_file_write(wdh, &cb, sizeof cb, err)) {
return FALSE;
}
ws_debug("wrote PEN = %u", cb.pen);
/* write custom data */
if (!wtap_dump_file_write(wdh, pd, rec->rec_header.custom_block_header.length, err)) {
return FALSE;
}
/* write padding (if any) */
if (pad_len > 0) {
if (!wtap_dump_file_write(wdh, &zero_pad, pad_len, err)) {
return FALSE;
}
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err)) {
return FALSE;
}
return TRUE;
}
static gboolean
pcapng_write_bblog_block(wtap_dumper *wdh, const wtap_rec *rec,
const guint8 *pd _U_, int *err)
{
pcapng_block_header_t bh;
guint32 options_size = 0;
guint32 pen, skipped, type;
/* Compute size of all the options */
options_size = compute_options_size(rec->block, compute_epb_option_size);
/* write block header */
bh.block_type = BLOCK_TYPE_CB_COPY;
bh.block_total_length = (guint32)(sizeof(bh) + sizeof(guint32) + sizeof(guint32) + options_size + 4);
if (rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.type == BBLOG_TYPE_SKIPPED_BLOCK) {
bh.block_total_length += (guint32)sizeof(guint32);
}
ws_debug("writing %u bytes, type %u",
bh.block_total_length, rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.type);
if (!wtap_dump_file_write(wdh, &bh, sizeof(bh), err)) {
return FALSE;
}
/* write PEN */
pen = PEN_NFLX;
if (!wtap_dump_file_write(wdh, &pen, sizeof(guint32), err)) {
return FALSE;
}
ws_debug("wrote PEN = %u", pen);
/* write type */
type = GUINT32_TO_LE(rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.type);
if (!wtap_dump_file_write(wdh, &type, sizeof(guint32), err)) {
return FALSE;
}
ws_debug("wrote type = %u", rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.type);
if (rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.type == BBLOG_TYPE_SKIPPED_BLOCK) {
skipped = GUINT32_TO_LE(rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.skipped);
if (!wtap_dump_file_write(wdh, &skipped, sizeof(guint32), err)) {
return FALSE;
}
ws_debug("wrote skipped = %u", rec->rec_header.custom_block_header.custom_data_header.nflx_custom_data_header.skipped);
}
/* Write options, if we have any */
if (options_size != 0) {
/*
* This block type supports only comments and custom options,
* so it doesn't need a callback.
*/
if (!write_options(wdh, rec->block, NULL, err))
return FALSE;
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err)) {
return FALSE;
}
return TRUE;
}
static gboolean
pcapng_write_decryption_secrets_block(wtap_dumper *wdh, wtap_block_t sdata, int *err)
{
pcapng_block_header_t bh;
pcapng_decryption_secrets_block_t dsb;
wtapng_dsb_mandatory_t *mand_data = (wtapng_dsb_mandatory_t *)wtap_block_get_mandatory_data(sdata);
guint pad_len = (4 - (mand_data->secrets_len & 3)) & 3;
/* write block header */
bh.block_type = BLOCK_TYPE_DSB;
bh.block_total_length = MIN_DSB_SIZE + mand_data->secrets_len + pad_len;
ws_debug("Total len %u", bh.block_total_length);
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
/* write block fixed content */
dsb.secrets_type = mand_data->secrets_type;
dsb.secrets_len = mand_data->secrets_len;
if (!wtap_dump_file_write(wdh, &dsb, sizeof dsb, err))
return FALSE;
if (!wtap_dump_file_write(wdh, mand_data->secrets_data, mand_data->secrets_len, err))
return FALSE;
if (pad_len) {
const guint32 zero_pad = 0;
if (!wtap_dump_file_write(wdh, &zero_pad, pad_len, err))
return FALSE;
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
return TRUE;
}
static bool
pcapng_write_sysdig_meta_event_block(wtap_dumper *wdh, wtap_block_t mev_data, int *err)
{
pcapng_block_header_t bh;
wtapng_sysdig_mev_mandatory_t *mand_data = (wtapng_sysdig_mev_mandatory_t *)wtap_block_get_mandatory_data(mev_data);
unsigned pad_len = (4 - (mand_data->mev_data_len & 3)) & 3;
/* write block header */
bh.block_type = mand_data->mev_type;
bh.block_total_length = MIN_BLOCK_SIZE + mand_data->mev_data_len + pad_len;
ws_debug("Sysdig mev total len %u", bh.block_total_length);
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return false;
/* write block fixed content */
if (!wtap_dump_file_write(wdh, mand_data->mev_data, mand_data->mev_data_len, err))
return false;
if (pad_len) {
const uint32_t zero_pad = 0;
if (!wtap_dump_file_write(wdh, &zero_pad, pad_len, err))
return false;
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return false;
return true;
}
/*
* libpcap's maximum pcapng block size is currently 16MB.
*
* The maximum pcapng block size in macOS's private pcapng reading code
* is 1MB. (Yes, this means that a program using the standard pcap
* code to read pcapng files can handle bigger blocks than can programs
* using the private code, such as Apple's tcpdump, can handle.)
*
* The pcapng reading code here can handle NRBs of arbitrary size (less
* than 4GB, obviously), as they read each NRB record independently,
* rather than reading the entire block into memory.
*
* So, for now, we set the maximum NRB block size we write as 1 MB.
*
* (Yes, for the benefit of the fussy, "MB" is really "MiB".)
*/
#define NRES_BLOCK_MAX_SIZE (1024*1024)
static guint32
compute_nrb_option_size(wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t* optval)
{
guint32 size;
switch(option_id)
{
case OPT_NS_DNSNAME:
size = pcapng_compute_string_option_size(optval);
break;
case OPT_NS_DNSIP4ADDR:
size = 4;
break;
case OPT_NS_DNSIP6ADDR:
size = 16;
break;
default:
/* Unknown options - size by datatype? */
size = 0;
break;
}
return size;
}
static gboolean
put_nrb_option(wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t* optval, void* user_data)
{
guint8 **opt_ptrp = (guint8 **)user_data;
guint32 size = 0;
struct pcapng_option_header option_hdr;
guint32 pad;
switch(option_id)
{
case OPT_COMMENT:
case OPT_NS_DNSNAME:
/* String options don't consider pad bytes part of the length */
size = (guint32)strlen(optval->stringval) & 0xffff;
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)size;
memcpy(*opt_ptrp, &option_hdr, 4);
*opt_ptrp += 4;
memcpy(*opt_ptrp, optval->stringval, size);
*opt_ptrp += size;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
/* put padding (if any) */
if (pad != 0) {
memset(*opt_ptrp, 0, pad);
*opt_ptrp += pad;
}
break;
case OPT_CUSTOM_STR_COPY:
case OPT_CUSTOM_BIN_COPY:
/* Custom options don't consider pad bytes part of the length */
size = (guint32)(optval->custom_opt.data.generic_data.custom_data_len + sizeof(guint32)) & 0xffff;
option_hdr.type = (guint16)option_id;
option_hdr.value_length = (guint16)size;
memcpy(*opt_ptrp, &option_hdr, 4);
*opt_ptrp += 4;
memcpy(*opt_ptrp, &optval->custom_opt.pen, sizeof(guint32));
*opt_ptrp += sizeof(guint32);
memcpy(*opt_ptrp, optval->custom_opt.data.generic_data.custom_data, optval->custom_opt.data.generic_data.custom_data_len);
*opt_ptrp += optval->custom_opt.data.generic_data.custom_data_len;
if ((size % 4)) {
pad = 4 - (size % 4);
} else {
pad = 0;
}
/* put padding (if any) */
if (pad != 0) {
memset(*opt_ptrp, 0, pad);
*opt_ptrp += pad;
}
break;
case OPT_NS_DNSIP4ADDR:
option_hdr.type = (guint16)option_id;
option_hdr.value_length = 4;
memcpy(*opt_ptrp, &option_hdr, 4);
*opt_ptrp += 4;
memcpy(*opt_ptrp, &optval->ipv4val, 4);
*opt_ptrp += 4;
break;
case OPT_NS_DNSIP6ADDR:
option_hdr.type = (guint16)option_id;
option_hdr.value_length = 16;
memcpy(*opt_ptrp, &option_hdr, 4);
*opt_ptrp += 4;
memcpy(*opt_ptrp, &optval->ipv6val, 16);
*opt_ptrp += 16;
break;
default:
/* Unknown options - size by datatype? */
break;
}
return TRUE; /* we always succeed */
}
static void
put_nrb_options(wtap_dumper *wdh _U_, wtap_block_t nrb, guint8 *opt_ptr)
{
struct pcapng_option option_hdr;
wtap_block_foreach_option(nrb, put_nrb_option, &opt_ptr);
/* Put end of options */
option_hdr.type = OPT_EOFOPT;
option_hdr.value_length = 0;
memcpy(opt_ptr, &option_hdr, 4);
}
static gboolean
pcapng_write_name_resolution_block(wtap_dumper *wdh, wtap_block_t sdata, int *err)
{
pcapng_block_header_t bh;
pcapng_name_resolution_block_t nrb;
wtapng_nrb_mandatory_t *mand_data = (wtapng_nrb_mandatory_t *)wtap_block_get_mandatory_data(sdata);
guint32 options_size;
size_t max_rec_data_size;
guint8 *block_data;
guint32 block_off;
size_t hostnamelen;
guint16 namelen;
guint32 tot_rec_len;
hashipv4_t *ipv4_hash_list_entry;
hashipv6_t *ipv6_hash_list_entry;
int i;
if (!mand_data) {
/*
* No name/address pairs to write.
* XXX - what if we have options?
*/
return TRUE;
}
/* Calculate the space needed for options. */
options_size = compute_options_size(sdata, compute_nrb_option_size);
/*
* Make sure we can fit at least one maximum-sized record, plus
* an end-of-records record, plus the options, into a maximum-sized
* block.
*
* That requires that there be enough space for the block header
* (8 bytes), a maximum-sized record (2 bytes of record type, 2
* bytes of record value length, 65535 bytes of record value,
* and 1 byte of padding), an end-of-records record (4 bytes),
* the options (options_size bytes), and the block trailer (4
* bytes).
*/
if (8 + 2 + 2 + 65535 + 1 + 4 + options_size + 4 > NRES_BLOCK_MAX_SIZE) {
/*
* XXX - we can't even fit the options in the largest NRB size
* we're willing to write and still have room enough for a
* maximum-sized record. Just discard the information for now.
*/
return TRUE;
}
/*
* Allocate a buffer for the largest block we'll write.
*/
block_data = (guint8 *)g_malloc(NRES_BLOCK_MAX_SIZE);
/*
* Calculate the maximum amount of record data we'll be able to
* fit into such a block, after taking into account the block header
* (8 bytes), the end-of-records record (4 bytes), the options
* (options_size bytes), and the block trailer (4 bytes).
*/
max_rec_data_size = NRES_BLOCK_MAX_SIZE - (8 + 4 + options_size + 4);
block_off = 8; /* block type + block total length */
bh.block_type = BLOCK_TYPE_NRB;
bh.block_total_length = 12; /* block header + block trailer */
/*
* Write out the IPv4 resolved addresses, if any.
*/
if (mand_data->ipv4_addr_list){
i = 0;
ipv4_hash_list_entry = (hashipv4_t *)g_list_nth_data(mand_data->ipv4_addr_list, i);
while(ipv4_hash_list_entry != NULL){
nrb.record_type = NRES_IP4RECORD;
hostnamelen = strlen(ipv4_hash_list_entry->name);
if (hostnamelen > (G_MAXUINT16 - 4) - 1) {
/*
* This won't fit in the largest possible NRB record;
* discard it.
*/
i++;
ipv4_hash_list_entry = (hashipv4_t *)g_list_nth_data(mand_data->ipv4_addr_list, i);
continue;
}
namelen = (guint16)(hostnamelen + 1);
nrb.record_len = 4 + namelen; /* 4 bytes IPv4 address length */
/* 2 bytes record type, 2 bytes length field */
tot_rec_len = 4 + nrb.record_len + PADDING4(nrb.record_len);
if (block_off + tot_rec_len > max_rec_data_size) {
/*
* This record would overflow our maximum size for Name
* Resolution Blocks; write out all the records we created
* before it, and start a new NRB.
*/
/* Append the end-of-records record */
memset(block_data + block_off, 0, 4);
block_off += 4;
bh.block_total_length += 4;
/*
* Put the options into the block.
*/
put_nrb_options(wdh, sdata, block_data + block_off);
block_off += options_size;
bh.block_total_length += options_size;
/* Copy the block header. */
memcpy(block_data, &bh, sizeof(bh));
/* Copy the block trailer. */
memcpy(block_data + block_off, &bh.block_total_length, sizeof(bh.block_total_length));
ws_debug("Write bh.block_total_length bytes %d, block_off %u",
bh.block_total_length, block_off);
if (!wtap_dump_file_write(wdh, block_data, bh.block_total_length, err)) {
g_free(block_data);
return FALSE;
}
/*Start a new NRB */
block_off = 8; /* block type + block total length */
bh.block_type = BLOCK_TYPE_NRB;
bh.block_total_length = 12; /* block header + block trailer */
}
bh.block_total_length += tot_rec_len;
memcpy(block_data + block_off, &nrb, sizeof(nrb));
block_off += 4;
memcpy(block_data + block_off, &(ipv4_hash_list_entry->addr), 4);
block_off += 4;
memcpy(block_data + block_off, ipv4_hash_list_entry->name, namelen);
block_off += namelen;
memset(block_data + block_off, 0, PADDING4(namelen));
block_off += PADDING4(namelen);
ws_debug("added IPv4 record for %s", ipv4_hash_list_entry->name);
i++;
ipv4_hash_list_entry = (hashipv4_t *)g_list_nth_data(mand_data->ipv4_addr_list, i);
}
}
if (mand_data->ipv6_addr_list){
i = 0;
ipv6_hash_list_entry = (hashipv6_t *)g_list_nth_data(mand_data->ipv6_addr_list, i);
while(ipv6_hash_list_entry != NULL){
nrb.record_type = NRES_IP6RECORD;
hostnamelen = strlen(ipv6_hash_list_entry->name);
if (hostnamelen > (G_MAXUINT16 - 16) - 1) {
/*
* This won't fit in the largest possible NRB record;
* discard it.
*/
i++;
ipv6_hash_list_entry = (hashipv6_t *)g_list_nth_data(mand_data->ipv6_addr_list, i);
continue;
}
namelen = (guint16)(hostnamelen + 1);
nrb.record_len = 16 + namelen; /* 16 bytes IPv6 address length */
/* 2 bytes record type, 2 bytes length field */
tot_rec_len = 4 + nrb.record_len + PADDING4(nrb.record_len);
if (block_off + tot_rec_len > max_rec_data_size) {
/*
* This record would overflow our maximum size for Name
* Resolution Blocks; write out all the records we created
* before it, and start a new NRB.
*/
/* Append the end-of-records record */
memset(block_data + block_off, 0, 4);
block_off += 4;
bh.block_total_length += 4;
/*
* Put the options into the block.
*/
put_nrb_options(wdh, sdata, block_data + block_off);
block_off += options_size;
bh.block_total_length += options_size;
/* Copy the block header. */
memcpy(block_data, &bh, sizeof(bh));
/* Copy the block trailer. */
memcpy(block_data + block_off, &bh.block_total_length, sizeof(bh.block_total_length));
ws_debug("write bh.block_total_length bytes %d, block_off %u",
bh.block_total_length, block_off);
if (!wtap_dump_file_write(wdh, block_data, bh.block_total_length, err)) {
g_free(block_data);
return FALSE;
}
/*Start a new NRB */
block_off = 8; /* block type + block total length */
bh.block_type = BLOCK_TYPE_NRB;
bh.block_total_length = 12; /* block header + block trailer */
}
bh.block_total_length += tot_rec_len;
memcpy(block_data + block_off, &nrb, sizeof(nrb));
block_off += 4;
memcpy(block_data + block_off, &(ipv6_hash_list_entry->addr), 16);
block_off += 16;
memcpy(block_data + block_off, ipv6_hash_list_entry->name, namelen);
block_off += namelen;
memset(block_data + block_off, 0, PADDING4(namelen));
block_off += PADDING4(namelen);
ws_debug("added IPv6 record for %s", ipv6_hash_list_entry->name);
i++;
ipv6_hash_list_entry = (hashipv6_t *)g_list_nth_data(mand_data->ipv6_addr_list, i);
}
}
/* Append the end-of-records record */
memset(block_data + block_off, 0, 4);
block_off += 4;
bh.block_total_length += 4;
/*
* Put the options into the block.
*/
put_nrb_options(wdh, sdata, block_data + block_off);
block_off += options_size;
bh.block_total_length += options_size;
/* Copy the block header. */
memcpy(block_data, &bh, sizeof(bh));
/* Copy the block trailer. */
memcpy(block_data + block_off, &bh.block_total_length, sizeof(bh.block_total_length));
ws_debug("Write bh.block_total_length bytes %d, block_off %u",
bh.block_total_length, block_off);
if (!wtap_dump_file_write(wdh, block_data, bh.block_total_length, err)) {
g_free(block_data);
return FALSE;
}
g_free(block_data);
return TRUE;
}
static guint32 compute_isb_option_size(wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval _U_)
{
guint32 size;
switch(option_id)
{
case OPT_ISB_STARTTIME:
case OPT_ISB_ENDTIME:
size = 8;
break;
case OPT_ISB_IFRECV:
case OPT_ISB_IFDROP:
case OPT_ISB_FILTERACCEPT:
case OPT_ISB_OSDROP:
case OPT_ISB_USRDELIV:
size = 8;
break;
default:
/* Unknown options - size by datatype? */
size = 0;
break;
}
return size;
}
static gboolean write_wtap_isb_option(wtap_dumper *wdh, wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval, int *err)
{
switch(option_id)
{
case OPT_ISB_STARTTIME:
case OPT_ISB_ENDTIME:
if (!pcapng_write_timestamp_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_ISB_IFRECV:
case OPT_ISB_IFDROP:
case OPT_ISB_FILTERACCEPT:
case OPT_ISB_OSDROP:
case OPT_ISB_USRDELIV:
if (!pcapng_write_uint64_option(wdh, option_id, optval, err))
return FALSE;
break;
default:
/* Unknown options - write by datatype? */
break;
}
return TRUE; /* success */
}
static gboolean
pcapng_write_interface_statistics_block(wtap_dumper *wdh, wtap_block_t if_stats, int *err)
{
pcapng_block_header_t bh;
pcapng_interface_statistics_block_t isb;
guint32 options_size;
wtapng_if_stats_mandatory_t* mand_data = (wtapng_if_stats_mandatory_t*)wtap_block_get_mandatory_data(if_stats);
ws_debug("entering function");
/* Compute size of all the options */
options_size = compute_options_size(if_stats, compute_isb_option_size);
/* write block header */
bh.block_type = BLOCK_TYPE_ISB;
bh.block_total_length = (guint32)(sizeof(bh) + sizeof(isb) + options_size + 4);
ws_debug("Total len %u", bh.block_total_length);
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
/* write block fixed content */
isb.interface_id = mand_data->interface_id;
isb.timestamp_high = mand_data->ts_high;
isb.timestamp_low = mand_data->ts_low;
if (!wtap_dump_file_write(wdh, &isb, sizeof isb, err))
return FALSE;
/* Write options */
if (options_size != 0) {
if (!write_options(wdh, if_stats, write_wtap_isb_option, err))
return FALSE;
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
return TRUE;
}
static guint32 compute_idb_option_size(wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval)
{
guint32 size;
switch(option_id)
{
case OPT_IDB_NAME:
case OPT_IDB_DESCRIPTION:
case OPT_IDB_OS:
case OPT_IDB_HARDWARE:
size = pcapng_compute_string_option_size(optval);
break;
case OPT_IDB_SPEED:
size = 8;
break;
case OPT_IDB_TSRESOL:
size = 1;
break;
case OPT_IDB_FILTER:
size = pcapng_compute_if_filter_option_size(optval);
break;
case OPT_IDB_FCSLEN:
size = 1;
break;
case OPT_IDB_TSOFFSET:
/*
* The time stamps handed to us when writing a file are
* absolute time staps, so the time stamp offset is
* zero.
*
* We do not adjust them when writing, so we should not
* write if_tsoffset options; that is interpreted as
* the offset is zero, i.e. the time stamps in the file
* are absolute.
*/
size = 0;
break;
default:
/* Unknown options - size by datatype? */
size = 0;
break;
}
return size;
}
static gboolean write_wtap_idb_option(wtap_dumper *wdh, wtap_block_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_optval_t *optval, int *err)
{
switch(option_id)
{
case OPT_IDB_NAME:
case OPT_IDB_DESCRIPTION:
case OPT_IDB_OS:
case OPT_IDB_HARDWARE:
if (!pcapng_write_string_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_IDB_SPEED:
if (!pcapng_write_uint64_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_IDB_TSRESOL:
if (!pcapng_write_uint8_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_IDB_FILTER:
if (!pcapng_write_if_filter_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_IDB_FCSLEN:
if (!pcapng_write_uint8_option(wdh, option_id, optval, err))
return FALSE;
break;
case OPT_IDB_TSOFFSET:
/*
* As noted above, we discard these.
*/
break;
default:
/* Unknown options - size by datatype? */
break;
}
return TRUE;
}
static gboolean
pcapng_write_if_descr_block(wtap_dumper *wdh, wtap_block_t int_data, int *err)
{
pcapng_block_header_t bh;
pcapng_interface_description_block_t idb;
guint32 options_size;
wtapng_if_descr_mandatory_t* mand_data = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data);
int link_type;
ws_debug("encap = %d (%s), snaplen = %d",
mand_data->wtap_encap,
wtap_encap_description(mand_data->wtap_encap),
mand_data->snap_len);
link_type = wtap_wtap_encap_to_pcap_encap(mand_data->wtap_encap);
if (link_type == -1) {
if (!pcapng_encap_is_ft_specific(mand_data->wtap_encap)) {
*err = WTAP_ERR_UNWRITABLE_ENCAP;
return FALSE;
}
}
/* Compute size of all the options */
options_size = compute_options_size(int_data, compute_idb_option_size);
/* write block header */
bh.block_type = BLOCK_TYPE_IDB;
bh.block_total_length = (guint32)(sizeof(bh) + sizeof(idb) + options_size + 4);
ws_debug("Total len %u", bh.block_total_length);
if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err))
return FALSE;
/* write block fixed content */
idb.linktype = link_type;
idb.reserved = 0;
idb.snaplen = mand_data->snap_len;
if (!wtap_dump_file_write(wdh, &idb, sizeof idb, err))
return FALSE;
if (options_size != 0) {
/* Write options */
if (!write_options(wdh, int_data, write_wtap_idb_option, err))
return FALSE;
}
/* write block footer */
if (!wtap_dump_file_write(wdh, &bh.block_total_length,
sizeof bh.block_total_length, err))
return FALSE;
return TRUE;
}
static gboolean pcapng_add_idb(wtap_dumper *wdh, wtap_block_t idb,
int *err, gchar **err_info _U_)
{
wtap_block_t idb_copy;
/*
* Add a copy of this IDB to our array of IDBs.
*/
idb_copy = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO);
wtap_block_copy(idb_copy, idb);
g_array_append_val(wdh->interface_data, idb_copy);
/*
* And write it to the output file.
*/
return pcapng_write_if_descr_block(wdh, idb_copy, err);
}
static gboolean pcapng_write_internal_blocks(wtap_dumper *wdh, int *err)
{
/* Write (optional) Decryption Secrets Blocks that were collected while
* reading packet blocks. */
if (wdh->dsbs_growing) {
for (guint i = wdh->dsbs_growing_written; i < wdh->dsbs_growing->len; i++) {
ws_debug("writing DSB %u", i);
wtap_block_t dsb = g_array_index(wdh->dsbs_growing, wtap_block_t, i);
if (!pcapng_write_decryption_secrets_block(wdh, dsb, err)) {
return FALSE;
}
++wdh->dsbs_growing_written;
}
}
/* Write (optional) Sysdig Meta Event Blocks that were collected while
* reading packet blocks. */
if (wdh->sysdig_mev_growing) {
for (unsigned i = wdh->sysdig_mev_growing_written; i < wdh->sysdig_mev_growing->len; i++) {
ws_debug("writing Sysdig mev %u", i);
wtap_block_t mev = g_array_index(wdh->sysdig_mev_growing, wtap_block_t, i);
if (!pcapng_write_sysdig_meta_event_block(wdh, mev, err)) {
return false;
}
++wdh->sysdig_mev_growing_written;
}
}
/* Write any hostname resolution info from wtap_dump_set_addrinfo_list() */
if (!wtap_addrinfo_list_empty(wdh->addrinfo_lists)) {
/*
* XXX: get_addrinfo_list() returns a list of all known and used
* resolved addresses, regardless of origin: existing NRBs, externally
* resolved, DNS packet data, a hosts file, and manual host resolution
* through the GUI. It does not include the source for each.
*
* If it did, we could instead create multiple NRBs, one for each
* server (as the options can only be included once per block.)
* Instead, we copy the options from the first already existing NRB
* (if there is one), since some of the name resolutions may be
* from that block.
*/
wtap_block_t nrb;
if (wdh->nrbs_growing && wdh->nrbs_growing->len) {
nrb = wtap_block_make_copy(g_array_index(wdh->nrbs_growing, wtap_block_t, 0));
} else {
nrb = wtap_block_create(WTAP_BLOCK_NAME_RESOLUTION);
}
wtapng_nrb_mandatory_t *mand_data = (wtapng_nrb_mandatory_t *)wtap_block_get_mandatory_data(nrb);
mand_data->ipv4_addr_list = wdh->addrinfo_lists->ipv4_addr_list;
mand_data->ipv6_addr_list = wdh->addrinfo_lists->ipv6_addr_list;
if (!pcapng_write_name_resolution_block(wdh, nrb, err)) {
return FALSE;
}
mand_data->ipv4_addr_list = NULL;
mand_data->ipv6_addr_list = NULL;
wtap_block_unref(nrb);
g_list_free(wdh->addrinfo_lists->ipv4_addr_list);
wdh->addrinfo_lists->ipv4_addr_list = NULL;
g_list_free(wdh->addrinfo_lists->ipv6_addr_list);
wdh->addrinfo_lists->ipv6_addr_list = NULL;
/* Since the addrinfo lists include information from existing NRBs,
* avoid writing them to avoid duplication.
*
* XXX: Perhaps we don't want to include information from the NRBs
* in get_addrinfo_list at all, so that we could write existing
* NRBs as-is.
*
* This is still not well oriented for one-pass programs, where we
* don't have addrinfo_lists until we've already written the
* NRBs. We should not write both in such a situation. See bug 15502.
*/
wtap_dump_discard_name_resolution(wdh);
}
/* Write (optional) Name Resolution Blocks that were collected while
* reading packet blocks. */
if (wdh->nrbs_growing) {
for (guint i = wdh->nrbs_growing_written; i < wdh->nrbs_growing->len; i++) {
wtap_block_t nrb = g_array_index(wdh->nrbs_growing, wtap_block_t, i);
if (!pcapng_write_name_resolution_block(wdh, nrb, err)) {
return FALSE;
}
++wdh->nrbs_growing_written;
}
}
return TRUE;
}
static gboolean pcapng_dump(wtap_dumper *wdh,
const wtap_rec *rec,
const guint8 *pd, int *err, gchar **err_info)
{
#ifdef HAVE_PLUGINS
block_handler *handler;
#endif
if (!pcapng_write_internal_blocks(wdh, err)) {
return FALSE;
}
ws_debug("encap = %d (%s) rec type = %u",
rec->rec_header.packet_header.pkt_encap,
wtap_encap_description(rec->rec_header.packet_header.pkt_encap),
rec->rec_type);
switch (rec->rec_type) {
case REC_TYPE_PACKET:
/*
* XXX - write a Simple Packet Block if there's no time
* stamp or other information that doesn't appear in an
* SPB?
*/
if (!pcapng_write_enhanced_packet_block(wdh, rec, pd, err,
err_info)) {
return FALSE;
}
break;
case REC_TYPE_FT_SPECIFIC_EVENT:
case REC_TYPE_FT_SPECIFIC_REPORT:
#ifdef HAVE_PLUGINS
/*
* Do we have a handler for this block type?
*/
if (block_handlers != NULL &&
(handler = (block_handler *)g_hash_table_lookup(block_handlers,
GUINT_TO_POINTER(rec->rec_header.ft_specific_header.record_type))) != NULL) {
/* Yes. Call it to write out this record. */
if (!handler->writer(wdh, rec, pd, err))
return FALSE;
} else
#endif
{
/* No. */
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
return FALSE;
}
break;
case REC_TYPE_SYSCALL:
if (!pcapng_write_sysdig_event_block(wdh, rec, pd, err)) {
return FALSE;
}
break;
case REC_TYPE_SYSTEMD_JOURNAL_EXPORT:
if (!pcapng_write_systemd_journal_export_block(wdh, rec, pd, err)) {
return FALSE;
}
break;
case REC_TYPE_CUSTOM_BLOCK:
switch (rec->rec_header.custom_block_header.pen) {
case PEN_NFLX:
if (!pcapng_write_bblog_block(wdh, rec, pd, err)) {
return FALSE;
}
break;
default:
if (!pcapng_write_custom_block(wdh, rec, pd, err)) {
return FALSE;
}
break;
}
break;
default:
/* We don't support writing this record type. */
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
return FALSE;
}
return TRUE;
}
/* Finish writing to a dump file.
Returns TRUE on success, FALSE on failure. */
static gboolean pcapng_dump_finish(wtap_dumper *wdh, int *err,
gchar **err_info _U_)
{
guint i, j;
/* Flush any hostname resolution or decryption secrets info we may have */
if (!pcapng_write_internal_blocks(wdh, err)) {
return FALSE;
}
for (i = 0; i < wdh->interface_data->len; i++) {
/* Get the interface description */
wtap_block_t int_data;
wtapng_if_descr_mandatory_t *int_data_mand;
int_data = g_array_index(wdh->interface_data, wtap_block_t, i);
int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(int_data);
for (j = 0; j < int_data_mand->num_stat_entries; j++) {
wtap_block_t if_stats;
if_stats = g_array_index(int_data_mand->interface_statistics, wtap_block_t, j);
ws_debug("write ISB for interface %u",
((wtapng_if_stats_mandatory_t*)wtap_block_get_mandatory_data(if_stats))->interface_id);
if (!pcapng_write_interface_statistics_block(wdh, if_stats, err)) {
return FALSE;
}
}
}
ws_debug("leaving function");
return TRUE;
}
/* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
failure */
static gboolean
pcapng_dump_open(wtap_dumper *wdh, int *err, gchar **err_info _U_)
{
guint i;
ws_debug("entering function");
/* This is a pcapng file */
wdh->subtype_add_idb = pcapng_add_idb;
wdh->subtype_write = pcapng_dump;
wdh->subtype_finish = pcapng_dump_finish;
/* write the section header block */
if (!pcapng_write_section_header_block(wdh, err)) {
return FALSE;
}
ws_debug("wrote section header block.");
/* Write the Interface description blocks */
ws_debug("Number of IDBs to write (number of interfaces) %u",
wdh->interface_data->len);
for (i = 0; i < wdh->interface_data->len; i++) {
/* Get the interface description */
wtap_block_t idb;
idb = g_array_index(wdh->interface_data, wtap_block_t, i);
if (!pcapng_write_if_descr_block(wdh, idb, err)) {
return FALSE;
}
}
/* Write (optional) fixed Decryption Secrets Blocks. */
if (wdh->dsbs_initial) {
for (i = 0; i < wdh->dsbs_initial->len; i++) {
wtap_block_t dsb = g_array_index(wdh->dsbs_initial, wtap_block_t, i);
if (!pcapng_write_decryption_secrets_block(wdh, dsb, err)) {
return FALSE;
}
}
}
return TRUE;
}
/* Returns 0 if we could write the specified encapsulation type,
an error indication otherwise. */
static int pcapng_dump_can_write_encap(int wtap_encap)
{
ws_debug("encap = %d (%s)",
wtap_encap,
wtap_encap_description(wtap_encap));
/* Per-packet encapsulation is supported. */
if (wtap_encap == WTAP_ENCAP_PER_PACKET)
return 0;
/* No encapsulation type (yet) is supported. */
if (wtap_encap == WTAP_ENCAP_NONE)
return 0;
/* Is it a filetype-specific encapsulation that we support? */
if (pcapng_encap_is_ft_specific(wtap_encap)) {
return 0;
}
/* Make sure we can figure out this DLT type */
if (wtap_wtap_encap_to_pcap_encap(wtap_encap) == -1)
return WTAP_ERR_UNWRITABLE_ENCAP;
return 0;
}
/*
* Returns TRUE if the specified encapsulation type is filetype-specific
* and one that we support.
*/
gboolean pcapng_encap_is_ft_specific(int encap)
{
switch (encap) {
case WTAP_ENCAP_SYSTEMD_JOURNAL:
return TRUE;
}
return FALSE;
}
/*
* pcapng supports several block types, and supports more than one
* of them.
*
* It also supports comments for many block types, as well as other
* option types.
*/
/* Options for section blocks. */
static const struct supported_option_type section_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_SHB_HARDWARE, ONE_OPTION_SUPPORTED },
{ OPT_SHB_USERAPPL, ONE_OPTION_SUPPORTED }
};
/* Options for interface blocks. */
static const struct supported_option_type interface_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_IDB_NAME, ONE_OPTION_SUPPORTED },
{ OPT_IDB_DESCRIPTION, ONE_OPTION_SUPPORTED },
{ OPT_IDB_IP4ADDR, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_IDB_IP6ADDR, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_IDB_MACADDR, ONE_OPTION_SUPPORTED },
{ OPT_IDB_EUIADDR, ONE_OPTION_SUPPORTED },
{ OPT_IDB_SPEED, ONE_OPTION_SUPPORTED },
{ OPT_IDB_TSRESOL, ONE_OPTION_SUPPORTED },
{ OPT_IDB_TZONE, ONE_OPTION_SUPPORTED },
{ OPT_IDB_FILTER, ONE_OPTION_SUPPORTED },
{ OPT_IDB_OS, ONE_OPTION_SUPPORTED },
{ OPT_IDB_FCSLEN, ONE_OPTION_SUPPORTED },
{ OPT_IDB_TSOFFSET, ONE_OPTION_SUPPORTED },
{ OPT_IDB_HARDWARE, ONE_OPTION_SUPPORTED }
};
/* Options for name resolution blocks. */
static const struct supported_option_type name_resolution_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_NS_DNSNAME, ONE_OPTION_SUPPORTED },
{ OPT_NS_DNSIP4ADDR, ONE_OPTION_SUPPORTED },
{ OPT_NS_DNSIP6ADDR, ONE_OPTION_SUPPORTED }
};
/* Options for interface statistics blocks. */
static const struct supported_option_type interface_statistics_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_ISB_STARTTIME, ONE_OPTION_SUPPORTED },
{ OPT_ISB_ENDTIME, ONE_OPTION_SUPPORTED },
{ OPT_ISB_IFRECV, ONE_OPTION_SUPPORTED },
{ OPT_ISB_IFDROP, ONE_OPTION_SUPPORTED },
{ OPT_ISB_FILTERACCEPT, ONE_OPTION_SUPPORTED },
{ OPT_ISB_OSDROP, ONE_OPTION_SUPPORTED },
{ OPT_ISB_USRDELIV, ONE_OPTION_SUPPORTED }
};
/* Options for decryption secrets blocks. */
static const struct supported_option_type decryption_secrets_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED }
};
/* Options for Sysdig meta event blocks. */
static const struct supported_option_type sysdig_meta_events_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED }
};
/* Options for packet blocks. */
static const struct supported_option_type packet_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_PKT_FLAGS, ONE_OPTION_SUPPORTED },
{ OPT_PKT_DROPCOUNT, ONE_OPTION_SUPPORTED },
{ OPT_PKT_PACKETID, ONE_OPTION_SUPPORTED },
{ OPT_PKT_QUEUE, ONE_OPTION_SUPPORTED },
{ OPT_PKT_HASH, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_PKT_VERDICT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED }
};
/* Options for file-type-sepcific reports. */
static const struct supported_option_type ft_specific_report_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED }
};
/* Options for file-type-sepcific event. */
static const struct supported_option_type ft_specific_event_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED }
};
/* Options for systemd journal entry. */
static const struct supported_option_type systemd_journal_export_block_options_supported[] = {
{ OPT_COMMENT, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_STR_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED },
{ OPT_CUSTOM_BIN_NO_COPY, MULTIPLE_OPTIONS_SUPPORTED }
};
static const struct supported_block_type pcapng_blocks_supported[] = {
/* Multiple sections. */
{ WTAP_BLOCK_SECTION, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(section_block_options_supported) },
/* Multiple interfaces. */
{ WTAP_BLOCK_IF_ID_AND_INFO, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(interface_block_options_supported) },
/* Multiple blocks of name resolution information */
{ WTAP_BLOCK_NAME_RESOLUTION, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(name_resolution_block_options_supported) },
/* Multiple blocks of interface statistics. */
{ WTAP_BLOCK_IF_STATISTICS, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(interface_statistics_block_options_supported) },
/* Multiple blocks of decryption secrets. */
{ WTAP_BLOCK_DECRYPTION_SECRETS, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(decryption_secrets_block_options_supported) },
/* Multiple blocks of decryption secrets. */
{ WTAP_BLOCK_SYSDIG_META_EVENT, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(sysdig_meta_events_block_options_supported) },
/* And, obviously, multiple packets. */
{ WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(packet_block_options_supported) },
/* Multiple file-type specific reports (including local ones). */
{ WTAP_BLOCK_FT_SPECIFIC_REPORT, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(ft_specific_report_block_options_supported) },
/* Multiple file-type specific events (including local ones). */
{ WTAP_BLOCK_FT_SPECIFIC_EVENT, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(ft_specific_event_block_options_supported) },
/* Multiple systemd journal export records. */
{ WTAP_BLOCK_SYSTEMD_JOURNAL_EXPORT, MULTIPLE_BLOCKS_SUPPORTED, OPTION_TYPES_SUPPORTED(systemd_journal_export_block_options_supported) },
/* Multiple custom blocks. */
{ WTAP_BLOCK_CUSTOM, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED },
};
static const struct file_type_subtype_info pcapng_info = {
"Wireshark/... - pcapng", "pcapng", "pcapng", "ntar",
FALSE, BLOCKS_SUPPORTED(pcapng_blocks_supported),
pcapng_dump_can_write_encap, pcapng_dump_open, NULL
};
void register_pcapng(void)
{
pcapng_file_type_subtype = wtap_register_file_type_subtype(&pcapng_info);
wtap_register_backwards_compatibility_lua_name("PCAPNG",
pcapng_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/
|